Refactor tests

This commit is contained in:
Gregory Lirent 2023-03-26 12:08:37 +03:00
parent fba8de4878
commit 384d6de89f
17 changed files with 198 additions and 281 deletions

View File

@ -7,11 +7,6 @@
#ifndef LIBCDSB_TESTS_RANDOM_H #ifndef LIBCDSB_TESTS_RANDOM_H
#define LIBCDSB_TESTS_RANDOM_H #define LIBCDSB_TESTS_RANDOM_H
typedef struct {
var_t value[1];
vtype type;
} value_t;
extern int random_init(int argc, char** argv); extern int random_init(int argc, char** argv);
extern vtype_bool random_boolean(); extern vtype_bool random_boolean();
@ -32,6 +27,6 @@ extern vtype_int64 random_int64();
extern char random_ascii_char(); extern char random_ascii_char();
extern unsigned int random_unicode_symbol(); extern unsigned int random_unicode_symbol();
extern value_t random_value(); extern var_t random_variable(int rand);
#endif /* LIBCDSB_TESTS_RANDOM_H */ #endif /* LIBCDSB_TESTS_RANDOM_H */

View File

@ -9,7 +9,7 @@
extern void put_separator(unsigned int hpos); extern void put_separator(unsigned int hpos);
extern void print_container_values_prefix(const char* name, const char* prefix, unsigned int hpos); extern void print_container_values_prefix(const char* name, const char* prefix, unsigned int hpos);
extern void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type, unsigned int hpos); extern void print_container_value(const ssize_t* index, vtype_variable var, _Bool print_type, unsigned int hpos);
extern void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb, unsigned int hpos); extern void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb, unsigned int hpos);
extern void test_init(int argc, char** argv); extern void test_init(int argc, char** argv);

View File

@ -4,7 +4,7 @@
#include "../plug.h" #include "../plug.h"
static int array_value_print(vtype_variable v, ssize_t i, void* _) { static int array_value_print(vtype_variable v, ssize_t i, void* _) {
print_container_value(&i, v.pointer, v.type, 1, *(unsigned int*)_); print_container_value(&i, v, 1, *(unsigned int*)_);
return 0; return 0;
} }

View File

