Split up 8 byte wrappers from generic bit array functions.
[apps/agl-service-can-low-level.git] / tests / bitfield_tests.c
1 #include <check.h>
2 #include <stdint.h>
3 #include <bitfield/bitfield.h>
4
5 START_TEST (test_get_byte)
6 {
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);
12 }
13 END_TEST
14
15 START_TEST (test_get_nibble)
16 {
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);
24 }
25 END_TEST
26
27 START_TEST (test_get_bits_out_of_range)
28 {
29     uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
30     uint8_t result[4];
31     fail_if(copyBitsRightAligned(data, 4, 25, 16, result, 4));
32 }
33 END_TEST
34
35 START_TEST (test_get_bits)
36 {
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);
42 }
43 END_TEST
44
45 START_TEST (test_get_uneven_bits)
46 {
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);
52 }
53 END_TEST
54
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);
64
65     return s;
66 }
67
68 int main(void) {
69     int numberFailed;
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);
76     srunner_free(sr);
77     return (numberFailed == 0) ? 0 : 1;
78 }