summaryrefslogtreecommitdiffstats
path: root/tests/8byte_tests.c
blob: 140c4efb1bcf35217c9db64d30ecf166008b3230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <check.h>
#include <stdint.h>
#include <bitfield/bitfield.h>

START_TEST (test_large_bitmask)
{
    // yeah, this isn't a public method but I wanted to unit test it to track
    // down a bug
    extern uint64_t bitmask(int numBits);
    uint64_t result = bitmask(32);
    fail_if(result != 0xffffffff);
}
END_TEST

START_TEST (test_one_bit_not_swapped)
{
    uint64_t data = 0x80;
    uint64_t result = getBitField(data, 0, 1, false);
    fail_if(result == 1);
}
END_TEST

START_TEST (test_one_bit)
{
    uint64_t data = 0x8000000000000000;
    uint64_t result = getBitField(data, 0, 1, false);
    fail_unless(result == 0x1,
            "First bit in 0x%llx was 0x%llx instead of 0x1", data, result);
}
END_TEST

START_TEST (test_32_bit_parse)
{
    uint64_t data = 0x0402574d555a0401;
    uint64_t result = getBitField(data, 16, 32, false);
    uint64_t expectedValue = 0x574d555a;
    fail_unless(result == expectedValue,
            "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
            result, expectedValue);
}
END_TEST

START_TEST (test_16_bit_parse)
{
    uint64_t data = 0xF34DFCFF00000000;
    uint64_t result = getBitField(data, 16, 16, false);
    uint64_t expectedValue = 0xFCFF;
    fail_unless(result == expectedValue,
            "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
            result, expectedValue);
}
END_TEST

START_TEST (test_one_byte)
{
    uint64_t data = 0xFA00000000000000;
    uint64_t result = getBitField(data, 0, 4, false);
    fail_unless(result == 0xF,
            "First 4 bits in 0x%llx was 0x%llx instead of 0xF", data, result);
    result = getBitField(data, 4, 4, false);
    fail_unless(result == 0xA,
            "First 4 bits in 0x%llx was 0x%llx instead of 0xA", data, result);
    result = getBitField(data, 0, 8, false);
    fail_unless(result == 0xFA,
            "All bits in 0x%llx were 0x%llx instead of 0x%llx", data, result, data);
}
END_TEST

START_TEST (test_multi_byte)
{
    uint64_t data = 0x12FA000000000000;
    uint64_t result = getBitField(data, 0, 4, false);
    fail_unless(result == 0x1,
            "First 4 bits in 0x%llx was 0x%llx instead of 0xF", (data >> 60) & 0xF,
            result);
    result = getBitField(data, 4, 4, false);
    fail_unless(result == 0x2,
            "Second 4 bits in 0x%llx was 0x%llx instead of 0xA", (data >> 56) & 0xF,
            result);
    result = getBitField(data, 8, 4, false);
    fail_unless(result == 0xF,
            "First 4 bits in 0x%llx was 0x%llx instead of 0x1", (data >> 52) & 0xF,
            result);
    result = getBitField(data, 12, 4, false);
    fail_unless(result == 0xA,
            "Second 4 bits in 0x%llx was 0x%llx instead of 0x2", (data >> 48) % 0xF,
            result);
}
END_TEST

START_TEST (test_get_multi_byte)
{
    uint64_t data = 0x12FA000000000000;
    uint64_t result = getBitField(data, 0, 9, false);
    ck_assert_int_eq(result, 0x25);
}
END_TEST

START_TEST (test_get_off_byte_boundary)
{
    uint64_t data = 0x000012FA00000000;
    uint64_t result = getBitField(data, 12, 8, false);
    ck_assert_int_eq(result, 0x01);
} END_TEST

