3 #include <bitfield/bitfield.h>
5 START_TEST (test_large_bitmask)
7 // yeah, this isn't a public method but I wanted to unit test it to track
9 extern uint64_t bitmask(int numBits);
10 uint64_t result = bitmask(32);
11 fail_if(result != 0xffffffff);
15 START_TEST (test_one_bit_not_swapped)
18 uint64_t result = get_bit_field(data, 0, 1, false);
23 START_TEST (test_one_bit)
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);
32 START_TEST (test_32_bit_parse)
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);
43 START_TEST (test_16_bit_parse)
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);
54 START_TEST (test_one_byte)
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);
69 START_TEST (test_multi_byte)
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,
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,
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,
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,
91 START_TEST (test_get_multi_byte)
93 uint64_t data = 0x12FA000000000000;
94 uint64_t result = get_bit_field(data, 0, 9, false);
95 ck_assert_int_eq(result, 0x25);
99 START_TEST (test_get_off_byte_boundary)
101 uint64_t data = 0x000012FA00000000;
102 uint64_t result = get_bit_field(data, 12, 8, false);
103 ck_assert_int_eq(result, 0x01);
106 START_TEST (test_set_wont_fit)
109 fail_if(set_bit_field(&data, 100, 0, 1));
113 START_TEST (test_set_field)
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);
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);
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);
131 START_TEST (test_set_doesnt_clobber_existing_data)
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,
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",
148 START_TEST (test_set_off_byte_boundary)
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);
157 START_TEST (test_set_odd_number_of_bits)
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,
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,
175 START_TEST(test_eightbyte_get_byte)
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);
182 result = eightbyte_get_byte(data, 4, false);
184 ck_assert_int_eq(result, expected);
186 result = eightbyte_get_byte(data, 5, false);
188 ck_assert_int_eq(result, expected);
190 result = eightbyte_get_byte(data, 6, false);
192 ck_assert_int_eq(result, expected);
194 result = eightbyte_get_byte(data, 7, false);
196 ck_assert_int_eq(result, expected);
200 START_TEST(test_eightbyte_get_nibble)
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);
207 result = eightbyte_get_nibble(data, 2, false);
209 ck_assert_int_eq(result, expected);
211 result = eightbyte_get_nibble(data, 8, false);
213 ck_assert_int_eq(result, expected);
215 result = eightbyte_get_nibble(data, 9, false);
217 ck_assert_int_eq(result, expected);
219 result = eightbyte_get_nibble(data, 10, false);
221 ck_assert_int_eq(result, expected);
223 result = eightbyte_get_nibble(data, 13, false);
225 ck_assert_int_eq(result, expected);
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);
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);
262 return (numberFailed == 0) ? 0 : 1;