tests/test_string.c

Sun, 14 Jan 2024 13:50:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Jan 2024 13:50:17 +0100
changeset 806
e06249e09f99
parent 777
e5b29e6f0615
child 809
b7e2e1d7ed22
permissions
-rw-r--r--

add constant for reading out strstr sbo size - relates to #343

also fixes the related test which was working with the old SBO size of 256 and was broken after increasing it to 512

     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     char *longstrc = malloc(longstrlen+1);
   183     char *longstrpatternc = malloc(longstrpatternlen+1);
   185     memcpy(longstrc,
   186            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
   187            "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
   188            "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
   189            "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
   190            "abababababababababababababababababababababababababababababababab",
   191            320
   192     );
   193     memcpy(longstrpatternc,
   194            "abababababababababababababababababababababababababababababababab",
   195            64
   196     );
   197     char x = 'a', y='b', z;
   198     for (size_t i = 0; i < cx_strstr_sbo_size ; i++) {
   199         longstrpatternc[64+i] = x;
   200         longstrc[320+i] = x;
   201         z=x; x=y; y=z;
   202     }
   203     longstrpatternc[longstrpatternlen] = '\0';
   204     memcpy(longstrc+longstrlen-14, "wxyz1234567890", 15);
   206     cxmutstr longstr = cx_mutstrn(longstrc, longstrlen);
   207     cxstring longstrpattern = cx_strn(longstrpatternc, longstrpatternlen);
   208     cxmutstr longstrresult = cx_mutstrn(longstrc+256, longstrlen-256);
   210     CX_TEST_DO {
   211         cxstring notfound = cx_strstr(str, CX_STR("no match"));
   212         CX_TEST_ASSERT(notfound.length == 0);
   214         cxstring result = cx_strstr(str, CX_STR("match"));
   215         CX_TEST_ASSERT(result.length == 20);
   216         CX_TEST_ASSERT(0 == strcmp(result.ptr, "match in this string"));
   218         result = cx_strstr(str, CX_STR(""));
   219         CX_TEST_ASSERT(result.length == str.length);
   220         CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
   222         cxmutstr resultm = cx_strstr_m(longstr, longstrpattern);
   223         CX_TEST_ASSERT(resultm.length == longstrresult.length);
   224         CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr));
   225     }
   227     free(longstrc);
   228     free(longstrpatternc);
   229 }
   231 CX_TEST(test_strcmp) {
   232     cxstring str = CX_STR("compare this");
   233     CX_TEST_DO {
   234         CX_TEST_ASSERT(0 == cx_strcmp(CX_STR(""), CX_STR("")));
   235         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("")));
   236         CX_TEST_ASSERT(0 == cx_strcmp(str, CX_STR("compare this")));
   237         CX_TEST_ASSERT(0 != cx_strcmp(str, CX_STR("Compare This")));
   238         CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare tool")));
   239         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare shit")));
   240         CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare this not")));
   241         CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare")));
   243         cxstring str2 = CX_STR("Compare This");
   244         CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2));
   245         str2 = CX_STR("compare this");
   246         CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2));
   247     }
   248 }
   250 CX_TEST(test_strcasecmp) {
   251     cxstring str = CX_STR("compare this");
   252     CX_TEST_DO {
   253         CX_TEST_ASSERT(0 == cx_strcasecmp(CX_STR(""), CX_STR("")));
   254         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("")));
   255         CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("compare this")));
   256         CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("Compare This")));
   257         CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare tool")));
   258         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare shit")));
   259         CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare this not")));
   260         CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare")));
   262         cxstring str2 = CX_STR("Compare This");
   263         CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2));
   264         str2 = CX_STR("Compare Tool");
   265         CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2));
   266     }
   267 }
   269 CX_TEST(test_strcat) {
   270     cxstring s1 = CX_STR("12");
   271     cxstring s2 = CX_STR("34");
   272     cxstring s3 = CX_STR("56");
   273     cxstring sn = {NULL, 0};
   275     CxTestingAllocator talloc;
   276     cx_testing_allocator_init(&talloc);
   277     CxAllocator *alloc = &talloc.base;
   279     CX_TEST_DO {
   280         cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2);
   281         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t1), CX_STR("1234")));
   282         ASSERT_ZERO_TERMINATED(t1);
   283         cx_strfree_a(alloc, &t1);
   285         cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3);
   286         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t2), CX_STR("123456")));
   287         ASSERT_ZERO_TERMINATED(t2);
   288         cx_strfree_a(alloc, &t2);
   290         cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn);
   291         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t3), CX_STR("123456")));
   292         ASSERT_ZERO_TERMINATED(t3);
   293         cx_strfree_a(alloc, &t3);
   295         cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn);
   296         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t4), CX_STR("")));
   297         ASSERT_ZERO_TERMINATED(t4);
   298         cx_strfree_a(alloc, &t4);
   300         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   302         // use the macro
   303         cxmutstr t5 = cx_strcat(3, s3, s1, s2);
   304         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t5), CX_STR("561234")));
   305         ASSERT_ZERO_TERMINATED(t5);
   306         cx_strfree(&t5);
   308         // use an initial string
   309         cxmutstr t6 = cx_strdup(CX_STR("Hello"));
   310         t6 = cx_strcat_m(t6, 2, CX_STR(", "), CX_STR("World!"));
   311         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t6), CX_STR("Hello, World!")));
   312         ASSERT_ZERO_TERMINATED(t6);
   313         cx_strfree(&t6);
   314     }
   315     cx_testing_allocator_destroy(&talloc);
   316 }
   318 CX_TEST(test_strsplit) {
   319     cxstring test = CX_STR("this,is,a,csv,string");
   320     size_t capa = 8;
   321     cxstring list[8];
   322     size_t n;
   323     CX_TEST_DO {
   324         // special case: empty string
   325         n = cx_strsplit(test, CX_STR(""), capa, list);
   326         CX_TEST_ASSERT(n == 1);
   327         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   329         // no delimiter occurrence
   330         n = cx_strsplit(test, CX_STR("z"), capa, list);
   331         CX_TEST_ASSERT(n == 1);
   332         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   334         // partially matching delimiter
   335         n = cx_strsplit(test, CX_STR("is,not"), capa, list);
   336         CX_TEST_ASSERT(n == 1);
   337         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   339         // matching single-char delimiter
   340         n = cx_strsplit(test, CX_STR(","), capa, list);
   341         CX_TEST_ASSERT(n == 5);
   342         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   343         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   344         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
   345         CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
   346         CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
   348         // matching multi-char delimiter
   349         n = cx_strsplit(test, CX_STR("is"), capa, list);
   350         CX_TEST_ASSERT(n == 3);
   351         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   352         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
   353         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
   355         // bounded list using single-char delimiter
   356         n = cx_strsplit(test, CX_STR(","), 3, list);
   357         CX_TEST_ASSERT(n == 3);
   358         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   359         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   360         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   362         // bounded list using multi-char delimiter
   363         n = cx_strsplit(test, CX_STR("is"), 2, list);
   364         CX_TEST_ASSERT(n == 2);
   365         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   366         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   368         // start with delimiter
   369         n = cx_strsplit(test, CX_STR("this"), capa, list);
   370         CX_TEST_ASSERT(n == 2);
   371         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   372         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   374         // end with delimiter
   375         n = cx_strsplit(test, CX_STR("string"), capa, list);
   376         CX_TEST_ASSERT(n == 2);
   377         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
   378         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   381         // end with delimiter exceed bound
   382         n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list);
   383         CX_TEST_ASSERT(n == 3);
   384         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
   385         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
   386         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
   388         // exact match
   389         n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list);
   390         CX_TEST_ASSERT(n == 2);
   391         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   392         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   394         // string to be split is only substring
   395         n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list);
   396         CX_TEST_ASSERT(n == 1);
   397         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   399         // subsequent encounter of delimiter (the string between is empty)
   400         n = cx_strsplit(test, CX_STR("is,"), capa, list);
   401         CX_TEST_ASSERT(n == 3);
   402         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   403         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   404         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   406         // call the _m variant just for coverage
   407         cxmutstr mtest = cx_strdup(test);
   408         cxmutstr mlist[4];
   409         n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist);
   410         CX_TEST_ASSERT(n == 3);
   411         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
   412         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
   413         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
   414         cx_strfree(&mtest);
   415     }
   416 }
   418 CX_TEST(test_strsplit_a) {
   419     CxTestingAllocator talloc;
   420     cx_testing_allocator_init(&talloc);
   421     CxAllocator *alloc = &talloc.base;
   423     cxstring test = CX_STR("this,is,a,csv,string");
   424     size_t capa = 8;
   425     cxstring *list;
   426     size_t n;
   427     CX_TEST_DO {
   428         // special case: empty string
   429         n = cx_strsplit_a(alloc, test, CX_STR(""), capa, &list);
   430         CX_TEST_ASSERT(n == 1);
   431         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   432         cxFree(alloc, list);
   434         // no delimiter occurrence
   435         n = cx_strsplit_a(alloc, test, CX_STR("z"), capa, &list);
   436         CX_TEST_ASSERT(n == 1);
   437         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   438         cxFree(alloc, list);
   440         // partially matching delimiter
   441         n = cx_strsplit_a(alloc, test, CX_STR("is,not"), capa, &list);
   442         CX_TEST_ASSERT(n == 1);
   443         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   444         cxFree(alloc, list);
   446         // matching single-char delimiter
   447         n = cx_strsplit_a(alloc, test, CX_STR(","), capa, &list);
   448         CX_TEST_ASSERT(n == 5);
   449         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   450         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   451         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
   452         CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
   453         CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
   454         cxFree(alloc, list);
   456         // matching multi-char delimiter
   457         n = cx_strsplit_a(alloc, test, CX_STR("is"), capa, &list);
   458         CX_TEST_ASSERT(n == 3);
   459         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   460         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
   461         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
   462         cxFree(alloc, list);
   464         // bounded list using single-char delimiter
   465         n = cx_strsplit_a(alloc, test, CX_STR(","), 3, &list);
   466         CX_TEST_ASSERT(n == 3);
   467         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
   468         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
   469         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   470         cxFree(alloc, list);
   472         // bounded list using multi-char delimiter
   473         n = cx_strsplit_a(alloc, test, CX_STR("is"), 2, &list);
   474         CX_TEST_ASSERT(n == 2);
   475         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   476         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   477         cxFree(alloc, list);
   479         // start with delimiter
   480         n = cx_strsplit_a(alloc, test, CX_STR("this"), capa, &list);
   481         CX_TEST_ASSERT(n == 2);
   482         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   483         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
   484         cxFree(alloc, list);
   486         // end with delimiter
   487         n = cx_strsplit_a(alloc, test, CX_STR("string"), capa, &list);
   488         CX_TEST_ASSERT(n == 2);
   489         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
   490         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   491         cxFree(alloc, list);
   493         // end with delimiter exceed bound
   494         n = cx_strsplit_a(alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list);
   495         CX_TEST_ASSERT(n == 3);
   496         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
   497         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
   498         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
   499         cxFree(alloc, list);
   501         // exact match
   502         n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string"), capa, &list);
   503         CX_TEST_ASSERT(n == 2);
   504         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
   505         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   506         cxFree(alloc, list);
   508         // string to be split is only substring
   509         n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list);
   510         CX_TEST_ASSERT(n == 1);
   511         CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
   512         cxFree(alloc, list);
   514         // subsequent encounter of delimiter (the string between is empty)
   515         n = cx_strsplit_a(alloc, test, CX_STR("is,"), capa, &list);
   516         CX_TEST_ASSERT(n == 3);
   517         CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
   518         CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
   519         CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
   520         cxFree(alloc, list);
   522         // call the _m variant just for coverage
   523         cxmutstr mtest = cx_strdup(test);
   524         cxmutstr *mlist;
   525         n = cx_strsplit_ma(alloc, mtest, CX_STR("is,"), 4, &mlist);
   526         CX_TEST_ASSERT(n == 3);
   527         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
   528         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
   529         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
   530         cxFree(alloc, mlist);
   531         cx_strfree(&mtest);
   533         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   534     }
   535     cx_testing_allocator_destroy(&talloc);
   536 }
   538 CX_TEST(test_strtrim) {
   539     cxstring t1 = cx_strtrim(CX_STR("  ein test  \t "));
   540     cxstring t2 = cx_strtrim(CX_STR("abc"));
   541     cxstring t3 = cx_strtrim(CX_STR(" 123"));
   542     cxstring t4 = cx_strtrim(CX_STR("xyz "));
   543     cxstring t5 = cx_strtrim(CX_STR("   "));
   544     cxstring empty = cx_strtrim(CX_STR(""));
   546     CX_TEST_DO {
   547         CX_TEST_ASSERT(0 == cx_strcmp(t1, CX_STR("ein test")));
   548         CX_TEST_ASSERT(0 == cx_strcmp(t2, CX_STR("abc")));
   549         CX_TEST_ASSERT(0 == cx_strcmp(t3, CX_STR("123")));
   550         CX_TEST_ASSERT(0 == cx_strcmp(t4, CX_STR("xyz")));
   551         CX_TEST_ASSERT(0 == cx_strcmp(t5, CX_STR("")));
   552         CX_TEST_ASSERT(0 == cx_strcmp(empty, CX_STR("")));
   554         // call the _m variant just for coverage
   555         cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) "  ein test  \t "));
   556         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m1), CX_STR("ein test")));
   557     }
   558 }
   560 CX_TEST(test_strprefix) {
   561     cxstring str = CX_STR("test my prefix and my suffix");
   562     cxstring empty = CX_STR("");
   563     CX_TEST_DO {
   564         CX_TEST_ASSERT(!cx_strprefix(empty, CX_STR("pref")));
   565         CX_TEST_ASSERT(cx_strprefix(str, empty));
   566         CX_TEST_ASSERT(cx_strprefix(empty, empty));
   567         CX_TEST_ASSERT(cx_strprefix(str, CX_STR("test ")));
   568         CX_TEST_ASSERT(!cx_strprefix(str, CX_STR("8-) fsck ")));
   569     }
   570 }
   572 CX_TEST(test_strsuffix) {
   573     cxstring str = CX_STR("test my prefix and my suffix");
   574     cxstring empty = CX_STR("");
   575     CX_TEST_DO {
   576         CX_TEST_ASSERT(!cx_strsuffix(empty, CX_STR("suf")));
   577         CX_TEST_ASSERT(cx_strsuffix(str, empty));
   578         CX_TEST_ASSERT(cx_strsuffix(empty, empty));
   579         CX_TEST_ASSERT(cx_strsuffix(str, CX_STR("fix")));
   580         CX_TEST_ASSERT(!cx_strsuffix(str, CX_STR("fox")));
   581     }
   582 }
   584 CX_TEST(test_strcaseprefix) {
   585     cxstring str = CX_STR("test my prefix and my suffix");
   586     cxstring empty = CX_STR("");
   587     CX_TEST_DO {
   588         CX_TEST_ASSERT(!cx_strcaseprefix(empty, CX_STR("pREf")));
   589         CX_TEST_ASSERT(cx_strcaseprefix(str, empty));
   590         CX_TEST_ASSERT(cx_strcaseprefix(empty, empty));
   591         CX_TEST_ASSERT(cx_strcaseprefix(str, CX_STR("TEST ")));
   592         CX_TEST_ASSERT(!cx_strcaseprefix(str, CX_STR("8-) fsck ")));
   593     }
   594 }
   596 CX_TEST(test_strcasesuffix) {
   597     cxstring str = CX_STR("test my prefix and my suffix");
   598     cxstring empty = CX_STR("");
   599     CX_TEST_DO {
   600         CX_TEST_ASSERT(!cx_strcasesuffix(empty, CX_STR("sUf")));
   601         CX_TEST_ASSERT(cx_strcasesuffix(str, empty));
   602         CX_TEST_ASSERT(cx_strcasesuffix(empty, empty));
   603         CX_TEST_ASSERT(cx_strcasesuffix(str, CX_STR("FIX")));
   604         CX_TEST_ASSERT(!cx_strcasesuffix(str, CX_STR("fox")));
   605     }
   606 }
   608 CX_TEST(test_strreplace) {
   609     CxTestingAllocator talloc;
   610     cx_testing_allocator_init(&talloc);
   611     CxAllocator *alloc = &talloc.base;
   613     cxstring str = CX_STR("test ababab string aba");
   614     cxstring longstr = CX_STR(
   615             "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
   616     cxstring notrail = CX_STR("test abab");
   617     cxstring empty = CX_STR("");
   618     cxstring astr = CX_STR("aaaaaaaaaa");
   619     cxstring csstr = CX_STR("test AB ab TEST xyz");
   621     cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger"));
   622     char const *expected = "test muchlongerab string aba";
   624     cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2);
   625     char const *expectedn = "test ccab string aba";
   627     cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z"));
   628     char const *longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd";
   630     cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z"));
   631     char const *notrailexpect = "test zz";
   633     cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello"));
   634     char const *eqexpect = "hello";
   636     cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty
   637     cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty);
   638     char const *emptyexpect2 = "test ab string aba";
   640     cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST "));
   641     char const *preexpected = "TEST ababab string aba";
   643     cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1);
   644     char const *an1expected = "xaaaaaaaaa";
   646     cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4);
   647     char const *an4expected = "xxxxaaaaaa";
   649     cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9);
   650     char const *an9expected = "xxxxxxxxxa";
   652     cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10);
   653     char const *an10expected = "xxxxxxxxxx";
   655     CX_TEST_DO {
   656         cxmutstr repl1_a = cx_strreplace_a(alloc, csstr, CX_STR("AB"), CX_STR("*"));
   657         char const *expeced1_a = "test * ab TEST xyz";
   659         cxmutstr repl2_a = cx_strreplace_a(alloc, csstr, CX_STR("test"), CX_STR("TEST"));
   660         char const *expected2_a = "TEST AB ab TEST xyz";
   662         CX_TEST_ASSERT(repl.ptr != str.ptr);
   663         ASSERT_ZERO_TERMINATED(repl);
   664         CX_TEST_ASSERT(0 == strcmp(repl.ptr, expected));
   665         ASSERT_ZERO_TERMINATED(repln);
   666         CX_TEST_ASSERT(0 == strcmp(repln.ptr, expectedn));
   667         ASSERT_ZERO_TERMINATED(longrepl);
   668         CX_TEST_ASSERT(0 == strcmp(longrepl.ptr, longexpect));
   669         ASSERT_ZERO_TERMINATED(replnotrail);
   670         CX_TEST_ASSERT(0 == strcmp(replnotrail.ptr, notrailexpect));
   671         ASSERT_ZERO_TERMINATED(repleq);
   672         CX_TEST_ASSERT(0 == strcmp(repleq.ptr, eqexpect));
   673         ASSERT_ZERO_TERMINATED(replempty1);
   674         CX_TEST_ASSERT(0 == strcmp(replempty1.ptr, ""));
   675         ASSERT_ZERO_TERMINATED(replempty2);
   676         CX_TEST_ASSERT(0 == strcmp(replempty2.ptr, emptyexpect2));
   677         ASSERT_ZERO_TERMINATED(replpre);
   678         CX_TEST_ASSERT(0 == strcmp(replpre.ptr, preexpected));
   679         ASSERT_ZERO_TERMINATED(replan1);
   680         CX_TEST_ASSERT(0 == strcmp(replan1.ptr, an1expected));
   681         ASSERT_ZERO_TERMINATED(replan4);
   682         CX_TEST_ASSERT(0 == strcmp(replan4.ptr, an4expected));
   683         ASSERT_ZERO_TERMINATED(replan9);
   684         CX_TEST_ASSERT(0 == strcmp(replan9.ptr, an9expected));
   685         ASSERT_ZERO_TERMINATED(replan10);
   686         CX_TEST_ASSERT(0 == strcmp(replan10.ptr, an10expected));
   687         ASSERT_ZERO_TERMINATED(repl1_a);
   688         CX_TEST_ASSERT(0 == strcmp(repl1_a.ptr, expeced1_a));
   689         ASSERT_ZERO_TERMINATED(repl2_a);
   690         CX_TEST_ASSERT(0 == strcmp(repl2_a.ptr, expected2_a));
   692         cx_strfree_a(alloc, &repl1_a);
   693         cx_strfree_a(alloc, &repl2_a);
   694         CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   695     }
   697     cx_strfree(&repl);
   698     cx_strfree(&repln);
   699     cx_strfree(&longrepl);
   700     cx_strfree(&replnotrail);
   701     cx_strfree(&repleq);
   702     cx_strfree(&replempty1);
   703     cx_strfree(&replempty2);
   704     cx_strfree(&replpre);
   705     cx_strfree(&replan1);
   706     cx_strfree(&replan4);
   707     cx_strfree(&replan9);
   708     cx_strfree(&replan10);
   709     cx_testing_allocator_destroy(&talloc);
   710 }
   712 CX_TEST(test_strupper) {
   713     cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
   714     CX_TEST_DO {
   715         cx_strupper(str);
   716         CX_TEST_ASSERT(0 == strcmp(str.ptr, "THIS 1S @ TE$T"));
   717     }
   718     cx_strfree(&str);
   719 }
   721 CX_TEST(test_strlower) {
   722     cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
   723     CX_TEST_DO {
   724         cx_strlower(str);
   725         CX_TEST_ASSERT(0 == strcmp(str.ptr, "this 1s @ te$t"));
   726     }
   727     cx_strfree(&str);
   728 }
   730 CX_TEST(test_strtok) {
   731     cxstring str = CX_STR("a,comma,separated,string");
   732     cxstring delim = CX_STR(",");
   733     CX_TEST_DO {
   734         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   735         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   736         CX_TEST_ASSERT(ctx.str.length == str.length);
   737         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   738         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   739         CX_TEST_ASSERT(ctx.limit == 3);
   740         CX_TEST_ASSERT(ctx.found == 0);
   741         CX_TEST_ASSERT(ctx.pos == 0);
   742         CX_TEST_ASSERT(ctx.next_pos == 0);
   743         CX_TEST_ASSERT(ctx.delim_more == NULL);
   744         CX_TEST_ASSERT(ctx.delim_more_count == 0);
   745     }
   746 }
   748 CX_TEST(test_strtok_m) {
   749     cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string"));
   750     cxstring delim = CX_STR(",");
   751     CX_TEST_DO {
   752         CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
   753         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   754         CX_TEST_ASSERT(ctx.str.length == str.length);
   755         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   756         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   757         CX_TEST_ASSERT(ctx.limit == 3);
   758         CX_TEST_ASSERT(ctx.found == 0);
   759         CX_TEST_ASSERT(ctx.pos == 0);
   760         CX_TEST_ASSERT(ctx.next_pos == 0);
   761         CX_TEST_ASSERT(ctx.delim_more == NULL);
   762         CX_TEST_ASSERT(ctx.delim_more_count == 0);
   763     }
   764     cx_strfree(&str);
   765 }
   767 CX_TEST(test_strtok_delim) {
   768     cxstring str = CX_STR("an,arbitrarily|separated;string");
   769     cxstring delim = CX_STR(",");
   770     cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
   771     CX_TEST_DO {
   772         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   773         cx_strtok_delim(&ctx, delim_more, 2);
   774         CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
   775         CX_TEST_ASSERT(ctx.str.length == str.length);
   776         CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
   777         CX_TEST_ASSERT(ctx.delim.length == delim.length);
   778         CX_TEST_ASSERT(ctx.limit == 3);
   779         CX_TEST_ASSERT(ctx.found == 0);
   780         CX_TEST_ASSERT(ctx.pos == 0);
   781         CX_TEST_ASSERT(ctx.next_pos == 0);
   782         CX_TEST_ASSERT(ctx.delim_more == delim_more);
   783         CX_TEST_ASSERT(ctx.delim_more_count == 2);
   784     }
   785 }
   787 CX_TEST(test_strtok_next_easy) {
   788     cxstring str = CX_STR("a,comma,separated,string");
   789     cxstring delim = CX_STR(",");
   790     CX_TEST_DO {
   791         CxStrtokCtx ctx = cx_strtok(str, delim, 3);
   792         bool ret;
   793         cxstring tok;
   795         ret = cx_strtok_next(&ctx, &tok);
   796         CX_TEST_ASSERT(ret);
   797         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("a")));
   798         CX_TEST_ASSERT(ctx.pos == 0);
   799         CX_TEST_ASSERT(ctx.next_pos == 2);
   800         CX_TEST_ASSERT(ctx.delim_pos == 1);
   801         CX_TEST_ASSERT(ctx.found == 1);
   803         ret = cx_strtok_next(&ctx, &tok);
   804         CX_TEST_ASSERT(ret);
   805         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("comma")));
   806         CX_TEST_ASSERT(ctx.pos == 2);
   807         CX_TEST_ASSERT(ctx.next_pos == 8);
   808         CX_TEST_ASSERT(ctx.delim_pos == 7);
   809         CX_TEST_ASSERT(ctx.found == 2);
   811         ret = cx_strtok_next(&ctx, &tok);
   812         CX_TEST_ASSERT(ret);
   813         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
   814         CX_TEST_ASSERT(ctx.pos == 8);
   815         CX_TEST_ASSERT(ctx.next_pos == 18);
   816         CX_TEST_ASSERT(ctx.delim_pos == 17);
   817         CX_TEST_ASSERT(ctx.found == 3);
   819         ret = cx_strtok_next(&ctx, &tok);
   820         CX_TEST_ASSERT(!ret);
   821         CX_TEST_ASSERT(ctx.pos == 8);
   822         CX_TEST_ASSERT(ctx.next_pos == 18);
   823         CX_TEST_ASSERT(ctx.delim_pos == 17);
   824         CX_TEST_ASSERT(ctx.found == 3);
   825     }
   826 }
   828 CX_TEST(test_strtok_next_unlimited) {
   829     cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;");
   830     cxstring delim = CX_STR(";-;");
   831     CX_TEST_DO {
   832         CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
   833         bool ret;
   834         cxstring tok;
   836         ret = cx_strtok_next(&ctx, &tok);
   837         CX_TEST_ASSERT(ret);
   838         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("some")));
   839         CX_TEST_ASSERT(ctx.pos == 0);
   840         CX_TEST_ASSERT(ctx.next_pos == 7);
   841         CX_TEST_ASSERT(ctx.delim_pos == 4);
   842         CX_TEST_ASSERT(ctx.found == 1);
   844         ret = cx_strtok_next(&ctx, &tok);
   845         CX_TEST_ASSERT(ret);
   846         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("otherwise")));
   847         CX_TEST_ASSERT(ctx.pos == 7);
   848         CX_TEST_ASSERT(ctx.next_pos == 19);
   849         CX_TEST_ASSERT(ctx.delim_pos == 16);
   850         CX_TEST_ASSERT(ctx.found == 2);
   852         ret = cx_strtok_next(&ctx, &tok);
   853         CX_TEST_ASSERT(ret);
   854         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
   855         CX_TEST_ASSERT(ctx.pos == 19);
   856         CX_TEST_ASSERT(ctx.next_pos == 31);
   857         CX_TEST_ASSERT(ctx.delim_pos == 28);
   858         CX_TEST_ASSERT(ctx.found == 3);
   860         ret = cx_strtok_next(&ctx, &tok);
   861         CX_TEST_ASSERT(ret);
   862         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("string")));
   863         CX_TEST_ASSERT(ctx.pos == 31);
   864         CX_TEST_ASSERT(ctx.next_pos == 40);
   865         CX_TEST_ASSERT(ctx.delim_pos == 37);
   866         CX_TEST_ASSERT(ctx.found == 4);
   868         ret = cx_strtok_next(&ctx, &tok);
   869         CX_TEST_ASSERT(ret);
   870         CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("")));
   871         CX_TEST_ASSERT(ctx.pos == 40);
   872         CX_TEST_ASSERT(ctx.next_pos == 40);
   873         CX_TEST_ASSERT(ctx.delim_pos == 40);
   874         CX_TEST_ASSERT(ctx.found == 5);
   876         ret = cx_strtok_next(&ctx, &tok);
   877         CX_TEST_ASSERT(!ret);
   878         CX_TEST_ASSERT(ctx.pos == 40);
   879         CX_TEST_ASSERT(ctx.delim_pos == 40);
   880         CX_TEST_ASSERT(ctx.found == 5);
   881     }
   882 }
   884 CX_TEST(test_strtok_next_advanced) {
   885     cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string"));
   886     cxstring delim = CX_STR(",");
   887     cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
   888     CX_TEST_DO {
   889         CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
   890         cx_strtok_delim(&ctx, delim_more, 2);
   891         bool ret;
   892         cxmutstr tok;
   894         ret = cx_strtok_next_m(&ctx, &tok);
   895         CX_TEST_ASSERT(ret);
   896         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("an")));
   897         CX_TEST_ASSERT(ctx.pos == 0);
   898         CX_TEST_ASSERT(ctx.next_pos == 3);
   899         CX_TEST_ASSERT(ctx.delim_pos == 2);
   900         CX_TEST_ASSERT(ctx.found == 1);
   901         cx_strupper(tok);
   903         ret = cx_strtok_next_m(&ctx, &tok);
   904         CX_TEST_ASSERT(ret);
   905         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")));
   906         CX_TEST_ASSERT(ctx.pos == 3);
   907         CX_TEST_ASSERT(ctx.next_pos == 15);
   908         CX_TEST_ASSERT(ctx.delim_pos == 14);
   909         CX_TEST_ASSERT(ctx.found == 2);
   910         cx_strupper(tok);
   912         ret = cx_strtok_next_m(&ctx, &tok);
   913         CX_TEST_ASSERT(ret);
   914         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("")));
   915         CX_TEST_ASSERT(ctx.pos == 15);
   916         CX_TEST_ASSERT(ctx.next_pos == 17);
   917         CX_TEST_ASSERT(ctx.delim_pos == 15);
   918         CX_TEST_ASSERT(ctx.found == 3);
   919         cx_strupper(tok);
   921         ret = cx_strtok_next_m(&ctx, &tok);
   922         CX_TEST_ASSERT(ret);
   923         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("separated")));
   924         CX_TEST_ASSERT(ctx.pos == 17);
   925         CX_TEST_ASSERT(ctx.next_pos == 27);
   926         CX_TEST_ASSERT(ctx.delim_pos == 26);
   927         CX_TEST_ASSERT(ctx.found == 4);
   928         cx_strupper(tok);
   930         ret = cx_strtok_next_m(&ctx, &tok);
   931         CX_TEST_ASSERT(ret);
   932         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("string")));
   933         CX_TEST_ASSERT(ctx.pos == 27);
   934         CX_TEST_ASSERT(ctx.next_pos == 33);
   935         CX_TEST_ASSERT(ctx.delim_pos == 33);
   936         CX_TEST_ASSERT(ctx.found == 5);
   937         cx_strupper(tok);
   939         ret = cx_strtok_next_m(&ctx, &tok);
   940         CX_TEST_ASSERT(!ret);
   941         CX_TEST_ASSERT(ctx.pos == 27);
   942         CX_TEST_ASSERT(ctx.next_pos == 33);
   943         CX_TEST_ASSERT(ctx.delim_pos == 33);
   944         CX_TEST_ASSERT(ctx.found == 5);
   946         CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")));
   947     }
   948     cx_strfree(&str);
   949 }
   952 CxTestSuite *cx_test_suite_string(void) {
   953     CxTestSuite *suite = cx_test_suite_new("string");
   955     cx_test_register(suite, test_string_construct);
   956     cx_test_register(suite, test_strfree);
   957     cx_test_register(suite, test_strdup);
   958     cx_test_register(suite, test_strdup_shortened);
   959     cx_test_register(suite, test_strlen);
   960     cx_test_register(suite, test_strsubs);
   961     cx_test_register(suite, test_strchr);
   962     cx_test_register(suite, test_strrchr);
   963     cx_test_register(suite, test_strstr);
   964     cx_test_register(suite, test_strcmp);
   965     cx_test_register(suite, test_strcasecmp);
   966     cx_test_register(suite, test_strcat);
   967     cx_test_register(suite, test_strsplit);
   968     cx_test_register(suite, test_strsplit_a);
   969     cx_test_register(suite, test_strtrim);
   970     cx_test_register(suite, test_strprefix);
   971     cx_test_register(suite, test_strsuffix);
   972     cx_test_register(suite, test_strcaseprefix);
   973     cx_test_register(suite, test_strcasesuffix);
   974     cx_test_register(suite, test_strreplace);
   975     cx_test_register(suite, test_strupper);
   976     cx_test_register(suite, test_strlower);
   977     cx_test_register(suite, test_strtok);
   978     cx_test_register(suite, test_strtok_m);
   979     cx_test_register(suite, test_strtok_delim);
   980     cx_test_register(suite, test_strtok_next_easy);
   981     cx_test_register(suite, test_strtok_next_unlimited);
   982     cx_test_register(suite, test_strtok_next_advanced);
   984     return suite;
   985 }

mercurial