Updated test-case structs to reflect the name of the new type

master
Aadhavan Srinivasan 2 weeks ago
parent f5eb9c8218
commit 80ea262064

@ -8,160 +8,166 @@ import (
var reTests = []struct { var reTests = []struct {
re string re string
str string str string
result []MatchIndex result []Group // Stores all zero-groups in the match
}{ }{
{"a", "abc", []MatchIndex{{0, 1}}}, {"a", "abc", []Group{{0, 1}}},
{"a", "bca", []MatchIndex{{2, 3}}}, {"a", "bca", []Group{{2, 3}}},
{"l", "ggllgg", []MatchIndex{{2, 3}, {3, 4}}}, {"l", "ggllgg", []Group{{2, 3}, {3, 4}}},
{"(b|c)", "abdceb", []MatchIndex{{1, 2}, {3, 4}, {5, 6}}}, {"(b|c)", "abdceb", []Group{{1, 2}, {3, 4}, {5, 6}}},
{"a+", "brerereraaaaabbbbb", []MatchIndex{{8, 13}}}, {"a+", "brerereraaaaabbbbb", []Group{{8, 13}}},
{"ab+", "qweqweqweaqweqweabbbbbr", []MatchIndex{{16, 22}}}, {"ab+", "qweqweqweaqweqweabbbbbr", []Group{{16, 22}}},
{"(b|c|A)", "ooaoobocA", []MatchIndex{{5, 6}, {7, 8}, {8, 9}}}, {"(b|c|A)", "ooaoobocA", []Group{{5, 6}, {7, 8}, {8, 9}}},
{"ab*", "a", []MatchIndex{{0, 1}}}, {"ab*", "a", []Group{{0, 1}}},
{"ab*", "abb", []MatchIndex{{0, 3}}}, {"ab*", "abb", []Group{{0, 3}}},
{"a*b", "aaab", []MatchIndex{{0, 4}}}, {"a*b", "aaab", []Group{{0, 4}}},
{"a*b", "qwqw", []MatchIndex{}}, {"a*b", "qwqw", []Group{}},
{"(abc)*", "abcabcabc", []MatchIndex{{0, 9}, {9, 9}}}, {"(abc)*", "abcabcabc", []Group{{0, 9}, {9, 9}}},
{"((abc)|(def))*", "abcdef", []MatchIndex{{0, 6}, {6, 6}}}, {"((abc)|(def))*", "abcdef", []Group{{0, 6}, {6, 6}}},
{"(abc)*|(def)*", "abcdef", []MatchIndex{{0, 3}, {3, 6}, {6, 6}}}, {"(abc)*|(def)*", "abcdef", []Group{{0, 3}, {3, 6}, {6, 6}}},
{"b*a*a", "bba", []MatchIndex{{0, 3}}}, {"b*a*a", "bba", []Group{{0, 3}}},
{"(ab)+", "abcabddd", []MatchIndex{{0, 2}, {3, 5}}}, {"(ab)+", "abcabddd", []Group{{0, 2}, {3, 5}}},
{"a(b(c|d)*)*", "abccbd", []MatchIndex{{0, 6}}}, {"a(b(c|d)*)*", "abccbd", []Group{{0, 6}}},
{"a(b|c)*d+", "abccdd", []MatchIndex{{0, 6}}}, {"a(b|c)*d+", "abccdd", []Group{{0, 6}}},
{"a*", "", []MatchIndex{{0, 0}}}, {"a*", "", []Group{{0, 0}}},
{"a|b", "c", []MatchIndex{}}, {"a|b", "c", []Group{}},
{"(a|b)*c", "aabbc", []MatchIndex{{0, 5}}}, {"(a|b)*c", "aabbc", []Group{{0, 5}}},
{"a(b|b)", "ab", []MatchIndex{{0, 2}}}, {"a(b|b)", "ab", []Group{{0, 2}}},
{"a*", "aaaaaaaa", []MatchIndex{{0, 8}, {8, 8}}}, {"a*", "aaaaaaaa", []Group{{0, 8}, {8, 8}}},
{"ab?", "ab", []MatchIndex{{0, 2}}}, {"ab?", "ab", []Group{{0, 2}}},
{"a?b", "ab", []MatchIndex{{0, 2}}}, {"a?b", "ab", []Group{{0, 2}}},
{"a?", "", []MatchIndex{{0, 0}}}, {"a?", "", []Group{{0, 0}}},
{"a?b?c", "a", []MatchIndex{}}, {"a?b?c", "a", []Group{}},
{"a?b?c?", "ab", []MatchIndex{{0, 2}, {2, 2}}}, {"a?b?c?", "ab", []Group{{0, 2}, {2, 2}}},
{"a?b?c?", "ac", []MatchIndex{{0, 2}, {2, 2}}}, {"a?b?c?", "ac", []Group{{0, 2}, {2, 2}}},
{"a?b?c", "abc", []MatchIndex{{0, 3}}}, {"a?b?c", "abc", []Group{{0, 3}}},
{"a?b?c", "acb", []MatchIndex{{0, 2}}}, {"a?b?c", "acb", []Group{{0, 2}}},
{"[abc]", "defadefbdefce", []MatchIndex{{3, 4}, {7, 8}, {11, 12}}}, {"[abc]", "defadefbdefce", []Group{{3, 4}, {7, 8}, {11, 12}}},
{"[ab]c", "ab", []MatchIndex{}}, {"[ab]c", "ab", []Group{}},
{"g[ab]c", "gac", []MatchIndex{{0, 3}}}, {"g[ab]c", "gac", []Group{{0, 3}}},
{"g[ab]c", "gbc", []MatchIndex{{0, 3}}}, {"g[ab]c", "gbc", []Group{{0, 3}}},
{"g[ab]c", "gc", []MatchIndex{}}, {"g[ab]c", "gc", []Group{}},
{"g[ab]c", "gfc", []MatchIndex{}}, {"g[ab]c", "gfc", []Group{}},
{"[ab]*", "aabbbabaababab", []MatchIndex{{0, 14}, {14, 14}}}, {"[ab]*", "aabbbabaababab", []Group{{0, 14}, {14, 14}}},
{"[ab]+", "aabbbablaababab", []MatchIndex{{0, 7}, {8, 15}}}, {"[ab]+", "aabbbablaababab", []Group{{0, 7}, {8, 15}}},
{"[Ff]r[Uu]it", "fruit", []MatchIndex{{0, 5}}}, {"[Ff]r[Uu]it", "fruit", []Group{{0, 5}}},
{"[Ff]r[Uu]it", "FrUit", []MatchIndex{{0, 5}}}, {"[Ff]r[Uu]it", "FrUit", []Group{{0, 5}}},
{"[Ff]r[Uu|]it", "Fr|it", []MatchIndex{{0, 5}}}, {"[Ff]r[Uu|]it", "Fr|it", []Group{{0, 5}}},
{"[Ff]r([Uu]|[pP])it", "Frpit", []MatchIndex{{0, 5}}}, {"[Ff]r([Uu]|[pP])it", "Frpit", []Group{{0, 5}}},
{"[Ff]r[Uu]|[pP]it", "Frpit", []MatchIndex{{2, 5}}}, {"[Ff]r[Uu]|[pP]it", "Frpit", []Group{{2, 5}}},
{"[a-zA-Z]+", "Hello, how is it going?", []MatchIndex{{0, 5}, {7, 10}, {11, 13}, {14, 16}, {17, 22}}}, {"[a-zA-Z]+", "Hello, how is it going?", []Group{{0, 5}, {7, 10}, {11, 13}, {14, 16}, {17, 22}}},
{".+", "Hello, how is it going?", []MatchIndex{{0, 23}}}, {".+", "Hello, how is it going?", []Group{{0, 23}}},
{"a.", "a ", []MatchIndex{{0, 2}}}, {"a.", "a ", []Group{{0, 2}}},
{"a.b", "a/b", []MatchIndex{{0, 3}}}, {"a.b", "a/b", []Group{{0, 3}}},
{".", "a ", []MatchIndex{{0, 1}, {1, 2}}}, {".", "a ", []Group{{0, 1}, {1, 2}}},
{"a.", "a ", []MatchIndex{{0, 2}}}, {"a.", "a ", []Group{{0, 2}}},
{".+b", "abc", []MatchIndex{{0, 2}}}, {".+b", "abc", []Group{{0, 2}}},
{`\d`, "1a0a3s'''34343s", []MatchIndex{{0, 1}, {2, 3}, {4, 5}, {9, 10}, {10, 11}, {11, 12}, {12, 13}, {13, 14}}}, {`\d`, "1a0a3s'''34343s", []Group{{0, 1}, {2, 3}, {4, 5}, {9, 10}, {10, 11}, {11, 12}, {12, 13}, {13, 14}}},
{`\\`, `a\b\c\qwe\`, []MatchIndex{{1, 2}, {3, 4}, {5, 6}, {9, 10}}}, {`\\`, `a\b\c\qwe\`, []Group{{1, 2}, {3, 4}, {5, 6}, {9, 10}}},
{`\W`, `"Hello", he said. How are you doing?`, []MatchIndex{{0, 1}, {6, 7}, {7, 8}, {8, 9}, {11, 12}, {16, 17}, {17, 18}, {21, 22}, {25, 26}, {29, 30}, {35, 36}}}, {`\W`, `"Hello", he said. How are you doing?`, []Group{{0, 1}, {6, 7}, {7, 8}, {8, 9}, {11, 12}, {16, 17}, {17, 18}, {21, 22}, {25, 26}, {29, 30}, {35, 36}}},
{`\w`, ";';';';';'qwe12", []MatchIndex{{10, 11}, {11, 12}, {12, 13}, {13, 14}, {14, 15}}}, {`\w`, ";';';';';'qwe12", []Group{{10, 11}, {11, 12}, {12, 13}, {13, 14}, {14, 15}}},
{`\s`, "a b c d", []MatchIndex{{1, 2}, {3, 4}, {5, 6}, {6, 7}}}, {`\s`, "a b c d", []Group{{1, 2}, {3, 4}, {5, 6}, {6, 7}}},
{`\<`, "<HTML><body>", []MatchIndex{{0, 1}, {6, 7}}}, {`\<`, "<HTML><body>", []Group{{0, 1}, {6, 7}}},
{`\(.+\)`, "Not (paranthesized), (so) is (this) not", []MatchIndex{{4, 35}}}, {`\(.+\)`, "Not (paranthesized), (so) is (this) not", []Group{{4, 35}}},
{"[^abc]+", "qarbtopsaplpclkpasdmb prejip0r,p", []MatchIndex{{0, 1}, {2, 3}, {4, 8}, {9, 12}, {13, 16}, {17, 20}, {21, 32}}}, {"[^abc]+", "qarbtopsaplpclkpasdmb prejip0r,p", []Group{{0, 1}, {2, 3}, {4, 8}, {9, 12}, {13, 16}, {17, 20}, {21, 32}}},
{"[^a]+", "qqqaq", []MatchIndex{{0, 3}, {4, 5}}}, {"[^a]+", "qqqaq", []Group{{0, 3}, {4, 5}}},
{"[^0-9]+", "a1b2c3dd", []MatchIndex{{0, 1}, {2, 3}, {4, 5}, {6, 8}}}, {"[^0-9]+", "a1b2c3dd", []Group{{0, 1}, {2, 3}, {4, 5}, {6, 8}}},
{"[^abc]+", "ababababbababaccacacacaca", []MatchIndex{}}, {"[^abc]+", "ababababbababaccacacacaca", []Group{}},
{`\[`, "a[b[c[]]]", []MatchIndex{{1, 2}, {3, 4}, {5, 6}}}, {`\[`, "a[b[c[]]]", []Group{{1, 2}, {3, 4}, {5, 6}}},
{`\([^)]+\)`, "Not (paranthesized), (so) is (this) not", []MatchIndex{{4, 19}, {21, 25}, {29, 35}}}, {`\([^)]+\)`, "Not (paranthesized), (so) is (this) not", []Group{{4, 19}, {21, 25}, {29, 35}}},
{"^ab", "ab bab", []MatchIndex{{0, 2}}}, {"^ab", "ab bab", []Group{{0, 2}}},
{"^aaaa^", "aaaaaaaa", []MatchIndex{}}, {"^aaaa^", "aaaaaaaa", []Group{}},
{"^([bB][Gg])", "bG", []MatchIndex{{0, 2}}}, {"^([bB][Gg])", "bG", []Group{{0, 2}}},
{"b$", "ba", []MatchIndex{}}, {"b$", "ba", []Group{}},
{"(boy|girl)$", "girlf", []MatchIndex{}}, {"(boy|girl)$", "girlf", []Group{}},
{`\bint\b`, "print int integer", []MatchIndex{{6, 9}}}, {`\bint\b`, "print int integer", []Group{{6, 9}}},
{`int\b`, "ints", []MatchIndex{}}, {`int\b`, "ints", []Group{}},
{`int(\b|a)`, "inta", []MatchIndex{{0, 4}}}, {`int(\b|a)`, "inta", []Group{{0, 4}}},
{`\b\d+\b`, "511 a3 43", []MatchIndex{{0, 3}, {7, 9}}}, {`\b\d+\b`, "511 a3 43", []Group{{0, 3}, {7, 9}}},
{`\Bint\B`, "prints int integer print", []MatchIndex{{2, 5}}}, {`\Bint\B`, "prints int integer print", []Group{{2, 5}}},
{`^`, "5^3^2", []MatchIndex{{0, 0}}}, {`^`, "5^3^2", []Group{{0, 0}}},
{`\^`, "5^3^2", []MatchIndex{{1, 2}, {3, 4}}}, {`\^`, "5^3^2", []Group{{1, 2}, {3, 4}}},
{`pool$`, "pool carpool", []MatchIndex{{8, 12}}}, {`pool$`, "pool carpool", []Group{{8, 12}}},
{`^int$`, "print int integer", []MatchIndex{}}, {`^int$`, "print int integer", []Group{}},
{`^int$`, "int", []MatchIndex{{0, 3}}}, {`^int$`, "int", []Group{{0, 3}}},
{`b*`, "aaaaaaaaaaqweqwe", []MatchIndex{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14}, {15, 15}, {16, 16}}}, {`b*`, "aaaaaaaaaaqweqwe", []Group{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14}, {15, 15}, {16, 16}}},
{"a{4}", "aabaaa", []MatchIndex{}}, {"a{4}", "aabaaa", []Group{}},
{"ab{5}", "abbbbbab", []MatchIndex{{0, 6}}}, {"ab{5}", "abbbbbab", []Group{{0, 6}}},
{"(a|b){3,4}", "aba", []MatchIndex{{0, 3}}}, {"(a|b){3,4}", "aba", []Group{{0, 3}}},
{"(a|b){3,4}", "ababaa", []MatchIndex{{0, 4}}}, {"(a|b){3,4}", "ababaa", []Group{{0, 4}}},
{"(bc){5,}", "bcbcbcbcbcbcbcbc", []MatchIndex{{0, 16}}}, {"(bc){5,}", "bcbcbcbcbcbcbcbc", []Group{{0, 16}}},
{`\d{3,4}`, "1209", []MatchIndex{{0, 4}}}, {`\d{3,4}`, "1209", []Group{{0, 4}}},
{`\d{3,4}`, "109", []MatchIndex{{0, 3}}}, {`\d{3,4}`, "109", []Group{{0, 3}}},
{`\d{3,4}`, "5", []MatchIndex{}}, {`\d{3,4}`, "5", []Group{}},
{`\d{3,4}`, "123135", []MatchIndex{{0, 4}}}, {`\d{3,4}`, "123135", []Group{{0, 4}}},
{`\d{3,4}`, "89a-0", []MatchIndex{}}, {`\d{3,4}`, "89a-0", []Group{}},
{`\d{3,4}`, "ababab555", []MatchIndex{{6, 9}}}, {`\d{3,4}`, "ababab555", []Group{{6, 9}}},
{`\bpaint\b`, "paints", []MatchIndex{}}, {`\bpaint\b`, "paints", []Group{}},
{`\b\w{5}\b`, "paint", []MatchIndex{{0, 5}}}, {`\b\w{5}\b`, "paint", []Group{{0, 5}}},
// Unicode tests // Unicode tests
{`.+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []MatchIndex{{0, 25}}}, {`.+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}},
{`a.b`, "a²b", []MatchIndex{{0, 3}}}, {`a.b`, "a²b", []Group{{0, 3}}},
{`[^a]+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []MatchIndex{{0, 25}}}, {`[^a]+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}},
// Fun experiment - AI-generated tests // Fun experiment - AI-generated tests
{"(abc|def|ghi)", "abcdefg", []MatchIndex{{0, 3}, {3, 6}}}, {"(abc|def|ghi)", "abcdefg", []Group{{0, 3}, {3, 6}}},
{"a(b|c)d", "abcd", []MatchIndex{}}, {"a(b|c)d", "abcd", []Group{}},
{"a(b|c)*d", "abcbcd", []MatchIndex{{0, 6}}}, {"a(b|c)*d", "abcbcd", []Group{{0, 6}}},
{"a(b|c)+d", "abcbcd", []MatchIndex{{0, 6}}}, {"a(b|c)+d", "abcbcd", []Group{{0, 6}}},
{"a(b|c)?d", "abd", []MatchIndex{{0, 3}}}, {"a(b|c)?d", "abd", []Group{{0, 3}}},
{".+", "hello world", []MatchIndex{{0, 11}}}, {".+", "hello world", []Group{{0, 11}}},
{"a.b", "aXb", []MatchIndex{{0, 3}}}, {"a.b", "aXb", []Group{{0, 3}}},
{"a.*b", "aXb", []MatchIndex{{0, 3}}}, {"a.*b", "aXb", []Group{{0, 3}}},
{"a.{2,3}b", "aXXb", []MatchIndex{{0, 4}}}, {"a.{2,3}b", "aXXb", []Group{{0, 4}}},
{"a.{2,}b", "aXXXb", []MatchIndex{{0, 5}}}, {"a.{2,}b", "aXXXb", []Group{{0, 5}}},
{"a.{0,3}b", "ab", []MatchIndex{{0, 2}}}, {"a.{0,3}b", "ab", []Group{{0, 2}}},
{"[abc]+", "abcabc", []MatchIndex{{0, 6}}}, {"[abc]+", "abcabc", []Group{{0, 6}}},
{"[a-zA-Z]+", "HelloWorld", []MatchIndex{{0, 10}}}, {"[a-zA-Z]+", "HelloWorld", []Group{{0, 10}}},
{"[^abc]+", "defghi", []MatchIndex{{0, 6}}}, {"[^abc]+", "defghi", []Group{{0, 6}}},
{"^hello", "hello world", []MatchIndex{{0, 5}}}, {"^hello", "hello world", []Group{{0, 5}}},
{"world$", "hello world", []MatchIndex{{6, 11}}}, {"world$", "hello world", []Group{{6, 11}}},
{`\bhello\b`, "hello world", []MatchIndex{{0, 5}}}, {`\bhello\b`, "hello world", []Group{{0, 5}}},
{`\Bhello\B`, "hello world", []MatchIndex{}}, {`\Bhello\B`, "hello world", []Group{}},
{"(hello|world)", "hello world", []MatchIndex{{0, 5}, {6, 11}}}, {"(hello|world)", "hello world", []Group{{0, 5}, {6, 11}}},
{"(hello|world)+", "hello world", []MatchIndex{{0, 5}, {6, 11}}}, {"(hello|world)+", "hello world", []Group{{0, 5}, {6, 11}}},
{"(hello|world)*", "hello world", []MatchIndex{{0, 5}, {5, 5}, {6, 11}, {11, 11}}}, {"(hello|world)*", "hello world", []Group{{0, 5}, {5, 5}, {6, 11}, {11, 11}}},
{"(hello|world)?", "hello world", []MatchIndex{{0, 5}, {5, 5}, {6, 11}, {11, 11}}}, {"(hello|world)?", "hello world", []Group{{0, 5}, {5, 5}, {6, 11}, {11, 11}}},
{"ú.+ï", "úïäö´«åæïëòöê»éãçâï«úïòíñ", []MatchIndex{{0, 22}}}, {"ú.+ï", "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 22}}},
{"(?=hello)", "hello world", []MatchIndex{{0, 0}}}, {"(?=hello)", "hello world", []Group{{0, 0}}},
{"(?!hello)", "hello world", []MatchIndex{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}}, {"(?!hello)", "hello world", []Group{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}},
{"(?<=hello)", "hello world", []MatchIndex{{5, 5}}}, {"(?<=hello)", "hello world", []Group{{5, 5}}},
{"(?<!hello)", "hello world", []MatchIndex{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}}, {"(?<!hello)", "hello world", []Group{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}}},
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "40", []MatchIndex{{0, 2}}}, {"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "40", []Group{{0, 2}}},
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "040", []MatchIndex{}}, {"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "040", []Group{}},
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "400", []MatchIndex{{0, 3}}}, {"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "400", []Group{{0, 3}}},
{"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "4000", []MatchIndex{}}, {"^((3[7-9])|([4-9][0-9])|([1-9][0-9][0-9])|(1000))$", "4000", []Group{}},
// Todo - add lookaround tests // Todo - add lookaround tests
{"(?<=f)f+(?=f)", "fffff", []MatchIndex{{1, 4}}}, // Todo - add numeric range tests
{"(?<=f)f+(?=f)", "fffffa", []MatchIndex{{1, 4}}}, // Todo - add capturing group tests
{"(?<=f)f+(?=f)", "fffff", []Group{{1, 4}}},
{"(?<=f)f+(?=f)", "fffffa", []Group{{1, 4}}},
} }
func TestFindAllMatches(t *testing.T) { func TestFindAllMatches(t *testing.T) {
for _, test := range reTests { for _, test := range reTests {
t.Run(test.re+" "+test.str, func(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) {
re_postfix := shuntingYard(test.re) re_postfix := shuntingYard(test.re)
startState := thompson(re_postfix) startState, numGroups := thompson(re_postfix)
matchIndices := findAllMatches(startState, []rune(test.str)) matchIndices := findAllMatches(startState, []rune(test.str), numGroups)
if !slices.Equal(test.result, matchIndices) { zeroGroups := make([]Group, len(matchIndices))
t.Errorf("Wanted %v Got %v\n", test.result, matchIndices) for i, m := range matchIndices {
zeroGroups[i] = m[0]
}
if !slices.Equal(test.result, zeroGroups) {
t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups)
} }
}) })
} }

Loading…
Cancel
Save