diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/8byte_tests.c | 18 | ||||
-rw-r--r-- | tests/bitfield_tests.c | 23 | ||||
-rw-r--r-- | tests/write_tests.c | 76 |
3 files changed, 96 insertions, 21 deletions
diff --git a/tests/8byte_tests.c b/tests/8byte_tests.c index 51e927e4..258b8805 100644 --- a/tests/8byte_tests.c +++ b/tests/8byte_tests.c @@ -103,23 +103,23 @@ START_TEST (test_get_off_byte_boundary) START_TEST (test_set_wont_fit) { uint64_t data = 0; - fail_if(eightbyte_set_bitfield(&data, 100, 0, 1)); + fail_if(eightbyte_set_bitfield(100, 0, 1, &data)); } END_TEST START_TEST (test_set_field) { uint64_t data = 0; - fail_unless(eightbyte_set_bitfield(&data, 1, 0, 1)); + fail_unless(eightbyte_set_bitfield(1, 0, 1, &data)); uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); ck_assert_int_eq(result, 0x1); data = 0; - fail_unless(eightbyte_set_bitfield(&data, 1, 1, 1)); + fail_unless(eightbyte_set_bitfield(1, 1, 1, &data)); result = eightbyte_get_bitfield(data, 1, 1, false); ck_assert_int_eq(result, 0x1); data = 0; - fail_unless(eightbyte_set_bitfield(&data, 0xf, 3, 4)); + fail_unless(eightbyte_set_bitfield(0xf, 3, 4, &data)); result = eightbyte_get_bitfield(data, 3, 4, false); ck_assert_int_eq(result, 0xf); } @@ -128,14 +128,14 @@ END_TEST START_TEST (test_set_doesnt_clobber_existing_data) { uint64_t data = 0xFFFC4DF300000000; - fail_unless(eightbyte_set_bitfield(&data, 0x4fc8, 16, 16)); + fail_unless(eightbyte_set_bitfield(0x4fc8, 16, 16, &data)); uint64_t result = eightbyte_get_bitfield(data, 16, 16, false); fail_unless(result == 0x4fc8, "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result, 0xc84f); data = 0x8000000000000000; - fail_unless(eightbyte_set_bitfield(&data, 1, 21, 1)); + fail_unless(eightbyte_set_bitfield(1, 21, 1, &data)); fail_unless(data == 0x8000040000000000LLU, "Expected combined value 0x8000040000000000 but got 0x%llx%llx", data >> 32, data); @@ -145,7 +145,7 @@ END_TEST START_TEST (test_set_off_byte_boundary) { uint64_t data = 0xFFFC4DF300000000; - fail_unless(eightbyte_set_bitfield(&data, 0x12, 12, 8)); + fail_unless(eightbyte_set_bitfield(0x12, 12, 8, &data)); uint64_t result = eightbyte_get_bitfield(data, 12, 12, false); ck_assert_int_eq(result,0x12d); } @@ -154,14 +154,14 @@ END_TEST START_TEST (test_set_odd_number_of_bits) { uint64_t data = 0xFFFC4DF300000000LLU; - fail_unless(eightbyte_set_bitfield(&data, 0x12, 11, 5)); + fail_unless(eightbyte_set_bitfield(0x12, 11, 5, &data)); uint64_t result = eightbyte_get_bitfield(data, 11, 5, false); fail_unless(result == 0x12, "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result, 0x12); data = 0xFFFC4DF300000000LLU; - fail_unless(eightbyte_set_bitfield(&data, 0x2, 11, 5)); + fail_unless(eightbyte_set_bitfield(0x2, 11, 5, &data)); result = eightbyte_get_bitfield(data, 11, 5, false); fail_unless(result == 0x2, "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result, diff --git a/tests/bitfield_tests.c b/tests/bitfield_tests.c index b03882a2..b8c83b59 100644 --- a/tests/bitfield_tests.c +++ b/tests/bitfield_tests.c @@ -26,6 +26,27 @@ START_TEST (test_set_nibble) } END_TEST +START_TEST (test_set_bitfield) +{ + uint8_t data[4] = {0}; + fail_unless(set_bitfield(0x12, 0, 8, data, sizeof(data))); + fail_unless(set_bitfield(bitmask(3), 10, 3, data, sizeof(data))); + ck_assert_int_eq(data[0], 0x12); + ck_assert_int_eq(data[1], 0x38); +} +END_TEST + +START_TEST (test_set_bitfield_doesnt_fit) +{ + uint8_t data[4] = {0}; + fail_if(set_bitfield(0xffff, 0, 8, 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); +} +END_TEST + START_TEST (test_get_nibble) { uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; @@ -87,6 +108,8 @@ Suite* bitfieldSuite(void) { tcase_add_test(tc_core, test_get_byte); tcase_add_test(tc_core, test_get_nibble); tcase_add_test(tc_core, test_set_nibble); + tcase_add_test(tc_core, test_set_bitfield); + tcase_add_test(tc_core, test_set_bitfield_doesnt_fit); tcase_add_test(tc_core, test_get_bits); tcase_add_test(tc_core, test_copy_bytes); tcase_add_test(tc_core, test_get_bits_out_of_range); diff --git a/tests/write_tests.c b/tests/write_tests.c index 8f34a468..4d5d8fc3 100644 --- a/tests/write_tests.c +++ b/tests/write_tests.c @@ -2,7 +2,14 @@ #include <check.h> #include <stdint.h> -START_TEST (test_encode_can_signal) +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); @@ -12,14 +19,7 @@ START_TEST (test_encode_can_signal) } END_TEST -START_TEST (test_encode_can_signal_rounding_precision) -{ - uint64_t value = eightbyte_encode_float(50, 2, 19, 0.001, 0); - ck_assert_int_eq(value, 0x061a800000000000LLU); -} -END_TEST - -START_TEST (test_encode_bool) +START_TEST (test_eightbyte_encode_bool) { uint64_t value = eightbyte_encode_bool(true, 1, 3); ck_assert_int_eq(value, 0x1000000000000000LLU); @@ -28,13 +28,65 @@ START_TEST (test_encode_bool) } 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_encode_can_signal); - tcase_add_test(tc_core, test_encode_bool); - tcase_add_test(tc_core, test_encode_can_signal_rounding_precision); + 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; |