1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <check.h>
#include <stdint.h>
#include <bitfield/bitfield.h>
START_TEST (test_get_byte)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result = get_byte(data, sizeof(data), 0);
ck_assert_int_eq(result, 0x12);
result = get_byte(data, sizeof(data), 3);
ck_assert_int_eq(result, 0x78);
}
END_TEST
START_TEST (test_get_nibble)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result = get_nibble(data, sizeof(data), 0);
ck_assert_int_eq(result, 0x1);
result = get_nibble(data, sizeof(data), 1);
ck_assert_int_eq(result, 0x2);
result = get_nibble(data, sizeof(data), 2);
ck_assert_int_eq(result, 0x3);
}
END_TEST
START_TEST (test_get_bits_out_of_range)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4];
fail_if(copy_bits_right_aligned(data, 4, 25, 16, result, 4));
}
END_TEST
START_TEST (test_get_bits)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4] = {0};
fail_unless(copy_bits_right_aligned(data, 4, 0, 16, result, 4));
ck_assert_int_eq(result[0], 0x12);
ck_assert_int_eq(result[1], 0x34);
}
END_TEST
START_TEST (test_get_uneven_bits)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4] = {0};
fail_unless(copy_bits_right_aligned(data, 4, 4, 12, result, 4));
ck_assert_int_eq(result[0], 0x2);
ck_assert_int_eq(result[1], 0x34);
}
END_TEST
Suite* bitfieldSuite(void) {
Suite* s = suite_create("bitfield");
TCase *tc_core = tcase_create("core");
tcase_add_test(tc_core, test_get_byte);
tcase_add_test(tc_core, test_get_nibble);
tcase_add_test(tc_core, test_get_bits);
tcase_add_test(tc_core, test_get_bits_out_of_range);
tcase_add_test(tc_core, test_get_uneven_bits);
suite_add_tcase(s, tc_core);
return s;
}
int main(void) {
int numberFailed;
Suite* s = bitfieldSuite();
SRunner *sr = srunner_create(s);
// Don't fork so we can actually use gdb
srunner_set_fork_status(sr, CK_NOFORK);
srunner_run_all(sr, CK_NORMAL);
numberFailed = srunner_ntests_failed(sr);
srunner_free(sr);
return (numberFailed == 0) ? 0 : 1;
}
|