From d57f5ed2c6ef9435867e83e21fefdb5ee14ba3b7 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 21:17:07 +0300 Subject: [PATCH] Update map tests --- tests/src/map/plug.h | 1 - tests/src/map/src/io.c | 71 +++++++++++++++++++------------------- tests/src/map/src/random.c | 10 +++--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h index bb64636..a2ae449 100644 --- a/tests/src/map/plug.h +++ b/tests/src/map/plug.h @@ -10,7 +10,6 @@ void map_push_random(map_t* x, _Bool silent, unsigned int hpos); void map_remove_random(map_t* x, _Bool silent, unsigned int hpos); -void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br); void map_print(map_t* x, const char* prefix, unsigned int hpos); void map_info(const map_t* x, unsigned int hpos); void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos); diff --git a/tests/src/map/src/io.c b/tests/src/map/src/io.c index 5bb48f4..a01da4b 100644 --- a/tests/src/map/src/io.c +++ b/tests/src/map/src/io.c @@ -8,6 +8,39 @@ static int node_print_callback(const void* k, vtype kt, void* v, vtype vt, void* return 0; } +static void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br, unsigned int hpos) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (mnode_is_empty(s)) return; + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + + printf("\e[%dG%s", hpos+1, ind); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); + printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); + + rbtree_print(s->left, t, x, false, hpos); + rbtree_print(s->right, t, x, true, hpos); +} + void map_print(map_t* x, const char* prefix, unsigned int hpos) { print_container_values_prefix("Map", prefix, 0); map_foreach(x, &hpos, node_print_callback); @@ -21,41 +54,9 @@ void map_info(const map_t* x, unsigned int hpos) { void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos) { print_container_values_prefix("Map", prefix, 0); - rbtree_print(x->root, x->type, 0, 0); + rbtree_print(x->root, x->type, 0, 0, hpos); } -void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br) { - if (!ind) { - ind = "\e[36m"; - br = 1; - } - - size_t n = strlen(ind); - char x[n + 10]; - - if (mnode_is_empty(s)) return; - - memcpy(x, ind, n); - memcpy(x + n, " \0 ", 9); - - fputs(ind, stdout); - - if (br) { - fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); - } else { - fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); - memcpy(x + n, "│", 3); - x[n + 5] = ' '; - } - - fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); - printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); - printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); - printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); - - rbtree_print(s->left, t, x, false); - rbtree_print(s->right, t, x, true); -} void visual_push(map_t* x, size_t n) { while (n--) { @@ -64,7 +65,7 @@ void visual_push(map_t* x, size_t n) { map_push_random(x, 0, 0); map_info(x, 0); - rbtree_print(x->root, x->type, 0, 0); + map_rbtree_print(x, 0, 0); psleep(100000); fputs("\e[u\e[J", stdout); @@ -76,7 +77,7 @@ void visual_remove(map_t* x) { fputs("\e[s", stdout); map_remove_random(x, 0, 0); map_info(x, 0); - rbtree_print(x->root, x->type, 0, 0); + map_rbtree_print(x, 0, 0); psleep(100000); fputs("\e[u\e[J", stdout); } diff --git a/tests/src/map/src/random.c b/tests/src/map/src/random.c index e917874..c4615f2 100644 --- a/tests/src/map/src/random.c +++ b/tests/src/map/src/random.c @@ -9,9 +9,9 @@ static int remove_callback(const void* k, vtype kt, void* v, vtype vt, void* _) if (!d->n--) { print_container_value(0, k, kt, 0, d->hp); - if (libcdsb_map_find(d->x, k, kt, 0, 0, 1)) { - printf("\e[%dG\e[32;1mSUCCESS\e[m", d->hp+1); - } else printf("\e[%dG\e[31;1mFAILURE\e[m", d->hp+1); + if (libcdsb_map_find(d->x, k, kt, 0, 0, 1) == 0) { + printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1); + } else printf("\e[%dG\e[31;1mFAILURE\e[m\n", d->hp+1); return -2; } @@ -29,8 +29,8 @@ void map_push_random(map_t* x, _Bool silent, unsigned int hpos) { } if (libcdsb_map_update(x, k.value, k.type, v.value, v.type)) { - if (!silent) printf("\e[%dG\\e[33;1mCHANGE\e[m", hpos+1); - } else if (!silent) printf("\e[%dG\\e[32;1mINSERT\e[m", hpos+1); + if (!silent) printf("\e[%dG\e[33;1mCHANGE\e[m\n", hpos+1); + } else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1); if (!silent) put_separator(0); }