comparison tests.c @ 613:2e3b81ae8c8a

More work on regexes.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Jan 2020 16:01:27 +0200
parents a0e8d9c6300b
children afcaf5e38f56
comparison
equal deleted inserted replaced
612:cc9ec51b4875 613:2e3b81ae8c8a
522 522
523 th_cfg_add_section(&cfg, "another_sect", sect1); 523 th_cfg_add_section(&cfg, "another_sect", sect1);
524 524
525 // Test ptr 525 // Test ptr
526 test_start(&ctx, "Test configuration string list ptr"); 526 test_start(&ctx, "Test configuration string list ptr");
527 test_result(&ctx, (item = th_cfg_find(cfg, NULL, "string_list", -1)) != NULL && item->v.list == &v_str_list); 527 test_result(&ctx,
528 (item = th_cfg_find(cfg, NULL, "string_list", -1)) != NULL &&
529 item->v.list == &v_str_list);
528 test_end(&ctx); 530 test_end(&ctx);
529 531
530 // Test value finding 532 // Test value finding
531 test_config_values(cfg); 533 test_config_values(cfg);
532 534
565 size_t nmatches; 567 size_t nmatches;
566 int flags; 568 int flags;
567 } test_regex_def; 569 } test_regex_def;
568 570
569 571
570 void test_regex_list(const th_regex_char *pattern, const test_regex_def *list) 572 void test_regex_list(const test_regex_def *list, const th_regex_char *pattern)
571 { 573 {
572 th_regex_ctx *reg = NULL; 574 th_regex_ctx *reg = NULL;
573 int res; 575 int res;
574 576
575 577 printf("========================================\n");
576 printf("pattern '%s'\n", pattern); 578 printf("pattern '%s'\n", pattern);
577 if ((res = th_regex_compile(&reg, pattern)) != THERR_OK) 579 if ((res = th_regex_compile(&reg, pattern)) != THERR_OK)
578 { 580 {
579 THERR("Regex compilation failed: %s\n", 581 THERR("Regex compilation failed: %s\n",
580 th_error_str(res)); 582 th_error_str(res));
584 for (const test_regex_def *def = list; def->str != NULL; def++) 586 for (const test_regex_def *def = list; def->str != NULL; def++)
585 { 587 {
586 th_regex_match_node *matches = NULL; 588 th_regex_match_node *matches = NULL;
587 size_t nmatches; 589 size_t nmatches;
588 590
589 if ((res = th_regex_match(reg, def->str, &nmatches, &matches, -1, def->flags)) != THERR_OK) 591 if ((res = th_regex_match(reg, def->str,
592 &nmatches, &matches, -1, def->flags)) != THERR_OK)
590 { 593 {
591 THERR("Regex match returned error: %s\n", 594 THERR("Regex match returned error: %s\n",
592 th_error_str(res)); 595 th_error_str(res));
593 goto out; 596 goto out;
594 } 597 }
600 603
601 for (th_regex_match_node *m = matches; 604 for (th_regex_match_node *m = matches;
602 m != NULL; m = (th_regex_match_node *) m->node.next) 605 m != NULL; m = (th_regex_match_node *) m->node.next)
603 { 606 {
604 char *tmp = th_strndup(def->str + m->start, m->len); 607 char *tmp = th_strndup(def->str + m->start, m->len);
605 printf(" match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n", 608 printf(" match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n",
606 m->start, m->len, tmp); 609 m->start, m->len, tmp);
607 th_free(tmp); 610 th_free(tmp);
608 } 611 }
609 612
610 th_regex_free_matches(matches); 613 th_regex_free_matches(matches);
825 #if TH_ARCH == 32 828 #if TH_ARCH == 32
826 0x11223344; 829 0x11223344;
827 #elif TH_ARCH == 64 830 #elif TH_ARCH == 64
828 0xaabbccdd11223344; 831 0xaabbccdd11223344;
829 #else 832 #else
830 #error Unsupported TH_ARCH value. 833 # error Unsupported TH_ARCH value.
831 #endif 834 #endif
832 835
833 snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz); 836 snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz);
834 #if TH_ARCH == 32 837 #if TH_ARCH == 32
835 TEST2(strcmp, tmp, "0000000011223344h", TRUE); 838 TEST2(strcmp, tmp, "0000000011223344h", TRUE);
871 if (test_set_start("Regular expressions")) 874 if (test_set_start("Regular expressions"))
872 { 875 {
873 th_regex_ctx *reg = NULL; 876 th_regex_ctx *reg = NULL;
874 int res; 877 int res;
875 878
879 #if 0
876 res = th_regex_compile(&reg, "z*k+abba fabboa? k{4} [gz]{1,2} foo(bar|zoo)?"); 880 res = th_regex_compile(&reg, "z*k+abba fabboa? k{4} [gz]{1,2} foo(bar|zoo)?");
877 if (res != THERR_OK) 881 if (res != THERR_OK)
878 printf("result: %s\n", th_error_str(res)); 882 printf("result: %s\n", th_error_str(res));
879 th_regex_free(reg); 883 th_regex_free(reg);
880 884
881 test_regex_def tst1[] = 885 //
882 { 886 {
883 { "abcfoabccg" , 1, 0 }, 887 static const test_regex_def tlist[] =
884 { "abcbcfoabccg" , 1, 0 }, 888 {
885 { "abcbcfoabccgabcbcfoabccg" , 2, 0 }, 889 { "abcfoabccg" , 1, 0 },
886 { NULL, 0, 0 } 890 { "abcbcfoabccg" , 1, 0 },
887 }; 891 { "abcbcfoabccgabcbcfoabccg" , 2, 0 },
888 892 { "ffdsafS abcbcfoabccg zasdf" , 1, 0 },
889 test_regex_list("a(bc){1,2}fo[oab]*cc?g", tst1); 893 { NULL , 0, 0 }
894 };
895
896 test_regex_list(tlist, "a(bc){1,2}fo[oab]*cc?g");
897 }
898
899 {
900 static const test_regex_def tlist[] =
901 {
902 { "abcfoabccg" , 1, 0 },
903 { "abcbcfoabccg" , 1, 0 },
904 { "abcbcfoabccgabcbcfoabccg" , 2, 0 },
905 { "ffdsafS abcbcfoabccg zasdf" , 0, 0 },
906 { NULL , 0, 0 }
907 };
908
909 test_regex_list(tlist, "^a(bc){1,2}fo[oab]*cc?g");
910 }
911
912 {
913 static const test_regex_def tlist[] =
914 {
915 { "cg" , 1, 0 },
916 { "g" , 1, 0 },
917 { "" , 0, 0 },
918 { "c" , 0, 0 },
919 { NULL , 0, 0 }
920 };
921
922 test_regex_list(tlist, "g$");
923 }
924 #endif
925
926 {
927 static const test_regex_def tlist[] =
928 {
929 // { "zoobar" , 1, 0 },
930 { "zoo lol bar" , 1, 0 },
931 // { "hoho zoo lol lol bar bar" , 1, 0 },
932 { NULL , 0, 0 }
933 };
934
935 test_regex_list(tlist, "zoo.*?bar");
936 }
890 } 937 }
891 938
892 // 939 //
893 // Print summary and exit 940 // Print summary and exit
894 // 941 //