START_TEST (test_set_field)
{
    uint64_t data = 0;
    setBitField(&data, 1, 0, 1);
    uint64_t result = getBitField(data, 0, 1, false);
    ck_assert_int_eq(result, 0x1);
    data = 0;
    setBitField(&data, 1, 1, 1);
    result = getBitField(data, 1, 1, false);
    ck_assert_int_eq(result, 0x1);

    data = 0;
    setBitField(&data, 0xf, 3, 4);
    result = getBitField(data, 3, 4, false);
    ck_assert_int_eq(result, 0xf);
}
END_TEST

START_TEST (test_set_doesnt_clobber_existing_data)
{
    uint64_t data = 0xFFFC4DF300000000;
    setBitField(&data, 0x4fc8, 16, 16);
    uint64_t result = getBitField(data, 16, 16, false);
    fail_unless(result == 0x4fc8,
            "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result,
            0xc84f);

    data = 0x8000000000000000;
    setBitField(&data, 1, 21, 1);
    fail_unless(data == 0x8000040000000000LLU,
            "Expected combined value 0x8000040000000000 but got 0x%llx%llx",
            data >> 32, data);
}
END_TEST

START_TEST (test_set_off_byte_boundary)
{
    uint64_t data = 0xFFFC4DF300000000;
    setBitField(&data, 0x12, 12, 8);
    uint64_t result = getBitField(data, 12, 12, false);
    ck_assert_int_eq(result,0x12d);
}
END_TEST

START_TEST (test_set_odd_number_of_bits)
{
    uint64_t data = 0xFFFC4DF300000000LLU;
    setBitField(&data, 0x12, 11, 5);
    uint64_t result = getBitField(data, 11, 5, false);
    fail_unless(result == 0x12,
            "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
            0x12);

    data = 0xFFFC4DF300000000LLU;
    setBitField(&data, 0x2, 11, 5);
    result = getBitField(data, 11, 5, false);
    fail_unless(result == 0x2,
            "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
            0x2);
}
END_TEST

START_TEST(test_nth_byte)
{
    uint64_t data = 0x00000000F34DFCFF;
    uint8_t result = nthByte(data, 0);
    uint8_t expected = 0x0;
    ck_assert_int_eq(result, expected);

    result = nthByte(data, 4);
    expected = 0xF3;
    ck_assert_int_eq(result, expected);

    result = nthByte(data, 5);
    expected = 0x4D;
    ck_assert_int_eq(result, expected);

    result = nthByte(data, 6);
    expected = 0xFC;
    ck_assert_int_eq(result, expected);

    result = nthByte(data, 7);
    expected = 0xFF;
    ck_assert_int_eq(result, expected);
}
END_TEST

Suite* bitfieldSuite(void) {
    Suite* s = suite_create("bitfield");
    TCase *tc_core = tcase_create("core");
    tcase_add_test(tc_core, test_large_bitmask);
    tcase_add_test(tc_core, test_one_bit);
    tcase_add_test(tc_core, test_one_bit_not_swapped);
    tcase_add_test(tc_core, test_one_byte);
    tcase_add_test(tc_core, test_16_bit_parse);
    tcase_add_test(tc_core, test_32_bit_parse);
    tcase_add_test(tc_core, test_multi_byte);
    tcase_add_test(tc_core, test_get_multi_byte);
    tcase_add_test(tc_core, test_get_off_byte_boundary);
    tcase_add_test(tc_core, test_set_field);
    tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
    tcase_add_test(tc_core, test_set_off_byte_boundary);
    tcase_add_test(tc_core, test_set_odd_number_of_bits);
    tcase_add_test(tc_core, test_nth_byte);
    suite_add_tcase(s, tc_core);

    return s;
}

int main(void) {
    int numberFailed;
    Suite* s = bitfieldSuite();
    SRunner *sr = srunner_create(s);
    // Don't fork so we can actually use gdb
    srunner_set_fork_status(sr, CK_NOFORK);
    srunner_run_all(sr, CK_NORMAL);
    numberFailed = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (numberFailed == 0) ? 0 : 1;
}