Standardize argument ordering for bitfield functions.
[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 bit in 0x%llx was 0x%llx 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%llx was 0x%llx instead of 0x%llx", 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%llx was 0x%llx instead of 0x%llx", 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%llx was 0x%llx instead of 0xF", data, result);
60     result = getBitField(data, 4, 4, false);
61     fail_unless(result == 0xA,
62             "First 4 bits in 0x%llx was 0x%llx instead of 0xA", data, result);
63     result = getBitField(data, 0, 8, false);
64     fail_unless(result == 0xFA,
65             "All bits in 0x%llx were 0x%llx instead of 0x%llx", 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%llx was 0x%llx 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%llx was 0x%llx 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%llx was 0x%llx 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%llx was 0x%llx 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%llx was 0x%llx 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%llx%llx",
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%llx%llx%llx%llx was 0x%llx instead of 0x%llx", 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%llx%llx%llx%llx was 0x%llx instead of 0x%llx", 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(data, sizeof(data), 0);
197     ck_assert_int_eq(result, 0x12);
198     result = getByte(data, sizeof(data), 3);
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(data, sizeof(data), 0);
207     ck_assert_int_eq(result, 0x1);
208     result = getNibble(data, sizeof(data), 1);
209     ck_assert_int_eq(result, 0x2);
210     result = getNibble(data, sizeof(data), 2);
211     ck_assert_int_eq(result, 0x3);
212 }
213 END_TEST
214
215 START_TEST (test_get_bits_out_of_range)
216 {
217     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
218     uint8_t result[4];
219     fail_if(copyBitsRightAligned(data, 4, 25, 16, result, 4));
220 }
221 END_TEST
222
223 START_TEST (test_get_bits)
224 {
225     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
226     uint8_t result[4] = {0};
227     fail_unless(copyBitsRightAligned(data, 4, 0, 16, result, 4));
228     ck_assert_int_eq(result[0], 0x12);
229     ck_assert_int_eq(result[1], 0x34);
230 }
231 END_TEST
232
233 START_TEST (test_get_uneven_bits)
234 {
235     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
236     uint8_t result[4] = {0};
237     fail_unless(copyBitsRightAligned(data, 4, 4, 12, result, 4));
238     ck_assert_int_eq(result[0], 0x2);
239     ck_assert_int_eq(result[1], 0x34);
240 }
241 END_TEST
242
243 Suite* bitfieldSuite(void) {
244     Suite* s = suite_create("bitfield");
245     TCase *tc_core = tcase_create("core");
246     tcase_add_test(tc_core, test_large_bitmask);
247     tcase_add_test(tc_core, test_one_bit);
248     tcase_add_test(tc_core, test_one_bit_not_swapped);
249     tcase_add_test(tc_core, test_one_byte);
250     tcase_add_test(tc_core, test_16_bit_parse);
251     tcase_add_test(tc_core, test_32_bit_parse);
252     tcase_add_test(tc_core, test_multi_byte);
253     tcase_add_test(tc_core, test_get_multi_byte);
254     tcase_add_test(tc_core, test_get_off_byte_boundary);
255     tcase_add_test(tc_core, test_set_field);
256     tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
257     tcase_add_test(tc_core, test_set_off_byte_boundary);
258     tcase_add_test(tc_core, test_set_odd_number_of_bits);
259     tcase_add_test(tc_core, test_nth_byte);
260     tcase_add_test(tc_core, test_get_byte);
261     tcase_add_test(tc_core, test_get_nibble);
262     tcase_add_test(tc_core, test_get_bits);
263     tcase_add_test(tc_core, test_get_bits_out_of_range);
264     tcase_add_test(tc_core, test_get_uneven_bits);
265     suite_add_tcase(s, tc_core);
266
267     return s;
268 }
269
270 int main(void) {
271     int numberFailed;
272     Suite* s = bitfieldSuite();
273     SRunner *sr = srunner_create(s);
274     // Don't fork so we can actually use gdb
275     srunner_set_fork_status(sr, CK_NOFORK);
276     srunner_run_all(sr, CK_NORMAL);
277     numberFailed = srunner_ntests_failed(sr);
278     srunner_free(sr);
279     return (numberFailed == 0) ? 0 : 1;
280 }