You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
var reTests = []struct {
|
|
re string
|
|
str string
|
|
result []matchIndex
|
|
}{
|
|
{"a", "abc", []matchIndex{{0, 1}}},
|
|
{"a", "bca", []matchIndex{{2, 3}}},
|
|
{"l", "ggllgg", []matchIndex{{2, 3}, {3, 4}}},
|
|
{"(b|c)", "abdceb", []matchIndex{{1, 2}, {3, 4}, {5, 6}}},
|
|
{"a+", "brerereraaaaabbbbb", []matchIndex{{8, 13}}},
|
|
{"ab+", "qweqweqweaqweqweabbbbbr", []matchIndex{{16, 22}}},
|
|
{"(b|c|A)", "ooaoobocA", []matchIndex{{5, 6}, {7, 8}, {8, 9}}},
|
|
{"ab*", "a", []matchIndex{{0, 1}}},
|
|
{"ab*", "abb", []matchIndex{{0, 3}}},
|
|
{"a*b", "aaab", []matchIndex{{0, 4}}},
|
|
{"a*b", "qwqw", []matchIndex{}},
|
|
{"(abc)*", "abcabcabc", []matchIndex{{0, 9}}},
|
|
{"((abc)|(def))*", "abcdef", []matchIndex{{0, 6}}},
|
|
{"(abc)*|(def)*", "abcdef", []matchIndex{{0, 3}, {3, 3}, {3, 6}, {6, 6}}},
|
|
{"b*a*a", "bba", []matchIndex{{0, 3}}},
|
|
{"(ab)+", "abcabddd", []matchIndex{{0, 2}, {3, 5}}},
|
|
{"a(b(c|d)*)*", "abccbd", []matchIndex{{0, 6}}},
|
|
{"a(b|c)*d+", "abccdd", []matchIndex{{0, 6}}},
|
|
{"a*", "", []matchIndex{{0, 0}}},
|
|
{"a|b", "c", []matchIndex{}},
|
|
{"(a|b)*c", "aabbc", []matchIndex{{0, 5}}},
|
|
{"a*", "aaaaaaaa", []matchIndex{{0, 8}, {8, 8}}},
|
|
{"ab?", "ab", []matchIndex{{0, 2}}},
|
|
{"a?b", "ab", []matchIndex{{0, 2}}},
|
|
{"a?", "", []matchIndex{{0, 0}}},
|
|
{"a?b?c", "a", []matchIndex{}},
|
|
{"a?b?c?", "ab", []matchIndex{{0, 2}}},
|
|
{"a?b?c?", "ac", []matchIndex{{0, 2}}},
|
|
{"a?b?c", "abc", []matchIndex{{0, 3}}},
|
|
{"a?b?c", "acb", []matchIndex{{0, 2}}},
|
|
}
|
|
|
|
func TestFindAllMatches(t *testing.T) {
|
|
for _, test := range reTests {
|
|
t.Run(test.re+" "+test.str, func(t *testing.T) {
|
|
re_postfix := shuntingYard(test.re)
|
|
startState := thompson(re_postfix)
|
|
matchIndices := findAllMatches(startState, test.str)
|
|
if !slices.Equal(test.result, matchIndices) {
|
|
t.Errorf("Wanted %v Got %v\n", test.result, matchIndices)
|
|
}
|
|
})
|
|
}
|
|
}
|