Draft work making the bitfield functions more generic.
[apps/agl-service-can-low-level.git] / tests / bitfield_tests.c
1 #include <check.h>
2 #include <stdint.h>
3 #include <bitfield/bitfield.h>
4
5 START_TEST (test_large_bitmask)
6 {
7     // yeah, this isn't a public method but I wanted to unit test it to track
8     // down a bug
9     extern uint64_t bitmask(int numBits);
10     uint64_t result = bitmask(32);
11     fail_if(result != 0xffffffff);
12 }
13 END_TEST
14
15 START_TEST (test_one_bit_not_swapped)
16 {
17     uint64_t data = 0x80;
18     uint64_t result = getBitField(data, 0, 1, false);
19     fail_if(result == 1);
20 }
21 END_TEST
22
23 START_TEST (test_one_bit)
24 {
25     uint64_t data = 0x8000000000000000;
26     uint64_t result = getBitField(data, 0, 1, false);
27     fail_unless(result == 0x1,
28             "First bits in 0x%X was 0x%X instead of 0x1", data, result);
29 }
30 END_TEST
31
32 START_TEST (test_32_bit_parse)
33 {
34     uint64_t data = 0x0402574d555a0401;
35     uint64_t result = getBitField(data, 16, 32, false);
36     uint64_t expectedValue = 0x574d555a;
37     fail_unless(result == expectedValue,
38             "Field retrieved in 0x%X was 0x%X instead of %d", data,
39             result, expectedValue);
40 }
41 END_TEST
42
43 START_TEST (test_16_bit_parse)
44 {
45     uint64_t data = 0xF34DFCFF00000000;
46     uint64_t result = getBitField(data, 16, 16, false);
47     uint64_t expectedValue = 0xFCFF;
48     fail_unless(result == expectedValue,
49             "Field retrieved in 0x%X was 0x%X instead of %d", data,
50             result, expectedValue);
51 }
52 END_TEST
53
54 START_TEST (test_one_byte)
55 {
56     uint64_t data = 0xFA00000000000000;
57     uint64_t result = getBitField(data, 0, 4, false);
58     fail_unless(result == 0xF,
59             "First 4 bits in 0x%X was 0x%X instead of 0xF", data, result);
60     result = getBitField(data, 4, 4, false);
61     fail_unless(result == 0xA,
62             "First 4 bits in 0x%X was 0x%X instead of 0xA", data, result);
63     result = getBitField(data, 0, 8, false);
64     fail_unless(result == 0xFA,
65             "All bits in 0x%X were 0x%X instead of 0x%X", data, result, data);
66 }
67 END_TEST
68
69 START_TEST (test_multi_byte)
70 {
71     uint64_t data = 0x12FA000000000000;
72     uint64_t result = getBitField(data, 0, 4, false);
73     fail_unless(result == 0x1,
74             "First 4 bits in 0x%X was 0x%X instead of 0xF", (data >> 60) & 0xF,
75             result);
76     result = getBitField(data, 4, 4, false);
77     fail_unless(result == 0x2,
78             "Second 4 bits in 0x%X was %d instead of 0xA", (data >> 56) & 0xF,
79             result);
80     result = getBitField(data, 8, 4, false);
81     fail_unless(result == 0xF,
82             "First 4 bits in 0x%X was %d instead of 0x1", (data >> 52) & 0xF,
83             result);
84     result = getBitField(data, 12, 4, false);
85     fail_unless(result == 0xA,
86             "Second 4 bits in 0x%X was %d instead of 0x2", (data >> 48) % 0xF,
87             result);
88 }
89 END_TEST
90
91 START_TEST (test_get_multi_byte)
92 {
93     uint64_t data = 0x12FA000000000000;
94     uint64_t result = getBitField(data, 0, 9, false);
95     ck_assert_int_eq(result, 0x25);
96 }
97 END_TEST
98
99 START_TEST (test_get_off_byte_boundary)
100 {
101     uint64_t data = 0x000012FA00000000;
102     uint64_t result = getBitField(data, 12, 8, false);
103     ck_assert_int_eq(result, 0x01);
104 } END_TEST
105
106 START_TEST (test_set_field)
107 {
108     uint64_t data = 0;
109     setBitField(&data, 1, 0, 1);
110     uint64_t result = getBitField(data, 0, 1, false);
111     ck_assert_int_eq(result, 0x1);
112     data = 0;
113     setBitField(&data, 1, 1, 1);
114     result = getBitField(data, 1, 1, false);
115     ck_assert_int_eq(result, 0x1);
116
117     data = 0;
118     setBitField(&data, 0xf, 3, 4);
119     result = getBitField(data, 3, 4, false);
120     ck_assert_int_eq(result, 0xf);
121 }
122 END_TEST
123
124 START_TEST (test_set_doesnt_clobber_existing_data)
125 {
126     uint64_t data = 0xFFFC4DF300000000;
127     setBitField(&data, 0x4fc8, 16, 16);
128     uint64_t result = getBitField(data, 16, 16, false);
129     fail_unless(result == 0x4fc8,
130             "Field retrieved in 0x%X was 0x%X instead of 0x%X", data, result,
131             0xc84f);
132
133     data = 0x8000000000000000;
134     setBitField(&data, 1, 21, 1);
135     fail_unless(data == 0x8000040000000000LLU,
136             "Expected combined value 0x8000040000000000 but got 0x%X%X",
137             data >> 32, data);
138 }
139 END_TEST
140
141 START_TEST (test_set_off_byte_boundary)
142 {
143     uint64_t data = 0xFFFC4DF300000000;
144     setBitField(&data, 0x12, 12, 8);
145     uint64_t result = getBitField(data, 12, 12, false);
146     ck_assert_int_eq(result,0x12d);
147 }
148 END_TEST
149
150 START_TEST (test_set_odd_number_of_bits)
151 {
152     uint64_t data = 0xFFFC4DF300000000LLU;
153     setBitField(&data, 0x12, 11, 5);
154     uint64_t result = getBitField(data, 11, 5, false);
155     fail_unless(result == 0x12,
156             "Field set in 0x%X%X%X%X was %d instead of %d", data, result,
157             0x12);
158
159     data = 0xFFFC4DF300000000LLU;
160     setBitField(&data, 0x2, 11, 5);
161     result = getBitField(data, 11, 5, false);
162     fail_unless(result == 0x2,
163             "Field set in 0x%X%X%X%X was %d instead of %d", data, result,
164             0x2);
165 }
166 END_TEST
167
168 START_TEST(test_nth_byte)
169 {
170     uint64_t data = 0x00000000F34DFCFF;
171     uint8_t result = nthByte(data, 0);
172     uint8_t expected = 0x0;
173     ck_assert_int_eq(result, expected);
174
175     result = nthByte(data, 4);
176     expected = 0xF3;
177     ck_assert_int_eq(result, expected);
178
179     result = nthByte(data, 5);
180     expected = 0x4D;
181     ck_assert_int_eq(result, expected);
182
183     result = nthByte(data, 6);
184     expected = 0xFC;
185     ck_assert_int_eq(result, expected);
186
187     result = nthByte(data, 7);
188     expected = 0xFF;
189     ck_assert_int_eq(result, expected);
190 }
191 END_TEST
192
193 START_TEST (test_get_byte)
194 {
195     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
196     uint8_t result = getByte(0, data, sizeof(data), ENDIANNESS_BIG_ENDIAN);
197     ck_assert_int_eq(result, 0x12);
198     result = getByte(3, data, sizeof(data), ENDIANNESS_BIG_ENDIAN);
199     ck_assert_int_eq(result, 0x78);
200 }
201 END_TEST
202
203 START_TEST (test_get_nibble)
204 {
205     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
206     uint8_t result = getNibble(0, data, sizeof(data), ENDIANNESS_BIG_ENDIAN);
207     ck_assert_int_eq(result, 0x1);
208     result = getNibble(1, data, sizeof(data), ENDIANNESS_BIG_ENDIAN);
209     ck_assert_int_eq(result, 0x2);
210     result = getNibble(2, data, sizeof(data), ENDIANNESS_BIG_ENDIAN);
211     ck_assert_int_eq(result, 0x3);
212 }
213 END_TEST
214
215 START_TEST (test_get_bits)
216 {
217     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
218     uint8_t result[4];
219     getBits(0, 16, data, 36, ENDIANNESS_BIG_ENDIAN, result);
220     ck_assert_int_eq(result[0], 0x12);
221     ck_assert_int_eq(result[1], 0x34);
222 }
223 END_TEST
224
225 START_TEST (test_get_uneven_bits)
226 {
227     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
228     uint8_t result[4] = {0};
229     getBits(4, 12, data, 36, ENDIANNESS_BIG_ENDIAN, result);
230     ck_assert_int_eq(result[0], 0x2);
231     ck_assert_int_eq(result[1], 0x34);
232 }
233 END_TEST
234
235 Suite* bitfieldSuite(void) {
236     Suite* s = suite_create("bitfield");
237     TCase *tc_core = tcase_create("core");
238     tcase_add_test(tc_core, test_large_bitmask);
239     tcase_add_test(tc_core, test_one_bit);
240     tcase_add_test(tc_core, test_one_bit_not_swapped);
241     tcase_add_test(tc_core, test_one_byte);
242     tcase_add_test(tc_core, test_16_bit_parse);
243     tcase_add_test(tc_core, test_32_bit_parse);
244     tcase_add_test(tc_core, test_multi_byte);
245     tcase_add_test(tc_core, test_get_multi_byte);
246     tcase_add_test(tc_core, test_get_off_byte_boundary);
247     tcase_add_test(tc_core, test_set_field);
248     tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
249     tcase_add_test(tc_core, test_set_off_byte_boundary);
250     tcase_add_test(tc_core, test_set_odd_number_of_bits);
251     tcase_add_test(tc_core, test_nth_byte);
252     tcase_add_test(tc_core, test_get_byte);
253     tcase_add_test(tc_core, test_get_nibble);
254     tcase_add_test(tc_core, test_get_bits);
255     tcase_add_test(tc_core, test_get_uneven_bits);
256     suite_add_tcase(s, tc_core);
257
258     return s;
259 }
260
261 int main(void) {
262     int numberFailed;
263     Suite* s = bitfieldSuite();
264     SRunner *sr = srunner_create(s);
265     // Don't fork so we can actually use gdb
266     srunner_set_fork_status(sr, CK_NOFORK);
267     srunner_run_all(sr, CK_NORMAL);
268     numberFailed = srunner_ntests_failed(sr);
269     srunner_free(sr);
270     return (numberFailed == 0) ? 0 : 1;
271 }