3 #include <bitfield/bitfield.h>
4 #include <bitfield/8byte.h>
6 START_TEST (test_large_bitmask)
8 uint64_t result = bitmask(32);
9 fail_if(result != 0xffffffff);
13 START_TEST (test_one_bit_not_swapped)
16 uint64_t result = eightbyte_get_bitfield(data, 0, 1, false);
21 START_TEST (test_one_bit)
23 uint64_t data = 0x8000000000000000;
24 uint64_t result = eightbyte_get_bitfield(data, 0, 1, false);
25 fail_unless(result == 0x1,
26 "First bit in 0x%llx was 0x%llx instead of 0x1", data, result);
30 START_TEST (test_32_bit_parse)
32 uint64_t data = 0x0402574d555a0401;
33 uint64_t result = eightbyte_get_bitfield(data, 16, 32, false);
34 uint64_t expectedValue = 0x574d555a;
35 fail_unless(result == expectedValue,
36 "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
37 result, expectedValue);
41 START_TEST (test_16_bit_parse)
43 uint64_t data = 0xF34DFCFF00000000;
44 uint64_t result = eightbyte_get_bitfield(data, 16, 16, false);
45 uint64_t expectedValue = 0xFCFF;
46 fail_unless(result == expectedValue,
47 "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
48 result, expectedValue);
52 START_TEST (test_one_byte)
54 uint64_t data = 0xFA00000000000000;
55 uint64_t result = eightbyte_get_bitfield(data, 0, 4, false);
56 fail_unless(result == 0xF,
57 "First nibble in 0x%llx was 0x%llx instead of 0xF", data, result);
58 result = eightbyte_get_bitfield(data, 4, 4, false);
59 fail_unless(result == 0xA,
60 "Second nibble in 0x%llx was 0x%llx instead of 0xA", data, result);
61 result = eightbyte_get_bitfield(data, 0, 8, false);
62 fail_unless(result == 0xFA,
63 "All bits in 0x%llx were 0x%llx instead of 0x%llx", data, result, data);
67 START_TEST (test_multi_byte)
69 uint64_t data = 0x12FA000000000000;
70 uint64_t result = eightbyte_get_bitfield(data, 0, 4, false);
71 fail_unless(result == 0x1,
72 "First 4 bits in 0x%llx was 0x%llx instead of 0xF", (data >> 60) & 0xF,
74 result = eightbyte_get_bitfield(data, 4, 4, false);
75 fail_unless(result == 0x2,
76 "Second 4 bits in 0x%llx was 0x%llx instead of 0xA", (data >> 56) & 0xF,
78 result = eightbyte_get_bitfield(data, 8, 4, false);
79 fail_unless(result == 0xF,
80 "First 4 bits in 0x%llx was 0x%llx instead of 0x1", (data >> 52) & 0xF,
82 result = eightbyte_get_bitfield(data, 12, 4, false);
83 fail_unless(result == 0xA,
84 "Second 4 bits in 0x%llx was 0x%llx instead of 0x2", (data >> 48) % 0xF,
89 START_TEST (test_get_multi_byte)
91 uint64_t data = 0x12FA000000000000;
92 uint64_t result = eightbyte_get_bitfield(data, 0, 9, false);
93 ck_assert_int_eq(result, 0x25);
97 START_TEST (test_get_off_byte_boundary)
99 uint64_t data = 0x000012FA00000000;
100 uint64_t result = eightbyte_get_bitfield(data, 12, 8, false);
101 ck_assert_int_eq(result, 0x01);
104 START_TEST (test_set_wont_fit)
107 fail_if(eightbyte_set_bitfield(100, 0, 1, &data));
111 START_TEST (test_set_field)
114 fail_unless(eightbyte_set_bitfield(1, 0, 1, &data));
115 uint64_t result = eightbyte_get_bitfield(data, 0, 1, false);
116 ck_assert_int_eq(result, 0x1);
118 fail_unless(eightbyte_set_bitfield(1, 1, 1, &data));
119 result = eightbyte_get_bitfield(data, 1, 1, false);
120 ck_assert_int_eq(result, 0x1);
123 fail_unless(eightbyte_set_bitfield(0xf, 3, 4, &data));
124 result = eightbyte_get_bitfield(data, 3, 4, false);
125 ck_assert_int_eq(result, 0xf);
129 START_TEST (test_set_doesnt_clobber_existing_data)
131 uint64_t data = 0xFFFC4DF300000000;
132 fail_unless(eightbyte_set_bitfield(0x4fc8, 16, 16, &data));
133 uint64_t result = eightbyte_get_bitfield(data, 16, 16, false);
134 fail_unless(result == 0x4fc8,
135 "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result,
138 data = 0x8000000000000000;
139 fail_unless(eightbyte_set_bitfield(1, 21, 1, &data));
140 fail_unless(data == 0x8000040000000000LLU,
141 "Expected combined value 0x8000040000000000 but got 0x%llx%llx",
146 START_TEST (test_set_off_byte_boundary)
148 uint64_t data = 0xFFFC4DF300000000;
149 fail_unless(eightbyte_set_bitfield(0x12, 12, 8, &data));
150 uint64_t result = eightbyte_get_bitfield(data, 12, 12, false);
151 ck_assert_int_eq(result,0x12d);
155 START_TEST (test_set_odd_number_of_bits)
157 uint64_t data = 0xFFFC4DF300000000LLU;
158 fail_unless(eightbyte_set_bitfield(0x12, 11, 5, &data));
159 uint64_t result = eightbyte_get_bitfield(data, 11, 5, false);
160 fail_unless(result == 0x12,
161 "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
164 data = 0xFFFC4DF300000000LLU;
165 fail_unless(eightbyte_set_bitfield(0x2, 11, 5, &data));
166 result = eightbyte_get_bitfield(data, 11, 5, false);
167 fail_unless(result == 0x2,
168 "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
173 START_TEST(test_eightbyte_get_byte)
175 uint64_t data = 0x00000000F34DFCFF;
176 uint8_t result = eightbyte_get_byte(data, 0, false);
177 uint8_t expected = 0x0;
178 ck_assert_int_eq(result, expected);
180 result = eightbyte_get_byte(data, 4, false);
182 ck_assert_int_eq(result, expected);
184 result = eightbyte_get_byte(data, 5, false);
186 ck_assert_int_eq(result, expected);
188 result = eightbyte_get_byte(data, 6, false);
190 ck_assert_int_eq(result, expected);
192 result = eightbyte_get_byte(data, 7, false);
194 ck_assert_int_eq(result, expected);
198 START_TEST(test_eightbyte_get_nibble)
200 uint64_t data = 0x00000000F34DFCFF;
201 uint8_t result = eightbyte_get_nibble(data, 0, false);
202 uint8_t expected = 0x0;
203 ck_assert_int_eq(result, expected);
205 result = eightbyte_get_nibble(data, 2, false);
207 ck_assert_int_eq(result, expected);
209 result = eightbyte_get_nibble(data, 8, false);
211 ck_assert_int_eq(result, expected);
213 result = eightbyte_get_nibble(data, 9, false);
215 ck_assert_int_eq(result, expected);
217 result = eightbyte_get_nibble(data, 10, false);
219 ck_assert_int_eq(result, expected);
221 result = eightbyte_get_nibble(data, 13, false);
223 ck_assert_int_eq(result, expected);
227 Suite* bitfieldSuite(void) {
228 Suite* s = suite_create("bitfield");
229 TCase *tc_core = tcase_create("core");
230 tcase_add_test(tc_core, test_large_bitmask);
231 tcase_add_test(tc_core, test_one_bit);
232 tcase_add_test(tc_core, test_one_bit_not_swapped);
233 tcase_add_test(tc_core, test_one_byte);
234 tcase_add_test(tc_core, test_16_bit_parse);
235 tcase_add_test(tc_core, test_32_bit_parse);
236 tcase_add_test(tc_core, test_multi_byte);
237 tcase_add_test(tc_core, test_get_multi_byte);
238 tcase_add_test(tc_core, test_get_off_byte_boundary);
239 tcase_add_test(tc_core, test_set_wont_fit);
240 tcase_add_test(tc_core, test_set_field);
241 tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data);
242 tcase_add_test(tc_core, test_set_off_byte_boundary);
243 tcase_add_test(tc_core, test_set_odd_number_of_bits);
244 tcase_add_test(tc_core, test_eightbyte_get_nibble);
245 tcase_add_test(tc_core, test_eightbyte_get_byte);
246 suite_add_tcase(s, tc_core);
253 Suite* s = bitfieldSuite();
254 SRunner *sr = srunner_create(s);
255 // Don't fork so we can actually use gdb
256 srunner_set_fork_status(sr, CK_NOFORK);
257 srunner_run_all(sr, CK_NORMAL);
258 numberFailed = srunner_ntests_failed(sr);
260 return (numberFailed == 0) ? 0 : 1;