tests/test_string.cpp

Sat, 01 Apr 2023 11:00:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 01 Apr 2023 11:00:59 +0200
changeset 671
d7a67375a7ac
parent 653
e081643aae2a
child 684
380bd45bc94a
permissions
-rw-r--r--

test coverage for cx_strcmp pointer variants

universe@583 1 /*
universe@583 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@583 3 *
universe@583 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@583 5 *
universe@583 6 * Redistribution and use in source and binary forms, with or without
universe@583 7 * modification, are permitted provided that the following conditions are met:
universe@583 8 *
universe@583 9 * 1. Redistributions of source code must retain the above copyright
universe@583 10 * notice, this list of conditions and the following disclaimer.
universe@583 11 *
universe@583 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@583 13 * notice, this list of conditions and the following disclaimer in the
universe@583 14 * documentation and/or other materials provided with the distribution.
universe@583 15 *
universe@583 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@583 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@583 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@583 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@583 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@583 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@583 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@583 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@583 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@583 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@583 26 * POSSIBILITY OF SUCH DAMAGE.
universe@583 27 */
universe@583 28
universe@583 29 #include "cx/string.h"
universe@583 30 #include "util_allocator.h"
universe@583 31
universe@583 32 #include <gtest/gtest.h>
universe@583 33
universe@589 34 #define EXPECT_ZERO_TERMINATED(str) EXPECT_EQ((str).ptr[(str).length], '\0')
universe@589 35
universe@583 36 TEST(String, construct) {
universe@583 37 cxstring s1 = cx_str("1234");
universe@583 38 cxstring s2 = cx_strn("abcd", 2);
universe@583 39 cxmutstr s3 = cx_mutstr((char *) "1234");
universe@583 40 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
universe@583 41
universe@583 42 EXPECT_EQ(s1.length, 4);
universe@583 43 EXPECT_EQ(s2.length, 2);
universe@583 44 EXPECT_EQ(s3.length, 4);
universe@583 45 EXPECT_EQ(s4.length, 2);
universe@583 46 }
universe@583 47
universe@583 48 TEST(String, strfree) {
universe@583 49 CxTestingAllocator alloc;
universe@583 50 auto test = (char *) cxMalloc(&alloc, 16);
universe@583 51 cxmutstr str = cx_mutstrn(test, 16);
universe@583 52 ASSERT_EQ(str.ptr, test);
universe@583 53 EXPECT_EQ(str.length, 16);
universe@583 54 cx_strfree_a(&alloc, &str);
universe@583 55 EXPECT_EQ(str.ptr, nullptr);
universe@583 56 EXPECT_EQ(str.length, 0);
universe@583 57 EXPECT_TRUE(alloc.verify());
universe@583 58 }
universe@583 59
universe@589 60 TEST(String, strdup) {
universe@589 61 cxstring str = CX_STR("test");
universe@589 62 cxmutstr dup = cx_strdup(str);
universe@589 63 ASSERT_EQ(dup.length, str.length);
universe@589 64 EXPECT_STREQ(dup.ptr, str.ptr);
universe@589 65 EXPECT_ZERO_TERMINATED(dup);
universe@589 66 cx_strfree(&dup);
universe@589 67
universe@589 68 str.length = 2;
universe@589 69 dup = cx_strdup(str);
universe@589 70 ASSERT_EQ(dup.length, str.length);
universe@589 71 EXPECT_STREQ(dup.ptr, "te");
universe@589 72 EXPECT_ZERO_TERMINATED(dup);
universe@589 73 cx_strfree(&dup);
universe@589 74 }
universe@589 75
universe@583 76 TEST(String, strlen) {
universe@583 77 cxstring s1 = CX_STR("1234");
universe@583 78 cxstring s2 = CX_STR(".:.:.");
universe@583 79 cxstring s3 = CX_STR("X");
universe@583 80
universe@583 81 size_t len0 = cx_strlen(0);
universe@583 82 size_t len1 = cx_strlen(1, s1);
universe@583 83 size_t len2 = cx_strlen(2, s1, s2);
universe@583 84 size_t len3 = cx_strlen(3, s1, s2, s3);
universe@583 85
universe@583 86 EXPECT_EQ(len0, 0);
universe@583 87 EXPECT_EQ(len1, 4);
universe@583 88 EXPECT_EQ(len2, 9);
universe@583 89 EXPECT_EQ(len3, 10);
universe@583 90 }
universe@583 91
universe@585 92 TEST(String, strsubs) {
universe@585 93 cxstring str = CX_STR("A test string");
universe@585 94
universe@585 95 cxstring sub = cx_strsubs(str, 0);
universe@585 96 EXPECT_EQ(cx_strcmp(sub, str), 0);
universe@585 97
universe@585 98 sub = cx_strsubs(str, 2);
universe@585 99 EXPECT_EQ(cx_strcmp(sub, cx_str("test string")), 0);
universe@585 100
universe@585 101 sub = cx_strsubs(str, 7);
universe@585 102 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0);
universe@585 103
universe@585 104 sub = cx_strsubs(str, 15);
universe@585 105 EXPECT_EQ(cx_strcmp(sub, cx_str("")), 0);
universe@585 106
universe@585 107 sub = cx_strsubsl(str, 2, 4);
universe@585 108 EXPECT_EQ(cx_strcmp(sub, cx_str("test")), 0);
universe@585 109
universe@585 110 sub = cx_strsubsl(str, 7, 3);
universe@585 111 EXPECT_EQ(cx_strcmp(sub, cx_str("str")), 0);
universe@585 112
universe@585 113 sub = cx_strsubsl(str, 7, 20);
universe@585 114 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0);
universe@585 115
universe@585 116 // just for coverage, call the _m variant
universe@585 117 auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0);
universe@585 118 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0);
universe@585 119 }
universe@583 120
universe@583 121 TEST(String, strchr) {
universe@583 122 cxstring str = CX_STR("I will find you - and I will kill you");
universe@583 123
universe@583 124 cxstring notfound = cx_strchr(str, 'x');
universe@583 125 EXPECT_EQ(notfound.length, 0);
universe@583 126
universe@583 127 cxstring result = cx_strchr(str, 'w');
universe@583 128 EXPECT_EQ(result.length, 35);
universe@587 129 EXPECT_STREQ(result.ptr, "will find you - and I will kill you");
universe@585 130
universe@585 131 // just for coverage, call the _m variant
universe@585 132 auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a');
universe@585 133 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0);
universe@583 134 }
universe@583 135
universe@583 136 TEST(String, strrchr) {
universe@583 137 cxstring str = CX_STR("I will find you - and I will kill you");
universe@583 138
universe@583 139 cxstring notfound = cx_strrchr(str, 'x');
universe@583 140 EXPECT_EQ(notfound.length, 0);
universe@583 141
universe@583 142 cxstring result = cx_strrchr(str, 'w');
universe@583 143 EXPECT_EQ(result.length, 13);
universe@587 144 EXPECT_STREQ(result.ptr, "will kill you");
universe@585 145
universe@585 146 // just for coverage, call the _m variant
universe@585 147 auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a');
universe@585 148 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0);
universe@583 149 }
universe@583 150
universe@583 151 TEST(String, strstr) {
universe@583 152 cxstring str = CX_STR("find the match in this string");
universe@583 153 cxstring longstr = CX_STR(
universe@583 154 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
universe@583 155 "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
universe@583 156 "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
universe@583 157 "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
universe@583 158 "abababababababababababababababababababababababababababababababab"
universe@583 159 "abababababababababababababababababababababababababababababababab"
universe@583 160 "abababababababababababababababababababababababababababababababab"
universe@583 161 "abababababababababababababababababababababababababababababababab"
universe@583 162 "abababababababababababababababababababababababababababababababab"
universe@583 163 "abababababababababababababababababababababababababababababababab"
universe@583 164 "wxyz1234567890");
universe@583 165 cxstring longstrpattern = CX_STR(
universe@583 166 "abababababababababababababababababababababababababababababababab"
universe@583 167 "abababababababababababababababababababababababababababababababab"
universe@583 168 "abababababababababababababababababababababababababababababababab"
universe@583 169 "abababababababababababababababababababababababababababababababab"
universe@583 170 "abababababababababababababababababababababababababababababababab"
universe@583 171 );
universe@583 172 cxstring longstrresult = CX_STR(
universe@583 173 "abababababababababababababababababababababababababababababababab"
universe@583 174 "abababababababababababababababababababababababababababababababab"
universe@583 175 "abababababababababababababababababababababababababababababababab"
universe@583 176 "abababababababababababababababababababababababababababababababab"
universe@583 177 "abababababababababababababababababababababababababababababababab"
universe@583 178 "abababababababababababababababababababababababababababababababab"
universe@583 179 "wxyz1234567890"
universe@583 180 );
universe@583 181
universe@583 182 cxstring notfound = cx_strstr(str, cx_str("no match"));
universe@583 183 EXPECT_EQ(notfound.length, 0);
universe@583 184
universe@583 185 cxstring result = cx_strstr(str, cx_str("match"));
universe@583 186 EXPECT_EQ(result.length, 20);
universe@587 187 EXPECT_STREQ(result.ptr, "match in this string");
universe@583 188
universe@583 189 result = cx_strstr(str, cx_str(""));
universe@583 190 EXPECT_EQ(result.length, str.length);
universe@587 191 EXPECT_STREQ(result.ptr, str.ptr);
universe@583 192
universe@583 193 result = cx_strstr(longstr, longstrpattern);
universe@583 194 EXPECT_EQ(result.length, longstrresult.length);
universe@587 195 EXPECT_STREQ(result.ptr, longstrresult.ptr);
universe@585 196
universe@585 197 // just for coverage, call the _m variant
universe@585 198 auto mstr = cx_strdup(longstr);
universe@585 199 auto m = cx_strstr_m(mstr, longstrpattern);
universe@585 200 EXPECT_EQ(m.length, longstrresult.length);
universe@587 201 EXPECT_STREQ(m.ptr, longstrresult.ptr);
universe@585 202 cx_strfree(&mstr);
universe@583 203 }
universe@583 204
universe@583 205 TEST(String, strcmp) {
universe@583 206 cxstring str = CX_STR("compare this");
universe@583 207
universe@588 208 EXPECT_EQ(cx_strcmp(cx_str(""), cx_str("")), 0);
universe@588 209 EXPECT_GT(cx_strcmp(str, cx_str("")), 0);
universe@588 210 EXPECT_EQ(cx_strcmp(str, cx_str("compare this")), 0);
universe@588 211 EXPECT_NE(cx_strcmp(str, cx_str("Compare This")), 0);
universe@588 212 EXPECT_LT(cx_strcmp(str, cx_str("compare tool")), 0);
universe@588 213 EXPECT_GT(cx_strcmp(str, cx_str("compare shit")), 0);
universe@588 214 EXPECT_LT(cx_strcmp(str, cx_str("compare this not")), 0);
universe@588 215 EXPECT_GT(cx_strcmp(str, cx_str("compare")), 0);
universe@671 216
universe@671 217 cxstring str2 = cx_str("Compare This");
universe@671 218 EXPECT_NE(cx_strcmp_p(&str, &str2), 0);
universe@671 219 str2 = cx_str("compare this");
universe@671 220 EXPECT_EQ(cx_strcmp_p(&str, &str2), 0);
universe@583 221 }
universe@583 222
universe@583 223 TEST(String, strcasecmp) {
universe@583 224 cxstring str = CX_STR("compare this");
universe@583 225
universe@588 226 EXPECT_EQ(cx_strcasecmp(cx_str(""), cx_str("")), 0);
universe@588 227 EXPECT_GT(cx_strcasecmp(str, cx_str("")), 0);
universe@588 228 EXPECT_EQ(cx_strcasecmp(str, cx_str("compare this")), 0);
universe@588 229 EXPECT_EQ(cx_strcasecmp(str, cx_str("Compare This")), 0);
universe@588 230 EXPECT_LT(cx_strcasecmp(str, cx_str("compare tool")), 0);
universe@588 231 EXPECT_GT(cx_strcasecmp(str, cx_str("compare shit")), 0);
universe@588 232 EXPECT_LT(cx_strcasecmp(str, cx_str("compare this not")), 0);
universe@588 233 EXPECT_GT(cx_strcasecmp(str, cx_str("compare")), 0);
universe@671 234
universe@671 235 cxstring str2 = cx_str("Compare This");
universe@671 236 EXPECT_EQ(cx_strcasecmp_p(&str, &str2), 0);
universe@671 237 str2 = cx_str("Compare Tool");
universe@671 238 EXPECT_LT(cx_strcasecmp_p(&str, &str2), 0);
universe@583 239 }
universe@583 240
universe@583 241 TEST(String, strcat) {
universe@583 242 cxstring s1 = CX_STR("12");
universe@583 243 cxstring s2 = CX_STR("34");
universe@583 244 cxstring s3 = CX_STR("56");
universe@583 245 cxstring sn = {nullptr, 0};
universe@583 246
universe@583 247 CxTestingAllocator alloc;
universe@583 248
universe@583 249 cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2);
universe@588 250 EXPECT_EQ(cx_strcmp(cx_strcast(t1), cx_str("1234")), 0);
universe@589 251 EXPECT_ZERO_TERMINATED(t1);
universe@583 252 cx_strfree_a(&alloc, &t1);
universe@583 253
universe@583 254 cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3);
universe@588 255 EXPECT_EQ(cx_strcmp(cx_strcast(t2), cx_str("123456")), 0);
universe@589 256 EXPECT_ZERO_TERMINATED(t2);
universe@583 257 cx_strfree_a(&alloc, &t2);
universe@583 258
universe@583 259 cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn);
universe@588 260 EXPECT_EQ(cx_strcmp(cx_strcast(t3), cx_str("123456")), 0);
universe@589 261 EXPECT_ZERO_TERMINATED(t3);
universe@583 262 cx_strfree_a(&alloc, &t3);
universe@583 263
universe@583 264 cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn);
universe@588 265 EXPECT_EQ(cx_strcmp(cx_strcast(t4), cx_str("")), 0);
universe@589 266 EXPECT_ZERO_TERMINATED(t4);
universe@583 267 cx_strfree_a(&alloc, &t4);
universe@583 268
universe@583 269 EXPECT_TRUE(alloc.verify());
universe@597 270
universe@597 271 // use the macro
universe@597 272 cxmutstr t5 = cx_strcat(3, s3, s1, s2);
universe@597 273 EXPECT_EQ(cx_strcmp(cx_strcast(t5), cx_str("561234")), 0);
universe@597 274 EXPECT_ZERO_TERMINATED(t5);
universe@597 275 cx_strfree(&t5);
universe@583 276 }
universe@583 277
universe@583 278 TEST(String, strsplit) {
universe@583 279
universe@583 280 cxstring test = cx_str("this,is,a,csv,string");
universe@583 281 size_t capa = 8;
universe@583 282 cxstring list[8];
universe@583 283 size_t n;
universe@583 284
universe@628 285 // special case: empty string
universe@583 286 n = cx_strsplit(test, cx_str(""), capa, list);
universe@583 287 ASSERT_EQ(n, 1);
universe@583 288 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 289
universe@628 290 // no delimiter occurrence
universe@583 291 n = cx_strsplit(test, cx_str("z"), capa, list);
universe@583 292 ASSERT_EQ(n, 1);
universe@583 293 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 294
universe@628 295 // partially matching delimiter
universe@583 296 n = cx_strsplit(test, cx_str("is,not"), capa, list);
universe@583 297 ASSERT_EQ(n, 1);
universe@583 298 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 299
universe@628 300 // matching single-char delimiter
universe@583 301 n = cx_strsplit(test, cx_str(","), capa, list);
universe@583 302 ASSERT_EQ(n, 5);
universe@583 303 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
universe@583 304 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
universe@583 305 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
universe@583 306 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
universe@583 307 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
universe@583 308
universe@628 309 // matching multi-char delimiter
universe@583 310 n = cx_strsplit(test, cx_str("is"), capa, list);
universe@583 311 ASSERT_EQ(n, 3);
universe@583 312 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 313 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
universe@583 314 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
universe@583 315
universe@628 316 // bounded list using single-char delimiter
universe@583 317 n = cx_strsplit(test, cx_str(","), 3, list);
universe@583 318 ASSERT_EQ(n, 3);
universe@583 319 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
universe@583 320 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
universe@583 321 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
universe@583 322
universe@628 323 // bounded list using multi-char delimiter
universe@583 324 n = cx_strsplit(test, cx_str("is"), 2, list);
universe@583 325 ASSERT_EQ(n, 2);
universe@583 326 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 327 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
universe@583 328
universe@628 329 // start with delimiter
universe@583 330 n = cx_strsplit(test, cx_str("this"), capa, list);
universe@583 331 ASSERT_EQ(n, 2);
universe@583 332 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
universe@583 333 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
universe@583 334
universe@628 335 // end with delimiter
universe@583 336 n = cx_strsplit(test, cx_str("string"), capa, list);
universe@583 337 ASSERT_EQ(n, 2);
universe@583 338 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
universe@583 339 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 340
universe@583 341
universe@628 342 // end with delimiter exceed bound
universe@583 343 n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list);
universe@583 344 ASSERT_EQ(n, 3);
universe@583 345 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
universe@583 346 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
universe@583 347 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
universe@583 348
universe@628 349 // exact match
universe@583 350 n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list);
universe@583 351 ASSERT_EQ(n, 2);
universe@583 352 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
universe@583 353 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 354
universe@628 355 // string to be split is only substring
universe@583 356 n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list);
universe@583 357 ASSERT_EQ(n, 1);
universe@583 358 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 359
universe@628 360 // subsequent encounter of delimiter (the string between is empty)
universe@583 361 n = cx_strsplit(test, cx_str("is,"), capa, list);
universe@583 362 ASSERT_EQ(n, 3);
universe@583 363 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 364 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 365 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
universe@585 366
universe@628 367 // call the _m variant just for coverage
universe@585 368 auto mtest = cx_strdup(test);
universe@585 369 cxmutstr mlist[4];
universe@585 370 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist);
universe@585 371 ASSERT_EQ(n, 3);
universe@585 372 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0);
universe@585 373 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0);
universe@585 374 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0);
universe@585 375 cx_strfree(&mtest);
universe@583 376 }
universe@583 377
universe@583 378 TEST(String, strsplit_a) {
universe@583 379 CxTestingAllocator alloc;
universe@583 380
universe@583 381 cxstring test = cx_str("this,is,a,csv,string");
universe@583 382 size_t capa = 8;
universe@583 383 cxstring *list;
universe@583 384 size_t n;
universe@583 385
universe@628 386 // special case: empty string
universe@583 387 n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list);
universe@583 388 ASSERT_EQ(n, 1);
universe@583 389 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 390 cxFree(&alloc, list);
universe@583 391
universe@628 392 // no delimiter occurrence
universe@583 393 n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list);
universe@583 394 ASSERT_EQ(n, 1);
universe@583 395 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 396 cxFree(&alloc, list);
universe@583 397
universe@628 398 // partially matching delimiter
universe@583 399 n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list);
universe@583 400 ASSERT_EQ(n, 1);
universe@583 401 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 402 cxFree(&alloc, list);
universe@583 403
universe@628 404 // matching single-char delimiter
universe@583 405 n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list);
universe@583 406 ASSERT_EQ(n, 5);
universe@583 407 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
universe@583 408 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
universe@583 409 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
universe@583 410 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
universe@583 411 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
universe@583 412 cxFree(&alloc, list);
universe@583 413
universe@628 414 // matching multi-char delimiter
universe@583 415 n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list);
universe@583 416 ASSERT_EQ(n, 3);
universe@583 417 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 418 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
universe@583 419 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
universe@583 420 cxFree(&alloc, list);
universe@583 421
universe@628 422 // bounded list using single-char delimiter
universe@583 423 n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list);
universe@583 424 ASSERT_EQ(n, 3);
universe@583 425 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
universe@583 426 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
universe@583 427 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
universe@583 428 cxFree(&alloc, list);
universe@583 429
universe@628 430 // bounded list using multi-char delimiter
universe@583 431 n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list);
universe@583 432 ASSERT_EQ(n, 2);
universe@583 433 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 434 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
universe@583 435 cxFree(&alloc, list);
universe@583 436
universe@628 437 // start with delimiter
universe@583 438 n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list);
universe@583 439 ASSERT_EQ(n, 2);
universe@583 440 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
universe@583 441 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
universe@583 442 cxFree(&alloc, list);
universe@583 443
universe@628 444 // end with delimiter
universe@583 445 n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list);
universe@583 446 ASSERT_EQ(n, 2);
universe@583 447 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
universe@583 448 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 449 cxFree(&alloc, list);
universe@583 450
universe@628 451 // end with delimiter exceed bound
universe@583 452 n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list);
universe@583 453 ASSERT_EQ(n, 3);
universe@583 454 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
universe@583 455 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
universe@583 456 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
universe@583 457 cxFree(&alloc, list);
universe@583 458
universe@628 459 // exact match
universe@583 460 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list);
universe@583 461 ASSERT_EQ(n, 2);
universe@583 462 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
universe@583 463 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 464 cxFree(&alloc, list);
universe@583 465
universe@628 466 // string to be split is only substring
universe@583 467 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list);
universe@583 468 ASSERT_EQ(n, 1);
universe@583 469 EXPECT_EQ(cx_strcmp(list[0], test), 0);
universe@583 470 cxFree(&alloc, list);
universe@583 471
universe@628 472 // subsequent encounter of delimiter (the string between is empty)
universe@583 473 n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list);
universe@583 474 ASSERT_EQ(n, 3);
universe@583 475 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
universe@583 476 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
universe@583 477 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
universe@583 478 cxFree(&alloc, list);
universe@583 479
universe@628 480 // call the _m variant just for coverage
universe@585 481 auto mtest = cx_strdup(test);
universe@585 482 cxmutstr *mlist;
universe@585 483 n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist);
universe@585 484 ASSERT_EQ(n, 3);
universe@585 485 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0);
universe@585 486 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0);
universe@585 487 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0);
universe@585 488 cxFree(&alloc, mlist);
universe@585 489 cx_strfree(&mtest);
universe@585 490
universe@583 491 EXPECT_TRUE(alloc.verify());
universe@583 492 }
universe@583 493
universe@583 494 TEST(String, strtrim) {
universe@583 495 cxstring t1 = cx_strtrim(cx_str(" ein test \t "));
universe@583 496 cxstring t2 = cx_strtrim(cx_str("abc"));
universe@583 497 cxstring t3 = cx_strtrim(cx_str(" 123"));
universe@583 498 cxstring t4 = cx_strtrim(cx_str("xyz "));
universe@583 499 cxstring t5 = cx_strtrim(cx_str(" "));
universe@583 500 cxstring empty = cx_strtrim(cx_str(""));
universe@583 501
universe@583 502 EXPECT_EQ(cx_strcmp(t1, cx_str("ein test")), 0);
universe@583 503 EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0);
universe@583 504 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0);
universe@583 505 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0);
universe@583 506 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0);
universe@583 507 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0);
universe@585 508
universe@628 509 // call the _m variant just for coverage
universe@585 510 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t "));
universe@585 511 EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0);
universe@583 512 }
universe@583 513
universe@583 514 TEST(String, strprefix) {
universe@583 515 cxstring str = CX_STR("test my prefix and my suffix");
universe@583 516 cxstring empty = CX_STR("");
universe@583 517 EXPECT_FALSE(cx_strprefix(empty, cx_str("pref")));
universe@583 518 EXPECT_TRUE(cx_strprefix(str, empty));
universe@583 519 EXPECT_TRUE(cx_strprefix(empty, empty));
universe@583 520 EXPECT_TRUE(cx_strprefix(str, cx_str("test ")));
universe@583 521 EXPECT_FALSE(cx_strprefix(str, cx_str("8-) fsck ")));
universe@583 522 }
universe@583 523
universe@583 524 TEST(String, strsuffix) {
universe@583 525 cxstring str = CX_STR("test my prefix and my suffix");
universe@583 526 cxstring empty = CX_STR("");
universe@583 527 EXPECT_FALSE(cx_strsuffix(empty, cx_str("suf")));
universe@583 528 EXPECT_TRUE(cx_strsuffix(str, empty));
universe@583 529 EXPECT_TRUE(cx_strsuffix(empty, empty));
universe@583 530 EXPECT_TRUE(cx_strsuffix(str, cx_str("fix")));
universe@583 531 EXPECT_FALSE(cx_strsuffix(str, cx_str("fox")));
universe@583 532 }
universe@583 533
universe@583 534 TEST(String, strcaseprefix) {
universe@583 535 cxstring str = CX_STR("test my prefix and my suffix");
universe@583 536 cxstring empty = CX_STR("");
universe@583 537 EXPECT_FALSE(cx_strcaseprefix(empty, cx_str("pREf")));
universe@583 538 EXPECT_TRUE(cx_strcaseprefix(str, empty));
universe@583 539 EXPECT_TRUE(cx_strcaseprefix(empty, empty));
universe@583 540 EXPECT_TRUE(cx_strcaseprefix(str, cx_str("TEST ")));
universe@583 541 EXPECT_FALSE(cx_strcaseprefix(str, cx_str("8-) fsck ")));
universe@583 542 }
universe@583 543
universe@583 544 TEST(String, strcasesuffix) {
universe@583 545 cxstring str = CX_STR("test my prefix and my suffix");
universe@583 546 cxstring empty = CX_STR("");
universe@583 547 EXPECT_FALSE(cx_strcasesuffix(empty, cx_str("sUf")));
universe@583 548 EXPECT_TRUE(cx_strcasesuffix(str, empty));
universe@583 549 EXPECT_TRUE(cx_strcasesuffix(empty, empty));
universe@583 550 EXPECT_TRUE(cx_strcasesuffix(str, cx_str("FIX")));
universe@583 551 EXPECT_FALSE(cx_strcasesuffix(str, cx_str("fox")));
universe@583 552 }
universe@583 553
universe@583 554 TEST(String, strreplace) {
universe@598 555 CxTestingAllocator alloc;
universe@583 556 cxstring str = CX_STR("test ababab string aba");
universe@583 557 cxstring longstr = CX_STR(
universe@583 558 "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
universe@583 559 cxstring notrail = CX_STR("test abab");
universe@583 560 cxstring empty = CX_STR("");
universe@583 561 cxstring astr = CX_STR("aaaaaaaaaa");
universe@583 562 cxstring csstr = CX_STR("test AB ab TEST xyz");
universe@583 563
universe@583 564 cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger"));
universe@589 565 auto expected = "test muchlongerab string aba";
universe@583 566
universe@583 567 cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2);
universe@589 568 auto expectedn = "test ccab string aba";
universe@583 569
universe@583 570 cxmutstr longrepl = cx_strreplace(longstr, cx_str("a"), cx_str("z"));
universe@589 571 auto longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd";
universe@583 572
universe@583 573 cxmutstr replnotrail = cx_strreplace(notrail, cx_str("ab"), cx_str("z"));
universe@589 574 auto notrailexpect = "test zz";
universe@583 575
universe@583 576 cxmutstr repleq = cx_strreplace(str, str, cx_str("hello"));
universe@589 577 auto eqexpect = "hello";
universe@583 578
universe@583 579 cxmutstr replempty1 = cx_strreplace(empty, cx_str("ab"), cx_str("c")); // expect: empty
universe@583 580 cxmutstr replempty2 = cx_strreplace(str, cx_str("abab"), empty);
universe@589 581 auto emptyexpect2 = "test ab string aba";
universe@583 582
universe@583 583 cxmutstr replpre = cx_strreplace(str, cx_str("test "), cx_str("TEST "));
universe@589 584 auto preexpected = "TEST ababab string aba";
universe@583 585
universe@583 586 cxmutstr replan1 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 1);
universe@589 587 auto an1expected = "xaaaaaaaaa";
universe@583 588
universe@583 589 cxmutstr replan4 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 4);
universe@589 590 auto an4expected = "xxxxaaaaaa";
universe@583 591
universe@583 592 cxmutstr replan9 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 9);
universe@589 593 auto an9expected = "xxxxxxxxxa";
universe@583 594
universe@583 595 cxmutstr replan10 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 10);
universe@589 596 auto an10expected = "xxxxxxxxxx";
universe@583 597
universe@598 598 cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, cx_str("AB"), cx_str("*"));
universe@598 599 auto expeced1_a = "test * ab TEST xyz";
universe@583 600
universe@598 601 cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, cx_str("test"), cx_str("TEST"));
universe@598 602 auto expected2_a = "TEST AB ab TEST xyz";
universe@583 603
universe@583 604
universe@583 605 EXPECT_NE(repl.ptr, str.ptr);
universe@589 606 EXPECT_ZERO_TERMINATED(repl);
universe@589 607 EXPECT_STREQ(repl.ptr, expected);
universe@589 608 EXPECT_ZERO_TERMINATED(repln);
universe@589 609 EXPECT_STREQ(repln.ptr, expectedn);
universe@589 610 EXPECT_ZERO_TERMINATED(longrepl);
universe@589 611 EXPECT_STREQ(longrepl.ptr, longexpect);
universe@589 612 EXPECT_ZERO_TERMINATED(replnotrail);
universe@589 613 EXPECT_STREQ(replnotrail.ptr, notrailexpect);
universe@589 614 EXPECT_ZERO_TERMINATED(repleq);
universe@589 615 EXPECT_STREQ(repleq.ptr, eqexpect);
universe@589 616 EXPECT_ZERO_TERMINATED(replempty1);
universe@589 617 EXPECT_STREQ(replempty1.ptr, "");
universe@589 618 EXPECT_ZERO_TERMINATED(replempty2);
universe@589 619 EXPECT_STREQ(replempty2.ptr, emptyexpect2);
universe@589 620 EXPECT_ZERO_TERMINATED(replpre);
universe@589 621 EXPECT_STREQ(replpre.ptr, preexpected);
universe@589 622 EXPECT_ZERO_TERMINATED(replan1);
universe@589 623 EXPECT_STREQ(replan1.ptr, an1expected);
universe@589 624 EXPECT_ZERO_TERMINATED(replan4);
universe@589 625 EXPECT_STREQ(replan4.ptr, an4expected);
universe@589 626 EXPECT_ZERO_TERMINATED(replan9);
universe@589 627 EXPECT_STREQ(replan9.ptr, an9expected);
universe@589 628 EXPECT_ZERO_TERMINATED(replan10);
universe@589 629 EXPECT_STREQ(replan10.ptr, an10expected);
universe@598 630 EXPECT_ZERO_TERMINATED(repl1_a);
universe@598 631 EXPECT_STREQ(repl1_a.ptr, expeced1_a);
universe@598 632 EXPECT_ZERO_TERMINATED(repl2_a);
universe@598 633 EXPECT_STREQ(repl2_a.ptr, expected2_a);
universe@583 634
universe@583 635 cx_strfree(&repl);
universe@583 636 cx_strfree(&repln);
universe@583 637 cx_strfree(&longrepl);
universe@583 638 cx_strfree(&replnotrail);
universe@583 639 cx_strfree(&repleq);
universe@583 640 cx_strfree(&replempty1);
universe@583 641 cx_strfree(&replempty2);
universe@583 642 cx_strfree(&replpre);
universe@583 643 cx_strfree(&replan1);
universe@583 644 cx_strfree(&replan4);
universe@583 645 cx_strfree(&replan9);
universe@583 646 cx_strfree(&replan10);
universe@598 647
universe@598 648 cx_strfree_a(&alloc, &repl1_a);
universe@598 649 cx_strfree_a(&alloc, &repl2_a);
universe@598 650 EXPECT_TRUE(alloc.verify());
universe@583 651 }
universe@586 652
universe@586 653 TEST(String, strupper) {
universe@586 654 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t"));
universe@586 655 cx_strupper(str);
universe@586 656 EXPECT_STREQ(str.ptr, "THIS 1S @ TE$T");
universe@586 657 cx_strfree(&str);
universe@586 658 }
universe@586 659
universe@586 660 TEST(String, strlower) {
universe@586 661 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t"));
universe@586 662 cx_strlower(str);
universe@586 663 EXPECT_STREQ(str.ptr, "this 1s @ te$t");
universe@586 664 cx_strfree(&str);
universe@586 665 }
universe@645 666
universe@645 667 TEST(String, strtok) {
universe@645 668 cxstring str = cx_str("a,comma,separated,string");
universe@645 669 cxstring delim = cx_str(",");
universe@645 670 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
universe@645 671 EXPECT_EQ(ctx.str.ptr, str.ptr);
universe@645 672 EXPECT_EQ(ctx.str.length, str.length);
universe@645 673 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
universe@645 674 EXPECT_EQ(ctx.delim.length, delim.length);
universe@645 675 EXPECT_EQ(ctx.limit, 3);
universe@645 676 EXPECT_EQ(ctx.found, 0);
universe@645 677 EXPECT_EQ(ctx.pos, 0);
universe@645 678 EXPECT_EQ(ctx.next_pos, 0);
universe@645 679 EXPECT_EQ(ctx.delim_more, nullptr);
universe@645 680 EXPECT_EQ(ctx.delim_more_count, 0);
universe@645 681 }
universe@645 682
universe@645 683 TEST(String, strtok_m) {
universe@645 684 cxmutstr str = cx_strdup(cx_str("a,comma,separated,string"));
universe@645 685 cxstring delim = cx_str(",");
universe@645 686 CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
universe@645 687 EXPECT_EQ(ctx.str.ptr, str.ptr);
universe@645 688 EXPECT_EQ(ctx.str.length, str.length);
universe@645 689 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
universe@645 690 EXPECT_EQ(ctx.delim.length, delim.length);
universe@645 691 EXPECT_EQ(ctx.limit, 3);
universe@645 692 EXPECT_EQ(ctx.found, 0);
universe@645 693 EXPECT_EQ(ctx.pos, 0);
universe@645 694 EXPECT_EQ(ctx.next_pos, 0);
universe@645 695 EXPECT_EQ(ctx.delim_more, nullptr);
universe@645 696 EXPECT_EQ(ctx.delim_more_count, 0);
universe@645 697 cx_strfree(&str);
universe@645 698 }
universe@645 699
universe@645 700 TEST(String, strtok_delim) {
universe@645 701 cxstring str = cx_str("an,arbitrarily|separated;string");
universe@645 702 cxstring delim = cx_str(",");
universe@645 703 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
universe@645 704 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
universe@645 705 cx_strtok_delim(&ctx, delim_more, 2);
universe@645 706 EXPECT_EQ(ctx.str.ptr, str.ptr);
universe@645 707 EXPECT_EQ(ctx.str.length, str.length);
universe@645 708 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
universe@645 709 EXPECT_EQ(ctx.delim.length, delim.length);
universe@645 710 EXPECT_EQ(ctx.limit, 3);
universe@645 711 EXPECT_EQ(ctx.found, 0);
universe@645 712 EXPECT_EQ(ctx.pos, 0);
universe@645 713 EXPECT_EQ(ctx.next_pos, 0);
universe@645 714 EXPECT_EQ(ctx.delim_more, delim_more);
universe@645 715 EXPECT_EQ(ctx.delim_more_count, 2);
universe@645 716 }
universe@645 717
universe@645 718 TEST(String, strtok_next_easy) {
universe@645 719 cxstring str = cx_str("a,comma,separated,string");
universe@645 720 cxstring delim = cx_str(",");
universe@645 721 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
universe@645 722 bool ret;
universe@645 723 cxstring tok;
universe@645 724
universe@645 725 ret = cx_strtok_next(&ctx, &tok);
universe@645 726 ASSERT_TRUE(ret);
universe@645 727 EXPECT_EQ(cx_strcmp(tok, cx_str("a")), 0);
universe@645 728 EXPECT_EQ(ctx.pos, 0);
universe@645 729 EXPECT_EQ(ctx.next_pos, 2);
universe@645 730 EXPECT_EQ(ctx.delim_pos, 1);
universe@645 731 EXPECT_EQ(ctx.found, 1);
universe@645 732
universe@645 733 ret = cx_strtok_next(&ctx, &tok);
universe@645 734 ASSERT_TRUE(ret);
universe@645 735 EXPECT_EQ(cx_strcmp(tok, cx_str("comma")), 0);
universe@645 736 EXPECT_EQ(ctx.pos, 2);
universe@645 737 EXPECT_EQ(ctx.next_pos, 8);
universe@645 738 EXPECT_EQ(ctx.delim_pos, 7);
universe@645 739 EXPECT_EQ(ctx.found, 2);
universe@645 740
universe@645 741 ret = cx_strtok_next(&ctx, &tok);
universe@645 742 ASSERT_TRUE(ret);
universe@645 743 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
universe@645 744 EXPECT_EQ(ctx.pos, 8);
universe@645 745 EXPECT_EQ(ctx.next_pos, 18);
universe@645 746 EXPECT_EQ(ctx.delim_pos, 17);
universe@645 747 EXPECT_EQ(ctx.found, 3);
universe@645 748
universe@645 749 ret = cx_strtok_next(&ctx, &tok);
universe@645 750 ASSERT_FALSE(ret);
universe@645 751 EXPECT_EQ(ctx.pos, 8);
universe@645 752 EXPECT_EQ(ctx.next_pos, 18);
universe@645 753 EXPECT_EQ(ctx.delim_pos, 17);
universe@645 754 EXPECT_EQ(ctx.found, 3);
universe@645 755 }
universe@645 756
universe@645 757 TEST(String, strtok_next_unlimited) {
universe@645 758 cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;");
universe@645 759 cxstring delim = cx_str(";-;");
universe@645 760 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
universe@645 761 bool ret;
universe@645 762 cxstring tok;
universe@645 763
universe@645 764 ret = cx_strtok_next(&ctx, &tok);
universe@645 765 ASSERT_TRUE(ret);
universe@645 766 EXPECT_EQ(cx_strcmp(tok, cx_str("some")), 0);
universe@645 767 EXPECT_EQ(ctx.pos, 0);
universe@645 768 EXPECT_EQ(ctx.next_pos, 7);
universe@645 769 EXPECT_EQ(ctx.delim_pos, 4);
universe@645 770 EXPECT_EQ(ctx.found, 1);
universe@645 771
universe@645 772 ret = cx_strtok_next(&ctx, &tok);
universe@645 773 ASSERT_TRUE(ret);
universe@645 774 EXPECT_EQ(cx_strcmp(tok, cx_str("otherwise")), 0);
universe@645 775 EXPECT_EQ(ctx.pos, 7);
universe@645 776 EXPECT_EQ(ctx.next_pos, 19);
universe@645 777 EXPECT_EQ(ctx.delim_pos, 16);
universe@645 778 EXPECT_EQ(ctx.found, 2);
universe@645 779
universe@645 780 ret = cx_strtok_next(&ctx, &tok);
universe@645 781 ASSERT_TRUE(ret);
universe@645 782 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
universe@645 783 EXPECT_EQ(ctx.pos, 19);
universe@645 784 EXPECT_EQ(ctx.next_pos, 31);
universe@645 785 EXPECT_EQ(ctx.delim_pos, 28);
universe@645 786 EXPECT_EQ(ctx.found, 3);
universe@645 787
universe@645 788 ret = cx_strtok_next(&ctx, &tok);
universe@645 789 ASSERT_TRUE(ret);
universe@645 790 EXPECT_EQ(cx_strcmp(tok, cx_str("string")), 0);
universe@645 791 EXPECT_EQ(ctx.pos, 31);
universe@645 792 EXPECT_EQ(ctx.next_pos, 40);
universe@645 793 EXPECT_EQ(ctx.delim_pos, 37);
universe@645 794 EXPECT_EQ(ctx.found, 4);
universe@645 795
universe@645 796 ret = cx_strtok_next(&ctx, &tok);
universe@645 797 ASSERT_TRUE(ret);
universe@645 798 EXPECT_EQ(cx_strcmp(tok, cx_str("")), 0);
universe@645 799 EXPECT_EQ(ctx.pos, 40);
universe@645 800 EXPECT_EQ(ctx.next_pos, 40);
universe@645 801 EXPECT_EQ(ctx.delim_pos, 40);
universe@645 802 EXPECT_EQ(ctx.found, 5);
universe@645 803
universe@645 804 ret = cx_strtok_next(&ctx, &tok);
universe@645 805 ASSERT_FALSE(ret);
universe@645 806 EXPECT_EQ(ctx.pos, 40);
universe@645 807 EXPECT_EQ(ctx.delim_pos, 40);
universe@645 808 EXPECT_EQ(ctx.found, 5);
universe@645 809 }
universe@645 810
universe@645 811 TEST(String, strtok_next_advanced) {
universe@645 812 cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string"));
universe@645 813 cxstring delim = cx_str(",");
universe@645 814 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
universe@645 815 CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
universe@645 816 cx_strtok_delim(&ctx, delim_more, 2);
universe@645 817 bool ret;
universe@645 818 cxmutstr tok;
universe@645 819
universe@645 820 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 821 ASSERT_TRUE(ret);
universe@645 822 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("an")), 0);
universe@645 823 EXPECT_EQ(ctx.pos, 0);
universe@645 824 EXPECT_EQ(ctx.next_pos, 3);
universe@645 825 EXPECT_EQ(ctx.delim_pos, 2);
universe@645 826 EXPECT_EQ(ctx.found, 1);
universe@645 827 cx_strupper(tok);
universe@645 828
universe@645 829 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 830 ASSERT_TRUE(ret);
universe@645 831 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("arbitrarily")), 0);
universe@645 832 EXPECT_EQ(ctx.pos, 3);
universe@645 833 EXPECT_EQ(ctx.next_pos, 15);
universe@645 834 EXPECT_EQ(ctx.delim_pos, 14);
universe@645 835 EXPECT_EQ(ctx.found, 2);
universe@645 836 cx_strupper(tok);
universe@645 837
universe@645 838 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 839 ASSERT_TRUE(ret);
universe@645 840 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("")), 0);
universe@645 841 EXPECT_EQ(ctx.pos, 15);
universe@645 842 EXPECT_EQ(ctx.next_pos, 17);
universe@645 843 EXPECT_EQ(ctx.delim_pos, 15);
universe@645 844 EXPECT_EQ(ctx.found, 3);
universe@645 845 cx_strupper(tok);
universe@645 846
universe@645 847 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 848 ASSERT_TRUE(ret);
universe@645 849 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("separated")), 0);
universe@645 850 EXPECT_EQ(ctx.pos, 17);
universe@645 851 EXPECT_EQ(ctx.next_pos, 27);
universe@645 852 EXPECT_EQ(ctx.delim_pos, 26);
universe@645 853 EXPECT_EQ(ctx.found, 4);
universe@645 854 cx_strupper(tok);
universe@645 855
universe@645 856 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 857 ASSERT_TRUE(ret);
universe@645 858 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("string")), 0);
universe@645 859 EXPECT_EQ(ctx.pos, 27);
universe@645 860 EXPECT_EQ(ctx.next_pos, 33);
universe@645 861 EXPECT_EQ(ctx.delim_pos, 33);
universe@645 862 EXPECT_EQ(ctx.found, 5);
universe@645 863 cx_strupper(tok);
universe@645 864
universe@645 865 ret = cx_strtok_next_m(&ctx, &tok);
universe@645 866 ASSERT_FALSE(ret);
universe@645 867 EXPECT_EQ(ctx.pos, 27);
universe@645 868 EXPECT_EQ(ctx.next_pos, 33);
universe@645 869 EXPECT_EQ(ctx.delim_pos, 33);
universe@645 870 EXPECT_EQ(ctx.found, 5);
universe@645 871
universe@645 872 EXPECT_EQ(cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING")), 0);
universe@645 873
universe@645 874 cx_strfree(&str);
universe@645 875 }

mercurial