summaryrefslogtreecommitdiffstats
path: root/libs/bitfield-c/tests/write_tests.c
blob: 9e65b9d0b30dc1f288946c92b39185d7fee9383c (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <canutil/write.h>
#include <check.h>
#include <stdint.h>

START_TEST (test_eightbyte_encode_float_precision)
{
    uint64_t value = eightbyte_encode_float(50, 2, 19, 0.001, 0);
    ck_assert_int_eq(value, 0x061a800000000000LLU);
}
END_TEST

START_TEST (test_eightbyte_encode_float)
{
    uint64_t value = eightbyte_encode_float(0, 1, 3, 1, 0);
    ck_assert_int_eq(value, 0);

    value = eightbyte_encode_float(1, 1, 3, 1, 0);
    ck_assert_int_eq(value, 0x1000000000000000LLU);
}
END_TEST

START_TEST (test_eightbyte_encode_bool)
{
    uint64_t value = eightbyte_encode_bool(true, 1, 3);
    ck_assert_int_eq(value, 0x1000000000000000LLU);
    value = eightbyte_encode_bool(false, 1, 3);
    ck_assert_int_eq(value, 0x0000000000000000LLU);
}
END_TEST

START_TEST (test_bitfield_encode_float)
{
    unsigned int data[8] = {0};
    bitfield_encode_float(0, 1, 3, 1, 0, data, sizeof(data));
    ck_assert_int_eq(data[0], 0);
    ck_assert_int_eq(data[1], 0);
    ck_assert_int_eq(data[2], 0);
    ck_assert_int_eq(data[3], 0);
    ck_assert_int_eq(data[4], 0);
    ck_assert_int_eq(data[5], 0);
    ck_assert_int_eq(data[6], 0);
    ck_assert_int_eq(data[7], 0);

    bitfield_encode_float(1, 1, 3, 1, 0, data, sizeof(data));
    ck_assert_int_eq(data[0], 0x10);
    ck_assert_int_eq(data[1], 0);
    ck_assert_int_eq(data[2], 0);
    ck_assert_int_eq(data[3], 0);
    ck_assert_int_eq(data[4], 0);
    ck_assert_int_eq(data[5], 0);
    ck_assert_int_eq(data[6], 0);
    ck_assert_int_eq(data[7], 0);
}
END_TEST

START_TEST (test_bitfield_encode_bool)
{
    unsigned int data[8] = {0};
    bitfield_encode_bool(true, 1, 3, data, sizeof(data));
    ck_assert_int_eq(data[0], 0x10);
    ck_assert_int_eq(data[1], 0);
    ck_assert_int_eq(data[2], 0);
    ck_assert_int_eq(data[3], 0);
    ck_assert_int_eq(data[4], 0);
    ck_assert_int_eq(data[5], 0);
    ck_assert_int_eq(data[6], 0);
    ck_assert_int_eq(data[7], 0);

    bitfield_encode_bool(false, 1, 3, data, sizeof(data));
    ck_assert_int_eq(data[0], 0);
    ck_assert_int_eq(data[1], 0);
    ck_assert_int_eq(data[2], 0);
    ck_assert_int_eq(data[3], 0);
    ck_assert_int_eq(data[4], 0);
    ck_assert_int_eq(data[5], 0);
    ck_assert_int_eq(data[6], 0);
    ck_assert_int_eq(data[7], 0);
}
END_TEST

Suite* canwriteSuite(void) {
    Suite* s = suite_create("write");
    TCase *tc_core = tcase_create("core");
    tcase_add_checked_fixture(tc_core, NULL, NULL);
    tcase_add_test(tc_core, test_eightbyte_encode_float);
    tcase_add_test(tc_core, test_eightbyte_encode_bool);
    tcase_add_test(tc_core, test_eightbyte_encode_float_precision);
    tcase_add_test(tc_core, test_bitfield_encode_float);
    tcase_add_test(tc_core, test_bitfield_encode_bool);
    suite_add_tcase(s, tc_core);

    return s;
}

int main(void) {
    int numberFailed;
    Suite* s = canwriteSuite();
    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;
}