test/test_string.cpp

changeset 583
0f3c9662f9b5
child 585
038f5e99e00f
equal deleted inserted replaced
582:96fa7fa6af4f 583:0f3c9662f9b5
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 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 */
28
29 #include "cx/string.h"
30 #include "util_allocator.h"
31
32 #include <gtest/gtest.h>
33
34 TEST(String, construct) {
35 cxstring s1 = cx_str("1234");
36 cxstring s2 = cx_strn("abcd", 2);
37 cxmutstr s3 = cx_mutstr((char *) "1234");
38 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
39
40 EXPECT_EQ(s1.length, 4);
41 EXPECT_EQ(s2.length, 2);
42 EXPECT_EQ(s3.length, 4);
43 EXPECT_EQ(s4.length, 2);
44 }
45
46 TEST(String, strfree) {
47 CxTestingAllocator alloc;
48 auto test = (char *) cxMalloc(&alloc, 16);
49 cxmutstr str = cx_mutstrn(test, 16);
50 ASSERT_EQ(str.ptr, test);
51 EXPECT_EQ(str.length, 16);
52 cx_strfree_a(&alloc, &str);
53 EXPECT_EQ(str.ptr, nullptr);
54 EXPECT_EQ(str.length, 0);
55 EXPECT_TRUE(alloc.verify());
56 }
57
58 TEST(String, strlen) {
59 cxstring s1 = CX_STR("1234");
60 cxstring s2 = CX_STR(".:.:.");
61 cxstring s3 = CX_STR("X");
62
63 size_t len0 = cx_strlen(0);
64 size_t len1 = cx_strlen(1, s1);
65 size_t len2 = cx_strlen(2, s1, s2);
66 size_t len3 = cx_strlen(3, s1, s2, s3);
67
68 EXPECT_EQ(len0, 0);
69 EXPECT_EQ(len1, 4);
70 EXPECT_EQ(len2, 9);
71 EXPECT_EQ(len3, 10);
72 }
73
74
75 TEST(String, strchr) {
76 cxstring str = CX_STR("I will find you - and I will kill you");
77
78 cxstring notfound = cx_strchr(str, 'x');
79 EXPECT_EQ(notfound.length, 0);
80
81 cxstring result = cx_strchr(str, 'w');
82 EXPECT_EQ(result.length, 35);
83 EXPECT_EQ(strcmp("will find you - and I will kill you", result.ptr), 0);
84 }
85
86 TEST(String, strrchr) {
87 cxstring str = CX_STR("I will find you - and I will kill you");
88
89 cxstring notfound = cx_strrchr(str, 'x');
90 EXPECT_EQ(notfound.length, 0);
91
92 cxstring result = cx_strrchr(str, 'w');
93 EXPECT_EQ(result.length, 13);
94 EXPECT_EQ(strcmp("will kill you", result.ptr), 0);
95 }
96
97 TEST(String, strstr) {
98 cxstring str = CX_STR("find the match in this string");
99 cxstring longstr = CX_STR(
100 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
101 "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
102 "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
103 "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
104 "abababababababababababababababababababababababababababababababab"
105 "abababababababababababababababababababababababababababababababab"
106 "abababababababababababababababababababababababababababababababab"
107 "abababababababababababababababababababababababababababababababab"
108 "abababababababababababababababababababababababababababababababab"
109 "abababababababababababababababababababababababababababababababab"
110 "wxyz1234567890");
111 cxstring longstrpattern = CX_STR(
112 "abababababababababababababababababababababababababababababababab"
113 "abababababababababababababababababababababababababababababababab"
114 "abababababababababababababababababababababababababababababababab"
115 "abababababababababababababababababababababababababababababababab"
116 "abababababababababababababababababababababababababababababababab"
117 );
118 cxstring longstrresult = CX_STR(
119 "abababababababababababababababababababababababababababababababab"
120 "abababababababababababababababababababababababababababababababab"
121 "abababababababababababababababababababababababababababababababab"
122 "abababababababababababababababababababababababababababababababab"
123 "abababababababababababababababababababababababababababababababab"
124 "abababababababababababababababababababababababababababababababab"
125 "wxyz1234567890"
126 );
127
128 cxstring notfound = cx_strstr(str, cx_str("no match"));
129 EXPECT_EQ(notfound.length, 0);
130
131 cxstring result = cx_strstr(str, cx_str("match"));
132 EXPECT_EQ(result.length, 20);
133 EXPECT_EQ(strcmp("match in this string", result.ptr), 0);
134
135 result = cx_strstr(str, cx_str(""));
136 EXPECT_EQ(result.length, str.length);
137 EXPECT_EQ(strcmp(str.ptr, result.ptr), 0);
138
139 result = cx_strstr(longstr, longstrpattern);
140 EXPECT_EQ(result.length, longstrresult.length);
141 EXPECT_EQ(strcmp(result.ptr, longstrresult.ptr), 0);
142 }
143
144 TEST(String, strcmp) {
145 cxstring str = CX_STR("compare this");
146
147 EXPECT_EQ(cx_strcmp(CX_STR(""), CX_STR("")), 0);
148 EXPECT_GT(cx_strcmp(str, CX_STR("")), 0);
149 EXPECT_EQ(cx_strcmp(str, CX_STR("compare this")), 0);
150 EXPECT_NE(cx_strcmp(str, CX_STR("Compare This")), 0);
151 EXPECT_LT(cx_strcmp(str, CX_STR("compare tool")), 0);
152 EXPECT_GT(cx_strcmp(str, CX_STR("compare shit")), 0);
153 EXPECT_LT(cx_strcmp(str, CX_STR("compare this not")), 0);
154 EXPECT_GT(cx_strcmp(str, CX_STR("compare")), 0);
155 }
156
157 TEST(String, strcasecmp) {
158 cxstring str = CX_STR("compare this");
159
160 EXPECT_EQ(cx_strcasecmp(CX_STR(""), CX_STR("")), 0);
161 EXPECT_GT(cx_strcasecmp(str, CX_STR("")), 0);
162 EXPECT_EQ(cx_strcasecmp(str, CX_STR("compare this")), 0);
163 EXPECT_EQ(cx_strcasecmp(str, CX_STR("Compare This")), 0);
164 EXPECT_LT(cx_strcasecmp(str, CX_STR("compare tool")), 0);
165 EXPECT_GT(cx_strcasecmp(str, CX_STR("compare shit")), 0);
166 EXPECT_LT(cx_strcasecmp(str, CX_STR("compare this not")), 0);
167 EXPECT_GT(cx_strcasecmp(str, CX_STR("compare")), 0);
168 }
169
170
171 TEST(String, strcat) {
172 cxstring s1 = CX_STR("12");
173 cxstring s2 = CX_STR("34");
174 cxstring s3 = CX_STR("56");
175 cxstring sn = {nullptr, 0};
176
177 CxTestingAllocator alloc;
178
179 cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2);
180 EXPECT_EQ(cx_strcmp(cx_strcast(t1), CX_STR("1234")), 0);
181 cx_strfree_a(&alloc, &t1);
182
183 cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3);
184 EXPECT_EQ(cx_strcmp(cx_strcast(t2), CX_STR("123456")), 0);
185 cx_strfree_a(&alloc, &t2);
186
187 cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn);
188 EXPECT_EQ(cx_strcmp(cx_strcast(t3), CX_STR("123456")), 0);
189 cx_strfree_a(&alloc, &t3);
190
191 cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn);
192 EXPECT_EQ(cx_strcmp(cx_strcast(t4), CX_STR("")), 0);
193 cx_strfree_a(&alloc, &t4);
194
195 EXPECT_TRUE(alloc.verify());
196 }
197
198 TEST(String, strsplit) {
199
200 cxstring test = cx_str("this,is,a,csv,string");
201 size_t capa = 8;
202 cxstring list[8];
203 size_t n;
204
205 /* special case: empty string */
206 n = cx_strsplit(test, cx_str(""), capa, list);
207 ASSERT_EQ(n, 1);
208 EXPECT_EQ(cx_strcmp(list[0], test), 0);
209
210 /* no delimiter occurrence */
211 n = cx_strsplit(test, cx_str("z"), capa, list);
212 ASSERT_EQ(n, 1);
213 EXPECT_EQ(cx_strcmp(list[0], test), 0);
214
215 /* partially matching delimiter */
216 n = cx_strsplit(test, cx_str("is,not"), capa, list);
217 ASSERT_EQ(n, 1);
218 EXPECT_EQ(cx_strcmp(list[0], test), 0);
219
220 /* matching single-char delimiter */
221 n = cx_strsplit(test, cx_str(","), capa, list);
222 ASSERT_EQ(n, 5);
223 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
224 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
225 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
226 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
227 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
228
229 /* matching multi-char delimiter */
230 n = cx_strsplit(test, cx_str("is"), capa, list);
231 ASSERT_EQ(n, 3);
232 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
233 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
234 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
235
236 /* bounded list using single-char delimiter */
237 n = cx_strsplit(test, cx_str(","), 3, list);
238 ASSERT_EQ(n, 3);
239 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
240 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
241 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
242
243 /* bounded list using multi-char delimiter */
244 n = cx_strsplit(test, cx_str("is"), 2, list);
245 ASSERT_EQ(n, 2);
246 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
247 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
248
249 /* start with delimiter */
250 n = cx_strsplit(test, cx_str("this"), capa, list);
251 ASSERT_EQ(n, 2);
252 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
253 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
254
255 /* end with delimiter */
256 n = cx_strsplit(test, cx_str("string"), capa, list);
257 ASSERT_EQ(n, 2);
258 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
259 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
260
261
262 /* end with delimiter exceed bound */
263 n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list);
264 ASSERT_EQ(n, 3);
265 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
266 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
267 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
268
269 /* exact match */
270 n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list);
271 ASSERT_EQ(n, 2);
272 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
273 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
274
275 /* string to be split is only substring */
276 n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list);
277 ASSERT_EQ(n, 1);
278 EXPECT_EQ(cx_strcmp(list[0], test), 0);
279
280 /* subsequent encounter of delimiter (the string between is empty) */
281 n = cx_strsplit(test, cx_str("is,"), capa, list);
282 ASSERT_EQ(n, 3);
283 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
284 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
285 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
286 }
287
288 TEST(String, strsplit_a) {
289 CxTestingAllocator alloc;
290
291 cxstring test = cx_str("this,is,a,csv,string");
292 size_t capa = 8;
293 cxstring *list;
294 size_t n;
295
296 /* special case: empty string */
297 n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list);
298 ASSERT_EQ(n, 1);
299 EXPECT_EQ(cx_strcmp(list[0], test), 0);
300 cxFree(&alloc, list);
301
302 /* no delimiter occurrence */
303 n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list);
304 ASSERT_EQ(n, 1);
305 EXPECT_EQ(cx_strcmp(list[0], test), 0);
306 cxFree(&alloc, list);
307
308 /* partially matching delimiter */
309 n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list);
310 ASSERT_EQ(n, 1);
311 EXPECT_EQ(cx_strcmp(list[0], test), 0);
312 cxFree(&alloc, list);
313
314 /* matching single-char delimiter */
315 n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list);
316 ASSERT_EQ(n, 5);
317 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
318 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
319 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
320 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
321 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
322 cxFree(&alloc, list);
323
324 /* matching multi-char delimiter */
325 n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list);
326 ASSERT_EQ(n, 3);
327 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
328 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
329 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
330 cxFree(&alloc, list);
331
332 /* bounded list using single-char delimiter */
333 n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list);
334 ASSERT_EQ(n, 3);
335 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
336 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
337 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
338 cxFree(&alloc, list);
339
340 /* bounded list using multi-char delimiter */
341 n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list);
342 ASSERT_EQ(n, 2);
343 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
344 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
345 cxFree(&alloc, list);
346
347 /* start with delimiter */
348 n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list);
349 ASSERT_EQ(n, 2);
350 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
351 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
352 cxFree(&alloc, list);
353
354 /* end with delimiter */
355 n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list);
356 ASSERT_EQ(n, 2);
357 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
358 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
359 cxFree(&alloc, list);
360
361 /* end with delimiter exceed bound */
362 n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list);
363 ASSERT_EQ(n, 3);
364 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
365 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
366 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
367 cxFree(&alloc, list);
368
369 /* exact match */
370 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list);
371 ASSERT_EQ(n, 2);
372 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
373 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
374 cxFree(&alloc, list);
375
376 /* string to be split is only substring */
377 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list);
378 ASSERT_EQ(n, 1);
379 EXPECT_EQ(cx_strcmp(list[0], test), 0);
380 cxFree(&alloc, list);
381
382 /* subsequent encounter of delimiter (the string between is empty) */
383 n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list);
384 ASSERT_EQ(n, 3);
385 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
386 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
387 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
388 cxFree(&alloc, list);
389
390 EXPECT_TRUE(alloc.verify());
391 }
392
393 TEST(String, strtrim) {
394 cxstring t1 = cx_strtrim(cx_str(" ein test \t "));
395 cxstring t2 = cx_strtrim(cx_str("abc"));
396 cxstring t3 = cx_strtrim(cx_str(" 123"));
397 cxstring t4 = cx_strtrim(cx_str("xyz "));
398 cxstring t5 = cx_strtrim(cx_str(" "));
399 cxstring empty = cx_strtrim(cx_str(""));
400
401 EXPECT_EQ(cx_strcmp(t1, cx_str("ein test")), 0);
402 EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0);
403 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0);
404 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0);
405 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0);
406 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0);
407 }
408
409 TEST(String, strprefix) {
410 cxstring str = CX_STR("test my prefix and my suffix");
411 cxstring empty = CX_STR("");
412 EXPECT_FALSE(cx_strprefix(empty, cx_str("pref")));
413 EXPECT_TRUE(cx_strprefix(str, empty));
414 EXPECT_TRUE(cx_strprefix(empty, empty));
415 EXPECT_TRUE(cx_strprefix(str, cx_str("test ")));
416 EXPECT_FALSE(cx_strprefix(str, cx_str("8-) fsck ")));
417 }
418
419 TEST(String, strsuffix) {
420 cxstring str = CX_STR("test my prefix and my suffix");
421 cxstring empty = CX_STR("");
422 EXPECT_FALSE(cx_strsuffix(empty, cx_str("suf")));
423 EXPECT_TRUE(cx_strsuffix(str, empty));
424 EXPECT_TRUE(cx_strsuffix(empty, empty));
425 EXPECT_TRUE(cx_strsuffix(str, cx_str("fix")));
426 EXPECT_FALSE(cx_strsuffix(str, cx_str("fox")));
427 }
428
429 TEST(String, strcaseprefix) {
430 cxstring str = CX_STR("test my prefix and my suffix");
431 cxstring empty = CX_STR("");
432 EXPECT_FALSE(cx_strcaseprefix(empty, cx_str("pREf")));
433 EXPECT_TRUE(cx_strcaseprefix(str, empty));
434 EXPECT_TRUE(cx_strcaseprefix(empty, empty));
435 EXPECT_TRUE(cx_strcaseprefix(str, cx_str("TEST ")));
436 EXPECT_FALSE(cx_strcaseprefix(str, cx_str("8-) fsck ")));
437 }
438
439 TEST(String, strcasesuffix) {
440 cxstring str = CX_STR("test my prefix and my suffix");
441 cxstring empty = CX_STR("");
442 EXPECT_FALSE(cx_strcasesuffix(empty, cx_str("sUf")));
443 EXPECT_TRUE(cx_strcasesuffix(str, empty));
444 EXPECT_TRUE(cx_strcasesuffix(empty, empty));
445 EXPECT_TRUE(cx_strcasesuffix(str, cx_str("FIX")));
446 EXPECT_FALSE(cx_strcasesuffix(str, cx_str("fox")));
447 }
448
449 TEST(String, strreplace) {
450 cxstring str = CX_STR("test ababab string aba");
451 cxstring longstr = CX_STR(
452 "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
453 cxstring notrail = CX_STR("test abab");
454 cxstring empty = CX_STR("");
455 cxstring astr = CX_STR("aaaaaaaaaa");
456 cxstring csstr = CX_STR("test AB ab TEST xyz");
457
458 cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger"));
459 cxstring expected = CX_STR("test muchlongerab string aba");
460
461 cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2);
462 cxstring expectedn = CX_STR("test ccab string aba");
463
464 cxmutstr longrepl = cx_strreplace(longstr, cx_str("a"), cx_str("z"));
465 cxstring longexpect = CX_STR(
466 "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd");
467
468 cxmutstr replnotrail = cx_strreplace(notrail, cx_str("ab"), cx_str("z"));
469 cxstring notrailexpect = CX_STR("test zz");
470
471 cxmutstr repleq = cx_strreplace(str, str, cx_str("hello"));
472 cxstring eqexpect = CX_STR("hello");
473
474 cxmutstr replempty1 = cx_strreplace(empty, cx_str("ab"), cx_str("c")); // expect: empty
475 cxmutstr replempty2 = cx_strreplace(str, cx_str("abab"), empty);
476 cxstring emptyexpect2 = CX_STR("test ab string aba");
477
478 cxmutstr replpre = cx_strreplace(str, cx_str("test "), cx_str("TEST "));
479 cxstring preexpected = CX_STR("TEST ababab string aba");
480
481 cxmutstr replan1 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 1);
482 cxstring an1expected = CX_STR("xaaaaaaaaa");
483
484 cxmutstr replan4 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 4);
485 cxstring an4expected = CX_STR("xxxxaaaaaa");
486
487 cxmutstr replan9 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 9);
488 cxstring an9expected = CX_STR("xxxxxxxxxa");
489
490 cxmutstr replan10 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 10);
491 cxstring an10expected = CX_STR("xxxxxxxxxx");
492
493 cxmutstr replcs1 = cx_strreplace(csstr, cx_str("AB"), cx_str("*"));
494 cxstring cs1expected = CX_STR("test * ab TEST xyz");
495
496 cxmutstr replcs2 = cx_strreplace(csstr, cx_str("test"), cx_str("TEST"));
497 cxstring cs2expected = CX_STR("TEST AB ab TEST xyz");
498
499
500 EXPECT_NE(repl.ptr, str.ptr);
501 EXPECT_EQ(cx_strcmp(cx_strcast(repl), expected), 0);
502 EXPECT_NE(repln.ptr, str.ptr);
503 EXPECT_EQ(cx_strcmp(cx_strcast(repln), expectedn), 0);
504 EXPECT_EQ(cx_strcmp(cx_strcast(longrepl), longexpect), 0);
505 EXPECT_EQ(cx_strcmp(cx_strcast(replnotrail), notrailexpect), 0);
506 EXPECT_EQ(cx_strcmp(cx_strcast(repleq), eqexpect), 0);
507 EXPECT_EQ(cx_strcmp(cx_strcast(replempty1), empty), 0);
508 EXPECT_EQ(cx_strcmp(cx_strcast(replempty2), emptyexpect2), 0);
509 EXPECT_EQ(cx_strcmp(cx_strcast(replpre), preexpected), 0);
510 EXPECT_EQ(cx_strcmp(cx_strcast(replan1), an1expected), 0);
511 EXPECT_EQ(cx_strcmp(cx_strcast(replan4), an4expected), 0);
512 EXPECT_EQ(cx_strcmp(cx_strcast(replan9), an9expected), 0);
513 EXPECT_EQ(cx_strcmp(cx_strcast(replan10), an10expected), 0);
514 EXPECT_EQ(cx_strcmp(cx_strcast(replcs1), cs1expected), 0);
515 EXPECT_EQ(cx_strcmp(cx_strcast(replcs2), cs2expected), 0);
516
517 cx_strfree(&repl);
518 cx_strfree(&repln);
519 cx_strfree(&longrepl);
520 cx_strfree(&replnotrail);
521 cx_strfree(&repleq);
522 cx_strfree(&replempty1);
523 cx_strfree(&replempty2);
524 cx_strfree(&replpre);
525 cx_strfree(&replan1);
526 cx_strfree(&replan4);
527 cx_strfree(&replan9);
528 cx_strfree(&replan10);
529 cx_strfree(&replcs1);
530 cx_strfree(&replcs2);
531 }

mercurial