diff --git a/tests/Makefile b/tests/Makefile index f9a6205..b1f7a65 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -27,6 +27,7 @@ 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_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)) @@ -34,12 +35,14 @@ OBJECTS_ARRAY := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix . 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_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-)) ######################################################################################################################## @@ -49,6 +52,7 @@ tests: $(addprefix $(BUILD_PATH)/string-,$(notdir $(basename $(wildcard ./src/st tests: $(addprefix $(BUILD_PATH)/list-,$(notdir $(basename $(wildcard ./src/list/*.c)))) 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)))) ######################################################################################################################## @@ -66,6 +70,8 @@ $(BUILD_PATH)/obj/map-%.o: ./src/map/src/%.c | $(BUILD_PATH)/obj/ $(CC) $^ -o $@ $(CFLAGS) $(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)/array-%: ./src/array/%.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall @@ -77,6 +83,8 @@ $(BUILD_PATH)/map-%: ./src/map/%.c $(OBJECTS_MAP) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall $(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 ######################################################################################################################## @@ -97,4 +105,4 @@ FORCE: modules: ../bin/debug/libcdsb.a ../bin/debug/libcdsb.a: FORCE - cd ../ && $(MAKE) debug + cd ../ && CFLAGS=-DDICT_CAPACITY_BLOCK\=5 $(MAKE) debug diff --git a/tests/src/array/src/plug.c b/tests/src/array/src/plug.c index 364b6fc..0ed9a6b 100644 --- a/tests/src/array/src/plug.c +++ b/tests/src/array/src/plug.c @@ -9,18 +9,28 @@ vtype_string* string_duplicate(const vtype_string* x) { return 0; } vtype_list* list_duplicate (const vtype_list* x) { return 0; } vtype_map* map_duplicate (const vtype_map* x) { return 0; } vtype_set* vset_duplicate (const vtype_set* x) { return 0; } +vtype_dict* dict_duplicate (const vtype_dict* x) { return 0; } void string_free(vtype_string* x) {} void list_free (vtype_list* x) {} void map_free (vtype_map* x) {} void vset_free (vtype_set* x) {} +void dict_free (vtype_dict* x) {} int string_compare(const vtype_string* s0, const vtype_string* 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(); } int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +int dict_compare (const vtype_dict* s0, const vtype_dict* s1) { return random_int8(); } -extern void string_copy_init(vtype_string* x, const vtype_string* s) { memset(x, 0, sizeof(*x)); } -extern void list_copy_init (vtype_list* x, const vtype_list* s) { memset(x, 0, sizeof(*x)); } -extern void map_copy_init (vtype_map* x, const vtype_map* s) { memset(x, 0, sizeof(*x)); } -extern void vset_copy_init (vtype_set* x, const vtype_set* s) { memset(x, 0, sizeof(*x)); } +hash_t string_hash(const vtype_string* s) { return 0; } +hash_t list_hash (const vtype_list* s) { return 0; } +hash_t map_hash (const vtype_map* s) { return 0; } +hash_t vset_hash (const vtype_set* s) { return 0; } +hash_t dict_hash (const vtype_dict* s) { return 0; } + +void string_copy_init(vtype_string* x, const vtype_string* s) { memset(x, 0, sizeof(*x)); } +void list_copy_init (vtype_list* x, const vtype_list* s) { memset(x, 0, sizeof(*x)); } +void map_copy_init (vtype_map* x, const vtype_map* s) { memset(x, 0, sizeof(*x)); } +void vset_copy_init (vtype_set* x, const vtype_set* s) { memset(x, 0, sizeof(*x)); } +void dict_copy_init (vtype_dict* x, const vtype_dict* s) { memset(x, 0, sizeof(*x)); } diff --git a/tests/src/dict/main.c b/tests/src/dict/main.c new file mode 100644 index 0000000..e1b73dc --- /dev/null +++ b/tests/src/dict/main.c @@ -0,0 +1,15 @@ +/* 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); + + dict_t x; + + dict_init(&x); + + visual_push(&x, random_uint8()%17 + 16); + visual_remove(&x); +} diff --git a/tests/src/dict/plug.h b/tests/src/dict/plug.h new file mode 100644 index 0000000..57f84dc --- /dev/null +++ b/tests/src/dict/plug.h @@ -0,0 +1,17 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../include/extra/dict.h" + +#include "../../include/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +void dict_push_random(dict_t* x, bool silent, unsigned int hpos); +void dict_remove_random(dict_t* x, bool silent, unsigned int hpos); + +void dict_print(dict_t* x, const char* prefix, unsigned int hpos); +void dict_info(const dict_t* x, unsigned int hpos); + +void visual_push(dict_t* x, size_t n); +void visual_remove(dict_t* x); diff --git a/tests/src/dict/src/io.c b/tests/src/dict/src/io.c new file mode 100644 index 0000000..d0c4336 --- /dev/null +++ b/tests/src/dict/src/io.c @@ -0,0 +1,47 @@ +/* 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* k, vtype kt, void* v, vtype vt, void* _) { + print_container_value(0, k, kt, 0, *(unsigned int*)_); + return 0; +} + +void dict_print(dict_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Dict", prefix, 0); + dict_foreach(x, &hpos, node_print_callback); + put_separator(0); +} + +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 visual_push(dict_t* x, size_t n) { + while (n--) { + fputs("\e[s", stdout); + + dict_push_random(x, 0, 0); + + dict_info(x, 0); + dict_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove(dict_t* x) { + while (x->size) { + fputs("\e[s", stdout); + dict_remove_random(x, 0, 0); + + dict_info(x, 0); + dict_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} diff --git a/tests/src/dict/src/plug.c b/tests/src/dict/src/plug.c new file mode 100644 index 0000000..bb5545a --- /dev/null +++ b/tests/src/dict/src/plug.c @@ -0,0 +1,29 @@ +/* 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; } +vtype_set* vset_duplicate (const vtype_set* 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) {} +void vset_free (vtype_set* 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(); } +int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } + +hash_t string_hash(const vtype_string* s) { return 0; } +hash_t array_hash (const vtype_array* s) { return 0; } +hash_t list_hash (const vtype_list* s) { return 0; } +hash_t map_hash (const vtype_map* s) { return 0; } +hash_t vset_hash (const vtype_set* s) { return 0; } diff --git a/tests/src/dict/src/random.c b/tests/src/dict/src/random.c new file mode 100644 index 0000000..0218398 --- /dev/null +++ b/tests/src/dict/src/random.c @@ -0,0 +1,53 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int remove_callback(const void* k, vtype kt, void* v, vtype vt, void* _) { + struct { size_t n; dict_t* x; unsigned int hp; } *d = _; + + if (!d->n--) { + print_container_value(0, k, kt, 0, d->hp); + + if (libcdsb_dict_get(d->x, k, kt, 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 dict_push_random(dict_t* x, _Bool silent, unsigned int hpos) { + + value_t k = random_value(); + value_t v = random_value(); + + 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); + } + + if (libcdsb_dict_update(x, k.value, k.type, v.value, v.type)) { + 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); + + if (!silent) put_separator(0); +} + + +void dict_remove_random(dict_t* x, _Bool silent, unsigned int hpos) { + struct { size_t n; dict_t* x; unsigned int hp; } d = { .n = dict_size(x), .x = x, .hp = hpos }; + + if (!silent) + printf("\e[%dG\e[36mTry to remove value from dict by key:\e[m\n", hpos+1); + + if (d.n) { + d.n = random_uint32()%d.n; + dict_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); +} diff --git a/tests/src/list/src/plug.c b/tests/src/list/src/plug.c index eff34f8..7a32c0e 100644 --- a/tests/src/list/src/plug.c +++ b/tests/src/list/src/plug.c @@ -8,13 +8,22 @@ vtype_string* string_duplicate(const vtype_string* x) { return 0; } vtype_array* array_duplicate (const vtype_array* x) { return 0; } vtype_map* map_duplicate (const vtype_map* x) { return 0; } vtype_set* vset_duplicate (const vtype_set* x) { return 0; } +vtype_dict* dict_duplicate (const vtype_dict* x) { return 0; } void string_free(vtype_string* x) {} void array_free (vtype_array* x) {} void map_free (vtype_map* x) {} void vset_free (vtype_set* x) {} +void dict_free (vtype_dict* 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 map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +int dict_compare (const vtype_dict* s0, const vtype_dict* s1) { return random_int8(); } + +hash_t string_hash(const vtype_string* s) { return 0; } +hash_t array_hash (const vtype_array* s) { return 0; } +hash_t map_hash (const vtype_map* s) { return 0; } +hash_t vset_hash (const vtype_set* s) { return 0; } +hash_t dict_hash (const vtype_dict* s) { return 0; } diff --git a/tests/src/map/src/plug.c b/tests/src/map/src/plug.c index b2a8764..2462c64 100644 --- a/tests/src/map/src/plug.c +++ b/tests/src/map/src/plug.c @@ -8,13 +8,22 @@ 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_set* vset_duplicate (const vtype_set* x) { return 0; } +vtype_dict* dict_duplicate (const vtype_dict* x) { return 0; } void string_free(vtype_string* x) {} void array_free (vtype_array* x) {} void list_free (vtype_list* x) {} void vset_free (vtype_set* x) {} +void dict_free (vtype_dict* 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 vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +int dict_compare (const vtype_dict* s0, const vtype_dict* s1) { return random_int8(); } + +hash_t string_hash(const vtype_string* s) { return 0; } +hash_t array_hash (const vtype_array* s) { return 0; } +hash_t list_hash (const vtype_list* s) { return 0; } +hash_t vset_hash (const vtype_set* s) { return 0; } +hash_t dict_hash (const vtype_dict* s) { return 0; } diff --git a/tests/src/set/src/plug.c b/tests/src/set/src/plug.c index 00cf60d..6dc338a 100644 --- a/tests/src/set/src/plug.c +++ b/tests/src/set/src/plug.c @@ -8,13 +8,22 @@ 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; } +vtype_dict* dict_duplicate (const vtype_dict* 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) {} +void dict_free (vtype_dict* 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(); } +int dict_compare (const vtype_dict* s0, const vtype_dict* s1) { return random_int8(); } + +hash_t string_hash(const vtype_string* s) { return 0; } +hash_t array_hash (const vtype_array* s) { return 0; } +hash_t list_hash (const vtype_list* s) { return 0; } +hash_t map_hash (const vtype_map* s) { return 0; } +hash_t dict_hash (const vtype_dict* s) { return 0; } diff --git a/tests/src/string/src/plug.c b/tests/src/string/src/plug.c index 6da62c4..02f7f0f 100644 --- a/tests/src/string/src/plug.c +++ b/tests/src/string/src/plug.c @@ -8,13 +8,22 @@ 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; } vtype_set* vset_duplicate (const vtype_set* x) { return 0; } +vtype_dict* dict_duplicate (const vtype_dict* x) { return 0; } void array_free (vtype_array* x) {} void list_free (vtype_list* x) {} void map_free (vtype_map* x) {} void vset_free (vtype_set* x) {} +void dict_free (vtype_dict* x) {} 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(); } int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +int dict_compare (const vtype_dict* s0, const vtype_dict* s1) { return random_int8(); } + +hash_t array_hash (const vtype_array* s) { return 0; } +hash_t list_hash (const vtype_list* s) { return 0; } +hash_t map_hash (const vtype_map* s) { return 0; } +hash_t vset_hash (const vtype_set* s) { return 0; } +hash_t dict_hash (const vtype_dict* s) { return 0; }