Update tests

This commit is contained in:
Gregory Lirent 2022-06-07 21:24:17 +03:00
parent 0760d7a6a8
commit 0fdca5e083
4 changed files with 71 additions and 18 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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;
}