Add global (communicative) test

This commit is contained in:
Gregory Lirent 2022-08-16 18:38:35 +03:00
parent 4056544d49
commit 382bd00701
5 changed files with 375 additions and 22 deletions

View File

@ -17,32 +17,25 @@ $(BUILD_PATH)/obj/%.o: CFLAGS := $(CFLAGS) -Og -fPIC -c -g3 -Wall
########################################################################################################################
c_objects = $(addsuffix .o,$(addprefix $(2),$(notdir $(basename $(wildcard $(1)*.c)))))
OBJECTS_TESTS := $(call c_objects,./src/,)
OBJECTS_BASE := $(call c_objects,../src/,)
OBJECTS_STRING := $(OBJECTS_BASE) $(call c_objects,../src/string/,string-)
OBJECTS_ARRAY := $(OBJECTS_BASE) $(call c_objects,../src/array/,array-)
OBJECTS_LIST := $(OBJECTS_BASE) $(call c_objects,../src/list/,list-)
OBJECTS_MAP := $(OBJECTS_BASE) $(call c_objects,../src/map/,map-)
OBJECTS_SET := $(OBJECTS_BASE) $(call c_objects,../src/set/,set-)
OBJECTS_DICT := $(OBJECTS_BASE) $(call c_objects,../src/dict/,dict-)
OBJECTS_TESTS := $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/,))
OBJECTS_BASE := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/,))
OBJECTS_STRING := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/string/,string-))
OBJECTS_ARRAY := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/array/,array-))
OBJECTS_LIST := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/list/,list-))
OBJECTS_MAP := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/map/,map-))
OBJECTS_SET := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/set/,set-))
OBJECTS_DICT := $(addprefix ../bin/debug/obj/,$(call c_objects,../src/dict/,dict-))
OBJECTS_BASE := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_BASE))
OBJECTS_STRING := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_STRING))
OBJECTS_ARRAY := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_ARRAY))
OBJECTS_LIST := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_LIST))
OBJECTS_MAP := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_MAP))
OBJECTS_SET := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_SET))
OBJECTS_DICT := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix ../bin/debug/obj/,$(OBJECTS_DICT))
OBJECTS_GLOBAL := $(OBJECTS_TESTS) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/global/src/,global-))
OBJECTS_STRING := $(OBJECTS_STRING) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/string/src/,string-))
OBJECTS_ARRAY := $(OBJECTS_ARRAY) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/array/src/,array-))
OBJECTS_LIST := $(OBJECTS_LIST) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/list/src/,list-))
OBJECTS_MAP := $(OBJECTS_MAP) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/map/src/,map-))
OBJECTS_SET := $(OBJECTS_SET) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/set/src/,set-))
OBJECTS_DICT := $(OBJECTS_DICT) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/dict/src/,dict-))
OBJECTS_STRING := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_STRING) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/string/src/,string-))
OBJECTS_ARRAY := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_ARRAY) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/array/src/,array-))
OBJECTS_LIST := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_LIST) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/list/src/,list-))
OBJECTS_MAP := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_MAP) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/map/src/,map-))
OBJECTS_SET := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_SET) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/set/src/,set-))
OBJECTS_DICT := $(OBJECTS_BASE) $(OBJECTS_TESTS) $(OBJECTS_DICT) $(addprefix $(BUILD_PATH)/obj/,$(call c_objects,./src/dict/src/,dict-))
########################################################################################################################
@ -53,6 +46,7 @@ tests: $(addprefix $(BUILD_PATH)/list-,$(notdir $(basename $(wildcard ./src/list
tests: $(addprefix $(BUILD_PATH)/map-,$(notdir $(basename $(wildcard ./src/map/*.c))))
tests: $(addprefix $(BUILD_PATH)/set-,$(notdir $(basename $(wildcard ./src/set/*.c))))
tests: $(addprefix $(BUILD_PATH)/dict-,$(notdir $(basename $(wildcard ./src/dict/*.c))))
tests: $(addprefix $(BUILD_PATH)/global-,$(notdir $(basename $(wildcard ./src/global/*.c))))
########################################################################################################################
@ -72,6 +66,8 @@ $(BUILD_PATH)/obj/set-%.o: ./src/set/src/%.c | $(BUILD_PATH)/obj/
$(CC) $^ -o $@ $(CFLAGS)
$(BUILD_PATH)/obj/dict-%.o: ./src/dict/src/%.c | $(BUILD_PATH)/obj/
$(CC) $^ -o $@ $(CFLAGS)
$(BUILD_PATH)/obj/global-%.o: ./src/global/src/%.c | $(BUILD_PATH)/obj/
$(CC) $^ -o $@ $(CFLAGS)
$(BUILD_PATH)/array-%: ./src/array/%.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/
$(CC) $^ -o $@ $(CFLAGS) -g3 -Wall
@ -85,6 +81,8 @@ $(BUILD_PATH)/set-%: ./src/set/%.c $(OBJECTS_SET) | $(BUILD_PATH)/
$(CC) $^ -o $@ $(CFLAGS) -g3 -Wall
$(BUILD_PATH)/dict-%: ./src/dict/%.c $(OBJECTS_DICT) | $(BUILD_PATH)/
$(CC) $^ -o $@ $(CFLAGS) -g3 -Wall
$(BUILD_PATH)/global-%: ./src/global/%.c $(OBJECTS_GLOBAL) | $(BUILD_PATH)/
$(CC) $^ ../bin/debug/libcdsb.a ../modules/libunic/bin/libunic.a -o $@ $(CFLAGS) -g3 -Wall
########################################################################################################################

34
tests/src/global/main.c Normal file
View File

@ -0,0 +1,34 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "plug.h"
int main(int argc, char** argv) {
test_init(argc, argv);
value_t x = random_container(0);
switch (x.type) {
default: abort();
case VTYPE_ARRAY:
array_info((void*)x.value, 0);
array_free((void*)x.value);
return 0;
case VTYPE_MAP:
map_info((void*)x.value, 0);
map_free((void*)x.value);
return 0;
case VTYPE_LIST:
list_info((void*)x.value, 0);
list_free((void*)x.value);
return 0;
case VTYPE_SET:
vset_info((void*)x.value, 0);
vset_free((void*)x.value);
return 0;
case VTYPE_DICT:
dict_info((void*)x.value, 0);
dict_free((void*)x.value);
return 0;
}
}

23
tests/src/global/plug.h Normal file
View File

@ -0,0 +1,23 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../../include/extra/string.h"
#include "../../../include/extra/array.h"
#include "../../../include/extra/list.h"
#include "../../../include/extra/set.h"
#include "../../../include/extra/map.h"
#include "../../../include/extra/dict.h"
#include "../../include/random.h"
#include "../../include/test.h"
#include "../../include/time.h"
extern value_t random_container(bool embd);
extern value_t 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);
extern void dict_info(const dict_t* x, unsigned int hpos);
extern void map_info(const map_t* x, unsigned int hpos);
extern void vset_info(const set_t* x, unsigned int hpos);
extern void string_info(str_t* x);

33
tests/src/global/src/io.c Normal file
View File

@ -0,0 +1,33 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
void map_info(const map_t* x, unsigned int hpos) {
print_container_info("Map", "nodes", &x->type, map_size(x), -1, 0);
put_separator(0);
}
void array_info(arr_t* x, unsigned int hpos) {
print_container_info("Array", "nodes", 0, array_size(x), -1, hpos);
}
void dict_info(const dict_t* x, unsigned int hpos) {
print_container_info("Dict", "nodes", 0, dict_size(x), -1, 0);
put_separator(0);
}
void list_info(list_t* x, unsigned int hpos) {
print_container_info("List", "nodes", 0, list_size(x), -1, hpos);
}
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 string_info(str_t* x) {
print_container_info("String", "utf8 chars", 0, string_size(x), string_nmemb(x), 0);
put_separator(0);
}

View File

@ -0,0 +1,265 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
#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 str_t random_string() {
str_t x;
size_t n = random_uint16()%MAX_ELEMENTS;
string_init(&x, 0);
while (n--) {
string_concat(&x, random_unicode_symbol());
}
return x;
}
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;
}
return v;
}
static value_t random_value_by_type(vtype type, bool embd) {
value_t 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;
}
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);
void (*callback)(void*);
switch (v.type) {
default: callback = nullptr; break;
case VTYPE_STRING: callback = (void*)string_free; break;
case VTYPE_ARRAY: callback = (void*)array_free; break;
case VTYPE_LIST: callback = (void*)list_free; break;
case VTYPE_MAP: callback = (void*)map_free; break;
case VTYPE_SET: callback = (void*)vset_free; break;
case VTYPE_DICT: callback = (void*)dict_free; break;
}
array_init(&x, v.type);
while(n--) {
libcdsb_array_push(&x, v.value, v.type);
if (callback) callback(v.value);
v = random_value_by_type(v.type, 1);
}
if (callback) callback(v.value);
return x;
}
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);
void (*callback)(void*);
switch (v.type) {
default: callback = nullptr; break;
case VTYPE_STRING: callback = (void*)string_free; break;
case VTYPE_ARRAY: callback = (void*)array_free; break;
case VTYPE_LIST: callback = (void*)list_free; break;
case VTYPE_MAP: callback = (void*)map_free; break;
case VTYPE_SET: callback = (void*)vset_free; break;
case VTYPE_DICT: callback = (void*)dict_free; break;
}
vset_init(&x, v.type);
while(n--) {
libcdsb_vset_insert(&x, v.value, v.type);
if (callback) callback(v.value);
v = random_value_by_type(v.type, 1);
}
if (callback) callback(v.value);
return x;
}
static list_t random_list(bool embd) {
list_t x;
value_t v;
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
list_init(&x);
while(n--) {
v = (!embd) ? random_value2() : real_random_value(1);
libcdsb_list_update(&x, -1, v.value, v.type, 1);
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;
}
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);
void (*callback)(void*);
switch (k.type) {
default: callback = nullptr; break;
case VTYPE_STRING: callback = (void*)string_free; break;
case VTYPE_ARRAY: callback = (void*)array_free; break;
case VTYPE_LIST: callback = (void*)list_free; break;
case VTYPE_MAP: callback = (void*)map_free; break;
case VTYPE_SET: callback = (void*)vset_free; break;
case VTYPE_DICT: callback = (void*)dict_free; break;
}
map_init(&x, k.type);
while(n--) {
value_t v = (!embd) ? random_value2() : real_random_value(1);
libcdsb_map_update(&x, k.value, k.type, v.value, v.type);
if (callback) callback(k.value);
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);
}
if (callback) callback(k.value);
return x;
}
static dict_t random_dict(bool embd) {
dict_t x;
value_t k, v;
size_t n = random_uint16()%((!embd)?MAX_ELEMENTS:100);
dict_init(&x);
while(n--) {
k = (!embd) ? random_value2() : real_random_value(1);
v = (!embd) ? random_value2() : real_random_value(1);
libcdsb_dict_update(&x, k.value, k.type, v.value, 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;
}
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;
}
value_t random_container(bool embd) {
value_t v;
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;
}
return v;
}
value_t real_random_value(bool embd) {
return random_boolean() ? random_value2() : random_container(embd);
}