Add 'CAN-binder/libs/bitfield-c/' from commit 'a34745ec93ae0a1d4f1b640dba8fb6702331a8e9'
[apps/agl-service-can-low-level.git] / CAN-binder / libs / bitfield-c / tests / read_tests.c
1 #include <check.h>
2 #include <stdint.h>
3 #include <canutil/read.h>
4
5 const uint64_t BIG_ENDIAN_TEST_DATA = __builtin_bswap64(0xEB00000000000000);
6 const uint8_t ARRAY_TEST_DATA[] = {0xEB, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
7
8 START_TEST (test_eightbyte_parse_float)
9 {
10     float result = eightbyte_parse_float(BIG_ENDIAN_TEST_DATA, 2, 4, 1001.0,
11             -30000.0);
12     float correctResult = 0xA * 1001.0 - 30000.0;
13     fail_unless(result == correctResult,
14             "parse is incorrect: %f but should be %f", result, correctResult);
15 }
16 END_TEST
17
18 START_TEST (test_eightbyte_parse_bool)
19 {
20     bool result = eightbyte_parse_bool(BIG_ENDIAN_TEST_DATA, 0, 1, 1.0, 0);
21     bool correctResult = true;
22     fail_unless(result == correctResult,
23             "parse is incorrect: %d but should be %d", result, correctResult);
24 }
25 END_TEST
26
27 START_TEST (test_bitfield_parse_float)
28 {
29     float result = bitfield_parse_float(ARRAY_TEST_DATA,
30             sizeof(ARRAY_TEST_DATA), 2, 4, 1001.0, -30000.0);
31     float correctResult = 0xA * 1001.0 - 30000.0;
32     fail_unless(result == correctResult,
33             "parse is incorrect: %f but should be %f", result, correctResult);
34 }
35 END_TEST
36
37 START_TEST (test_bitfield_parse_bool)
38 {
39     fail_unless(bitfield_parse_bool(ARRAY_TEST_DATA, sizeof(ARRAY_TEST_DATA),
40             0, 1, 1.0, 0));
41 }
42 END_TEST
43
44 Suite* canreadSuite(void) {
45     Suite* s = suite_create("read");
46     TCase *tc_core = tcase_create("core");
47     tcase_add_checked_fixture(tc_core, NULL, NULL);
48     tcase_add_test(tc_core, test_eightbyte_parse_float);
49     tcase_add_test(tc_core, test_eightbyte_parse_bool);
50     tcase_add_test(tc_core, test_bitfield_parse_float);
51     tcase_add_test(tc_core, test_bitfield_parse_bool);
52     suite_add_tcase(s, tc_core);
53
54     return s;
55 }
56
57 int main(void) {
58     int numberFailed;
59     Suite* s = canreadSuite();
60     SRunner *sr = srunner_create(s);
61     // Don't fork so we can actually use gdb
62     srunner_set_fork_status(sr, CK_NOFORK);
63     srunner_run_all(sr, CK_NORMAL);
64     numberFailed = srunner_ntests_failed(sr);
65     srunner_free(sr);
66     return (numberFailed == 0) ? 0 : 1;
67 }