summaryrefslogtreecommitdiffstats
path: root/CAN-binder/libs/bitfield-c/tests/write_tests.c
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:50:50 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:50:50 +0200
commitd170c9faeae2cf29c19f3523714ecdf58be73bed (patch)
tree114b0f8bc52df642567a7450cfbee2dbfce117d8 /CAN-binder/libs/bitfield-c/tests/write_tests.c
parentb6c09cbda2b6b47981d65265184a21223e526d4a (diff)
parenta34745ec93ae0a1d4f1b640dba8fb6702331a8e9 (diff)
Add 'CAN-binder/libs/bitfield-c/' from commit 'a34745ec93ae0a1d4f1b640dba8fb6702331a8e9'
git-subtree-dir: CAN-binder/libs/bitfield-c git-subtree-mainline: b6c09cbda2b6b47981d65265184a21223e526d4a git-subtree-split: a34745ec93ae0a1d4f1b640dba8fb6702331a8e9
Diffstat (limited to 'CAN-binder/libs/bitfield-c/tests/write_tests.c')
-rw-r--r--CAN-binder/libs/bitfield-c/tests/write_tests.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/CAN-binder/libs/bitfield-c/tests/write_tests.c b/CAN-binder/libs/bitfield-c/tests/write_tests.c
new file mode 100644
index 00000000..4d5d8fc3
--- /dev/null
+++ b/CAN-binder/libs/bitfield-c/tests/write_tests.c
@@ -0,0 +1,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)
+{
+ uint8_t 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)
+{
+ uint8_t 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;
+}