69 EXPECT_EQ(len1, 4); |
69 EXPECT_EQ(len1, 4); |
70 EXPECT_EQ(len2, 9); |
70 EXPECT_EQ(len2, 9); |
71 EXPECT_EQ(len3, 10); |
71 EXPECT_EQ(len3, 10); |
72 } |
72 } |
73 |
73 |
|
74 TEST(String, strsubs) { |
|
75 cxstring str = CX_STR("A test string"); |
|
76 |
|
77 cxstring sub = cx_strsubs(str, 0); |
|
78 EXPECT_EQ(cx_strcmp(sub, str), 0); |
|
79 |
|
80 sub = cx_strsubs(str, 2); |
|
81 EXPECT_EQ(cx_strcmp(sub, cx_str("test string")), 0); |
|
82 |
|
83 sub = cx_strsubs(str, 7); |
|
84 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); |
|
85 |
|
86 sub = cx_strsubs(str, 15); |
|
87 EXPECT_EQ(cx_strcmp(sub, cx_str("")), 0); |
|
88 |
|
89 sub = cx_strsubsl(str, 2, 4); |
|
90 EXPECT_EQ(cx_strcmp(sub, cx_str("test")), 0); |
|
91 |
|
92 sub = cx_strsubsl(str, 7, 3); |
|
93 EXPECT_EQ(cx_strcmp(sub, cx_str("str")), 0); |
|
94 |
|
95 sub = cx_strsubsl(str, 7, 20); |
|
96 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); |
|
97 |
|
98 // just for coverage, call the _m variant |
|
99 auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0); |
|
100 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); |
|
101 } |
74 |
102 |
75 TEST(String, strchr) { |
103 TEST(String, strchr) { |
76 cxstring str = CX_STR("I will find you - and I will kill you"); |
104 cxstring str = CX_STR("I will find you - and I will kill you"); |
77 |
105 |
78 cxstring notfound = cx_strchr(str, 'x'); |
106 cxstring notfound = cx_strchr(str, 'x'); |
79 EXPECT_EQ(notfound.length, 0); |
107 EXPECT_EQ(notfound.length, 0); |
80 |
108 |
81 cxstring result = cx_strchr(str, 'w'); |
109 cxstring result = cx_strchr(str, 'w'); |
82 EXPECT_EQ(result.length, 35); |
110 EXPECT_EQ(result.length, 35); |
83 EXPECT_EQ(strcmp("will find you - and I will kill you", result.ptr), 0); |
111 EXPECT_EQ(strcmp("will find you - and I will kill you", result.ptr), 0); |
|
112 |
|
113 // just for coverage, call the _m variant |
|
114 auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a'); |
|
115 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); |
84 } |
116 } |
85 |
117 |
86 TEST(String, strrchr) { |
118 TEST(String, strrchr) { |
87 cxstring str = CX_STR("I will find you - and I will kill you"); |
119 cxstring str = CX_STR("I will find you - and I will kill you"); |
88 |
120 |
90 EXPECT_EQ(notfound.length, 0); |
122 EXPECT_EQ(notfound.length, 0); |
91 |
123 |
92 cxstring result = cx_strrchr(str, 'w'); |
124 cxstring result = cx_strrchr(str, 'w'); |
93 EXPECT_EQ(result.length, 13); |
125 EXPECT_EQ(result.length, 13); |
94 EXPECT_EQ(strcmp("will kill you", result.ptr), 0); |
126 EXPECT_EQ(strcmp("will kill you", result.ptr), 0); |
|
127 |
|
128 // just for coverage, call the _m variant |
|
129 auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a'); |
|
130 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); |
95 } |
131 } |
96 |
132 |
97 TEST(String, strstr) { |
133 TEST(String, strstr) { |
98 cxstring str = CX_STR("find the match in this string"); |
134 cxstring str = CX_STR("find the match in this string"); |
99 cxstring longstr = CX_STR( |
135 cxstring longstr = CX_STR( |
137 EXPECT_EQ(strcmp(str.ptr, result.ptr), 0); |
173 EXPECT_EQ(strcmp(str.ptr, result.ptr), 0); |
138 |
174 |
139 result = cx_strstr(longstr, longstrpattern); |
175 result = cx_strstr(longstr, longstrpattern); |
140 EXPECT_EQ(result.length, longstrresult.length); |
176 EXPECT_EQ(result.length, longstrresult.length); |
141 EXPECT_EQ(strcmp(result.ptr, longstrresult.ptr), 0); |
177 EXPECT_EQ(strcmp(result.ptr, longstrresult.ptr), 0); |
|
178 |
|
179 // just for coverage, call the _m variant |
|
180 auto mstr = cx_strdup(longstr); |
|
181 auto m = cx_strstr_m(mstr, longstrpattern); |
|
182 EXPECT_EQ(m.length, longstrresult.length); |
|
183 EXPECT_EQ(strcmp(m.ptr, longstrresult.ptr), 0); |
|
184 cx_strfree(&mstr); |
142 } |
185 } |
143 |
186 |
144 TEST(String, strcmp) { |
187 TEST(String, strcmp) { |
145 cxstring str = CX_STR("compare this"); |
188 cxstring str = CX_STR("compare this"); |
146 |
189 |
281 n = cx_strsplit(test, cx_str("is,"), capa, list); |
324 n = cx_strsplit(test, cx_str("is,"), capa, list); |
282 ASSERT_EQ(n, 3); |
325 ASSERT_EQ(n, 3); |
283 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); |
326 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); |
284 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); |
327 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); |
285 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); |
328 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); |
|
329 |
|
330 /* call the _m variant just for coverage */ |
|
331 auto mtest = cx_strdup(test); |
|
332 cxmutstr mlist[4]; |
|
333 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); |
|
334 ASSERT_EQ(n, 3); |
|
335 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); |
|
336 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); |
|
337 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); |
|
338 cx_strfree(&mtest); |
286 } |
339 } |
287 |
340 |
288 TEST(String, strsplit_a) { |
341 TEST(String, strsplit_a) { |
289 CxTestingAllocator alloc; |
342 CxTestingAllocator alloc; |
290 |
343 |
385 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); |
438 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); |
386 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); |
439 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); |
387 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); |
440 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); |
388 cxFree(&alloc, list); |
441 cxFree(&alloc, list); |
389 |
442 |
|
443 /* call the _m variant just for coverage */ |
|
444 auto mtest = cx_strdup(test); |
|
445 cxmutstr *mlist; |
|
446 n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist); |
|
447 ASSERT_EQ(n, 3); |
|
448 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); |
|
449 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); |
|
450 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); |
|
451 cxFree(&alloc, mlist); |
|
452 cx_strfree(&mtest); |
|
453 |
390 EXPECT_TRUE(alloc.verify()); |
454 EXPECT_TRUE(alloc.verify()); |
391 } |
455 } |
392 |
456 |
393 TEST(String, strtrim) { |
457 TEST(String, strtrim) { |
394 cxstring t1 = cx_strtrim(cx_str(" ein test \t ")); |
458 cxstring t1 = cx_strtrim(cx_str(" ein test \t ")); |
402 EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0); |
466 EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0); |
403 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0); |
467 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0); |
404 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0); |
468 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0); |
405 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); |
469 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); |
406 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); |
470 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); |
|
471 |
|
472 /* call the _m variant just for coverage */ |
|
473 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); |
|
474 EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0); |
407 } |
475 } |
408 |
476 |
409 TEST(String, strprefix) { |
477 TEST(String, strprefix) { |
410 cxstring str = CX_STR("test my prefix and my suffix"); |
478 cxstring str = CX_STR("test my prefix and my suffix"); |
411 cxstring empty = CX_STR(""); |
479 cxstring empty = CX_STR(""); |