tests/test_string.c

Tue, 16 Jan 2024 23:12:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 16 Jan 2024 23:12:43 +0100
changeset 809
b7e2e1d7ed22
parent 806
e06249e09f99
permissions
-rw-r--r--

fix assertion failure depending on possibly uninitialized memory

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/test.h"
    30 #include "util_allocator.h"
    32 #include "cx/string.h"
    34 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
    35     #str " is not zero terminated")
    37 CX_TEST(test_string_construct) {
    38     cxstring s1 = CX_STR("1234");
    39     cxstring s2 = cx_strn("abcd", 2);
    40     cxmutstr s3 = cx_mutstr((char *) "1234");
    41     cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
    42     CX_TEST_DO {
    43         CX_TEST_ASSERT(s1.length == 4);
    44         CX_TEST_ASSERT(strncmp(s1.ptr, "1234", 4) == 0);
    45         CX_TEST_ASSERT(s2.length == 2);
    46         CX_TEST_ASSERT(strncmp(s2.ptr, "ab", 2) == 0);
    47         CX_TEST_ASSERT(s3.length == 4);
    48         CX_TEST_ASSERT(strncmp(s3.ptr, "1234", 4) == 0);
    49         CX_TEST_ASSERT(s4.length == 2);
    50         CX_TEST_ASSERT(strncmp(s4.ptr, "ab", 2) == 0);
    51     }
    52 }
    54 CX_TEST(test_strfree) {
    55     CxTestingAllocator talloc;
    56     cx_testing_allocator_init(&talloc);
    57     CxAllocator *alloc = &talloc.base;
    58     CX_TEST_DO {
    59         char *test = cxMalloc(alloc, 16);
    60         cxmutstr str = cx_mutstrn(test, 16);
    61         CX_TEST_ASSERT(str.ptr == test);
    62         CX_TEST_ASSERT(str.length == 16);
    63         cx_strfree_a(alloc, &str);
    64         CX_TEST_ASSERT(str.ptr == NULL);
    65         CX_TEST_ASSERT(str.length == 0);
    66         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
    67     }
    68     cx_testing_allocator_destroy(&talloc);
    69 }
    71 CX_TEST(test_strdup) {
    72     cxstring str = CX_STR("test");
    73     cxmutstr dup = cx_strdup(str);
    74     CX_TEST_DO {
    75         CX_TEST_ASSERT(dup.length == str.length);
    76         CX_TEST_ASSERT(0 == strcmp(dup.ptr, str.ptr));
    77         ASSERT_ZERO_TERMINATED(dup);
    78     }
    79     cx_strfree(&dup);
    80 }
    82 CX_TEST(test_strdup_shortened) {
    83     cxstring str = CX_STR("test");
    84     str.length = 2;
    85     cxmutstr dup = cx_strdup(str);
    86     CX_TEST_DO {
    87         CX_TEST_ASSERT(dup.length == str.length);
    88         CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te"));
    89         ASSERT_ZERO_TERMINATED(dup);
    90     }
    91     cx_strfree(&dup);
    92 }
    94 CX_TEST(test_strlen) {
    95     cxstring s1 = CX_STR("1234");
    96     cxstring s2 = CX_STR(".:.:.");
    97     cxstring s3 = CX_STR("X");
    98     CX_TEST_DO {
    99         size_t len0 = cx_strlen(0);
   100         size_t len1 = cx_strlen(1, s1);
   101         size_t len2 = cx_strlen(2, s1, s2);
   102         size_t len3 = cx_strlen(3, s1, s2, s3);
   104         CX_TEST_ASSERT(len0 == 0);
   105         CX_TEST_ASSERT(len1 == 4);
   106         CX_TEST_ASSERT(len2 == 9);
   107         CX_TEST_ASSERT(len3 == 10);
   108     }
   109 }
   111 CX_TEST(test_strsubs) {
   112     cxstring str = CX_STR("A test string");
   114     CX_TEST_DO {
   115         cxstring sub = cx_strsubs(str, 0);
   116         CX_TEST_ASSERT(0 == cx_strcmp(sub, str));
   118         sub = cx_strsubs(str, 2);
   119         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test string")));
   121         sub = cx_strsubs(str, 7);
   122         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string")));
   124         sub = cx_strsubs(str, 15);
   125         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("")));
   127         sub = cx_strsubsl(str, 2, 4);
   128         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test")));
   130         sub = cx_strsubsl(str, 7, 3);
   131         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("str")));
   133         sub = cx_strsubsl(str, 7, 20);
   134         CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string")));
   136         // just for coverage, call the _m variant
   137         cxmutstr m = cx_strsubs_m(cx_mutstrn(NULL, 0), 0);
   138         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
   139     }
   140 }
   142 CX_TEST(test_strchr) {
   143     cxstring str = CX_STR("I will find you - and I will kill you");
   145     CX_TEST_DO {
   146         cxstring notfound = cx_strchr(str, 'x');
   147         CX_TEST_ASSERT(notfound.length == 0);
   149         cxstring result = cx_strchr(str, 'w');
   150         CX_TEST_ASSERT(result.length == 35);
   151         CX_TEST_ASSERT(0 == strcmp(result.ptr, "will find you - and I will kill you"));
   153         // just for coverage, call the _m variant
   154         cxmutstr m = cx_strchr_m(cx_mutstrn(NULL, 0), 'a');
   155         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
   156     }
   157 }
   159 CX_TEST(test_strrchr) {
   160     cxstring str = CX_STR("I will find you - and I will kill you");
   162     CX_TEST_DO {
   163         cxstring notfound = cx_strrchr(str, 'x');
   164         CX_TEST_ASSERT(notfound.length == 0);
   166         cxstring result = cx_strrchr(str, 'w');
   167         CX_TEST_ASSERT(result.length == 13);
   168         CX_TEST_ASSERT(0 == strcmp(result.ptr, "will kill you"));
   170         // just for coverage, call the _m variant
   171         cxmutstr m = cx_strrchr_m(cx_mutstrn(NULL, 0), 'a');
   172         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
   173     }
   174 }
   176 CX_TEST(test_strstr) {
   177     cxstring str = CX_STR("find the match in this string");
   179     size_t const longstrpatternlen = 64 + cx_strstr_sbo_size;
   180     size_t const longstrlen = 320 + longstrpatternlen + 14;
   182     // it is more expensive to use calloc here, because we will overwrite
   183     // the memory anyway in the test preparation - but it is more reliable
   184     // in case we are doing something horribly wrong
   185     char *longstrc = calloc(longstrlen+1, 1);
   186     char *longstrpatternc = calloc(longstrpatternlen+1, 1);
   188     memcpy(longstrc,
   189            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
   190            "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
   191            "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
   192            "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
   193            "abababababababababababababababababababababababababababababababab",
   194            320
   195     );
   196     memcpy(longstrpatternc,
   197            "abababababababababababababababababababababababababababababababab",
   198            64
   199     );
   200     char x = 'a', y='b', z;
   201     for (size_t i = 0; i < cx_strstr_sbo_size ; i++) {
   202         longstrpatternc[64+i] = x;
   203         longstrc[320+i] = x;
   204         z=x; x=y; y=z;
   205     }
   206     longstrpatternc[longstrpatternlen] = '\0';
   207     memcpy(longstrc+longstrlen-14, "wxyz1234567890", 15);
   209     cxmutstr longstr = cx_mutstrn(longstrc, longstrlen);
   210     cxstring longstrpattern = cx_strn(longstrpatternc, longstrpatternlen);
   211     cxmutstr longstrresult = cx_mutstrn(longstrc+256, longstrlen-256);
   213     CX_TEST_DO {
   214         cxstring notfound = cx_strstr(str, CX_STR("no match"));
   215         CX_TEST_ASSERT(notfound.length == 0);
   217         cxstring result = cx_strstr(str, CX_STR("match"));
   218         CX_TEST_ASSERT(result.length == 20);
   219         CX_TEST_ASSERT(0 == strcmp(result.ptr, "match in this string"));
   221         result = cx_strstr(str, CX_STR(""));
   222         CX_TEST_ASSERT(result.length == str.length);
   223         CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
   225         cxmutstr resultm = cx_strstr_m(longstr, longstrpattern);
   226         CX_TEST_ASSERT(resultm.length == longstrresult.length);
   227         CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr));
   228     }
   230     free(longstrc);
   231     free(longstrpatternc);
   232 }
   234 CX_TEST(test_strcmp) {
   235     cxstring str = CX_STR("compare this");
   236     CX_TEST_DO {
   237         CX_TEST_ASSERT(0 == cx_strcmp(CX_STR(""), CX_STR("")));
   238         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("")));
   239         CX_TEST_ASSERT(0 == cx_strcmp(str, CX_STR("compare this")));
   240         CX_TEST_ASSERT(0 != cx_strcmp(str, CX_STR("Compare This")));
   241         CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare tool")));
   242         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare shit")));
   243         CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare this not")));
   244         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare")));
   246         cxstring str2 = CX_STR("Compare This");
   247         CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2));
   248         str2 = CX_STR("compare this");
   249         CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2));
   250     }
   251 }
   253 CX_TEST(test_strcasecmp) {
   254     cxstring str = CX_STR("compare this");
   255     CX_TEST_DO {
   256         CX_TEST_ASSERT(0 == cx_strcasecmp(CX_STR(""), CX_STR("")));
   257         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("")));
   258         CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("compare this")));
   259         CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("Compare This")));
   260         CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare tool")));
   261         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare shit")));
   262         CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare this not")));
   263         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare")));
   265         cxstring str2 = CX_STR("Compare This");
   266         CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2));
   267         str2 = CX_STR("Compare Tool");
   268         CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2));
   269     }
   270 }
   272 CX_TEST(test_strcat) {
   273     cxstring s1 = CX_STR("12");
   274     cxstring s2 = CX_STR("34");
   275     cxstring s3 = CX_STR("56");
   276     cxstring sn = {NULL, 0};
   278     CxTestingAllocator talloc;
   279     cx_testing_allocator_init(&talloc);
   280     CxAllocator *alloc = &talloc.base;
   282     CX_TEST_DO {
   283         cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2);
   284         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t1), CX_STR("1234")));
   285         ASSERT_ZERO_TERMINATED(t1);
   286         cx_strfree_a(alloc, &t1);
   288         cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3);
   289         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t2), CX_STR("123456")));
   290         ASSERT_ZERO_TERMINATED(t2);
   291         cx_strfree_a(alloc, &t2);
   293         cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn);
   294         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t3), CX_STR("123456")));
   295         ASSERT_ZERO_TERMINATED(t3);
   296         cx_strfree_a(alloc, &t3);
   298         cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn);
   299         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t4), CX_STR("")));
   300         ASSERT_ZERO_TERMINATED(t4);
   301         cx_strfree_a(alloc, &t4);
   303         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   305         // use the macro
   306         cxmutstr t5 = cx_strcat(3, s3, s1, s2);
   307         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t5), CX_STR("561234")));
   308         ASSERT_ZERO_TERMINATED(t5);
   309         cx_strfree(&t5);
   311         // use an initial string
   312         cxmutstr t6 = cx_strdup(CX_STR("Hello"));
   313         t6 = cx_strcat_m(t6, 2, CX_STR(", "), CX_STR("World!"));
   314         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t6), CX_STR("Hello, World!")));
   315         ASSERT_ZERO_TERMINATED(t6);
   316         cx_strfree(&t6);
   317     }
   318     cx_testing_allocator_destroy(&talloc);
   319 }
   321 CX_TEST(test_strsplit) {
   322     cxstring test = CX_STR("this,is,a,csv,string");
   323     size_t capa = 8;
   324     cxstring list[8];
   325     size_t n;
   326     CX_TEST_DO {
   327         // special case: empty string
   328         n = cx_strsplit(test, CX_STR(""), capa, list);
   329         CX_TEST_ASSERT(n == 1);
   330         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   332         // no delimiter occurrence
   333         n = cx_strsplit(test, CX_STR("z"), capa, list);
   334         CX_TEST_ASSERT(n == 1);
   335         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   337         // partially matching delimiter
   338         n = cx_strsplit(test, CX_STR("is,not"), capa, list);
   339         CX_TEST_ASSERT(n == 1);
   340         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   342         // matching single-char delimiter
   343         n = cx_strsplit(test, CX_STR(","), capa, list);
   344         CX_TEST_ASSERT(n == 5);
   345         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   346         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   347         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
   348         CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
   349         CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
   351         // matching multi-char delimiter
   352         n = cx_strsplit(test, CX_STR("is"), capa, list);
   353         CX_TEST_ASSERT(n == 3);
   354         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   355         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
   356         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
   358         // bounded list using single-char delimiter
   359         n = cx_strsplit(test, CX_STR(","), 3, list);
   360         CX_TEST_ASSERT(n == 3);
   361         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   362         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   363         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   365         // bounded list using multi-char delimiter
   366         n = cx_strsplit(test, CX_STR("is"), 2, list);
   367         CX_TEST_ASSERT(n == 2);
   368         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   369         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   371         // start with delimiter
   372         n = cx_strsplit(test, CX_STR("this"), capa, list);
   373         CX_TEST_ASSERT(n == 2);
   374         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   375         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   377         // end with delimiter
   378         n = cx_strsplit(test, CX_STR("string"), capa, list);
   379         CX_TEST_ASSERT(n == 2);
   380         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
   381         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   384         // end with delimiter exceed bound
   385         n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list);
   386         CX_TEST_ASSERT(n == 3);
   387         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
   388         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
   389         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
   391         // exact match
   392         n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list);
   393         CX_TEST_ASSERT(n == 2);
   394         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   395         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   397         // string to be split is only substring
   398         n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list);
   399         CX_TEST_ASSERT(n == 1);
   400         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   402         // subsequent encounter of delimiter (the string between is empty)
   403         n = cx_strsplit(test, CX_STR("is,"), capa, list);
   404         CX_TEST_ASSERT(n == 3);
   405         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   406         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   407         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   409         // call the _m variant just for coverage
   410         cxmutstr mtest = cx_strdup(test);
   411         cxmutstr mlist[4];
   412         n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist);
   413         CX_TEST_ASSERT(n == 3);
   414         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
   415         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
   416         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
   417         cx_strfree(&mtest);
   418     }
   419 }
   421 CX_TEST(test_strsplit_a) {
   422     CxTestingAllocator talloc;
   423     cx_testing_allocator_init(&talloc);
   424     CxAllocator *alloc = &talloc.base;
   426     cxstring test = CX_STR("this,is,a,csv,string");
   427     size_t capa = 8;
   428     cxstring *list;
   429     size_t n;
   430     CX_TEST_DO {
   431         // special case: empty string
   432         n = cx_strsplit_a(alloc, test, CX_STR(""), capa, &list);
   433         CX_TEST_ASSERT(n == 1);
   434         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   435         cxFree(alloc, list);
   437         // no delimiter occurrence
   438         n = cx_strsplit_a(alloc, test, CX_STR("z"), capa, &list);
   439         CX_TEST_ASSERT(n == 1);
   440         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   441         cxFree(alloc, list);
   443         // partially matching delimiter
   444         n = cx_strsplit_a(alloc, test, CX_STR("is,not"), capa, &list);
   445         CX_TEST_ASSERT(n == 1);
   446         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   447         cxFree(alloc, list);
   449         // matching single-char delimiter
   450         n = cx_strsplit_a(alloc, test, CX_STR(","), capa, &list);
   451         CX_TEST_ASSERT(n == 5);
   452         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   453         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   454         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
   455         CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
   456         CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
   457         cxFree(alloc, list);
   459         // matching multi-char delimiter
   460         n = cx_strsplit_a(alloc, test, CX_STR("is"), capa, &list);
   461         CX_TEST_ASSERT(n == 3);
   462         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   463         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
   464         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
   465         cxFree(alloc, list);
   467         // bounded list using single-char delimiter
   468         n = cx_strsplit_a(alloc, test, CX_STR(","), 3, &list);
   469         CX_TEST_ASSERT(n == 3);
   470         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   471         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   472         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   473         cxFree(alloc, list);
   475         // bounded list using multi-char delimiter
   476         n = cx_strsplit_a(alloc, test, CX_STR("is"), 2, &list);
   477         CX_TEST_ASSERT(n == 2);
   478         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   479         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   480         cxFree(alloc, list);
   482         // start with delimiter
   483         n = cx_strsplit_a(alloc, test, CX_STR("this"), capa, &list);
   484         CX_TEST_ASSERT(n == 2);
   485         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   486         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   487         cxFree(alloc, list);
   489         // end with delimiter
   490         n = cx_strsplit_a(alloc, test, CX_STR("string"), capa, &list);
   491         CX_TEST_ASSERT(n == 2);
   492         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
   493         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   494         cxFree(alloc, list);
   496         // end with delimiter exceed bound
   497         n = cx_strsplit_a(alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list);
   498         CX_TEST_ASSERT(n == 3);
   499         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
   500         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
   501         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
   502         cxFree(alloc, list);
   504         // exact match
   505         n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string"), capa, &list);
   506         CX_TEST_ASSERT(n == 2);
   507         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   508         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   509         cxFree(alloc, list);
   511         // string to be split is only substring
   512         n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list);
   513         CX_TEST_ASSERT(n == 1);
   514         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   515         cxFree(alloc, list);
   517         // subsequent encounter of delimiter (the string between is empty)
   518         n = cx_strsplit_a(alloc, test, CX_STR("is,"), capa, &list);
   519         CX_TEST_ASSERT(n == 3);
   520         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   521         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   522         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   523         cxFree(alloc, list);
   525         // call the _m variant just for coverage
   526         cxmutstr mtest = cx_strdup(test);
   527         cxmutstr *mlist;
   528         n = cx_strsplit_ma(alloc, mtest, CX_STR("is,"), 4, &mlist);
   529         CX_TEST_ASSERT(n == 3);
   530         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
   531         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
   532         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
   533         cxFree(alloc, mlist);
   534         cx_strfree(&mtest);
   536         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   537     }
   538     cx_testing_allocator_destroy(&talloc);
   539 }
   541 CX_TEST(test_strtrim) {
   542     cxstring t1 = cx_strtrim(CX_STR("  ein test  \t "));
   543     cxstring t2 = cx_strtrim(CX_STR("abc"));
   544     cxstring t3 = cx_strtrim(CX_STR(" 123"));
   545     cxstring t4 = cx_strtrim(CX_STR("xyz "));
   546     cxstring t5 = cx_strtrim(CX_STR("   "));
   547     cxstring empty = cx_strtrim(CX_STR(""));
   549     CX_TEST_DO {
   550         CX_TEST_ASSERT(0 == cx_strcmp(t1, CX_STR("ein test")));
   551         CX_TEST_ASSERT(0 == cx_strcmp(t2, CX_STR("abc")));
   552         CX_TEST_ASSERT(0 == cx_strcmp(t3, CX_STR("123")));
   553         CX_TEST_ASSERT(0 == cx_strcmp(t4, CX_STR("xyz")));
   554         CX_TEST_ASSERT(0 == cx_strcmp(t5, CX_STR("")));
   555         CX_TEST_ASSERT(0 == cx_strcmp(empty, CX_STR("")));
   557         // call the _m variant just for coverage
   558         cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) "  ein test  \t "));
   559         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m1), CX_STR("ein test")));
   560     }
   561 }
   563 CX_TEST(test_strprefix) {
   564     cxstring str = CX_STR("test my prefix and my suffix");
   565     cxstring empty = CX_STR("");
   566     CX_TEST_DO {
   567         CX_TEST_ASSERT(!cx_strprefix(empty, CX_STR("pref")));
   568         CX_TEST_ASSERT(cx_strprefix(str, empty));
   569         CX_TEST_ASSERT(cx_strprefix(empty, empty));
   570         CX_TEST_ASSERT(cx_strprefix(str, CX_STR("test ")));
   571         CX_TEST_ASSERT(!cx_strprefix(str, CX_STR("8-) fsck ")));
   572     }
   573 }
   575 CX_TEST(test_strsuffix) {
   576     cxstring str = CX_STR("test my prefix and my suffix");
   577     cxstring empty = CX_STR("");
   578     CX_TEST_DO {
   579         CX_TEST_ASSERT(!cx_strsuffix(empty, CX_STR("suf")));
   580         CX_TEST_ASSERT(cx_strsuffix(str, empty));
   581         CX_TEST_ASSERT(cx_strsuffix(empty, empty));
   582         CX_TEST_ASSERT(cx_strsuffix(str, CX_STR("fix")));
   583         CX_TEST_ASSERT(!cx_strsuffix(str, CX_STR("fox")));
   584     }
   585 }
   587 CX_TEST(test_strcaseprefix) {
   588     cxstring str = CX_STR("test my prefix and my suffix");
   589     cxstring empty = CX_STR("");
   590     CX_TEST_DO {
   591         CX_TEST_ASSERT(!cx_strcaseprefix(empty, CX_STR("pREf")));
   592         CX_TEST_ASSERT(cx_strcaseprefix(str, empty));
   593         CX_TEST_ASSERT(cx_strcaseprefix(empty, empty));
   594         CX_TEST_ASSERT(cx_strcaseprefix(str, CX_STR("TEST ")));
   595         CX_TEST_ASSERT(!cx_strcaseprefix(str, CX_STR("8-) fsck ")));
   596     }
   597 }
   599 CX_TEST(test_strcasesuffix) {
   600     cxstring str = CX_STR("test my prefix and my suffix");
   601     cxstring empty = CX_STR("");
   602     CX_TEST_DO {
   603         CX_TEST_ASSERT(!cx_strcasesuffix(empty, CX_STR("sUf")));
   604         CX_TEST_ASSERT(cx_strcasesuffix(str, empty));
   605         CX_TEST_ASSERT(cx_strcasesuffix(empty, empty));
   606         CX_TEST_ASSERT(cx_strcasesuffix(str, CX_STR("FIX")));
   607         CX_TEST_ASSERT(!cx_strcasesuffix(str, CX_STR("fox")));
   608     }
   609 }
   611 CX_TEST(test_strreplace) {
   612     CxTestingAllocator talloc;
   613     cx_testing_allocator_init(&talloc);
   614     CxAllocator *alloc = &talloc.base;
   616     cxstring str = CX_STR("test ababab string aba");
   617     cxstring longstr = CX_STR(
   618             "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
   619     cxstring notrail = CX_STR("test abab");
   620     cxstring empty = CX_STR("");
   621     cxstring astr = CX_STR("aaaaaaaaaa");
   622     cxstring csstr = CX_STR("test AB ab TEST xyz");
   624     cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger"));
   625     char const *expected = "test muchlongerab string aba";
   627     cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2);
   628     char const *expectedn = "test ccab string aba";
   630     cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z"));
   631     char const *longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd";
   633     cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z"));
   634     char const *notrailexpect = "test zz";
   636     cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello"));
   637     char const *eqexpect = "hello";
   639     cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty
   640     cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty);
   641     char const *emptyexpect2 = "test ab string aba";
   643     cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST "));
   644     char const *preexpected = "TEST ababab string aba";
   646     cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1);
   647     char const *an1expected = "xaaaaaaaaa";
   649     cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4);
   650     char const *an4expected = "xxxxaaaaaa";
   652     cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9);
   653     char const *an9expected = "xxxxxxxxxa";
   655     cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10);
   656     char const *an10expected = "xxxxxxxxxx";
   658     CX_TEST_DO {
   659         cxmutstr repl1_a = cx_strreplace_a(alloc, csstr, CX_STR("AB"), CX_STR("*"));
   660         char const *expeced1_a = "test * ab TEST xyz";
   662         cxmutstr repl2_a = cx_strreplace_a(alloc, csstr, CX_STR("test"), CX_STR("TEST"));
   663         char const *expected2_a = "TEST AB ab TEST xyz";
   665         CX_TEST_ASSERT(repl.ptr != str.ptr);
   666         ASSERT_ZERO_TERMINATED(repl);
   667         CX_TEST_ASSERT(0 == strcmp(repl.ptr, expected));
   668         ASSERT_ZERO_TERMINATED(repln);
   669         CX_TEST_ASSERT(0 == strcmp(repln.ptr, expectedn));
   670         ASSERT_ZERO_TERMINATED(longrepl);
   671         CX_TEST_ASSERT(0 == strcmp(longrepl.ptr, longexpect));
   672         ASSERT_ZERO_TERMINATED(replnotrail);
   673         CX_TEST_ASSERT(0 == strcmp(replnotrail.ptr, notrailexpect));
   674         ASSERT_ZERO_TERMINATED(repleq);
   675         CX_TEST_ASSERT(0 == strcmp(repleq.ptr, eqexpect));
   676         ASSERT_ZERO_TERMINATED(replempty1);
   677         CX_TEST_ASSERT(0 == strcmp(replempty1.ptr, ""));
   678         ASSERT_ZERO_TERMINATED(replempty2);
   679         CX_TEST_ASSERT(0 == strcmp(replempty2.ptr, emptyexpect2));
   680         ASSERT_ZERO_TERMINATED(replpre);
   681         CX_TEST_ASSERT(0 == strcmp(replpre.ptr, preexpected));
   682         ASSERT_ZERO_TERMINATED(replan1);
   683         CX_TEST_ASSERT(0 == strcmp(replan1.ptr, an1expected));
   684         ASSERT_ZERO_TERMINATED(replan4);
   685         CX_TEST_ASSERT(0 == strcmp(replan4.ptr, an4expected));
   686         ASSERT_ZERO_TERMINATED(replan9);
   687         CX_TEST_ASSERT(0 == strcmp(replan9.ptr, an9expected));
   688         ASSERT_ZERO_TERMINATED(replan10);
   689         CX_TEST_ASSERT(0 == strcmp(replan10.ptr, an10expected));
   690         ASSERT_ZERO_TERMINATED(repl1_a);
   691         CX_TEST_ASSERT(0 == strcmp(repl1_a.ptr, expeced1_a));
   692         ASSERT_ZERO_TERMINATED(repl2_a);
   693         CX_TEST_ASSERT(0 == strcmp(repl2_a.ptr, expected2_a));
   695         cx_strfree_a(alloc, &repl1_a);
   696         cx_strfree_a(alloc, &repl2_a);
   697         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   698     }
   700     cx_strfree(&repl);
   701     cx_strfree(&repln);
   702     cx_strfree(&longrepl);
   703     cx_strfree(&replnotrail);
   704     cx_strfree(&repleq);
   705     cx_strfree(&replempty1);
   706     cx_strfree(&replempty2);
   707     cx_strfree(&replpre);
   708     cx_strfree(&replan1);
   709     cx_strfree(&replan4);
   710     cx_strfree(&replan9);
   711     cx_strfree(&replan10);
   712     cx_testing_allocator_destroy(&talloc);
   713 }
   715 CX_TEST(test_strupper) {
   716     cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
   717     CX_TEST_DO {
   718         cx_strupper(str);
   719         CX_TEST_ASSERT(0 == strcmp(str.ptr, "THIS 1S @ TE$T"));
   720     }
   721     cx_strfree(&str);
   722 }
   724 CX_TEST(test_strlower) {
   725     cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
   726     CX_TEST_DO {
   727         cx_strlower(str);
   728         CX_TEST_ASSERT(0 == strcmp(str.ptr, "this 1s @ te$t"));
   729     }
   730     cx_strfree(&str);
   731 }
   733 CX_TEST(test_strtok) {
   734     cxstring str = CX_STR("a,comma,separated,string");
   735     cxstring delim = CX_STR(",");
   736     CX_TEST_DO {
   737         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   738         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   739         CX_TEST_ASSERT(ctx.str.length == str.length);
   740         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   741         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   742         CX_TEST_ASSERT(ctx.limit == 3);
   743         CX_TEST_ASSERT(ctx.found == 0);
   744         CX_TEST_ASSERT(ctx.pos == 0);
   745         CX_TEST_ASSERT(ctx.next_pos == 0);
   746         CX_TEST_ASSERT(ctx.delim_more == NULL);
   747         CX_TEST_ASSERT(ctx.delim_more_count == 0);
   748     }
   749 }
   751 CX_TEST(test_strtok_m) {
   752     cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string"));
   753     cxstring delim = CX_STR(",");
   754     CX_TEST_DO {
   755         CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
   756         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   757         CX_TEST_ASSERT(ctx.str.length == str.length);
   758         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   759         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   760         CX_TEST_ASSERT(ctx.limit == 3);
   761         CX_TEST_ASSERT(ctx.found == 0);
   762         CX_TEST_ASSERT(ctx.pos == 0);
   763         CX_TEST_ASSERT(ctx.next_pos == 0);
   764         CX_TEST_ASSERT(ctx.delim_more == NULL);
   765         CX_TEST_ASSERT(ctx.delim_more_count == 0);
   766     }
   767     cx_strfree(&str);
   768 }
   770 CX_TEST(test_strtok_delim) {
   771     cxstring str = CX_STR("an,arbitrarily|separated;string");
   772     cxstring delim = CX_STR(",");
   773     cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
   774     CX_TEST_DO {
   775         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   776         cx_strtok_delim(&ctx, delim_more, 2);
   777         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   778         CX_TEST_ASSERT(ctx.str.length == str.length);
   779         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   780         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   781         CX_TEST_ASSERT(ctx.limit == 3);
   782         CX_TEST_ASSERT(ctx.found == 0);
   783         CX_TEST_ASSERT(ctx.pos == 0);
   784         CX_TEST_ASSERT(ctx.next_pos == 0);
   785         CX_TEST_ASSERT(ctx.delim_more == delim_more);
   786         CX_TEST_ASSERT(ctx.delim_more_count == 2);
   787     }
   788 }
   790 CX_TEST(test_strtok_next_easy) {
   791     cxstring str = CX_STR("a,comma,separated,string");
   792     cxstring delim = CX_STR(",");
   793     CX_TEST_DO {
   794         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   795         bool ret;
   796         cxstring tok;
   798         ret = cx_strtok_next(&ctx, &tok);
   799         CX_TEST_ASSERT(ret);
   800         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("a")));
   801         CX_TEST_ASSERT(ctx.pos == 0);
   802         CX_TEST_ASSERT(ctx.next_pos == 2);
   803         CX_TEST_ASSERT(ctx.delim_pos == 1);
   804         CX_TEST_ASSERT(ctx.found == 1);
   806         ret = cx_strtok_next(&ctx, &tok);
   807         CX_TEST_ASSERT(ret);
   808         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("comma")));
   809         CX_TEST_ASSERT(ctx.pos == 2);
   810         CX_TEST_ASSERT(ctx.next_pos == 8);
   811         CX_TEST_ASSERT(ctx.delim_pos == 7);
   812         CX_TEST_ASSERT(ctx.found == 2);
   814         ret = cx_strtok_next(&ctx, &tok);
   815         CX_TEST_ASSERT(ret);
   816         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
   817         CX_TEST_ASSERT(ctx.pos == 8);
   818         CX_TEST_ASSERT(ctx.next_pos == 18);
   819         CX_TEST_ASSERT(ctx.delim_pos == 17);
   820         CX_TEST_ASSERT(ctx.found == 3);
   822         ret = cx_strtok_next(&ctx, &tok);
   823         CX_TEST_ASSERT(!ret);
   824         CX_TEST_ASSERT(ctx.pos == 8);
   825         CX_TEST_ASSERT(ctx.next_pos == 18);
   826         CX_TEST_ASSERT(ctx.delim_pos == 17);
   827         CX_TEST_ASSERT(ctx.found == 3);
   828     }
   829 }
   831 CX_TEST(test_strtok_next_unlimited) {
   832     cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;");
   833     cxstring delim = CX_STR(";-;");
   834     CX_TEST_DO {
   835         CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
   836         bool ret;
   837         cxstring tok;
   839         ret = cx_strtok_next(&ctx, &tok);
   840         CX_TEST_ASSERT(ret);
   841         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("some")));
   842         CX_TEST_ASSERT(ctx.pos == 0);
   843         CX_TEST_ASSERT(ctx.next_pos == 7);
   844         CX_TEST_ASSERT(ctx.delim_pos == 4);
   845         CX_TEST_ASSERT(ctx.found == 1);
   847         ret = cx_strtok_next(&ctx, &tok);
   848         CX_TEST_ASSERT(ret);
   849         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("otherwise")));
   850         CX_TEST_ASSERT(ctx.pos == 7);
   851         CX_TEST_ASSERT(ctx.next_pos == 19);
   852         CX_TEST_ASSERT(ctx.delim_pos == 16);
   853         CX_TEST_ASSERT(ctx.found == 2);
   855         ret = cx_strtok_next(&ctx, &tok);
   856         CX_TEST_ASSERT(ret);
   857         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
   858         CX_TEST_ASSERT(ctx.pos == 19);
   859         CX_TEST_ASSERT(ctx.next_pos == 31);
   860         CX_TEST_ASSERT(ctx.delim_pos == 28);
   861         CX_TEST_ASSERT(ctx.found == 3);
   863         ret = cx_strtok_next(&ctx, &tok);
   864         CX_TEST_ASSERT(ret);
   865         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("string")));
   866         CX_TEST_ASSERT(ctx.pos == 31);
   867         CX_TEST_ASSERT(ctx.next_pos == 40);
   868         CX_TEST_ASSERT(ctx.delim_pos == 37);
   869         CX_TEST_ASSERT(ctx.found == 4);
   871         ret = cx_strtok_next(&ctx, &tok);
   872         CX_TEST_ASSERT(ret);
   873         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("")));
   874         CX_TEST_ASSERT(ctx.pos == 40);
   875         CX_TEST_ASSERT(ctx.next_pos == 40);
   876         CX_TEST_ASSERT(ctx.delim_pos == 40);
   877         CX_TEST_ASSERT(ctx.found == 5);
   879         ret = cx_strtok_next(&ctx, &tok);
   880         CX_TEST_ASSERT(!ret);
   881         CX_TEST_ASSERT(ctx.pos == 40);
   882         CX_TEST_ASSERT(ctx.delim_pos == 40);
   883         CX_TEST_ASSERT(ctx.found == 5);
   884     }
   885 }
   887 CX_TEST(test_strtok_next_advanced) {
   888     cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string"));
   889     cxstring delim = CX_STR(",");
   890     cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
   891     CX_TEST_DO {
   892         CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
   893         cx_strtok_delim(&ctx, delim_more, 2);
   894         bool ret;
   895         cxmutstr tok;
   897         ret = cx_strtok_next_m(&ctx, &tok);
   898         CX_TEST_ASSERT(ret);
   899         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("an")));
   900         CX_TEST_ASSERT(ctx.pos == 0);
   901         CX_TEST_ASSERT(ctx.next_pos == 3);
   902         CX_TEST_ASSERT(ctx.delim_pos == 2);
   903         CX_TEST_ASSERT(ctx.found == 1);
   904         cx_strupper(tok);
   906         ret = cx_strtok_next_m(&ctx, &tok);
   907         CX_TEST_ASSERT(ret);
   908         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")));
   909         CX_TEST_ASSERT(ctx.pos == 3);
   910         CX_TEST_ASSERT(ctx.next_pos == 15);
   911         CX_TEST_ASSERT(ctx.delim_pos == 14);
   912         CX_TEST_ASSERT(ctx.found == 2);
   913         cx_strupper(tok);
   915         ret = cx_strtok_next_m(&ctx, &tok);
   916         CX_TEST_ASSERT(ret);
   917         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("")));
   918         CX_TEST_ASSERT(ctx.pos == 15);
   919         CX_TEST_ASSERT(ctx.next_pos == 17);
   920         CX_TEST_ASSERT(ctx.delim_pos == 15);
   921         CX_TEST_ASSERT(ctx.found == 3);
   922         cx_strupper(tok);
   924         ret = cx_strtok_next_m(&ctx, &tok);
   925         CX_TEST_ASSERT(ret);
   926         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("separated")));
   927         CX_TEST_ASSERT(ctx.pos == 17);
   928         CX_TEST_ASSERT(ctx.next_pos == 27);
   929         CX_TEST_ASSERT(ctx.delim_pos == 26);
   930         CX_TEST_ASSERT(ctx.found == 4);
   931         cx_strupper(tok);
   933         ret = cx_strtok_next_m(&ctx, &tok);
   934         CX_TEST_ASSERT(ret);
   935         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("string")));
   936         CX_TEST_ASSERT(ctx.pos == 27);
   937         CX_TEST_ASSERT(ctx.next_pos == 33);
   938         CX_TEST_ASSERT(ctx.delim_pos == 33);
   939         CX_TEST_ASSERT(ctx.found == 5);
   940         cx_strupper(tok);
   942         ret = cx_strtok_next_m(&ctx, &tok);
   943         CX_TEST_ASSERT(!ret);
   944         CX_TEST_ASSERT(ctx.pos == 27);
   945         CX_TEST_ASSERT(ctx.next_pos == 33);
   946         CX_TEST_ASSERT(ctx.delim_pos == 33);
   947         CX_TEST_ASSERT(ctx.found == 5);
   949         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")));
   950     }
   951     cx_strfree(&str);
   952 }
   955 CxTestSuite *cx_test_suite_string(void) {
   956     CxTestSuite *suite = cx_test_suite_new("string");
   958     cx_test_register(suite, test_string_construct);
   959     cx_test_register(suite, test_strfree);
   960     cx_test_register(suite, test_strdup);
   961     cx_test_register(suite, test_strdup_shortened);
   962     cx_test_register(suite, test_strlen);
   963     cx_test_register(suite, test_strsubs);
   964     cx_test_register(suite, test_strchr);
   965     cx_test_register(suite, test_strrchr);
   966     cx_test_register(suite, test_strstr);
   967     cx_test_register(suite, test_strcmp);
   968     cx_test_register(suite, test_strcasecmp);
   969     cx_test_register(suite, test_strcat);
   970     cx_test_register(suite, test_strsplit);
   971     cx_test_register(suite, test_strsplit_a);
   972     cx_test_register(suite, test_strtrim);
   973     cx_test_register(suite, test_strprefix);
   974     cx_test_register(suite, test_strsuffix);
   975     cx_test_register(suite, test_strcaseprefix);
   976     cx_test_register(suite, test_strcasesuffix);
   977     cx_test_register(suite, test_strreplace);
   978     cx_test_register(suite, test_strupper);
   979     cx_test_register(suite, test_strlower);
   980     cx_test_register(suite, test_strtok);
   981     cx_test_register(suite, test_strtok_m);
   982     cx_test_register(suite, test_strtok_delim);
   983     cx_test_register(suite, test_strtok_next_easy);
   984     cx_test_register(suite, test_strtok_next_unlimited);
   985     cx_test_register(suite, test_strtok_next_advanced);
   987     return suite;
   988 }

mercurial