Build with test coverage calculation.
[apps/agl-service-can-low-level.git] / tests / 8byte_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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(data, 0, 4, false);
58     fail_unless(result == 0xF,
59             "First nibble in 0x%llx was 0x%llx instead of 0xF", data, result);
60     result = get_bit_field(data, 4, 4, false);
61     fail_unless(result == 0xA,
62             "Second nibble in 0x%llx was 0x%llx instead of 0xA", data, result);
63     result = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(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 = get_bit_field(data, 12, 8, false);
103     ck_assert_int_eq(result, 0x01);
104 } END_TEST
105
106 START_TEST (test_set_wont_fit)
107 {
108     uint64_t data = 0;
109     fail_if(set_bit_field(&data, 100, 0, 1));
110 }
111 END_TEST
112
113 START_TEST (test_set_field)
114 {
115     uint64_t data = 0;
116     fail_unless(set_bit_field(&data, 1, 0, 1));
117     uint64_t result = get_bit_field(data, 0, 1, false);
118     ck_assert_int_eq(result, 0x1);
119     data = 0;
120     fail_unless(set_bit_field(&data, 1, 1, 1));
121     result = get_bit_field(data, 1, 1, false);
122     ck_assert_int_eq(result, 0x1);
123
124     data = 0;
125     fail_unless(set_bit_field(&data, 0xf, 3, 4));
126     result = get_bit_field(data, 3, 4, false);
127     ck_assert_int_eq(result, 0xf);
128 }
129 END_TEST
130
131 START_TEST (test_set_doesnt_clobber_existing_data)
132 {
133     uint64_t data = 0xFFFC4DF300000000;
134     fail_unless(set_bit_field(&data, 0x4fc8, 16, 16));
135     uint64_t result = get_bit_field(data, 16, 16, false);
136     fail_unless(result == 0x4fc8,
137             "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result,
138             0xc84f);
139
140     data = 0x8000000000000000;
141     fail_unless(set_bit_field(&data, 1, 21, 1));
142     fail_unless(data == 0x8000040000000000LLU,
143             "Expected combined value 0x8000040000000000 but got 0x%llx%llx",
144             data >> 32, data);
145 }
146 END_TEST
147
148 START_TEST (test_set_off_byte_boundary)
149 {
150     uint64_t data = 0xFFFC4DF300000000;
151     fail_unless(set_bit_field(&data, 0x12, 12, 8));
152     uint64_t result = get_bit_field(data, 12, 12, false);
153     ck_assert_int_eq(result,0x12d);
154 }
155 END_TEST
156
157 START_TEST (test_set_odd_number_of_bits)
158 {
159     uint64_t data = 0xFFFC4DF300000000LLU;
160     fail_unless(set_bit_field(&data, 0x12, 11, 5));
161     uint64_t result = get_bit_field(data, 11, 5, false);
162     fail_unless(result == 0x12,
163             "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
164             0x12);
165
166     data = 0xFFFC4DF300000000LLU;
167     fail_unless(set_bit_field(&data, 0x2, 11, 5));
168     result = get_bit_field(data, 11, 5, false);
169     fail_unless(result == 0x2,
170             "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
171             0x2);
172 }
173 END_TEST
174
175 START_TEST(test_eightbyte_get_byte)
176 {
177     uint64_t data = 0x00000000F34DFCFF;
178     uint8_t result = eightbyte_get_byte(data, 0, false);
179     uint8_t expected = 0x0;
180     ck_assert_int_eq(result, expected);
181
182     result = eightbyte_get_byte(data, 4, false);
183     expected = 0xF3;
184     ck_assert_int_eq(result, expected);
185
186     result = eightbyte_get_byte(data, 5, false);
187     expected = 0x4D;
188     ck_assert_int_eq(result, expected);
189
190     result = eightbyte_get_byte(data, 6, false);
191     expected = 0xFC;
192     ck_assert_int_eq(result, expected);
193
194     result = eightbyte_get_byte(data, 7, false);
195     expected = 0xFF;
196     ck_assert_int_eq(result, expected);
197 }
198 END_TEST
199
200 START_TEST(test_eightbyte_get_nibble)
201 {
202     uint64_t data = 0x00000000F34DFCFF;
203     uint8_t result = eightbyte_get_nibble(data, 0, false);
204     uint8_t expected = 0x0;
205     ck_assert_int_eq(result, expected);
206
207     result = eightbyte_get_nibble(data, 2, false);
208     expected = 0x0;
209     ck_assert_int_eq(result, expected);
210
211     result = eightbyte_get_nibble(data, 8, false);
212     expected = 0xF;
213     ck_assert_int_eq(result, expected);
214
215     result = eightbyte_get_nibble(data, 9, false);
216     expected = 0x3;
217     ck_assert_int_eq(result, expected);
218
219     result = eightbyte_get_nibble(data, 10, false);
220     expected = 0x4;
221     ck_assert_int_eq(result, expected);
222
223     result = eightbyte_get_nibble(data, 13, false);
224     expected = 0xC;
225     ck_assert_int_eq(result, expected);
226 }
227 END_TEST
228
229 Suite* bitfieldSuite(void) {
230     Suite* s = suite_create("bitfield");
231     TCase *tc_core = tcase_create("core");
232     tcase_add_test(tc_core, test_large_bitmask);
233     tcase_add_test(tc_core, test_one_bit);
234     tcase_add_test(tc_core, test_one_bit_not_swapped);
235     tcase_add_test(tc_core, test_one_byte);
236     tcase_add_test(tc_core, test_16_bit_parse);
237     tcase_add_test(tc_core, test_32_bit_parse);
238     tcase_add_test(tc_core, test_multi_byte);
239     tcase_add_test(tc_core, test_get_multi_byte);
240     tcase_add_test(tc_core, test_get_off_byte_boundary);
241     tcase_add_test(tc_core, test_set_wont_fit);
242     tcase_add_test(tc_core, test_set_field);
243     tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
244     tcase_add_test(tc_core, test_set_off_byte_boundary);
245     tcase_add_test(tc_core, test_set_odd_number_of_bits);
246     tcase_add_test(tc_core, test_eightbyte_get_nibble);
247     tcase_add_test(tc_core, test_eightbyte_get_byte);
248     suite_add_tcase(s, tc_core);
249
250     return s;
251 }
252
253 int main(void) {
254     int numberFailed;
255     Suite* s = bitfieldSuite();
256     SRunner *sr = srunner_create(s);
257     // Don't fork so we can actually use gdb
258     srunner_set_fork_status(sr, CK_NOFORK);
259     srunner_run_all(sr, CK_NORMAL);
260     numberFailed = srunner_ntests_failed(sr);
261     srunner_free(sr);
262     return (numberFailed == 0) ? 0 : 1;
263 }