Function to generate string representation of byte array in testing.h
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
#include <lib/base.h>
|
#include <lib/base.h>
|
||||||
#include <testing.h>
|
#include <testing.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void testing_lib_base_word_safe_sub(void)
|
void testing_lib_base_word_safe_sub(void)
|
||||||
{
|
{
|
||||||
const struct TestCase
|
const struct TestCase
|
||||||
@@ -119,11 +121,14 @@ void testing_lib_base_bytes_to_hword(void)
|
|||||||
{{0, 0, 0, 0b10000000}, 1 << 31},
|
{{0, 0, 0, 0b10000000}, 1 << 31},
|
||||||
{{0x89, 0xab, 0xcd, 0xef}, 0xefcdab89}};
|
{{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)
|
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);
|
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],
|
byte_array_to_string(tests[i].bytes, 4, str);
|
||||||
tests[i].bytes[1], tests[i].bytes[2], tests[i].bytes[3]);
|
INFO(__func__, "Testing%s\n", str);
|
||||||
if (tests[i].expected != got)
|
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,
|
||||||
@@ -146,12 +151,13 @@ void testing_lib_base_bytes_to_word(void)
|
|||||||
{{0, 0, 0, 0, 0, 0, 0, 0b10000000}, 1LU << 63},
|
{{0, 0, 0, 0, 0, 0, 0, 0b10000000}, 1LU << 63},
|
||||||
{{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 0xefcdab8967452301}};
|
{{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)
|
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",
|
memset(str, 0, n);
|
||||||
tests[i].bytes[0], tests[i].bytes[1], tests[i].bytes[2],
|
byte_array_to_string(tests[i].bytes, 8, str);
|
||||||
tests[i].bytes[3], tests[i].bytes[4], tests[i].bytes[5],
|
INFO(__func__, "Testing%s\n", str);
|
||||||
tests[i].bytes[6], tests[i].bytes[7]);
|
|
||||||
const word_t got = convert_bytes_to_word(tests[i].bytes);
|
const word_t got = convert_bytes_to_word(tests[i].bytes);
|
||||||
if (tests[i].expected != got)
|
if (tests[i].expected != got)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,8 +15,10 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <lib/base.h>
|
#include <lib/base.h>
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
|
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
|
||||||
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
||||||
@@ -51,4 +53,29 @@ struct Test
|
|||||||
} \
|
} \
|
||||||
SUCCESS(#SUITE, "%s", "Finished test suite!\n")
|
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
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user