Split up 8 byte wrappers from generic bit array functions.
[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 = 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 Suite* bitfieldSuite(void) {
194     Suite* s = suite_create("bitfield");
195     TCase *tc_core = tcase_create("core");
196     tcase_add_test(tc_core, test_large_bitmask);
197     tcase_add_test(tc_core, test_one_bit);
198     tcase_add_test(tc_core, test_one_bit_not_swapped);
199     tcase_add_test(tc_core, test_one_byte);
200     tcase_add_test(tc_core, test_16_bit_parse);
201     tcase_add_test(tc_core, test_32_bit_parse);
202     tcase_add_test(tc_core, test_multi_byte);
203     tcase_add_test(tc_core, test_get_multi_byte);
204     tcase_add_test(tc_core, test_get_off_byte_boundary);
205     tcase_add_test(tc_core, test_set_field);
206     tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
207     tcase_add_test(tc_core, test_set_off_byte_boundary);
208     tcase_add_test(tc_core, test_set_odd_number_of_bits);
209     tcase_add_test(tc_core, test_nth_byte);
210     suite_add_tcase(s, tc_core);
211
212     return s;
213 }
214
215 int main(void) {
216     int numberFailed;
217     Suite* s = bitfieldSuite();
218     SRunner *sr = srunner_create(s);
219     // Don't fork so we can actually use gdb
220     srunner_set_fork_status(sr, CK_NOFORK);
221     srunner_run_all(sr, CK_NORMAL);
222     numberFailed = srunner_ntests_failed(sr);
223     srunner_free(sr);
224     return (numberFailed == 0) ? 0 : 1;
225 }