Update set tests

This commit is contained in:
Gregory Lirent 2022-06-10 21:17:35 +03:00
parent d57f5ed2c6
commit f8878390a7
5 changed files with 166 additions and 187 deletions

View File

@ -6,8 +6,10 @@
int main(int argc, char** argv) {
test_init(argc, argv);
set_t x = vset_random(36, 0.1);
set_t x;
while(vset_remove_random(&x, 0.1)) {}
vset_free(&x);
vset_init(&x, random_uint8()%VTYPE_LDOUBLE + 1);
visual_push(&x, random_uint8()%17 + 16);
visual_remove(&x);
}

View File

@ -8,188 +8,11 @@
#include "../../include/test.h"
#include "../../include/time.h"
vtype_string* string_duplicate(const vtype_string* x) { return 0; }
vtype_array* array_duplicate (const vtype_array* x) { return 0; }
vtype_list* list_duplicate (const vtype_list* x) { return 0; }
vtype_map* map_duplicate (const vtype_map* x) { return 0; }
extern void vset_push_random(set_t* x, _Bool silent, unsigned int hpos);
extern void vset_remove_random(set_t* x, _Bool silent, unsigned int hpos);
void string_free(vtype_string* x) {}
void array_free (vtype_array* x) {}
void list_free (vtype_list* x) {}
void map_free (vtype_map* 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 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, void* _) {
print_container_value(0, v, t, false);
return 0;
}
static void vset_print(set_t* x, const char* prefix) {
print_container_values_prefix("Set", prefix);
vset_foreach(x, 0, vset_node_print);
put_separator();
}
static void vset_info(const set_t* 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(set_t* 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 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:
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, pause);
vset_push_random(&x, pause);
}
return x;
}
extern void vset_print(set_t* x, const char* prefix, unsigned int hpos);
extern void vset_info(const set_t* x, unsigned int hpos);
extern void vset_rbtree_print(set_t *x, const char* prefix, unsigned int hpos);
extern void visual_push(set_t* x, size_t n);
extern void visual_remove(set_t* x);

82
tests/src/set/src/io.c Normal file
View File

@ -0,0 +1,82 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int node_print_callback(const void* v, vtype t, void* _) {
print_container_value(0, v, t, 0, *(unsigned int*)_);
return 0;
}
static void rbtree_print(const rbnode_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 (rbnode_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\n", vnode_stringify(&s->value, t));
rbtree_print(s->left, t, x, false, hpos);
rbtree_print(s->right, t, x, true, hpos);
}
void vset_print(set_t* x, const char* prefix, unsigned int hpos) {
print_container_values_prefix("Set", prefix, 0);
vset_foreach(x, &hpos, node_print_callback);
put_separator(0);
}
void vset_info(const set_t* x, unsigned int hpos) {
print_container_info("Set", "nodes", &x->type, vset_size(x), -1, 0);
put_separator(0);
}
void vset_rbtree_print(set_t *x, const char* prefix, unsigned int hpos) {
print_container_values_prefix("Set", prefix, 0);
rbtree_print(x->root, x->type, 0, 0, hpos);
}
void visual_push(set_t* x, size_t n) {
while (n--) {
fputs("\e[s", stdout);
vset_push_random(x, 0, 0);
vset_info(x, 0);
vset_rbtree_print(x, 0, 0);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_remove(set_t* x) {
while (!rbnode_is_empty(x->root)) {
fputs("\e[s", stdout);
vset_remove_random(x, 0, 0);
vset_info(x, 0);
vset_rbtree_print(x, 0, 0);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}

20
tests/src/set/src/plug.c Normal file
View File

@ -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_list* list_duplicate (const vtype_list* x) { return 0; }
vtype_map* map_duplicate (const vtype_map* x) { return 0; }
void string_free(vtype_string* x) {}
void array_free (vtype_array* x) {}
void list_free (vtype_list* x) {}
void map_free (vtype_map* 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 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(); }

View File

@ -0,0 +1,52 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int remove_callback(const void* v, vtype t, void* _) {
struct { size_t n; set_t* x; unsigned int hp; } *d = _;
if (!d->n--) {
print_container_value(0, v, t, 0, d->hp);
if (libcdsb_vset_find(d->x, v, t, 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;
}
return 0;
}
void vset_push_random(set_t* x, _Bool silent, unsigned int hpos) {
value_t v = random_value();
if (!silent) {
printf("\e[%dG\e[36mUpdate value in set (\e[m\e[32;1m%s\e[m\e[36m):\e[m\n", hpos+1, vtype_name(x->type));
print_container_value(0, v.value, v.type, 1, hpos);
}
if (libcdsb_vset_insert(x, v.value, v.type)) {
if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1);
} else if (!silent) printf("\e[%dG\e[31;1mFAILURE\e[m\n", hpos+1);
if (!silent) put_separator(0);
}
void vset_remove_random(set_t* x, _Bool silent, unsigned int hpos) {
struct { size_t n; set_t* x; unsigned int hp; } d = { .n = vset_size(x), .x = x, .hp = hpos };
if (!silent)
printf("\e[%dG\e[36mTry to remove value from set (\e[m\e[32;1m%s\e[m\e[36m):\e[m\n", hpos+1, libcdsb_vtype_name(x->type));
if (d.n) {
d.n = random_uint32()%d.n;
vset_foreach(x, &d, remove_callback);
} else if (!silent) {
printf("\e[%dG\e[32;1m\nFAILURE\e[m\n", hpos+1);
}
if (!silent) put_separator(hpos);
}