Update string tests

This commit is contained in:
Gregory Lirent 2022-06-04 00:30:23 +03:00
parent a01bc2c7c7
commit f178825ce6
3 changed files with 102 additions and 32 deletions

View File

@ -3,59 +3,65 @@
#include "plug.h" #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) { puts("\e[36mStrings comparsion:\e[m\n");
vtype_string x; printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer);
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) { static void string_print_case_compare(const vtype_string* s0, const vtype_string* s1) {
char* v; int c = string_case_compare(s0, s1);
char* m;
if (c == 0) m = "==";
else m = (c < 0) ? "<" : ">";
if (random_boolean()) { puts("\e[36mStrings case insensitive comparsion:\e[m\n");
v = random_utf8_cstring(n); printf("\e[33m\"%s\"\e[m \e[31m%s\e[m \e[33m\"%s\"\e[m\n", s0->buffer, m, s1->buffer);
} 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) { int main(int argc, char** argv) {
test_init(argc, 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_print(&x, "(part 1)");
string_concat(&x, '\n'); 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*)&hack, "(part 2)");
string_print((void*)&repl, "(part 2 replaced)");
string_replace(&x, hack, repl, -1);
free(repl);
} }
string_info(&x); string_info(&x);
string_print(&x, "concatenated"); 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(&x);
string_free(&y);
} }

View File

@ -47,3 +47,47 @@ static char* random_utf8_cstring(size_t size) {
*p = 0; *p = 0;
return v; 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);
}

20
tests/src/string/trim.c Normal file
View File

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