@ -5,7 +5,7 @@
static int remove_callback(vtype_variable v, ssize_t i, void* _) { static int remove_callback(vtype_variable v, ssize_t i, void* _) {
struct { arr_t* x; _Bool s; unsigned int p; } *x = _; struct { arr_t* x; _Bool s; unsigned int p; } *x = _;
if (!x->s) print_container_value(0, v.pointer, v.type, 1, x->p); if (!x->s) print_container_value(0, v, 1, x->p);
if (libcdsb_array_find(x->x, libcdsb_variable_build(v.pointer, v.type), 0, 0, 1, 1)) if (libcdsb_array_find(x->x, libcdsb_variable_build(v.pointer, v.type), 0, 0, 1, 1))
return -2; return -2;
@ -13,17 +13,17 @@ static int remove_callback(vtype_variable v, ssize_t i, void* _) {
} }
static int change_callback(vtype_variable v, ssize_t i, void* _) { static int change_callback(vtype_variable v, ssize_t i, void* _) {
struct { value_t v; _Bool s; unsigned int p; } *x = _; struct { vtype_variable v; _Bool s; unsigned int p; } *x = _;
while (x->v.type != v.type) { x->v = random_value(); } while (x->v.type != v.type) { x->v = random_variable(-1); }
memcpy(v.pointer, &x->v, vtype_size(v.type)); memcpy(v.pointer, x->v.pointer, vtype_size(v.type));
return 0; return 0;
} }
void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) { void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) {
struct { value_t v; _Bool s; unsigned int p; } _ = { .v = random_value(), .s = silent, .p = hpos }; struct { vtype_variable v; _Bool s; unsigned int p; } _ = { .v = random_variable(-1), .s = silent, .p = hpos };
_Bool r; _Bool r;
if (random_boolean()) { if (random_boolean()) {
@ -31,7 +31,7 @@ void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) {
printf("\e[%dG\e[36mTry to push value to back of the array:\e[m\n", hpos+1); printf("\e[%dG\e[36mTry to push value to back of the array:\e[m\n", hpos+1);
} }
r = libcdsb_array_insert(x, libcdsb_variable_build(_.v.value, _.v.type)) >= 0; r = libcdsb_array_insert(x, _.v) >= 0;
} else { } else {
ssize_t i = array_size(x); ssize_t i = array_size(x);
@ -46,7 +46,7 @@ void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) {
} }
if (!silent) { if (!silent) {
print_container_value(0, _.v.value, _.v.type, 1, hpos); print_container_value(0, _.v, 1, hpos);
printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m"); printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m");
put_separator(hpos); put_separator(hpos);
} }
@ -62,7 +62,7 @@ void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos) {
if (random_boolean()) { if (random_boolean()) {
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1); printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1);
print_container_value(0, &i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32, 0, hpos); print_container_value(0, libcdsb_variable_build(&i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32), 0, hpos);
} }
switch (array_remove_by_index(x, i)) { switch (array_remove_by_index(x, i)) {
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;

View File

@ -4,7 +4,7 @@
#include "../plug.h" #include "../plug.h"
static int node_print_callback(vtype_variable k, vtype_variable v, void* _) { static int node_print_callback(vtype_variable k, vtype_variable v, void* _) {
print_container_value(0, k.pointer, k.type, 0, *(unsigned int*)_); print_container_value(0, k, 0, *(unsigned int*)_);
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@ static int remove_callback(vtype_variable k, vtype_variable v, void* _) {
struct { size_t n; dict_t* x; unsigned int hp; } *d = _; struct { size_t n; dict_t* x; unsigned int hp; } *d = _;
if (!d->n--) { if (!d->n--) {
print_container_value(0, k.pointer, k.type, 0, d->hp); print_container_value(0, k, 0, d->hp);
if (libcdsb_dict_find(d->x, k, 0, 0, 1) == 0) { if (libcdsb_dict_find(d->x, k, 0, 0, 1) == 0) {
printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1); printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1);
@ -20,15 +20,15 @@ static int remove_callback(vtype_variable k, vtype_variable v, void* _) {
void dict_push_random(dict_t* x, _Bool silent, unsigned int hpos) { void dict_push_random(dict_t* x, _Bool silent, unsigned int hpos) {
value_t k = random_value(); vtype_variable k = random_variable(-1);
value_t v = random_value(); vtype_variable v = random_variable(-1);
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mUpdate value in dict with key:\e[m\n", hpos+1); printf("\e[%dG\e[36mUpdate value in dict with key:\e[m\n", hpos+1);
print_container_value(0, k.value, k.type, 1, hpos); print_container_value(0, k, 1, hpos);
} }
if (libcdsb_dict_update(x, libcdsb_variable_build(k.value, k.type), libcdsb_variable_build(v.value, v.type), 0, 0)) { if (libcdsb_dict_update(x, k, v, 0, 0)) {
if (!silent) printf("\e[%dG\e[33;1mCHANGE\e[m\n", 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); } else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1);

View File

@ -6,29 +6,29 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
test_init(argc, argv); test_init(argc, argv);
value_t x = random_container(0); vtype_variable x = random_container(0);
switch (x.type) { switch (x.type) {
default: abort(); default: abort();
case VTYPE_ARRAY: case VTYPE_ARRAY:
array_info((void*)x.value, 0); array_info((void*)x.pointer, 0);
array_free((void*)x.value); array_free((void*)x.pointer);
return 0; return 0;
case VTYPE_MAP: case VTYPE_MAP:
map_info((void*)x.value, 0); map_info((void*)x.pointer, 0);
map_free((void*)x.value); map_free((void*)x.pointer);
return 0; return 0;
case VTYPE_LIST: case VTYPE_LIST:
list_info((void*)x.value, 0); list_info((void*)x.pointer, 0);
list_free((void*)x.value); list_free((void*)x.pointer);
return 0; return 0;
case VTYPE_SET: case VTYPE_SET:
vset_info((void*)x.value, 0); vset_info((void*)x.pointer, 0);
vset_free((void*)x.value); vset_free((void*)x.pointer);
return 0; return 0;
case VTYPE_DICT: case VTYPE_DICT:
dict_info((void*)x.value, 0); dict_info((void*)x.pointer, 0);
dict_free((void*)x.value); dict_free((void*)x.pointer);
return 0; return 0;
} }
} }

View File

@ -12,8 +12,8 @@
#include "../../include/test.h" #include "../../include/test.h"
#include "../../include/time.h" #include "../../include/time.h"
extern value_t random_container(bool embd); extern vtype_variable random_container(bool embd);
extern value_t real_random_value(bool embd); extern vtype_variable real_random_value(bool embd);
extern void array_info(arr_t* x, unsigned int hpos); extern void array_info(arr_t* x, unsigned int hpos);
extern void list_info (list_t* x, unsigned int hpos); extern void list_info (list_t* x, unsigned int hpos);

View File

@ -5,275 +5,188 @@
#define MAX_ELEMENTS 2000 #define MAX_ELEMENTS 2000
static arr_t random_array(bool embd); static vtype_variable random_array(bool embd);
static list_t random_list (bool embd); static vtype_variable random_list (bool embd);
static set_t random_set (bool embd); static vtype_variable random_set (bool embd);
static map_t random_map (bool embd); static vtype_variable random_map (bool embd);
static dict_t random_dict (bool embd); static vtype_variable random_dict (bool embd);
static str_t random_string() { static vtype_variable random_string() {
str_t x; static str_t values[16];
static size_t pos = 0;
if (pos == 16) pos = 0;
str_t* x = &values[pos++];
size_t n = random_uint16()%MAX_ELEMENTS; size_t n = random_uint16()%MAX_ELEMENTS;
string_init(&x, 0); string_init(x, 0);
while (n--) { while (n--) {
string_concat(&x, random_unicode_symbol()); string_concat(x, random_unicode_symbol());
} }
return x; return libcdsb_variable_build(x, VTYPE_STRING);
} }
static value_t random_value2() { static vtype_variable random_variable2() {
value_t v; vtype_variable v;
switch (random_uint8()%14) { int rand;
default:
case 0: v.value[0].b = random_boolean(); v.type = VTYPE_BOOLEAN; break; switch (rand = random_uint8()%14) {
case 1: v.value[0].u8 = random_uint8 (); v.type = VTYPE_UINT8; break; default: v = random_variable(rand); break;
case 2: v.value[0].u16 = random_uint16 (); v.type = VTYPE_UINT16; break; case 13: v = random_string(); break;
case 3: v.value[0].u32 = random_uint32 (); v.type = VTYPE_UINT32; break;
case 4: v.value[0].u64 = random_uint64 (); v.type = VTYPE_UINT64; break;
case 5: v.value[0].u8 = random_int8 (); v.type = VTYPE_INT8; break;
case 6: v.value[0].u16 = random_uint16 (); v.type = VTYPE_INT16; break;
case 7: v.value[0].u32 = random_int32 (); v.type = VTYPE_INT32; break;
case 8: v.value[0].u64 = random_int64 (); v.type = VTYPE_INT64; break;
case 9: v.value[0].f = random_float (); v.type = VTYPE_FLOAT; break;
case 10: v.value[0].d = random_double (); v.type = VTYPE_DOUBLE; break;
case 11: v.value[0].ld = random_ldouble(); v.type = VTYPE_LDOUBLE; break;
case 12: v.value[0].ptr = (void*)(uintptr_t)random_uint64(); v.type = VTYPE_POINTER; break;
case 13: v.value[0].s = random_string(); v.type = VTYPE_STRING; break;
} }
return v; return v;
} }
static value_t random_value_by_type(vtype type, bool embd) { static vtype_variable random_value_by_type(vtype type, bool embd) {
value_t v; vtype_variable v;
switch ((v.type = type)) { switch ((v.type = type)) {
default: default:
case VTYPE_BOOLEAN: v.value[0].b = random_boolean(); break; case VTYPE_BOOLEAN: v = random_variable( 0); break;
case VTYPE_UINT8: v.value[0].u8 = random_uint8 (); break; case VTYPE_UINT8: v = random_variable( 1); break;
case VTYPE_UINT16: v.value[0].u16 = random_uint16 (); break; case VTYPE_UINT16: v = random_variable( 2); break;
case VTYPE_UINT32: v.value[0].u32 = random_uint32 (); break; case VTYPE_UINT32: v = random_variable( 3); break;
case VTYPE_UINT64: v.value[0].u64 = random_uint64 (); break; case VTYPE_UINT64: v = random_variable( 4); break;
case VTYPE_INT8: v.value[0].u8 = random_int8 (); break; case VTYPE_INT8: v = random_variable( 5); break;
case VTYPE_INT16: v.value[0].u16 = random_uint16 (); break; case VTYPE_INT16: v = random_variable( 6); break;
case VTYPE_INT32: v.value[0].u32 = random_int32 (); break; case VTYPE_INT32: v = random_variable( 7); break;
case VTYPE_INT64: v.value[0].u64 = random_int64 (); break; case VTYPE_INT64: v = random_variable( 8); break;
case VTYPE_FLOAT: v.value[0].f = random_float (); break; case VTYPE_FLOAT: v = random_variable( 9); break;
case VTYPE_DOUBLE: v.value[0].d = random_double (); break; case VTYPE_DOUBLE: v = random_variable(10); break;
case VTYPE_LDOUBLE: v.value[0].ld = random_ldouble(); break; case VTYPE_LDOUBLE: v = random_variable(11); break;
case VTYPE_STRING: v.value[0].s = random_string(); break; case VTYPE_STRING: v = random_string (); break;
case VTYPE_ARRAY: v.value[0].a = random_array(embd); break; case VTYPE_ARRAY: v = random_array (embd); break;
case VTYPE_MAP: v.value[0].m = random_map (embd); break; case VTYPE_MAP: v = random_map (embd); break;
case VTYPE_DICT: v.value[0].vd = random_dict (embd); break; case VTYPE_DICT: v = random_dict (embd); break;
case VTYPE_LIST: v.value[0].l = random_list (embd); break; case VTYPE_LIST: v = random_list (embd); break;
case VTYPE_SET: v.value[0].vs = random_set (embd); break; case VTYPE_SET: v = random_set (embd); break;
case VTYPE_POINTER: v.value[0].ptr = (void*)(uintptr_t)random_uint64(); break; case VTYPE_POINTER: v = random_variable(12); break;
} }
return v; return v;
} }
static arr_t random_array(bool embd) { static vtype_variable random_array(bool embd) {
arr_t x; static arr_t values[16];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100); static size_t pos = 0;
value_t v = (!embd) ? random_value2() : real_random_value(1);
array_init(&x, v.type); if (pos == 16) pos = 0;
while(n--) { arr_t* x = &values[pos++];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
vtype_variable v = (!embd) ? random_variable2() : real_random_value(1);
libcdsb_array_insert(&x, libcdsb_variable_build(v.value, v.type)); array_init(x, v.type);
switch (v.type) { for(;;) {
default: break; libcdsb_array_attach(x, v);
case VTYPE_STRING: string_free((void*)v.value); break; if (--n) v = random_value_by_type(v.type, 1);
case VTYPE_ARRAY: array_free((void*)v.value); break; else break;
case VTYPE_LIST: list_free((void*)v.value); break;
case VTYPE_MAP: map_free((void*)v.value); break;
case VTYPE_SET: vset_free((void*)v.value); break;
case VTYPE_DICT: dict_free((void*)v.value); break;
}
v = random_value_by_type(v.type, 1);
} }
switch (v.type) { return libcdsb_variable_build(x, VTYPE_ARRAY);
default: break;
case VTYPE_STRING: string_free((void*)v.value); break;
case VTYPE_ARRAY: array_free((void*)v.value); break;
case VTYPE_LIST: list_free((void*)v.value); break;
case VTYPE_MAP: map_free((void*)v.value); break;
case VTYPE_SET: vset_free((void*)v.value); break;
case VTYPE_DICT: dict_free((void*)v.value); break;
}
return x;
} }
static set_t random_set(bool embd) { static vtype_variable random_set(bool embd) {
set_t x; static set_t values[16];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100); static size_t pos = 0;
value_t v = (!embd) ? random_value2() : real_random_value(1);
vset_init(&x, v.type); if (pos == 16) pos = 0;
while(n--) { set_t* x = &values[pos++];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
vtype_variable v = (!embd) ? random_variable2() : real_random_value(1);
libcdsb_vset_insert(&x, libcdsb_variable_build(v.value, v.type)); vset_init(x, v.type);
switch (v.type) { for(;;) {
default: break; libcdsb_vset_attach(x, v);
case VTYPE_STRING: string_free((void*)v.value); break; if (--n) v = random_value_by_type(v.type, 1);
case VTYPE_ARRAY: array_free((void*)v.value); break; else break;
case VTYPE_LIST: list_free((void*)v.value); break;
case VTYPE_MAP: map_free((void*)v.value); break;
case VTYPE_SET: vset_free((void*)v.value); break;
case VTYPE_DICT: dict_free((void*)v.value); break;
}
v = random_value_by_type(v.type, 1);
} }
switch (v.type) { return libcdsb_variable_build(x, VTYPE_SET);
default: break;
case VTYPE_STRING: string_free((void*)v.value); break;
case VTYPE_ARRAY: array_free((void*)v.value); break;
case VTYPE_LIST: list_free((void*)v.value); break;
case VTYPE_MAP: map_free((void*)v.value); break;
case VTYPE_SET: vset_free((void*)v.value); break;
case VTYPE_DICT: dict_free((void*)v.value); break;
}
return x;
} }
static list_t random_list(bool embd) { static vtype_variable random_list(bool embd) {
list_t x; static list_t values[16];
value_t v; static size_t pos = 0;
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
list_init(&x); if (pos == 16) pos = 0;
list_t* x = &values[pos++];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
vtype_variable v;
list_init(x);
while(n--) { while(n--) {
v = (!embd) ? random_value2() : real_random_value(1); v = (!embd) ? random_variable2() : real_random_value(1);
libcdsb_list_insert(&x, -1, libcdsb_variable_build(v.value, v.type), 1, 0, 0); libcdsb_list_attach(x, -1, v, 1, 0, 0);
switch (v.type) {
default: break;
case VTYPE_STRING: string_free((void*)v.value); break;
case VTYPE_ARRAY: array_free ((void*)v.value); break;
case VTYPE_LIST: list_free ((void*)v.value); break;
case VTYPE_MAP: map_free ((void*)v.value); break;
case VTYPE_SET: vset_free ((void*)v.value); break;
case VTYPE_DICT: dict_free ((void*)v.value); break;
}
} }
return x; return libcdsb_variable_build(x, VTYPE_LIST);
} }
static map_t random_map(bool embd) { static vtype_variable random_map(bool embd) {
map_t x; static map_t values[16];
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100); static size_t pos = 0;
value_t k = (!embd) ? random_value2() : real_random_value(1);
map_init(&x, k.type); if (pos == 16) pos = 0;
while(n--) { map_t* x = &values[pos++];
value_t v = (!embd) ? random_value2() : real_random_value(1); size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
vtype_variable k = (!embd) ? random_variable2() : real_random_value(1);
vtype_variable v;
libcdsb_map_update(&x, libcdsb_variable_build(k.value, k.type), libcdsb_variable_build(v.value, v.type), 0, 0); map_init(x, k.type);
switch (k.type) { for(;;) {
default: break; v = (!embd) ? random_variable2() : real_random_value(1);
case VTYPE_STRING: string_free((void*)k.value); break; libcdsb_map_inject(x, k, v, 0, 0);
case VTYPE_ARRAY: array_free((void*)k.value); break; if (--n) k = random_value_by_type(k.type, 1);
case VTYPE_LIST: list_free((void*)k.value); break; else break;
case VTYPE_MAP: map_free((void*)k.value); break;
case VTYPE_SET: vset_free((void*)k.value); break;
case VTYPE_DICT: dict_free((void*)k.value); break;
}
switch (v.type) {
default: break;
case VTYPE_STRING: string_free((void*)v.value); break;
case VTYPE_ARRAY: array_free ((void*)v.value); break;
case VTYPE_LIST: list_free ((void*)v.value); break;
case VTYPE_MAP: map_free ((void*)v.value); break;
case VTYPE_SET: vset_free ((void*)v.value); break;
case VTYPE_DICT: dict_free ((void*)v.value); break;
}
k = random_value_by_type(k.type, 1);
} }
switch (k.type) { return libcdsb_variable_build(x, VTYPE_MAP);
default: break;
case VTYPE_STRING: string_free((void*)k.value); break;
case VTYPE_ARRAY: array_free((void*)k.value); break;
case VTYPE_LIST: list_free((void*)k.value); break;
case VTYPE_MAP: map_free((void*)k.value); break;
case VTYPE_SET: vset_free((void*)k.value); break;
case VTYPE_DICT: dict_free((void*)k.value); break;
}
return x;
} }
static dict_t random_dict(bool embd) { static vtype_variable random_dict(bool embd) {
dict_t x; static dict_t values[16];
value_t k, v; static size_t pos = 0;
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
dict_init(&x); if (pos == 16) pos = 0;
while(n--) { dict_t* x = &values[pos++];
k = (!embd) ? random_value2() : real_random_value(1); size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
v = (!embd) ? random_value2() : real_random_value(1); vtype_variable k;
libcdsb_dict_update(&x, libcdsb_variable_build(k.value, k.type), libcdsb_variable_build(v.value, v.type), 0, 0); vtype_variable v;
switch (v.type) { dict_init(x);
default: break;
case VTYPE_STRING: string_free((void*)v.value); break;
case VTYPE_ARRAY: array_free ((void*)v.value); break;
case VTYPE_LIST: list_free ((void*)v.value); break;
case VTYPE_MAP: map_free ((void*)v.value); break;
case VTYPE_SET: vset_free ((void*)v.value); break;
case VTYPE_DICT: dict_free ((void*)v.value); break;
}
switch (k.type) { while (n--) {
default: break; k = (!embd) ? random_variable2() : real_random_value(1);
case VTYPE_STRING: string_free((void*)k.value); break; v = (!embd) ? random_variable2() : real_random_value(1);
case VTYPE_ARRAY: array_free ((void*)k.value); break; libcdsb_dict_inject(x, k, v, 0, 0);
case VTYPE_LIST: list_free ((void*)k.value); break;
case VTYPE_MAP: map_free ((void*)k.value); break;
case VTYPE_SET: vset_free ((void*)k.value); break;
case VTYPE_DICT: dict_free ((void*)k.value); break;
}
} }
return x; return libcdsb_variable_build(x, VTYPE_DICT);
} }
value_t random_container(bool embd) { vtype_variable random_container(bool embd) {
value_t v;
switch (random_uint8()%5) { switch (random_uint8()%5) {
default: default:
case 0: v.value[0].a = random_array(embd); v.type = VTYPE_ARRAY; break; case 0: return random_array(embd);
case 1: v.value[0].m = random_map (embd); v.type = VTYPE_MAP; break; case 1: return random_map (embd);
case 2: v.value[0].vd = random_dict (embd); v.type = VTYPE_DICT; break; case 2: return random_dict (embd);
case 3: v.value[0].l = random_list (embd); v.type = VTYPE_LIST; break; case 3: return random_list (embd);
case 4: v.value[0].vs = random_set (embd); v.type = VTYPE_SET; break; case 4: return random_set (embd);
} }
return v;
} }
value_t real_random_value(bool embd) { vtype_variable real_random_value(bool embd) {
return random_boolean() ? random_value2() : random_container(embd); return random_boolean() ? random_variable2() : random_container(embd);
} }

View File

@ -4,7 +4,7 @@
#include "../plug.h" #include "../plug.h"
static int list_node_print(vtype_variable v, ssize_t i, void* _) { static int list_node_print(vtype_variable v, ssize_t i, void* _) {
print_container_value(&i, v.pointer, v.type, 1, *(unsigned int*)_); print_container_value(&i, v, 1, *(unsigned int*)_);
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@
static int remove_callback(vtype_variable v, ssize_t i, void* _) { static int remove_callback(vtype_variable v, ssize_t i, void* _) {
struct { list_t* x; _Bool s; unsigned int p; } *x = _; struct { list_t* x; _Bool s; unsigned int p; } *x = _;
if (!x->s) { if (!x->s) {
print_container_value(0, v.pointer, v.type, 1, x->p); print_container_value(0, v, 1, x->p);
} }
if (libcdsb_list_find(x->x, libcdsb_variable_build(v.pointer, v.type), 0, 0, 1, 1)) { if (libcdsb_list_find(x->x, libcdsb_variable_build(v.pointer, v.type), 0, 0, 1, 1)) {
@ -19,19 +19,19 @@ static int remove_callback(vtype_variable v, ssize_t i, void* _) {
void list_push_random(list_t* x, _Bool silent, unsigned int hpos) { void list_push_random(list_t* x, _Bool silent, unsigned int hpos) {
value_t v = random_value(); vtype_variable v = random_variable(-1);
_Bool r; _Bool r;
if (random_boolean()) { if (random_boolean()) {
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mTry to push value to back of the list:\e[m\n", hpos+1); printf("\e[%dG\e[36mTry to push value to back of the list:\e[m\n", hpos+1);
} }
r = libcdsb_list_insert(x, -1, libcdsb_variable_build(v.value, v.type), 1, 0, 0); r = libcdsb_list_insert(x, -1, v, 1, 0, 0);
} else if (random_boolean()) { } else if (random_boolean()) {
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mTry to push value to front of the list:\e[m\n", hpos+1); printf("\e[%dG\e[36mTry to push value to front of the list:\e[m\n", hpos+1);
} }
r = libcdsb_list_insert(x, -1, libcdsb_variable_build(v.value, v.type), 1, 0, 0); r = libcdsb_list_insert(x, -1, v, 1, 0, 0);
} else { } else {
ssize_t i = list_size(x); ssize_t i = list_size(x);
i = random_uint64()% (i ? i : 1); i = random_uint64()% (i ? i : 1);
@ -41,11 +41,11 @@ void list_push_random(list_t* x, _Bool silent, unsigned int hpos) {
printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m in the list:\e[m\n", hpos+1, i); printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m in the list:\e[m\n", hpos+1, i);
} }
r = libcdsb_list_insert(x, i, libcdsb_variable_build(v.value, v.type), 0, 0, 0); r = libcdsb_list_insert(x, i, v, 0, 0, 0);
} }
if (!silent) { if (!silent) {
print_container_value(0, v.value, v.type, 1, hpos); print_container_value(0, v, 1, hpos);
printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m"); printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m");
put_separator(hpos); put_separator(hpos);
} }
@ -61,7 +61,7 @@ void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) {
if (random_boolean()) { if (random_boolean()) {
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1); printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1);
print_container_value(0, &i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32, 0, hpos); print_container_value(0, libcdsb_variable_build(&i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32), 0, hpos);
} }
switch (list_remove_by_index(x, i)) { switch (list_remove_by_index(x, i)) {
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;

View File

@ -4,7 +4,7 @@
#include "../plug.h" #include "../plug.h"
static int node_print_callback(vtype_variable k, vtype_variable v, void* _) { static int node_print_callback(vtype_variable k, vtype_variable v, void* _) {
print_container_value(0, k.pointer, k.type, 0, *(unsigned int*)_); print_container_value(0, k, 0, *(unsigned int*)_);
return 0; return 0;
} }

View File

@ -7,9 +7,9 @@ static int remove_callback(vtype_variable k, vtype_variable v, void* _) {
struct { size_t n; map_t* x; unsigned int hp; } *d = _; struct { size_t n; map_t* x; unsigned int hp; } *d = _;
if (!d->n--) { if (!d->n--) {
print_container_value(0, k.pointer, k.type, 0, d->hp); print_container_value(0, k, 0, d->hp);
if (libcdsb_map_find(d->x, libcdsb_variable_build(k.pointer, k.type), 0, 0, 1) == 0) { if (libcdsb_map_find(d->x, k, 0, 0, 1) == 0) {
printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1); 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); } else printf("\e[%dG\e[31;1mFAILURE\e[m\n", d->hp+1);
@ -20,15 +20,15 @@ static int remove_callback(vtype_variable k, vtype_variable v, void* _) {
void map_push_random(map_t* x, _Bool silent, unsigned int hpos) { void map_push_random(map_t* x, _Bool silent, unsigned int hpos) {
value_t k = random_value(); vtype_variable k = random_variable(-1);
value_t v = random_value(); vtype_variable v = random_variable(-1);
if (!silent) { if (!silent) {
printf("\e[%dG\e[36mUpdate value in map (\e[m\e[32;1m%s\e[m\e[36m) with key:\e[m\n", hpos+1, vtype_name(x->type)); printf("\e[%dG\e[36mUpdate value in map (\e[m\e[32;1m%s\e[m\e[36m) with key:\e[m\n", hpos+1, vtype_name(x->type));
print_container_value(0, k.value, k.type, 1, hpos); print_container_value(0, k, 1, hpos);
} }
if (libcdsb_map_update(x, libcdsb_variable_build(k.value, k.type), libcdsb_variable_build(v.value, v.type), 0, 0)) { if (libcdsb_map_update(x, k, v, 0, 0)) {
if (!silent) printf("\e[%dG\e[33;1mCHANGE\e[m\n", 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); } else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1);

View File

@ -110,23 +110,32 @@ unsigned int random_unicode_symbol() {
} }
} }
value_t random_value() { vtype_variable random_variable(int rand) {
value_t v; static char values[2*sizeof(void*) * 16];
switch (random_uint8()%13) { static size_t n = 0;
if (n == 16) n = 0;
vtype_variable v = { .pointer = &values[n++*2] };
if (rand < 0 || rand > 12)
rand = random_uint8()%13;
switch (rand) {
default: default:
case 0: v.value[0].b = random_boolean(); v.type = VTYPE_BOOLEAN; break; case 0: *(vtype_bool*) v.pointer = random_boolean(); v.type = VTYPE_BOOLEAN; break;
case 1: v.value[0].u8 = random_uint8 (); v.type = VTYPE_UINT8; break; case 1: *(vtype_uint8*) v.pointer = random_uint8 (); v.type = VTYPE_UINT8; break;
case 2: v.value[0].u16 = random_uint16 (); v.type = VTYPE_UINT16; break; case 2: *(vtype_uint16*) v.pointer = random_uint16 (); v.type = VTYPE_UINT16; break;
case 3: v.value[0].u32 = random_uint32 (); v.type = VTYPE_UINT32; break; case 3: *(vtype_uint32*) v.pointer = random_uint32 (); v.type = VTYPE_UINT32; break;
case 4: v.value[0].u64 = random_uint64 (); v.type = VTYPE_UINT64; break; case 4: *(vtype_uint64*) v.pointer = random_uint64 (); v.type = VTYPE_UINT64; break;
case 5: v.value[0].u8 = random_int8 (); v.type = VTYPE_INT8; break; case 5: *(vtype_int8*) v.pointer = random_int8 (); v.type = VTYPE_INT8; break;
case 6: v.value[0].u16 = random_int16 (); v.type = VTYPE_INT16; break; case 6: *(vtype_int16*) v.pointer = random_int16 (); v.type = VTYPE_INT16; break;
case 7: v.value[0].u32 = random_int32 (); v.type = VTYPE_INT32; break; case 7: *(vtype_int32*) v.pointer = random_int32 (); v.type = VTYPE_INT32; break;
case 8: v.value[0].u64 = random_int64 (); v.type = VTYPE_INT64; break; case 8: *(vtype_int64*) v.pointer = random_int64 (); v.type = VTYPE_INT64; break;
case 9: v.value[0].f = random_float (); v.type = VTYPE_FLOAT; break; case 9: *(vtype_float*) v.pointer = random_float (); v.type = VTYPE_FLOAT; break;
case 10: v.value[0].d = random_double (); v.type = VTYPE_DOUBLE; break; case 10: *(vtype_double*) v.pointer = random_double (); v.type = VTYPE_DOUBLE; break;
case 11: v.value[0].ld = random_ldouble(); v.type = VTYPE_LDOUBLE; break; case 11: *(vtype_ldouble*) v.pointer = random_ldouble(); v.type = VTYPE_LDOUBLE; break;
case 12: v.value[0].ptr = (void*)(uintptr_t)random_uint64(); v.type = VTYPE_POINTER; break; case 12: *(vtype_uint64*) v.pointer = random_uint64 (); v.type = VTYPE_POINTER; break;
} }
return v; return v;

View File

@ -5,7 +5,7 @@
static int node_print_callback(vtype_variable v, void* _) { static int node_print_callback(vtype_variable v, void* _) {
print_container_value(0, v.pointer, v.type, 0, *(unsigned int*)_); print_container_value(0, v, 0, *(unsigned int*)_);
return 0; return 0;
} }

View File

@ -3,11 +3,11 @@
#include "../plug.h" #include "../plug.h"
static int remove_callback(vtype_variable v, void* _) { static int remove_callback(var_t v, void* _) {
struct { size_t n; set_t* x; unsigned int hp; } *d = _; struct { size_t n; set_t* x; unsigned int hp; } *d = _;
if (!d->n--) { if (!d->n--) {
print_container_value(0, v.pointer, v.type, 0, d->hp); print_container_value(0, v, 0, d->hp);
if (libcdsb_vset_find(d->x, v, 0, 0, 1) == 0) { if (libcdsb_vset_find(d->x, v, 0, 0, 1) == 0) {
printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1); printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1);
@ -20,14 +20,14 @@ static int remove_callback(vtype_variable v, void* _) {
void vset_push_random(set_t* x, _Bool silent, unsigned int hpos) { void vset_push_random(set_t* x, _Bool silent, unsigned int hpos) {
value_t v = random_value(); var_t v = random_variable(-1);
if (!silent) { 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)); 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); print_container_value(0, v, 1, hpos);
} }
if (libcdsb_vset_insert(x, libcdsb_variable_build(v.value, v.type))) { if (libcdsb_vset_insert(x, v)) {
if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); 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); } else if (!silent) printf("\e[%dG\e[31;1mFAILURE\e[m\n", hpos+1);

View File

@ -63,17 +63,17 @@ void print_container_values_prefix(const char* name, const char* prefix, unsigne
} }
} }
void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type, unsigned int hpos) { void print_container_value(const ssize_t* index, vtype_variable var, _Bool print_type, unsigned int hpos) {
if (index) { if (index) {
printf("\e[%dG\e[32;1m%5ld: \e[m", hpos+1, *index); printf("\e[%dG\e[32;1m%5ld: \e[m", hpos+1, *index);
} else { } else {
printf("\e[%dG ", hpos+1); printf("\e[%dG ", hpos+1);
} }
printf("\e[31m%24s\e[m", libcdsb_variable_stringify(libcdsb_variable_build((void*)value, type))); printf("\e[31m%24s\e[m", libcdsb_variable_stringify(libcdsb_variable_build((void*)var.pointer, var.type)));
if (print_type) { if (print_type) {
printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(type)); printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(var.type));
} }
puts(""); puts("");