Function to generate string representation of byte array in testing.h

This commit is contained in:
2024-04-28 20:56:13 +05:30
parent 8461dcf423
commit 8c0bebc8ea
2 changed files with 39 additions and 6 deletions

View File

@@ -15,8 +15,10 @@
#include <assert.h>
#include <lib/base.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#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