|
1 | 1 | /* test_kstring.c -- kstring unit tests |
2 | 2 |
|
3 | | - Copyright (C) 2018, 2020, 2024 Genome Research Ltd. |
| 3 | + Copyright (C) 2018, 2020, 2024-2025 Genome Research Ltd. |
4 | 4 |
|
5 | 5 | Author: Rob Davies <rmd@sanger.ac.uk> |
6 | 6 |
|
@@ -451,6 +451,134 @@ static int test_kgetline2(void) { |
451 | 451 | return EXIT_SUCCESS; |
452 | 452 | } |
453 | 453 |
|
| 454 | +static int test_kinsertchar(void) { |
| 455 | + kstring_t t = KS_INITIALIZE, res = KS_INITIALIZE; |
| 456 | + int i = 0; |
| 457 | + struct data { |
| 458 | + int pos; |
| 459 | + const char *val; |
| 460 | + }; |
| 461 | + |
| 462 | + struct data tdata[] = { { -1, ""}, {0, "X0123"}, {1, "0X123"}, {2, "01X23"}, |
| 463 | + {3, "012X3"}, {4, "0123X"}, {5, ""} }; |
| 464 | + |
| 465 | + for (i = -1; i < 6; ++i) { |
| 466 | + kstring_t s = KS_INITIALIZE; |
| 467 | + kputs("0123", &s); |
| 468 | + if (kinsert_char('X', i, &s) < 0) { |
| 469 | + if ( i < 0 || i > 4) { ks_free(&s); continue; } //expected failures |
| 470 | + fprintf(stderr, "kinsert_char failed\n"); |
| 471 | + ks_free(&s); |
| 472 | + return -1; |
| 473 | + } |
| 474 | + if (s.s[s.l] != '\0') { |
| 475 | + fprintf(stderr, "No NUL termination on string from kinsert_char\n"); |
| 476 | + ks_free(&s); |
| 477 | + return -1; |
| 478 | + } |
| 479 | + if (memcmp(s.s, tdata[i + 1].val, s.l + 1)) { |
| 480 | + fprintf(stderr, "kinsert_char comparison failed\n"); |
| 481 | + ks_free(&s); |
| 482 | + return -1; |
| 483 | + } |
| 484 | + ks_free(&s); |
| 485 | + } |
| 486 | + //realloc checks |
| 487 | + for (i = 0; i < 7; ++i) { |
| 488 | + kputc('A' + i, &res); |
| 489 | + if (kinsert_char('A' + i, t.l, &t) < 0) { |
| 490 | + fprintf(stderr, "kinsert_char failed in realloc\n"); |
| 491 | + ks_free(&res); ks_free(&t); |
| 492 | + return -1; |
| 493 | + } |
| 494 | + if (t.s[t.l] != '\0') { |
| 495 | + fprintf(stderr, "No NUL termination on string from kinsert_char in realloc\n"); |
| 496 | + ks_free(&res); ks_free(&t); |
| 497 | + return -1; |
| 498 | + } |
| 499 | + if (memcmp(t.s, res.s, res.l+1)) { |
| 500 | + fprintf(stderr, "kinsert_char realloc comparison failed in realloc\n"); |
| 501 | + ks_free(&res); ks_free(&t); |
| 502 | + return -1; |
| 503 | + } |
| 504 | + } |
| 505 | + ks_free(&t); |
| 506 | + ks_free(&res); |
| 507 | + return 0; |
| 508 | +} |
| 509 | + |
| 510 | +static int test_kinsertstr(void) { |
| 511 | + kstring_t t = KS_INITIALIZE, res = KS_INITIALIZE; |
| 512 | + int i = 0; |
| 513 | + struct data { |
| 514 | + int pos; |
| 515 | + const char *val; |
| 516 | + }; |
| 517 | + |
| 518 | + struct data tdata[] = { { -1, ""}, {0, "XYZ0123"}, {1, "0XYZ123"}, |
| 519 | + {2, "01XYZ23"}, {3, "012XYZ3"}, {4, "0123XYZ"}, {5, ""} }; |
| 520 | + |
| 521 | + for (i = -1; i < 6; ++i) { |
| 522 | + kstring_t s = KS_INITIALIZE; |
| 523 | + kputs("0123", &s); |
| 524 | + if (kinsert_str("XYZ", i, &s) < 0) { |
| 525 | + if ( i < 0 || i > 4) { ks_free(&s); continue; } //expected failures |
| 526 | + fprintf(stderr, "kinsert_str failed\n"); |
| 527 | + return -1; |
| 528 | + } |
| 529 | + if (s.s[s.l] != '\0') { |
| 530 | + fprintf(stderr, "No NUL termination on string from kinsert_str\n"); |
| 531 | + return -1; |
| 532 | + } |
| 533 | + if (memcmp(s.s, tdata[i + 1].val, s.l + 1)) { |
| 534 | + fprintf(stderr, "kinsert_str comparison failed\n"); |
| 535 | + return -1; |
| 536 | + } |
| 537 | + ks_free(&s); |
| 538 | + } |
| 539 | + //realloc checks |
| 540 | + for (i = 0; i < 15; ++i) { |
| 541 | + kstring_t val = KS_INITIALIZE; |
| 542 | + ksprintf(&val, "%c", 'A' + i); |
| 543 | + kputs(val.s, &res); |
| 544 | + if (kinsert_str(val.s, t.l, &t) < 0) { |
| 545 | + ks_free(&val); |
| 546 | + fprintf(stderr, "kinsert_str failed in realloc\n"); |
| 547 | + return -1; |
| 548 | + } |
| 549 | + if (t.s[t.l] != '\0') { |
| 550 | + ks_free(&val); ks_free(&res); |
| 551 | + fprintf(stderr, "No NUL termination on string from kinsert_str in realloc\n"); |
| 552 | + return -1; |
| 553 | + } |
| 554 | + if (memcmp(t.s, res.s, res.l+1)) { |
| 555 | + ks_free(&val); ks_free(&res); |
| 556 | + fprintf(stderr, "kinsert_str realloc comparison failed in realloc\n"); |
| 557 | + return -1; |
| 558 | + } |
| 559 | + ks_free(&val); |
| 560 | + } |
| 561 | + //empty strings |
| 562 | + ks_free(&t); |
| 563 | + if (kinsert_str("", 1, &t)) { //expected |
| 564 | + if (kinsert_str("", 0, &t) || t.l != 0) { |
| 565 | + fprintf(stderr, "kinsert_str empty insertion failed\n"); |
| 566 | + return -1; |
| 567 | + } |
| 568 | + } else { |
| 569 | + fprintf(stderr, "kinsert_str empty ins to invalid pos succeeded\n"); |
| 570 | + return -1; |
| 571 | + } |
| 572 | + i = res.l; |
| 573 | + if (kinsert_str("", 1, &res) || i != res.l) { |
| 574 | + fprintf(stderr, "kinsert_str empty ins to valid pos failed\n"); |
| 575 | + ks_free(&res); |
| 576 | + return -1; |
| 577 | + } |
| 578 | + ks_free(&res); |
| 579 | + return 0; |
| 580 | +} |
| 581 | + |
454 | 582 | int main(int argc, char **argv) { |
455 | 583 | int opt, res = EXIT_SUCCESS; |
456 | 584 | int64_t start = 0; |
@@ -500,5 +628,11 @@ int main(int argc, char **argv) { |
500 | 628 | if (!test || strcmp(test, "kgetline2") == 0) |
501 | 629 | if (test_kgetline2() != 0) res = EXIT_FAILURE; |
502 | 630 |
|
| 631 | + if (!test || strcmp(test, "kinsertchar") == 0) |
| 632 | + if (test_kinsertchar() != 0) res = EXIT_FAILURE; |
| 633 | + |
| 634 | + if (!test || strcmp(test, "kinsertstr") == 0) |
| 635 | + if (test_kinsertstr() != 0) res = EXIT_FAILURE; |
| 636 | + |
503 | 637 | return res; |
504 | 638 | } |
0 commit comments