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 = getBitField(data, 0, 1, false);
23 START_TEST (test_one_bit)
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);
32 START_TEST (test_32_bit_parse)
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);
43 START_TEST (test_16_bit_parse)
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);
54 START_TEST (test_one_byte)
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);
69 START_TEST (test_multi_byte)
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,
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,
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,
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,
91 START_TEST (test_get_multi_byte)
93 uint64_t data = 0x12FA000000000000;
94 uint64_t result = getBitField(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 = getBitField(data, 12, 8, false);
103 ck_assert_int_eq(result, 0x01);
106 START_TEST (test_set_field)
109 setBitField(&data, 1, 0, 1);
110 uint64_t result = getBitField(data, 0, 1, false);
111 ck_assert_int_eq(result, 0x1);
113 setBitField(&data, 1, 1, 1);
114 result = getBitField(data, 1, 1, false);
115 ck_assert_int_eq(result, 0x1);
118 setBitField(&data, 0xf, 3, 4);
119 result = getBitField(data, 3, 4, false);
120 ck_assert_int_eq(result, 0xf);
124 START_TEST (test_set_doesnt_clobber_existing_data)
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,
133 data = 0x8000000000000000;
134 setBitField(&data, 1, 21, 1);
135 fail_unless(data == 0x8000040000000000LLU,
136 "Expected combined value 0x8000040000000000 but got 0x%llx%llx",
141 START_TEST (test_set_off_byte_boundary)
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);
150 START_TEST (test_set_odd_number_of_bits)
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,
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,
168 START_TEST(test_nth_byte)
170 uint64_t data = 0x00000000F34DFCFF;
171 uint8_t result = nthByte(data, 0);
172 uint8_t expected = 0x0;
173 ck_assert_int_eq(result, expected);
175 result = nthByte(data, 4);
177 ck_assert_int_eq(result, expected);
179 result = nthByte(data, 5);
181 ck_assert_int_eq(result, expected);
183 result = nthByte(data, 6);
185 ck_assert_int_eq(result, expected);
187 result = nthByte(data, 7);
189 ck_assert_int_eq(result, expected);
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);
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);
224 return (numberFailed == 0) ? 0 : 1;