Standardize argument ordering for bitfield functions.
[apps/low-level-can-service.git] / tests / bitfield_tests.c
index d248989..00a28a3 100644 (file)
@@ -25,7 +25,7 @@ START_TEST (test_one_bit)
     uint64_t data = 0x8000000000000000;
     uint64_t result = getBitField(data, 0, 1, false);
     fail_unless(result == 0x1,
-            "First bits in 0x%X was 0x%X instead of 0x1", data, result);
+            "First bit in 0x%llx was 0x%llx instead of 0x1", data, result);
 }
 END_TEST
 
@@ -35,7 +35,7 @@ START_TEST (test_32_bit_parse)
     uint64_t result = getBitField(data, 16, 32, false);
     uint64_t expectedValue = 0x574d555a;
     fail_unless(result == expectedValue,
-            "Field retrieved in 0x%X was 0x%X instead of %d", data,
+            "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
             result, expectedValue);
 }
 END_TEST
@@ -46,7 +46,7 @@ START_TEST (test_16_bit_parse)
     uint64_t result = getBitField(data, 16, 16, false);
     uint64_t expectedValue = 0xFCFF;
     fail_unless(result == expectedValue,
-            "Field retrieved in 0x%X was 0x%X instead of %d", data,
+            "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data,
             result, expectedValue);
 }
 END_TEST
@@ -56,13 +56,13 @@ START_TEST (test_one_byte)
     uint64_t data = 0xFA00000000000000;
     uint64_t result = getBitField(data, 0, 4, false);
     fail_unless(result == 0xF,
-            "First 4 bits in 0x%X was 0x%X instead of 0xF", data, result);
+            "First 4 bits in 0x%llx was 0x%llx instead of 0xF", data, result);
     result = getBitField(data, 4, 4, false);
     fail_unless(result == 0xA,
-            "First 4 bits in 0x%X was 0x%X instead of 0xA", data, result);
+            "First 4 bits in 0x%llx was 0x%llx instead of 0xA", data, result);
     result = getBitField(data, 0, 8, false);
     fail_unless(result == 0xFA,
-            "All bits in 0x%X were 0x%X instead of 0x%X", data, result, data);
+            "All bits in 0x%llx were 0x%llx instead of 0x%llx", data, result, data);
 }
 END_TEST
 
@@ -71,19 +71,19 @@ START_TEST (test_multi_byte)
     uint64_t data = 0x12FA000000000000;
     uint64_t result = getBitField(data, 0, 4, false);
     fail_unless(result == 0x1,
-            "First 4 bits in 0x%X was 0x%X instead of 0xF", (data >> 60) & 0xF,
+            "First 4 bits in 0x%llx was 0x%llx instead of 0xF", (data >> 60) & 0xF,
             result);
     result = getBitField(data, 4, 4, false);
     fail_unless(result == 0x2,
-            "Second 4 bits in 0x%X was %d instead of 0xA", (data >> 56) & 0xF,
+            "Second 4 bits in 0x%llx was 0x%llx instead of 0xA", (data >> 56) & 0xF,
             result);
     result = getBitField(data, 8, 4, false);
     fail_unless(result == 0xF,
-            "First 4 bits in 0x%X was %d instead of 0x1", (data >> 52) & 0xF,
+            "First 4 bits in 0x%llx was 0x%llx instead of 0x1", (data >> 52) & 0xF,
             result);
     result = getBitField(data, 12, 4, false);
     fail_unless(result == 0xA,
-            "Second 4 bits in 0x%X was %d instead of 0x2", (data >> 48) % 0xF,
+            "Second 4 bits in 0x%llx was 0x%llx instead of 0x2", (data >> 48) % 0xF,
             result);
 }
 END_TEST
@@ -127,13 +127,13 @@ START_TEST (test_set_doesnt_clobber_existing_data)
     setBitField(&data, 0x4fc8, 16, 16);
     uint64_t result = getBitField(data, 16, 16, false);
     fail_unless(result == 0x4fc8,
-            "Field retrieved in 0x%X was 0x%X instead of 0x%X", data, result,
+            "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result,
             0xc84f);
 
     data = 0x8000000000000000;
     setBitField(&data, 1, 21, 1);
     fail_unless(data == 0x8000040000000000LLU,
-            "Expected combined value 0x8000040000000000 but got 0x%X%X",
+            "Expected combined value 0x8000040000000000 but got 0x%llx%llx",
             data >> 32, data);
 }
 END_TEST
@@ -153,14 +153,14 @@ START_TEST (test_set_odd_number_of_bits)
     setBitField(&data, 0x12, 11, 5);
     uint64_t result = getBitField(data, 11, 5, false);
     fail_unless(result == 0x12,
-            "Field set in 0x%X%X%X%X was %d instead of %d", data, result,
+            "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
             0x12);
 
     data = 0xFFFC4DF300000000LLU;
     setBitField(&data, 0x2, 11, 5);
     result = getBitField(data, 11, 5, false);
     fail_unless(result == 0x2,
-            "Field set in 0x%X%X%X%X was %d instead of %d", data, result,
+            "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result,
             0x2);
 }
 END_TEST
@@ -190,6 +190,56 @@ START_TEST(test_nth_byte)
 }
 END_TEST
 
+START_TEST (test_get_byte)
+{
+    uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
+    uint8_t result = getByte(data, sizeof(data), 0);
+    ck_assert_int_eq(result, 0x12);
+    result = getByte(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 = getNibble(data, sizeof(data), 0);
+    ck_assert_int_eq(result, 0x1);
+    result = getNibble(data, sizeof(data), 1);
+    ck_assert_int_eq(result, 0x2);
+    result = getNibble(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(copyBitsRightAligned(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(copyBitsRightAligned(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(copyBitsRightAligned(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");
@@ -207,6 +257,11 @@ Suite* bitfieldSuite(void) {
     tcase_add_test(tc_core, test_set_off_byte_boundary);
     tcase_add_test(tc_core, test_set_odd_number_of_bits);
     tcase_add_test(tc_core, test_nth_byte);
+    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;