#include "c-libs/string-view.h" #include #include Test(sv, construction) { const char *data = "abcdefg"; size_t len = strlen(data); StringView sv1 = sv_new(data); StringView sv2 = sv_new_sized(data, 2); cr_assert(sv1.data == data); cr_assert(sv1.end == data + len); cr_assert(sv2.data == data); cr_assert(sv2.end == data + 2); } Test(sv, basic_manipulation) { const char *data = "abcdefg"; size_t len = strlen(data); StringView sv1 = sv_new(data); char *clone = sv_clone(sv1); cr_assert(sv_len(sv1) == len); 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); }