Refactor tests
This commit is contained in:
parent
fba8de4878
commit
384d6de89f
@ -7,11 +7,6 @@
|
||||
#ifndef 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 vtype_bool random_boolean();
|
||||
@ -32,6 +27,6 @@ extern vtype_int64 random_int64();
|
||||
extern char random_ascii_char();
|
||||
extern unsigned int random_unicode_symbol();
|
||||
|
||||
extern value_t random_value();
|
||||
extern var_t random_variable(int rand);
|
||||
|
||||
#endif /* LIBCDSB_TESTS_RANDOM_H */
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
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_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 test_init(int argc, char** argv);
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../plug.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
static int remove_callback(vtype_variable v, ssize_t i, void* _) {
|
||||
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))
|
||||
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* _) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
r = libcdsb_array_insert(x, libcdsb_variable_build(_.v.value, _.v.type)) >= 0;
|
||||
r = libcdsb_array_insert(x, _.v) >= 0;
|
||||
} else {
|
||||
ssize_t i = array_size(x);
|
||||
|
||||
@ -46,7 +46,7 @@ void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) {
|
||||
}
|
||||
|
||||
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");
|
||||
put_separator(hpos);
|
||||
}
|
||||
@ -62,7 +62,7 @@ void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos) {
|
||||
if (random_boolean()) {
|
||||
if (!silent) {
|
||||
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)) {
|
||||
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../plug.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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 = _;
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
||||
value_t k = random_value();
|
||||
value_t v = random_value();
|
||||
vtype_variable k = random_variable(-1);
|
||||
vtype_variable v = random_variable(-1);
|
||||
|
||||
if (!silent) {
|
||||
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);
|
||||
} else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1);
|
||||
|
||||
|
@ -6,29 +6,29 @@
|
||||
int main(int argc, char** argv) {
|
||||
test_init(argc, argv);
|
||||
|
||||
value_t x = random_container(0);
|
||||
vtype_variable x = random_container(0);
|
||||
|
||||
switch (x.type) {
|
||||
default: abort();
|
||||
case VTYPE_ARRAY:
|
||||
array_info((void*)x.value, 0);
|
||||
array_free((void*)x.value);
|
||||
array_info((void*)x.pointer, 0);
|
||||
array_free((void*)x.pointer);
|
||||
return 0;
|
||||
case VTYPE_MAP:
|
||||
map_info((void*)x.value, 0);
|
||||
map_free((void*)x.value);
|
||||
map_info((void*)x.pointer, 0);
|
||||
map_free((void*)x.pointer);
|
||||
return 0;
|
||||
case VTYPE_LIST:
|
||||
list_info((void*)x.value, 0);
|
||||
list_free((void*)x.value);
|
||||
list_info((void*)x.pointer, 0);
|
||||
list_free((void*)x.pointer);
|
||||
return 0;
|
||||
case VTYPE_SET:
|
||||
vset_info((void*)x.value, 0);
|
||||
vset_free((void*)x.value);
|
||||
vset_info((void*)x.pointer, 0);
|
||||
vset_free((void*)x.pointer);
|
||||
return 0;
|
||||
case VTYPE_DICT:
|
||||
dict_info((void*)x.value, 0);
|
||||
dict_free((void*)x.value);
|
||||
dict_info((void*)x.pointer, 0);
|
||||
dict_free((void*)x.pointer);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,8 @@
|
||||
#include "../../include/test.h"
|
||||
#include "../../include/time.h"
|
||||
|
||||
extern value_t random_container(bool embd);
|
||||
extern value_t real_random_value(bool embd);
|
||||
extern vtype_variable random_container(bool embd);
|
||||
extern vtype_variable real_random_value(bool embd);
|
||||
|
||||
extern void array_info(arr_t* x, unsigned int hpos);
|
||||
extern void list_info (list_t* x, unsigned int hpos);
|
||||
|
@ -5,275 +5,188 @@
|
||||
|
||||
#define MAX_ELEMENTS 2000
|
||||
|
||||
static arr_t random_array(bool embd);
|
||||
static list_t random_list (bool embd);
|
||||
static set_t random_set (bool embd);
|
||||
static map_t random_map (bool embd);
|
||||
static dict_t random_dict (bool embd);
|
||||
static vtype_variable random_array(bool embd);
|
||||
static vtype_variable random_list (bool embd);
|
||||
static vtype_variable random_set (bool embd);
|
||||
static vtype_variable random_map (bool embd);
|
||||
static vtype_variable random_dict (bool embd);
|
||||
|
||||
static str_t random_string() {
|
||||
str_t x;
|
||||
static vtype_variable random_string() {
|
||||
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;
|
||||
|
||||
string_init(&x, 0);
|
||||
string_init(x, 0);
|
||||
|
||||
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() {
|
||||
value_t v;
|
||||
switch (random_uint8()%14) {
|
||||
default:
|
||||
case 0: v.value[0].b = random_boolean(); v.type = VTYPE_BOOLEAN; break;
|
||||
case 1: v.value[0].u8 = random_uint8 (); v.type = VTYPE_UINT8; break;
|
||||
case 2: v.value[0].u16 = random_uint16 (); v.type = VTYPE_UINT16; 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;
|
||||
static vtype_variable random_variable2() {
|
||||
vtype_variable v;
|
||||
int rand;
|
||||
|
||||
switch (rand = random_uint8()%14) {
|
||||
default: v = random_variable(rand); break;
|
||||
case 13: v = random_string(); break;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static value_t random_value_by_type(vtype type, bool embd) {
|
||||
value_t v;
|
||||
static vtype_variable random_value_by_type(vtype type, bool embd) {
|
||||
vtype_variable v;
|
||||
switch ((v.type = type)) {
|
||||
default:
|
||||
case VTYPE_BOOLEAN: v.value[0].b = random_boolean(); break;
|
||||
case VTYPE_UINT8: v.value[0].u8 = random_uint8 (); break;
|
||||
case VTYPE_UINT16: v.value[0].u16 = random_uint16 (); break;
|
||||
case VTYPE_UINT32: v.value[0].u32 = random_uint32 (); break;
|
||||
case VTYPE_UINT64: v.value[0].u64 = random_uint64 (); break;
|
||||
case VTYPE_INT8: v.value[0].u8 = random_int8 (); break;
|
||||
case VTYPE_INT16: v.value[0].u16 = random_uint16 (); break;
|
||||
case VTYPE_INT32: v.value[0].u32 = random_int32 (); break;
|
||||
case VTYPE_INT64: v.value[0].u64 = random_int64 (); break;
|
||||
case VTYPE_FLOAT: v.value[0].f = random_float (); break;
|
||||
case VTYPE_DOUBLE: v.value[0].d = random_double (); break;
|
||||
case VTYPE_LDOUBLE: v.value[0].ld = random_ldouble(); break;
|
||||
case VTYPE_STRING: v.value[0].s = random_string(); break;
|
||||
case VTYPE_ARRAY: v.value[0].a = random_array(embd); break;
|
||||
case VTYPE_MAP: v.value[0].m = random_map (embd); break;
|
||||
case VTYPE_DICT: v.value[0].vd = random_dict (embd); break;
|
||||
case VTYPE_LIST: v.value[0].l = random_list (embd); break;
|
||||
case VTYPE_SET: v.value[0].vs = random_set (embd); break;
|
||||
case VTYPE_POINTER: v.value[0].ptr = (void*)(uintptr_t)random_uint64(); break;
|
||||
case VTYPE_BOOLEAN: v = random_variable( 0); break;
|
||||
case VTYPE_UINT8: v = random_variable( 1); break;
|
||||
case VTYPE_UINT16: v = random_variable( 2); break;
|
||||
case VTYPE_UINT32: v = random_variable( 3); break;
|
||||
case VTYPE_UINT64: v = random_variable( 4); break;
|
||||
case VTYPE_INT8: v = random_variable( 5); break;
|
||||
case VTYPE_INT16: v = random_variable( 6); break;
|
||||
case VTYPE_INT32: v = random_variable( 7); break;
|
||||
case VTYPE_INT64: v = random_variable( 8); break;
|
||||
case VTYPE_FLOAT: v = random_variable( 9); break;
|
||||
case VTYPE_DOUBLE: v = random_variable(10); break;
|
||||
case VTYPE_LDOUBLE: v = random_variable(11); break;
|
||||
case VTYPE_STRING: v = random_string (); break;
|
||||
case VTYPE_ARRAY: v = random_array (embd); break;
|
||||
case VTYPE_MAP: v = random_map (embd); break;
|
||||
case VTYPE_DICT: v = random_dict (embd); break;
|
||||
case VTYPE_LIST: v = random_list (embd); break;
|
||||
case VTYPE_SET: v = random_set (embd); break;
|
||||
case VTYPE_POINTER: v = random_variable(12); break;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static arr_t random_array(bool embd) {
|
||||
arr_t x;
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
value_t v = (!embd) ? random_value2() : real_random_value(1);
|
||||
static vtype_variable random_array(bool embd) {
|
||||
static arr_t values[16];
|
||||
static size_t pos = 0;
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
v = random_value_by_type(v.type, 1);
|
||||
for(;;) {
|
||||
libcdsb_array_attach(x, v);
|
||||
if (--n) v = random_value_by_type(v.type, 1);
|
||||
else 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;
|
||||
}
|
||||
|
||||
return x;
|
||||
return libcdsb_variable_build(x, VTYPE_ARRAY);
|
||||
}
|
||||
|
||||
static set_t random_set(bool embd) {
|
||||
set_t x;
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
value_t v = (!embd) ? random_value2() : real_random_value(1);
|
||||
static vtype_variable random_set(bool embd) {
|
||||
static set_t values[16];
|
||||
static size_t pos = 0;
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
v = random_value_by_type(v.type, 1);
|
||||
for(;;) {
|
||||
libcdsb_vset_attach(x, v);
|
||||
if (--n) v = random_value_by_type(v.type, 1);
|
||||
else 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;
|
||||
}
|
||||
|
||||
return x;
|
||||
return libcdsb_variable_build(x, VTYPE_SET);
|
||||
}
|
||||
|
||||
static list_t random_list(bool embd) {
|
||||
list_t x;
|
||||
value_t v;
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
static vtype_variable random_list(bool embd) {
|
||||
static list_t values[16];
|
||||
static size_t pos = 0;
|
||||
|
||||
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--) {
|
||||
v = (!embd) ? random_value2() : real_random_value(1);
|
||||
libcdsb_list_insert(&x, -1, libcdsb_variable_build(v.value, v.type), 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;
|
||||
}
|
||||
v = (!embd) ? random_variable2() : real_random_value(1);
|
||||
libcdsb_list_attach(x, -1, v, 1, 0, 0);
|
||||
}
|
||||
|
||||
return x;
|
||||
return libcdsb_variable_build(x, VTYPE_LIST);
|
||||
}
|
||||
|
||||
static map_t random_map(bool embd) {
|
||||
map_t x;
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
value_t k = (!embd) ? random_value2() : real_random_value(1);
|
||||
static vtype_variable random_map(bool embd) {
|
||||
static map_t values[16];
|
||||
static size_t pos = 0;
|
||||
|
||||
map_init(&x, k.type);
|
||||
if (pos == 16) pos = 0;
|
||||
|
||||
while(n--) {
|
||||
value_t v = (!embd) ? random_value2() : real_random_value(1);
|
||||
map_t* x = &values[pos++];
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
for(;;) {
|
||||
v = (!embd) ? random_variable2() : real_random_value(1);
|
||||
libcdsb_map_inject(x, k, v, 0, 0);
|
||||
if (--n) k = random_value_by_type(k.type, 1);
|
||||
else break;
|
||||
}
|
||||
|
||||
switch (k.type) {
|
||||
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;
|
||||
|
||||
return libcdsb_variable_build(x, VTYPE_MAP);
|
||||
}
|
||||
|
||||
static dict_t random_dict(bool embd) {
|
||||
dict_t x;
|
||||
value_t k, v;
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
static vtype_variable random_dict(bool embd) {
|
||||
static dict_t values[16];
|
||||
static size_t pos = 0;
|
||||
|
||||
dict_init(&x);
|
||||
if (pos == 16) pos = 0;
|
||||
|
||||
while(n--) {
|
||||
k = (!embd) ? random_value2() : real_random_value(1);
|
||||
v = (!embd) ? random_value2() : real_random_value(1);
|
||||
libcdsb_dict_update(&x, libcdsb_variable_build(k.value, k.type), libcdsb_variable_build(v.value, v.type), 0, 0);
|
||||
dict_t* x = &values[pos++];
|
||||
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
|
||||
vtype_variable k;
|
||||
vtype_variable v;
|
||||
|
||||
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;
|
||||
}
|
||||
dict_init(x);
|
||||
|
||||
switch (k.type) {
|
||||
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;
|
||||
}
|
||||
while (n--) {
|
||||
k = (!embd) ? random_variable2() : real_random_value(1);
|
||||
v = (!embd) ? random_variable2() : real_random_value(1);
|
||||
libcdsb_dict_inject(x, k, v, 0, 0);
|
||||
}
|
||||
|
||||
return x;
|
||||
return libcdsb_variable_build(x, VTYPE_DICT);
|
||||
}
|
||||
|
||||
value_t random_container(bool embd) {
|
||||
value_t v;
|
||||
|
||||
vtype_variable random_container(bool embd) {
|
||||
switch (random_uint8()%5) {
|
||||
default:
|
||||
case 0: v.value[0].a = random_array(embd); v.type = VTYPE_ARRAY; break;
|
||||
case 1: v.value[0].m = random_map (embd); v.type = VTYPE_MAP; break;
|
||||
case 2: v.value[0].vd = random_dict (embd); v.type = VTYPE_DICT; break;
|
||||
case 3: v.value[0].l = random_list (embd); v.type = VTYPE_LIST; break;
|
||||
case 4: v.value[0].vs = random_set (embd); v.type = VTYPE_SET; break;
|
||||
case 0: return random_array(embd);
|
||||
case 1: return random_map (embd);
|
||||
case 2: return random_dict (embd);
|
||||
case 3: return random_list (embd);
|
||||
case 4: return random_set (embd);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
value_t real_random_value(bool embd) {
|
||||
return random_boolean() ? random_value2() : random_container(embd);
|
||||
vtype_variable real_random_value(bool embd) {
|
||||
return random_boolean() ? random_variable2() : random_container(embd);
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../plug.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
static int remove_callback(vtype_variable v, ssize_t i, void* _) {
|
||||
struct { list_t* x; _Bool s; unsigned int p; } *x = _;
|
||||
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)) {
|
||||
@ -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) {
|
||||
|
||||
value_t v = random_value();
|
||||
vtype_variable v = random_variable(-1);
|
||||
_Bool r;
|
||||
|
||||
if (random_boolean()) {
|
||||
if (!silent) {
|
||||
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()) {
|
||||
if (!silent) {
|
||||
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 {
|
||||
ssize_t i = list_size(x);
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
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");
|
||||
put_separator(hpos);
|
||||
}
|
||||
@ -61,7 +61,7 @@ void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) {
|
||||
if (random_boolean()) {
|
||||
if (!silent) {
|
||||
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)) {
|
||||
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../plug.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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 = _;
|
||||
|
||||
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);
|
||||
} 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) {
|
||||
|
||||
value_t k = random_value();
|
||||
value_t v = random_value();
|
||||
vtype_variable k = random_variable(-1);
|
||||
vtype_variable v = random_variable(-1);
|
||||
|
||||
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));
|
||||
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);
|
||||
} else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1);
|
||||
|
||||
|
@ -110,23 +110,32 @@ unsigned int random_unicode_symbol() {
|
||||
}
|
||||
}
|
||||
|
||||
value_t random_value() {
|
||||
value_t v;
|
||||
switch (random_uint8()%13) {
|
||||
vtype_variable random_variable(int rand) {
|
||||
static char values[2*sizeof(void*) * 16];
|
||||
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:
|
||||
case 0: v.value[0].b = random_boolean(); v.type = VTYPE_BOOLEAN; break;
|
||||
case 1: v.value[0].u8 = random_uint8 (); v.type = VTYPE_UINT8; break;
|
||||
case 2: v.value[0].u16 = random_uint16 (); v.type = VTYPE_UINT16; 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_int16 (); 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 0: *(vtype_bool*) v.pointer = random_boolean(); v.type = VTYPE_BOOLEAN; break;
|
||||
case 1: *(vtype_uint8*) v.pointer = random_uint8 (); v.type = VTYPE_UINT8; break;
|
||||
case 2: *(vtype_uint16*) v.pointer = random_uint16 (); v.type = VTYPE_UINT16; break;
|
||||
case 3: *(vtype_uint32*) v.pointer = random_uint32 (); v.type = VTYPE_UINT32; break;
|
||||
case 4: *(vtype_uint64*) v.pointer = random_uint64 (); v.type = VTYPE_UINT64; break;
|
||||
case 5: *(vtype_int8*) v.pointer = random_int8 (); v.type = VTYPE_INT8; break;
|
||||
case 6: *(vtype_int16*) v.pointer = random_int16 (); v.type = VTYPE_INT16; break;
|
||||
case 7: *(vtype_int32*) v.pointer = random_int32 (); v.type = VTYPE_INT32; break;
|
||||
case 8: *(vtype_int64*) v.pointer = random_int64 (); v.type = VTYPE_INT64; break;
|
||||
case 9: *(vtype_float*) v.pointer = random_float (); v.type = VTYPE_FLOAT; break;
|
||||
case 10: *(vtype_double*) v.pointer = random_double (); v.type = VTYPE_DOUBLE; break;
|
||||
case 11: *(vtype_ldouble*) v.pointer = random_ldouble(); v.type = VTYPE_LDOUBLE; break;
|
||||
case 12: *(vtype_uint64*) v.pointer = random_uint64 (); v.type = VTYPE_POINTER; break;
|
||||
}
|
||||
|
||||
return v;
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,11 @@
|
||||
|
||||
#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 = _;
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
||||
value_t v = random_value();
|
||||
var_t v = random_variable(-1);
|
||||
|
||||
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);
|
||||
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);
|
||||
} else if (!silent) printf("\e[%dG\e[31;1mFAILURE\e[m\n", hpos+1);
|
||||
|
||||
|
@ -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) {
|
||||
printf("\e[%dG\e[32;1m%5ld: \e[m", hpos+1, *index);
|
||||
} else {
|
||||
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) {
|
||||
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("");
|
||||
|
Loading…
Reference in New Issue
Block a user