Mercurial > hg > th-libs
comparison tests.c @ 610:a0e8d9c6300b
A bit more work on the regex stuff.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 16 Jan 2020 03:33:11 +0200 |
parents | 566e6ef41f9d |
children | 2e3b81ae8c8a |
comparison
equal
deleted
inserted
replaced
609:69f1cb7f9b38 | 610:a0e8d9c6300b |
---|---|
560 | 560 |
561 | 561 |
562 typedef struct | 562 typedef struct |
563 { | 563 { |
564 th_regex_char *str; | 564 th_regex_char *str; |
565 size_t nmatches; | |
565 int flags; | 566 int flags; |
566 BOOL result; | |
567 } test_regex_def; | 567 } test_regex_def; |
568 | 568 |
569 | 569 |
570 void test_regex_list(const th_regex_char *pattern, const test_regex_def *list) | 570 void test_regex_list(const th_regex_char *pattern, const test_regex_def *list) |
571 { | 571 { |
581 goto out; | 581 goto out; |
582 } | 582 } |
583 | 583 |
584 for (const test_regex_def *def = list; def->str != NULL; def++) | 584 for (const test_regex_def *def = list; def->str != NULL; def++) |
585 { | 585 { |
586 BOOL matched = FALSE; | 586 th_regex_match_node *matches = NULL; |
587 | 587 size_t nmatches; |
588 if ((res = th_regex_match(reg, def->str, &matched, NULL, -1, def->flags)) != THERR_OK) | 588 |
589 if ((res = th_regex_match(reg, def->str, &nmatches, &matches, -1, def->flags)) != THERR_OK) | |
589 { | 590 { |
590 THERR("Regex match returned error: %s\n", | 591 THERR("Regex match returned error: %s\n", |
591 th_error_str(res)); | 592 th_error_str(res)); |
592 goto out; | 593 goto out; |
593 } | 594 } |
594 | 595 |
595 printf(" '%s': %s\n", def->str, matched ? "YES" : "NO"); | 596 printf(" '%s': matched %" PRIu_SIZE_T " time(s), testresult=%s\n", |
597 def->str, | |
598 nmatches, | |
599 def->nmatches == nmatches ? "YES" : "NO"); | |
600 | |
601 for (th_regex_match_node *m = matches; | |
602 m != NULL; m = (th_regex_match_node *) m->node.next) | |
603 { | |
604 char *tmp = th_strndup(def->str + m->start, m->len); | |
605 printf(" match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n", | |
606 m->start, m->len, tmp); | |
607 th_free(tmp); | |
608 } | |
609 | |
610 th_regex_free_matches(matches); | |
596 } | 611 } |
597 | 612 |
598 out: | 613 out: |
599 th_regex_free(reg); | 614 th_regex_free(reg); |
600 } | 615 } |
863 printf("result: %s\n", th_error_str(res)); | 878 printf("result: %s\n", th_error_str(res)); |
864 th_regex_free(reg); | 879 th_regex_free(reg); |
865 | 880 |
866 test_regex_def tst1[] = | 881 test_regex_def tst1[] = |
867 { | 882 { |
868 { "abcfoabccg" , 0, TRUE }, | 883 { "abcfoabccg" , 1, 0 }, |
869 { "abcbcfoabccg" , 0, TRUE }, | 884 { "abcbcfoabccg" , 1, 0 }, |
870 { NULL, 0, FALSE } | 885 { "abcbcfoabccgabcbcfoabccg" , 2, 0 }, |
886 { NULL, 0, 0 } | |
871 }; | 887 }; |
872 | 888 |
873 test_regex_list("^a(bc){1,2}fo[oab]*cc?g", tst1); | 889 test_regex_list("a(bc){1,2}fo[oab]*cc?g", tst1); |
874 } | 890 } |
875 | 891 |
876 // | 892 // |
877 // Print summary and exit | 893 // Print summary and exit |
878 // | 894 // |