Started adding tests from Python's RE test suite

master
Aadhavan Srinivasan 2 days ago
parent 099612ae7f
commit 9dc4fd4595

@ -110,6 +110,9 @@ var reTests = []struct {
{`\d{3,4}`, "ababab555", []Group{{6, 9}}}, {`\d{3,4}`, "ababab555", []Group{{6, 9}}},
{`\bpaint\b`, "paints", []Group{}}, {`\bpaint\b`, "paints", []Group{}},
{`\b\w{5}\b`, "paint", []Group{{0, 5}}}, {`\b\w{5}\b`, "paint", []Group{{0, 5}}},
{`[^\w]`, "abcdef1230[]qq';;'", []Group{{10, 11}, {11, 12}, {14, 15}, {15, 16}, {16, 17}, {17, 18}}},
{`[^\W]`, "abcdef1230[]qq';;'", []Group{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {12, 13}, {13, 14}}},
{`[\[\]]`, "a[b[l]]", []Group{{1, 2}, {3, 4}, {5, 6}, {6, 7}}},
// Unicode tests // Unicode tests
{`.+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}}, {`.+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}},
@ -156,6 +159,24 @@ var reTests = []struct {
{"(?<=f)f+(?=f)", "fffff", []Group{{1, 4}}}, {"(?<=f)f+(?=f)", "fffff", []Group{{1, 4}}},
{"(?<=f)f+(?=f)", "fffffa", []Group{{1, 4}}}, {"(?<=f)f+(?=f)", "fffffa", []Group{{1, 4}}},
// Test cases from Python's RE test suite
{`[\1]`, "\x01", []Group{{0, 1}}},
{`\0`, "\x00", []Group{{0, 1}}},
{`[\0a]`, "\x00", []Group{{0, 1}}},
{`[\0a]`, "\x00", []Group{{0, 1}}},
{`[a\0]`, "\x00", []Group{{0, 1}}},
{`[^a\0]`, "\x00", []Group{}},
{`\a[\b]\f\n\r\t\v`, "\a\b\f\n\r\t\v", []Group{{0, 7}}},
{`[\a][\b][\f][\n][\r][\t][\v]`, "\a\b\f\n\r\t\v", []Group{{0, 7}}},
{`\u`, "", nil},
{`\xff`, "ÿ", []Group{{0, 1}}},
{`\x00ffffffffffffff`, "\xff", []Group{}},
{`\x00f`, "\x0f", []Group{}},
{`\x00fe`, "\xfe", []Group{}},
{`^\w+=(\\[\000-\277]|[^\n\\])*`, "SRC=eval.c g.c blah blah blah \\\\\n\tapes.c", []Group{{0, 32}}},
// Todo - add numeric range tests // Todo - add numeric range tests
} }
@ -183,6 +204,7 @@ var groupTests = []struct {
{"(a?)a?", "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 1}, {1, 1}}, []Group{{2, 2}, {2, 2}}}}, {"(a?)a?", "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 1}, {1, 1}}, []Group{{2, 2}, {2, 2}}}},
{"(a?)a?", "aa", []Match{[]Group{{0, 2}, {0, 1}}, []Group{{2, 2}, {2, 2}}}}, {"(a?)a?", "aa", []Match{[]Group{{0, 2}, {0, 1}}, []Group{{2, 2}, {2, 2}}}},
{"a((b.d){3})", "abfdbhdbid", []Match{[]Group{{0, 10}, {1, 10}, {7, 10}}}}, {"a((b.d){3})", "abfdbhdbid", []Match{[]Group{{0, 10}, {1, 10}, {7, 10}}}},
{`(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\071`, `abcdefghijkl9`, []Match{[]Group{{0, 13}, {0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {10, 11}, {11, 12}}}},
} }
func TestFindAllMatches(t *testing.T) { func TestFindAllMatches(t *testing.T) {
@ -190,8 +212,10 @@ func TestFindAllMatches(t *testing.T) {
t.Run(test.re+" "+test.str, func(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re) regComp, err := Compile(test.re)
if err != nil { if err != nil {
if test.result != nil {
panic(err) panic(err)
} }
} else {
matchIndices := FindAllMatches(regComp, test.str) matchIndices := FindAllMatches(regComp, test.str)
zeroGroups := make([]Group, len(matchIndices)) zeroGroups := make([]Group, len(matchIndices))
for i, m := range matchIndices { for i, m := range matchIndices {
@ -200,6 +224,7 @@ func TestFindAllMatches(t *testing.T) {
if !slices.Equal(test.result, zeroGroups) { if !slices.Equal(test.result, zeroGroups) {
t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups) t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups)
} }
}
}) })
} }
} }
@ -209,8 +234,10 @@ func TestFindString(t *testing.T) {
t.Run(test.re+" "+test.str, func(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re) regComp, err := Compile(test.re)
if err != nil { if err != nil {
if test.result != nil {
panic(err) panic(err)
} }
} else {
foundString := FindString(regComp, test.str) foundString := FindString(regComp, test.str)
if len(test.result) == 0 { if len(test.result) == 0 {
if foundString != "" { if foundString != "" {
@ -218,10 +245,11 @@ func TestFindString(t *testing.T) {
} }
} else { } else {
expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx] expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx]
if foundString != foundString { if foundString != expectedString {
t.Errorf("Wanted %v Got %v\n", expectedString, foundString) t.Errorf("Wanted %v Got %v\n", expectedString, foundString)
} }
} }
}
}) })
} }
} }
@ -231,8 +259,10 @@ func TestFindAllGroups(t *testing.T) {
t.Run(test.re+" "+test.str, func(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re) regComp, err := Compile(test.re)
if err != nil { if err != nil {
if test.result != nil {
panic(err) panic(err)
} }
}
matchIndices := FindAllMatches(regComp, test.str) matchIndices := FindAllMatches(regComp, test.str)
for i := range matchIndices { for i := range matchIndices {
for j := range matchIndices[i] { for j := range matchIndices[i] {

Loading…
Cancel
Save