test/test_string.cpp

changeset 628
1e2be40f0cb5
parent 598
70b7456b5b12
child 645
ec50abb285ad
equal deleted inserted replaced
627:cc8cbabd27cd 628:1e2be40f0cb5
270 cxstring test = cx_str("this,is,a,csv,string"); 270 cxstring test = cx_str("this,is,a,csv,string");
271 size_t capa = 8; 271 size_t capa = 8;
272 cxstring list[8]; 272 cxstring list[8];
273 size_t n; 273 size_t n;
274 274
275 /* special case: empty string */ 275 // special case: empty string
276 n = cx_strsplit(test, cx_str(""), capa, list); 276 n = cx_strsplit(test, cx_str(""), capa, list);
277 ASSERT_EQ(n, 1); 277 ASSERT_EQ(n, 1);
278 EXPECT_EQ(cx_strcmp(list[0], test), 0); 278 EXPECT_EQ(cx_strcmp(list[0], test), 0);
279 279
280 /* no delimiter occurrence */ 280 // no delimiter occurrence
281 n = cx_strsplit(test, cx_str("z"), capa, list); 281 n = cx_strsplit(test, cx_str("z"), capa, list);
282 ASSERT_EQ(n, 1); 282 ASSERT_EQ(n, 1);
283 EXPECT_EQ(cx_strcmp(list[0], test), 0); 283 EXPECT_EQ(cx_strcmp(list[0], test), 0);
284 284
285 /* partially matching delimiter */ 285 // partially matching delimiter
286 n = cx_strsplit(test, cx_str("is,not"), capa, list); 286 n = cx_strsplit(test, cx_str("is,not"), 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 /* matching single-char delimiter */ 290 // matching single-char delimiter
291 n = cx_strsplit(test, cx_str(","), capa, list); 291 n = cx_strsplit(test, cx_str(","), capa, list);
292 ASSERT_EQ(n, 5); 292 ASSERT_EQ(n, 5);
293 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 293 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
294 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 294 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
295 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); 295 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
296 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); 296 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
297 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); 297 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
298 298
299 /* matching multi-char delimiter */ 299 // matching multi-char delimiter
300 n = cx_strsplit(test, cx_str("is"), capa, list); 300 n = cx_strsplit(test, cx_str("is"), capa, list);
301 ASSERT_EQ(n, 3); 301 ASSERT_EQ(n, 3);
302 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 302 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
303 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); 303 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
304 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); 304 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
305 305
306 /* bounded list using single-char delimiter */ 306 // bounded list using single-char delimiter
307 n = cx_strsplit(test, cx_str(","), 3, list); 307 n = cx_strsplit(test, cx_str(","), 3, list);
308 ASSERT_EQ(n, 3); 308 ASSERT_EQ(n, 3);
309 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 309 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
310 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 310 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
311 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 311 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
312 312
313 /* bounded list using multi-char delimiter */ 313 // bounded list using multi-char delimiter
314 n = cx_strsplit(test, cx_str("is"), 2, list); 314 n = cx_strsplit(test, cx_str("is"), 2, list);
315 ASSERT_EQ(n, 2); 315 ASSERT_EQ(n, 2);
316 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 316 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
317 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 317 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
318 318
319 /* start with delimiter */ 319 // start with delimiter
320 n = cx_strsplit(test, cx_str("this"), capa, list); 320 n = cx_strsplit(test, cx_str("this"), capa, list);
321 ASSERT_EQ(n, 2); 321 ASSERT_EQ(n, 2);
322 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 322 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
323 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 323 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
324 324
325 /* end with delimiter */ 325 // end with delimiter
326 n = cx_strsplit(test, cx_str("string"), capa, list); 326 n = cx_strsplit(test, cx_str("string"), capa, list);
327 ASSERT_EQ(n, 2); 327 ASSERT_EQ(n, 2);
328 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); 328 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
329 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 329 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
330 330
331 331
332 /* end with delimiter exceed bound */ 332 // end with delimiter exceed bound
333 n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list); 333 n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list);
334 ASSERT_EQ(n, 3); 334 ASSERT_EQ(n, 3);
335 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); 335 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
336 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); 336 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
337 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); 337 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
338 338
339 /* exact match */ 339 // exact match
340 n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list); 340 n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list);
341 ASSERT_EQ(n, 2); 341 ASSERT_EQ(n, 2);
342 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 342 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
343 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 343 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
344 344
345 /* string to be split is only substring */ 345 // string to be split is only substring
346 n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list); 346 n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list);
347 ASSERT_EQ(n, 1); 347 ASSERT_EQ(n, 1);
348 EXPECT_EQ(cx_strcmp(list[0], test), 0); 348 EXPECT_EQ(cx_strcmp(list[0], test), 0);
349 349
350 /* subsequent encounter of delimiter (the string between is empty) */ 350 // subsequent encounter of delimiter (the string between is empty)
351 n = cx_strsplit(test, cx_str("is,"), capa, list); 351 n = cx_strsplit(test, cx_str("is,"), capa, list);
352 ASSERT_EQ(n, 3); 352 ASSERT_EQ(n, 3);
353 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 353 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
354 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 354 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
355 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 355 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
356 356
357 /* call the _m variant just for coverage */ 357 // call the _m variant just for coverage
358 auto mtest = cx_strdup(test); 358 auto mtest = cx_strdup(test);
359 cxmutstr mlist[4]; 359 cxmutstr mlist[4];
360 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); 360 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist);
361 ASSERT_EQ(n, 3); 361 ASSERT_EQ(n, 3);
362 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); 362 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0);
371 cxstring test = cx_str("this,is,a,csv,string"); 371 cxstring test = cx_str("this,is,a,csv,string");
372 size_t capa = 8; 372 size_t capa = 8;
373 cxstring *list; 373 cxstring *list;
374 size_t n; 374 size_t n;
375 375
376 /* special case: empty string */ 376 // special case: empty string
377 n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list); 377 n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list);
378 ASSERT_EQ(n, 1); 378 ASSERT_EQ(n, 1);
379 EXPECT_EQ(cx_strcmp(list[0], test), 0); 379 EXPECT_EQ(cx_strcmp(list[0], test), 0);
380 cxFree(&alloc, list); 380 cxFree(&alloc, list);
381 381
382 /* no delimiter occurrence */ 382 // no delimiter occurrence
383 n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list); 383 n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list);
384 ASSERT_EQ(n, 1); 384 ASSERT_EQ(n, 1);
385 EXPECT_EQ(cx_strcmp(list[0], test), 0); 385 EXPECT_EQ(cx_strcmp(list[0], test), 0);
386 cxFree(&alloc, list); 386 cxFree(&alloc, list);
387 387
388 /* partially matching delimiter */ 388 // partially matching delimiter
389 n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list); 389 n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list);
390 ASSERT_EQ(n, 1); 390 ASSERT_EQ(n, 1);
391 EXPECT_EQ(cx_strcmp(list[0], test), 0); 391 EXPECT_EQ(cx_strcmp(list[0], test), 0);
392 cxFree(&alloc, list); 392 cxFree(&alloc, list);
393 393
394 /* matching single-char delimiter */ 394 // matching single-char delimiter
395 n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list); 395 n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list);
396 ASSERT_EQ(n, 5); 396 ASSERT_EQ(n, 5);
397 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 397 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
398 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 398 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
399 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); 399 EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0);
400 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); 400 EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0);
401 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); 401 EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0);
402 cxFree(&alloc, list); 402 cxFree(&alloc, list);
403 403
404 /* matching multi-char delimiter */ 404 // matching multi-char delimiter
405 n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list); 405 n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list);
406 ASSERT_EQ(n, 3); 406 ASSERT_EQ(n, 3);
407 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 407 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
408 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); 408 EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0);
409 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); 409 EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0);
410 cxFree(&alloc, list); 410 cxFree(&alloc, list);
411 411
412 /* bounded list using single-char delimiter */ 412 // bounded list using single-char delimiter
413 n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list); 413 n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list);
414 ASSERT_EQ(n, 3); 414 ASSERT_EQ(n, 3);
415 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); 415 EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0);
416 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); 416 EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0);
417 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 417 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
418 cxFree(&alloc, list); 418 cxFree(&alloc, list);
419 419
420 /* bounded list using multi-char delimiter */ 420 // bounded list using multi-char delimiter
421 n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list); 421 n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list);
422 ASSERT_EQ(n, 2); 422 ASSERT_EQ(n, 2);
423 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 423 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
424 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 424 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
425 cxFree(&alloc, list); 425 cxFree(&alloc, list);
426 426
427 /* start with delimiter */ 427 // start with delimiter
428 n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list); 428 n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list);
429 ASSERT_EQ(n, 2); 429 ASSERT_EQ(n, 2);
430 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 430 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
431 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); 431 EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0);
432 cxFree(&alloc, list); 432 cxFree(&alloc, list);
433 433
434 /* end with delimiter */ 434 // end with delimiter
435 n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list); 435 n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list);
436 ASSERT_EQ(n, 2); 436 ASSERT_EQ(n, 2);
437 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); 437 EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0);
438 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 438 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
439 cxFree(&alloc, list); 439 cxFree(&alloc, list);
440 440
441 /* end with delimiter exceed bound */ 441 // end with delimiter exceed bound
442 n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list); 442 n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list);
443 ASSERT_EQ(n, 3); 443 ASSERT_EQ(n, 3);
444 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); 444 EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0);
445 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); 445 EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0);
446 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); 446 EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0);
447 cxFree(&alloc, list); 447 cxFree(&alloc, list);
448 448
449 /* exact match */ 449 // exact match
450 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list); 450 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list);
451 ASSERT_EQ(n, 2); 451 ASSERT_EQ(n, 2);
452 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); 452 EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0);
453 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 453 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
454 cxFree(&alloc, list); 454 cxFree(&alloc, list);
455 455
456 /* string to be split is only substring */ 456 // string to be split is only substring
457 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list); 457 n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list);
458 ASSERT_EQ(n, 1); 458 ASSERT_EQ(n, 1);
459 EXPECT_EQ(cx_strcmp(list[0], test), 0); 459 EXPECT_EQ(cx_strcmp(list[0], test), 0);
460 cxFree(&alloc, list); 460 cxFree(&alloc, list);
461 461
462 /* subsequent encounter of delimiter (the string between is empty) */ 462 // subsequent encounter of delimiter (the string between is empty)
463 n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list); 463 n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list);
464 ASSERT_EQ(n, 3); 464 ASSERT_EQ(n, 3);
465 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); 465 EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0);
466 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); 466 EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0);
467 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); 467 EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0);
468 cxFree(&alloc, list); 468 cxFree(&alloc, list);
469 469
470 /* call the _m variant just for coverage */ 470 // call the _m variant just for coverage
471 auto mtest = cx_strdup(test); 471 auto mtest = cx_strdup(test);
472 cxmutstr *mlist; 472 cxmutstr *mlist;
473 n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist); 473 n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist);
474 ASSERT_EQ(n, 3); 474 ASSERT_EQ(n, 3);
475 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); 475 EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0);
494 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0); 494 EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0);
495 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0); 495 EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0);
496 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); 496 EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0);
497 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); 497 EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0);
498 498
499 /* call the _m variant just for coverage */ 499 // call the _m variant just for coverage
500 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); 500 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t "));
501 EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0); 501 EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0);
502 } 502 }
503 503
504 TEST(String, strprefix) { 504 TEST(String, strprefix) {

mercurial