Update string tests

This commit is contained in:
Gregory Lirent 2022-06-10 21:42:24 +03:00
parent 10a22d2f26
commit a44a6d701e
6 changed files with 160 additions and 138 deletions

View File

@ -3,53 +3,13 @@
#include "plug.h" #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) { int main(int argc, char** argv) {
test_init(argc, 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); x = string_random(30);
string_print(&x, "(part 1)"); string_print(&x, "(part 1)");
@ -77,8 +37,8 @@ int main(int argc, char** argv) {
string_to_lower(&x); string_to_lower(&x);
string_to_upper(&y); string_to_upper(&y);
string_print_compare(&x, &y); visual_compare(&x, &y);
string_print_case_compare(&x, &y); visual_case_compare(&x, &y);
string_reverse(&y); string_reverse(&y);
string_capitalize(&y); string_capitalize(&y);
@ -87,4 +47,31 @@ int main(int argc, char** argv) {
string_free(&x); string_free(&x);
string_free(&y); 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);
} }

View File

@ -9,65 +9,14 @@
#include "../../include/test.h" #include "../../include/test.h"
#include "../../include/time.h" #include "../../include/time.h"
vtype_array* array_duplicate (const vtype_array* x) { return 0; } extern char* random_ascii_cstring(size_t size);
vtype_list* list_duplicate (const vtype_list* x) { return 0; } extern char* random_utf8_cstring(size_t size);
vtype_map* map_duplicate (const vtype_map* x) { return 0; } extern void string_concat_random(str_t* x, unsigned int n);
vtype_set* vset_duplicate (const vtype_set* x) { return 0; } extern void string_replace_random(str_t* x, unsigned int n);
void array_free (vtype_array* x) {} extern void string_info(str_t* x);
void list_free (vtype_list* x) {} extern void string_print(const str_t* x, const char* prefix);
void map_free (vtype_map* x) {} extern str_t string_random(unsigned int n);
void vset_free (vtype_set* x) {}
int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); } extern void visual_compare(const str_t* s0, const str_t* s1);
int list_compare (const vtype_list* s0, const vtype_list* s1) { return random_int8(); } extern void visual_case_compare(const str_t* s0, const str_t* s1);
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;
}

48
tests/src/string/src/io.c Normal file
View File

@ -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);
}

View File

@ -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(); }

View File

@ -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);
}

View File

@ -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);
}