From 15ed8e32c2e3402c85108971bc2f72daf89bdfeb Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 28 Apr 2024 22:33:35 +0530 Subject: [PATCH] test_lib -> test_lib_base and implement remaining tests for base Also refactored tests to new byteswap method --- test/lib/main.c | 2 +- test/lib/test-base.h | 158 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 137 insertions(+), 23 deletions(-) diff --git a/test/lib/main.c b/test/lib/main.c index d7529c7..5b5cd7b 100644 --- a/test/lib/main.c +++ b/test/lib/main.c @@ -14,6 +14,6 @@ int main(void) { - RUN_TEST_SUITE(test_lib); + RUN_TEST_SUITE(test_lib_base); return 0; } diff --git a/test/lib/test-base.h b/test/lib/test-base.h index 4fd34a3..e194ee0 100644 --- a/test/lib/test-base.h +++ b/test/lib/test-base.h @@ -18,7 +18,7 @@ #include -void testing_lib_base_word_safe_sub(void) +void test_lib_base_word_safe_sub(void) { const struct TestCase { @@ -36,7 +36,10 @@ void testing_lib_base_word_safe_sub(void) const struct TestCase test = tests[i]; const word_t res = test.expected; const word_t got = WORD_SAFE_SUB(test.a, test.b); - INFO(__func__, "Testing(a=0x%lX, b=0x%lX)\n", test.a, test.b); +#if VERBOSE > 1 + INFO(__func__, "Testing(a=0x%lX, b=0x%lX) = 0x%lX\n", test.a, test.b, + test.expected); +#endif if (got != res) { FAIL(__func__, "[%lu] -> Expected 0x%lX got 0x%lX\n", i, res, got); @@ -45,7 +48,7 @@ void testing_lib_base_word_safe_sub(void) } } -void testing_lib_base_word_nth_byte(void) +void test_lib_base_word_nth_byte(void) { const struct TestCase { @@ -69,17 +72,19 @@ void testing_lib_base_word_nth_byte(void) const word_t w = tests[i].word; const size_t n = tests[i].n; const byte_t expected = tests[i].expected; - INFO(__func__, "Testing(0x%lX, %lu)\n", w, n); +#if VERBOSE > 1 + INFO(__func__, "Testing(0x%lX, %lu) = 0x%X\n", w, n, expected); +#endif const byte_t got = WORD_NTH_BYTE(w, n); if (got != expected) { - FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, expected, got); + FAIL(__func__, "[%lu] -> Expected 0x%X got 0x%X\n", i, expected, got); assert(false); } } } -void testing_lib_base_word_nth_hword(void) +void test_lib_base_word_nth_hword(void) { const struct TestCase { @@ -99,17 +104,78 @@ void testing_lib_base_word_nth_hword(void) const word_t w = tests[i].word; const size_t n = tests[i].n; const hword_t expected = tests[i].expected; - INFO(__func__, "Testing(0x%lX, %lu)\n", w, n); +#if VERBOSE > 1 + INFO(__func__, "Testing(0x%lX, %lu) = 0x%X\n", w, n, expected); +#endif const hword_t got = WORD_NTH_HWORD(w, n); if (got != expected) { - FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, expected, got); + FAIL(__func__, "[%lu] -> Expected 0x%X got 0x%X\n", i, expected, got); assert(false); } } } -void testing_lib_base_bytes_to_hword(void) +void test_lib_base_hword_byteswap(void) +{ + struct TestCase + { + hword_t sample, expected; + } tests[] = { + {0, 0}, + {0xFF0000AA, 0xAA0000FF}, + {0x89ABCDEF, 0xEFCDAB89}, + {0x12FAAF21, 0x21AFFA12}, + {0xEFBEADDE, 0xDEADBEEF}, + }; + + for (size_t i = 0; i < ARR_SIZE(tests); ++i) + { + hword_t test = tests[i].sample; + hword_t expected = tests[i].expected; +#if VERBOSE > 1 + INFO(__func__, "Testing 0x%X = 0x%X\n", test, expected); +#endif + hword_t got = hword_byteswap(test); + if (expected != got) + { + FAIL(__func__, "[%lu] -> Expected 0x%X got 0x%X\n", i, expected, got); + assert(false); + } + } +} + +void test_lib_base_word_byteswap(void) +{ + struct TestCase + { + word_t sample, expected; + } tests[] = { + {0, 0}, + {0xFF000000000000AA, 0xAA000000000000FF}, + {0x1023456789ABCDEF, 0xEFCDAB8967452310}, + {0xAAAAAAAAFFFFFFFF, 0xFFFFFFFFAAAAAAAA}, + {0x89ABCDEFFEDCBA98, 0x98BADCFEEFCDAB89}, + {0x1000EFBEADDE0011, 0x1100DEADBEEF0010}, + }; + + for (size_t i = 0; i < ARR_SIZE(tests); ++i) + { + word_t test = tests[i].sample; + word_t expected = tests[i].expected; +#if VERBOSE > 1 + INFO(__func__, "Testing 0x%lX = 0x%lX\n", test, expected); +#endif + word_t got = word_byteswap(test); + if (expected != got) + { + FAIL(__func__, "[%lu] -> Expected 0x%lX got 0x%lX\n", i, expected, got); + assert(false); + } + } +} + +void test_lib_base_bytes_to_hword(void) { const struct TestCase { @@ -128,17 +194,19 @@ void testing_lib_base_bytes_to_hword(void) memset(str, 0, n); const hword_t got = convert_bytes_to_hword(tests[i].bytes); byte_array_to_string(tests[i].bytes, 4, str); - INFO(__func__, "Testing%s\n", str); +#if VERBOSE > 1 + INFO(__func__, "Testing%s = 0x%X\n", str, tests[i].expected); +#endif if (tests[i].expected != got) { - FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, tests[i].expected, + FAIL(__func__, "[%lu] -> Expected 0x%X got 0x%X\n", i, tests[i].expected, got); assert(false); } } } -void testing_lib_base_bytes_to_word(void) +void test_lib_base_bytes_to_word(void) { const struct TestCase { @@ -157,7 +225,9 @@ void testing_lib_base_bytes_to_word(void) { memset(str, 0, n); byte_array_to_string(tests[i].bytes, 8, str); - INFO(__func__, "Testing%s\n", str); +#if VERBOSE > 1 + INFO(__func__, "Testing%s = 0x%lX\n", str, tests[i].expected); +#endif const word_t got = convert_bytes_to_word(tests[i].bytes); if (tests[i].expected != got) { @@ -168,7 +238,7 @@ void testing_lib_base_bytes_to_word(void) } } -void testing_lib_base_hword_to_bytes(void) +void test_lib_base_hword_to_bytes(void) { const struct TestCase { @@ -184,10 +254,15 @@ void testing_lib_base_hword_to_bytes(void) { byte_t buffer[4] = {0}; convert_hword_to_bytes(tests[i].sample, buffer); - INFO(__func__, "Testing(0x%x)\n", tests[i].sample); - const size_t n = size_byte_array_to_string(8); +#if VERBOSE > 1 + const size_t n = size_byte_array_to_string(4); + char str_expected[n]; + byte_array_to_string(tests[i].expected, 4, str_expected); + INFO(__func__, "Testing(0x%X) = %s\n", tests[i].sample, str_expected); +#endif if (memcmp(buffer, tests[i].expected, 4) != 0) { + const size_t n = size_byte_array_to_string(4); char str_expected[n]; char str_got[n]; byte_array_to_string(tests[i].expected, 4, str_expected); @@ -198,11 +273,50 @@ void testing_lib_base_hword_to_bytes(void) } } -TEST_SUITE(test_lib, CREATE_TEST(testing_lib_base_word_safe_sub), - CREATE_TEST(testing_lib_base_word_nth_byte), - CREATE_TEST(testing_lib_base_word_nth_hword), - CREATE_TEST(testing_lib_base_bytes_to_hword), - CREATE_TEST(testing_lib_base_bytes_to_word), - CREATE_TEST(testing_lib_base_hword_to_bytes), ); +void test_lib_base_word_to_bytes(void) +{ + const struct TestCase + { + word_t sample; + byte_t expected[8]; + } tests[] = { + {0, {0, 0, 0, 0}}, + {WORD_MAX, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {1, {0x01, 0, 0, 0, 0, 0, 0, 0}}, + {1LU << 63, {0, 0, 0, 0, 0, 0, 0, 0b10000000}}, + {0xefcdab8967452301, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}}; + + for (size_t i = 0; i < ARR_SIZE(tests); ++i) + { + byte_t buffer[8] = {0}; + convert_word_to_bytes(tests[i].sample, buffer); +#if VERBOSE > 1 + const size_t n = size_byte_array_to_string(8); + char str_expected[n]; + byte_array_to_string(tests[i].expected, 8, str_expected); + INFO(__func__, "Testing(0x%lx) = %s\n", tests[i].sample, str_expected); +#endif + if (memcmp(buffer, tests[i].expected, 8) != 0) + { + const size_t n = size_byte_array_to_string(8); + char str_expected[n]; + char str_got[n]; + byte_array_to_string(tests[i].expected, 8, str_expected); + byte_array_to_string(buffer, 8, str_got); + FAIL(__func__, "[%lu] -> Expected %s got %s\n", i, str_expected, str_got); + assert(false); + } + } +} + +TEST_SUITE(test_lib_base, CREATE_TEST(test_lib_base_word_safe_sub), + CREATE_TEST(test_lib_base_word_nth_byte), + CREATE_TEST(test_lib_base_word_nth_hword), + CREATE_TEST(test_lib_base_hword_byteswap), + CREATE_TEST(test_lib_base_word_byteswap), + CREATE_TEST(test_lib_base_bytes_to_hword), + CREATE_TEST(test_lib_base_bytes_to_word), + CREATE_TEST(test_lib_base_hword_to_bytes), + CREATE_TEST(test_lib_base_word_to_bytes), ); #endif