From a44a6d701ee02f0cb9bab5b003b45e8e59e1589c Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 10 Jun 2022 21:42:24 +0300 Subject: [PATCH] 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); -}