test(sv): add remaining tests

This commit is contained in:
2026-04-12 20:33:35 +02:00
parent 13cf7946bf
commit 5312fcffaa
2 changed files with 93 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
#include "c-libs/string-view.h"
#include <criterion/criterion.h>
#include <ctype.h>
Test(sv, construction) {
const char *data = "abcdefg";
@@ -14,7 +15,7 @@ Test(sv, construction) {
cr_assert(sv2.end == data + 2);
}
Test(sv, inspection) {
Test(sv, basic_manipulation) {
const char *data = "abcdefg";
size_t len = strlen(data);
@@ -25,6 +26,87 @@ Test(sv, inspection) {
cr_assert_str_eq(data, clone);
cr_assert(sv_eq(sv1, sv1));
cr_assert_not(sv_eq(sv1, sv_drop(sv1, 1)));
cr_assert(sv_eq(sv_new("bcdefg"), sv_drop(sv1, 1)));
cr_assert(sv_eq(sv_new("efg"), sv_drop(sv1, 4)));
cr_assert(sv_eq(sv_new(""), sv_drop(sv1, 100)));
cr_assert(sv_eq(sv_new("abc"), sv_take(sv1, 3)));
cr_assert(sv_eq(sv_new(""), sv_take(sv1, 0)));
cr_assert(sv_eq(sv_new(data), sv_take(sv1, 100)));
cr_assert(sv_eq(sv_new("abcd"), sv_shrink(sv1, 3)));
cr_assert(sv_eq(sv_new(data), sv_shrink(sv1, 0)));
cr_assert(sv_eq(sv_new(""), sv_shrink(sv1, 100)));
free(clone);
}
Test(sv, advanced_manipulation) {
const char *data = "a,b,c,d,e,f,g";
StringView sv1 = sv_new(data);
cr_assert(sv_eq(sv_new("e,f,g"), sv_seek(sv1, 'e')));
cr_assert(sv_eq(sv_new("f,g"), sv_seek(sv1, 'f')));
cr_assert(sv_eq(sv_new(data), sv_seek(sv1, 'a')));
cr_assert(sv_eq(sv_new(""), sv_seek(sv1, 'x')));
cr_assert(sv_eq(sv_new("a"), sv_seek_back(sv1, 'a')));
cr_assert(sv_eq(sv_new("a,b"), sv_seek_back(sv1, 'b')));
cr_assert(sv_eq(sv_new(""), sv_seek_back(sv1, 'x')));
cr_assert(sv_eq(sv_new(",b,c,d,e,f,g"), sv_trim_front(sv1, isalnum)));
cr_assert(sv_eq(sv_new("a,b,c,d,e,f,"), sv_trim_back(sv1, isalnum)));
cr_assert(sv_eq(sv_new(",b,c,d,e,f,"), sv_trim(sv1, isalnum)));
}
Test(sv, splitting) {
const char *data = "Hello : world : simple : splits";
StringView sv1 = sv_new(data);
cr_assert(sv_eq(sv_new("Hello "), sv_split_at(&sv1, ':')));
cr_assert(sv_eq(sv_new(" world : simple : splits"), sv1));
cr_assert(sv_eq(sv_new(" world "), sv_split_at(&sv1, ':')));
cr_assert(sv_eq(sv_new(" simple : splits"), sv1));
cr_assert(sv_eq(sv_new(" simple "), sv_split_at(&sv1, ':')));
cr_assert(sv_eq(sv_new(" splits"), sv1));
cr_assert(sv_eq(sv_new(" splits"), sv_split_at(&sv1, ':')));
cr_assert(sv_eq(sv_new(""), sv1));
sv1 = sv_new(data);
sv_split_at(&sv1, '?');
cr_assert(sv_len(sv1) == 0);
sv1 = sv_new(data);
StringView sep = sv_new(" : ");
cr_assert(sv_eq(sv_new("Hello"), sv_split_at_sv(&sv1, sep)));
cr_assert(sv_eq(sv_new("world : simple : splits"), sv1));
cr_assert(sv_eq(sv_new("world"), sv_split_at_sv(&sv1, sep)));
cr_assert(sv_eq(sv_new("simple : splits"), sv1));
cr_assert(sv_eq(sv_new("simple"), sv_split_at_sv(&sv1, sep)));
cr_assert(sv_eq(sv_new("splits"), sv1));
cr_assert(sv_eq(sv_new("splits"), sv_split_at_sv(&sv1, sep)));
cr_assert(sv_eq(sv_new(""), sv1));
}
Test(sv, aux) {
StringView elems[4] = {
sv_new("This"),
sv_new("Is"),
sv_new("A"),
sv_new("Test"),
};
char *out = sv_concat_with_sep(elems, 4, sv_new(" - "));
cr_assert_str_eq(out, "This - Is - A - Test");
free(out);
}