diff --git a/test/lib/test-base.h b/test/lib/test-base.h index 5c79034..dd364e2 100644 --- a/test/lib/test-base.h +++ b/test/lib/test-base.h @@ -16,6 +16,8 @@ #include #include +#include + void testing_lib_base_word_safe_sub(void) { const struct TestCase @@ -119,11 +121,14 @@ void testing_lib_base_bytes_to_hword(void) {{0, 0, 0, 0b10000000}, 1 << 31}, {{0x89, 0xab, 0xcd, 0xef}, 0xefcdab89}}; + const size_t n = size_byte_array_to_string(4); + char str[n]; for (size_t i = 0; i < ARR_SIZE(tests); ++i) { + memset(str, 0, n); const hword_t got = convert_bytes_to_hword(tests[i].bytes); - INFO(__func__, "Testing(0x%X, 0x%X, 0x%X, 0x%X)\n", tests[i].bytes[0], - tests[i].bytes[1], tests[i].bytes[2], tests[i].bytes[3]); + byte_array_to_string(tests[i].bytes, 4, str); + INFO(__func__, "Testing%s\n", str); if (tests[i].expected != got) { FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, tests[i].expected, @@ -146,12 +151,13 @@ void testing_lib_base_bytes_to_word(void) {{0, 0, 0, 0, 0, 0, 0, 0b10000000}, 1LU << 63}, {{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 0xefcdab8967452301}}; + const size_t n = size_byte_array_to_string(8); + char str[n]; for (size_t i = 0; i < ARR_SIZE(tests); ++i) { - INFO(__func__, "Testing(0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X)\n", - tests[i].bytes[0], tests[i].bytes[1], tests[i].bytes[2], - tests[i].bytes[3], tests[i].bytes[4], tests[i].bytes[5], - tests[i].bytes[6], tests[i].bytes[7]); + memset(str, 0, n); + byte_array_to_string(tests[i].bytes, 8, str); + INFO(__func__, "Testing%s\n", str); const word_t got = convert_bytes_to_word(tests[i].bytes); if (tests[i].expected != got) { diff --git a/test/testing.h b/test/testing.h index 4c177a1..568362a 100644 --- a/test/testing.h +++ b/test/testing.h @@ -15,8 +15,10 @@ #include #include + #include #include +#include #define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \ fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__) @@ -51,4 +53,29 @@ struct Test } \ SUCCESS(#SUITE, "%s", "Finished test suite!\n") +static size_t size_byte_array_to_string(const size_t n) +{ + return 3 + (4 * n) + (2 * (n - 1)); +} + +static void byte_array_to_string(const byte_t *bytes, size_t size_bytes, + char *str) +{ + str[0] = '{'; + size_t j = 1; + for (size_t i = 0; i < size_bytes; ++i) + { + ; + char buffer[7]; + int k = i == size_bytes - 1 ? 0 : 2; + size_t n = 2 + (bytes[i] < 10 ? 1 : 2) + k; + + sprintf(buffer, "0x%X, ", bytes[i]); + memcpy(str + j, buffer, n); + j += n; + } + str[j] = '}'; + str[j + 1] = '\0'; +} + #endif