Update set tests

This commit is contained in:
Gregory Lirent 2022-06-06 22:08:10 +03:00
parent 566de8019e
commit aedc18c9fa
2 changed files with 125 additions and 6 deletions

View File

@ -3,11 +3,9 @@
#include "plug.h"
vtype_set* vset_duplicate(const vtype_set* x) { return 0; }
int vset_compare(const vtype_set* s0, const vtype_set* s1) { return random_int8(); }
void vset_free(vtype_set* x) {}
int main(int argc, char** argv) {
test_init(argc, argv);
vtype_set x = vset_random(32);
vset_free(&x);
}

View File

@ -1,7 +1,8 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../../include/set.h"
#include "../../../include/extra/set.h"
#include "../../../src/__internal/rbtree.h"
#include "../../include/random.h"
#include "../../include/test.h"
@ -21,3 +22,123 @@ int string_compare(const vtype_string* s0, const vtype_string* s1) { return rand
int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); }
int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); }
int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); }
static int vset_node_print(const void* v, vtype t) {
print_container_value(0, v, t, false);
return 0;
}
static void vset_print(const vtype_set* x, const char* prefix) {
print_container_values_prefix("Set", prefix);
vset_foreach(x, vset_node_print);
put_separator();
}
static void vset_info(const vtype_set* x) {
print_container_info("Set", "nodes", &x->type, vset_size(x), -1);
put_separator();
}
static void rbtree_print(const rbnode_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 (rbnode_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\n", libcdsb_vtype_stringify(vnode_peek(&s->value, t), t));
rbtree_print(s->left, t, x, false);
rbtree_print(s->right, t, x, true);
}
static void vset_push_random(vtype_set* x, double pause) {
var_t _;
vtype t;
printf("\e[s\e[36mTry to insert into set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type));
switch (random_uint8()%13) {
default:
case 0: _.b = random_boolean(); print_container_value(0, &_, t = VTYPE_BOOLEAN, 1); break;
case 1: _.u8 = random_uint8(); print_container_value(0, &_, t = VTYPE_UINT8, 1); break;
case 2: _.u16 = random_uint16(); print_container_value(0, &_, t = VTYPE_UINT16, 1); break;
case 3: _.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_UINT32, 1); break;
case 4: _.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_UINT64, 1); break;
case 5: _.u8 = random_int8(); print_container_value(0, &_, t = VTYPE_INT8, 1); break;
case 6: _.u16 = random_int16(); print_container_value(0, &_, t = VTYPE_INT16, 1); break;
case 7: _.u32 = random_int32(); print_container_value(0, &_, t = VTYPE_INT32, 1); break;
case 8: _.u64 = random_int64(); print_container_value(0, &_, t = VTYPE_INT64, 1); break;
case 9: _.f = random_float(); print_container_value(0, &_, t = VTYPE_FLOAT, 1); break;
case 10: _.d = random_double(); print_container_value(0, &_, t = VTYPE_DOUBLE, 1); break;
case 11: _.ld = random_ldouble(); print_container_value(0, &_, t = VTYPE_LDOUBLE, 1); break;
case 12: if (sizeof(void*) == 8) {
_.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break;
} else {
_.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break;
}
}
if (libcdsb_vset_insert(x, &_, t)) {
puts("\e[32;1mSUCCESS\e[m");
} else {
puts("\e[31;1mFAILURE\e[m");
}
put_separator();
rbtree_print(x->root, x->type, 0, 0);
put_separator();
psleep(pause * 1000000);
fputs("\e[u\e[J", stdout);
}
static vtype_set vset_random(unsigned int n) {
vtype_set x;
switch (random_uint8()%12) {
default:
case 0: vset_init(&x, VTYPE_UINT8); break;
case 1: vset_init(&x, VTYPE_UINT16); break;
case 2: vset_init(&x, VTYPE_UINT32); break;
case 3: vset_init(&x, VTYPE_UINT64); break;
case 4: vset_init(&x, VTYPE_INT8); break;
case 5: vset_init(&x, VTYPE_INT16); break;
case 6: vset_init(&x, VTYPE_INT32); break;
case 7: vset_init(&x, VTYPE_INT64); break;
case 8: vset_init(&x, VTYPE_POINTER); break;
case 9: vset_init(&x, VTYPE_FLOAT); break;
case 10: vset_init(&x, VTYPE_DOUBLE); break;
case 11: vset_init(&x, VTYPE_LDOUBLE); break;
}
if (n) {
while (--n) vset_push_random(&x, 0.95);
vset_push_random(&x, 1.95);
}
vset_info(&x);
vset_print(&x, 0);
return x;
}