From 84cccc73ec3356ac6e194ce4bed19541999d36e4 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 11 Dec 2024 00:16:35 -0500 Subject: [PATCH] Added grouping tests --- re_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/re_test.go b/re_test.go index ef8c2d5..5d205c4 100644 --- a/re_test.go +++ b/re_test.go @@ -156,6 +156,28 @@ var reTests = []struct { {"(?<=f)f+(?=f)", "fffffa", []Group{{1, 4}}}, } +var groupTests = []struct { + re string + str string + result []Match +}{ + {"(a)(b)", "ab", []Match{[]Group{{0, 2}, {0, 1}, {1, 2}}}}, + {"((a))(b)", "ab", []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {1, 2}}}}, + {"(0)", "ab", []Match{[]Group{}}}, + {"(a)b", "ab", []Match{[]Group{{0, 2}, {0, 1}}}}, + {"a(b)", "ab", []Match{[]Group{{0, 2}, {1, 2}}}}, + {"(a|b)", "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 2}, {1, 2}}}}, + {"(a)|(b)", "ab", []Match{[]Group{{0, 1}, {0, 1}, {-1, -1}}, []Group{{1, 2}, {-1, -1}, {1, 2}}}}, + {"(a+)(a)", "aaaa", []Match{[]Group{{0, 4}, {0, 3}, {3, 4}}}}, + {"(a+)|(a)", "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}}, + {"(a+)(aa)", "aaaa", []Match{[]Group{{0, 4}, {0, 2}, {2, 4}}}}, + {"(aaaa)|(aaaa)", "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}}, + {"(aaa)|(aaaa)", "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}}, + {"(aaa)|(aaaa)", "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}}, + {"(aaaa)|(aaa)", "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}}, + {"(a)|(aa)", "aa", []Match{[]Group{{0, 2}, {-1, -1}, {0, 2}}}}, +} + func TestFindAllMatches(t *testing.T) { for _, test := range reTests { t.Run(test.re+" "+test.str, func(t *testing.T) { @@ -172,3 +194,22 @@ func TestFindAllMatches(t *testing.T) { }) } } + +func TestFindAllGroups(t *testing.T) { + for _, test := range groupTests { + t.Run(test.re+" "+test.str, func(t *testing.T) { + re_postfix := shuntingYard(test.re) + startState, numGroups := thompson(re_postfix) + matchIndices := findAllMatches(startState, []rune(test.str), numGroups) + for i := range matchIndices { + for j := range matchIndices[i] { + if matchIndices[i][j].isValid() { + if test.result[i][j] != matchIndices[i][j] { + t.Errorf("Wanted %v Got %v\n", test.result, matchIndices) + } + } + } + } + }) + } +}