3 #include <bitfield/bitfield.h>
5 START_TEST (test_get_byte)
7 uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
8 uint8_t result = getByte(data, sizeof(data), 0);
9 ck_assert_int_eq(result, 0x12);
10 result = getByte(data, sizeof(data), 3);
11 ck_assert_int_eq(result, 0x78);
15 START_TEST (test_get_nibble)
17 uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
18 uint8_t result = getNibble(data, sizeof(data), 0);
19 ck_assert_int_eq(result, 0x1);
20 result = getNibble(data, sizeof(data), 1);
21 ck_assert_int_eq(result, 0x2);
22 result = getNibble(data, sizeof(data), 2);
23 ck_assert_int_eq(result, 0x3);
27 START_TEST (test_get_bits_out_of_range)
29 uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
31 fail_if(copyBitsRightAligned(data, 4, 25, 16, result, 4));
35 START_TEST (test_get_bits)
37 uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
38 uint8_t result[4] = {0};
39 fail_unless(copyBitsRightAligned(data, 4, 0, 16, result, 4));
40 ck_assert_int_eq(result[0], 0x12);
41 ck_assert_int_eq(result[1], 0x34);
45 START_TEST (test_get_uneven_bits)
47 uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
48 uint8_t result[4] = {0};
49 fail_unless(copyBitsRightAligned(data, 4, 4, 12, result, 4));
50 ck_assert_int_eq(result[0], 0x2);
51 ck_assert_int_eq(result[1], 0x34);
55 Suite* bitfieldSuite(void) {
56 Suite* s = suite_create("bitfield");
57 TCase *tc_core = tcase_create("core");
58 tcase_add_test(tc_core, test_get_byte);
59 tcase_add_test(tc_core, test_get_nibble);
60 tcase_add_test(tc_core, test_get_bits);
61 tcase_add_test(tc_core, test_get_bits_out_of_range);
62 tcase_add_test(tc_core, test_get_uneven_bits);
63 suite_add_tcase(s, tc_core);
70 Suite* s = bitfieldSuite();
71 SRunner *sr = srunner_create(s);
72 // Don't fork so we can actually use gdb
73 srunner_set_fork_status(sr, CK_NOFORK);
74 srunner_run_all(sr, CK_NORMAL);
75 numberFailed = srunner_ntests_failed(sr);
77 return (numberFailed == 0) ? 0 : 1;