From 0fdca5e083caeb0a1a149cff1ae2a9fca67ca047 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 7 Jun 2022 21:24:17 +0300 Subject: [PATCH] Update tests --- tests/src/array/plug.h | 4 +-- tests/src/list/plug.h | 4 +-- tests/src/set/main.c | 4 ++- tests/src/set/plug.h | 77 +++++++++++++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index afd1616..a54e99f 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -80,14 +80,14 @@ static vtype_array array_random(unsigned int n) { return x; } -static int array_value_print(void* v, ssize_t i, vtype t) { +static int array_value_print(void* v, ssize_t i, vtype t, void* _) { 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); + array_foreach(x, 0, array_value_print); put_separator(); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index b700edd..d3571ed 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -60,14 +60,14 @@ static void list_push_random(vtype_list* x, vtype_bool front) { } } -static int list_node_print(void* v, ssize_t i, vtype t) { +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, list_node_print); + list_foreach(x, 0, list_node_print); put_separator(); } diff --git a/tests/src/set/main.c b/tests/src/set/main.c index 61f3ef6..d6bd747 100644 --- a/tests/src/set/main.c +++ b/tests/src/set/main.c @@ -6,6 +6,8 @@ int main(int argc, char** argv) { test_init(argc, argv); - vtype_set x = vset_random(32); + vtype_set x = vset_random(36, 0.1); + + while(vset_remove_random(&x, 0.1)) {} vset_free(&x); } diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h index ab7abdb..68265d3 100644 --- a/tests/src/set/plug.h +++ b/tests/src/set/plug.h @@ -24,18 +24,18 @@ int list_compare (const vtype_list* s0, const vtype_list* s1) { return rand int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } -static int vset_node_print(const void* v, vtype t) { +static int vset_node_print(const void* v, vtype t, void* _) { print_container_value(0, v, t, false); return 0; } -static void vset_print(const vtype_set* x, const char* prefix) { +static void vset_print(const set_t* x, const char* prefix) { print_container_values_prefix("Set", prefix); - vset_foreach(x, vset_node_print); + vset_foreach(x, 0, vset_node_print); put_separator(); } -static void vset_info(const vtype_set* x) { +static void vset_info(const set_t* x) { print_container_info("Set", "nodes", &x->type, vset_size(x), -1); put_separator(); } @@ -51,7 +51,6 @@ static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { if (rbnode_is_empty(s)) return; - memcpy(x, ind, n); memcpy(x + n, " \0 ", 9); fputs(ind, stdout); @@ -72,7 +71,7 @@ static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { rbtree_print(s->right, t, x, true); } -static void vset_push_random(vtype_set* x, double pause) { +static void vset_push_random(set_t* x, double pause) { var_t _; vtype t; @@ -113,8 +112,63 @@ static void vset_push_random(vtype_set* x, double pause) { fputs("\e[u\e[J", stdout); } -static vtype_set vset_random(unsigned int n) { - vtype_set x; +static int vset_node_remove_random(const void* v, vtype t, void* data) { + struct { + size_t n; + set_t* set; + double pause; + } *d = data; + + if (!d->n--) { + print_container_value(0, v, t, 1); + + if (libcdsb_vset_find(0, d->set, v, t, 1)) { + puts("\e[32;1mSUCCESS\e[m"); + } else { + puts("\e[31;1mFAILURE\e[m"); + abort(); + } + + put_separator(); + + rbtree_print(d->set->root, d->set->type, 0, 0); + put_separator(); + psleep(d->pause * 1000000); + + return true; + } + return 0; +} + + +static bool vset_remove_random(set_t* x, double pause) { + struct { + size_t n; + set_t* set; + double pause; + } d; + d.n = vset_size(x); + d.set = x; + d.pause = pause; + + printf("\e[s\e[36mTry to remove value from set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); + + if (d.n) { + d.n = random_uint32()%d.n; + } else goto end_; + + if (!vset_foreach(x, &d, vset_node_remove_random)) { + end_: + puts("\e[u\e[J\e[32;1mSet is empty\e[m"); + return false; + } + + fputs("\e[u\e[J", stdout); + return true; +} + +static set_t vset_random(unsigned int n, double pause) { + set_t x; switch (random_uint8()%12) { default: @@ -133,12 +187,9 @@ static vtype_set vset_random(unsigned int n) { } if (n) { - while (--n) vset_push_random(&x, 0.95); - vset_push_random(&x, 1.95); + while (--n) vset_push_random(&x, pause); + vset_push_random(&x, pause); } - vset_info(&x); - vset_print(&x, 0); - return x; }