| 38 | } |
| 39 | |
| 40 | static void test_regex() { |
| 41 | printf("[%s]\n", __func__); |
| 42 | auto test = [](const test_case & test_case) { |
| 43 | common_regex cr(test_case.pattern); |
| 44 | std::cout << "Testing pattern: /" << test_case.pattern << "/\n"; |
| 45 | // std::cout << " partial rev: " << cr.reversed_partial_pattern.str() << '\n'; |
| 46 | for (const auto & input_output : test_case.inputs_outputs) { |
| 47 | std::cout << " Input: " << input_output.input << '\n'; |
| 48 | auto m = cr.search(input_output.input, 0); |
| 49 | if (m != input_output.output) { |
| 50 | auto match_to_str = [&](const std::optional<common_regex_match> & m) { |
| 51 | std::ostringstream ss; |
| 52 | if (m->type == COMMON_REGEX_MATCH_TYPE_NONE) { |
| 53 | ss << "<no match>"; |
| 54 | } else { |
| 55 | GGML_ASSERT(!input_output.output.groups.empty()); |
| 56 | std::vector<std::string> parts; |
| 57 | for (const auto & g : m->groups) { |
| 58 | parts.push_back("{" + std::to_string(g.begin) + ", " + std::to_string(g.end) + "}"); |
| 59 | } |
| 60 | ss << "{" << common_regex_match_type_name(m->type) << ", {" << string_join(parts, ", ") << "}}"; |
| 61 | } |
| 62 | return ss.str(); |
| 63 | }; |
| 64 | std::cout << " Expected: " << match_to_str(input_output.output) << '\n'; |
| 65 | std::cout << " Got: " << match_to_str(m) << '\n'; |
| 66 | std::cout << " Inverted pattern: /" << regex_to_reversed_partial_regex(test_case.pattern) << "/\n"; |
| 67 | |
| 68 | throw std::runtime_error("Test failed"); |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | test({ |
| 73 | "a", |
| 74 | { |
| 75 | {"a", {COMMON_REGEX_MATCH_TYPE_FULL, {{0, 1}}}}, |
| 76 | {"b", {COMMON_REGEX_MATCH_TYPE_NONE, {}}}, |
| 77 | {"ab", {COMMON_REGEX_MATCH_TYPE_FULL, {{0, 1}}}}, |
| 78 | {"ba", {COMMON_REGEX_MATCH_TYPE_FULL, {{1, 2}}}}, |
| 79 | } |
| 80 | }); |
| 81 | test({ |
| 82 | "abcd", |
| 83 | { |
| 84 | {"abcd", {COMMON_REGEX_MATCH_TYPE_FULL, {{0, 4}}}}, |
| 85 | {"abcde", {COMMON_REGEX_MATCH_TYPE_FULL, {{0, 4}}}}, |
| 86 | {"abc", {COMMON_REGEX_MATCH_TYPE_PARTIAL, {{0, 3}}}}, |
| 87 | {"ab", {COMMON_REGEX_MATCH_TYPE_PARTIAL, {{0, 2}}}}, |
| 88 | {"a", {COMMON_REGEX_MATCH_TYPE_PARTIAL, {{0, 1}}}}, |
| 89 | {"d", {}}, |
| 90 | {"bcd", {}}, |
| 91 | {"cde", {}}, |
| 92 | {"cd", {}}, |
| 93 | {"yeah ab", {COMMON_REGEX_MATCH_TYPE_PARTIAL, {{5, 7}}}}, |
| 94 | {"abbie", {}}, |
| 95 | {"", {}}, |
| 96 | } |
| 97 | }); |
no test coverage detected