comparison tests.c @ 647:1e7e3f96632e

And some more work.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 06:47:41 +0200
parents b897995101b7
children 2c9260f5cf44
comparison
equal deleted inserted replaced
646:9fcb0098f302 647:1e7e3f96632e
566 typedef struct 566 typedef struct
567 { 567 {
568 th_regex_char_t *str; 568 th_regex_char_t *str;
569 size_t nmatches; 569 size_t nmatches;
570 int flags; 570 int flags;
571 } test_regex_def; 571 } test_regex_def1;
572
573
574 typedef struct
575 {
576 th_regex_char_t *pattern;
577 th_regex_char_t *str;
578 size_t nmatches;
579 int flags;
580 } test_regex_def2;
572 581
573 582
574 void test_regex_print_matches(const th_regex_char_t *str, const th_regex_match_t *matches) 583 void test_regex_print_matches(const th_regex_char_t *str, const th_regex_match_t *matches)
575 { 584 {
576 for (const th_regex_match_t *mt = matches; 585 for (const th_regex_match_t *mt = matches;
584 th_free(tmp); 593 th_free(tmp);
585 } 594 }
586 } 595 }
587 596
588 597
589 void test_regex_list(const test_regex_def *list, const th_regex_char_t *pattern) 598 void test_regex_list1(const test_regex_def1 *list, const th_regex_char_t *pattern)
590 { 599 {
591 th_regex_t *expr = NULL; 600 th_regex_t *expr = NULL;
592 int res; 601 int res;
593 602
594 printf("========================================\n"); 603 printf("========================================\n");
600 goto out; 609 goto out;
601 } 610 }
602 611
603 th_regex_dump(stdout, 1, expr); 612 th_regex_dump(stdout, 1, expr);
604 613
605 for (const test_regex_def *def = list; def->str != NULL; def++) 614 for (const test_regex_def1 *def = list; def->str != NULL; def++)
606 { 615 {
607 th_regex_match_t *matches = NULL; 616 th_regex_match_t *matches = NULL;
608 size_t nmatches; 617 size_t nmatches;
609 618
610 printf("----------------------------------------\n"); 619 printf("----------------------------------------\n");
625 th_regex_free_matches(matches); 634 th_regex_free_matches(matches);
626 } 635 }
627 636
628 out: 637 out:
629 th_regex_free(expr); 638 th_regex_free(expr);
639 }
640
641 void test_regex_list2(const test_regex_def2 *list)
642 {
643 printf("========================================\n");
644
645 for (const test_regex_def2 *def = list; def->str != NULL; def++)
646 {
647 th_regex_t *expr = NULL;
648 th_regex_match_t *matches = NULL;
649 size_t nmatches;
650 int res;
651
652 printf("Compiling pattern \"%s\"\n", def->pattern);
653 if ((res = th_regex_compile(&expr, def->pattern)) != THERR_OK)
654 {
655 THERR("Regex compilation failed: %s\n",
656 th_error_str(res));
657 goto out;
658 }
659
660 th_regex_dump(stdout, 1, expr);
661
662 printf("----------------------------------------\n");
663
664 if ((res = th_regex_match(expr, def->str,
665 &nmatches, &matches, -1, def->flags)) != THERR_OK)
666 {
667 THERR("Regex match returned error: %s\n",
668 th_error_str(res));
669 goto out;
670 }
671
672 printf("'%s': matched %" PRIu_SIZE_T " time(s), testresult=%s\n",
673 def->str,
674 nmatches,
675 def->nmatches == nmatches ? "YES" : "NO");
676
677 test_regex_print_matches(def->str, matches);
678
679 out:
680 th_regex_free_matches(matches);
681 th_regex_free(expr);
682 }
630 } 683 }
631 684
632 #endif 685 #endif
633 686
634 687
904 th_regex_free(expr); 957 th_regex_free(expr);
905 } 958 }
906 959
907 #if 0 960 #if 0
908 { 961 {
909 static const test_regex_def tlist[] = 962 static const test_regex_def1 tlist[] =
910 { 963 {
911 { "abcfoabccg" , 1, 0 }, 964 { "abcfoabccg" , 1, 0 },
912 { "abcbcfoabccg" , 1, 0 }, 965 { "abcbcfoabccg" , 1, 0 },
913 { "abcbcfoabccgabcbcfoabccg" , 2, 0 }, 966 { "abcbcfoabccgabcbcfoabccg" , 2, 0 },
914 { "ffdsafS abcbcfoabccg zasdf" , 1, 0 }, 967 { "ffdsafS abcbcfoabccg zasdf" , 1, 0 },
915 { NULL , 0, 0 } 968 { NULL , 0, 0 }
916 }; 969 };
917 970
918 test_regex_list(tlist, "a(bc){1,2}fo[oab]*cc?g"); 971 test_regex_list1(tlist, "a(bc){1,2}fo[oab]*cc?g");
919 } 972 }
920 973
921 { 974 {
922 static const test_regex_def tlist[] = 975 static const test_regex_def1 tlist[] =
923 { 976 {
924 { "abcfoabccg" , 1, 0 }, 977 { "abcfoabccg" , 1, 0 },
925 { "abcbcfoabccg" , 1, 0 }, 978 { "abcbcfoabccg" , 1, 0 },
926 { "abcbcfoabccgabcbcfoabccg" , 2, 0 }, 979 { "abcbcfoabccgabcbcfoabccg" , 2, 0 },
927 { "ffdsafS abcbcfoabccg zasdf" , 0, 0 }, 980 { "ffdsafS abcbcfoabccg zasdf" , 0, 0 },
928 { NULL , 0, 0 } 981 { NULL , 0, 0 }
929 }; 982 };
930 983
931 test_regex_list(tlist, "^a(bc){1,2}fo[oab]*cc?g"); 984 test_regex_list1(tlist, "^a(bc){1,2}fo[oab]*cc?g");
932 } 985 }
933 986
934 { 987 {
935 static const test_regex_def tlist[] = 988 static const test_regex_def1 tlist[] =
936 { 989 {
937 { "cg" , 1, 0 }, 990 { "cg" , 1, 0 },
938 { "g" , 1, 0 }, 991 { "g" , 1, 0 },
939 { "" , 0, 0 }, 992 { "" , 0, 0 },
940 { "c" , 0, 0 }, 993 { "c" , 0, 0 },
941 { NULL , 0, 0 } 994 { NULL , 0, 0 }
942 }; 995 };
943 996
944 test_regex_list(tlist, "g$"); 997 test_regex_list1(tlist, "g$");
945 } 998 }
946 #endif 999 #endif
947 1000
948 { 1001 {
949 static const test_regex_def tlist[] = 1002 static const test_regex_def1 tlist[] =
950 { 1003 {
951 // { "zoobar" , 1, 0 }, 1004 // { "zoobar" , 1, 0 },
952 { "zoo lol bar" , 1, 0 }, 1005 { "zoo lol bar" , 1, 0 },
953 { "hoho zoo lol lol bar bar" , 1, 0 }, 1006 { "hoho zoo lol lol bar bar" , 1, 0 },
954 { NULL , 0, 0 } 1007 { NULL , 0, 0 }
955 }; 1008 };
956 1009
957 test_regex_list(tlist, "zoo.*?bar"); 1010 test_regex_list1(tlist, "zoo.*?bar");
1011 // test_regex_list(tlist, "zoo.*?bar");
958 } 1012 }
959 } 1013 }
960 #endif 1014 #endif
961 1015
962 // 1016 //