From f73f8010133a5bffcc6ae52b0cfdb7315dc52bae Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Jun 2022 21:41:06 +0300 Subject: [PATCH 01/42] Add tests base --- tests/include/random.h | 26 +++++++++ tests/include/test.h | 12 +++++ tests/include/time.h | 24 +++++++++ tests/src/random.c | 80 +++++++++++++++++++++++++++ tests/src/test.c | 41 ++++++++++++++ tests/src/time.c | 120 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 303 insertions(+) create mode 100644 tests/include/random.h create mode 100644 tests/include/test.h create mode 100644 tests/include/time.h create mode 100644 tests/src/random.c create mode 100644 tests/src/test.c create mode 100644 tests/src/time.c diff --git a/tests/include/random.h b/tests/include/random.h new file mode 100644 index 0000000..fa24373 --- /dev/null +++ b/tests/include/random.h @@ -0,0 +1,26 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../include/vtype.h" + +#ifndef LIBCDSB_TESTS_RANDOM_H +#define LIBCDSB_TESTS_RANDOM_H + +extern int random_init(int argc, char** argv); + +extern vtype_bool random_boolean(); +extern vtype_float random_float(); +extern vtype_double random_double(); +extern vtype_ldouble random_ldouble(); + +extern vtype_uint8 random_uint8(); +extern vtype_uint16 random_uint16(); +extern vtype_uint32 random_uint32(); +extern vtype_uint64 random_uint64(); + +extern vtype_int8 random_int8(); +extern vtype_int16 random_int16(); +extern vtype_int32 random_int32(); +extern vtype_int64 random_int64(); + +#endif /* LIBCDSB_TESTS_RANDOM_H */ diff --git a/tests/include/test.h b/tests/include/test.h new file mode 100644 index 0000000..4a6a4c5 --- /dev/null +++ b/tests/include/test.h @@ -0,0 +1,12 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include + +#ifndef LIBCDSB_TESTS_TEST_H +#define LIBCDSB_TESTS_TEST_H + +extern void put_separator(); +extern void test_init(int argc, char** argv); + +#endif /* LIBCDSB_TESTS_TEST_H */ diff --git a/tests/include/time.h b/tests/include/time.h new file mode 100644 index 0000000..0558e5c --- /dev/null +++ b/tests/include/time.h @@ -0,0 +1,24 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../include/vtype.h" + +#ifndef LIBCDSB_TESTS_TIME_H +#define LIBCDSB_TESTS_TIME_H + +typedef struct timer { + void* info; +} TIMER; + + +extern void timer_init (TIMER* timer); +extern void timer_start(TIMER* timer); +extern void timer_stop (TIMER* timer); +extern void timer_free (TIMER* timer); + +extern vtype_ldouble timer_value(TIMER* timer); + +extern void psleep(unsigned long microsec); + + +#endif /* LIBCDSB_TESTS_TIME_H */ diff --git a/tests/src/random.c b/tests/src/random.c new file mode 100644 index 0000000..a09ea4a --- /dev/null +++ b/tests/src/random.c @@ -0,0 +1,80 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include + +#include "../include/test.h" +#include "../include/random.h" + + +static void init(unsigned int seed) { + printf("\e[36mRandom module initialized with seed: \e[m\e[32m%u\n\e[m", seed); + put_separator(); + srand(seed); +} + +int random_init(int argc, char** argv) { + if (argc < 2) { + init(time(0)); + } else init(atol(argv[1])); + + return 0; +} + +vtype_bool random_boolean() { + return rand()&1; +} + +vtype_float random_float() { + if (rand()&1) { + return ((vtype_float)random_uint16() / (vtype_float)random_int16()) * -1; + } + return (vtype_float)random_uint16() / (vtype_float)random_int16(); +} + +vtype_double random_double() { + if (rand()&1) { + return ((vtype_double)random_uint32() / (vtype_double)random_int32()) * -1; + } + return (vtype_double)random_uint32() / (vtype_double)random_int32(); +} + +vtype_ldouble random_ldouble() { + if (rand()&1) { + return ((vtype_ldouble)random_uint64() / (vtype_ldouble)random_int64()) * -1; + } + return (vtype_ldouble)random_uint64() / (vtype_ldouble)random_int64(); +} + +vtype_int8 random_int8() { + return random_uint8(); +} + +vtype_int16 random_int16() { + return random_uint16(); +} + +vtype_int32 random_int32() { + return rand() | ((rand()&1) << 31); +} + +vtype_uint8 random_uint8() { + return rand() % 0x100; +} + +vtype_uint16 random_uint16() { + return rand() % 0x10000; +} + +vtype_uint32 random_uint32() { + return rand() << 1 | rand()&1; +} + +vtype_uint64 random_uint64() { + return random_int32() * random_int32(); +} + +vtype_int64 random_int64() { + return random_uint32() * random_uint32(); +} diff --git a/tests/src/test.c b/tests/src/test.c new file mode 100644 index 0000000..397a067 --- /dev/null +++ b/tests/src/test.c @@ -0,0 +1,41 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../include/test.h" +#include "../include/time.h" +#include "../include/random.h" + +static TIMER GLOBAL_TIMER; +static const char* TEST_NAME = 0; + +static void test_uload() __attribute__((destructor)); + +static void test_uload() { + if (TEST_NAME) { + timer_stop(&GLOBAL_TIMER); + puts(""); + put_separator(); + printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is end.\nExecusion time: \e[m\e[32m%.6Lf sec\e[m\n", TEST_NAME, timer_value(&GLOBAL_TIMER)); + put_separator(); + /////////////////////////// + timer_free(&GLOBAL_TIMER); + } +} + +void put_separator() { puts("\e[37;2m=== === === === === === === ===\e[m"); } + +void test_init(int argc, char** argv) { + timer_init(&GLOBAL_TIMER); + timer_start(&GLOBAL_TIMER); + + TEST_NAME = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]; + + put_separator(); + printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME); + put_separator(); + + random_init(argc, argv); + + puts(""); +} diff --git a/tests/src/time.c b/tests/src/time.c new file mode 100644 index 0000000..1151708 --- /dev/null +++ b/tests/src/time.c @@ -0,0 +1,120 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#define __USE_MISC +#include +#include +#include +#include "../include/time.h" + +typedef struct timer_internal { + struct rusage begin; + struct rusage end; + struct rusage br; + unsigned long offset; + struct { + int isbr:1; + int isend:1; + }; +} TIMER_INFO; + + +void timer_init(TIMER* timer) { + TIMER_INFO *t; + if (!(t = timer->info = malloc(sizeof(TIMER_INFO)))) + abort(); + memset(t, 0, sizeof(*t)); +} + +void timer_start(TIMER* timer) { + TIMER_INFO *t = timer->info; + + if (t->isend) + return; + + if (t->isbr) { + struct rusage br; + + if (getrusage(RUSAGE_SELF, &br) < 0) + abort(); + + t->offset += br.ru_utime.tv_sec * 1000000 - t->br.ru_utime.tv_sec * 1000000; + t->offset -= t->br.ru_utime.tv_usec; + t->offset += br.ru_utime.tv_usec; + + memset(&t->br, 0, sizeof(t->br)); + t->isbr = 0; + } else if (getrusage(RUSAGE_SELF, &t->begin) < 0) + abort(); +} + +void timer_pause (TIMER* timer) { + TIMER_INFO *t = timer->info; + + if (t->isend) + return; + + if (!t->isbr) { + if (getrusage(RUSAGE_SELF, &t->br) < 0) + abort(); + + t->isbr = 1; + } +} + +void timer_stop (TIMER* timer) { + TIMER_INFO *t = timer->info; + + if (t->isend) + return; + + if (getrusage(RUSAGE_SELF, &t->end) < 0) + abort(); + + if (t->isbr) { + t->offset += t->end.ru_utime.tv_sec * 1000000 - t->br.ru_utime.tv_sec * 1000000; + t->offset -= t->br.ru_utime.tv_usec; + t->offset += t->end.ru_utime.tv_usec; + + memset(&t->br, 0, sizeof(t->br)); + t->isbr = 0; + } + + t->isend = 1; +} + +void timer_free (TIMER* timer) { + free(timer->info); +} + +vtype_ldouble timer_value(TIMER* timer) { + static char empty[sizeof(struct rusage)] = { 0 }; + + TIMER_INFO *t = timer->info; + + struct rusage _; + struct rusage *end; + vtype_ldouble value; + + if (memcmp(&t->begin, empty, sizeof(struct rusage)) == 0) { + return 0; + } + + if (!t->isend) { + if (getrusage(RUSAGE_SELF, end = &_) < 0) + abort(); + } else end = &t->end; + + value = end->ru_utime.tv_sec * 1000000 - t->begin.ru_utime.tv_sec * 1000000; + value -= t->begin.ru_utime.tv_usec; + value += end->ru_utime.tv_usec; + value -= t->offset; + + return value / 1000000; +} + +void psleep(unsigned long microsec) { + usleep(microsec); +} From ddca5c14cce785329871bcb3b1bc92b757b6755b Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Jun 2022 21:48:56 +0300 Subject: [PATCH 02/42] Update tests base --- tests/src/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/random.c b/tests/src/random.c index a09ea4a..340b935 100644 --- a/tests/src/random.c +++ b/tests/src/random.c @@ -68,7 +68,7 @@ vtype_uint16 random_uint16() { } vtype_uint32 random_uint32() { - return rand() << 1 | rand()&1; + return (rand()<<1) | (rand()&1); } vtype_uint64 random_uint64() { From 4c2d6df0eeb6f08fd14bf95e2d876ffef7ec6491 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Jun 2022 22:22:13 +0300 Subject: [PATCH 03/42] Add tests .gitignore --- tests/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/.gitignore diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..5e56e04 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +/bin From 6d09188fdd1d23a4ece9e7757235c1f369b4a025 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Jun 2022 22:23:01 +0300 Subject: [PATCH 04/42] Add tests skeleton --- tests/Makefile | 81 +++++++++++++++++++++++++++++++++++++++++ tests/src/array/main.c | 8 ++++ tests/src/array/plug.h | 29 +++++++++++++++ tests/src/list/main.c | 13 +++++++ tests/src/list/plug.h | 23 ++++++++++++ tests/src/map/main.c | 13 +++++++ tests/src/map/plug.h | 23 ++++++++++++ tests/src/set/main.c | 13 +++++++ tests/src/set/plug.h | 23 ++++++++++++ tests/src/string/main.c | 8 ++++ tests/src/string/plug.h | 23 ++++++++++++ 11 files changed, 257 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/src/array/main.c create mode 100644 tests/src/array/plug.h create mode 100644 tests/src/list/main.c create mode 100644 tests/src/list/plug.h create mode 100644 tests/src/map/main.c create mode 100644 tests/src/map/plug.h create mode 100644 tests/src/set/main.c create mode 100644 tests/src/set/plug.h create mode 100644 tests/src/string/main.c create mode 100644 tests/src/string/plug.h diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..0bd0ddf --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,81 @@ +# This software is licensed by the MIT License, see LICENSE file +# Copyright © 2022 Gregory Lirent + +######################################################################################################################## + +BUILD_PATH ?= ./bin + +######################################################################################################################## + +CC = clang +MKDIR = mkdir -p +RMRF = rm -rf +AR = ar crs +CP = cp + +$(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_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)) + +######################################################################################################################## + +tests: modules +tests: $(BUILD_PATH)/array +tests: $(BUILD_PATH)/string +tests: $(BUILD_PATH)/list +tests: $(BUILD_PATH)/map +tests: $(BUILD_PATH)/set + +######################################################################################################################## + +$(BUILD_PATH)/obj/%.o: ./src/%.c | $(BUILD_PATH)/obj/ + $(CC) $^ -o $@ $(CFLAGS) + +$(BUILD_PATH)/array: ./src/array/main.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ + $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/string: ./src/string/main.c $(OBJECTS_STRING) | $(BUILD_PATH)/ + $(CC) $^ -o $@ ../modules/libunic/bin/libunic.a $(CFLAGS) +$(BUILD_PATH)/list: ./src/list/main.c $(OBJECTS_LIST) | $(BUILD_PATH)/ + $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/map: ./src/map/main.c $(OBJECTS_MAP) | $(BUILD_PATH)/ + $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/set: ./src/set/main.c $(OBJECTS_SET) | $(BUILD_PATH)/ + $(CC) $^ -o $@ $(CFLAGS) + +######################################################################################################################## + +$(BUILD_PATH)/: + $(MKDIR) $@ +$(BUILD_PATH)/obj/: | $(BUILD_PATH)/ + $(MKDIR) $@ + +######################################################################################################################## + +clean: + $(RMRF) ./bin/ + cd ../ && $(MAKE) clean + +######################################################################################################################## + +modules: ../bin/debug/libcdsb.a + +../bin/debug/libcdsb.a: + cd ../ && $(MAKE) debug diff --git a/tests/src/array/main.c b/tests/src/array/main.c new file mode 100644 index 0000000..5c9f26c --- /dev/null +++ b/tests/src/array/main.c @@ -0,0 +1,8 @@ +/* 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); +} diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h new file mode 100644 index 0000000..74bf6d4 --- /dev/null +++ b/tests/src/array/plug.h @@ -0,0 +1,29 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../../../include/array.h" + +#include "../../include/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +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; } + +void string_free(vtype_string* 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 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(); } + +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)); } diff --git a/tests/src/list/main.c b/tests/src/list/main.c new file mode 100644 index 0000000..8fa9908 --- /dev/null +++ b/tests/src/list/main.c @@ -0,0 +1,13 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "plug.h" + +vtype_list* list_duplicate(const vtype_list* x) { return 0; } +int list_compare(const vtype_list* s0, const vtype_list* s1) { return random_int8(); } +void list_free(vtype_list* x) {} + + +int main(int argc, char** argv) { + test_init(argc, argv); +} diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h new file mode 100644 index 0000000..76e2ed4 --- /dev/null +++ b/tests/src/list/plug.h @@ -0,0 +1,23 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../include/list.h" + +#include "../../include/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_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 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 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(); } diff --git a/tests/src/map/main.c b/tests/src/map/main.c new file mode 100644 index 0000000..a0638f1 --- /dev/null +++ b/tests/src/map/main.c @@ -0,0 +1,13 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "plug.h" + +vtype_map* map_duplicate(const vtype_map* x) { return 0; } +int map_compare(const vtype_map* s0, const vtype_map* s1) { return random_int8(); } +void map_free(vtype_map* x) {} + + +int main(int argc, char** argv) { + test_init(argc, argv); +} diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h new file mode 100644 index 0000000..093107c --- /dev/null +++ b/tests/src/map/plug.h @@ -0,0 +1,23 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../include/map.h" + +#include "../../include/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_list* list_duplicate (const vtype_list* x) { return 0; } +vtype_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 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 vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } diff --git a/tests/src/set/main.c b/tests/src/set/main.c new file mode 100644 index 0000000..3ba1d93 --- /dev/null +++ b/tests/src/set/main.c @@ -0,0 +1,13 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "plug.h" + +vtype_set* vset_duplicate(const vtype_set* x) { return 0; } +int vset_compare(const vtype_set* s0, const vtype_set* s1) { return random_int8(); } +void vset_free(vtype_set* x) {} + + +int main(int argc, char** argv) { + test_init(argc, argv); +} diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h new file mode 100644 index 0000000..661c878 --- /dev/null +++ b/tests/src/set/plug.h @@ -0,0 +1,23 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../include/set.h" + +#include "../../include/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_list* list_duplicate (const vtype_list* x) { return 0; } +vtype_map* map_duplicate (const vtype_map* x) { return 0; } + +void string_free(vtype_string* x) {} +void array_free (vtype_array* x) {} +void list_free (vtype_list* x) {} +void map_free (vtype_map* x) {} + +int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); } +int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } +int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); } +int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } diff --git a/tests/src/string/main.c b/tests/src/string/main.c new file mode 100644 index 0000000..5c9f26c --- /dev/null +++ b/tests/src/string/main.c @@ -0,0 +1,8 @@ +/* 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); +} diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h new file mode 100644 index 0000000..257d6e9 --- /dev/null +++ b/tests/src/string/plug.h @@ -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/random.h" +#include "../../include/test.h" +#include "../../include/time.h" + +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 array_free (vtype_array* x) {} +void list_free (vtype_list* x) {} +void map_free (vtype_map* x) {} +void vset_free (vtype_set* 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(); } From 8ed715965594b072a41dd90c4bb2440db0e5cdaa Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 11:53:54 +0300 Subject: [PATCH 05/42] Add debug symbols to tests --- tests/Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 0bd0ddf..7f10ce4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,7 +13,7 @@ RMRF = rm -rf AR = ar crs CP = cp -$(BUILD_PATH)/obj/%.o: CFLAGS := $(CFLAGS) -Og -fPIC -c -g3 -Wall +$(BUILD_PATH)/obj/%.o: CFLAGS := $(CFLAGS) -Og -fPIC -c -g3 -Wall ######################################################################################################################## @@ -50,15 +50,15 @@ $(BUILD_PATH)/obj/%.o: ./src/%.c | $(BUILD_PATH)/obj/ $(CC) $^ -o $@ $(CFLAGS) $(BUILD_PATH)/array: ./src/array/main.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ - $(CC) $^ -o $@ $(CFLAGS) + $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall $(BUILD_PATH)/string: ./src/string/main.c $(OBJECTS_STRING) | $(BUILD_PATH)/ - $(CC) $^ -o $@ ../modules/libunic/bin/libunic.a $(CFLAGS) + $(CC) $^ -o $@ ../modules/libunic/bin/libunic.a $(CFLAGS) -g3 -Wall $(BUILD_PATH)/list: ./src/list/main.c $(OBJECTS_LIST) | $(BUILD_PATH)/ - $(CC) $^ -o $@ $(CFLAGS) + $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall $(BUILD_PATH)/map: ./src/map/main.c $(OBJECTS_MAP) | $(BUILD_PATH)/ - $(CC) $^ -o $@ $(CFLAGS) + $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall $(BUILD_PATH)/set: ./src/set/main.c $(OBJECTS_SET) | $(BUILD_PATH)/ - $(CC) $^ -o $@ $(CFLAGS) + $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall ######################################################################################################################## From ee602d91b90aa395aa96a64cb9a18dd381ed92d7 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 11:54:22 +0300 Subject: [PATCH 06/42] Update tests base --- tests/src/random.c | 3 ++- tests/src/time.c | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/src/random.c b/tests/src/random.c index 340b935..1fa5a28 100644 --- a/tests/src/random.c +++ b/tests/src/random.c @@ -9,6 +9,7 @@ static void init(unsigned int seed) { + if (!seed) seed = time(0); printf("\e[36mRandom module initialized with seed: \e[m\e[32m%u\n\e[m", seed); put_separator(); srand(seed); @@ -16,7 +17,7 @@ static void init(unsigned int seed) { int random_init(int argc, char** argv) { if (argc < 2) { - init(time(0)); + init(0); } else init(atol(argv[1])); return 0; diff --git a/tests/src/time.c b/tests/src/time.c index 1151708..255f799 100644 --- a/tests/src/time.c +++ b/tests/src/time.c @@ -3,7 +3,9 @@ #include #include +#ifndef __USE_MISC #define __USE_MISC +#endif #include #include #include From f2742661fb6d50acbb62ba4aafc65e4e97e32c45 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 11:54:40 +0300 Subject: [PATCH 07/42] Update array test --- tests/src/array/main.c | 94 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tests/src/array/main.c b/tests/src/array/main.c index 5c9f26c..1586be0 100644 --- a/tests/src/array/main.c +++ b/tests/src/array/main.c @@ -1,8 +1,102 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ +#include "../../../include/extra/vtype.h" #include "plug.h" +static void array_push_random_value(vtype_array* x) { + switch (random_uint8()%10) { + default: + case 0: array_push(x, random_boolean()); break; + case 1: array_push(x, random_uint8()); break; + case 2: array_push(x, random_uint16()); break; + case 3: array_push(x, random_uint32()); break; + case 4: array_push(x, random_uint64()); break; + case 5: array_push(x, random_int8()); break; + case 6: array_push(x, random_int16()); break; + case 7: array_push(x, random_int32()); break; + case 8: array_push(x, random_int64()); break; + case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + } +} + +static void array_push_random_float(vtype_array* x) { + switch (random_uint8()%3) { + default: + case 0: array_push(x, random_float()); break; + case 1: array_push(x, random_double()); break; + case 2: array_push(x, random_ldouble()); break; + } +} + +static void array_push_random(vtype_array* x) { + if (x->type < VTYPE_FLOAT) { + array_push_random_value(x); + } else array_push_random_float(x); +} + +static void array_init_random(vtype_array* x) { + switch (random_uint8()%13) { + default: + case 0: array_init(x, VTYPE_BOOLEAN); break; + case 1: array_init(x, VTYPE_UINT8); break; + case 2: array_init(x, VTYPE_UINT16); break; + case 3: array_init(x, VTYPE_UINT32); break; + case 4: array_init(x, VTYPE_UINT64); break; + case 5: array_init(x, VTYPE_INT8); break; + case 6: array_init(x, VTYPE_INT16); break; + case 7: array_init(x, VTYPE_INT32); break; + case 8: array_init(x, VTYPE_INT64); break; + case 9: array_init(x, VTYPE_POINTER); break; + case 10: array_init(x, VTYPE_FLOAT); break; + case 11: array_init(x, VTYPE_DOUBLE); break; + case 12: array_init(x, VTYPE_LDOUBLE); break; + } +} + +static vtype_array array_random() { + unsigned short n = random_int16(); + + vtype_array x; + + array_init_random(&x); + + for (int i = 0; i < 10; ++i) { + array_push_random(&x); + } + + return x; +} + + int main(int argc, char** argv) { test_init(argc, argv); + + vtype_array x = { .mem = 0 }; + + do { + array_free(&x); + x = array_random(); + } while (x.type >= VTYPE_FLOAT); + + printf("Array initialized with type `%s`;\n", libcdsb_vtype_name(x.type)); + printf("Array has %lu elements (%lu bytes);\n", array_size(&x), array_nmemb(&x)); + puts(""); + + for (int i = 0; i < x.size; ++i) { + printf("%s\n", libcdsb_vtype_stringify(array_at(&x, i), x.type)); + } + + for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) { + ssize_t index = array_indexof(&x, v); + if (index >= 0) { + printf("Value %lu was found on the position %ld\n", v, index); + break; + } + } + + + + + array_free(&x); } From 6a6b558979cbd6f302dbe8f809f7d05a376b3fa8 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 12:18:58 +0300 Subject: [PATCH 08/42] Delete test's .gitignore --- tests/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/.gitignore diff --git a/tests/.gitignore b/tests/.gitignore deleted file mode 100644 index 5e56e04..0000000 --- a/tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin From 315e35e7f6b515b67c4575608268a1d64628f11a Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 14:09:11 +0300 Subject: [PATCH 09/42] Update tests base --- tests/src/test.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/src/test.c b/tests/src/test.c index 397a067..7e24bc4 100644 --- a/tests/src/test.c +++ b/tests/src/test.c @@ -18,7 +18,6 @@ static void test_uload() { put_separator(); printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is end.\nExecusion time: \e[m\e[32m%.6Lf sec\e[m\n", TEST_NAME, timer_value(&GLOBAL_TIMER)); put_separator(); - /////////////////////////// timer_free(&GLOBAL_TIMER); } } @@ -32,7 +31,7 @@ void test_init(int argc, char** argv) { TEST_NAME = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]; put_separator(); - printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME); + printf("\e[H\e[J\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME); put_separator(); random_init(argc, argv); From ed737e31774a8ff3f3bbe9067f53023ec89ed0c1 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 14:09:23 +0300 Subject: [PATCH 10/42] Update array tests --- tests/src/array/change.c | 29 ++++++++++++ tests/src/array/main.c | 96 +++++++--------------------------------- tests/src/array/plug.h | 80 +++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 80 deletions(-) create mode 100644 tests/src/array/change.c diff --git a/tests/src/array/change.c b/tests/src/array/change.c new file mode 100644 index 0000000..92a6fa0 --- /dev/null +++ b/tests/src/array/change.c @@ -0,0 +1,29 @@ +/* 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); + + vtype_array y; + vtype_array x = array_random(8); + + array_info(&x); + array_sort(&x); + array_print(&x, 0); + + array_slice(&y, &x, 2, 4, 1); + + array_print(&x, "source"); + array_print(&y, "cutted"); + + array_remove_by_index(&x, -1); + array_remove_by_index(&y, -1); + + array_print(&x, "source (last removed)"); + array_print(&y, "cutted (last removed)"); + + array_free(&x); + array_free(&y); +} diff --git a/tests/src/array/main.c b/tests/src/array/main.c index 1586be0..a8b5ab4 100644 --- a/tests/src/array/main.c +++ b/tests/src/array/main.c @@ -1,74 +1,20 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ -#include "../../../include/extra/vtype.h" #include "plug.h" -static void array_push_random_value(vtype_array* x) { - switch (random_uint8()%10) { - default: - case 0: array_push(x, random_boolean()); break; - case 1: array_push(x, random_uint8()); break; - case 2: array_push(x, random_uint16()); break; - case 3: array_push(x, random_uint32()); break; - case 4: array_push(x, random_uint64()); break; - case 5: array_push(x, random_int8()); break; - case 6: array_push(x, random_int16()); break; - case 7: array_push(x, random_int32()); break; - case 8: array_push(x, random_int64()); break; - case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; +static void array_find_minimal(const vtype_array* x) { + array_print(x, 0); + for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) { + ssize_t index = array_indexof(x, v); + if (index >= 0) { + printf("\e[36mValue \e[m\e[31m%lu\e[m\e[36m was found at the position \e[m\e[32;1m%ld\e[m\n", v, index); + break; + } } + put_separator(); } -static void array_push_random_float(vtype_array* x) { - switch (random_uint8()%3) { - default: - case 0: array_push(x, random_float()); break; - case 1: array_push(x, random_double()); break; - case 2: array_push(x, random_ldouble()); break; - } -} - -static void array_push_random(vtype_array* x) { - if (x->type < VTYPE_FLOAT) { - array_push_random_value(x); - } else array_push_random_float(x); -} - -static void array_init_random(vtype_array* x) { - switch (random_uint8()%13) { - default: - case 0: array_init(x, VTYPE_BOOLEAN); break; - case 1: array_init(x, VTYPE_UINT8); break; - case 2: array_init(x, VTYPE_UINT16); break; - case 3: array_init(x, VTYPE_UINT32); break; - case 4: array_init(x, VTYPE_UINT64); break; - case 5: array_init(x, VTYPE_INT8); break; - case 6: array_init(x, VTYPE_INT16); break; - case 7: array_init(x, VTYPE_INT32); break; - case 8: array_init(x, VTYPE_INT64); break; - case 9: array_init(x, VTYPE_POINTER); break; - case 10: array_init(x, VTYPE_FLOAT); break; - case 11: array_init(x, VTYPE_DOUBLE); break; - case 12: array_init(x, VTYPE_LDOUBLE); break; - } -} - -static vtype_array array_random() { - unsigned short n = random_int16(); - - vtype_array x; - - array_init_random(&x); - - for (int i = 0; i < 10; ++i) { - array_push_random(&x); - } - - return x; -} - - int main(int argc, char** argv) { test_init(argc, argv); @@ -76,27 +22,17 @@ int main(int argc, char** argv) { do { array_free(&x); - x = array_random(); + x = array_random(8); } while (x.type >= VTYPE_FLOAT); - printf("Array initialized with type `%s`;\n", libcdsb_vtype_name(x.type)); - printf("Array has %lu elements (%lu bytes);\n", array_size(&x), array_nmemb(&x)); - puts(""); - - for (int i = 0; i < x.size; ++i) { - printf("%s\n", libcdsb_vtype_stringify(array_at(&x, i), x.type)); - } - - for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) { - ssize_t index = array_indexof(&x, v); - if (index >= 0) { - printf("Value %lu was found on the position %ld\n", v, index); - break; - } - } - + array_info(&x); + array_find_minimal(&x); + array_reverse(&x); + array_print(&x, "reversed"); + array_sort(&x); + array_print(&x, "sorted"); array_free(&x); } diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index 74bf6d4..8e17da4 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -2,6 +2,7 @@ /* Copyright © 2022 Gregory Lirent */ #include +#include "../../../include/extra/vtype.h" #include "../../../include/array.h" #include "../../include/random.h" @@ -27,3 +28,82 @@ extern void string_copy_init(vtype_string* x, const vtype_string* s) { memset(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)); } + + +static void array_push_random_value(vtype_array* x) { + switch (random_uint8()%10) { + default: + case 0: array_push(x, random_boolean()); break; + case 1: array_push(x, random_uint8()); break; + case 2: array_push(x, random_uint16()); break; + case 3: array_push(x, random_uint32()); break; + case 4: array_push(x, random_uint64()); break; + case 5: array_push(x, random_int8()); break; + case 6: array_push(x, random_int16()); break; + case 7: array_push(x, random_int32()); break; + case 8: array_push(x, random_int64()); break; + case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + } +} + +static void array_push_random_float(vtype_array* x) { + switch (random_uint8()%3) { + default: + case 0: array_push(x, random_float()); break; + case 1: array_push(x, random_double()); break; + case 2: array_push(x, random_ldouble()); break; + } +} + +static void array_push_random(vtype_array* x) { + if (x->type < VTYPE_FLOAT) { + array_push_random_value(x); + } else array_push_random_float(x); +} + +static void array_init_random(vtype_array* x) { + switch (random_uint8()%13) { + default: + case 0: array_init(x, VTYPE_BOOLEAN); break; + case 1: array_init(x, VTYPE_UINT8); break; + case 2: array_init(x, VTYPE_UINT16); break; + case 3: array_init(x, VTYPE_UINT32); break; + case 4: array_init(x, VTYPE_UINT64); break; + case 5: array_init(x, VTYPE_INT8); break; + case 6: array_init(x, VTYPE_INT16); break; + case 7: array_init(x, VTYPE_INT32); break; + case 8: array_init(x, VTYPE_INT64); break; + case 9: array_init(x, VTYPE_POINTER); break; + case 10: array_init(x, VTYPE_FLOAT); break; + case 11: array_init(x, VTYPE_DOUBLE); break; + case 12: array_init(x, VTYPE_LDOUBLE); break; + } +} + +static vtype_array array_random(unsigned int n) { + vtype_array x; + + array_init_random(&x); + + for (int i = 0; i < n; ++i) { + array_push_random(&x); + } + + return x; +} + +static void array_print(const vtype_array* x, const char* prefix) { + if (!prefix) puts("\e[36mArray values:\e[m"); + else printf("\e[36mArray %s values:\e[m\n", prefix); + for (int i = 0; i < x->size; ++i) { + printf("\e[32;1m%5d: \e[m\e[31m%24s\e[m\n", i, libcdsb_vtype_stringify(array_at(x, i), x->type)); + } + put_separator(); +} + + +static void array_info(const vtype_array* x) { + printf("\e[36mArray initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", libcdsb_vtype_name(x->type)); + printf("Array has \e[m\e[32m%lu\e[m\e[36m elements (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", array_size(x), array_nmemb(x)); + put_separator(); +} From 005014319fe070b5d6c7955a167b4219c4c1b853 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 14:09:41 +0300 Subject: [PATCH 11/42] Update tests Makefile --- tests/Makefile | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 7f10ce4..67d4238 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -38,26 +38,27 @@ OBJECTS_SET := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix . ######################################################################################################################## tests: modules -tests: $(BUILD_PATH)/array -tests: $(BUILD_PATH)/string -tests: $(BUILD_PATH)/list -tests: $(BUILD_PATH)/map -tests: $(BUILD_PATH)/set +tests: $(addprefix $(BUILD_PATH)/array-,$(notdir $(basename $(wildcard ./src/array/*.c)))) +tests: $(addprefix $(BUILD_PATH)/string-,$(notdir $(basename $(wildcard ./src/string/*.c)))) +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)))) + ######################################################################################################################## $(BUILD_PATH)/obj/%.o: ./src/%.c | $(BUILD_PATH)/obj/ $(CC) $^ -o $@ $(CFLAGS) -$(BUILD_PATH)/array: ./src/array/main.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ +$(BUILD_PATH)/array-%: ./src/array/%.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall -$(BUILD_PATH)/string: ./src/string/main.c $(OBJECTS_STRING) | $(BUILD_PATH)/ +$(BUILD_PATH)/string-%: ./src/string/%.c $(OBJECTS_STRING) | $(BUILD_PATH)/ $(CC) $^ -o $@ ../modules/libunic/bin/libunic.a $(CFLAGS) -g3 -Wall -$(BUILD_PATH)/list: ./src/list/main.c $(OBJECTS_LIST) | $(BUILD_PATH)/ +$(BUILD_PATH)/list-%: ./src/list/%.c $(OBJECTS_LIST) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall -$(BUILD_PATH)/map: ./src/map/main.c $(OBJECTS_MAP) | $(BUILD_PATH)/ +$(BUILD_PATH)/map-%: ./src/map/%.c $(OBJECTS_MAP) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall -$(BUILD_PATH)/set: ./src/set/main.c $(OBJECTS_SET) | $(BUILD_PATH)/ +$(BUILD_PATH)/set-%: ./src/set/%.c $(OBJECTS_SET) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall ######################################################################################################################## From c32d771793ed4bd42077df77d268327ea9dae9ea Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 19:28:44 +0300 Subject: [PATCH 12/42] Update tests Makefile --- tests/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 67d4238..d810ace 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -76,7 +76,8 @@ clean: ######################################################################################################################## +FORCE: modules: ../bin/debug/libcdsb.a -../bin/debug/libcdsb.a: +../bin/debug/libcdsb.a: FORCE cd ../ && $(MAKE) debug From 25bb3904084f48d7d3e5966f1913192370cada16 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 19:29:06 +0300 Subject: [PATCH 13/42] Update tests base --- tests/include/random.h | 2 ++ tests/src/random.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/include/random.h b/tests/include/random.h index fa24373..69b2b07 100644 --- a/tests/include/random.h +++ b/tests/include/random.h @@ -23,4 +23,6 @@ extern vtype_int16 random_int16(); extern vtype_int32 random_int32(); extern vtype_int64 random_int64(); +extern char random_ascii_char(); +extern unsigned int random_unicode_symbol(); #endif /* LIBCDSB_TESTS_RANDOM_H */ diff --git a/tests/src/random.c b/tests/src/random.c index 1fa5a28..c77e74a 100644 --- a/tests/src/random.c +++ b/tests/src/random.c @@ -79,3 +79,33 @@ vtype_uint64 random_uint64() { vtype_int64 random_int64() { return random_uint32() * random_uint32(); } + +char random_ascii_char() { + return (rand()%0x5f) + 0x20; +} + +unsigned int random_unicode_symbol() { + switch (rand()%20) { + default: + case 0: return (random_uint16()%0x005f) + 0x000020; + case 1: return (random_uint16()%0x020f) + 0x0000a1; + case 2: return (random_uint16()%0x01a8) + 0x000388; + case 3: return (random_uint16()%0x0026) + 0x000531; + case 4: return (random_uint16()%0x002a) + 0x000560; + case 5: return (random_uint16()%0x00c6) + 0x001000; + case 6: return (random_uint16()%0x0030) + 0x0010d0; + case 7: return (random_uint16()%0x029d) + 0x001400; + case 8: return (random_uint16()%0x0058) + 0x0016a0; + case 9: return (random_uint16()%0x0074) + 0x001b80; + case 10: return (random_uint16()%0x00f4) + 0x001d00; + case 11: return (random_uint16()%0x0115) + 0x001e00; + case 12: return (random_uint16()%0x008b) + 0x002100; + case 13: return (random_uint16()%0x0297) + 0x002190; + case 14: return (random_uint16()%0x0714) + 0x002460; + case 15: return (random_uint16()%0x00b7) + 0x00a640; + case 16: return (random_uint16()%0x0074) + 0x00a8e0; + case 17: return (random_uint16()%0x009e) + 0x010400; + case 18: return (random_uint16()%0x0137) + 0x010600; + case 19: return (random_uint16()%0x03d8) + 0x01f300; + } +} From 0e0346c368a75879ee4acd0592478e132de5c768 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 19:29:46 +0300 Subject: [PATCH 14/42] Update string tests --- tests/src/string/main.c | 53 +++++++++++++++++++++++++++++++++++++++++ tests/src/string/plug.h | 26 ++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/tests/src/string/main.c b/tests/src/string/main.c index 5c9f26c..4eb7e31 100644 --- a/tests/src/string/main.c +++ b/tests/src/string/main.c @@ -3,6 +3,59 @@ #include "plug.h" + +static vtype_string string_random(unsigned int n) { + vtype_string x; + + if (random_boolean()) { + x.buffer = random_utf8_cstring(n); + } else x.buffer = random_ascii_cstring(n); + + return x; +} + +static void string_concat_random(vtype_string* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_concat(x, v); + free(v); +} + +static void string_info(vtype_string* x) { + printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x)); +} + +static void string_print(const vtype_string* x, const char* prefix) { + if (!prefix) puts("\e[36mString content:\e[m\n"); + else printf("\e[36mString %s content:\e[m\n\n", prefix); + printf("\e[33m%s\e[m\n", x->buffer); + put_separator(); +} + + int main(int argc, char** argv) { test_init(argc, argv); + vtype_string x; + + x = string_random(31); + string_print(&x, "(part 1)"); + + string_concat(&x, '\n'); + string_concat_random(&x, 31); + + { + void* hack = string_at(&x, 32); + string_print((void*)&hack, "(part 2)"); + } + + string_info(&x); + string_print(&x, "concatenated"); + + + + string_free(&x); } diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index 257d6e9..c144b5e 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -1,6 +1,8 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ +#include +#include "../../../modules/libunic/include.h" #include "../../../include/extra/string.h" #include "../../include/random.h" @@ -21,3 +23,27 @@ int array_compare (const vtype_array* s0, const vtype_array* s1) { return rand 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(); } + +static char* random_ascii_cstring(size_t size) { + char* v = malloc(size + 1); + char* p = v; + + while (size--) { + *(p++) = random_ascii_char(); + } + *p = 0; + + return v; +} + +static char* random_utf8_cstring(size_t size) { + char* v = malloc(size * 4 + 1); + char* p = v; + + while (size--) { + p = tochar_unicode(p, random_unicode_symbol()); + } + + *p = 0; + return v; +} From f178825ce6e6b11bd3feda578103956459137dbe Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sat, 4 Jun 2022 00:30:23 +0300 Subject: [PATCH 15/42] Update string tests --- tests/src/string/main.c | 70 ++++++++++++++++++++++------------------- tests/src/string/plug.h | 44 ++++++++++++++++++++++++++ tests/src/string/trim.c | 20 ++++++++++++ 3 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 tests/src/string/trim.c diff --git a/tests/src/string/main.c b/tests/src/string/main.c index 4eb7e31..8b90e66 100644 --- a/tests/src/string/main.c +++ b/tests/src/string/main.c @@ -3,59 +3,65 @@ #include "plug.h" +static void string_print_compare(const vtype_string* s0, const vtype_string* s1) { + int c = string_compare(s0, s1); + char* m; + if (c == 0) m = "=="; + else m = (c < 0) ? "<" : ">"; -static vtype_string string_random(unsigned int n) { - vtype_string x; - - if (random_boolean()) { - x.buffer = random_utf8_cstring(n); - } else x.buffer = random_ascii_cstring(n); - - return x; + puts("\e[36mStrings comparsion:\e[m\n"); + printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); } -static void string_concat_random(vtype_string* x, unsigned int n) { - char* v; +static void string_print_case_compare(const vtype_string* s0, const vtype_string* s1) { + int c = string_case_compare(s0, s1); + char* m; + if (c == 0) m = "=="; + else m = (c < 0) ? "<" : ">"; - if (random_boolean()) { - v = random_utf8_cstring(n); - } else v = random_ascii_cstring(n); - - string_concat(x, v); - free(v); + puts("\e[36mStrings case insensitive comparsion:\e[m\n"); + printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); } -static void string_info(vtype_string* x) { - printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x)); -} - -static void string_print(const vtype_string* x, const char* prefix) { - if (!prefix) puts("\e[36mString content:\e[m\n"); - else printf("\e[36mString %s content:\e[m\n\n", prefix); - printf("\e[33m%s\e[m\n", x->buffer); - put_separator(); -} - - int main(int argc, char** argv) { test_init(argc, argv); - vtype_string x; + vtype_string x, y; - x = string_random(31); + x = string_random(30); string_print(&x, "(part 1)"); string_concat(&x, '\n'); - string_concat_random(&x, 31); + string_concat_random(&x, 30); { - void* hack = string_at(&x, 32); + char* repl = random_utf8_cstring(30); + void* hack = string_at(&x, 31); string_print((void*)&hack, "(part 2)"); + string_print((void*)&repl, "(part 2 replaced)"); + string_replace(&x, hack, repl, -1); + free(repl); } string_info(&x); string_print(&x, "concatenated"); + do { + string_replace_random(&x, 12); + } while (string_size(&x) == string_nmemb(&x)); + y = string_copy(&x); + + string_to_lower(&x); + string_to_upper(&y); + + string_print_compare(&x, &y); + string_print_case_compare(&x, &y); + + string_reverse(&y); + string_capitalize(&y); + + string_print(&y, "reversed & capitalized"); string_free(&x); + string_free(&y); } diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index c144b5e..d0fc750 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -47,3 +47,47 @@ static char* random_utf8_cstring(size_t size) { *p = 0; return v; } + +static void string_info(vtype_string* x) { + printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x)); +} + +static void string_print(const vtype_string* x, const char* prefix) { + if (!prefix) puts("\e[36mString content:\e[m\n"); + else printf("\e[36mString %s content:\e[m\n\n", prefix); + printf("\e[33m\"%s\"\e[m\n", x->buffer); + put_separator(); +} + +static vtype_string string_random(unsigned int n) { + vtype_string x; + + if (random_boolean()) { + x.buffer = random_utf8_cstring(n); + } else x.buffer = random_ascii_cstring(n); + + return x; +} + +static void string_concat_random(vtype_string* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_concat(x, v); + free(v); +} + +static void string_replace_random(vtype_string* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_replace(x, x, v, -1); + + free(v); +} diff --git a/tests/src/string/trim.c b/tests/src/string/trim.c new file mode 100644 index 0000000..a320ce8 --- /dev/null +++ b/tests/src/string/trim.c @@ -0,0 +1,20 @@ +/* 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); + vtype_string x; + + x = string_random(12); + + string_align_center(&x, 30, 0); + + string_print(&x, 0); + + string_trim(&x, 0); + string_print(&x, 0); + + string_free(&x); +} From 46a7f69a381cbe084f23d97816168bf2649df860 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sat, 4 Jun 2022 15:38:55 +0300 Subject: [PATCH 16/42] Update string tests --- tests/src/string/trim.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/src/string/trim.c b/tests/src/string/trim.c index a320ce8..4111e50 100644 --- a/tests/src/string/trim.c +++ b/tests/src/string/trim.c @@ -3,18 +3,27 @@ #include "plug.h" + int main(int argc, char** argv) { test_init(argc, argv); vtype_string x; + int c = random_unicode_symbol(); x = string_random(12); string_align_center(&x, 30, 0); - string_print(&x, 0); - string_trim(&x, 0); + string_trim_spaces(&x); string_print(&x, 0); + + string_align_center(&x, 30, c); + string_print(&x, 0); + + string_trim(&x, c); + string_print(&x, 0); + + string_free(&x); } From f6033d0c4f006809b5989ecfbaad6e4dc1b59143 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sat, 4 Jun 2022 22:17:03 +0300 Subject: [PATCH 17/42] Update used array symbols --- tests/src/array/change.c | 4 ++-- tests/src/array/plug.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/array/change.c b/tests/src/array/change.c index 92a6fa0..989193a 100644 --- a/tests/src/array/change.c +++ b/tests/src/array/change.c @@ -18,8 +18,8 @@ int main(int argc, char** argv) { array_print(&x, "source"); array_print(&y, "cutted"); - array_remove_by_index(&x, -1); - array_remove_by_index(&y, -1); + array_remove(&x, -1); + array_remove(&y, -1); array_print(&x, "source (last removed)"); array_print(&y, "cutted (last removed)"); diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index 8e17da4..63e22d0 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -3,7 +3,7 @@ #include #include "../../../include/extra/vtype.h" -#include "../../../include/array.h" +#include "../../../include/extra/array.h" #include "../../include/random.h" #include "../../include/test.h" From a6a623bff6d193e9cff287aecb90934252bed9ce Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sat, 4 Jun 2022 22:17:26 +0300 Subject: [PATCH 18/42] Refactor string tests --- tests/src/string/main.c | 23 +++++++++++++++++++++++ tests/src/string/plug.h | 23 ----------------------- tests/src/string/trim.c | 10 +++++++--- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/tests/src/string/main.c b/tests/src/string/main.c index 8b90e66..141e0cc 100644 --- a/tests/src/string/main.c +++ b/tests/src/string/main.c @@ -3,6 +3,29 @@ #include "plug.h" +static void string_concat_random(vtype_string* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_concat(x, v); + free(v); +} + +static void string_replace_random(vtype_string* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_replace(x, x, v, -1); + + free(v); +} + static void string_print_compare(const vtype_string* s0, const vtype_string* s1) { int c = string_compare(s0, s1); char* m; diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index d0fc750..472bbf1 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -68,26 +68,3 @@ static vtype_string string_random(unsigned int n) { return x; } - -static void string_concat_random(vtype_string* x, unsigned int n) { - char* v; - - if (random_boolean()) { - v = random_utf8_cstring(n); - } else v = random_ascii_cstring(n); - - string_concat(x, v); - free(v); -} - -static void string_replace_random(vtype_string* x, unsigned int n) { - char* v; - - if (random_boolean()) { - v = random_utf8_cstring(n); - } else v = random_ascii_cstring(n); - - string_replace(x, x, v, -1); - - free(v); -} diff --git a/tests/src/string/trim.c b/tests/src/string/trim.c index 4111e50..c5329d8 100644 --- a/tests/src/string/trim.c +++ b/tests/src/string/trim.c @@ -12,17 +12,21 @@ int main(int argc, char** argv) { x = string_random(12); string_align_center(&x, 30, 0); + string_info(&x); string_print(&x, 0); string_trim_spaces(&x); - string_print(&x, 0); - + string_info(&x); + string_print(&x, "trimmed"); + put_separator(); string_align_center(&x, 30, c); + string_info(&x); string_print(&x, 0); string_trim(&x, c); - string_print(&x, 0); + string_info(&x); + string_print(&x, "trimmed"); string_free(&x); From 8774804289eacc61f4bf5900efaec60730b8d688 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sun, 5 Jun 2022 18:31:19 +0300 Subject: [PATCH 19/42] Update list tests --- tests/src/list/main.c | 21 +++++++++++---- tests/src/list/plug.h | 61 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/tests/src/list/main.c b/tests/src/list/main.c index 8fa9908..93809b5 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -3,11 +3,22 @@ #include "plug.h" -vtype_list* list_duplicate(const vtype_list* x) { return 0; } -int list_compare(const vtype_list* s0, const vtype_list* s1) { return random_int8(); } -void list_free(vtype_list* x) {} - - int main(int argc, char** argv) { test_init(argc, argv); + + vtype_list x; + + list_init(&x); + + for (int i = 0, n = 12 + random_boolean(); i < n; ++i) { + list_push_random(&x, 0); + } + list_sort(&x); + list_info(&x); + list_print(&x, "sorted"); + + list_reverse(&x); + list_print(&x, "reversed"); + + list_free(&x); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index 76e2ed4..6e7ddf9 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -1,12 +1,14 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ -#include "../../../include/list.h" +#include "../../../include/extra/list.h" #include "../../include/random.h" #include "../../include/test.h" #include "../../include/time.h" +#include "../../../src/__internal/vnode.h" + 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; } @@ -21,3 +23,60 @@ int string_compare(const vtype_string* s0, const vtype_string* s1) { return rand 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(); } + + +static void list_push_random(vtype_list* x, vtype_bool front) { + + if (!front) switch (random_uint8()%13) { + default: + case 0: list_push_back(x, random_boolean()); break; + case 1: list_push_back(x, random_uint8()); break; + case 2: list_push_back(x, random_uint16()); break; + case 3: list_push_back(x, random_uint32()); break; + case 4: list_push_back(x, random_uint64()); break; + case 5: list_push_back(x, random_int8()); break; + case 6: list_push_back(x, random_int16()); break; + case 7: list_push_back(x, random_int32()); break; + case 8: list_push_back(x, random_int64()); break; + case 9: list_push_back(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + case 10: list_push_back(x, random_float()); break; + case 11: list_push_back(x, random_double()); break; + case 12: list_push_back(x, random_ldouble()); break; + } else switch (random_uint8()%13) { + default: + case 0: list_push_front(x, random_boolean()); break; + case 1: list_push_front(x, random_uint8()); break; + case 2: list_push_front(x, random_uint16()); break; + case 3: list_push_front(x, random_uint32()); break; + case 4: list_push_front(x, random_uint64()); break; + case 5: list_push_front(x, random_int8()); break; + case 6: list_push_front(x, random_int16()); break; + case 7: list_push_front(x, random_int32()); break; + case 8: list_push_front(x, random_int64()); break; + case 9: list_push_front(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + case 10: list_push_front(x, random_float()); break; + case 11: list_push_front(x, random_double()); break; + case 12: list_push_front(x, random_ldouble()); break; + } +} + +static int list_node_print(void* v, ssize_t i, vtype t) { + printf("\e[32;1m%5ld: \e[m\e[31m%24s\e[m \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", i, + libcdsb_vtype_stringify(v, t), + libcdsb_vtype_name(t)); + return 0; +} + + +static void list_print(const vtype_list* x, const char* prefix) { + if (!prefix) puts("\e[36mList values:\e[m"); + else printf("\e[36mList %s values:\e[m\n", prefix); + + list_foreach(x, list_node_print); + put_separator(); +} + +static void list_info(const vtype_list* x) { + printf("List has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x)); + put_separator(); +} From ab8aad910c2b5efcf09cd286dd355bd407023796 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sun, 5 Jun 2022 19:28:24 +0300 Subject: [PATCH 20/42] Update list tests --- tests/src/list/main.c | 2 +- tests/src/list/plug.h | 2 +- tests/src/list/remove.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/src/list/remove.c diff --git a/tests/src/list/main.c b/tests/src/list/main.c index 93809b5..529a266 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -11,7 +11,7 @@ int main(int argc, char** argv) { list_init(&x); for (int i = 0, n = 12 + random_boolean(); i < n; ++i) { - list_push_random(&x, 0); + list_push_random(&x, random_boolean()); } list_sort(&x); list_info(&x); diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index 6e7ddf9..1da67d2 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -77,6 +77,6 @@ static void list_print(const vtype_list* x, const char* prefix) { } static void list_info(const vtype_list* x) { - printf("List has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x)); + printf("\e[36mList has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x)); put_separator(); } diff --git a/tests/src/list/remove.c b/tests/src/list/remove.c new file mode 100644 index 0000000..2f83e7e --- /dev/null +++ b/tests/src/list/remove.c @@ -0,0 +1,32 @@ +/* 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); + + vtype_list x; + + list_init(&x); + + for (int i = 0, c = random_uint8()%16; i < 12; ++i) { + if (i == c) { + list_push_back(&x, 0); + } else list_push_random(&x, random_boolean()); + } + + list_info(&x); + list_print(&x, 0); + + for (int i = 0, n = list_countof(&x, 0); i < n; ++i) { + int index = list_indexof(&x, 0); + list_remove(&x, 0); + + printf("\e[36mRemove element with index \e[m\e[32;1m%d\e[m\e[36m from list\e[m\n", index); + } + put_separator(); + list_print(&x, "cleaned"); + + list_free(&x); +} From 96b06906f851e12a1b03a7565bb074fc6b1de1dc Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Sun, 5 Jun 2022 19:49:48 +0300 Subject: [PATCH 21/42] Update list tests --- tests/src/list/remove.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/list/remove.c b/tests/src/list/remove.c index 2f83e7e..b3c7039 100644 --- a/tests/src/list/remove.c +++ b/tests/src/list/remove.c @@ -10,7 +10,7 @@ int main(int argc, char** argv) { list_init(&x); - for (int i = 0, c = random_uint8()%16; i < 12; ++i) { + for (int i = 0, c = random_uint8()%12; i < 12; ++i) { if (i == c) { list_push_back(&x, 0); } else list_push_random(&x, random_boolean()); From 566de8019e0f8c3b10fd64c21ceaf9591d877ca0 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Mon, 6 Jun 2022 22:07:35 +0300 Subject: [PATCH 22/42] Refactor test plugs --- tests/include/test.h | 5 ++++ tests/src/array/plug.h | 62 +++++++++++++++++------------------------ tests/src/list/plug.h | 11 ++------ tests/src/string/plug.h | 9 ++++-- tests/src/test.c | 44 +++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 48 deletions(-) diff --git a/tests/include/test.h b/tests/include/test.h index 4a6a4c5..2b689ce 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -2,11 +2,16 @@ /* Copyright © 2022 Gregory Lirent */ #include +#include "../../include/extra/vtype.h" #ifndef LIBCDSB_TESTS_TEST_H #define LIBCDSB_TESTS_TEST_H extern void put_separator(); +extern void print_container_values_prefix(const char* name, const char* prefix); +extern void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type); +extern void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb); + extern void test_init(int argc, char** argv); #endif /* LIBCDSB_TESTS_TEST_H */ diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index 63e22d0..afd1616 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -30,35 +30,23 @@ extern void map_copy_init (vtype_map* x, const vtype_map* s) { memset(x, extern void vset_copy_init (vtype_set* x, const vtype_set* s) { memset(x, 0, sizeof(*x)); } -static void array_push_random_value(vtype_array* x) { - switch (random_uint8()%10) { - default: - case 0: array_push(x, random_boolean()); break; - case 1: array_push(x, random_uint8()); break; - case 2: array_push(x, random_uint16()); break; - case 3: array_push(x, random_uint32()); break; - case 4: array_push(x, random_uint64()); break; - case 5: array_push(x, random_int8()); break; - case 6: array_push(x, random_int16()); break; - case 7: array_push(x, random_int32()); break; - case 8: array_push(x, random_int64()); break; - case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - } -} - -static void array_push_random_float(vtype_array* x) { - switch (random_uint8()%3) { - default: - case 0: array_push(x, random_float()); break; - case 1: array_push(x, random_double()); break; - case 2: array_push(x, random_ldouble()); break; - } -} - static void array_push_random(vtype_array* x) { - if (x->type < VTYPE_FLOAT) { - array_push_random_value(x); - } else array_push_random_float(x); + switch (random_uint8()%13) { + default: + case 0: array_push(x, random_boolean()); break; + case 1: array_push(x, random_uint8()); break; + case 2: array_push(x, random_uint16()); break; + case 3: array_push(x, random_uint32()); break; + case 4: array_push(x, random_uint64()); break; + case 5: array_push(x, random_int8()); break; + case 6: array_push(x, random_int16()); break; + case 7: array_push(x, random_int32()); break; + case 8: array_push(x, random_int64()); break; + case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; + case 10: array_push(x, random_float()); break; + case 11: array_push(x, random_double()); break; + case 12: array_push(x, random_ldouble()); break; + } } static void array_init_random(vtype_array* x) { @@ -92,18 +80,18 @@ static vtype_array array_random(unsigned int n) { return x; } -static void array_print(const vtype_array* x, const char* prefix) { - if (!prefix) puts("\e[36mArray values:\e[m"); - else printf("\e[36mArray %s values:\e[m\n", prefix); - for (int i = 0; i < x->size; ++i) { - printf("\e[32;1m%5d: \e[m\e[31m%24s\e[m\n", i, libcdsb_vtype_stringify(array_at(x, i), x->type)); - } - put_separator(); +static int array_value_print(void* v, ssize_t i, vtype t) { + print_container_value(&i, v, t, false); + return 0; } +static void array_print(const vtype_array* x, const char* prefix) { + print_container_values_prefix("Array", prefix); + array_foreach(x, array_value_print); + put_separator(); +} static void array_info(const vtype_array* x) { - printf("\e[36mArray initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", libcdsb_vtype_name(x->type)); - printf("Array has \e[m\e[32m%lu\e[m\e[36m elements (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", array_size(x), array_nmemb(x)); + print_container_info("Array", 0, &x->type, array_size(x), array_nmemb(x)); put_separator(); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index 1da67d2..b700edd 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -61,22 +61,17 @@ static void list_push_random(vtype_list* x, vtype_bool front) { } static int list_node_print(void* v, ssize_t i, vtype t) { - printf("\e[32;1m%5ld: \e[m\e[31m%24s\e[m \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", i, - libcdsb_vtype_stringify(v, t), - libcdsb_vtype_name(t)); + print_container_value(&i, v, t, 1); return 0; } - static void list_print(const vtype_list* x, const char* prefix) { - if (!prefix) puts("\e[36mList values:\e[m"); - else printf("\e[36mList %s values:\e[m\n", prefix); - + print_container_values_prefix("List", prefix); list_foreach(x, list_node_print); put_separator(); } static void list_info(const vtype_list* x) { - printf("\e[36mList has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x)); + print_container_info("List", "nodes", 0, list_size(x), -1); put_separator(); } diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index 472bbf1..e0b2ad5 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -49,12 +49,15 @@ static char* random_utf8_cstring(size_t size) { } static void string_info(vtype_string* x) { - printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x)); + print_container_info("String", "utf8 chars", 0, string_size(x), string_nmemb(x)); + put_separator(); } static void string_print(const vtype_string* x, const char* prefix) { - if (!prefix) puts("\e[36mString content:\e[m\n"); - else printf("\e[36mString %s content:\e[m\n\n", prefix); + if (prefix) { + printf("\e[36m%s %s content:\e[m\n", "String", prefix); + } else printf("\e[36m%s content:\e[m\n", "String"); + printf("\e[33m\"%s\"\e[m\n", x->buffer); put_separator(); } diff --git a/tests/src/test.c b/tests/src/test.c index 7e24bc4..1d570cf 100644 --- a/tests/src/test.c +++ b/tests/src/test.c @@ -24,6 +24,50 @@ static void test_uload() { void put_separator() { puts("\e[37;2m=== === === === === === === ===\e[m"); } +void print_container_values_prefix(const char* name, const char* prefix) { + if (prefix) { + printf("\e[36m%s %s values:\e[m\n", name, prefix); + } else printf("\e[36m%s values:\e[m\n", name); +} + +void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type) { + if (index) { + printf("\e[32;1m%5ld: \e[m", *index); + } else fputs(" ", stdout); + + printf("\e[31m%24s\e[m", libcdsb_vtype_stringify(value, type)); + + if (print_type) { + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(type)); + } + + puts(""); +} + +void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb) { + if (!el_name) el_name = "elements"; + + if (type) { + printf("\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", name, libcdsb_vtype_name(*type)); + } + + if (size >= 0 || nmemb >= 0) { + printf("\e[36m%s consists of \e[m", name); + } + + if (size >= 0) { + printf("\e[32m%ld\e[m \e[36m%s", size, el_name); + + if (nmemb >= 0) { + printf(" (\e[m\e[32m%ld bytes\e[m\e[36m)\e[m\n", nmemb); + } else puts("\e[m"); + + } else if (nmemb >= 0) { + printf("\e[32m%ld bytes\e[m\n", nmemb); + } +} + + void test_init(int argc, char** argv) { timer_init(&GLOBAL_TIMER); timer_start(&GLOBAL_TIMER); From aedc18c9fa8fbbef955c84b3ecb09bcbdaf4b75f Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Mon, 6 Jun 2022 22:08:10 +0300 Subject: [PATCH 23/42] Update set tests --- tests/src/set/main.c | 8 ++- tests/src/set/plug.h | 123 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 6 deletions(-) diff --git a/tests/src/set/main.c b/tests/src/set/main.c index 3ba1d93..61f3ef6 100644 --- a/tests/src/set/main.c +++ b/tests/src/set/main.c @@ -3,11 +3,9 @@ #include "plug.h" -vtype_set* vset_duplicate(const vtype_set* x) { return 0; } -int vset_compare(const vtype_set* s0, const vtype_set* s1) { return random_int8(); } -void vset_free(vtype_set* x) {} - - int main(int argc, char** argv) { test_init(argc, argv); + + vtype_set x = vset_random(32); + vset_free(&x); } diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h index 661c878..ab7abdb 100644 --- a/tests/src/set/plug.h +++ b/tests/src/set/plug.h @@ -1,7 +1,8 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ -#include "../../../include/set.h" +#include "../../../include/extra/set.h" +#include "../../../src/__internal/rbtree.h" #include "../../include/random.h" #include "../../include/test.h" @@ -21,3 +22,123 @@ int string_compare(const vtype_string* s0, const vtype_string* s1) { return rand int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); } int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } + + +static int vset_node_print(const void* v, vtype t) { + print_container_value(0, v, t, false); + return 0; +} + +static void vset_print(const vtype_set* x, const char* prefix) { + print_container_values_prefix("Set", prefix); + vset_foreach(x, vset_node_print); + put_separator(); +} + +static void vset_info(const vtype_set* x) { + print_container_info("Set", "nodes", &x->type, vset_size(x), -1); + put_separator(); +} + +static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (rbnode_is_empty(s)) return; + + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + fputs(ind, stdout); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m\n", libcdsb_vtype_stringify(vnode_peek(&s->value, t), t)); + + rbtree_print(s->left, t, x, false); + rbtree_print(s->right, t, x, true); +} + +static void vset_push_random(vtype_set* x, double pause) { + var_t _; + vtype t; + + printf("\e[s\e[36mTry to insert into set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); + + switch (random_uint8()%13) { + default: + case 0: _.b = random_boolean(); print_container_value(0, &_, t = VTYPE_BOOLEAN, 1); break; + case 1: _.u8 = random_uint8(); print_container_value(0, &_, t = VTYPE_UINT8, 1); break; + case 2: _.u16 = random_uint16(); print_container_value(0, &_, t = VTYPE_UINT16, 1); break; + case 3: _.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_UINT32, 1); break; + case 4: _.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_UINT64, 1); break; + case 5: _.u8 = random_int8(); print_container_value(0, &_, t = VTYPE_INT8, 1); break; + case 6: _.u16 = random_int16(); print_container_value(0, &_, t = VTYPE_INT16, 1); break; + case 7: _.u32 = random_int32(); print_container_value(0, &_, t = VTYPE_INT32, 1); break; + case 8: _.u64 = random_int64(); print_container_value(0, &_, t = VTYPE_INT64, 1); break; + case 9: _.f = random_float(); print_container_value(0, &_, t = VTYPE_FLOAT, 1); break; + case 10: _.d = random_double(); print_container_value(0, &_, t = VTYPE_DOUBLE, 1); break; + case 11: _.ld = random_ldouble(); print_container_value(0, &_, t = VTYPE_LDOUBLE, 1); break; + + case 12: if (sizeof(void*) == 8) { + _.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break; + } else { + _.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break; + } + } + + if (libcdsb_vset_insert(x, &_, t)) { + puts("\e[32;1mSUCCESS\e[m"); + } else { + puts("\e[31;1mFAILURE\e[m"); + } + put_separator(); + + rbtree_print(x->root, x->type, 0, 0); + put_separator(); + psleep(pause * 1000000); + fputs("\e[u\e[J", stdout); +} + +static vtype_set vset_random(unsigned int n) { + vtype_set x; + + switch (random_uint8()%12) { + default: + case 0: vset_init(&x, VTYPE_UINT8); break; + case 1: vset_init(&x, VTYPE_UINT16); break; + case 2: vset_init(&x, VTYPE_UINT32); break; + case 3: vset_init(&x, VTYPE_UINT64); break; + case 4: vset_init(&x, VTYPE_INT8); break; + case 5: vset_init(&x, VTYPE_INT16); break; + case 6: vset_init(&x, VTYPE_INT32); break; + case 7: vset_init(&x, VTYPE_INT64); break; + case 8: vset_init(&x, VTYPE_POINTER); break; + case 9: vset_init(&x, VTYPE_FLOAT); break; + case 10: vset_init(&x, VTYPE_DOUBLE); break; + case 11: vset_init(&x, VTYPE_LDOUBLE); break; + } + + if (n) { + while (--n) vset_push_random(&x, 0.95); + vset_push_random(&x, 1.95); + } + + vset_info(&x); + vset_print(&x, 0); + + return x; +} From 0fdca5e083caeb0a1a149cff1ae2a9fca67ca047 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 7 Jun 2022 21:24:17 +0300 Subject: [PATCH 24/42] Update tests --- tests/src/array/plug.h | 4 +-- tests/src/list/plug.h | 4 +-- tests/src/set/main.c | 4 ++- tests/src/set/plug.h | 77 +++++++++++++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index afd1616..a54e99f 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -80,14 +80,14 @@ static vtype_array array_random(unsigned int n) { return x; } -static int array_value_print(void* v, ssize_t i, vtype t) { +static int array_value_print(void* v, ssize_t i, vtype t, void* _) { print_container_value(&i, v, t, false); return 0; } static void array_print(const vtype_array* x, const char* prefix) { print_container_values_prefix("Array", prefix); - array_foreach(x, array_value_print); + array_foreach(x, 0, array_value_print); put_separator(); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index b700edd..d3571ed 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -60,14 +60,14 @@ static void list_push_random(vtype_list* x, vtype_bool front) { } } -static int list_node_print(void* v, ssize_t i, vtype t) { +static int list_node_print(void* v, ssize_t i, vtype t, void* _) { print_container_value(&i, v, t, 1); return 0; } static void list_print(const vtype_list* x, const char* prefix) { print_container_values_prefix("List", prefix); - list_foreach(x, list_node_print); + list_foreach(x, 0, list_node_print); put_separator(); } diff --git a/tests/src/set/main.c b/tests/src/set/main.c index 61f3ef6..d6bd747 100644 --- a/tests/src/set/main.c +++ b/tests/src/set/main.c @@ -6,6 +6,8 @@ int main(int argc, char** argv) { test_init(argc, argv); - vtype_set x = vset_random(32); + vtype_set x = vset_random(36, 0.1); + + while(vset_remove_random(&x, 0.1)) {} vset_free(&x); } diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h index ab7abdb..68265d3 100644 --- a/tests/src/set/plug.h +++ b/tests/src/set/plug.h @@ -24,18 +24,18 @@ int list_compare (const vtype_list* s0, const vtype_list* s1) { return rand int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } -static int vset_node_print(const void* v, vtype t) { +static int vset_node_print(const void* v, vtype t, void* _) { print_container_value(0, v, t, false); return 0; } -static void vset_print(const vtype_set* x, const char* prefix) { +static void vset_print(const set_t* x, const char* prefix) { print_container_values_prefix("Set", prefix); - vset_foreach(x, vset_node_print); + vset_foreach(x, 0, vset_node_print); put_separator(); } -static void vset_info(const vtype_set* x) { +static void vset_info(const set_t* x) { print_container_info("Set", "nodes", &x->type, vset_size(x), -1); put_separator(); } @@ -51,7 +51,6 @@ static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { if (rbnode_is_empty(s)) return; - memcpy(x, ind, n); memcpy(x + n, " \0 ", 9); fputs(ind, stdout); @@ -72,7 +71,7 @@ static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { rbtree_print(s->right, t, x, true); } -static void vset_push_random(vtype_set* x, double pause) { +static void vset_push_random(set_t* x, double pause) { var_t _; vtype t; @@ -113,8 +112,63 @@ static void vset_push_random(vtype_set* x, double pause) { fputs("\e[u\e[J", stdout); } -static vtype_set vset_random(unsigned int n) { - vtype_set x; +static int vset_node_remove_random(const void* v, vtype t, void* data) { + struct { + size_t n; + set_t* set; + double pause; + } *d = data; + + if (!d->n--) { + print_container_value(0, v, t, 1); + + if (libcdsb_vset_find(0, d->set, v, t, 1)) { + puts("\e[32;1mSUCCESS\e[m"); + } else { + puts("\e[31;1mFAILURE\e[m"); + abort(); + } + + put_separator(); + + rbtree_print(d->set->root, d->set->type, 0, 0); + put_separator(); + psleep(d->pause * 1000000); + + return true; + } + return 0; +} + + +static bool vset_remove_random(set_t* x, double pause) { + struct { + size_t n; + set_t* set; + double pause; + } d; + d.n = vset_size(x); + d.set = x; + d.pause = pause; + + printf("\e[s\e[36mTry to remove value from set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); + + if (d.n) { + d.n = random_uint32()%d.n; + } else goto end_; + + if (!vset_foreach(x, &d, vset_node_remove_random)) { + end_: + puts("\e[u\e[J\e[32;1mSet is empty\e[m"); + return false; + } + + fputs("\e[u\e[J", stdout); + return true; +} + +static set_t vset_random(unsigned int n, double pause) { + set_t x; switch (random_uint8()%12) { default: @@ -133,12 +187,9 @@ static vtype_set vset_random(unsigned int n) { } if (n) { - while (--n) vset_push_random(&x, 0.95); - vset_push_random(&x, 1.95); + while (--n) vset_push_random(&x, pause); + vset_push_random(&x, pause); } - vset_info(&x); - vset_print(&x, 0); - return x; } From 48ed3d3fab10863cac0311f17c24f70c88cc40af Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Wed, 8 Jun 2022 10:52:29 +0300 Subject: [PATCH 25/42] Update tests --- tests/src/array/plug.h | 2 +- tests/src/map/main.c | 10 +-- tests/src/map/plug.h | 199 ++++++++++++++++++++++++++++++++++++++++- tests/src/set/main.c | 2 +- tests/src/set/plug.h | 2 +- 5 files changed, 206 insertions(+), 9 deletions(-) diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index a54e99f..dd8ed86 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -85,7 +85,7 @@ static int array_value_print(void* v, ssize_t i, vtype t, void* _) { return 0; } -static void array_print(const vtype_array* x, const char* prefix) { +static void array_print(vtype_array* x, const char* prefix) { print_container_values_prefix("Array", prefix); array_foreach(x, 0, array_value_print); put_separator(); diff --git a/tests/src/map/main.c b/tests/src/map/main.c index a0638f1..f08ef64 100644 --- a/tests/src/map/main.c +++ b/tests/src/map/main.c @@ -3,11 +3,11 @@ #include "plug.h" -vtype_map* map_duplicate(const vtype_map* x) { return 0; } -int map_compare(const vtype_map* s0, const vtype_map* s1) { return random_int8(); } -void map_free(vtype_map* x) {} - - int main(int argc, char** argv) { test_init(argc, argv); + + map_t x = map_random(36, 0.1); + + while(map_remove_random(&x, 0.1)) {} + map_free(&x); } diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h index 093107c..2ea8ee7 100644 --- a/tests/src/map/plug.h +++ b/tests/src/map/plug.h @@ -1,7 +1,7 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ -#include "../../../include/map.h" +#include "../../../src/map/include.h" #include "../../include/random.h" #include "../../include/test.h" @@ -21,3 +21,200 @@ int string_compare(const vtype_string* s0, const vtype_string* s1) { return rand 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(); } + + +static int map_node_print(const void* k, vtype kt, void* v, vtype vt, void* _) { + printf(" \e[31m%24s\e[m \e[36m:\e[m", vtype_stringify(k, kt)); + printf(" \e[31m%s\e[m", vtype_stringify(v, vt)); + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(vt)); + return 0; +} + +static void map_print(map_t* x, const char* prefix) { + print_container_values_prefix("Map", prefix); + map_foreach(x, 0, map_node_print); + put_separator(); +} + +static void map_info(const map_t* x) { + print_container_info("Map", "nodes", &x->type, map_size(x), -1); + put_separator(); +} + +static void map_rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (mnode_is_empty(s)) return; + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + fputs(ind, stdout); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); + printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); + + map_rbtree_print(s->left, t, x, false); + map_rbtree_print(s->right, t, x, true); +} + +static void map_push_random(map_t* x, double pause) { + var_t _[2]; + vtype t[2]; + + printf("\e[s\e[36mUpdate value in map (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", vtype_name(x->type)); + + switch (random_uint8()%13) { + default: + case 0: _[0].b = random_boolean(); print_container_value(0, _, t[0] = VTYPE_BOOLEAN, 1); break; + case 1: _[0].u8 = random_uint8(); print_container_value(0, _, t[0] = VTYPE_UINT8, 1); break; + case 2: _[0].u16 = random_uint16(); print_container_value(0, _, t[0] = VTYPE_UINT16, 1); break; + case 3: _[0].u32 = random_uint32(); print_container_value(0, _, t[0] = VTYPE_UINT32, 1); break; + case 4: _[0].u64 = random_uint64(); print_container_value(0, _, t[0] = VTYPE_UINT64, 1); break; + case 5: _[0].u8 = random_int8(); print_container_value(0, _, t[0] = VTYPE_INT8, 1); break; + case 6: _[0].u16 = random_int16(); print_container_value(0, _, t[0] = VTYPE_INT16, 1); break; + case 7: _[0].u32 = random_int32(); print_container_value(0, _, t[0] = VTYPE_INT32, 1); break; + case 8: _[0].u64 = random_int64(); print_container_value(0, _, t[0] = VTYPE_INT64, 1); break; + case 9: _[0].f = random_float(); print_container_value(0, _, t[0] = VTYPE_FLOAT, 1); break; + case 10: _[0].d = random_double(); print_container_value(0, _, t[0] = VTYPE_DOUBLE, 1); break; + case 11: _[0].ld = random_ldouble(); print_container_value(0, _, t[0] = VTYPE_LDOUBLE, 1); break; + + case 12: if (sizeof(void*) == 8) { + _[0].u64 = random_uint64(); print_container_value(0, _, t[0] = VTYPE_POINTER, 1); break; + } else { + _[0].u32 = random_uint32(); print_container_value(0, _, t[0] = VTYPE_POINTER, 1); break; + } + } + + switch (random_uint8()%13) { + default: + case 0: _[1].b = random_boolean(); print_container_value(0, _+1, t[1] = VTYPE_BOOLEAN, 1); break; + case 1: _[1].u8 = random_uint8(); print_container_value(0, _+1, t[1] = VTYPE_UINT8, 1); break; + case 2: _[1].u16 = random_uint16(); print_container_value(0, _+1, t[1] = VTYPE_UINT16, 1); break; + case 3: _[1].u32 = random_uint32(); print_container_value(0, _+1, t[1] = VTYPE_UINT32, 1); break; + case 4: _[1].u64 = random_uint64(); print_container_value(0, _+1, t[1] = VTYPE_UINT64, 1); break; + case 5: _[1].u8 = random_int8(); print_container_value(0, _+1, t[1] = VTYPE_INT8, 1); break; + case 6: _[1].u16 = random_int16(); print_container_value(0, _+1, t[1] = VTYPE_INT16, 1); break; + case 7: _[1].u32 = random_int32(); print_container_value(0, _+1, t[1] = VTYPE_INT32, 1); break; + case 8: _[1].u64 = random_int64(); print_container_value(0, _+1, t[1] = VTYPE_INT64, 1); break; + case 9: _[1].f = random_float(); print_container_value(0, _+1, t[1] = VTYPE_FLOAT, 1); break; + case 10: _[1].d = random_double(); print_container_value(0, _+1, t[1] = VTYPE_DOUBLE, 1); break; + case 11: _[1].ld = random_ldouble(); print_container_value(0, _+1, t[1] = VTYPE_LDOUBLE, 1); break; + + case 12: if (sizeof(void*) == 8) { + _[1].u64 = random_uint64(); print_container_value(0, _+1, t[1] = VTYPE_POINTER, 1); break; + } else { + _[1].u32 = random_uint32(); print_container_value(0, _+1, t[1] = VTYPE_POINTER, 1); break; + } + } + + if (libcdsb_map_update(x, _, t[0], _+1, t[1])) { + puts("\e[33;1mCHANGE\e[m"); + } else { + puts("\e[32;1mINSERT\e[m"); + } + + put_separator(); + + map_rbtree_print(x->root, x->type, 0, 0); + put_separator(); + psleep(pause * 1000000); + fputs("\e[u\e[J", stdout); +} + +static int map_node_remove_random(const void* k, vtype kt, void* v, vtype vt, void* data) { + struct { + size_t n; + map_t* set; + double pause; + } *d = data; + + if (!d->n--) { + map_node_print(k, kt, v, vt, 0); + + if (libcdsb_map_find(0, d->set, k, kt, 1)) { + puts("\e[32;1mSUCCESS\e[m"); + } else { + puts("\e[s\e[31;1mFAILURE\e[m"); + abort(); + } + + put_separator(); + + map_rbtree_print(d->set->root, d->set->type, 0, 0); + put_separator(); + psleep(d->pause * 1000000); + + return true; + } + return 0; +} + + +static bool map_remove_random(map_t* x, double pause) { + struct { + size_t n; + map_t* set; + double pause; + } d; + d.n = map_size(x); + d.set = x; + d.pause = pause; + + printf("\e[s\e[36mTry to remove value from map (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); + + if (d.n) { + d.n = random_uint32()%d.n; + } else goto end_; + + if (!map_foreach(x, &d, map_node_remove_random)) { + end_: + puts("\e[u\e[J\e[32;1mMap is empty\e[m"); + return false; + } + + fputs("\e[u\e[J", stdout); + return true; +} + +static map_t map_random(unsigned int n, double pause) { + map_t x; + + switch (random_uint8()%12) { + default: + case 0: map_init(&x, VTYPE_UINT8); break; + case 1: map_init(&x, VTYPE_UINT16); break; + case 2: map_init(&x, VTYPE_UINT32); break; + case 3: map_init(&x, VTYPE_UINT64); break; + case 4: map_init(&x, VTYPE_INT8); break; + case 5: map_init(&x, VTYPE_INT16); break; + case 6: map_init(&x, VTYPE_INT32); break; + case 7: map_init(&x, VTYPE_INT64); break; + case 8: map_init(&x, VTYPE_POINTER); break; + case 9: map_init(&x, VTYPE_FLOAT); break; + case 10: map_init(&x, VTYPE_DOUBLE); break; + case 11: map_init(&x, VTYPE_LDOUBLE); break; + } + + if (n) { + while (--n) map_push_random(&x, pause); + map_push_random(&x, pause); + } + + return x; +} diff --git a/tests/src/set/main.c b/tests/src/set/main.c index d6bd747..be56d43 100644 --- a/tests/src/set/main.c +++ b/tests/src/set/main.c @@ -6,7 +6,7 @@ int main(int argc, char** argv) { test_init(argc, argv); - vtype_set x = vset_random(36, 0.1); + set_t x = vset_random(36, 0.1); while(vset_remove_random(&x, 0.1)) {} vset_free(&x); diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h index 68265d3..fd4e092 100644 --- a/tests/src/set/plug.h +++ b/tests/src/set/plug.h @@ -29,7 +29,7 @@ static int vset_node_print(const void* v, vtype t, void* _) { return 0; } -static void vset_print(const set_t* x, const char* prefix) { +static void vset_print(set_t* x, const char* prefix) { print_container_values_prefix("Set", prefix); vset_foreach(x, 0, vset_node_print); put_separator(); From c6cc57575999a4095437f1cdf7f8bc81a0b50e84 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 9 Jun 2022 11:39:19 +0300 Subject: [PATCH 26/42] Update tests base --- tests/Makefile | 17 ++++++++++ tests/include/random.h | 9 +++++ tests/include/test.h | 8 ++--- tests/src/random.c | 24 ++++++++++++- tests/src/test.c | 77 ++++++++++++++++++++++++++---------------- 5 files changed, 101 insertions(+), 34 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index d810ace..f9a6205 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -35,6 +35,12 @@ OBJECTS_LIST := $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS_TESTS)) $(addprefix . 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_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-)) + ######################################################################################################################## tests: modules @@ -50,6 +56,17 @@ tests: $(addprefix $(BUILD_PATH)/set-,$(notdir $(basename $(wildcard ./src/set/* $(BUILD_PATH)/obj/%.o: ./src/%.c | $(BUILD_PATH)/obj/ $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/obj/array-%.o: ./src/array/src/%.c | $(BUILD_PATH)/obj/ + $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/obj/string-%.o: ./src/string/src/%.c | $(BUILD_PATH)/obj/ + $(CC) $^ -o $@ $(CFLAGS) +$(BUILD_PATH)/obj/list-%.o: ./src/list/src/%.c | $(BUILD_PATH)/obj/ + $(CC) $^ -o $@ $(CFLAGS) +$(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)/array-%: ./src/array/%.c $(OBJECTS_ARRAY) | $(BUILD_PATH)/ $(CC) $^ -o $@ $(CFLAGS) -g3 -Wall $(BUILD_PATH)/string-%: ./src/string/%.c $(OBJECTS_STRING) | $(BUILD_PATH)/ diff --git a/tests/include/random.h b/tests/include/random.h index 69b2b07..517c27b 100644 --- a/tests/include/random.h +++ b/tests/include/random.h @@ -2,10 +2,16 @@ /* Copyright © 2022 Gregory Lirent */ #include "../../include/vtype.h" +#include "../../src/__internal/vnode.h" #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(); @@ -25,4 +31,7 @@ extern vtype_int64 random_int64(); extern char random_ascii_char(); extern unsigned int random_unicode_symbol(); + +extern value_t random_value(); + #endif /* LIBCDSB_TESTS_RANDOM_H */ diff --git a/tests/include/test.h b/tests/include/test.h index 2b689ce..ac3d5e9 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -7,10 +7,10 @@ #ifndef LIBCDSB_TESTS_TEST_H #define LIBCDSB_TESTS_TEST_H -extern void put_separator(); -extern void print_container_values_prefix(const char* name, const char* prefix); -extern void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type); -extern void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb); +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_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); diff --git a/tests/src/random.c b/tests/src/random.c index c77e74a..337932c 100644 --- a/tests/src/random.c +++ b/tests/src/random.c @@ -11,7 +11,7 @@ static void init(unsigned int seed) { if (!seed) seed = time(0); printf("\e[36mRandom module initialized with seed: \e[m\e[32m%u\n\e[m", seed); - put_separator(); + put_separator(0); srand(seed); } @@ -109,3 +109,25 @@ unsigned int random_unicode_symbol() { case 19: return (random_uint16()%0x03d8) + 0x01f300; } } + +value_t random_value() { + value_t v; + switch (random_uint8()%13) { + 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; + } + + return v; +} diff --git a/tests/src/test.c b/tests/src/test.c index 1d570cf..32e11e4 100644 --- a/tests/src/test.c +++ b/tests/src/test.c @@ -1,6 +1,7 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ +#include #include #include "../include/test.h" #include "../include/time.h" @@ -15,25 +16,59 @@ static void test_uload() { if (TEST_NAME) { timer_stop(&GLOBAL_TIMER); puts(""); - put_separator(); + put_separator(0); printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is end.\nExecusion time: \e[m\e[32m%.6Lf sec\e[m\n", TEST_NAME, timer_value(&GLOBAL_TIMER)); - put_separator(); + put_separator(0); timer_free(&GLOBAL_TIMER); } } -void put_separator() { puts("\e[37;2m=== === === === === === === ===\e[m"); } +void test_init(int argc, char** argv) { + timer_init(&GLOBAL_TIMER); + timer_start(&GLOBAL_TIMER); -void print_container_values_prefix(const char* name, const char* prefix) { - if (prefix) { - printf("\e[36m%s %s values:\e[m\n", name, prefix); - } else printf("\e[36m%s values:\e[m\n", name); + TEST_NAME = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]; + + put_separator(0); + printf("\e[H\e[J\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME); + put_separator(0); + + random_init(argc, argv); + + puts(""); } -void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type) { +void hp_printf(unsigned int hpos, const char* format, ...) { + va_list args; + + va_start(args, format); + vprintf(format, args); + + printf("\e[%dG", hpos+1); + + vprintf(format, args); + va_end(args); +} + + +void put_separator(unsigned int hpos) { + printf("\e[%dG\e[37;2m=== === === === === === === ===\e[m\n", hpos+1); +} + +void print_container_values_prefix(const char* name, const char* prefix, unsigned int hpos) { + if (prefix) { + printf("\e[%dG\e[36m%s %s values:\e[m\n", hpos+1, name, prefix); + } else { + printf("\e[%dG\e[36m%s values:\e[m\n", hpos+1, name); + } +} + +void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type, unsigned int hpos) { if (index) { - printf("\e[32;1m%5ld: \e[m", *index); - } else fputs(" ", stdout); + 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_vtype_stringify(value, type)); @@ -44,15 +79,15 @@ void print_container_value(const ssize_t* index, const void* value, const vtype puts(""); } -void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb) { +void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb, unsigned int hpos) { if (!el_name) el_name = "elements"; if (type) { - printf("\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", name, libcdsb_vtype_name(*type)); + printf("\e[%dG\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", hpos+1, name, libcdsb_vtype_name(*type)); } if (size >= 0 || nmemb >= 0) { - printf("\e[36m%s consists of \e[m", name); + printf("\e[%dG\e[36m%s consists of \e[m", hpos+1, name); } if (size >= 0) { @@ -66,19 +101,3 @@ void print_container_info(const char* name, const char* el_name, const vtype* ty printf("\e[32m%ld bytes\e[m\n", nmemb); } } - - -void test_init(int argc, char** argv) { - timer_init(&GLOBAL_TIMER); - timer_start(&GLOBAL_TIMER); - - TEST_NAME = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]; - - put_separator(); - printf("\e[H\e[J\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME); - put_separator(); - - random_init(argc, argv); - - puts(""); -} From 760c04fb498850a1e25666d8c9c70d287dc67354 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 9 Jun 2022 11:39:37 +0300 Subject: [PATCH 27/42] Update list tests --- tests/src/list/main.c | 25 +++-- tests/src/list/plug.h | 74 +++----------- tests/src/list/remove.c | 32 ------ tests/src/list/src/io.c | 188 ++++++++++++++++++++++++++++++++++++ tests/src/list/src/plug.c | 20 ++++ tests/src/list/src/random.c | 83 ++++++++++++++++ 6 files changed, 314 insertions(+), 108 deletions(-) delete mode 100644 tests/src/list/remove.c create mode 100644 tests/src/list/src/io.c create mode 100644 tests/src/list/src/plug.c create mode 100644 tests/src/list/src/random.c diff --git a/tests/src/list/main.c b/tests/src/list/main.c index 529a266..cfee210 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -6,19 +6,18 @@ int main(int argc, char** argv) { test_init(argc, argv); - vtype_list x; - - list_init(&x); - - for (int i = 0, n = 12 + random_boolean(); i < n; ++i) { - list_push_random(&x, random_boolean()); - } - list_sort(&x); - list_info(&x); - list_print(&x, "sorted"); - - list_reverse(&x); - list_print(&x, "reversed"); + list_t x = { .first = 0, .last = 0 }; + list_t y = { .first = 0, .last = 0 }; list_free(&x); + + visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12); + + visual_extend(&x, &y); + + visual_sort2(&x, &y); + visual_remove2(&x, &y); + + list_free(&y); + list_free(&y); } diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index d3571ed..7362e9d 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -9,69 +9,17 @@ #include "../../../src/__internal/vnode.h" -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; } +extern void list_print(list_t* x, const char* prefix, unsigned int hpos); +extern void list_info (list_t* x, unsigned int hpos); -void string_free(vtype_string* x) {} -void array_free (vtype_array* x) {} -void map_free (vtype_map* x) {} -void vset_free (vtype_set* x) {} +extern void list_push_random(list_t* x, _Bool silent, unsigned int hpos); +extern void list_remove_random(list_t* x, _Bool silent, unsigned int hpos); -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(); } +extern void visual_push(list_t* x, size_t n); +extern void visual_sort(list_t* x); +extern void visual_remove(list_t* x); - -static void list_push_random(vtype_list* x, vtype_bool front) { - - if (!front) switch (random_uint8()%13) { - default: - case 0: list_push_back(x, random_boolean()); break; - case 1: list_push_back(x, random_uint8()); break; - case 2: list_push_back(x, random_uint16()); break; - case 3: list_push_back(x, random_uint32()); break; - case 4: list_push_back(x, random_uint64()); break; - case 5: list_push_back(x, random_int8()); break; - case 6: list_push_back(x, random_int16()); break; - case 7: list_push_back(x, random_int32()); break; - case 8: list_push_back(x, random_int64()); break; - case 9: list_push_back(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - case 10: list_push_back(x, random_float()); break; - case 11: list_push_back(x, random_double()); break; - case 12: list_push_back(x, random_ldouble()); break; - } else switch (random_uint8()%13) { - default: - case 0: list_push_front(x, random_boolean()); break; - case 1: list_push_front(x, random_uint8()); break; - case 2: list_push_front(x, random_uint16()); break; - case 3: list_push_front(x, random_uint32()); break; - case 4: list_push_front(x, random_uint64()); break; - case 5: list_push_front(x, random_int8()); break; - case 6: list_push_front(x, random_int16()); break; - case 7: list_push_front(x, random_int32()); break; - case 8: list_push_front(x, random_int64()); break; - case 9: list_push_front(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - case 10: list_push_front(x, random_float()); break; - case 11: list_push_front(x, random_double()); break; - case 12: list_push_front(x, random_ldouble()); break; - } -} - -static int list_node_print(void* v, ssize_t i, vtype t, void* _) { - print_container_value(&i, v, t, 1); - return 0; -} - -static void list_print(const vtype_list* x, const char* prefix) { - print_container_values_prefix("List", prefix); - list_foreach(x, 0, list_node_print); - put_separator(); -} - -static void list_info(const vtype_list* x) { - print_container_info("List", "nodes", 0, list_size(x), -1); - put_separator(); -} +extern void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1); +extern void visual_remove2(list_t* x0, list_t* x1); +extern void visual_sort2(list_t* x0, list_t* x1); +extern void visual_extend(list_t* x, list_t* s); diff --git a/tests/src/list/remove.c b/tests/src/list/remove.c deleted file mode 100644 index b3c7039..0000000 --- a/tests/src/list/remove.c +++ /dev/null @@ -1,32 +0,0 @@ -/* 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); - - vtype_list x; - - list_init(&x); - - for (int i = 0, c = random_uint8()%12; i < 12; ++i) { - if (i == c) { - list_push_back(&x, 0); - } else list_push_random(&x, random_boolean()); - } - - list_info(&x); - list_print(&x, 0); - - for (int i = 0, n = list_countof(&x, 0); i < n; ++i) { - int index = list_indexof(&x, 0); - list_remove(&x, 0); - - printf("\e[36mRemove element with index \e[m\e[32;1m%d\e[m\e[36m from list\e[m\n", index); - } - put_separator(); - list_print(&x, "cleaned"); - - list_free(&x); -} diff --git a/tests/src/list/src/io.c b/tests/src/list/src/io.c new file mode 100644 index 0000000..5ed0ed0 --- /dev/null +++ b/tests/src/list/src/io.c @@ -0,0 +1,188 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int list_node_print(void* v, ssize_t i, vtype t, void* _) { + print_container_value(&i, v, t, 1, *(unsigned int*)_); + return 0; +} + +void list_print(list_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("List", prefix, hpos); + list_foreach(x, &hpos, list_node_print); +} + +void list_info(list_t* x, unsigned int hpos) { + print_container_info("List", "nodes", 0, list_size(x), -1, hpos); +} + + +void visual_push(list_t* x, size_t n) { + for (int i = 0; i < n; ++i) { + fputs("\e[s", stdout); + + list_push_random(x, 0, 0); + + list_info(x, 0); + list_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_sort(list_t* x) { + puts("\e[s\e[36mTry to sort list values:\e[m\n\n"); + put_separator(0); + list_sort(x); + list_info(x, 0); + list_print(x, "sorted", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); + + puts("\e[s\e[36mTry to reverse list values:\e[m\n\n"); + put_separator(0); + list_reverse(x); + list_info(x, 0); + list_print(x, "reversed", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_remove(list_t* x) { + while (x->first) { + fputs("\e[s", stdout); + list_remove_random(x, 0, 0); + list_info(x, 0); + list_print(x, 0, 0); + + psleep(100000); + if (!0) fputs("\e[u\e[J", stdout); + } +} + +void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1) { + + for (;n0 || n1;) { + fputs("\e[s", stdout); + if (n0) { + list_push_random(x0, 0, 0); + --n0; + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (n1) { + list_push_random(x1, 0, 60); + --n1; + } else { + puts("\n\n"); + put_separator(60); + } + + list_info(x0, 0); + list_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove2(list_t* x0, list_t* x1) { + + for (;x0->first || x1->first;) { + fputs("\e[s", stdout); + + if (x0->first) { + list_remove_random(x0, 0, 0); + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (x1->first) { + list_remove_random(x1, 0, 60); + } else { + puts("\n\n"); + put_separator(60); + } + + list_info(x0, 0); + list_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + + +void visual_sort2(list_t* x0, list_t* x1) { + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to sort list values:", "Try to sort list values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_sort(x0); + list_sort(x1); + + list_info(x0, 0); + list_print(x0, "sorted", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, "sorted", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); + + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to reverse list values:", "Try to reverse list values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_reverse(x0); + list_reverse(x1); + + list_info(x0, 0); + list_print(x0, "reversed", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(x1, 60); + list_print(x1, "reversed", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_extend(list_t* x, list_t* s) { + + puts("\e[s\e[36mTry to extend list:\e[m\n\n"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_info(x, 0); + list_print(x, "(dest)", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(s, 60); + list_print(s, "(src)", 60); + + psleep(900000); + fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout); + + list_extend(x, s); + + puts(""); + put_separator(0); + + list_info(x, 0); + list_print(x, "(dest)", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); +} diff --git a/tests/src/list/src/plug.c b/tests/src/list/src/plug.c new file mode 100644 index 0000000..eff34f8 --- /dev/null +++ b/tests/src/list/src/plug.c @@ -0,0 +1,20 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../../src/__internal/include.h" +#include "../../../include/random.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_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 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 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(); } diff --git a/tests/src/list/src/random.c b/tests/src/list/src/random.c new file mode 100644 index 0000000..9d74048 --- /dev/null +++ b/tests/src/list/src/random.c @@ -0,0 +1,83 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int remove_callback(void* v, ssize_t i, vtype t, void* _) { + struct { list_t* x; _Bool s; unsigned int p; } *x = _; + if (!x->s) { + print_container_value(0, v, t, 1, x->p); + } + + if (libcdsb_list_find(x->x, v, t, 0, 0, 1, 1)) { + return -2; + } + + return 0; +} + + +void list_push_random(list_t* x, _Bool silent, unsigned int hpos) { + + value_t v = random_value(); + _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_update(x, -1, v.value, v.type, 1); + } 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_update(x, -1, v.value, v.type, 1); + } else { + ssize_t i = list_size(x); + i = random_uint64()% (i ? i : 1); + if (random_boolean()) i = ~i + 1; + + if (!silent) { + printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m into list:\e[m\n", hpos+1, i); + } + + r = libcdsb_list_update(x, i, v.value, v.type, 0); + } + + if (!silent) { + print_container_value(0, v.value, v.type, 1, hpos); + printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m"); + put_separator(hpos); + } +} + +void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) { + + size_t n = list_size(x); + ssize_t i = random_uint64()%n; + + if (random_boolean()) i = ~i + 1; + + 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); + } + switch (list_remove_by_index(x, i)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } else { + struct { list_t* x; _Bool s; unsigned int p; } v = { .x = x, .s = silent, .p = hpos }; + + if (!silent) printf("\e[%dG\e[36mTry to remove value from list:\e[m\n", hpos+1); + + switch (libcdsb_list_get(x, i, &v, remove_callback, 0)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + case 2: if (!silent) printf("\n\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } + + put_separator(hpos); +} From f804d4277cf633d90e123b8b107ece285d46c950 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 9 Jun 2022 22:02:12 +0300 Subject: [PATCH 28/42] Update list tests --- tests/src/list/main.c | 3 --- tests/src/list/src/random.c | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/src/list/main.c b/tests/src/list/main.c index cfee210..9cdcbcd 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -9,10 +9,7 @@ int main(int argc, char** argv) { list_t x = { .first = 0, .last = 0 }; list_t y = { .first = 0, .last = 0 }; - list_free(&x); - visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12); - visual_extend(&x, &y); visual_sort2(&x, &y); diff --git a/tests/src/list/src/random.c b/tests/src/list/src/random.c index 9d74048..2552978 100644 --- a/tests/src/list/src/random.c +++ b/tests/src/list/src/random.c @@ -38,7 +38,7 @@ void list_push_random(list_t* x, _Bool silent, unsigned int hpos) { if (random_boolean()) i = ~i + 1; if (!silent) { - printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m into 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_update(x, i, v.value, v.type, 0); @@ -74,7 +74,6 @@ void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) { switch (libcdsb_list_get(x, i, &v, remove_callback, 0)) { case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; - case 2: if (!silent) printf("\n\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; } } From 26a33062e7ed0c504846fb3043ba8b3703732510 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 9 Jun 2022 22:02:38 +0300 Subject: [PATCH 29/42] Update array tests --- tests/src/array/change.c | 29 ------ tests/src/array/main.c | 33 ++---- tests/src/array/plug.h | 30 +++--- tests/src/array/src/io.c | 197 +++++++++++++++++++++++++++++++++++ tests/src/array/src/plug.c | 25 +++++ tests/src/array/src/random.c | 83 +++++++++++++++ 6 files changed, 327 insertions(+), 70 deletions(-) delete mode 100644 tests/src/array/change.c create mode 100644 tests/src/array/src/io.c create mode 100644 tests/src/array/src/plug.c create mode 100644 tests/src/array/src/random.c diff --git a/tests/src/array/change.c b/tests/src/array/change.c deleted file mode 100644 index 989193a..0000000 --- a/tests/src/array/change.c +++ /dev/null @@ -1,29 +0,0 @@ -/* 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); - - vtype_array y; - vtype_array x = array_random(8); - - array_info(&x); - array_sort(&x); - array_print(&x, 0); - - array_slice(&y, &x, 2, 4, 1); - - array_print(&x, "source"); - array_print(&y, "cutted"); - - array_remove(&x, -1); - array_remove(&y, -1); - - array_print(&x, "source (last removed)"); - array_print(&y, "cutted (last removed)"); - - array_free(&x); - array_free(&y); -} diff --git a/tests/src/array/main.c b/tests/src/array/main.c index a8b5ab4..18b31fd 100644 --- a/tests/src/array/main.c +++ b/tests/src/array/main.c @@ -3,36 +3,21 @@ #include "plug.h" -static void array_find_minimal(const vtype_array* x) { - array_print(x, 0); - for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) { - ssize_t index = array_indexof(x, v); - if (index >= 0) { - printf("\e[36mValue \e[m\e[31m%lu\e[m\e[36m was found at the position \e[m\e[32;1m%ld\e[m\n", v, index); - break; - } - } - put_separator(); -} int main(int argc, char** argv) { test_init(argc, argv); - vtype_array x = { .mem = 0 }; + arr_t x = { .mem = 0, .size = 0, .type = random_uint8()%(VTYPE_LDOUBLE + 1) }; + arr_t y = { .mem = 0, .size = 0, .type = x.type }; - do { - array_free(&x); - x = array_random(8); - } while (x.type >= VTYPE_FLOAT); + visual_push(&x, (random_uint8()%17) + 16); - array_info(&x); - array_find_minimal(&x); + visual_slice(&x, &y); + visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12); - array_reverse(&x); - array_print(&x, "reversed"); + visual_sort2(&x, &y); + visual_remove2(&x, &y); - array_sort(&x); - array_print(&x, "sorted"); - - array_free(&x); + array_free(&y); + array_free(&y); } diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index dd8ed86..254611a 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -9,27 +9,22 @@ #include "../../include/test.h" #include "../../include/time.h" -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; } -void string_free(vtype_string* x) {} -void list_free (vtype_list* x) {} -void map_free (vtype_map* x) {} -void vset_free (vtype_set* x) {} +extern void array_print(arr_t* x, const char* prefix, unsigned int hpos); +extern void array_info(arr_t* x, unsigned int hpos); -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(); } - -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)); } +extern void visual_push(arr_t* x, size_t n); +extern void visual_sort(arr_t* x); +extern void visual_remove(arr_t* x); +extern void visual_push2(arr_t* x0, size_t n0, arr_t* x1, size_t n1); +extern void visual_remove2(arr_t* x0, arr_t* x1); +extern void visual_sort2(arr_t* x0, arr_t* x1); +extern void visual_slice(arr_t* x, arr_t* s); +extern void array_push_random (arr_t* x, _Bool silent, unsigned int hpos); +extern void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos); +/* static void array_push_random(vtype_array* x) { switch (random_uint8()%13) { default: @@ -95,3 +90,4 @@ static void array_info(const vtype_array* x) { print_container_info("Array", 0, &x->type, array_size(x), array_nmemb(x)); put_separator(); } +*/ diff --git a/tests/src/array/src/io.c b/tests/src/array/src/io.c new file mode 100644 index 0000000..127eaec --- /dev/null +++ b/tests/src/array/src/io.c @@ -0,0 +1,197 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int array_value_print(void* v, ssize_t i, vtype t, void* _) { + print_container_value(&i, v, t, 1, *(unsigned int*)_); + return 0; +} + +void array_print(arr_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Array", prefix, hpos); + array_foreach(x, &hpos, array_value_print); +} + +void array_info(arr_t* x, unsigned int hpos) { + print_container_info("Array", "nodes", 0, array_size(x), -1, hpos); +} + + +void visual_push(arr_t* x, size_t n) { + for (int i = 0; i < n; ++i) { + fputs("\e[s", stdout); + + array_push_random(x, 0, 0); + + array_info(x, 0); + array_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_sort(arr_t* x) { + puts("\e[s\e[36mTry to sort array values:\e[m\n\n"); + put_separator(0); + array_sort(x); + array_info(x, 0); + array_print(x, "sorted", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); + + puts("\e[s\e[36mTry to reverse array values:\e[m\n\n"); + put_separator(0); + array_reverse(x); + array_info(x, 0); + array_print(x, "reversed", 0); + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_remove(arr_t* x) { + while (x->size) { + fputs("\e[s", stdout); + array_remove_random(x, 0, 0); + array_info(x, 0); + array_print(x, 0, 0); + + psleep(100000); + if (!0) fputs("\e[u\e[J", stdout); + } +} + +void visual_push2(arr_t* x0, size_t n0, arr_t* x1, size_t n1) { + + for (;n0 || n1;) { + fputs("\e[s", stdout); + if (n0) { + array_push_random(x0, 0, 0); + --n0; + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (n1) { + array_push_random(x1, 0, 60); + --n1; + } else { + puts("\n\n"); + put_separator(60); + } + + array_info(x0, 0); + array_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(x1, 60); + array_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove2(arr_t* x0, arr_t* x1) { + + for (;x0->size || x1->size;) { + fputs("\e[s", stdout); + + if (x0->size) { + array_remove_random(x0, 0, 0); + } else { + puts("\n\n"); + put_separator(0); + } + + fputs("\e[u\e[s", stdout); + + if (x1->size) { + array_remove_random(x1, 0, 60); + } else { + puts("\n\n"); + put_separator(60); + } + + array_info(x0, 0); + array_print(x0, 0, 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(x1, 60); + array_print(x1, 0, 60); + psleep(100000); + + fputs("\e[u\e[J", stdout); + } +} + + +void visual_sort2(arr_t* x0, arr_t* x1) { + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to sort array values:", "Try to sort array values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + array_sort(x0); + array_sort(x1); + + array_info(x0, 0); + array_print(x0, "sorted", 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(x1, 60); + array_print(x1, "sorted", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); + + printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to reverse array values:", "Try to reverse array values:"); + printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + array_reverse(x0); + array_reverse(x1); + + array_info(x0, 0); + array_print(x0, "reversed", 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(x1, 60); + array_print(x1, "reversed", 60); + + psleep(900000); + fputs("\e[u\e[J", stdout); +} + +void visual_slice(arr_t* x, arr_t* s) { + + size_t n = array_size(x); + ssize_t i = random_uint64()%(n - 1); + + n = n - (i + 1); + + puts("\e[s\e[36mTry to slice array:\e[m\n\n"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + array_info(x, 0); + array_print(x, "(src)", 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(s, 60); + array_print(s, "(dest)", 60); + + psleep(900000); + + array_slice(s, x, i, n, 1); + fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout); + + puts(""); + put_separator(0); + + array_info(x, 0); + array_print(x, "(src)", 0); + fputs("\e[u\e[s\e[4E", stdout); + array_info(s, 60); + array_print(s, "(dest)", 60); + psleep(900000); + + fputs("\e[u\e[J", stdout); +} diff --git a/tests/src/array/src/plug.c b/tests/src/array/src/plug.c new file mode 100644 index 0000000..30511e8 --- /dev/null +++ b/tests/src/array/src/plug.c @@ -0,0 +1,25 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../../../../include/extra/vtype.h" + +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; } + +void string_free(vtype_string* 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) { random_int8(); } +int list_compare (const vtype_list* s0, const vtype_list* s1) { random_int8(); } +int map_compare (const vtype_map* s0, const vtype_map* s1) { random_int8(); } +int vset_compare (const vtype_set* s0, const vtype_set* s1) { 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)); } diff --git a/tests/src/array/src/random.c b/tests/src/array/src/random.c new file mode 100644 index 0000000..22af216 --- /dev/null +++ b/tests/src/array/src/random.c @@ -0,0 +1,83 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int remove_callback(void* v, ssize_t i, vtype t, void* _) { + struct { arr_t* x; _Bool s; unsigned int p; } *x = _; + if (!x->s) print_container_value(0, v, t, 1, x->p); + + if (libcdsb_array_find(x->x, v, t, 0, 0, 1, 1)) + return -2; + return 0; +} + +static int change_callback(void* v, ssize_t i, vtype t, void* _) { + struct { value_t v; _Bool s; unsigned int p; } *x = _; + + while (x->v.type != t) { x->v = random_value(); } + + memcpy(v, &x->v, vtype_size(t)); + 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 }; + _Bool r; + + if (random_boolean()) { + if (!silent) { + printf("\e[%dG\e[36mTry to push value to back of the array:\e[m\n", hpos+1); + } + + r = libcdsb_array_push(x, _.v.value, _.v.type) >= 0; + } else { + ssize_t i = array_size(x); + + i = random_uint64() % (i ? i : 1); + i = random_boolean() ? ~i + 1 : i ; + + if (!silent) { + printf("\e[%dG\e[36mTry to change value in the array with index \e[32;1m%ld\e[36m:\e[m\n", hpos+1, i); + } + + r = array_get_by_index(x, i, &_, change_callback) == 0; + } + + if (!silent) { + print_container_value(0, _.v.value, _.v.type, 1, hpos); + printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m"); + put_separator(hpos); + } +} + +void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos) { + + size_t n = array_size(x); + ssize_t i = random_uint64()%n; + + if (random_boolean()) i = ~i + 1; + + 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); + } + switch (array_remove_by_index(x, i)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } else { + struct { arr_t* x; _Bool s; unsigned int p; } v = { .x = x, .s = silent, .p = hpos }; + + if (!silent) printf("\e[%dG\e[36mTry to remove value from list:\e[m\n", hpos+1); + + switch (libcdsb_array_get(x, i, &v, remove_callback, 0)) { + case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break; + default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break; + } + } + + put_separator(hpos); +} From 11db0557b9b6b27b021e6563b6d091976a8fff8c Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 17:08:06 +0300 Subject: [PATCH 30/42] Update list tests --- tests/src/list/main.c | 5 ++++- tests/src/list/plug.h | 1 + tests/src/list/src/io.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/src/list/main.c b/tests/src/list/main.c index 9cdcbcd..71a3352 100644 --- a/tests/src/list/main.c +++ b/tests/src/list/main.c @@ -9,7 +9,10 @@ int main(int argc, char** argv) { list_t x = { .first = 0, .last = 0 }; list_t y = { .first = 0, .last = 0 }; - visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12); + visual_push(&x, (random_uint8()%9) + 8); + visual_slice(&x, &y); + + visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%3) + 6); visual_extend(&x, &y); visual_sort2(&x, &y); diff --git a/tests/src/list/plug.h b/tests/src/list/plug.h index 7362e9d..21c7aa5 100644 --- a/tests/src/list/plug.h +++ b/tests/src/list/plug.h @@ -23,3 +23,4 @@ extern void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1); extern void visual_remove2(list_t* x0, list_t* x1); extern void visual_sort2(list_t* x0, list_t* x1); extern void visual_extend(list_t* x, list_t* s); +extern void visual_slice(list_t* x, list_t* s); diff --git a/tests/src/list/src/io.c b/tests/src/list/src/io.c index 5ed0ed0..bbb441f 100644 --- a/tests/src/list/src/io.c +++ b/tests/src/list/src/io.c @@ -186,3 +186,38 @@ void visual_extend(list_t* x, list_t* s) { psleep(900000); fputs("\e[u\e[J", stdout); } + + +void visual_slice(list_t* x, list_t* s) { + + size_t n = list_size(x); + ssize_t i = random_uint64()%(n - 1); + + n = n - (i + 1); + + puts("\e[s\e[36mTry to slice list:\e[m\n\n"); + printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ==="); + + list_info(x, 0); + list_print(x, "(src)", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(s, 60); + list_print(s, "(dest)", 60); + + psleep(900000); + + list_slice(s, x, i, n, 1); + fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout); + + puts(""); + put_separator(0); + + list_info(x, 0); + list_print(x, "(src)", 0); + fputs("\e[u\e[s\e[4E", stdout); + list_info(s, 60); + list_print(s, "(dest)", 60); + psleep(900000); + + fputs("\e[u\e[J", stdout); +} From fe6732a39b37240a0fa7fb657fef49a931b6c955 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 17:08:36 +0300 Subject: [PATCH 31/42] Update array tests --- tests/src/array/src/plug.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/src/array/src/plug.c b/tests/src/array/src/plug.c index 30511e8..364b6fc 100644 --- a/tests/src/array/src/plug.c +++ b/tests/src/array/src/plug.c @@ -3,6 +3,7 @@ #include #include "../../../../include/extra/vtype.h" +#include "../../../include/random.h" vtype_string* string_duplicate(const vtype_string* x) { return 0; } vtype_list* list_duplicate (const vtype_list* x) { return 0; } @@ -14,10 +15,10 @@ 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) { random_int8(); } -int list_compare (const vtype_list* s0, const vtype_list* s1) { random_int8(); } -int map_compare (const vtype_map* s0, const vtype_map* s1) { random_int8(); } -int vset_compare (const vtype_set* s0, const vtype_set* s1) { random_int8(); } +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(); } 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)); } From 8dc5a84d49737861715b2c91b0fadcafefa63f56 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 20:50:03 +0300 Subject: [PATCH 32/42] Update tests --- tests/src/array/src/random.c | 2 +- tests/src/list/src/io.c | 2 +- tests/src/list/src/random.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/array/src/random.c b/tests/src/array/src/random.c index 22af216..4dcc8b7 100644 --- a/tests/src/array/src/random.c +++ b/tests/src/array/src/random.c @@ -79,5 +79,5 @@ void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos) { } } - put_separator(hpos); + if (!silent) put_separator(hpos); } diff --git a/tests/src/list/src/io.c b/tests/src/list/src/io.c index bbb441f..793ae33 100644 --- a/tests/src/list/src/io.c +++ b/tests/src/list/src/io.c @@ -58,7 +58,7 @@ void visual_remove(list_t* x) { list_print(x, 0, 0); psleep(100000); - if (!0) fputs("\e[u\e[J", stdout); + fputs("\e[u\e[J", stdout); } } diff --git a/tests/src/list/src/random.c b/tests/src/list/src/random.c index 2552978..5a4a1a9 100644 --- a/tests/src/list/src/random.c +++ b/tests/src/list/src/random.c @@ -78,5 +78,5 @@ void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) { } } - put_separator(hpos); + if (!silent) put_separator(hpos); } From 41c220b50766ee7e29bd0e825ce152507605dae9 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 20:50:20 +0300 Subject: [PATCH 33/42] Update map tests --- tests/src/map/main.c | 8 +- tests/src/map/plug.h | 217 ++----------------------------------- tests/src/map/src/io.c | 83 ++++++++++++++ tests/src/map/src/plug.c | 20 ++++ tests/src/map/src/random.c | 53 +++++++++ 5 files changed, 169 insertions(+), 212 deletions(-) create mode 100644 tests/src/map/src/io.c create mode 100644 tests/src/map/src/plug.c create mode 100644 tests/src/map/src/random.c diff --git a/tests/src/map/main.c b/tests/src/map/main.c index f08ef64..d054ec0 100644 --- a/tests/src/map/main.c +++ b/tests/src/map/main.c @@ -6,8 +6,10 @@ int main(int argc, char** argv) { test_init(argc, argv); - map_t x = map_random(36, 0.1); + map_t x; - while(map_remove_random(&x, 0.1)) {} - map_free(&x); + map_init(&x, random_uint8()%VTYPE_LDOUBLE + 1); + + visual_push(&x, random_uint8()%17 + 16); + visual_remove(&x); } diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h index 2ea8ee7..bb64636 100644 --- a/tests/src/map/plug.h +++ b/tests/src/map/plug.h @@ -7,214 +7,13 @@ #include "../../include/test.h" #include "../../include/time.h" -vtype_string* string_duplicate(const vtype_string* x) { return 0; } -vtype_array* array_duplicate (const vtype_array* x) { return 0; } -vtype_list* list_duplicate (const vtype_list* x) { return 0; } -vtype_set* vset_duplicate (const vtype_set* x) { return 0; } +void map_push_random(map_t* x, _Bool silent, unsigned int hpos); +void map_remove_random(map_t* x, _Bool silent, unsigned int hpos); -void string_free(vtype_string* x) {} -void array_free (vtype_array* x) {} -void list_free (vtype_list* x) {} -void vset_free (vtype_set* x) {} +void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br); +void map_print(map_t* x, const char* prefix, unsigned int hpos); +void map_info(const map_t* x, unsigned int hpos); +void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos); -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(); } - - -static int map_node_print(const void* k, vtype kt, void* v, vtype vt, void* _) { - printf(" \e[31m%24s\e[m \e[36m:\e[m", vtype_stringify(k, kt)); - printf(" \e[31m%s\e[m", vtype_stringify(v, vt)); - printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(vt)); - return 0; -} - -static void map_print(map_t* x, const char* prefix) { - print_container_values_prefix("Map", prefix); - map_foreach(x, 0, map_node_print); - put_separator(); -} - -static void map_info(const map_t* x) { - print_container_info("Map", "nodes", &x->type, map_size(x), -1); - put_separator(); -} - -static void map_rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br) { - if (!ind) { - ind = "\e[36m"; - br = 1; - } - - size_t n = strlen(ind); - char x[n + 10]; - - if (mnode_is_empty(s)) return; - - memcpy(x, ind, n); - memcpy(x + n, " \0 ", 9); - fputs(ind, stdout); - - if (br) { - fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); - } else { - fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); - memcpy(x + n, "│", 3); - x[n + 5] = ' '; - } - - fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); - printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); - printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); - printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); - - map_rbtree_print(s->left, t, x, false); - map_rbtree_print(s->right, t, x, true); -} - -static void map_push_random(map_t* x, double pause) { - var_t _[2]; - vtype t[2]; - - printf("\e[s\e[36mUpdate value in map (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", vtype_name(x->type)); - - switch (random_uint8()%13) { - default: - case 0: _[0].b = random_boolean(); print_container_value(0, _, t[0] = VTYPE_BOOLEAN, 1); break; - case 1: _[0].u8 = random_uint8(); print_container_value(0, _, t[0] = VTYPE_UINT8, 1); break; - case 2: _[0].u16 = random_uint16(); print_container_value(0, _, t[0] = VTYPE_UINT16, 1); break; - case 3: _[0].u32 = random_uint32(); print_container_value(0, _, t[0] = VTYPE_UINT32, 1); break; - case 4: _[0].u64 = random_uint64(); print_container_value(0, _, t[0] = VTYPE_UINT64, 1); break; - case 5: _[0].u8 = random_int8(); print_container_value(0, _, t[0] = VTYPE_INT8, 1); break; - case 6: _[0].u16 = random_int16(); print_container_value(0, _, t[0] = VTYPE_INT16, 1); break; - case 7: _[0].u32 = random_int32(); print_container_value(0, _, t[0] = VTYPE_INT32, 1); break; - case 8: _[0].u64 = random_int64(); print_container_value(0, _, t[0] = VTYPE_INT64, 1); break; - case 9: _[0].f = random_float(); print_container_value(0, _, t[0] = VTYPE_FLOAT, 1); break; - case 10: _[0].d = random_double(); print_container_value(0, _, t[0] = VTYPE_DOUBLE, 1); break; - case 11: _[0].ld = random_ldouble(); print_container_value(0, _, t[0] = VTYPE_LDOUBLE, 1); break; - - case 12: if (sizeof(void*) == 8) { - _[0].u64 = random_uint64(); print_container_value(0, _, t[0] = VTYPE_POINTER, 1); break; - } else { - _[0].u32 = random_uint32(); print_container_value(0, _, t[0] = VTYPE_POINTER, 1); break; - } - } - - switch (random_uint8()%13) { - default: - case 0: _[1].b = random_boolean(); print_container_value(0, _+1, t[1] = VTYPE_BOOLEAN, 1); break; - case 1: _[1].u8 = random_uint8(); print_container_value(0, _+1, t[1] = VTYPE_UINT8, 1); break; - case 2: _[1].u16 = random_uint16(); print_container_value(0, _+1, t[1] = VTYPE_UINT16, 1); break; - case 3: _[1].u32 = random_uint32(); print_container_value(0, _+1, t[1] = VTYPE_UINT32, 1); break; - case 4: _[1].u64 = random_uint64(); print_container_value(0, _+1, t[1] = VTYPE_UINT64, 1); break; - case 5: _[1].u8 = random_int8(); print_container_value(0, _+1, t[1] = VTYPE_INT8, 1); break; - case 6: _[1].u16 = random_int16(); print_container_value(0, _+1, t[1] = VTYPE_INT16, 1); break; - case 7: _[1].u32 = random_int32(); print_container_value(0, _+1, t[1] = VTYPE_INT32, 1); break; - case 8: _[1].u64 = random_int64(); print_container_value(0, _+1, t[1] = VTYPE_INT64, 1); break; - case 9: _[1].f = random_float(); print_container_value(0, _+1, t[1] = VTYPE_FLOAT, 1); break; - case 10: _[1].d = random_double(); print_container_value(0, _+1, t[1] = VTYPE_DOUBLE, 1); break; - case 11: _[1].ld = random_ldouble(); print_container_value(0, _+1, t[1] = VTYPE_LDOUBLE, 1); break; - - case 12: if (sizeof(void*) == 8) { - _[1].u64 = random_uint64(); print_container_value(0, _+1, t[1] = VTYPE_POINTER, 1); break; - } else { - _[1].u32 = random_uint32(); print_container_value(0, _+1, t[1] = VTYPE_POINTER, 1); break; - } - } - - if (libcdsb_map_update(x, _, t[0], _+1, t[1])) { - puts("\e[33;1mCHANGE\e[m"); - } else { - puts("\e[32;1mINSERT\e[m"); - } - - put_separator(); - - map_rbtree_print(x->root, x->type, 0, 0); - put_separator(); - psleep(pause * 1000000); - fputs("\e[u\e[J", stdout); -} - -static int map_node_remove_random(const void* k, vtype kt, void* v, vtype vt, void* data) { - struct { - size_t n; - map_t* set; - double pause; - } *d = data; - - if (!d->n--) { - map_node_print(k, kt, v, vt, 0); - - if (libcdsb_map_find(0, d->set, k, kt, 1)) { - puts("\e[32;1mSUCCESS\e[m"); - } else { - puts("\e[s\e[31;1mFAILURE\e[m"); - abort(); - } - - put_separator(); - - map_rbtree_print(d->set->root, d->set->type, 0, 0); - put_separator(); - psleep(d->pause * 1000000); - - return true; - } - return 0; -} - - -static bool map_remove_random(map_t* x, double pause) { - struct { - size_t n; - map_t* set; - double pause; - } d; - d.n = map_size(x); - d.set = x; - d.pause = pause; - - printf("\e[s\e[36mTry to remove value from map (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); - - if (d.n) { - d.n = random_uint32()%d.n; - } else goto end_; - - if (!map_foreach(x, &d, map_node_remove_random)) { - end_: - puts("\e[u\e[J\e[32;1mMap is empty\e[m"); - return false; - } - - fputs("\e[u\e[J", stdout); - return true; -} - -static map_t map_random(unsigned int n, double pause) { - map_t x; - - switch (random_uint8()%12) { - default: - case 0: map_init(&x, VTYPE_UINT8); break; - case 1: map_init(&x, VTYPE_UINT16); break; - case 2: map_init(&x, VTYPE_UINT32); break; - case 3: map_init(&x, VTYPE_UINT64); break; - case 4: map_init(&x, VTYPE_INT8); break; - case 5: map_init(&x, VTYPE_INT16); break; - case 6: map_init(&x, VTYPE_INT32); break; - case 7: map_init(&x, VTYPE_INT64); break; - case 8: map_init(&x, VTYPE_POINTER); break; - case 9: map_init(&x, VTYPE_FLOAT); break; - case 10: map_init(&x, VTYPE_DOUBLE); break; - case 11: map_init(&x, VTYPE_LDOUBLE); break; - } - - if (n) { - while (--n) map_push_random(&x, pause); - map_push_random(&x, pause); - } - - return x; -} +void visual_push(map_t* x, size_t n); +void visual_remove(map_t* x); diff --git a/tests/src/map/src/io.c b/tests/src/map/src/io.c new file mode 100644 index 0000000..5bb48f4 --- /dev/null +++ b/tests/src/map/src/io.c @@ -0,0 +1,83 @@ +/* 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 map_print(map_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Map", prefix, 0); + map_foreach(x, &hpos, node_print_callback); + put_separator(0); +} + +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 map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Map", prefix, 0); + rbtree_print(x->root, x->type, 0, 0); +} + +void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (mnode_is_empty(s)) return; + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + + fputs(ind, stdout); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); + printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); + + rbtree_print(s->left, t, x, false); + rbtree_print(s->right, t, x, true); +} + +void visual_push(map_t* x, size_t n) { + while (n--) { + fputs("\e[s", stdout); + + map_push_random(x, 0, 0); + + map_info(x, 0); + rbtree_print(x->root, x->type, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove(map_t* x) { + while (!mnode_is_empty(x->root)) { + fputs("\e[s", stdout); + map_remove_random(x, 0, 0); + map_info(x, 0); + rbtree_print(x->root, x->type, 0, 0); + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} diff --git a/tests/src/map/src/plug.c b/tests/src/map/src/plug.c new file mode 100644 index 0000000..b2a8764 --- /dev/null +++ b/tests/src/map/src/plug.c @@ -0,0 +1,20 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../../src/__internal/include.h" +#include "../../../include/random.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_list* list_duplicate (const vtype_list* x) { return 0; } +vtype_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 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 vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); } diff --git a/tests/src/map/src/random.c b/tests/src/map/src/random.c new file mode 100644 index 0000000..e917874 --- /dev/null +++ b/tests/src/map/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; map_t* x; unsigned int hp; } *d = _; + + if (!d->n--) { + print_container_value(0, k, kt, 0, d->hp); + + if (libcdsb_map_find(d->x, k, kt, 0, 0, 1)) { + printf("\e[%dG\e[32;1mSUCCESS\e[m", d->hp+1); + } else printf("\e[%dG\e[31;1mFAILURE\e[m", d->hp+1); + + return -2; + } + return 0; +} + +void map_push_random(map_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 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); + } + + if (libcdsb_map_update(x, k.value, k.type, v.value, v.type)) { + if (!silent) printf("\e[%dG\\e[33;1mCHANGE\e[m", hpos+1); + } else if (!silent) printf("\e[%dG\\e[32;1mINSERT\e[m", hpos+1); + + if (!silent) put_separator(0); +} + + +void map_remove_random(map_t* x, _Bool silent, unsigned int hpos) { + struct { size_t n; map_t* x; unsigned int hp; } d = { .n = map_size(x), .x = x, .hp = hpos }; + + if (!silent) + printf("\e[%dG\e[36mTry to remove value from map (\e[m\e[32;1m%s\e[m\e[36m) by key:\e[m\n", hpos+1, libcdsb_vtype_name(x->type)); + + if (d.n) { + d.n = random_uint32()%d.n; + map_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); +} From d57f5ed2c6ef9435867e83e21fefdb5ee14ba3b7 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 21:17:07 +0300 Subject: [PATCH 34/42] Update map tests --- tests/src/map/plug.h | 1 - tests/src/map/src/io.c | 71 +++++++++++++++++++------------------- tests/src/map/src/random.c | 10 +++--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h index bb64636..a2ae449 100644 --- a/tests/src/map/plug.h +++ b/tests/src/map/plug.h @@ -10,7 +10,6 @@ void map_push_random(map_t* x, _Bool silent, unsigned int hpos); void map_remove_random(map_t* x, _Bool silent, unsigned int hpos); -void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br); void map_print(map_t* x, const char* prefix, unsigned int hpos); void map_info(const map_t* x, unsigned int hpos); void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos); diff --git a/tests/src/map/src/io.c b/tests/src/map/src/io.c index 5bb48f4..a01da4b 100644 --- a/tests/src/map/src/io.c +++ b/tests/src/map/src/io.c @@ -8,6 +8,39 @@ static int node_print_callback(const void* k, vtype kt, void* v, vtype vt, void* return 0; } +static void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br, unsigned int hpos) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (mnode_is_empty(s)) return; + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + + printf("\e[%dG%s", hpos+1, ind); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); + printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); + printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); + + rbtree_print(s->left, t, x, false, hpos); + rbtree_print(s->right, t, x, true, hpos); +} + void map_print(map_t* x, const char* prefix, unsigned int hpos) { print_container_values_prefix("Map", prefix, 0); map_foreach(x, &hpos, node_print_callback); @@ -21,41 +54,9 @@ void map_info(const map_t* x, unsigned int hpos) { void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos) { print_container_values_prefix("Map", prefix, 0); - rbtree_print(x->root, x->type, 0, 0); + rbtree_print(x->root, x->type, 0, 0, hpos); } -void rbtree_print(const mnode_t* s, vtype t, const char* ind, bool br) { - if (!ind) { - ind = "\e[36m"; - br = 1; - } - - size_t n = strlen(ind); - char x[n + 10]; - - if (mnode_is_empty(s)) return; - - memcpy(x, ind, n); - memcpy(x + n, " \0 ", 9); - - fputs(ind, stdout); - - if (br) { - fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); - } else { - fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); - memcpy(x + n, "│", 3); - x[n + 5] = ' '; - } - - fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); - printf("%s\e[m \e[36m:\e[m ", vnode_stringify(&s->key, t)); - printf("\e[31m%s\e[m", vnode_stringify(&s->value, s->type)); - printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", vtype_name(s->type)); - - rbtree_print(s->left, t, x, false); - rbtree_print(s->right, t, x, true); -} void visual_push(map_t* x, size_t n) { while (n--) { @@ -64,7 +65,7 @@ void visual_push(map_t* x, size_t n) { map_push_random(x, 0, 0); map_info(x, 0); - rbtree_print(x->root, x->type, 0, 0); + map_rbtree_print(x, 0, 0); psleep(100000); fputs("\e[u\e[J", stdout); @@ -76,7 +77,7 @@ void visual_remove(map_t* x) { fputs("\e[s", stdout); map_remove_random(x, 0, 0); map_info(x, 0); - rbtree_print(x->root, x->type, 0, 0); + map_rbtree_print(x, 0, 0); psleep(100000); fputs("\e[u\e[J", stdout); } diff --git a/tests/src/map/src/random.c b/tests/src/map/src/random.c index e917874..c4615f2 100644 --- a/tests/src/map/src/random.c +++ b/tests/src/map/src/random.c @@ -9,9 +9,9 @@ static int remove_callback(const void* k, vtype kt, void* v, vtype vt, void* _) if (!d->n--) { print_container_value(0, k, kt, 0, d->hp); - if (libcdsb_map_find(d->x, k, kt, 0, 0, 1)) { - printf("\e[%dG\e[32;1mSUCCESS\e[m", d->hp+1); - } else printf("\e[%dG\e[31;1mFAILURE\e[m", d->hp+1); + if (libcdsb_map_find(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; } @@ -29,8 +29,8 @@ void map_push_random(map_t* x, _Bool silent, unsigned int hpos) { } if (libcdsb_map_update(x, k.value, k.type, v.value, v.type)) { - if (!silent) printf("\e[%dG\\e[33;1mCHANGE\e[m", hpos+1); - } else if (!silent) printf("\e[%dG\\e[32;1mINSERT\e[m", 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); if (!silent) put_separator(0); } From f8878390a792dbefc7a07fda79abe349bdbf064a Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 21:17:35 +0300 Subject: [PATCH 35/42] Update set tests --- tests/src/set/main.c | 8 +- tests/src/set/plug.h | 191 ++----------------------------------- tests/src/set/src/io.c | 82 ++++++++++++++++ tests/src/set/src/plug.c | 20 ++++ tests/src/set/src/random.c | 52 ++++++++++ 5 files changed, 166 insertions(+), 187 deletions(-) create mode 100644 tests/src/set/src/io.c create mode 100644 tests/src/set/src/plug.c create mode 100644 tests/src/set/src/random.c diff --git a/tests/src/set/main.c b/tests/src/set/main.c index be56d43..1ba5f76 100644 --- a/tests/src/set/main.c +++ b/tests/src/set/main.c @@ -6,8 +6,10 @@ int main(int argc, char** argv) { test_init(argc, argv); - set_t x = vset_random(36, 0.1); + set_t x; - while(vset_remove_random(&x, 0.1)) {} - vset_free(&x); + vset_init(&x, random_uint8()%VTYPE_LDOUBLE + 1); + + visual_push(&x, random_uint8()%17 + 16); + visual_remove(&x); } diff --git a/tests/src/set/plug.h b/tests/src/set/plug.h index fd4e092..756e649 100644 --- a/tests/src/set/plug.h +++ b/tests/src/set/plug.h @@ -8,188 +8,11 @@ #include "../../include/test.h" #include "../../include/time.h" -vtype_string* string_duplicate(const vtype_string* x) { return 0; } -vtype_array* array_duplicate (const vtype_array* x) { return 0; } -vtype_list* list_duplicate (const vtype_list* x) { return 0; } -vtype_map* map_duplicate (const vtype_map* x) { return 0; } +extern void vset_push_random(set_t* x, _Bool silent, unsigned int hpos); +extern void vset_remove_random(set_t* x, _Bool silent, unsigned int hpos); -void string_free(vtype_string* x) {} -void array_free (vtype_array* x) {} -void list_free (vtype_list* x) {} -void map_free (vtype_map* x) {} - -int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); } -int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } -int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); } -int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } - - -static int vset_node_print(const void* v, vtype t, void* _) { - print_container_value(0, v, t, false); - return 0; -} - -static void vset_print(set_t* x, const char* prefix) { - print_container_values_prefix("Set", prefix); - vset_foreach(x, 0, vset_node_print); - put_separator(); -} - -static void vset_info(const set_t* x) { - print_container_info("Set", "nodes", &x->type, vset_size(x), -1); - put_separator(); -} - -static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br) { - if (!ind) { - ind = "\e[36m"; - br = 1; - } - - size_t n = strlen(ind); - char x[n + 10]; - - if (rbnode_is_empty(s)) return; - - memcpy(x, ind, n); - memcpy(x + n, " \0 ", 9); - fputs(ind, stdout); - - if (br) { - fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); - } else { - fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); - memcpy(x + n, "│", 3); - x[n + 5] = ' '; - } - - - fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); - printf("%s\e[m\n", libcdsb_vtype_stringify(vnode_peek(&s->value, t), t)); - - rbtree_print(s->left, t, x, false); - rbtree_print(s->right, t, x, true); -} - -static void vset_push_random(set_t* x, double pause) { - var_t _; - vtype t; - - printf("\e[s\e[36mTry to insert into set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); - - switch (random_uint8()%13) { - default: - case 0: _.b = random_boolean(); print_container_value(0, &_, t = VTYPE_BOOLEAN, 1); break; - case 1: _.u8 = random_uint8(); print_container_value(0, &_, t = VTYPE_UINT8, 1); break; - case 2: _.u16 = random_uint16(); print_container_value(0, &_, t = VTYPE_UINT16, 1); break; - case 3: _.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_UINT32, 1); break; - case 4: _.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_UINT64, 1); break; - case 5: _.u8 = random_int8(); print_container_value(0, &_, t = VTYPE_INT8, 1); break; - case 6: _.u16 = random_int16(); print_container_value(0, &_, t = VTYPE_INT16, 1); break; - case 7: _.u32 = random_int32(); print_container_value(0, &_, t = VTYPE_INT32, 1); break; - case 8: _.u64 = random_int64(); print_container_value(0, &_, t = VTYPE_INT64, 1); break; - case 9: _.f = random_float(); print_container_value(0, &_, t = VTYPE_FLOAT, 1); break; - case 10: _.d = random_double(); print_container_value(0, &_, t = VTYPE_DOUBLE, 1); break; - case 11: _.ld = random_ldouble(); print_container_value(0, &_, t = VTYPE_LDOUBLE, 1); break; - - case 12: if (sizeof(void*) == 8) { - _.u64 = random_uint64(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break; - } else { - _.u32 = random_uint32(); print_container_value(0, &_, t = VTYPE_POINTER, 1); break; - } - } - - if (libcdsb_vset_insert(x, &_, t)) { - puts("\e[32;1mSUCCESS\e[m"); - } else { - puts("\e[31;1mFAILURE\e[m"); - } - put_separator(); - - rbtree_print(x->root, x->type, 0, 0); - put_separator(); - psleep(pause * 1000000); - fputs("\e[u\e[J", stdout); -} - -static int vset_node_remove_random(const void* v, vtype t, void* data) { - struct { - size_t n; - set_t* set; - double pause; - } *d = data; - - if (!d->n--) { - print_container_value(0, v, t, 1); - - if (libcdsb_vset_find(0, d->set, v, t, 1)) { - puts("\e[32;1mSUCCESS\e[m"); - } else { - puts("\e[31;1mFAILURE\e[m"); - abort(); - } - - put_separator(); - - rbtree_print(d->set->root, d->set->type, 0, 0); - put_separator(); - psleep(d->pause * 1000000); - - return true; - } - return 0; -} - - -static bool vset_remove_random(set_t* x, double pause) { - struct { - size_t n; - set_t* set; - double pause; - } d; - d.n = vset_size(x); - d.set = x; - d.pause = pause; - - printf("\e[s\e[36mTry to remove value from set (\e[m\e[32;1m%s\e[m\e[36m)\e[m:\n", libcdsb_vtype_name(x->type)); - - if (d.n) { - d.n = random_uint32()%d.n; - } else goto end_; - - if (!vset_foreach(x, &d, vset_node_remove_random)) { - end_: - puts("\e[u\e[J\e[32;1mSet is empty\e[m"); - return false; - } - - fputs("\e[u\e[J", stdout); - return true; -} - -static set_t vset_random(unsigned int n, double pause) { - set_t x; - - switch (random_uint8()%12) { - default: - case 0: vset_init(&x, VTYPE_UINT8); break; - case 1: vset_init(&x, VTYPE_UINT16); break; - case 2: vset_init(&x, VTYPE_UINT32); break; - case 3: vset_init(&x, VTYPE_UINT64); break; - case 4: vset_init(&x, VTYPE_INT8); break; - case 5: vset_init(&x, VTYPE_INT16); break; - case 6: vset_init(&x, VTYPE_INT32); break; - case 7: vset_init(&x, VTYPE_INT64); break; - case 8: vset_init(&x, VTYPE_POINTER); break; - case 9: vset_init(&x, VTYPE_FLOAT); break; - case 10: vset_init(&x, VTYPE_DOUBLE); break; - case 11: vset_init(&x, VTYPE_LDOUBLE); break; - } - - if (n) { - while (--n) vset_push_random(&x, pause); - vset_push_random(&x, pause); - } - - return x; -} +extern void vset_print(set_t* x, const char* prefix, unsigned int hpos); +extern void vset_info(const set_t* x, unsigned int hpos); +extern void vset_rbtree_print(set_t *x, const char* prefix, unsigned int hpos); +extern void visual_push(set_t* x, size_t n); +extern void visual_remove(set_t* x); diff --git a/tests/src/set/src/io.c b/tests/src/set/src/io.c new file mode 100644 index 0000000..378aaf6 --- /dev/null +++ b/tests/src/set/src/io.c @@ -0,0 +1,82 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + + +static int node_print_callback(const void* v, vtype t, void* _) { + print_container_value(0, v, t, 0, *(unsigned int*)_); + return 0; +} + +static void rbtree_print(const rbnode_t* s, vtype t, const char* ind, bool br, unsigned int hpos) { + if (!ind) { + ind = "\e[36m"; + br = 1; + } + + size_t n = strlen(ind); + char x[n + 10]; + + if (rbnode_is_empty(s)) return; + + memcpy(x, ind, n); + memcpy(x + n, " \0 ", 9); + + printf("\e[%dG%s", hpos+1, ind); + + if (br) { + fputs("\e[m\e[36;1mR\e[m\e[36m────\e[m", stdout); + } else { + fputs("\e[m\e[36;1mL\e[m\e[36m────\e[m", stdout); + memcpy(x + n, "│", 3); + x[n + 5] = ' '; + } + + fputs((s->colored) ? "\e[31;1m" : "\e[37m", stdout); + printf("%s\e[m \e[36m\e[m\n", vnode_stringify(&s->value, t)); + + rbtree_print(s->left, t, x, false, hpos); + rbtree_print(s->right, t, x, true, hpos); +} + +void vset_print(set_t* x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Set", prefix, 0); + vset_foreach(x, &hpos, node_print_callback); + put_separator(0); +} + +void vset_info(const set_t* x, unsigned int hpos) { + print_container_info("Set", "nodes", &x->type, vset_size(x), -1, 0); + put_separator(0); +} + +void vset_rbtree_print(set_t *x, const char* prefix, unsigned int hpos) { + print_container_values_prefix("Set", prefix, 0); + rbtree_print(x->root, x->type, 0, 0, hpos); +} + +void visual_push(set_t* x, size_t n) { + while (n--) { + fputs("\e[s", stdout); + + vset_push_random(x, 0, 0); + + vset_info(x, 0); + vset_rbtree_print(x, 0, 0); + + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} + +void visual_remove(set_t* x) { + while (!rbnode_is_empty(x->root)) { + fputs("\e[s", stdout); + vset_remove_random(x, 0, 0); + vset_info(x, 0); + vset_rbtree_print(x, 0, 0); + psleep(100000); + fputs("\e[u\e[J", stdout); + } +} diff --git a/tests/src/set/src/plug.c b/tests/src/set/src/plug.c new file mode 100644 index 0000000..00cf60d --- /dev/null +++ b/tests/src/set/src/plug.c @@ -0,0 +1,20 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../../src/__internal/include.h" +#include "../../../include/random.h" + +vtype_string* string_duplicate(const vtype_string* x) { return 0; } +vtype_array* array_duplicate (const vtype_array* x) { return 0; } +vtype_list* list_duplicate (const vtype_list* x) { return 0; } +vtype_map* map_duplicate (const vtype_map* x) { return 0; } + +void string_free(vtype_string* x) {} +void array_free (vtype_array* x) {} +void list_free (vtype_list* x) {} +void map_free (vtype_map* x) {} + +int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); } +int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } +int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); } +int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); } diff --git a/tests/src/set/src/random.c b/tests/src/set/src/random.c new file mode 100644 index 0000000..dc6d6d5 --- /dev/null +++ b/tests/src/set/src/random.c @@ -0,0 +1,52 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +static int remove_callback(const void* v, vtype t, void* _) { + struct { size_t n; set_t* x; unsigned int hp; } *d = _; + + if (!d->n--) { + print_container_value(0, v, t, 0, d->hp); + + if (libcdsb_vset_find(d->x, v, t, 0, 0, 1) == 0) { + printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1); + } else printf("\e[%dG\e[31;1mFAILURE\e[m\n", d->hp+1); + + return -2; + } + return 0; +} + +void vset_push_random(set_t* x, _Bool silent, unsigned int hpos) { + + value_t v = random_value(); + + if (!silent) { + printf("\e[%dG\e[36mUpdate value in set (\e[m\e[32;1m%s\e[m\e[36m):\e[m\n", hpos+1, vtype_name(x->type)); + print_container_value(0, v.value, v.type, 1, hpos); + } + + if (libcdsb_vset_insert(x, v.value, v.type)) { + if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); + } else if (!silent) printf("\e[%dG\e[31;1mFAILURE\e[m\n", hpos+1); + + if (!silent) put_separator(0); +} + + +void vset_remove_random(set_t* x, _Bool silent, unsigned int hpos) { + struct { size_t n; set_t* x; unsigned int hp; } d = { .n = vset_size(x), .x = x, .hp = hpos }; + + if (!silent) + printf("\e[%dG\e[36mTry to remove value from set (\e[m\e[32;1m%s\e[m\e[36m):\e[m\n", hpos+1, libcdsb_vtype_name(x->type)); + + if (d.n) { + d.n = random_uint32()%d.n; + vset_foreach(x, &d, remove_callback); + } else if (!silent) { + printf("\e[%dG\e[32;1m\nFAILURE\e[m\n", hpos+1); + } + + if (!silent) put_separator(hpos); +} From a44a6d701ee02f0cb9bab5b003b45e8e59e1589c Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 21:42:24 +0300 Subject: [PATCH 36/42] Update string tests --- tests/src/string/main.c | 77 +++++++++++++++-------------------- tests/src/string/plug.h | 69 ++++--------------------------- tests/src/string/src/io.c | 48 ++++++++++++++++++++++ tests/src/string/src/plug.c | 20 +++++++++ tests/src/string/src/random.c | 51 +++++++++++++++++++++++ tests/src/string/trim.c | 33 --------------- 6 files changed, 160 insertions(+), 138 deletions(-) create mode 100644 tests/src/string/src/io.c create mode 100644 tests/src/string/src/plug.c create mode 100644 tests/src/string/src/random.c delete mode 100644 tests/src/string/trim.c diff --git a/tests/src/string/main.c b/tests/src/string/main.c index 141e0cc..522b3f8 100644 --- a/tests/src/string/main.c +++ b/tests/src/string/main.c @@ -3,53 +3,13 @@ #include "plug.h" -static void string_concat_random(vtype_string* x, unsigned int n) { - char* v; - - if (random_boolean()) { - v = random_utf8_cstring(n); - } else v = random_ascii_cstring(n); - - string_concat(x, v); - free(v); -} - -static void string_replace_random(vtype_string* x, unsigned int n) { - char* v; - - if (random_boolean()) { - v = random_utf8_cstring(n); - } else v = random_ascii_cstring(n); - - string_replace(x, x, v, -1); - - free(v); -} - -static void string_print_compare(const vtype_string* s0, const vtype_string* s1) { - int c = string_compare(s0, s1); - char* m; - if (c == 0) m = "=="; - else m = (c < 0) ? "<" : ">"; - - puts("\e[36mStrings comparsion:\e[m\n"); - printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); -} - -static void string_print_case_compare(const vtype_string* s0, const vtype_string* s1) { - int c = string_case_compare(s0, s1); - char* m; - if (c == 0) m = "=="; - else m = (c < 0) ? "<" : ">"; - - puts("\e[36mStrings case insensitive comparsion:\e[m\n"); - printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); -} int main(int argc, char** argv) { test_init(argc, argv); - vtype_string x, y; + str_t x, y; + int c = random_unicode_symbol(); + fputs("\e[s", stdout); x = string_random(30); string_print(&x, "(part 1)"); @@ -77,8 +37,8 @@ int main(int argc, char** argv) { string_to_lower(&x); string_to_upper(&y); - string_print_compare(&x, &y); - string_print_case_compare(&x, &y); + visual_compare(&x, &y); + visual_case_compare(&x, &y); string_reverse(&y); string_capitalize(&y); @@ -87,4 +47,31 @@ int main(int argc, char** argv) { string_free(&x); string_free(&y); + + psleep(900000); + + fputs("\e[u\e[J", stdout); + + x = string_random(12); + + string_align_center(&x, 30, 0); + string_info(&x); + string_print(&x, 0); + + string_trim_spaces(&x); + string_info(&x); + string_print(&x, "trimmed"); + + put_separator(0); + string_align_center(&x, 30, c); + string_info(&x); + string_print(&x, 0); + + string_trim(&x, c); + string_info(&x); + string_print(&x, "trimmed"); + + psleep(900000); + + string_free(&x); } diff --git a/tests/src/string/plug.h b/tests/src/string/plug.h index e0b2ad5..241dc93 100644 --- a/tests/src/string/plug.h +++ b/tests/src/string/plug.h @@ -9,65 +9,14 @@ #include "../../include/test.h" #include "../../include/time.h" -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; } +extern char* random_ascii_cstring(size_t size); +extern char* random_utf8_cstring(size_t size); +extern void string_concat_random(str_t* x, unsigned int n); +extern void string_replace_random(str_t* x, unsigned int n); -void array_free (vtype_array* x) {} -void list_free (vtype_list* x) {} -void map_free (vtype_map* x) {} -void vset_free (vtype_set* x) {} +extern void string_info(str_t* x); +extern void string_print(const str_t* x, const char* prefix); +extern str_t string_random(unsigned int n); -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(); } - -static char* random_ascii_cstring(size_t size) { - char* v = malloc(size + 1); - char* p = v; - - while (size--) { - *(p++) = random_ascii_char(); - } - *p = 0; - - return v; -} - -static char* random_utf8_cstring(size_t size) { - char* v = malloc(size * 4 + 1); - char* p = v; - - while (size--) { - p = tochar_unicode(p, random_unicode_symbol()); - } - - *p = 0; - return v; -} - -static void string_info(vtype_string* x) { - print_container_info("String", "utf8 chars", 0, string_size(x), string_nmemb(x)); - put_separator(); -} - -static void string_print(const vtype_string* x, const char* prefix) { - if (prefix) { - printf("\e[36m%s %s content:\e[m\n", "String", prefix); - } else printf("\e[36m%s content:\e[m\n", "String"); - - printf("\e[33m\"%s\"\e[m\n", x->buffer); - put_separator(); -} - -static vtype_string string_random(unsigned int n) { - vtype_string x; - - if (random_boolean()) { - x.buffer = random_utf8_cstring(n); - } else x.buffer = random_ascii_cstring(n); - - return x; -} +extern void visual_compare(const str_t* s0, const str_t* s1); +extern void visual_case_compare(const str_t* s0, const str_t* s1); diff --git a/tests/src/string/src/io.c b/tests/src/string/src/io.c new file mode 100644 index 0000000..1da8654 --- /dev/null +++ b/tests/src/string/src/io.c @@ -0,0 +1,48 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +void string_info(str_t* x) { + print_container_info("String", "utf8 chars", 0, string_size(x), string_nmemb(x), 0); + put_separator(0); +} + +void string_print(const str_t* x, const char* prefix) { + if (prefix) { + printf("\e[36m%s %s content:\e[m\n", "String", prefix); + } else printf("\e[36m%s content:\e[m\n", "String"); + + printf("\e[33m\"%s\"\e[m\n", x->buffer); + put_separator(0); +} + +str_t string_random(unsigned int n) { + str_t x; + + if (random_boolean()) { + x.buffer = random_utf8_cstring(n); + } else x.buffer = random_ascii_cstring(n); + + return x; +} + +void visual_compare(const str_t* s0, const str_t* s1) { + int c = string_compare(s0, s1); + char* m; + if (c == 0) m = "=="; + else m = (c < 0) ? "<" : ">"; + + puts("\e[36mStrings comparsion:\e[m\n"); + printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); +} + +void visual_case_compare(const str_t* s0, const str_t* s1) { + int c = string_case_compare(s0, s1); + char* m; + if (c == 0) m = "=="; + else m = (c < 0) ? "<" : ">"; + + puts("\e[36mStrings case insensitive comparsion:\e[m\n"); + printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer); +} diff --git a/tests/src/string/src/plug.c b/tests/src/string/src/plug.c new file mode 100644 index 0000000..6da62c4 --- /dev/null +++ b/tests/src/string/src/plug.c @@ -0,0 +1,20 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../../../src/__internal/include.h" +#include "../../../include/random.h" + +vtype_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 array_free (vtype_array* x) {} +void list_free (vtype_list* x) {} +void map_free (vtype_map* x) {} +void vset_free (vtype_set* 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(); } diff --git a/tests/src/string/src/random.c b/tests/src/string/src/random.c new file mode 100644 index 0000000..36a587c --- /dev/null +++ b/tests/src/string/src/random.c @@ -0,0 +1,51 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../plug.h" + +char* random_ascii_cstring(size_t size) { + char* v = malloc(size + 1); + char* p = v; + + while (size--) { + *(p++) = random_ascii_char(); + } + *p = 0; + + return v; +} + +char* random_utf8_cstring(size_t size) { + char* v = malloc(size * 4 + 1); + char* p = v; + + while (size--) { + p = tochar_unicode(p, random_unicode_symbol()); + } + + *p = 0; + return v; +} + +void string_concat_random(str_t* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_concat(x, v); + free(v); +} + +void string_replace_random(str_t* x, unsigned int n) { + char* v; + + if (random_boolean()) { + v = random_utf8_cstring(n); + } else v = random_ascii_cstring(n); + + string_replace(x, x, v, -1); + + free(v); +} diff --git a/tests/src/string/trim.c b/tests/src/string/trim.c deleted file mode 100644 index c5329d8..0000000 --- a/tests/src/string/trim.c +++ /dev/null @@ -1,33 +0,0 @@ -/* 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); - vtype_string x; - int c = random_unicode_symbol(); - - x = string_random(12); - - string_align_center(&x, 30, 0); - string_info(&x); - string_print(&x, 0); - - string_trim_spaces(&x); - string_info(&x); - string_print(&x, "trimmed"); - - put_separator(); - string_align_center(&x, 30, c); - string_info(&x); - string_print(&x, 0); - - string_trim(&x, c); - string_info(&x); - string_print(&x, "trimmed"); - - - string_free(&x); -} From 1f7ef918505cbb1c07bf5ed120bab6438d882903 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Mon, 15 Aug 2022 17:24:31 +0300 Subject: [PATCH 37/42] Add dict tests --- tests/Makefile | 10 ++++++- tests/src/array/src/plug.c | 18 ++++++++++--- tests/src/dict/main.c | 15 +++++++++++ tests/src/dict/plug.h | 17 ++++++++++++ tests/src/dict/src/io.c | 47 ++++++++++++++++++++++++++++++++ tests/src/dict/src/plug.c | 29 ++++++++++++++++++++ tests/src/dict/src/random.c | 53 +++++++++++++++++++++++++++++++++++++ tests/src/list/src/plug.c | 9 +++++++ tests/src/map/src/plug.c | 9 +++++++ tests/src/set/src/plug.c | 9 +++++++ tests/src/string/src/plug.c | 9 +++++++ 11 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 tests/src/dict/main.c create mode 100644 tests/src/dict/plug.h create mode 100644 tests/src/dict/src/io.c create mode 100644 tests/src/dict/src/plug.c create mode 100644 tests/src/dict/src/random.c 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; } From 4056544d49a80fa7ac8dc351f99548b4dbc88e99 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 18:36:50 +0300 Subject: [PATCH 38/42] Update tests headers --- tests/src/array/plug.h | 69 ----------------------------------------- tests/src/dict/plug.h | 12 +++---- tests/src/list/src/io.c | 1 - tests/src/map/plug.h | 14 ++++----- 4 files changed, 13 insertions(+), 83 deletions(-) diff --git a/tests/src/array/plug.h b/tests/src/array/plug.h index 254611a..f852e45 100644 --- a/tests/src/array/plug.h +++ b/tests/src/array/plug.h @@ -9,7 +9,6 @@ #include "../../include/test.h" #include "../../include/time.h" - extern void array_print(arr_t* x, const char* prefix, unsigned int hpos); extern void array_info(arr_t* x, unsigned int hpos); @@ -23,71 +22,3 @@ extern void visual_slice(arr_t* x, arr_t* s); extern void array_push_random (arr_t* x, _Bool silent, unsigned int hpos); extern void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos); - -/* -static void array_push_random(vtype_array* x) { - switch (random_uint8()%13) { - default: - case 0: array_push(x, random_boolean()); break; - case 1: array_push(x, random_uint8()); break; - case 2: array_push(x, random_uint16()); break; - case 3: array_push(x, random_uint32()); break; - case 4: array_push(x, random_uint64()); break; - case 5: array_push(x, random_int8()); break; - case 6: array_push(x, random_int16()); break; - case 7: array_push(x, random_int32()); break; - case 8: array_push(x, random_int64()); break; - case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; - case 10: array_push(x, random_float()); break; - case 11: array_push(x, random_double()); break; - case 12: array_push(x, random_ldouble()); break; - } -} - -static void array_init_random(vtype_array* x) { - switch (random_uint8()%13) { - default: - case 0: array_init(x, VTYPE_BOOLEAN); break; - case 1: array_init(x, VTYPE_UINT8); break; - case 2: array_init(x, VTYPE_UINT16); break; - case 3: array_init(x, VTYPE_UINT32); break; - case 4: array_init(x, VTYPE_UINT64); break; - case 5: array_init(x, VTYPE_INT8); break; - case 6: array_init(x, VTYPE_INT16); break; - case 7: array_init(x, VTYPE_INT32); break; - case 8: array_init(x, VTYPE_INT64); break; - case 9: array_init(x, VTYPE_POINTER); break; - case 10: array_init(x, VTYPE_FLOAT); break; - case 11: array_init(x, VTYPE_DOUBLE); break; - case 12: array_init(x, VTYPE_LDOUBLE); break; - } -} - -static vtype_array array_random(unsigned int n) { - vtype_array x; - - array_init_random(&x); - - for (int i = 0; i < n; ++i) { - array_push_random(&x); - } - - return x; -} - -static int array_value_print(void* v, ssize_t i, vtype t, void* _) { - print_container_value(&i, v, t, false); - return 0; -} - -static void array_print(vtype_array* x, const char* prefix) { - print_container_values_prefix("Array", prefix); - array_foreach(x, 0, array_value_print); - put_separator(); -} - -static void array_info(const vtype_array* x) { - print_container_info("Array", 0, &x->type, array_size(x), array_nmemb(x)); - put_separator(); -} -*/ diff --git a/tests/src/dict/plug.h b/tests/src/dict/plug.h index 57f84dc..e6b73db 100644 --- a/tests/src/dict/plug.h +++ b/tests/src/dict/plug.h @@ -7,11 +7,11 @@ #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); +extern void dict_push_random(dict_t* x, bool silent, unsigned int hpos); +extern 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); +extern void dict_info(const dict_t* x, unsigned int hpos); +extern void dict_print(dict_t* x, const char* prefix, unsigned int hpos); -void visual_push(dict_t* x, size_t n); -void visual_remove(dict_t* x); +extern void visual_push(dict_t* x, size_t n); +extern void visual_remove(dict_t* x); diff --git a/tests/src/list/src/io.c b/tests/src/list/src/io.c index 793ae33..fb61586 100644 --- a/tests/src/list/src/io.c +++ b/tests/src/list/src/io.c @@ -17,7 +17,6 @@ void list_info(list_t* x, unsigned int hpos) { print_container_info("List", "nodes", 0, list_size(x), -1, hpos); } - void visual_push(list_t* x, size_t n) { for (int i = 0; i < n; ++i) { fputs("\e[s", stdout); diff --git a/tests/src/map/plug.h b/tests/src/map/plug.h index a2ae449..da2aeed 100644 --- a/tests/src/map/plug.h +++ b/tests/src/map/plug.h @@ -7,12 +7,12 @@ #include "../../include/test.h" #include "../../include/time.h" -void map_push_random(map_t* x, _Bool silent, unsigned int hpos); -void map_remove_random(map_t* x, _Bool silent, unsigned int hpos); +extern void map_push_random(map_t* x, _Bool silent, unsigned int hpos); +extern void map_remove_random(map_t* x, _Bool silent, unsigned int hpos); -void map_print(map_t* x, const char* prefix, unsigned int hpos); -void map_info(const map_t* x, unsigned int hpos); -void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos); +extern void map_info(const map_t* x, unsigned int hpos); +extern void map_print(map_t* x, const char* prefix, unsigned int hpos); +extern void map_rbtree_print(map_t *x, const char* prefix, unsigned int hpos); -void visual_push(map_t* x, size_t n); -void visual_remove(map_t* x); +extern void visual_push(map_t* x, size_t n); +extern void visual_remove(map_t* x); From 382bd0070100b8c38c8c1cc0e2154cca90acac19 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 18:38:35 +0300 Subject: [PATCH 39/42] Add global (communicative) test --- tests/Makefile | 42 +++--- tests/src/global/main.c | 34 +++++ tests/src/global/plug.h | 23 +++ tests/src/global/src/io.c | 33 +++++ tests/src/global/src/random.c | 265 ++++++++++++++++++++++++++++++++++ 5 files changed, 375 insertions(+), 22 deletions(-) create mode 100644 tests/src/global/main.c create mode 100644 tests/src/global/plug.h create mode 100644 tests/src/global/src/io.c create mode 100644 tests/src/global/src/random.c diff --git a/tests/Makefile b/tests/Makefile index b1f7a65..bb8f5ee 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 ######################################################################################################################## diff --git a/tests/src/global/main.c b/tests/src/global/main.c new file mode 100644 index 0000000..6717c31 --- /dev/null +++ b/tests/src/global/main.c @@ -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; + } +} diff --git a/tests/src/global/plug.h b/tests/src/global/plug.h new file mode 100644 index 0000000..6b280eb --- /dev/null +++ b/tests/src/global/plug.h @@ -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); diff --git a/tests/src/global/src/io.c b/tests/src/global/src/io.c new file mode 100644 index 0000000..d7a5900 --- /dev/null +++ b/tests/src/global/src/io.c @@ -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); +} diff --git a/tests/src/global/src/random.c b/tests/src/global/src/random.c new file mode 100644 index 0000000..df0de0f --- /dev/null +++ b/tests/src/global/src/random.c @@ -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); +} From 80ba7082d4c045af8e9809a3117bc73be5f5570e Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 18:39:55 +0300 Subject: [PATCH 40/42] Minor fixes --- include/array.h | 2 +- include/extra/set.h | 5 ++--- src/__internal/rbtree.h | 6 +++--- src/__internal/vnode.h | 2 +- src/extra-memory.c | 3 +++ 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/array.h b/include/array.h index 9e7dee1..5b6d177 100644 --- a/include/array.h +++ b/include/array.h @@ -21,7 +21,7 @@ extern void array_reverse(vtype_array* x) Nonnull__(1); #define array_pop(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 0, 1) #define array_find(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 0, 0) #define array_rfind(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 1, 0) -#define list_countof(x, value) _LIBCDSB_Generic(libcdsb_array, count, value)(x, value) +#define array_countof(x, value) _LIBCDSB_Generic(libcdsb_array, count, value)(x, value) #define array_remove(x, value) array_pop(x, value, 0, 0) #define in_array(x, value) (array_find(x, value, 0, 0) == 0) diff --git a/include/extra/set.h b/include/extra/set.h index 0b25cdb..a34113b 100644 --- a/include/extra/set.h +++ b/include/extra/set.h @@ -8,9 +8,8 @@ #define vset_foreach(x, data, callback) libcdsb_vset_foreach(x, data, callback, 0) -extern bool libcdsb_vset_insert(vtype_set* x, const void* value, vtype type) LIBCDSB_nn12__; - +extern bool libcdsb_vset_insert(vtype_set* x, const void* value, vtype type) Nonnull__(1); extern int libcdsb_vset_find (vtype_set* x, const void* value, vtype type, void* data, vset_access_callback, bool cut) Nonnull__(1); -extern int libcdsb_vset_foreach(vtype_set* x, void* data, vset_access_callback, bool flush) LIBCDSB_nn13__; +extern int libcdsb_vset_foreach(vtype_set* x, void* data, vset_access_callback, bool flush) Nonnull__(1,3); #endif /* LIBCDSB_EXTRA_SET_H */ diff --git a/src/__internal/rbtree.h b/src/__internal/rbtree.h index c9db416..8564bc1 100644 --- a/src/__internal/rbtree.h +++ b/src/__internal/rbtree.h @@ -32,9 +32,9 @@ extern rbnode_t* libcdsb_rbtree_node_delete(rbnode_t** root, rbnode_t* node) extern void* libcdsb_rbtree_duplicate(const rbnode_t* s, void* (*node_duplicate)(void* src, void* parent, void* info), void* info) wur__ Nonnull__(1); extern int libcdsb_rbtree_compare (const rbnode_t* s0, const rbnode_t* s1, int (*node_compare)(const rbnode_t* s0, const rbnode_t* s1, void* info), void* info) pure__ wur__ Nonnull__(1,2); -extern hash_t libcdsb_rbtree_hash(const void* s, hash_t (*node_hash)(const void* s, void* info), void* info) pure__ wur__ Nonnull__(1,2); -extern size_t libcdsb_rbtree_size(const void* s) pure__ wur__ Nonnull__(1); -extern void libcdsb_rbtree_free(void* x, void (*node_free)(void* x, void* info), void* info) Nonnull__(1); +extern hash_t libcdsb_rbtree_hash(const void* s, hash_t (*node_hash)(const void* s, void* info), void* info) pure__ wur__ Nonnull__(1); +extern size_t libcdsb_rbtree_size(const void* s) pure__ wur__ Nonnull__(1); +extern void libcdsb_rbtree_free(void* x, void (*node_free)(void* x, void* info), void* info) Nonnull__(1); #define rbtree_duplicate libcdsb_rbtree_duplicate #define rbtree_compare libcdsb_rbtree_compare diff --git a/src/__internal/vnode.h b/src/__internal/vnode.h index c7a5d95..ad9a393 100644 --- a/src/__internal/vnode.h +++ b/src/__internal/vnode.h @@ -11,7 +11,7 @@ typedef union { void* ptr; bool b; str_t s; arr_t a; list_t l; - map_t m; set_t vs; + map_t m; set_t vs; dict_t vd; u8_t u8; u16_t u16; u32_t u32; u64_t u64; fl_t f; dbl_t d; ldbl_t ld; } var_t; diff --git a/src/extra-memory.c b/src/extra-memory.c index 72fdaab..0ff5783 100644 --- a/src/extra-memory.c +++ b/src/extra-memory.c @@ -55,6 +55,9 @@ char* libcdsb_strdup(const char* s) { void* x; size_t n; + if (is_null(s)) + return nullptr; + if ((x = malloc(n = strlen(s) + 1))) return memcpy(x, s, n); abort(); From 125153076d5496637807f1b3a179101d2420174a Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 21:10:54 +0300 Subject: [PATCH 41/42] Minor fixes --- include/extra/set.h | 6 +++--- include/set.h | 7 ++++--- src/dict/extra.c | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/extra/set.h b/include/extra/set.h index a34113b..42be364 100644 --- a/include/extra/set.h +++ b/include/extra/set.h @@ -8,8 +8,8 @@ #define vset_foreach(x, data, callback) libcdsb_vset_foreach(x, data, callback, 0) -extern bool libcdsb_vset_insert(vtype_set* x, const void* value, vtype type) Nonnull__(1); -extern int libcdsb_vset_find (vtype_set* x, const void* value, vtype type, void* data, vset_access_callback, bool cut) Nonnull__(1); -extern int libcdsb_vset_foreach(vtype_set* x, void* data, vset_access_callback, bool flush) Nonnull__(1,3); +extern bool libcdsb_vset_insert (vtype_set* x, const void* value, vtype type) Nonnull__(1); +extern int libcdsb_vset_find (vtype_set* x, const void* value, vtype type, void* data, vset_access_callback, bool cut) Nonnull__(1); +extern int libcdsb_vset_foreach(vtype_set* x, void* data, vset_access_callback, bool flush) Nonnull__(1,3); #endif /* LIBCDSB_EXTRA_SET_H */ diff --git a/include/set.h b/include/set.h index 2ea5c92..238c910 100644 --- a/include/set.h +++ b/include/set.h @@ -11,10 +11,11 @@ typedef int (*vset_access_callback)(const void* value, vtype type, void* data); extern void vset_init(vtype_set* x, vtype type) Nonnull__(1); -#define vset_remove(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, value)(x, value, 1) -#define vset_push(x, value) _LIBCDSB_Generic (libcdsb_vset, push, value)(x, value) +#define vset_pop(x, value, data, callback) _LIBCDSB_Generic (libcdsb_vset, find, value)(x, value, data, callback, 1) +#define vset_get(x, value, data, callback) _LIBCDSB_Generic (libcdsb_vset, find, value)(x, value, data, callback, 0) +#define vset_push(x, value) _LIBCDSB_Generic (libcdsb_vset, push, value)(x, value) -#define in_vset(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, value)(x, value, 0) +#define in_vset(x, value) (vset_get(&x, value, 0, 0, 0) == 0) extern bool libcdsb_vset_push_pointer(vtype_set* x, const void* value) Nonnull__(1); extern bool libcdsb_vset_push_cstring(vtype_set* x, const char* value) Nonnull__(1,2); diff --git a/src/dict/extra.c b/src/dict/extra.c index 8688703..5b4aba5 100644 --- a/src/dict/extra.c +++ b/src/dict/extra.c @@ -165,7 +165,7 @@ int libcdsb_dict_get(dict_t* x, const void* k, vtype t, void* _, dict_access_cal cmp = vtype_compare(k, t, key, c->key_type); if (cmp == 0) { - cmp = (callback) ? callback(key, t, vnode_peek(&c->value, c->val_type), c->val_type, _) : 0; + cmp = (callback) ? callback(key, c->key_type, vnode_peek(&c->value, c->val_type), c->val_type, _) : 0; if (cut) { c = dnode_delete(x->nodes + index, c); From 86928b4b221b16704c57a3482ab4e1e584673a05 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 21:11:07 +0300 Subject: [PATCH 42/42] Add examples --- examples/Makefile | 40 +++++++++++++++++++++++++++++++ examples/array.c | 37 ++++++++++++++++++++++++++++ examples/dict.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ examples/list.c | 51 +++++++++++++++++++++++++++++++++++++++ examples/map.c | 55 ++++++++++++++++++++++++++++++++++++++++++ examples/set.c | 38 +++++++++++++++++++++++++++++ examples/string.c | 42 ++++++++++++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/array.c create mode 100644 examples/dict.c create mode 100644 examples/list.c create mode 100644 examples/map.c create mode 100644 examples/set.c create mode 100644 examples/string.c diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..2d99f6b --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,40 @@ +# This software is licensed by the MIT License, see LICENSE file +# Copyright © 2022 Gregory Lirent + +######################################################################################################################## + +BUILD_PATH ?= ./bin + +######################################################################################################################## + +CC = clang +MKDIR = mkdir -p +RMRF = rm -rf +AR = ar crs +CP = cp + +######################################################################################################################## + +examples: modules +examples: $(addprefix $(BUILD_PATH)/,$(notdir $(basename $(wildcard ./*.c)))) + +$(BUILD_PATH)/%: ./%.c | $(BUILD_PATH)/ + $(CC) $^ ../bin/release/libcdsb.a ../modules/libunic/bin/libunic.a -o $@ $(CFLAGS) -O2 -Wall + + +$(BUILD_PATH)/: + $(MKDIR) $@ +$(BUILD_PATH)/obj/: | $(BUILD_PATH)/ + $(MKDIR) $@ + +clean: + $(RMRF) ./bin/ + cd ../ && $(MAKE) clean + +######################################################################################################################## + +FORCE: +modules: ../bin/release/libcdsb.a + +../bin/release/libcdsb.a: FORCE + cd ../ && $(MAKE) release diff --git a/examples/array.c b/examples/array.c new file mode 100644 index 0000000..29108c4 --- /dev/null +++ b/examples/array.c @@ -0,0 +1,37 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/array.h" + +typedef vtype_array arr_t; + +int print_value(void* value, ssize_t index, vtype type, void* data) { + const char *n = data; + vtype_int32 *v = value; + + assert(type == VTYPE_INT32); + + printf("%s %d (index: %ld)\n", n, *v, index); + + return 0; +} + +int main(int argc, char** argv) { + + arr_t arr; + + array_init(&arr, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + array_push_back(&arr, i); + } + + array_get_by_index(&arr, 13, "Get value:", print_value); + array_pop_by_index(&arr, 18, "Pop value:", print_value); + + array_foreach(&arr, "Foreach loop:", print_value); + + array_free(&arr); +} diff --git a/examples/dict.c b/examples/dict.c new file mode 100644 index 0000000..1e548a4 --- /dev/null +++ b/examples/dict.c @@ -0,0 +1,61 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include +#include "../include/extra/dict.h" + +typedef vtype_dict dict_t; + +int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) { + const char *n = data; + + switch (key_type) { + default: abort(); + + case VTYPE_INT32: + printf("%s %d: ", n, *(vtype_int32*)key); + break; + case VTYPE_FLOAT: + printf("%s %f: ", n, *(vtype_float*)key); + break; + } + + switch (value_type) { + default: abort(); + + case VTYPE_INT32: + printf("%d\n", *(vtype_int32*)value); + break; + case VTYPE_FLOAT: + printf("%f\n", *(vtype_float*)value); + break; + } + + return 0; +} + +int main(int argc, char** argv) { + + dict_t dict; + vtype_float fl = 0.0; + + dict_init(&dict); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + dict_update(&dict, (vtype_float)fl, (vtype_int32)i); + } else { + dict_update(&dict, (vtype_int32)i, (vtype_float)fl); + } + fl += 0.05; + } + + dict_get(&dict, 14, "Get value:", print_value); + dict_pop(&dict, 0.25, "Pop value:", print_value); + + dict_foreach(&dict, "Foreach loop:", print_value); + + dict_free(&dict); +} diff --git a/examples/list.c b/examples/list.c new file mode 100644 index 0000000..449ba2d --- /dev/null +++ b/examples/list.c @@ -0,0 +1,51 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/list.h" + +typedef vtype_list list_t; + + +int print_value(void* value, ssize_t index, vtype type, void* data) { + const char *n = data; + + switch (type) { + default: abort(); + + case VTYPE_INT32: + printf("%s %d (index: %ld)\n", n, *(vtype_int32*)value, index); + break; + case VTYPE_FLOAT: + printf("%s %f (index: %ld)\n", n, *(vtype_float*)value, index); + break; + } + + return 0; +} + + +int main(int argc, char** argv) { + + list_t list; + vtype_float fl = 0.0; + + list_init(&list); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + list_push_back(&list, (vtype_int32)i); + } else { + list_push_back(&list, (vtype_float)fl); + fl += 0.05; + } + } + + list_get_by_index(&list, 13, "Get value:", print_value); + list_pop_by_index(&list, 18, "Pop value:", print_value); + + list_foreach(&list, "Foreach loop:", print_value); + + list_free(&list); +} diff --git a/examples/map.c b/examples/map.c new file mode 100644 index 0000000..115dc2e --- /dev/null +++ b/examples/map.c @@ -0,0 +1,55 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include +#include "../include/extra/map.h" + +typedef vtype_map map_t; + +int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) { + const char *n = data; + vtype_int32 *k = (void*)key; + + assert(key_type == VTYPE_INT32); + + printf("%s %d: ", n, *k); + + switch (value_type) { + default: abort(); + + case VTYPE_INT32: + printf("%d\n", *(vtype_int32*)value); + break; + case VTYPE_FLOAT: + printf("%f\n", *(vtype_float*)value); + break; + } + + return 0; +} + +int main(int argc, char** argv) { + + map_t map; + vtype_float fl = 0.0; + + map_init(&map, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + map_update(&map, i, (vtype_int32)i); + } else { + map_update(&map, i, (vtype_float)fl); + fl += 0.05; + } + } + + map_get(&map, 13, "Get value:", print_value); + map_pop(&map, 18, "Pop value:", print_value); + + map_foreach(&map, "Foreach loop:", print_value); + + map_free(&map); +} diff --git a/examples/set.c b/examples/set.c new file mode 100644 index 0000000..9b9b4d1 --- /dev/null +++ b/examples/set.c @@ -0,0 +1,38 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/set.h" + +typedef vtype_set vset_t; + +int print_value(const void* value, vtype type, void* data) { + const char *n = data; + vtype_int32 *v = (void*)value; + + assert(type == VTYPE_INT32); + + printf("%s %d\n", n, *v); + + return 0; +} + + +int main(int argc, char** argv) { + + vset_t set; + + vset_init(&set, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + vset_push(&set, i); + } + + vset_get(&set, 13, "Get value:", print_value); + vset_pop(&set, 18, "Pop value:", print_value); + + vset_foreach(&set, "Foreach loop:", print_value); + + vset_free(&set); +} diff --git a/examples/string.c b/examples/string.c new file mode 100644 index 0000000..4072af5 --- /dev/null +++ b/examples/string.c @@ -0,0 +1,42 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../include/extra/string.h" +#include "../include/extra/array.h" + +typedef vtype_string str_t; +typedef vtype_array arr_t; + +int main(int argc, char** argv) { + + str_t str; + + string_init(&str, "sed ut perspiciatis"); + + string_concat(&str, ", Unde omnis iste natus error sit voluptatem accusantium doloremque laudantium"); + string_concat(&str, ", Totam rem aperiam eaque ipsa"); + string_concat(&str, ", Quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt"); + string_concat(&str, ", Explicabo."); + + string_capitalize(&str); + + string_reverse(&str); + + printf("%s\n", str.buffer); + + arr_t parts = string_split(&str, ',', -1); + + printf("%lu\n", array_size(&parts)); + + for (size_t i = 0; i < array_size(&parts); ++i) { + str_t* value = array_at(&parts, i); + + string_trim_spaces(value); + + printf("%s (%lu)\n", value->buffer, string_nmemb(value)); + } + + array_free(&parts); + string_free(&str); +}