From 566de8019e0f8c3b10fd64c21ceaf9591d877ca0 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Mon, 6 Jun 2022 22:07:35 +0300 Subject: [PATCH] Refactor test plugs --- tests/include/test.h | 5 ++++ tests/src/array/plug.h | 62 +++++++++++++++++------------------------ tests/src/list/plug.h | 11 ++------ tests/src/string/plug.h | 9 ++++-- tests/src/test.c | 44 +++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 48 deletions(-) diff --git a/tests/include/test.h b/tests/include/test.h index 4a6a4c5..2b689ce 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -2,11 +2,16 @@ /* Copyright © 2022 Gregory Lirent */ #include +#include "../../include/extra/vtype.h" #ifndef LIBCDSB_TESTS_TEST_H #define LIBCDSB_TESTS_TEST_H extern void put_separator(); +extern void print_container_values_prefix(const char* name, const char* prefix); +extern void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type); +extern void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb); + extern void test_init(int argc, char** argv); #endif /* LIBCDSB_TESTS_TEST_H */ diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index 63e22d0..afd1616 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -30,35 +30,23 @@ extern void map_copy_init (vtype_map* x, const vtype_map* s) { memset(x, extern void vset_copy_init (vtype_set* x, const vtype_set* s) { memset(x, 0, sizeof(*x)); } -static void array_push_random_value(vtype_array* x) { - switch (random_uint8()%10) { - default: - case 0: array_push(x, random_boolean()); break; - case 1: array_push(x, random_uint8()); break; - case 2: array_push(x, random_uint16()); break; - case 3: array_push(x, random_uint32()); break; - case 4: array_push(x, random_uint64()); break; - case 5: array_push(x, random_int8()); break; - case 6: array_push(x, random_int16()); break; - case 7: array_push(x, random_int32()); break; - case 8: array_push(x, random_int64()); break; - case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - } -} - -static void array_push_random_float(vtype_array* x) { - switch (random_uint8()%3) { - default: - case 0: array_push(x, random_float()); break; - case 1: array_push(x, random_double()); break; - case 2: array_push(x, random_ldouble()); break; - } -} - static void array_push_random(vtype_array* x) { - if (x->type < VTYPE_FLOAT) { - array_push_random_value(x); - } else array_push_random_float(x); + switch (random_uint8()%13) { + default: + case 0: array_push(x, random_boolean()); break; + case 1: array_push(x, random_uint8()); break; + case 2: array_push(x, random_uint16()); break; + case 3: array_push(x, random_uint32()); break; + case 4: array_push(x, random_uint64()); break; + case 5: array_push(x, random_int8()); break; + case 6: array_push(x, random_int16()); break; + case 7: array_push(x, random_int32()); break; + case 8: array_push(x, random_int64()); break; + case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + case 10: array_push(x, random_float()); break; + case 11: array_push(x, random_double()); break; + case 12: array_push(x, random_ldouble()); break; + } } static void array_init_random(vtype_array* x) { @@ -92,18 +80,18 @@ static vtype_array array_random(unsigned int n) { return x; } -static void array_print(const vtype_array* x, const char* prefix) { - if (!prefix) puts("\e[36mArray values:\e[m"); - else printf("\e[36mArray %s values:\e[m\n", prefix); - for (int i = 0; i < x->size; ++i) { - printf("\e[32;1m%5d: \e[m\e[31m%24s\e[m\n", i, libcdsb_vtype_stringify(array_at(x, i), x->type)); - } - put_separator(); +static int array_value_print(void* v, ssize_t i, vtype t) { + print_container_value(&i, v, t, false); + return 0; } +static void array_print(const vtype_array* x, const char* prefix) { + print_container_values_prefix("Array", prefix); + array_foreach(x, array_value_print); + put_separator(); +} static void array_info(const vtype_array* x) { - printf("\e[36mArray initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", libcdsb_vtype_name(x->type)); - printf("Array has \e[m\e[32m%lu\e[m\e[36m elements (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", array_size(x), array_nmemb(x)); + print_container_info("Array", 0, &x->type, array_size(x), array_nmemb(x)); put_separator(); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index 1da67d2..b700edd 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -61,22 +61,17 @@ static void list_push_random(vtype_list* x, vtype_bool front) { } static int list_node_print(void* v, ssize_t i, vtype t) { - printf("\e[32;1m%5ld: \e[m\e[31m%24s\e[m \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", i, - libcdsb_vtype_stringify(v, t), - libcdsb_vtype_name(t)); + print_container_value(&i, v, t, 1); return 0; } - static void list_print(const vtype_list* x, const char* prefix) { - if (!prefix) puts("\e[36mList values:\e[m"); - else printf("\e[36mList %s values:\e[m\n", prefix); - + print_container_values_prefix("List", prefix); list_foreach(x, list_node_print); put_separator(); } static void list_info(const vtype_list* x) { - printf("\e[36mList has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x)); + print_container_info("List", "nodes", 0, list_size(x), -1); put_separator(); } diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index 472bbf1..e0b2ad5 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -49,12 +49,15 @@ static char* random_utf8_cstring(size_t size) { } static void string_info(vtype_string* x) { - printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x)); + print_container_info("String", "utf8 chars", 0, string_size(x), string_nmemb(x)); + put_separator(); } static void string_print(const vtype_string* x, const char* prefix) { - if (!prefix) puts("\e[36mString content:\e[m\n"); - else printf("\e[36mString %s content:\e[m\n\n", prefix); + if (prefix) { + printf("\e[36m%s %s content:\e[m\n", "String", prefix); + } else printf("\e[36m%s content:\e[m\n", "String"); + printf("\e[33m\"%s\"\e[m\n", x->buffer); put_separator(); } diff --git a/tests/src/test.c b/tests/src/test.c index 7e24bc4..1d570cf 100644 --- a/tests/src/test.c +++ b/tests/src/test.c @@ -24,6 +24,50 @@ static void test_uload() { void put_separator() { puts("\e[37;2m=== === === === === === === ===\e[m"); } +void print_container_values_prefix(const char* name, const char* prefix) { + if (prefix) { + printf("\e[36m%s %s values:\e[m\n", name, prefix); + } else printf("\e[36m%s values:\e[m\n", name); +} + +void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type) { + if (index) { + printf("\e[32;1m%5ld: \e[m", *index); + } else fputs(" ", stdout); + + printf("\e[31m%24s\e[m", libcdsb_vtype_stringify(value, type)); + + if (print_type) { + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(type)); + } + + puts(""); +} + +void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb) { + if (!el_name) el_name = "elements"; + + if (type) { + printf("\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", name, libcdsb_vtype_name(*type)); + } + + if (size >= 0 || nmemb >= 0) { + printf("\e[36m%s consists of \e[m", name); + } + + if (size >= 0) { + printf("\e[32m%ld\e[m \e[36m%s", size, el_name); + + if (nmemb >= 0) { + printf(" (\e[m\e[32m%ld bytes\e[m\e[36m)\e[m\n", nmemb); + } else puts("\e[m"); + + } else if (nmemb >= 0) { + printf("\e[32m%ld bytes\e[m\n", nmemb); + } +} + + void test_init(int argc, char** argv) { timer_init(&GLOBAL_TIMER); timer_start(&GLOBAL_TIMER);