tests/test_string.cpp

changeset 684
380bd45bc94a
parent 671
d7a67375a7ac
child 697
ebdce4bf262b
equal deleted inserted replaced
683:aa0d09f2d81c 684:380bd45bc94a
32 #include <gtest/gtest.h> 32 #include <gtest/gtest.h>
33 33
34 #define EXPECT_ZERO_TERMINATED(str) EXPECT_EQ((str).ptr[(str).length], '\0') 34 #define EXPECT_ZERO_TERMINATED(str) EXPECT_EQ((str).ptr[(str).length], '\0')
35 35
36 TEST(String, construct) { 36 TEST(String, construct) {
37 cxstring s1 = cx_str("1234"); 37 cxstring s1 = CX_STR("1234");
38 cxstring s2 = cx_strn("abcd", 2); 38 cxstring s2 = cx_strn("abcd", 2);
39 cxmutstr s3 = cx_mutstr((char *) "1234"); 39 cxmutstr s3 = cx_mutstr((char *) "1234");
40 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2); 40 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
41 41
42 EXPECT_EQ(s1.length, 4); 42 EXPECT_EQ(s1.length, 4);
94 94
95 cxstring sub = cx_strsubs(str, 0); 95 cxstring sub = cx_strsubs(str, 0);
96 EXPECT_EQ(cx_strcmp(sub, str), 0); 96 EXPECT_EQ(cx_strcmp(sub, str), 0);
97 97
98 sub = cx_strsubs(str, 2); 98 sub = cx_strsubs(str, 2);
99 EXPECT_EQ(cx_strcmp(sub, cx_str("test string")), 0); 99 EXPECT_EQ(cx_strcmp(sub, CX_STR("test string")), 0);
100 100
101 sub = cx_strsubs(str, 7); 101 sub = cx_strsubs(str, 7);
102 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); 102 EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0);
103 103
104 sub = cx_strsubs(str, 15); 104 sub = cx_strsubs(str, 15);
105 EXPECT_EQ(cx_strcmp(sub, cx_str("")), 0); 105 EXPECT_EQ(cx_strcmp(sub, CX_STR("")), 0);
106 106
107 sub = cx_strsubsl(str, 2, 4); 107 sub = cx_strsubsl(str, 2, 4);
108 EXPECT_EQ(cx_strcmp(sub, cx_str("test")), 0); 108 EXPECT_EQ(cx_strcmp(sub, CX_STR("test")), 0);
109 109
110 sub = cx_strsubsl(str, 7, 3); 110 sub = cx_strsubsl(str, 7, 3);
111 EXPECT_EQ(cx_strcmp(sub, cx_str("str")), 0); 111 EXPECT_EQ(cx_strcmp(sub, CX_STR("str")), 0);
112 112
113 sub = cx_strsubsl(str, 7, 20); 113 sub = cx_strsubsl(str, 7, 20);
114 EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); 114 EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0);
115 115
116 // just for coverage, call the _m variant 116 // just for coverage, call the _m variant
117 auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0); 117 auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0);
118 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); 118 EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0);
119 } 119 }
120 120
121 TEST(String, strchr) { 121 TEST(String, strchr) {
122 cxstring str = CX_STR("I will find you - and I will kill you"); 122 cxstring str = CX_STR("I will find you - and I will kill you");
123 123
128 EXPECT_EQ(result.length, 35); 128 EXPECT_EQ(result.length, 35);
129 EXPECT_STREQ(result.ptr, "will find you - and I will kill you"); 129 EXPECT_STREQ(result.ptr, "will find you - and I will kill you");
130 130
131 // just for coverage, call the _m variant 131 // just for coverage, call the _m variant
132 auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a'); 132 auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a');
133 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); 133 EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0);
134 } 134 }
135 135
136 TEST(String, strrchr) { 136 TEST(String, strrchr) {
137 cxstring str = CX_STR("I will find you - and I will kill you"); 137 cxstring str = CX_STR("I will find you - and I will kill you");
138 138
143 EXPECT_EQ(result.length, 13); 143 EXPECT_EQ(result.length, 13);
144 EXPECT_STREQ(result.ptr, "will kill you"); 144 EXPECT_STREQ(result.ptr, "will kill you");
145 145
146 // just for coverage, call the _m variant 146 // just for coverage, call the _m variant
147 auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a'); 147 auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a');
148 EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); 148 EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0);
149 } 149 }
150 150
151 TEST(String, strstr) { 151 TEST(String, strstr) {
152 cxstring str = CX_STR("find the match in this string"); 152 cxstring str = CX_STR("find the match in this string");
153 cxstring longstr = CX_STR( 153 cxstring longstr = CX_STR(
177 "abababababababababababababababababababababababababababababababab" 177 "abababababababababababababababababababababababababababababababab"
178 "abababababababababababababababababababababababababababababababab" 178 "abababababababababababababababababababababababababababababababab"
179 "wxyz1234567890" 179 "wxyz1234567890"
180 ); 180 );
181 181
182 cxstring notfound = cx_strstr(str, cx_str("no match")); 182 cxstring notfound = cx_strstr(str, CX_STR("no match"));
183 EXPECT_EQ(notfound.length, 0); 183 EXPECT_EQ(notfound.length, 0);
184 184
185 cxstring result = cx_strstr(str, cx_str("match")); 185 cxstring result = cx_strstr(str, CX_STR("match"));
186 EXPECT_EQ(result.length, 20); 186 EXPECT_EQ(result.length, 20);
187 EXPECT_STREQ(result.ptr, "match in this string"); 187 EXPECT_STREQ(result.ptr, "match in this string");
188 188
189 result = cx_strstr(str, cx_str("")); 189 result = cx_strstr(str, CX_STR(""));
190 EXPECT_EQ(result.length, str.length); 190 EXPECT_EQ(result.length, str.length);
191 EXPECT_STREQ(result.ptr, str.ptr); 191 EXPECT_STREQ(result.ptr, str.ptr);
192 192
193 result = cx_strstr(longstr, longstrpattern); 193 result = cx_strstr(longstr, longstrpattern);
194 EXPECT_EQ(result.length, longstrresult.length); 194 EXPECT_EQ(result.length, longstrresult.length);
203 } 203 }
204 204
205 TEST(String, strcmp) { 205 TEST(String, strcmp) {
206 cxstring str = CX_STR("compare this"); 206 cxstring str = CX_STR("compare this");
207 207
208 EXPECT_EQ(cx_strcmp(cx_str(""), cx_str("")), 0); 208 EXPECT_EQ(cx_strcmp(CX_STR(""), CX_STR("")), 0);
209 EXPECT_GT(cx_strcmp(str, cx_str("")), 0); 209 EXPECT_GT(cx_strcmp(str, CX_STR("")), 0);
210 EXPECT_EQ(cx_strcmp(str, cx_str("compare this")), 0); 210 EXPECT_EQ(cx_strcmp(str, CX_STR("compare this")), 0);
211 EXPECT_NE(cx_strcmp(str, cx_str("Compare This")), 0); 211 EXPECT_NE(cx_strcmp(str, CX_STR("Compare This")), 0);
212 EXPECT_LT(cx_strcmp(str, cx_str("compare tool")), 0); 212 EXPECT_LT(cx_strcmp(str, CX_STR("compare tool")), 0);
213 EXPECT_GT(cx_strcmp(str, cx_str("compare shit")), 0); 213 EXPECT_GT(cx_strcmp(str, CX_STR("compare shit")), 0);
214 EXPECT_LT(cx_strcmp(str, cx_str("compare this not")), 0); 214 EXPECT_LT(cx_strcmp(str, CX_STR("compare this not")), 0);
215 EXPECT_GT(cx_strcmp(str, cx_str("compare")), 0); 215 EXPECT_GT(cx_strcmp(str, CX_STR("compare")), 0);
216 216
217 cxstring str2 = cx_str("Compare This"); 217 cxstring str2 = CX_STR("Compare This");
218 EXPECT_NE(cx_strcmp_p(&str, &str2), 0); 218 EXPECT_NE(cx_strcmp_p(&str, &str2), 0);
219 str2 = cx_str("compare this"); 219 str2 = CX_STR("compare this");
220 EXPECT_EQ(cx_strcmp_p(&str, &str2), 0); 220 EXPECT_EQ(cx_strcmp_p(&str, &str2), 0);
221 } 221 }
222 222
223 TEST(String, strcasecmp) { 223 TEST(String, strcasecmp) {
224 cxstring str = CX_STR("compare this"); 224 cxstring str = CX_STR("compare this");
225 225
226 EXPECT_EQ(cx_strcasecmp(cx_str(""), cx_str("")), 0); 226 EXPECT_EQ(cx_strcasecmp(CX_STR(""), CX_STR("")), 0);
227 EXPECT_GT(cx_strcasecmp(str, cx_str("")), 0); 227 EXPECT_GT(cx_strcasecmp(str, CX_STR("")), 0);
228 EXPECT_EQ(cx_strcasecmp(str, cx_str("compare this")), 0); 228 EXPECT_EQ(cx_strcasecmp(str, CX_STR("compare this")), 0);
229 EXPECT_EQ(cx_strcasecmp(str, cx_str("Compare This")), 0); 229 EXPECT_EQ(cx_strcasecmp(str, CX_STR("Compare This")), 0);
230 EXPECT_LT(cx_strcasecmp(str, cx_str("compare tool")), 0); 230 EXPECT_LT(cx_strcasecmp(str, CX_STR("compare tool")), 0);
231 EXPECT_GT(cx_strcasecmp(str, cx_str("compare shit")), 0); 231 EXPECT_GT(cx_strcasecmp(str, CX_STR("compare shit")), 0);
232 EXPECT_LT(cx_strcasecmp(str, cx_str("compare this not")), 0); 232 EXPECT_LT(cx_strcasecmp(str, CX_STR("compare this not")), 0);
233 EXPECT_GT(cx_strcasecmp(str, cx_str("compare")), 0); 233 EXPECT_GT(cx_strcasecmp(str, CX_STR("compare")), 0);
234 234
235 cxstring str2 = cx_str("Compare This"); 235 cxstring str2 = CX_STR("Compare This");
236 EXPECT_EQ(cx_strcasecmp_p(&str, &str2), 0); 236 EXPECT_EQ(cx_strcasecmp_p(&str, &str2), 0);
237 str2 = cx_str("Compare Tool"); 237 str2 = CX_STR("Compare Tool");
238 EXPECT_LT(cx_strcasecmp_p(&str, &str2), 0); 238 EXPECT_LT(cx_strcasecmp_p(&str, &str2), 0);
239 } 239 }
240 240
241 TEST(String, strcat) { 241 TEST(String, strcat) {
242 cxstring s1 = CX_STR("12"); 242 cxstring s1 = CX_STR("12");
245 cxstring sn = {nullptr, 0}; 245 cxstring sn = {nullptr, 0};
246 246
247 CxTestingAllocator alloc; 247 CxTestingAllocator alloc;
248 248
249 cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2); 249 cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2);
250 EXPECT_EQ(cx_strcmp(cx_strcast(t1), cx_str("1234")), 0); 250 EXPECT_EQ(cx_strcmp(cx_strcast(t1), CX_STR("1234")), 0);
251 EXPECT_ZERO_TERMINATED(t1); 251 EXPECT_ZERO_TERMINATED(t1);
252 cx_strfree_a(&alloc, &t1); 252 cx_strfree_a(&alloc, &t1);
253 253
254 cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3); 254 cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3);
255 EXPECT_EQ(cx_strcmp(cx_strcast(t2), cx_str("123456")), 0); 255 EXPECT_EQ(cx_strcmp(cx_strcast(t2), CX_STR("123456")), 0);
256 EXPECT_ZERO_TERMINATED(t2); 256 EXPECT_ZERO_TERMINATED(t2);
257 cx_strfree_a(&alloc, &t2); 257 cx_strfree_a(&alloc, &t2);
258 258
259 cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn); 259 cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn);
260 EXPECT_EQ(cx_strcmp(cx_strcast(t3), cx_str("123456")), 0); 260 EXPECT_EQ(cx_strcmp(cx_strcast(t3), CX_STR("123456")), 0);
261 EXPECT_ZERO_TERMINATED(t3); 261 EXPECT_ZERO_TERMINATED(t3);
262 cx_strfree_a(&alloc, &t3); 262 cx_strfree_a(&alloc, &t3);
263 263
264 cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn); 264 cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn);
265 EXPECT_EQ(cx_strcmp(cx_strcast(t4), cx_str("")), 0); 265 EXPECT_EQ(cx_strcmp(cx_strcast(t4), CX_STR("")), 0);
266 EXPECT_ZERO_TERMINATED(t4); 266 EXPECT_ZERO_TERMINATED(t4);
267 cx_strfree_a(&alloc, &t4); 267 cx_strfree_a(&alloc, &t4);
268 268
269 EXPECT_TRUE(alloc.verify()); 269 EXPECT_TRUE(alloc.verify());
270 270
271 // use the macro 271 // use the macro
272 cxmutstr t5 = cx_strcat(3, s3, s1, s2); 272 cxmutstr t5 = cx_strcat(3, s3, s1, s2);
273 EXPECT_EQ(cx_strcmp(cx_strcast(t5), cx_str("561234")), 0); 273 EXPECT_EQ(cx_strcmp(cx_strcast(t5), CX_STR("561234")), 0);
274 EXPECT_ZERO_TERMINATED(t5); 274 EXPECT_ZERO_TERMINATED(t5);
275 cx_strfree(&t5); 275 cx_strfree(&t5);
276 } 276 }
277 277
278 TEST(String, strsplit) { 278 TEST(String, strsplit) {
279 279
280 cxstring test = cx_str("this,is,a,csv,string"); 280 cxstring test = CX_STR("this,is,a,csv,string");
281 size_t capa = 8; 281 size_t capa = 8;
282 cxstring list[8]; 282 cxstring list[8];
283 size_t n; 283 size_t n;
284 284
285 // special case: empty string 285 // special case: empty string
286 n = cx_strsplit(test, cx_str(""), capa, list); 286 n = cx_strsplit(test, CX_STR(""), capa, list);
287 ASSERT_EQ(n, 1); 287 ASSERT_EQ(n, 1);
288 EXPECT_EQ(cx_strcmp(list[0], test), 0); 288 EXPECT_EQ(cx_strcmp(list[0], test), 0);
289 289
290 // no delimiter occurrence 290 // no delimiter occurrence
291 n = cx_strsplit(test, cx_str("z"), capa, list); 291 n = cx_strsplit(test, CX_STR("z"), capa, list);
292 ASSERT_EQ(n, 1); 292 ASSERT_EQ(n, 1);
293 EXPECT_EQ(cx_strcmp(list[0], test), 0); 293 EXPECT_EQ(cx_strcmp(list[0], test), 0);
294 294
295 // partially matching delimiter 295 // partially matching delimiter
296 n = cx_strsplit(test, cx_str("is,not"), capa, list); 296 n = cx_strsplit(test, CX_STR("is,not"), capa, list);
297 ASSERT_EQ(n, 1); 297 ASSERT_EQ(n, 1);
298 EXPECT_EQ(cx_strcmp(list[0], test), 0); 298 EXPECT_EQ(cx_strcmp(list[0], test), 0);
299 299
300 // matching single-char delimiter 300 // matching single-char delimiter
301 n = cx_strsplit(test, cx_str(","), capa, list); 301 n = cx_strsplit(test, CX_STR(","), capa, list);
302 ASSERT_EQ(n, 5); 302 ASSERT_EQ(n, 5);
303 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 303 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0);
304 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 304 EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0);
305 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); 305 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0);
306 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); 306 EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0);
307 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); 307 EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0);
308 308
309 // matching multi-char delimiter 309 // matching multi-char delimiter
310 n = cx_strsplit(test, cx_str("is"), capa, list); 310 n = cx_strsplit(test, CX_STR("is"), capa, list);
311 ASSERT_EQ(n, 3); 311 ASSERT_EQ(n, 3);
312 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 312 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
313 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); 313 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0);
314 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); 314 EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0);
315 315
316 // bounded list using single-char delimiter 316 // bounded list using single-char delimiter
317 n = cx_strsplit(test, cx_str(","), 3, list); 317 n = cx_strsplit(test, CX_STR(","), 3, list);
318 ASSERT_EQ(n, 3); 318 ASSERT_EQ(n, 3);
319 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 319 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0);
320 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 320 EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0);
321 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 321 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0);
322 322
323 // bounded list using multi-char delimiter 323 // bounded list using multi-char delimiter
324 n = cx_strsplit(test, cx_str("is"), 2, list); 324 n = cx_strsplit(test, CX_STR("is"), 2, list);
325 ASSERT_EQ(n, 2); 325 ASSERT_EQ(n, 2);
326 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 326 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
327 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 327 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0);
328 328
329 // start with delimiter 329 // start with delimiter
330 n = cx_strsplit(test, cx_str("this"), capa, list); 330 n = cx_strsplit(test, CX_STR("this"), capa, list);
331 ASSERT_EQ(n, 2); 331 ASSERT_EQ(n, 2);
332 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 332 EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0);
333 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 333 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0);
334 334
335 // end with delimiter 335 // end with delimiter
336 n = cx_strsplit(test, cx_str("string"), capa, list); 336 n = cx_strsplit(test, CX_STR("string"), capa, list);
337 ASSERT_EQ(n, 2); 337 ASSERT_EQ(n, 2);
338 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); 338 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0);
339 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 339 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
340 340
341 341
342 // end with delimiter exceed bound 342 // end with delimiter exceed bound
343 n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list); 343 n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list);
344 ASSERT_EQ(n, 3); 344 ASSERT_EQ(n, 3);
345 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); 345 EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0);
346 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); 346 EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0);
347 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); 347 EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0);
348 348
349 // exact match 349 // exact match
350 n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list); 350 n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list);
351 ASSERT_EQ(n, 2); 351 ASSERT_EQ(n, 2);
352 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 352 EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0);
353 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 353 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
354 354
355 // string to be split is only substring 355 // string to be split is only substring
356 n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list); 356 n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list);
357 ASSERT_EQ(n, 1); 357 ASSERT_EQ(n, 1);
358 EXPECT_EQ(cx_strcmp(list[0], test), 0); 358 EXPECT_EQ(cx_strcmp(list[0], test), 0);
359 359
360 // subsequent encounter of delimiter (the string between is empty) 360 // subsequent encounter of delimiter (the string between is empty)
361 n = cx_strsplit(test, cx_str("is,"), capa, list); 361 n = cx_strsplit(test, CX_STR("is,"), capa, list);
362 ASSERT_EQ(n, 3); 362 ASSERT_EQ(n, 3);
363 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 363 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
364 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 364 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
365 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 365 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0);
366 366
367 // call the _m variant just for coverage 367 // call the _m variant just for coverage
368 auto mtest = cx_strdup(test); 368 auto mtest = cx_strdup(test);
369 cxmutstr mlist[4]; 369 cxmutstr mlist[4];
370 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); 370 n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist);
371 ASSERT_EQ(n, 3); 371 ASSERT_EQ(n, 3);
372 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); 372 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0);
373 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); 373 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0);
374 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); 374 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0);
375 cx_strfree(&mtest); 375 cx_strfree(&mtest);
376 } 376 }
377 377
378 TEST(String, strsplit_a) { 378 TEST(String, strsplit_a) {
379 CxTestingAllocator alloc; 379 CxTestingAllocator alloc;
380 380
381 cxstring test = cx_str("this,is,a,csv,string"); 381 cxstring test = CX_STR("this,is,a,csv,string");
382 size_t capa = 8; 382 size_t capa = 8;
383 cxstring *list; 383 cxstring *list;
384 size_t n; 384 size_t n;
385 385
386 // special case: empty string 386 // special case: empty string
387 n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list); 387 n = cx_strsplit_a(&alloc, test, CX_STR(""), capa, &list);
388 ASSERT_EQ(n, 1); 388 ASSERT_EQ(n, 1);
389 EXPECT_EQ(cx_strcmp(list[0], test), 0); 389 EXPECT_EQ(cx_strcmp(list[0], test), 0);
390 cxFree(&alloc, list); 390 cxFree(&alloc, list);
391 391
392 // no delimiter occurrence 392 // no delimiter occurrence
393 n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list); 393 n = cx_strsplit_a(&alloc, test, CX_STR("z"), capa, &list);
394 ASSERT_EQ(n, 1); 394 ASSERT_EQ(n, 1);
395 EXPECT_EQ(cx_strcmp(list[0], test), 0); 395 EXPECT_EQ(cx_strcmp(list[0], test), 0);
396 cxFree(&alloc, list); 396 cxFree(&alloc, list);
397 397
398 // partially matching delimiter 398 // partially matching delimiter
399 n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list); 399 n = cx_strsplit_a(&alloc, test, CX_STR("is,not"), capa, &list);
400 ASSERT_EQ(n, 1); 400 ASSERT_EQ(n, 1);
401 EXPECT_EQ(cx_strcmp(list[0], test), 0); 401 EXPECT_EQ(cx_strcmp(list[0], test), 0);
402 cxFree(&alloc, list); 402 cxFree(&alloc, list);
403 403
404 // matching single-char delimiter 404 // matching single-char delimiter
405 n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list); 405 n = cx_strsplit_a(&alloc, test, CX_STR(","), capa, &list);
406 ASSERT_EQ(n, 5); 406 ASSERT_EQ(n, 5);
407 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 407 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0);
408 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 408 EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0);
409 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); 409 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0);
410 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); 410 EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0);
411 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); 411 EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0);
412 cxFree(&alloc, list); 412 cxFree(&alloc, list);
413 413
414 // matching multi-char delimiter 414 // matching multi-char delimiter
415 n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list); 415 n = cx_strsplit_a(&alloc, test, CX_STR("is"), capa, &list);
416 ASSERT_EQ(n, 3); 416 ASSERT_EQ(n, 3);
417 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 417 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
418 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); 418 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0);
419 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); 419 EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0);
420 cxFree(&alloc, list); 420 cxFree(&alloc, list);
421 421
422 // bounded list using single-char delimiter 422 // bounded list using single-char delimiter
423 n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list); 423 n = cx_strsplit_a(&alloc, test, CX_STR(","), 3, &list);
424 ASSERT_EQ(n, 3); 424 ASSERT_EQ(n, 3);
425 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 425 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0);
426 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 426 EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0);
427 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 427 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0);
428 cxFree(&alloc, list); 428 cxFree(&alloc, list);
429 429
430 // bounded list using multi-char delimiter 430 // bounded list using multi-char delimiter
431 n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list); 431 n = cx_strsplit_a(&alloc, test, CX_STR("is"), 2, &list);
432 ASSERT_EQ(n, 2); 432 ASSERT_EQ(n, 2);
433 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 433 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
434 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 434 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0);
435 cxFree(&alloc, list); 435 cxFree(&alloc, list);
436 436
437 // start with delimiter 437 // start with delimiter
438 n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list); 438 n = cx_strsplit_a(&alloc, test, CX_STR("this"), capa, &list);
439 ASSERT_EQ(n, 2); 439 ASSERT_EQ(n, 2);
440 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 440 EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0);
441 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 441 EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0);
442 cxFree(&alloc, list); 442 cxFree(&alloc, list);
443 443
444 // end with delimiter 444 // end with delimiter
445 n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list); 445 n = cx_strsplit_a(&alloc, test, CX_STR("string"), capa, &list);
446 ASSERT_EQ(n, 2); 446 ASSERT_EQ(n, 2);
447 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); 447 EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0);
448 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 448 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
449 cxFree(&alloc, list); 449 cxFree(&alloc, list);
450 450
451 // end with delimiter exceed bound 451 // end with delimiter exceed bound
452 n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list); 452 n = cx_strsplit_a(&alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list);
453 ASSERT_EQ(n, 3); 453 ASSERT_EQ(n, 3);
454 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); 454 EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0);
455 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); 455 EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0);
456 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); 456 EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0);
457 cxFree(&alloc, list); 457 cxFree(&alloc, list);
458 458
459 // exact match 459 // exact match
460 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list); 460 n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string"), capa, &list);
461 ASSERT_EQ(n, 2); 461 ASSERT_EQ(n, 2);
462 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 462 EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0);
463 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 463 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
464 cxFree(&alloc, list); 464 cxFree(&alloc, list);
465 465
466 // string to be split is only substring 466 // string to be split is only substring
467 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list); 467 n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list);
468 ASSERT_EQ(n, 1); 468 ASSERT_EQ(n, 1);
469 EXPECT_EQ(cx_strcmp(list[0], test), 0); 469 EXPECT_EQ(cx_strcmp(list[0], test), 0);
470 cxFree(&alloc, list); 470 cxFree(&alloc, list);
471 471
472 // subsequent encounter of delimiter (the string between is empty) 472 // subsequent encounter of delimiter (the string between is empty)
473 n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list); 473 n = cx_strsplit_a(&alloc, test, CX_STR("is,"), capa, &list);
474 ASSERT_EQ(n, 3); 474 ASSERT_EQ(n, 3);
475 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 475 EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0);
476 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 476 EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0);
477 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 477 EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0);
478 cxFree(&alloc, list); 478 cxFree(&alloc, list);
479 479
480 // call the _m variant just for coverage 480 // call the _m variant just for coverage
481 auto mtest = cx_strdup(test); 481 auto mtest = cx_strdup(test);
482 cxmutstr *mlist; 482 cxmutstr *mlist;
483 n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist); 483 n = cx_strsplit_ma(&alloc, mtest, CX_STR("is,"), 4, &mlist);
484 ASSERT_EQ(n, 3); 484 ASSERT_EQ(n, 3);
485 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); 485 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0);
486 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); 486 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0);
487 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); 487 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0);
488 cxFree(&alloc, mlist); 488 cxFree(&alloc, mlist);
489 cx_strfree(&mtest); 489 cx_strfree(&mtest);
490 490
491 EXPECT_TRUE(alloc.verify()); 491 EXPECT_TRUE(alloc.verify());
492 } 492 }
493 493
494 TEST(String, strtrim) { 494 TEST(String, strtrim) {
495 cxstring t1 = cx_strtrim(cx_str(" ein test \t ")); 495 cxstring t1 = cx_strtrim(CX_STR(" ein test \t "));
496 cxstring t2 = cx_strtrim(cx_str("abc")); 496 cxstring t2 = cx_strtrim(CX_STR("abc"));
497 cxstring t3 = cx_strtrim(cx_str(" 123")); 497 cxstring t3 = cx_strtrim(CX_STR(" 123"));
498 cxstring t4 = cx_strtrim(cx_str("xyz ")); 498 cxstring t4 = cx_strtrim(CX_STR("xyz "));
499 cxstring t5 = cx_strtrim(cx_str(" ")); 499 cxstring t5 = cx_strtrim(CX_STR(" "));
500 cxstring empty = cx_strtrim(cx_str("")); 500 cxstring empty = cx_strtrim(CX_STR(""));
501 501
502 EXPECT_EQ(cx_strcmp(t1, cx_str("ein test")), 0); 502 EXPECT_EQ(cx_strcmp(t1, CX_STR("ein test")), 0);
503 EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0); 503 EXPECT_EQ(cx_strcmp(t2, CX_STR("abc")), 0);
504 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0); 504 EXPECT_EQ(cx_strcmp(t3, CX_STR("123")), 0);
505 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0); 505 EXPECT_EQ(cx_strcmp(t4, CX_STR("xyz")), 0);
506 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); 506 EXPECT_EQ(cx_strcmp(t5, CX_STR("")), 0);
507 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); 507 EXPECT_EQ(cx_strcmp(empty, CX_STR("")), 0);
508 508
509 // call the _m variant just for coverage 509 // call the _m variant just for coverage
510 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); 510 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t "));
511 EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0); 511 EXPECT_EQ(cx_strcmp(cx_strcast(m1), CX_STR("ein test")), 0);
512 } 512 }
513 513
514 TEST(String, strprefix) { 514 TEST(String, strprefix) {
515 cxstring str = CX_STR("test my prefix and my suffix"); 515 cxstring str = CX_STR("test my prefix and my suffix");
516 cxstring empty = CX_STR(""); 516 cxstring empty = CX_STR("");
517 EXPECT_FALSE(cx_strprefix(empty, cx_str("pref"))); 517 EXPECT_FALSE(cx_strprefix(empty, CX_STR("pref")));
518 EXPECT_TRUE(cx_strprefix(str, empty)); 518 EXPECT_TRUE(cx_strprefix(str, empty));
519 EXPECT_TRUE(cx_strprefix(empty, empty)); 519 EXPECT_TRUE(cx_strprefix(empty, empty));
520 EXPECT_TRUE(cx_strprefix(str, cx_str("test "))); 520 EXPECT_TRUE(cx_strprefix(str, CX_STR("test ")));
521 EXPECT_FALSE(cx_strprefix(str, cx_str("8-) fsck "))); 521 EXPECT_FALSE(cx_strprefix(str, CX_STR("8-) fsck ")));
522 } 522 }
523 523
524 TEST(String, strsuffix) { 524 TEST(String, strsuffix) {
525 cxstring str = CX_STR("test my prefix and my suffix"); 525 cxstring str = CX_STR("test my prefix and my suffix");
526 cxstring empty = CX_STR(""); 526 cxstring empty = CX_STR("");
527 EXPECT_FALSE(cx_strsuffix(empty, cx_str("suf"))); 527 EXPECT_FALSE(cx_strsuffix(empty, CX_STR("suf")));
528 EXPECT_TRUE(cx_strsuffix(str, empty)); 528 EXPECT_TRUE(cx_strsuffix(str, empty));
529 EXPECT_TRUE(cx_strsuffix(empty, empty)); 529 EXPECT_TRUE(cx_strsuffix(empty, empty));
530 EXPECT_TRUE(cx_strsuffix(str, cx_str("fix"))); 530 EXPECT_TRUE(cx_strsuffix(str, CX_STR("fix")));
531 EXPECT_FALSE(cx_strsuffix(str, cx_str("fox"))); 531 EXPECT_FALSE(cx_strsuffix(str, CX_STR("fox")));
532 } 532 }
533 533
534 TEST(String, strcaseprefix) { 534 TEST(String, strcaseprefix) {
535 cxstring str = CX_STR("test my prefix and my suffix"); 535 cxstring str = CX_STR("test my prefix and my suffix");
536 cxstring empty = CX_STR(""); 536 cxstring empty = CX_STR("");
537 EXPECT_FALSE(cx_strcaseprefix(empty, cx_str("pREf"))); 537 EXPECT_FALSE(cx_strcaseprefix(empty, CX_STR("pREf")));
538 EXPECT_TRUE(cx_strcaseprefix(str, empty)); 538 EXPECT_TRUE(cx_strcaseprefix(str, empty));
539 EXPECT_TRUE(cx_strcaseprefix(empty, empty)); 539 EXPECT_TRUE(cx_strcaseprefix(empty, empty));
540 EXPECT_TRUE(cx_strcaseprefix(str, cx_str("TEST "))); 540 EXPECT_TRUE(cx_strcaseprefix(str, CX_STR("TEST ")));
541 EXPECT_FALSE(cx_strcaseprefix(str, cx_str("8-) fsck "))); 541 EXPECT_FALSE(cx_strcaseprefix(str, CX_STR("8-) fsck ")));
542 } 542 }
543 543
544 TEST(String, strcasesuffix) { 544 TEST(String, strcasesuffix) {
545 cxstring str = CX_STR("test my prefix and my suffix"); 545 cxstring str = CX_STR("test my prefix and my suffix");
546 cxstring empty = CX_STR(""); 546 cxstring empty = CX_STR("");
547 EXPECT_FALSE(cx_strcasesuffix(empty, cx_str("sUf"))); 547 EXPECT_FALSE(cx_strcasesuffix(empty, CX_STR("sUf")));
548 EXPECT_TRUE(cx_strcasesuffix(str, empty)); 548 EXPECT_TRUE(cx_strcasesuffix(str, empty));
549 EXPECT_TRUE(cx_strcasesuffix(empty, empty)); 549 EXPECT_TRUE(cx_strcasesuffix(empty, empty));
550 EXPECT_TRUE(cx_strcasesuffix(str, cx_str("FIX"))); 550 EXPECT_TRUE(cx_strcasesuffix(str, CX_STR("FIX")));
551 EXPECT_FALSE(cx_strcasesuffix(str, cx_str("fox"))); 551 EXPECT_FALSE(cx_strcasesuffix(str, CX_STR("fox")));
552 } 552 }
553 553
554 TEST(String, strreplace) { 554 TEST(String, strreplace) {
555 CxTestingAllocator alloc; 555 CxTestingAllocator alloc;
556 cxstring str = CX_STR("test ababab string aba"); 556 cxstring str = CX_STR("test ababab string aba");
559 cxstring notrail = CX_STR("test abab"); 559 cxstring notrail = CX_STR("test abab");
560 cxstring empty = CX_STR(""); 560 cxstring empty = CX_STR("");
561 cxstring astr = CX_STR("aaaaaaaaaa"); 561 cxstring astr = CX_STR("aaaaaaaaaa");
562 cxstring csstr = CX_STR("test AB ab TEST xyz"); 562 cxstring csstr = CX_STR("test AB ab TEST xyz");
563 563
564 cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger")); 564 cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger"));
565 auto expected = "test muchlongerab string aba"; 565 auto expected = "test muchlongerab string aba";
566 566
567 cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2); 567 cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2);
568 auto expectedn = "test ccab string aba"; 568 auto expectedn = "test ccab string aba";
569 569
570 cxmutstr longrepl = cx_strreplace(longstr, cx_str("a"), cx_str("z")); 570 cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z"));
571 auto longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd"; 571 auto longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd";
572 572
573 cxmutstr replnotrail = cx_strreplace(notrail, cx_str("ab"), cx_str("z")); 573 cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z"));
574 auto notrailexpect = "test zz"; 574 auto notrailexpect = "test zz";
575 575
576 cxmutstr repleq = cx_strreplace(str, str, cx_str("hello")); 576 cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello"));
577 auto eqexpect = "hello"; 577 auto eqexpect = "hello";
578 578
579 cxmutstr replempty1 = cx_strreplace(empty, cx_str("ab"), cx_str("c")); // expect: empty 579 cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty
580 cxmutstr replempty2 = cx_strreplace(str, cx_str("abab"), empty); 580 cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty);
581 auto emptyexpect2 = "test ab string aba"; 581 auto emptyexpect2 = "test ab string aba";
582 582
583 cxmutstr replpre = cx_strreplace(str, cx_str("test "), cx_str("TEST ")); 583 cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST "));
584 auto preexpected = "TEST ababab string aba"; 584 auto preexpected = "TEST ababab string aba";
585 585
586 cxmutstr replan1 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 1); 586 cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1);
587 auto an1expected = "xaaaaaaaaa"; 587 auto an1expected = "xaaaaaaaaa";
588 588
589 cxmutstr replan4 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 4); 589 cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4);
590 auto an4expected = "xxxxaaaaaa"; 590 auto an4expected = "xxxxaaaaaa";
591 591
592 cxmutstr replan9 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 9); 592 cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9);
593 auto an9expected = "xxxxxxxxxa"; 593 auto an9expected = "xxxxxxxxxa";
594 594
595 cxmutstr replan10 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 10); 595 cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10);
596 auto an10expected = "xxxxxxxxxx"; 596 auto an10expected = "xxxxxxxxxx";
597 597
598 cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, cx_str("AB"), cx_str("*")); 598 cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, CX_STR("AB"), CX_STR("*"));
599 auto expeced1_a = "test * ab TEST xyz"; 599 auto expeced1_a = "test * ab TEST xyz";
600 600
601 cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, cx_str("test"), cx_str("TEST")); 601 cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, CX_STR("test"), CX_STR("TEST"));
602 auto expected2_a = "TEST AB ab TEST xyz"; 602 auto expected2_a = "TEST AB ab TEST xyz";
603 603
604 604
605 EXPECT_NE(repl.ptr, str.ptr); 605 EXPECT_NE(repl.ptr, str.ptr);
606 EXPECT_ZERO_TERMINATED(repl); 606 EXPECT_ZERO_TERMINATED(repl);
649 cx_strfree_a(&alloc, &repl2_a); 649 cx_strfree_a(&alloc, &repl2_a);
650 EXPECT_TRUE(alloc.verify()); 650 EXPECT_TRUE(alloc.verify());
651 } 651 }
652 652
653 TEST(String, strupper) { 653 TEST(String, strupper) {
654 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t")); 654 cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
655 cx_strupper(str); 655 cx_strupper(str);
656 EXPECT_STREQ(str.ptr, "THIS 1S @ TE$T"); 656 EXPECT_STREQ(str.ptr, "THIS 1S @ TE$T");
657 cx_strfree(&str); 657 cx_strfree(&str);
658 } 658 }
659 659
660 TEST(String, strlower) { 660 TEST(String, strlower) {
661 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t")); 661 cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
662 cx_strlower(str); 662 cx_strlower(str);
663 EXPECT_STREQ(str.ptr, "this 1s @ te$t"); 663 EXPECT_STREQ(str.ptr, "this 1s @ te$t");
664 cx_strfree(&str); 664 cx_strfree(&str);
665 } 665 }
666 666
667 TEST(String, strtok) { 667 TEST(String, strtok) {
668 cxstring str = cx_str("a,comma,separated,string"); 668 cxstring str = CX_STR("a,comma,separated,string");
669 cxstring delim = cx_str(","); 669 cxstring delim = CX_STR(",");
670 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 670 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
671 EXPECT_EQ(ctx.str.ptr, str.ptr); 671 EXPECT_EQ(ctx.str.ptr, str.ptr);
672 EXPECT_EQ(ctx.str.length, str.length); 672 EXPECT_EQ(ctx.str.length, str.length);
673 EXPECT_EQ(ctx.delim.ptr, delim.ptr); 673 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
674 EXPECT_EQ(ctx.delim.length, delim.length); 674 EXPECT_EQ(ctx.delim.length, delim.length);
679 EXPECT_EQ(ctx.delim_more, nullptr); 679 EXPECT_EQ(ctx.delim_more, nullptr);
680 EXPECT_EQ(ctx.delim_more_count, 0); 680 EXPECT_EQ(ctx.delim_more_count, 0);
681 } 681 }
682 682
683 TEST(String, strtok_m) { 683 TEST(String, strtok_m) {
684 cxmutstr str = cx_strdup(cx_str("a,comma,separated,string")); 684 cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string"));
685 cxstring delim = cx_str(","); 685 cxstring delim = CX_STR(",");
686 CxStrtokCtx ctx = cx_strtok_m(str, delim, 3); 686 CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
687 EXPECT_EQ(ctx.str.ptr, str.ptr); 687 EXPECT_EQ(ctx.str.ptr, str.ptr);
688 EXPECT_EQ(ctx.str.length, str.length); 688 EXPECT_EQ(ctx.str.length, str.length);
689 EXPECT_EQ(ctx.delim.ptr, delim.ptr); 689 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
690 EXPECT_EQ(ctx.delim.length, delim.length); 690 EXPECT_EQ(ctx.delim.length, delim.length);
696 EXPECT_EQ(ctx.delim_more_count, 0); 696 EXPECT_EQ(ctx.delim_more_count, 0);
697 cx_strfree(&str); 697 cx_strfree(&str);
698 } 698 }
699 699
700 TEST(String, strtok_delim) { 700 TEST(String, strtok_delim) {
701 cxstring str = cx_str("an,arbitrarily|separated;string"); 701 cxstring str = CX_STR("an,arbitrarily|separated;string");
702 cxstring delim = cx_str(","); 702 cxstring delim = CX_STR(",");
703 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")}; 703 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
704 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 704 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
705 cx_strtok_delim(&ctx, delim_more, 2); 705 cx_strtok_delim(&ctx, delim_more, 2);
706 EXPECT_EQ(ctx.str.ptr, str.ptr); 706 EXPECT_EQ(ctx.str.ptr, str.ptr);
707 EXPECT_EQ(ctx.str.length, str.length); 707 EXPECT_EQ(ctx.str.length, str.length);
714 EXPECT_EQ(ctx.delim_more, delim_more); 714 EXPECT_EQ(ctx.delim_more, delim_more);
715 EXPECT_EQ(ctx.delim_more_count, 2); 715 EXPECT_EQ(ctx.delim_more_count, 2);
716 } 716 }
717 717
718 TEST(String, strtok_next_easy) { 718 TEST(String, strtok_next_easy) {
719 cxstring str = cx_str("a,comma,separated,string"); 719 cxstring str = CX_STR("a,comma,separated,string");
720 cxstring delim = cx_str(","); 720 cxstring delim = CX_STR(",");
721 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 721 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
722 bool ret; 722 bool ret;
723 cxstring tok; 723 cxstring tok;
724 724
725 ret = cx_strtok_next(&ctx, &tok); 725 ret = cx_strtok_next(&ctx, &tok);
726 ASSERT_TRUE(ret); 726 ASSERT_TRUE(ret);
727 EXPECT_EQ(cx_strcmp(tok, cx_str("a")), 0); 727 EXPECT_EQ(cx_strcmp(tok, CX_STR("a")), 0);
728 EXPECT_EQ(ctx.pos, 0); 728 EXPECT_EQ(ctx.pos, 0);
729 EXPECT_EQ(ctx.next_pos, 2); 729 EXPECT_EQ(ctx.next_pos, 2);
730 EXPECT_EQ(ctx.delim_pos, 1); 730 EXPECT_EQ(ctx.delim_pos, 1);
731 EXPECT_EQ(ctx.found, 1); 731 EXPECT_EQ(ctx.found, 1);
732 732
733 ret = cx_strtok_next(&ctx, &tok); 733 ret = cx_strtok_next(&ctx, &tok);
734 ASSERT_TRUE(ret); 734 ASSERT_TRUE(ret);
735 EXPECT_EQ(cx_strcmp(tok, cx_str("comma")), 0); 735 EXPECT_EQ(cx_strcmp(tok, CX_STR("comma")), 0);
736 EXPECT_EQ(ctx.pos, 2); 736 EXPECT_EQ(ctx.pos, 2);
737 EXPECT_EQ(ctx.next_pos, 8); 737 EXPECT_EQ(ctx.next_pos, 8);
738 EXPECT_EQ(ctx.delim_pos, 7); 738 EXPECT_EQ(ctx.delim_pos, 7);
739 EXPECT_EQ(ctx.found, 2); 739 EXPECT_EQ(ctx.found, 2);
740 740
741 ret = cx_strtok_next(&ctx, &tok); 741 ret = cx_strtok_next(&ctx, &tok);
742 ASSERT_TRUE(ret); 742 ASSERT_TRUE(ret);
743 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0); 743 EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0);
744 EXPECT_EQ(ctx.pos, 8); 744 EXPECT_EQ(ctx.pos, 8);
745 EXPECT_EQ(ctx.next_pos, 18); 745 EXPECT_EQ(ctx.next_pos, 18);
746 EXPECT_EQ(ctx.delim_pos, 17); 746 EXPECT_EQ(ctx.delim_pos, 17);
747 EXPECT_EQ(ctx.found, 3); 747 EXPECT_EQ(ctx.found, 3);
748 748
753 EXPECT_EQ(ctx.delim_pos, 17); 753 EXPECT_EQ(ctx.delim_pos, 17);
754 EXPECT_EQ(ctx.found, 3); 754 EXPECT_EQ(ctx.found, 3);
755 } 755 }
756 756
757 TEST(String, strtok_next_unlimited) { 757 TEST(String, strtok_next_unlimited) {
758 cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;"); 758 cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;");
759 cxstring delim = cx_str(";-;"); 759 cxstring delim = CX_STR(";-;");
760 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX); 760 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
761 bool ret; 761 bool ret;
762 cxstring tok; 762 cxstring tok;
763 763
764 ret = cx_strtok_next(&ctx, &tok); 764 ret = cx_strtok_next(&ctx, &tok);
765 ASSERT_TRUE(ret); 765 ASSERT_TRUE(ret);
766 EXPECT_EQ(cx_strcmp(tok, cx_str("some")), 0); 766 EXPECT_EQ(cx_strcmp(tok, CX_STR("some")), 0);
767 EXPECT_EQ(ctx.pos, 0); 767 EXPECT_EQ(ctx.pos, 0);
768 EXPECT_EQ(ctx.next_pos, 7); 768 EXPECT_EQ(ctx.next_pos, 7);
769 EXPECT_EQ(ctx.delim_pos, 4); 769 EXPECT_EQ(ctx.delim_pos, 4);
770 EXPECT_EQ(ctx.found, 1); 770 EXPECT_EQ(ctx.found, 1);
771 771
772 ret = cx_strtok_next(&ctx, &tok); 772 ret = cx_strtok_next(&ctx, &tok);
773 ASSERT_TRUE(ret); 773 ASSERT_TRUE(ret);
774 EXPECT_EQ(cx_strcmp(tok, cx_str("otherwise")), 0); 774 EXPECT_EQ(cx_strcmp(tok, CX_STR("otherwise")), 0);
775 EXPECT_EQ(ctx.pos, 7); 775 EXPECT_EQ(ctx.pos, 7);
776 EXPECT_EQ(ctx.next_pos, 19); 776 EXPECT_EQ(ctx.next_pos, 19);
777 EXPECT_EQ(ctx.delim_pos, 16); 777 EXPECT_EQ(ctx.delim_pos, 16);
778 EXPECT_EQ(ctx.found, 2); 778 EXPECT_EQ(ctx.found, 2);
779 779
780 ret = cx_strtok_next(&ctx, &tok); 780 ret = cx_strtok_next(&ctx, &tok);
781 ASSERT_TRUE(ret); 781 ASSERT_TRUE(ret);
782 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0); 782 EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0);
783 EXPECT_EQ(ctx.pos, 19); 783 EXPECT_EQ(ctx.pos, 19);
784 EXPECT_EQ(ctx.next_pos, 31); 784 EXPECT_EQ(ctx.next_pos, 31);
785 EXPECT_EQ(ctx.delim_pos, 28); 785 EXPECT_EQ(ctx.delim_pos, 28);
786 EXPECT_EQ(ctx.found, 3); 786 EXPECT_EQ(ctx.found, 3);
787 787
788 ret = cx_strtok_next(&ctx, &tok); 788 ret = cx_strtok_next(&ctx, &tok);
789 ASSERT_TRUE(ret); 789 ASSERT_TRUE(ret);
790 EXPECT_EQ(cx_strcmp(tok, cx_str("string")), 0); 790 EXPECT_EQ(cx_strcmp(tok, CX_STR("string")), 0);
791 EXPECT_EQ(ctx.pos, 31); 791 EXPECT_EQ(ctx.pos, 31);
792 EXPECT_EQ(ctx.next_pos, 40); 792 EXPECT_EQ(ctx.next_pos, 40);
793 EXPECT_EQ(ctx.delim_pos, 37); 793 EXPECT_EQ(ctx.delim_pos, 37);
794 EXPECT_EQ(ctx.found, 4); 794 EXPECT_EQ(ctx.found, 4);
795 795
796 ret = cx_strtok_next(&ctx, &tok); 796 ret = cx_strtok_next(&ctx, &tok);
797 ASSERT_TRUE(ret); 797 ASSERT_TRUE(ret);
798 EXPECT_EQ(cx_strcmp(tok, cx_str("")), 0); 798 EXPECT_EQ(cx_strcmp(tok, CX_STR("")), 0);
799 EXPECT_EQ(ctx.pos, 40); 799 EXPECT_EQ(ctx.pos, 40);
800 EXPECT_EQ(ctx.next_pos, 40); 800 EXPECT_EQ(ctx.next_pos, 40);
801 EXPECT_EQ(ctx.delim_pos, 40); 801 EXPECT_EQ(ctx.delim_pos, 40);
802 EXPECT_EQ(ctx.found, 5); 802 EXPECT_EQ(ctx.found, 5);
803 803
807 EXPECT_EQ(ctx.delim_pos, 40); 807 EXPECT_EQ(ctx.delim_pos, 40);
808 EXPECT_EQ(ctx.found, 5); 808 EXPECT_EQ(ctx.found, 5);
809 } 809 }
810 810
811 TEST(String, strtok_next_advanced) { 811 TEST(String, strtok_next_advanced) {
812 cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string")); 812 cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string"));
813 cxstring delim = cx_str(","); 813 cxstring delim = CX_STR(",");
814 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; 814 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
815 CxStrtokCtx ctx = cx_strtok_m(str, delim, 10); 815 CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
816 cx_strtok_delim(&ctx, delim_more, 2); 816 cx_strtok_delim(&ctx, delim_more, 2);
817 bool ret; 817 bool ret;
818 cxmutstr tok; 818 cxmutstr tok;
819 819
820 ret = cx_strtok_next_m(&ctx, &tok); 820 ret = cx_strtok_next_m(&ctx, &tok);
821 ASSERT_TRUE(ret); 821 ASSERT_TRUE(ret);
822 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("an")), 0); 822 EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("an")), 0);
823 EXPECT_EQ(ctx.pos, 0); 823 EXPECT_EQ(ctx.pos, 0);
824 EXPECT_EQ(ctx.next_pos, 3); 824 EXPECT_EQ(ctx.next_pos, 3);
825 EXPECT_EQ(ctx.delim_pos, 2); 825 EXPECT_EQ(ctx.delim_pos, 2);
826 EXPECT_EQ(ctx.found, 1); 826 EXPECT_EQ(ctx.found, 1);
827 cx_strupper(tok); 827 cx_strupper(tok);
828 828
829 ret = cx_strtok_next_m(&ctx, &tok); 829 ret = cx_strtok_next_m(&ctx, &tok);
830 ASSERT_TRUE(ret); 830 ASSERT_TRUE(ret);
831 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("arbitrarily")), 0); 831 EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")), 0);
832 EXPECT_EQ(ctx.pos, 3); 832 EXPECT_EQ(ctx.pos, 3);
833 EXPECT_EQ(ctx.next_pos, 15); 833 EXPECT_EQ(ctx.next_pos, 15);
834 EXPECT_EQ(ctx.delim_pos, 14); 834 EXPECT_EQ(ctx.delim_pos, 14);
835 EXPECT_EQ(ctx.found, 2); 835 EXPECT_EQ(ctx.found, 2);
836 cx_strupper(tok); 836 cx_strupper(tok);
837 837
838 ret = cx_strtok_next_m(&ctx, &tok); 838 ret = cx_strtok_next_m(&ctx, &tok);
839 ASSERT_TRUE(ret); 839 ASSERT_TRUE(ret);
840 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("")), 0); 840 EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("")), 0);
841 EXPECT_EQ(ctx.pos, 15); 841 EXPECT_EQ(ctx.pos, 15);
842 EXPECT_EQ(ctx.next_pos, 17); 842 EXPECT_EQ(ctx.next_pos, 17);
843 EXPECT_EQ(ctx.delim_pos, 15); 843 EXPECT_EQ(ctx.delim_pos, 15);
844 EXPECT_EQ(ctx.found, 3); 844 EXPECT_EQ(ctx.found, 3);
845 cx_strupper(tok); 845 cx_strupper(tok);
846 846
847 ret = cx_strtok_next_m(&ctx, &tok); 847 ret = cx_strtok_next_m(&ctx, &tok);
848 ASSERT_TRUE(ret); 848 ASSERT_TRUE(ret);
849 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("separated")), 0); 849 EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("separated")), 0);
850 EXPECT_EQ(ctx.pos, 17); 850 EXPECT_EQ(ctx.pos, 17);
851 EXPECT_EQ(ctx.next_pos, 27); 851 EXPECT_EQ(ctx.next_pos, 27);
852 EXPECT_EQ(ctx.delim_pos, 26); 852 EXPECT_EQ(ctx.delim_pos, 26);
853 EXPECT_EQ(ctx.found, 4); 853 EXPECT_EQ(ctx.found, 4);
854 cx_strupper(tok); 854 cx_strupper(tok);
855 855
856 ret = cx_strtok_next_m(&ctx, &tok); 856 ret = cx_strtok_next_m(&ctx, &tok);
857 ASSERT_TRUE(ret); 857 ASSERT_TRUE(ret);
858 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("string")), 0); 858 EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("string")), 0);
859 EXPECT_EQ(ctx.pos, 27); 859 EXPECT_EQ(ctx.pos, 27);
860 EXPECT_EQ(ctx.next_pos, 33); 860 EXPECT_EQ(ctx.next_pos, 33);
861 EXPECT_EQ(ctx.delim_pos, 33); 861 EXPECT_EQ(ctx.delim_pos, 33);
862 EXPECT_EQ(ctx.found, 5); 862 EXPECT_EQ(ctx.found, 5);
863 cx_strupper(tok); 863 cx_strupper(tok);
867 EXPECT_EQ(ctx.pos, 27); 867 EXPECT_EQ(ctx.pos, 27);
868 EXPECT_EQ(ctx.next_pos, 33); 868 EXPECT_EQ(ctx.next_pos, 33);
869 EXPECT_EQ(ctx.delim_pos, 33); 869 EXPECT_EQ(ctx.delim_pos, 33);
870 EXPECT_EQ(ctx.found, 5); 870 EXPECT_EQ(ctx.found, 5);
871 871
872 EXPECT_EQ(cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING")), 0); 872 EXPECT_EQ(cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")), 0);
873 873
874 cx_strfree(&str); 874 cx_strfree(&str);
875 } 875 }

mercurial