Thu, 28 Dec 2023 20:37:53 +0100
migrate string tests - relates to #342
tests/Makefile | file | annotate | diff | comparison | revisions | |
tests/test_string.c | file | annotate | diff | comparison | revisions | |
tests/test_string.cpp | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
--- a/tests/Makefile Thu Dec 28 19:17:45 2023 +0100 +++ b/tests/Makefile Thu Dec 28 20:37:53 2023 +0100 @@ -23,11 +23,11 @@ include ../config.mk -CFLAGS += -I../src +CFLAGS += -I../src -Wno-clobbered TEST_DIR=$(build_dir)/tests -SRC = util_allocator.c test_utils.c test_hash_key.c ucxtest.c +SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c ucxtest.c OBJ_EXT=.o OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) @@ -59,6 +59,10 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +$(TEST_DIR)/test_string$(OBJ_EXT): test_string.c + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + $(TEST_DIR)/test_utils$(OBJ_EXT): test_utils.c ../src/cx/test.h \ ../src/cx/utils.h ../src/cx/common.h ../src/cx/buffer.h \ ../src/cx/allocator.h ../src/szmul.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_string.c Thu Dec 28 20:37:53 2023 +0100 @@ -0,0 +1,986 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "cx/test.h" +#include "util_allocator.h" + +#include "cx/string.h" + +#define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ + #str " is not zero terminated") + +CX_TEST(test_string_construct) { + cxstring s1 = CX_STR("1234"); + cxstring s2 = cx_strn("abcd", 2); + cxmutstr s3 = cx_mutstr((char *) "1234"); + cxmutstr s4 = cx_mutstrn((char *) "abcd", 2); + CX_TEST_DO { + CX_TEST_ASSERT(s1.length == 4); + CX_TEST_ASSERT(strncmp(s1.ptr, "1234", 4) == 0); + CX_TEST_ASSERT(s2.length == 2); + CX_TEST_ASSERT(strncmp(s2.ptr, "ab", 2) == 0); + CX_TEST_ASSERT(s3.length == 4); + CX_TEST_ASSERT(strncmp(s3.ptr, "1234", 4) == 0); + CX_TEST_ASSERT(s4.length == 2); + CX_TEST_ASSERT(strncmp(s4.ptr, "ab", 2) == 0); + } +} + +CX_TEST(test_strfree) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + CX_TEST_DO { + char *test = cxMalloc(alloc, 16); + cxmutstr str = cx_mutstrn(test, 16); + CX_TEST_ASSERT(str.ptr == test); + CX_TEST_ASSERT(str.length == 16); + cx_strfree_a(alloc, &str); + CX_TEST_ASSERT(str.ptr == NULL); + CX_TEST_ASSERT(str.length == 0); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_strdup) { + cxstring str = CX_STR("test"); + cxmutstr dup = cx_strdup(str); + CX_TEST_DO { + CX_TEST_ASSERT(dup.length == str.length); + CX_TEST_ASSERT(0 == strcmp(dup.ptr, str.ptr)); + ASSERT_ZERO_TERMINATED(dup); + } + cx_strfree(&dup); +} + +CX_TEST(test_strdup_shortened) { + cxstring str = CX_STR("test"); + str.length = 2; + cxmutstr dup = cx_strdup(str); + CX_TEST_DO { + CX_TEST_ASSERT(dup.length == str.length); + CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te")); + ASSERT_ZERO_TERMINATED(dup); + } + cx_strfree(&dup); +} + +CX_TEST(test_strlen) { + cxstring s1 = CX_STR("1234"); + cxstring s2 = CX_STR(".:.:."); + cxstring s3 = CX_STR("X"); + CX_TEST_DO { + size_t len0 = cx_strlen(0); + size_t len1 = cx_strlen(1, s1); + size_t len2 = cx_strlen(2, s1, s2); + size_t len3 = cx_strlen(3, s1, s2, s3); + + CX_TEST_ASSERT(len0 == 0); + CX_TEST_ASSERT(len1 == 4); + CX_TEST_ASSERT(len2 == 9); + CX_TEST_ASSERT(len3 == 10); + } +} + +CX_TEST(test_strsubs) { + cxstring str = CX_STR("A test string"); + + CX_TEST_DO { + cxstring sub = cx_strsubs(str, 0); + CX_TEST_ASSERT(0 == cx_strcmp(sub, str)); + + sub = cx_strsubs(str, 2); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test string"))); + + sub = cx_strsubs(str, 7); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string"))); + + sub = cx_strsubs(str, 15); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR(""))); + + sub = cx_strsubsl(str, 2, 4); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test"))); + + sub = cx_strsubsl(str, 7, 3); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("str"))); + + sub = cx_strsubsl(str, 7, 20); + CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string"))); + + // just for coverage, call the _m variant + cxmutstr m = cx_strsubs_m(cx_mutstrn(NULL, 0), 0); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR(""))); + } +} + +CX_TEST(test_strchr) { + cxstring str = CX_STR("I will find you - and I will kill you"); + + CX_TEST_DO { + cxstring notfound = cx_strchr(str, 'x'); + CX_TEST_ASSERT(notfound.length == 0); + + cxstring result = cx_strchr(str, 'w'); + CX_TEST_ASSERT(result.length == 35); + CX_TEST_ASSERT(0 == strcmp(result.ptr, "will find you - and I will kill you")); + + // just for coverage, call the _m variant + cxmutstr m = cx_strchr_m(cx_mutstrn(NULL, 0), 'a'); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR(""))); + } +} + +CX_TEST(test_strrchr) { + cxstring str = CX_STR("I will find you - and I will kill you"); + + CX_TEST_DO { + cxstring notfound = cx_strrchr(str, 'x'); + CX_TEST_ASSERT(notfound.length == 0); + + cxstring result = cx_strrchr(str, 'w'); + CX_TEST_ASSERT(result.length == 13); + CX_TEST_ASSERT(0 == strcmp(result.ptr, "will kill you")); + + // just for coverage, call the _m variant + cxmutstr m = cx_strrchr_m(cx_mutstrn(NULL, 0), 'a'); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR(""))); + } +} + +CX_TEST(test_strstr) { + cxstring str = CX_STR("find the match in this string"); + cxstring longstr = CX_STR( + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl" + "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx" + "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij" + "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "wxyz1234567890"); + cxstring longstrpattern = CX_STR( + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + ); + cxstring longstrresult = CX_STR( + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "abababababababababababababababababababababababababababababababab" + "wxyz1234567890" + ); + + CX_TEST_DO { + cxstring notfound = cx_strstr(str, CX_STR("no match")); + CX_TEST_ASSERT(notfound.length == 0); + + cxstring result = cx_strstr(str, CX_STR("match")); + CX_TEST_ASSERT(result.length == 20); + CX_TEST_ASSERT(0 == strcmp(result.ptr, "match in this string")); + + result = cx_strstr(str, CX_STR("")); + CX_TEST_ASSERT(result.length == str.length); + CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr)); + + result = cx_strstr(longstr, longstrpattern); + CX_TEST_ASSERT(result.length == longstrresult.length); + CX_TEST_ASSERT(0 == strcmp(result.ptr, longstrresult.ptr)); + + // just for coverage, call the _m variant + cxmutstr mstr = cx_strdup(longstr); + cxmutstr m = cx_strstr_m(mstr, longstrpattern); + CX_TEST_ASSERT(m.length == longstrresult.length); + CX_TEST_ASSERT(0 == strcmp(m.ptr, longstrresult.ptr)); + cx_strfree(&mstr); + } +} + +CX_TEST(test_strcmp) { + cxstring str = CX_STR("compare this"); + CX_TEST_DO { + CX_TEST_ASSERT(0 == cx_strcmp(CX_STR(""), CX_STR(""))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(str, CX_STR("compare this"))); + CX_TEST_ASSERT(0 != cx_strcmp(str, CX_STR("Compare This"))); + CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare tool"))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare shit"))); + CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare this not"))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare"))); + + cxstring str2 = CX_STR("Compare This"); + CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2)); + str2 = CX_STR("compare this"); + CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2)); + } +} + +CX_TEST(test_strcasecmp) { + cxstring str = CX_STR("compare this"); + CX_TEST_DO { + CX_TEST_ASSERT(0 == cx_strcasecmp(CX_STR(""), CX_STR(""))); + CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("compare this"))); + CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("Compare This"))); + CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare tool"))); + CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare shit"))); + CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare this not"))); + CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare"))); + + cxstring str2 = CX_STR("Compare This"); + CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2)); + str2 = CX_STR("Compare Tool"); + CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2)); + } +} + +CX_TEST(test_strcat) { + cxstring s1 = CX_STR("12"); + cxstring s2 = CX_STR("34"); + cxstring s3 = CX_STR("56"); + cxstring sn = {NULL, 0}; + + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + CX_TEST_DO { + cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t1), CX_STR("1234"))); + ASSERT_ZERO_TERMINATED(t1); + cx_strfree_a(alloc, &t1); + + cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t2), CX_STR("123456"))); + ASSERT_ZERO_TERMINATED(t2); + cx_strfree_a(alloc, &t2); + + cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t3), CX_STR("123456"))); + ASSERT_ZERO_TERMINATED(t3); + cx_strfree_a(alloc, &t3); + + cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t4), CX_STR(""))); + ASSERT_ZERO_TERMINATED(t4); + cx_strfree_a(alloc, &t4); + + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + + // use the macro + cxmutstr t5 = cx_strcat(3, s3, s1, s2); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t5), CX_STR("561234"))); + ASSERT_ZERO_TERMINATED(t5); + cx_strfree(&t5); + + // use an initial string + cxmutstr t6 = cx_strdup(CX_STR("Hello")); + t6 = cx_strcat_m(t6, 2, CX_STR(", "), CX_STR("World!")); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t6), CX_STR("Hello, World!"))); + ASSERT_ZERO_TERMINATED(t6); + cx_strfree(&t6); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_strsplit) { + cxstring test = CX_STR("this,is,a,csv,string"); + size_t capa = 8; + cxstring list[8]; + size_t n; + CX_TEST_DO { + // special case: empty string + n = cx_strsplit(test, CX_STR(""), capa, list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + + // no delimiter occurrence + n = cx_strsplit(test, CX_STR("z"), capa, list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + + // partially matching delimiter + n = cx_strsplit(test, CX_STR("is,not"), capa, list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + + // matching single-char delimiter + n = cx_strsplit(test, CX_STR(","), capa, list); + CX_TEST_ASSERT(n == 5); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string"))); + + // matching multi-char delimiter + n = cx_strsplit(test, CX_STR("is"), capa, list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(","))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string"))); + + // bounded list using single-char delimiter + n = cx_strsplit(test, CX_STR(","), 3, list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string"))); + + // bounded list using multi-char delimiter + n = cx_strsplit(test, CX_STR("is"), 2, list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string"))); + + // start with delimiter + n = cx_strsplit(test, CX_STR("this"), capa, list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string"))); + + // end with delimiter + n = cx_strsplit(test, CX_STR("string"), capa, list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + + + // end with delimiter exceed bound + n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,"))); + + // exact match + n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + + // string to be split is only substring + n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + + // subsequent encounter of delimiter (the string between is empty) + n = cx_strsplit(test, CX_STR("is,"), capa, list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string"))); + + // call the _m variant just for coverage + cxmutstr mtest = cx_strdup(test); + cxmutstr mlist[4]; + n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string"))); + cx_strfree(&mtest); + } +} + +CX_TEST(test_strsplit_a) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + cxstring test = CX_STR("this,is,a,csv,string"); + size_t capa = 8; + cxstring *list; + size_t n; + CX_TEST_DO { + // special case: empty string + n = cx_strsplit_a(alloc, test, CX_STR(""), capa, &list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + cxFree(alloc, list); + + // no delimiter occurrence + n = cx_strsplit_a(alloc, test, CX_STR("z"), capa, &list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + cxFree(alloc, list); + + // partially matching delimiter + n = cx_strsplit_a(alloc, test, CX_STR("is,not"), capa, &list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + cxFree(alloc, list); + + // matching single-char delimiter + n = cx_strsplit_a(alloc, test, CX_STR(","), capa, &list); + CX_TEST_ASSERT(n == 5); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string"))); + cxFree(alloc, list); + + // matching multi-char delimiter + n = cx_strsplit_a(alloc, test, CX_STR("is"), capa, &list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(","))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string"))); + cxFree(alloc, list); + + // bounded list using single-char delimiter + n = cx_strsplit_a(alloc, test, CX_STR(","), 3, &list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string"))); + cxFree(alloc, list); + + // bounded list using multi-char delimiter + n = cx_strsplit_a(alloc, test, CX_STR("is"), 2, &list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string"))); + cxFree(alloc, list); + + // start with delimiter + n = cx_strsplit_a(alloc, test, CX_STR("this"), capa, &list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string"))); + cxFree(alloc, list); + + // end with delimiter + n = cx_strsplit_a(alloc, test, CX_STR("string"), capa, &list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + cxFree(alloc, list); + + // end with delimiter exceed bound + n = cx_strsplit_a(alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,"))); + cxFree(alloc, list); + + // exact match + n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string"), capa, &list); + CX_TEST_ASSERT(n == 2); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + cxFree(alloc, list); + + // string to be split is only substring + n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list); + CX_TEST_ASSERT(n == 1); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], test)); + cxFree(alloc, list); + + // subsequent encounter of delimiter (the string between is empty) + n = cx_strsplit_a(alloc, test, CX_STR("is,"), capa, &list); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string"))); + cxFree(alloc, list); + + // call the _m variant just for coverage + cxmutstr mtest = cx_strdup(test); + cxmutstr *mlist; + n = cx_strsplit_ma(alloc, mtest, CX_STR("is,"), 4, &mlist); + CX_TEST_ASSERT(n == 3); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th"))); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string"))); + cxFree(alloc, mlist); + cx_strfree(&mtest); + + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_strtrim) { + cxstring t1 = cx_strtrim(CX_STR(" ein test \t ")); + cxstring t2 = cx_strtrim(CX_STR("abc")); + cxstring t3 = cx_strtrim(CX_STR(" 123")); + cxstring t4 = cx_strtrim(CX_STR("xyz ")); + cxstring t5 = cx_strtrim(CX_STR(" ")); + cxstring empty = cx_strtrim(CX_STR("")); + + CX_TEST_DO { + CX_TEST_ASSERT(0 == cx_strcmp(t1, CX_STR("ein test"))); + CX_TEST_ASSERT(0 == cx_strcmp(t2, CX_STR("abc"))); + CX_TEST_ASSERT(0 == cx_strcmp(t3, CX_STR("123"))); + CX_TEST_ASSERT(0 == cx_strcmp(t4, CX_STR("xyz"))); + CX_TEST_ASSERT(0 == cx_strcmp(t5, CX_STR(""))); + CX_TEST_ASSERT(0 == cx_strcmp(empty, CX_STR(""))); + + // call the _m variant just for coverage + cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m1), CX_STR("ein test"))); + } +} + +CX_TEST(test_strprefix) { + cxstring str = CX_STR("test my prefix and my suffix"); + cxstring empty = CX_STR(""); + CX_TEST_DO { + CX_TEST_ASSERT(!cx_strprefix(empty, CX_STR("pref"))); + CX_TEST_ASSERT(cx_strprefix(str, empty)); + CX_TEST_ASSERT(cx_strprefix(empty, empty)); + CX_TEST_ASSERT(cx_strprefix(str, CX_STR("test "))); + CX_TEST_ASSERT(!cx_strprefix(str, CX_STR("8-) fsck "))); + } +} + +CX_TEST(test_strsuffix) { + cxstring str = CX_STR("test my prefix and my suffix"); + cxstring empty = CX_STR(""); + CX_TEST_DO { + CX_TEST_ASSERT(!cx_strsuffix(empty, CX_STR("suf"))); + CX_TEST_ASSERT(cx_strsuffix(str, empty)); + CX_TEST_ASSERT(cx_strsuffix(empty, empty)); + CX_TEST_ASSERT(cx_strsuffix(str, CX_STR("fix"))); + CX_TEST_ASSERT(!cx_strsuffix(str, CX_STR("fox"))); + } +} + +CX_TEST(test_strcaseprefix) { + cxstring str = CX_STR("test my prefix and my suffix"); + cxstring empty = CX_STR(""); + CX_TEST_DO { + CX_TEST_ASSERT(!cx_strcaseprefix(empty, CX_STR("pREf"))); + CX_TEST_ASSERT(cx_strcaseprefix(str, empty)); + CX_TEST_ASSERT(cx_strcaseprefix(empty, empty)); + CX_TEST_ASSERT(cx_strcaseprefix(str, CX_STR("TEST "))); + CX_TEST_ASSERT(!cx_strcaseprefix(str, CX_STR("8-) fsck "))); + } +} + +CX_TEST(test_strcasesuffix) { + cxstring str = CX_STR("test my prefix and my suffix"); + cxstring empty = CX_STR(""); + CX_TEST_DO { + CX_TEST_ASSERT(!cx_strcasesuffix(empty, CX_STR("sUf"))); + CX_TEST_ASSERT(cx_strcasesuffix(str, empty)); + CX_TEST_ASSERT(cx_strcasesuffix(empty, empty)); + CX_TEST_ASSERT(cx_strcasesuffix(str, CX_STR("FIX"))); + CX_TEST_ASSERT(!cx_strcasesuffix(str, CX_STR("fox"))); + } +} + +CX_TEST(test_strreplace) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + cxstring str = CX_STR("test ababab string aba"); + cxstring longstr = CX_STR( + "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd"); + cxstring notrail = CX_STR("test abab"); + cxstring empty = CX_STR(""); + cxstring astr = CX_STR("aaaaaaaaaa"); + cxstring csstr = CX_STR("test AB ab TEST xyz"); + + cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger")); + char const *expected = "test muchlongerab string aba"; + + cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2); + char const *expectedn = "test ccab string aba"; + + cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z")); + char const *longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd"; + + cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z")); + char const *notrailexpect = "test zz"; + + cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello")); + char const *eqexpect = "hello"; + + cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty + cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty); + char const *emptyexpect2 = "test ab string aba"; + + cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST ")); + char const *preexpected = "TEST ababab string aba"; + + cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1); + char const *an1expected = "xaaaaaaaaa"; + + cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4); + char const *an4expected = "xxxxaaaaaa"; + + cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9); + char const *an9expected = "xxxxxxxxxa"; + + cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10); + char const *an10expected = "xxxxxxxxxx"; + + CX_TEST_DO { + cxmutstr repl1_a = cx_strreplace_a(alloc, csstr, CX_STR("AB"), CX_STR("*")); + char const *expeced1_a = "test * ab TEST xyz"; + + cxmutstr repl2_a = cx_strreplace_a(alloc, csstr, CX_STR("test"), CX_STR("TEST")); + char const *expected2_a = "TEST AB ab TEST xyz"; + + CX_TEST_ASSERT(repl.ptr != str.ptr); + ASSERT_ZERO_TERMINATED(repl); + CX_TEST_ASSERT(0 == strcmp(repl.ptr, expected)); + ASSERT_ZERO_TERMINATED(repln); + CX_TEST_ASSERT(0 == strcmp(repln.ptr, expectedn)); + ASSERT_ZERO_TERMINATED(longrepl); + CX_TEST_ASSERT(0 == strcmp(longrepl.ptr, longexpect)); + ASSERT_ZERO_TERMINATED(replnotrail); + CX_TEST_ASSERT(0 == strcmp(replnotrail.ptr, notrailexpect)); + ASSERT_ZERO_TERMINATED(repleq); + CX_TEST_ASSERT(0 == strcmp(repleq.ptr, eqexpect)); + ASSERT_ZERO_TERMINATED(replempty1); + CX_TEST_ASSERT(0 == strcmp(replempty1.ptr, "")); + ASSERT_ZERO_TERMINATED(replempty2); + CX_TEST_ASSERT(0 == strcmp(replempty2.ptr, emptyexpect2)); + ASSERT_ZERO_TERMINATED(replpre); + CX_TEST_ASSERT(0 == strcmp(replpre.ptr, preexpected)); + ASSERT_ZERO_TERMINATED(replan1); + CX_TEST_ASSERT(0 == strcmp(replan1.ptr, an1expected)); + ASSERT_ZERO_TERMINATED(replan4); + CX_TEST_ASSERT(0 == strcmp(replan4.ptr, an4expected)); + ASSERT_ZERO_TERMINATED(replan9); + CX_TEST_ASSERT(0 == strcmp(replan9.ptr, an9expected)); + ASSERT_ZERO_TERMINATED(replan10); + CX_TEST_ASSERT(0 == strcmp(replan10.ptr, an10expected)); + ASSERT_ZERO_TERMINATED(repl1_a); + CX_TEST_ASSERT(0 == strcmp(repl1_a.ptr, expeced1_a)); + ASSERT_ZERO_TERMINATED(repl2_a); + CX_TEST_ASSERT(0 == strcmp(repl2_a.ptr, expected2_a)); + + cx_strfree_a(alloc, &repl1_a); + cx_strfree_a(alloc, &repl2_a); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + + cx_strfree(&repl); + cx_strfree(&repln); + cx_strfree(&longrepl); + cx_strfree(&replnotrail); + cx_strfree(&repleq); + cx_strfree(&replempty1); + cx_strfree(&replempty2); + cx_strfree(&replpre); + cx_strfree(&replan1); + cx_strfree(&replan4); + cx_strfree(&replan9); + cx_strfree(&replan10); + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_strupper) { + cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); + CX_TEST_DO { + cx_strupper(str); + CX_TEST_ASSERT(0 == strcmp(str.ptr, "THIS 1S @ TE$T")); + } + cx_strfree(&str); +} + +CX_TEST(test_strlower) { + cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); + CX_TEST_DO { + cx_strlower(str); + CX_TEST_ASSERT(0 == strcmp(str.ptr, "this 1s @ te$t")); + } + cx_strfree(&str); +} + +CX_TEST(test_strtok) { + cxstring str = CX_STR("a,comma,separated,string"); + cxstring delim = CX_STR(","); + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok(str, delim, 3); + CX_TEST_ASSERT(ctx.str.ptr == str.ptr); + CX_TEST_ASSERT(ctx.str.length == str.length); + CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr); + CX_TEST_ASSERT(ctx.delim.length == delim.length); + CX_TEST_ASSERT(ctx.limit == 3); + CX_TEST_ASSERT(ctx.found == 0); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 0); + CX_TEST_ASSERT(ctx.delim_more == NULL); + CX_TEST_ASSERT(ctx.delim_more_count == 0); + } +} + +CX_TEST(test_strtok_m) { + cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string")); + cxstring delim = CX_STR(","); + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok_m(str, delim, 3); + CX_TEST_ASSERT(ctx.str.ptr == str.ptr); + CX_TEST_ASSERT(ctx.str.length == str.length); + CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr); + CX_TEST_ASSERT(ctx.delim.length == delim.length); + CX_TEST_ASSERT(ctx.limit == 3); + CX_TEST_ASSERT(ctx.found == 0); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 0); + CX_TEST_ASSERT(ctx.delim_more == NULL); + CX_TEST_ASSERT(ctx.delim_more_count == 0); + } + cx_strfree(&str); +} + +CX_TEST(test_strtok_delim) { + cxstring str = CX_STR("an,arbitrarily|separated;string"); + cxstring delim = CX_STR(","); + cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")}; + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok(str, delim, 3); + cx_strtok_delim(&ctx, delim_more, 2); + CX_TEST_ASSERT(ctx.str.ptr == str.ptr); + CX_TEST_ASSERT(ctx.str.length == str.length); + CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr); + CX_TEST_ASSERT(ctx.delim.length == delim.length); + CX_TEST_ASSERT(ctx.limit == 3); + CX_TEST_ASSERT(ctx.found == 0); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 0); + CX_TEST_ASSERT(ctx.delim_more == delim_more); + CX_TEST_ASSERT(ctx.delim_more_count == 2); + } +} + +CX_TEST(test_strtok_next_easy) { + cxstring str = CX_STR("a,comma,separated,string"); + cxstring delim = CX_STR(","); + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok(str, delim, 3); + bool ret; + cxstring tok; + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("a"))); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 2); + CX_TEST_ASSERT(ctx.delim_pos == 1); + CX_TEST_ASSERT(ctx.found == 1); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("comma"))); + CX_TEST_ASSERT(ctx.pos == 2); + CX_TEST_ASSERT(ctx.next_pos == 8); + CX_TEST_ASSERT(ctx.delim_pos == 7); + CX_TEST_ASSERT(ctx.found == 2); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated"))); + CX_TEST_ASSERT(ctx.pos == 8); + CX_TEST_ASSERT(ctx.next_pos == 18); + CX_TEST_ASSERT(ctx.delim_pos == 17); + CX_TEST_ASSERT(ctx.found == 3); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(!ret); + CX_TEST_ASSERT(ctx.pos == 8); + CX_TEST_ASSERT(ctx.next_pos == 18); + CX_TEST_ASSERT(ctx.delim_pos == 17); + CX_TEST_ASSERT(ctx.found == 3); + } +} + +CX_TEST(test_strtok_next_unlimited) { + cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;"); + cxstring delim = CX_STR(";-;"); + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX); + bool ret; + cxstring tok; + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("some"))); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 7); + CX_TEST_ASSERT(ctx.delim_pos == 4); + CX_TEST_ASSERT(ctx.found == 1); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("otherwise"))); + CX_TEST_ASSERT(ctx.pos == 7); + CX_TEST_ASSERT(ctx.next_pos == 19); + CX_TEST_ASSERT(ctx.delim_pos == 16); + CX_TEST_ASSERT(ctx.found == 2); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated"))); + CX_TEST_ASSERT(ctx.pos == 19); + CX_TEST_ASSERT(ctx.next_pos == 31); + CX_TEST_ASSERT(ctx.delim_pos == 28); + CX_TEST_ASSERT(ctx.found == 3); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("string"))); + CX_TEST_ASSERT(ctx.pos == 31); + CX_TEST_ASSERT(ctx.next_pos == 40); + CX_TEST_ASSERT(ctx.delim_pos == 37); + CX_TEST_ASSERT(ctx.found == 4); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR(""))); + CX_TEST_ASSERT(ctx.pos == 40); + CX_TEST_ASSERT(ctx.next_pos == 40); + CX_TEST_ASSERT(ctx.delim_pos == 40); + CX_TEST_ASSERT(ctx.found == 5); + + ret = cx_strtok_next(&ctx, &tok); + CX_TEST_ASSERT(!ret); + CX_TEST_ASSERT(ctx.pos == 40); + CX_TEST_ASSERT(ctx.delim_pos == 40); + CX_TEST_ASSERT(ctx.found == 5); + } +} + +CX_TEST(test_strtok_next_advanced) { + cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string")); + cxstring delim = CX_STR(","); + cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; + CX_TEST_DO { + CxStrtokCtx ctx = cx_strtok_m(str, delim, 10); + cx_strtok_delim(&ctx, delim_more, 2); + bool ret; + cxmutstr tok; + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("an"))); + CX_TEST_ASSERT(ctx.pos == 0); + CX_TEST_ASSERT(ctx.next_pos == 3); + CX_TEST_ASSERT(ctx.delim_pos == 2); + CX_TEST_ASSERT(ctx.found == 1); + cx_strupper(tok); + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily"))); + CX_TEST_ASSERT(ctx.pos == 3); + CX_TEST_ASSERT(ctx.next_pos == 15); + CX_TEST_ASSERT(ctx.delim_pos == 14); + CX_TEST_ASSERT(ctx.found == 2); + cx_strupper(tok); + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR(""))); + CX_TEST_ASSERT(ctx.pos == 15); + CX_TEST_ASSERT(ctx.next_pos == 17); + CX_TEST_ASSERT(ctx.delim_pos == 15); + CX_TEST_ASSERT(ctx.found == 3); + cx_strupper(tok); + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("separated"))); + CX_TEST_ASSERT(ctx.pos == 17); + CX_TEST_ASSERT(ctx.next_pos == 27); + CX_TEST_ASSERT(ctx.delim_pos == 26); + CX_TEST_ASSERT(ctx.found == 4); + cx_strupper(tok); + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(ret); + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("string"))); + CX_TEST_ASSERT(ctx.pos == 27); + CX_TEST_ASSERT(ctx.next_pos == 33); + CX_TEST_ASSERT(ctx.delim_pos == 33); + CX_TEST_ASSERT(ctx.found == 5); + cx_strupper(tok); + + ret = cx_strtok_next_m(&ctx, &tok); + CX_TEST_ASSERT(!ret); + CX_TEST_ASSERT(ctx.pos == 27); + CX_TEST_ASSERT(ctx.next_pos == 33); + CX_TEST_ASSERT(ctx.delim_pos == 33); + CX_TEST_ASSERT(ctx.found == 5); + + CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING"))); + } + cx_strfree(&str); +} + + +CxTestSuite *cx_test_suite_string(void) { + CxTestSuite *suite = cx_test_suite_new("string"); + + cx_test_register(suite, test_string_construct); + cx_test_register(suite, test_strfree); + cx_test_register(suite, test_strdup); + cx_test_register(suite, test_strdup_shortened); + cx_test_register(suite, test_strlen); + cx_test_register(suite, test_strsubs); + cx_test_register(suite, test_strchr); + cx_test_register(suite, test_strrchr); + cx_test_register(suite, test_strstr); + cx_test_register(suite, test_strcmp); + cx_test_register(suite, test_strcasecmp); + cx_test_register(suite, test_strcat); + cx_test_register(suite, test_strsplit); + cx_test_register(suite, test_strsplit_a); + cx_test_register(suite, test_strtrim); + cx_test_register(suite, test_strprefix); + cx_test_register(suite, test_strsuffix); + cx_test_register(suite, test_strcaseprefix); + cx_test_register(suite, test_strcasesuffix); + cx_test_register(suite, test_strreplace); + cx_test_register(suite, test_strupper); + cx_test_register(suite, test_strlower); + cx_test_register(suite, test_strtok); + cx_test_register(suite, test_strtok_m); + cx_test_register(suite, test_strtok_delim); + cx_test_register(suite, test_strtok_next_easy); + cx_test_register(suite, test_strtok_next_unlimited); + cx_test_register(suite, test_strtok_next_advanced); + + return suite; +}
--- a/tests/test_string.cpp Thu Dec 28 19:17:45 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,882 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cx/string.h" -#include "util_allocator.h" - -#include <gtest/gtest.h> - -#define EXPECT_ZERO_TERMINATED(str) EXPECT_EQ((str).ptr[(str).length], '\0') - -TEST(String, construct) { - cxstring s1 = CX_STR("1234"); - cxstring s2 = cx_strn("abcd", 2); - cxmutstr s3 = cx_mutstr((char *) "1234"); - cxmutstr s4 = cx_mutstrn((char *) "abcd", 2); - - EXPECT_EQ(s1.length, 4); - EXPECT_EQ(s2.length, 2); - EXPECT_EQ(s3.length, 4); - EXPECT_EQ(s4.length, 2); -} - -TEST(String, strfree) { - CxTestingAllocator alloc; - auto test = (char *) cxMalloc(&alloc, 16); - cxmutstr str = cx_mutstrn(test, 16); - ASSERT_EQ(str.ptr, test); - EXPECT_EQ(str.length, 16); - cx_strfree_a(&alloc, &str); - EXPECT_EQ(str.ptr, nullptr); - EXPECT_EQ(str.length, 0); - EXPECT_TRUE(alloc.verify()); -} - -TEST(String, strdup) { - cxstring str = CX_STR("test"); - cxmutstr dup = cx_strdup(str); - ASSERT_EQ(dup.length, str.length); - EXPECT_STREQ(dup.ptr, str.ptr); - EXPECT_ZERO_TERMINATED(dup); - cx_strfree(&dup); - - str.length = 2; - dup = cx_strdup(str); - ASSERT_EQ(dup.length, str.length); - EXPECT_STREQ(dup.ptr, "te"); - EXPECT_ZERO_TERMINATED(dup); - cx_strfree(&dup); -} - -TEST(String, strlen) { - cxstring s1 = CX_STR("1234"); - cxstring s2 = CX_STR(".:.:."); - cxstring s3 = CX_STR("X"); - - size_t len0 = cx_strlen(0); - size_t len1 = cx_strlen(1, s1); - size_t len2 = cx_strlen(2, s1, s2); - size_t len3 = cx_strlen(3, s1, s2, s3); - - EXPECT_EQ(len0, 0); - EXPECT_EQ(len1, 4); - EXPECT_EQ(len2, 9); - EXPECT_EQ(len3, 10); -} - -TEST(String, strsubs) { - cxstring str = CX_STR("A test string"); - - cxstring sub = cx_strsubs(str, 0); - EXPECT_EQ(cx_strcmp(sub, str), 0); - - sub = cx_strsubs(str, 2); - EXPECT_EQ(cx_strcmp(sub, CX_STR("test string")), 0); - - sub = cx_strsubs(str, 7); - EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0); - - sub = cx_strsubs(str, 15); - EXPECT_EQ(cx_strcmp(sub, CX_STR("")), 0); - - sub = cx_strsubsl(str, 2, 4); - EXPECT_EQ(cx_strcmp(sub, CX_STR("test")), 0); - - sub = cx_strsubsl(str, 7, 3); - EXPECT_EQ(cx_strcmp(sub, CX_STR("str")), 0); - - sub = cx_strsubsl(str, 7, 20); - EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0); - - // just for coverage, call the _m variant - auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); -} - -TEST(String, strchr) { - cxstring str = CX_STR("I will find you - and I will kill you"); - - cxstring notfound = cx_strchr(str, 'x'); - EXPECT_EQ(notfound.length, 0); - - cxstring result = cx_strchr(str, 'w'); - EXPECT_EQ(result.length, 35); - EXPECT_STREQ(result.ptr, "will find you - and I will kill you"); - - // just for coverage, call the _m variant - auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a'); - EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); -} - -TEST(String, strrchr) { - cxstring str = CX_STR("I will find you - and I will kill you"); - - cxstring notfound = cx_strrchr(str, 'x'); - EXPECT_EQ(notfound.length, 0); - - cxstring result = cx_strrchr(str, 'w'); - EXPECT_EQ(result.length, 13); - EXPECT_STREQ(result.ptr, "will kill you"); - - // just for coverage, call the _m variant - auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a'); - EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); -} - -TEST(String, strstr) { - cxstring str = CX_STR("find the match in this string"); - cxstring longstr = CX_STR( - "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl" - "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx" - "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij" - "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "wxyz1234567890"); - cxstring longstrpattern = CX_STR( - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - ); - cxstring longstrresult = CX_STR( - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "abababababababababababababababababababababababababababababababab" - "wxyz1234567890" - ); - - cxstring notfound = cx_strstr(str, CX_STR("no match")); - EXPECT_EQ(notfound.length, 0); - - cxstring result = cx_strstr(str, CX_STR("match")); - EXPECT_EQ(result.length, 20); - EXPECT_STREQ(result.ptr, "match in this string"); - - result = cx_strstr(str, CX_STR("")); - EXPECT_EQ(result.length, str.length); - EXPECT_STREQ(result.ptr, str.ptr); - - result = cx_strstr(longstr, longstrpattern); - EXPECT_EQ(result.length, longstrresult.length); - EXPECT_STREQ(result.ptr, longstrresult.ptr); - - // just for coverage, call the _m variant - auto mstr = cx_strdup(longstr); - auto m = cx_strstr_m(mstr, longstrpattern); - EXPECT_EQ(m.length, longstrresult.length); - EXPECT_STREQ(m.ptr, longstrresult.ptr); - cx_strfree(&mstr); -} - -TEST(String, strcmp) { - cxstring str = CX_STR("compare this"); - - EXPECT_EQ(cx_strcmp(CX_STR(""), CX_STR("")), 0); - EXPECT_GT(cx_strcmp(str, CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(str, CX_STR("compare this")), 0); - EXPECT_NE(cx_strcmp(str, CX_STR("Compare This")), 0); - EXPECT_LT(cx_strcmp(str, CX_STR("compare tool")), 0); - EXPECT_GT(cx_strcmp(str, CX_STR("compare shit")), 0); - EXPECT_LT(cx_strcmp(str, CX_STR("compare this not")), 0); - EXPECT_GT(cx_strcmp(str, CX_STR("compare")), 0); - - cxstring str2 = CX_STR("Compare This"); - EXPECT_NE(cx_strcmp_p(&str, &str2), 0); - str2 = CX_STR("compare this"); - EXPECT_EQ(cx_strcmp_p(&str, &str2), 0); -} - -TEST(String, strcasecmp) { - cxstring str = CX_STR("compare this"); - - EXPECT_EQ(cx_strcasecmp(CX_STR(""), CX_STR("")), 0); - EXPECT_GT(cx_strcasecmp(str, CX_STR("")), 0); - EXPECT_EQ(cx_strcasecmp(str, CX_STR("compare this")), 0); - EXPECT_EQ(cx_strcasecmp(str, CX_STR("Compare This")), 0); - EXPECT_LT(cx_strcasecmp(str, CX_STR("compare tool")), 0); - EXPECT_GT(cx_strcasecmp(str, CX_STR("compare shit")), 0); - EXPECT_LT(cx_strcasecmp(str, CX_STR("compare this not")), 0); - EXPECT_GT(cx_strcasecmp(str, CX_STR("compare")), 0); - - cxstring str2 = CX_STR("Compare This"); - EXPECT_EQ(cx_strcasecmp_p(&str, &str2), 0); - str2 = CX_STR("Compare Tool"); - EXPECT_LT(cx_strcasecmp_p(&str, &str2), 0); -} - -TEST(String, strcat) { - cxstring s1 = CX_STR("12"); - cxstring s2 = CX_STR("34"); - cxstring s3 = CX_STR("56"); - cxstring sn = {nullptr, 0}; - - CxTestingAllocator alloc; - - cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2); - EXPECT_EQ(cx_strcmp(cx_strcast(t1), CX_STR("1234")), 0); - EXPECT_ZERO_TERMINATED(t1); - cx_strfree_a(&alloc, &t1); - - cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3); - EXPECT_EQ(cx_strcmp(cx_strcast(t2), CX_STR("123456")), 0); - EXPECT_ZERO_TERMINATED(t2); - cx_strfree_a(&alloc, &t2); - - cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn); - EXPECT_EQ(cx_strcmp(cx_strcast(t3), CX_STR("123456")), 0); - EXPECT_ZERO_TERMINATED(t3); - cx_strfree_a(&alloc, &t3); - - cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn); - EXPECT_EQ(cx_strcmp(cx_strcast(t4), CX_STR("")), 0); - EXPECT_ZERO_TERMINATED(t4); - cx_strfree_a(&alloc, &t4); - - EXPECT_TRUE(alloc.verify()); - - // use the macro - cxmutstr t5 = cx_strcat(3, s3, s1, s2); - EXPECT_EQ(cx_strcmp(cx_strcast(t5), CX_STR("561234")), 0); - EXPECT_ZERO_TERMINATED(t5); - cx_strfree(&t5); - - // use an initial string - cxmutstr t6 = cx_strdup(CX_STR("Hello")); - t6 = cx_strcat_m(t6, 2, CX_STR(", "), CX_STR("World!")); - EXPECT_EQ(cx_strcmp(cx_strcast(t6), CX_STR("Hello, World!")), 0); - EXPECT_ZERO_TERMINATED(t6); - cx_strfree(&t6); -} - -TEST(String, strsplit) { - - cxstring test = CX_STR("this,is,a,csv,string"); - size_t capa = 8; - cxstring list[8]; - size_t n; - - // special case: empty string - n = cx_strsplit(test, CX_STR(""), capa, list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - - // no delimiter occurrence - n = cx_strsplit(test, CX_STR("z"), capa, list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - - // partially matching delimiter - n = cx_strsplit(test, CX_STR("is,not"), capa, list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - - // matching single-char delimiter - n = cx_strsplit(test, CX_STR(","), capa, list); - ASSERT_EQ(n, 5); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0); - EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0); - EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0); - - // matching multi-char delimiter - n = cx_strsplit(test, CX_STR("is"), capa, list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0); - - // bounded list using single-char delimiter - n = cx_strsplit(test, CX_STR(","), 3, list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); - - // bounded list using multi-char delimiter - n = cx_strsplit(test, CX_STR("is"), 2, list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); - - // start with delimiter - n = cx_strsplit(test, CX_STR("this"), capa, list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); - - // end with delimiter - n = cx_strsplit(test, CX_STR("string"), capa, list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - - - // end with delimiter exceed bound - n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0); - - // exact match - n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - - // string to be split is only substring - n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - - // subsequent encounter of delimiter (the string between is empty) - n = cx_strsplit(test, CX_STR("is,"), capa, list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); - - // call the _m variant just for coverage - auto mtest = cx_strdup(test); - cxmutstr mlist[4]; - n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0); - cx_strfree(&mtest); -} - -TEST(String, strsplit_a) { - CxTestingAllocator alloc; - - cxstring test = CX_STR("this,is,a,csv,string"); - size_t capa = 8; - cxstring *list; - size_t n; - - // special case: empty string - n = cx_strsplit_a(&alloc, test, CX_STR(""), capa, &list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - cxFree(&alloc, list); - - // no delimiter occurrence - n = cx_strsplit_a(&alloc, test, CX_STR("z"), capa, &list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - cxFree(&alloc, list); - - // partially matching delimiter - n = cx_strsplit_a(&alloc, test, CX_STR("is,not"), capa, &list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - cxFree(&alloc, list); - - // matching single-char delimiter - n = cx_strsplit_a(&alloc, test, CX_STR(","), capa, &list); - ASSERT_EQ(n, 5); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0); - EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0); - EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0); - cxFree(&alloc, list); - - // matching multi-char delimiter - n = cx_strsplit_a(&alloc, test, CX_STR("is"), capa, &list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0); - cxFree(&alloc, list); - - // bounded list using single-char delimiter - n = cx_strsplit_a(&alloc, test, CX_STR(","), 3, &list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); - cxFree(&alloc, list); - - // bounded list using multi-char delimiter - n = cx_strsplit_a(&alloc, test, CX_STR("is"), 2, &list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); - cxFree(&alloc, list); - - // start with delimiter - n = cx_strsplit_a(&alloc, test, CX_STR("this"), capa, &list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); - cxFree(&alloc, list); - - // end with delimiter - n = cx_strsplit_a(&alloc, test, CX_STR("string"), capa, &list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - cxFree(&alloc, list); - - // end with delimiter exceed bound - n = cx_strsplit_a(&alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0); - cxFree(&alloc, list); - - // exact match - n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string"), capa, &list); - ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - cxFree(&alloc, list); - - // string to be split is only substring - n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list); - ASSERT_EQ(n, 1); - EXPECT_EQ(cx_strcmp(list[0], test), 0); - cxFree(&alloc, list); - - // subsequent encounter of delimiter (the string between is empty) - n = cx_strsplit_a(&alloc, test, CX_STR("is,"), capa, &list); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); - cxFree(&alloc, list); - - // call the _m variant just for coverage - auto mtest = cx_strdup(test); - cxmutstr *mlist; - n = cx_strsplit_ma(&alloc, mtest, CX_STR("is,"), 4, &mlist); - ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0); - cxFree(&alloc, mlist); - cx_strfree(&mtest); - - EXPECT_TRUE(alloc.verify()); -} - -TEST(String, strtrim) { - cxstring t1 = cx_strtrim(CX_STR(" ein test \t ")); - cxstring t2 = cx_strtrim(CX_STR("abc")); - cxstring t3 = cx_strtrim(CX_STR(" 123")); - cxstring t4 = cx_strtrim(CX_STR("xyz ")); - cxstring t5 = cx_strtrim(CX_STR(" ")); - cxstring empty = cx_strtrim(CX_STR("")); - - EXPECT_EQ(cx_strcmp(t1, CX_STR("ein test")), 0); - EXPECT_EQ(cx_strcmp(t2, CX_STR("abc")), 0); - EXPECT_EQ(cx_strcmp(t3, CX_STR("123")), 0); - EXPECT_EQ(cx_strcmp(t4, CX_STR("xyz")), 0); - EXPECT_EQ(cx_strcmp(t5, CX_STR("")), 0); - EXPECT_EQ(cx_strcmp(empty, CX_STR("")), 0); - - // call the _m variant just for coverage - cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); - EXPECT_EQ(cx_strcmp(cx_strcast(m1), CX_STR("ein test")), 0); -} - -TEST(String, strprefix) { - cxstring str = CX_STR("test my prefix and my suffix"); - cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strprefix(empty, CX_STR("pref"))); - EXPECT_TRUE(cx_strprefix(str, empty)); - EXPECT_TRUE(cx_strprefix(empty, empty)); - EXPECT_TRUE(cx_strprefix(str, CX_STR("test "))); - EXPECT_FALSE(cx_strprefix(str, CX_STR("8-) fsck "))); -} - -TEST(String, strsuffix) { - cxstring str = CX_STR("test my prefix and my suffix"); - cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strsuffix(empty, CX_STR("suf"))); - EXPECT_TRUE(cx_strsuffix(str, empty)); - EXPECT_TRUE(cx_strsuffix(empty, empty)); - EXPECT_TRUE(cx_strsuffix(str, CX_STR("fix"))); - EXPECT_FALSE(cx_strsuffix(str, CX_STR("fox"))); -} - -TEST(String, strcaseprefix) { - cxstring str = CX_STR("test my prefix and my suffix"); - cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strcaseprefix(empty, CX_STR("pREf"))); - EXPECT_TRUE(cx_strcaseprefix(str, empty)); - EXPECT_TRUE(cx_strcaseprefix(empty, empty)); - EXPECT_TRUE(cx_strcaseprefix(str, CX_STR("TEST "))); - EXPECT_FALSE(cx_strcaseprefix(str, CX_STR("8-) fsck "))); -} - -TEST(String, strcasesuffix) { - cxstring str = CX_STR("test my prefix and my suffix"); - cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strcasesuffix(empty, CX_STR("sUf"))); - EXPECT_TRUE(cx_strcasesuffix(str, empty)); - EXPECT_TRUE(cx_strcasesuffix(empty, empty)); - EXPECT_TRUE(cx_strcasesuffix(str, CX_STR("FIX"))); - EXPECT_FALSE(cx_strcasesuffix(str, CX_STR("fox"))); -} - -TEST(String, strreplace) { - CxTestingAllocator alloc; - cxstring str = CX_STR("test ababab string aba"); - cxstring longstr = CX_STR( - "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd"); - cxstring notrail = CX_STR("test abab"); - cxstring empty = CX_STR(""); - cxstring astr = CX_STR("aaaaaaaaaa"); - cxstring csstr = CX_STR("test AB ab TEST xyz"); - - cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger")); - auto expected = "test muchlongerab string aba"; - - cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2); - auto expectedn = "test ccab string aba"; - - cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z")); - auto longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd"; - - cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z")); - auto notrailexpect = "test zz"; - - cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello")); - auto eqexpect = "hello"; - - cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty - cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty); - auto emptyexpect2 = "test ab string aba"; - - cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST ")); - auto preexpected = "TEST ababab string aba"; - - cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1); - auto an1expected = "xaaaaaaaaa"; - - cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4); - auto an4expected = "xxxxaaaaaa"; - - cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9); - auto an9expected = "xxxxxxxxxa"; - - cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10); - auto an10expected = "xxxxxxxxxx"; - - cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, CX_STR("AB"), CX_STR("*")); - auto expeced1_a = "test * ab TEST xyz"; - - cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, CX_STR("test"), CX_STR("TEST")); - auto expected2_a = "TEST AB ab TEST xyz"; - - - EXPECT_NE(repl.ptr, str.ptr); - EXPECT_ZERO_TERMINATED(repl); - EXPECT_STREQ(repl.ptr, expected); - EXPECT_ZERO_TERMINATED(repln); - EXPECT_STREQ(repln.ptr, expectedn); - EXPECT_ZERO_TERMINATED(longrepl); - EXPECT_STREQ(longrepl.ptr, longexpect); - EXPECT_ZERO_TERMINATED(replnotrail); - EXPECT_STREQ(replnotrail.ptr, notrailexpect); - EXPECT_ZERO_TERMINATED(repleq); - EXPECT_STREQ(repleq.ptr, eqexpect); - EXPECT_ZERO_TERMINATED(replempty1); - EXPECT_STREQ(replempty1.ptr, ""); - EXPECT_ZERO_TERMINATED(replempty2); - EXPECT_STREQ(replempty2.ptr, emptyexpect2); - EXPECT_ZERO_TERMINATED(replpre); - EXPECT_STREQ(replpre.ptr, preexpected); - EXPECT_ZERO_TERMINATED(replan1); - EXPECT_STREQ(replan1.ptr, an1expected); - EXPECT_ZERO_TERMINATED(replan4); - EXPECT_STREQ(replan4.ptr, an4expected); - EXPECT_ZERO_TERMINATED(replan9); - EXPECT_STREQ(replan9.ptr, an9expected); - EXPECT_ZERO_TERMINATED(replan10); - EXPECT_STREQ(replan10.ptr, an10expected); - EXPECT_ZERO_TERMINATED(repl1_a); - EXPECT_STREQ(repl1_a.ptr, expeced1_a); - EXPECT_ZERO_TERMINATED(repl2_a); - EXPECT_STREQ(repl2_a.ptr, expected2_a); - - cx_strfree(&repl); - cx_strfree(&repln); - cx_strfree(&longrepl); - cx_strfree(&replnotrail); - cx_strfree(&repleq); - cx_strfree(&replempty1); - cx_strfree(&replempty2); - cx_strfree(&replpre); - cx_strfree(&replan1); - cx_strfree(&replan4); - cx_strfree(&replan9); - cx_strfree(&replan10); - - cx_strfree_a(&alloc, &repl1_a); - cx_strfree_a(&alloc, &repl2_a); - EXPECT_TRUE(alloc.verify()); -} - -TEST(String, strupper) { - cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); - cx_strupper(str); - EXPECT_STREQ(str.ptr, "THIS 1S @ TE$T"); - cx_strfree(&str); -} - -TEST(String, strlower) { - cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); - cx_strlower(str); - EXPECT_STREQ(str.ptr, "this 1s @ te$t"); - cx_strfree(&str); -} - -TEST(String, strtok) { - cxstring str = CX_STR("a,comma,separated,string"); - cxstring delim = CX_STR(","); - CxStrtokCtx ctx = cx_strtok(str, delim, 3); - EXPECT_EQ(ctx.str.ptr, str.ptr); - EXPECT_EQ(ctx.str.length, str.length); - EXPECT_EQ(ctx.delim.ptr, delim.ptr); - EXPECT_EQ(ctx.delim.length, delim.length); - EXPECT_EQ(ctx.limit, 3); - EXPECT_EQ(ctx.found, 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 0); - EXPECT_EQ(ctx.delim_more, nullptr); - EXPECT_EQ(ctx.delim_more_count, 0); -} - -TEST(String, strtok_m) { - cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string")); - cxstring delim = CX_STR(","); - CxStrtokCtx ctx = cx_strtok_m(str, delim, 3); - EXPECT_EQ(ctx.str.ptr, str.ptr); - EXPECT_EQ(ctx.str.length, str.length); - EXPECT_EQ(ctx.delim.ptr, delim.ptr); - EXPECT_EQ(ctx.delim.length, delim.length); - EXPECT_EQ(ctx.limit, 3); - EXPECT_EQ(ctx.found, 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 0); - EXPECT_EQ(ctx.delim_more, nullptr); - EXPECT_EQ(ctx.delim_more_count, 0); - cx_strfree(&str); -} - -TEST(String, strtok_delim) { - cxstring str = CX_STR("an,arbitrarily|separated;string"); - cxstring delim = CX_STR(","); - cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")}; - CxStrtokCtx ctx = cx_strtok(str, delim, 3); - cx_strtok_delim(&ctx, delim_more, 2); - EXPECT_EQ(ctx.str.ptr, str.ptr); - EXPECT_EQ(ctx.str.length, str.length); - EXPECT_EQ(ctx.delim.ptr, delim.ptr); - EXPECT_EQ(ctx.delim.length, delim.length); - EXPECT_EQ(ctx.limit, 3); - EXPECT_EQ(ctx.found, 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 0); - EXPECT_EQ(ctx.delim_more, delim_more); - EXPECT_EQ(ctx.delim_more_count, 2); -} - -TEST(String, strtok_next_easy) { - cxstring str = CX_STR("a,comma,separated,string"); - cxstring delim = CX_STR(","); - CxStrtokCtx ctx = cx_strtok(str, delim, 3); - bool ret; - cxstring tok; - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("a")), 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 2); - EXPECT_EQ(ctx.delim_pos, 1); - EXPECT_EQ(ctx.found, 1); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("comma")), 0); - EXPECT_EQ(ctx.pos, 2); - EXPECT_EQ(ctx.next_pos, 8); - EXPECT_EQ(ctx.delim_pos, 7); - EXPECT_EQ(ctx.found, 2); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0); - EXPECT_EQ(ctx.pos, 8); - EXPECT_EQ(ctx.next_pos, 18); - EXPECT_EQ(ctx.delim_pos, 17); - EXPECT_EQ(ctx.found, 3); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_FALSE(ret); - EXPECT_EQ(ctx.pos, 8); - EXPECT_EQ(ctx.next_pos, 18); - EXPECT_EQ(ctx.delim_pos, 17); - EXPECT_EQ(ctx.found, 3); -} - -TEST(String, strtok_next_unlimited) { - cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;"); - cxstring delim = CX_STR(";-;"); - CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX); - bool ret; - cxstring tok; - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("some")), 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 7); - EXPECT_EQ(ctx.delim_pos, 4); - EXPECT_EQ(ctx.found, 1); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("otherwise")), 0); - EXPECT_EQ(ctx.pos, 7); - EXPECT_EQ(ctx.next_pos, 19); - EXPECT_EQ(ctx.delim_pos, 16); - EXPECT_EQ(ctx.found, 2); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0); - EXPECT_EQ(ctx.pos, 19); - EXPECT_EQ(ctx.next_pos, 31); - EXPECT_EQ(ctx.delim_pos, 28); - EXPECT_EQ(ctx.found, 3); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("string")), 0); - EXPECT_EQ(ctx.pos, 31); - EXPECT_EQ(ctx.next_pos, 40); - EXPECT_EQ(ctx.delim_pos, 37); - EXPECT_EQ(ctx.found, 4); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, CX_STR("")), 0); - EXPECT_EQ(ctx.pos, 40); - EXPECT_EQ(ctx.next_pos, 40); - EXPECT_EQ(ctx.delim_pos, 40); - EXPECT_EQ(ctx.found, 5); - - ret = cx_strtok_next(&ctx, &tok); - ASSERT_FALSE(ret); - EXPECT_EQ(ctx.pos, 40); - EXPECT_EQ(ctx.delim_pos, 40); - EXPECT_EQ(ctx.found, 5); -} - -TEST(String, strtok_next_advanced) { - cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string")); - cxstring delim = CX_STR(","); - cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; - CxStrtokCtx ctx = cx_strtok_m(str, delim, 10); - cx_strtok_delim(&ctx, delim_more, 2); - bool ret; - cxmutstr tok; - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("an")), 0); - EXPECT_EQ(ctx.pos, 0); - EXPECT_EQ(ctx.next_pos, 3); - EXPECT_EQ(ctx.delim_pos, 2); - EXPECT_EQ(ctx.found, 1); - cx_strupper(tok); - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")), 0); - EXPECT_EQ(ctx.pos, 3); - EXPECT_EQ(ctx.next_pos, 15); - EXPECT_EQ(ctx.delim_pos, 14); - EXPECT_EQ(ctx.found, 2); - cx_strupper(tok); - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("")), 0); - EXPECT_EQ(ctx.pos, 15); - EXPECT_EQ(ctx.next_pos, 17); - EXPECT_EQ(ctx.delim_pos, 15); - EXPECT_EQ(ctx.found, 3); - cx_strupper(tok); - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("separated")), 0); - EXPECT_EQ(ctx.pos, 17); - EXPECT_EQ(ctx.next_pos, 27); - EXPECT_EQ(ctx.delim_pos, 26); - EXPECT_EQ(ctx.found, 4); - cx_strupper(tok); - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("string")), 0); - EXPECT_EQ(ctx.pos, 27); - EXPECT_EQ(ctx.next_pos, 33); - EXPECT_EQ(ctx.delim_pos, 33); - EXPECT_EQ(ctx.found, 5); - cx_strupper(tok); - - ret = cx_strtok_next_m(&ctx, &tok); - ASSERT_FALSE(ret); - EXPECT_EQ(ctx.pos, 27); - EXPECT_EQ(ctx.next_pos, 33); - EXPECT_EQ(ctx.delim_pos, 33); - EXPECT_EQ(ctx.found, 5); - - EXPECT_EQ(cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")), 0); - - cx_strfree(&str); -}
--- a/tests/ucxtest.c Thu Dec 28 19:17:45 2023 +0100 +++ b/tests/ucxtest.c Thu Dec 28 20:37:53 2023 +0100 @@ -32,6 +32,8 @@ CxTestSuite *cx_test_suite_utils(void); CxTestSuite *cx_test_suite_hash_key(void); +CxTestSuite *cx_test_suite_string(void); + #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ for (size_t i = 0; i < sizeof(test_suites)/sizeof(void*) ; i++) {run_tests(test_suites[i]);} (void)0 @@ -43,7 +45,8 @@ execute_test_suites( cx_test_suite_testing_allocator(), cx_test_suite_utils(), - cx_test_suite_hash_key() + cx_test_suite_hash_key(), + cx_test_suite_string() ); printf("=== OVERALL RESULT ===\n"); printf(" Total: %u\n Success: %u\n Failure: %u\n",