libcdsb/tests/src/string/main.c

91 lines
2.2 KiB
C
Raw Normal View History

2022-06-02 22:23:01 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "plug.h"
2022-06-04 22:17:26 +03:00
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);
}
2022-06-04 00:30:23 +03:00
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);
2022-06-03 19:29:46 +03:00
}
2022-06-04 00:30:23 +03:00
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) ? "<" : ">";
2022-06-03 19:29:46 +03:00
2022-06-04 00:30:23 +03:00
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);
2022-06-03 19:29:46 +03:00
}
2022-06-02 22:23:01 +03:00
int main(int argc, char** argv) {
test_init(argc, argv);
2022-06-04 00:30:23 +03:00
vtype_string x, y;
2022-06-03 19:29:46 +03:00
2022-06-04 00:30:23 +03:00
x = string_random(30);
2022-06-03 19:29:46 +03:00
string_print(&x, "(part 1)");
string_concat(&x, '\n');
2022-06-04 00:30:23 +03:00
string_concat_random(&x, 30);
2022-06-03 19:29:46 +03:00
{
2022-06-04 00:30:23 +03:00
char* repl = random_utf8_cstring(30);
void* hack = string_at(&x, 31);
2022-06-03 19:29:46 +03:00
string_print((void*)&hack, "(part 2)");
2022-06-04 00:30:23 +03:00
string_print((void*)&repl, "(part 2 replaced)");
string_replace(&x, hack, repl, -1);
free(repl);
2022-06-03 19:29:46 +03:00
}
string_info(&x);
string_print(&x, "concatenated");
2022-06-04 00:30:23 +03:00
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);
2022-06-03 19:29:46 +03:00
2022-06-04 00:30:23 +03:00
string_print(&y, "reversed & capitalized");
2022-06-03 19:29:46 +03:00
string_free(&x);
2022-06-04 00:30:23 +03:00
string_free(&y);
2022-06-02 22:23:01 +03:00
}