From 760c04fb498850a1e25666d8c9c70d287dc67354 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 9 Jun 2022 11:39:37 +0300 Subject: [PATCH] Update list tests --- tests/src/list/main.c | 25 +++-- tests/src/list/plug.h | 74 +++----------- tests/src/list/remove.c | 32 ------ tests/src/list/src/io.c | 188 ++++++++++++++++++++++++++++++++++++ tests/src/list/src/plug.c | 20 ++++ tests/src/list/src/random.c | 83 ++++++++++++++++ 6 files changed, 314 insertions(+), 108 deletions(-) delete mode 100644 tests/src/list/remove.c create mode 100644 tests/src/list/src/io.c create mode 100644 tests/src/list/src/plug.c create mode 100644 tests/src/list/src/random.c diff --git a/tests/src/list/main.c b/tests/src/list/main.c index 529a266..cfee210 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -6,19 +6,18 @@ int main(int argc, char** argv) { test_init(argc, argv); - vtype_list x; - - list_init(&x); - - for (int i = 0, n = 12 + random_boolean(); i < n; ++i) { - list_push_random(&x, random_boolean()); - } - list_sort(&x); - list_info(&x); - list_print(&x, "sorted"); - - list_reverse(&x); - list_print(&x, "reversed"); + list_t x = { .first = 0, .last = 0 }; + list_t y = { .first = 0, .last = 0 }; list_free(&x); + + visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12); + + visual_extend(&x, &y); + + visual_sort2(&x, &y); + visual_remove2(&x, &y); + + list_free(&y); + list_free(&y); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index d3571ed..7362e9d 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -9,69 +9,17 @@ #include "../../../src/__internal/vnode.h" -vtype_string* string_duplicate(const vtype_string* x) { return 0; } -vtype_array* array_duplicate (const vtype_array* x) { return 0; } -vtype_map* map_duplicate (const vtype_map* x) { return 0; } -vtype_set* vset_duplicate (const vtype_set* x) { return 0; } +extern void list_print(list_t* x, const char* prefix, unsigned int hpos); +extern void list_info (list_t* x, unsigned int hpos); -void string_free(vtype_string* x) {} -void array_free (vtype_array* x) {} -void map_free (vtype_map* x) {} -void vset_free (vtype_set* x) {} +extern void list_push_random(list_t* x, _Bool silent, unsigned int hpos); +extern void list_remove_random(list_t* x, _Bool silent, unsigned int hpos); -int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); } -int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } -int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } -int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +extern void visual_push(list_t* x, size_t n); +extern void visual_sort(list_t* x); +extern void visual_remove(list_t* x); - -static void list_push_random(vtype_list* x, vtype_bool front) { - - if (!front) switch (random_uint8()%13) { - default: - case 0: list_push_back(x, random_boolean()); break; - case 1: list_push_back(x, random_uint8()); break; - case 2: list_push_back(x, random_uint16()); break; - case 3: list_push_back(x, random_uint32()); break; - case 4: list_push_back(x, random_uint64()); break; - case 5: list_push_back(x, random_int8()); break; - case 6: list_push_back(x, random_int16()); break; - case 7: list_push_back(x, random_int32()); break; - case 8: list_push_back(x, random_int64()); break; - case 9: list_push_back(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - case 10: list_push_back(x, random_float()); break; - case 11: list_push_back(x, random_double()); break; - case 12: list_push_back(x, random_ldouble()); break; - } else switch (random_uint8()%13) { - default: - case 0: list_push_front(x, random_boolean()); break; - case 1: list_push_front(x, random_uint8()); break; - case 2: list_push_front(x, random_uint16()); break; - case 3: list_push_front(x, random_uint32()); break; - case 4: list_push_front(x, random_uint64()); break; - case 5: list_push_front(x, random_int8()); break; - case 6: list_push_front(x, random_int16()); break; - case 7: list_push_front(x, random_int32()); break; - case 8: list_push_front(x, random_int64()); break; - case 9: list_push_front(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - case 10: list_push_front(x, random_float()); break; - case 11: list_push_front(x, random_double()); break; - case 12: list_push_front(x, random_ldouble()); break; - } -} - -static int list_node_print(void* v, ssize_t i, vtype t, void* _) { - print_container_value(&i, v, t, 1); - return 0; -} - -static void list_print(const vtype_list* x, const char* prefix) { - print_container_values_prefix("List", prefix); - list_foreach(x, 0, list_node_print); - put_separator(); -} - -static void list_info(const vtype_list* x) { - print_container_info("List", "nodes", 0, list_size(x), -1); - put_separator(); -} +extern void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1); +extern void visual_remove2(list_t* x0, list_t* x1); +extern void visual_sort2(list_t* x0, list_t* x1); +extern void visual_extend(list_t* x, list_t* s); diff --git a/tests/src/list/remove.c b/tests/src/list/remove.c deleted file mode 100644 index b3c7039..0000000 --- a/tests/src/list/remove.c +++ /dev/null @@ -1,32 +0,0 @@ -/* This software is licensed by the MIT License, see LICENSE file */ -/* Copyright © 2022 Gregory Lirent */ - -#include "plug.h" - -int main(int argc, char** argv) { - test_init(argc, argv); - - vtype_list x; - - list_init(&x); - - for (int i = 0, c = random_uint8()%12; i < 12; ++i) { - if (i == c) { - list_push_back(&x, 0); - } else list_push_random(&x, random_boolean()); - } - - list_info(&x); - list_print(&x, 0); - - for (int i = 0, n = list_countof(&x, 0); i < n; ++i) { - int index = list_indexof(&x, 0); - list_remove(&x, 0); - - printf("\e[36mRemove element with index \e[m\e[32;1m%d\e[m\e[36m from list\e[m\n", index); - } - put_separator(); - list_print(&x, "cleaned"); - - list_free(&x); -} diff --git a/tests/src/list/src/io.c b/tests/src/list/src/io.c new file mode 100644 index 0000000..5ed0ed0 --- /dev/null +++ b/tests/src/list/src/io.c @@ -0,0 +1,188 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int list_node_print(void* v, ssize_t i, vtype t, void* _) { + print_container_value(&i, v, t, 1, *(unsigned int*)_); + return 0; +} + +void list_print(list_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("List", prefix, hpos); + list_foreach(x, &hpos, list_node_print); +} + +void list_info(list_t* x, unsigned int hpos) { + print_container_info("List", "nodes", 0, list_size(x), -1, hpos); +} + + +void visual_push(list_t* x, size_t n) { + for (int i = 0; i < n; ++i) { + fputs("\e[s", stdout); + + list_push_random(x, 0, 0); + + list_info(x, 0); + list_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_sort(list_t* x) { + puts("\e[s\e[36mTry to sort list values:\e[m\n\n"); + put_separator(0); + list_sort(x); + list_info(x, 0); + list_print(x, "sorted", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); + + puts("\e[s\e[36mTry to reverse list values:\e[m\n\n"); + put_separator(0); + list_reverse(x); + list_info(x, 0); + list_print(x, "reversed", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_remove(list_t* x) { + while (x->first) { + fputs("\e[s", stdout); + list_remove_random(x, 0, 0); + list_info(x, 0); + list_print(x, 0, 0); + + psleep(100000); + if (!0) fputs("\e[u\e[J", stdout); + } +} + +void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1) { + + for (;n0 || n1;) { + fputs("\e[s", stdout); + if (n0) { + list_push_random(x0, 0, 0); + --n0; + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (n1) { + list_push_random(x1, 0, 60); + --n1; + } else { + puts("\n\n"); + put_separator(60); + } + + list_info(x0, 0); + list_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove2(list_t* x0, list_t* x1) { + + for (;x0->first || x1->first;) { + fputs("\e[s", stdout); + + if (x0->first) { + list_remove_random(x0, 0, 0); + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (x1->first) { + list_remove_random(x1, 0, 60); + } else { + puts("\n\n"); + put_separator(60); + } + + list_info(x0, 0); + list_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + + +void visual_sort2(list_t* x0, list_t* x1) { + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to sort list values:", "Try to sort list values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_sort(x0); + list_sort(x1); + + list_info(x0, 0); + list_print(x0, "sorted", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, "sorted", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); + + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to reverse list values:", "Try to reverse list values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_reverse(x0); + list_reverse(x1); + + list_info(x0, 0); + list_print(x0, "reversed", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, "reversed", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_extend(list_t* x, list_t* s) { + + puts("\e[s\e[36mTry to extend list:\e[m\n\n"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_info(x, 0); + list_print(x, "(dest)", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(s, 60); + list_print(s, "(src)", 60); + + psleep(900000); + fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout); + + list_extend(x, s); + + puts(""); + put_separator(0); + + list_info(x, 0); + list_print(x, "(dest)", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); +} diff --git a/tests/src/list/src/plug.c b/tests/src/list/src/plug.c new file mode 100644 index 0000000..eff34f8 --- /dev/null +++ b/tests/src/list/src/plug.c @@ -0,0 +1,20 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../../src/__internal/include.h" +#include "../../../include/random.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_map* map_duplicate (const vtype_map* x) { return 0; } +vtype_set* vset_duplicate (const vtype_set* x) { return 0; } + +void string_free(vtype_string* x) {} +void array_free (vtype_array* x) {} +void map_free (vtype_map* x) {} +void vset_free (vtype_set* x) {} + +int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); } +int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } +int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } +int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } diff --git a/tests/src/list/src/random.c b/tests/src/list/src/random.c new file mode 100644 index 0000000..9d74048 --- /dev/null +++ b/tests/src/list/src/random.c @@ -0,0 +1,83 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int remove_callback(void* v, ssize_t i, vtype t, void* _) { + struct { list_t* x; _Bool s; unsigned int p; } *x = _; + if (!x->s) { + print_container_value(0, v, t, 1, x->p); + } + + if (libcdsb_list_find(x->x, v, t, 0, 0, 1, 1)) { + return -2; + } + + return 0; +} + + +void list_push_random(list_t* x, _Bool silent, unsigned int hpos) { + + value_t v = random_value(); + _Bool r; + + if (random_boolean()) { + if (!silent) { + printf("\e[%dG\e[36mTry to push value to back of the list:\e[m\n", hpos+1); + } + r = libcdsb_list_update(x, -1, v.value, v.type, 1); + } else if (random_boolean()) { + if (!silent) { + printf("\e[%dG\e[36mTry to push value to front of the list:\e[m\n", hpos+1); + } + r = libcdsb_list_update(x, -1, v.value, v.type, 1); + } else { + ssize_t i = list_size(x); + i = random_uint64()% (i ? i : 1); + if (random_boolean()) i = ~i + 1; + + if (!silent) { + printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m into list:\e[m\n", hpos+1, i); + } + + r = libcdsb_list_update(x, i, v.value, v.type, 0); + } + + if (!silent) { + print_container_value(0, v.value, v.type, 1, hpos); + printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m"); + put_separator(hpos); + } +} + +void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) { + + size_t n = list_size(x); + ssize_t i = random_uint64()%n; + + if (random_boolean()) i = ~i + 1; + + if (random_boolean()) { + if (!silent) { + printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1); + print_container_value(0, &i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32, 0, hpos); + } + switch (list_remove_by_index(x, i)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } else { + struct { list_t* x; _Bool s; unsigned int p; } v = { .x = x, .s = silent, .p = hpos }; + + if (!silent) printf("\e[%dG\e[36mTry to remove value from list:\e[m\n", hpos+1); + + switch (libcdsb_list_get(x, i, &v, remove_callback, 0)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + case 2: if (!silent) printf("\n\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } + + put_separator(hpos); +}