From 935759ed9f3dcd1fe2308a21eefc57276b033455 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Oct 2024 14:40:01 -0400 Subject: [PATCH] Added testing file --- re_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 re_test.go diff --git a/re_test.go b/re_test.go new file mode 100644 index 0000000..9ee3ff1 --- /dev/null +++ b/re_test.go @@ -0,0 +1,31 @@ +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}}}, +} + +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) + } + }) + } +}