github.com/euank/go@v0.0.0-20160829210321-495514729181/src/regexp/all_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package regexp
     6  
     7  import (
     8  	"reflect"
     9  	"regexp/syntax"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var goodRe = []string{
    15  	``,
    16  	`.`,
    17  	`^.$`,
    18  	`a`,
    19  	`a*`,
    20  	`a+`,
    21  	`a?`,
    22  	`a|b`,
    23  	`a*|b*`,
    24  	`(a*|b)(c*|d)`,
    25  	`[a-z]`,
    26  	`[a-abc-c\-\]\[]`,
    27  	`[a-z]+`,
    28  	`[abc]`,
    29  	`[^1234]`,
    30  	`[^\n]`,
    31  	`\!\\`,
    32  }
    33  
    34  type stringError struct {
    35  	re  string
    36  	err string
    37  }
    38  
    39  var badRe = []stringError{
    40  	{`*`, "missing argument to repetition operator: `*`"},
    41  	{`+`, "missing argument to repetition operator: `+`"},
    42  	{`?`, "missing argument to repetition operator: `?`"},
    43  	{`(abc`, "missing closing ): `(abc`"},
    44  	{`abc)`, "unexpected ): `abc)`"},
    45  	{`x[a-z`, "missing closing ]: `[a-z`"},
    46  	{`[z-a]`, "invalid character class range: `z-a`"},
    47  	{`abc\`, "trailing backslash at end of expression"},
    48  	{`a**`, "invalid nested repetition operator: `**`"},
    49  	{`a*+`, "invalid nested repetition operator: `*+`"},
    50  	{`\x`, "invalid escape sequence: `\\x`"},
    51  }
    52  
    53  func compileTest(t *testing.T, expr string, error string) *Regexp {
    54  	re, err := Compile(expr)
    55  	if error == "" && err != nil {
    56  		t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
    57  	}
    58  	if error != "" && err == nil {
    59  		t.Error("compiling `", expr, "`; missing error")
    60  	} else if error != "" && !strings.Contains(err.Error(), error) {
    61  		t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error)
    62  	}
    63  	return re
    64  }
    65  
    66  func TestGoodCompile(t *testing.T) {
    67  	for i := 0; i < len(goodRe); i++ {
    68  		compileTest(t, goodRe[i], "")
    69  	}
    70  }
    71  
    72  func TestBadCompile(t *testing.T) {
    73  	for i := 0; i < len(badRe); i++ {
    74  		compileTest(t, badRe[i].re, badRe[i].err)
    75  	}
    76  }
    77  
    78  func matchTest(t *testing.T, test *FindTest) {
    79  	re := compileTest(t, test.pat, "")
    80  	if re == nil {
    81  		return
    82  	}
    83  	m := re.MatchString(test.text)
    84  	if m != (len(test.matches) > 0) {
    85  		t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
    86  	}
    87  	// now try bytes
    88  	m = re.Match([]byte(test.text))
    89  	if m != (len(test.matches) > 0) {
    90  		t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
    91  	}
    92  }
    93  
    94  func TestMatch(t *testing.T) {
    95  	for _, test := range findTests {
    96  		matchTest(t, &test)
    97  	}
    98  }
    99  
   100  func matchFunctionTest(t *testing.T, test *FindTest) {
   101  	m, err := MatchString(test.pat, test.text)
   102  	if err == nil {
   103  		return
   104  	}
   105  	if m != (len(test.matches) > 0) {
   106  		t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
   107  	}
   108  }
   109  
   110  func TestMatchFunction(t *testing.T) {
   111  	for _, test := range findTests {
   112  		matchFunctionTest(t, &test)
   113  	}
   114  }
   115  
   116  func copyMatchTest(t *testing.T, test *FindTest) {
   117  	re := compileTest(t, test.pat, "")
   118  	if re == nil {
   119  		return
   120  	}
   121  	m1 := re.MatchString(test.text)
   122  	m2 := re.Copy().MatchString(test.text)
   123  	if m1 != m2 {
   124  		t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t",
   125  			test, m1, m2, len(test.matches) > 0)
   126  	}
   127  }
   128  
   129  func TestCopyMatch(t *testing.T) {
   130  	for _, test := range findTests {
   131  		copyMatchTest(t, &test)
   132  	}
   133  }
   134  
   135  type ReplaceTest struct {
   136  	pattern, replacement, input, output string
   137  }
   138  
   139  var replaceTests = []ReplaceTest{
   140  	// Test empty input and/or replacement, with pattern that matches the empty string.
   141  	{"", "", "", ""},
   142  	{"", "x", "", "x"},
   143  	{"", "", "abc", "abc"},
   144  	{"", "x", "abc", "xaxbxcx"},
   145  
   146  	// Test empty input and/or replacement, with pattern that does not match the empty string.
   147  	{"b", "", "", ""},
   148  	{"b", "x", "", ""},
   149  	{"b", "", "abc", "ac"},
   150  	{"b", "x", "abc", "axc"},
   151  	{"y", "", "", ""},
   152  	{"y", "x", "", ""},
   153  	{"y", "", "abc", "abc"},
   154  	{"y", "x", "abc", "abc"},
   155  
   156  	// Multibyte characters -- verify that we don't try to match in the middle
   157  	// of a character.
   158  	{"[a-c]*", "x", "\u65e5", "x\u65e5x"},
   159  	{"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
   160  
   161  	// Start and end of a string.
   162  	{"^[a-c]*", "x", "abcdabc", "xdabc"},
   163  	{"[a-c]*$", "x", "abcdabc", "abcdx"},
   164  	{"^[a-c]*$", "x", "abcdabc", "abcdabc"},
   165  	{"^[a-c]*", "x", "abc", "x"},
   166  	{"[a-c]*$", "x", "abc", "x"},
   167  	{"^[a-c]*$", "x", "abc", "x"},
   168  	{"^[a-c]*", "x", "dabce", "xdabce"},
   169  	{"[a-c]*$", "x", "dabce", "dabcex"},
   170  	{"^[a-c]*$", "x", "dabce", "dabce"},
   171  	{"^[a-c]*", "x", "", "x"},
   172  	{"[a-c]*$", "x", "", "x"},
   173  	{"^[a-c]*$", "x", "", "x"},
   174  
   175  	{"^[a-c]+", "x", "abcdabc", "xdabc"},
   176  	{"[a-c]+$", "x", "abcdabc", "abcdx"},
   177  	{"^[a-c]+$", "x", "abcdabc", "abcdabc"},
   178  	{"^[a-c]+", "x", "abc", "x"},
   179  	{"[a-c]+$", "x", "abc", "x"},
   180  	{"^[a-c]+$", "x", "abc", "x"},
   181  	{"^[a-c]+", "x", "dabce", "dabce"},
   182  	{"[a-c]+$", "x", "dabce", "dabce"},
   183  	{"^[a-c]+$", "x", "dabce", "dabce"},
   184  	{"^[a-c]+", "x", "", ""},
   185  	{"[a-c]+$", "x", "", ""},
   186  	{"^[a-c]+$", "x", "", ""},
   187  
   188  	// Other cases.
   189  	{"abc", "def", "abcdefg", "defdefg"},
   190  	{"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
   191  	{"abc", "", "abcdabc", "d"},
   192  	{"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
   193  	{"abc", "d", "", ""},
   194  	{"abc", "d", "abc", "d"},
   195  	{".+", "x", "abc", "x"},
   196  	{"[a-c]*", "x", "def", "xdxexfx"},
   197  	{"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
   198  	{"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
   199  
   200  	// Substitutions
   201  	{"a+", "($0)", "banana", "b(a)n(a)n(a)"},
   202  	{"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
   203  	{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
   204  	{"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
   205  	{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
   206  	{"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
   207  	{"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
   208  	{"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
   209  	{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
   210  	{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
   211  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
   212  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
   213  	{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
   214  	{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
   215  	{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
   216  	{"a+", "${oops", "aaa", "${oops"},
   217  	{"a+", "$$", "aaa", "$"},
   218  	{"a+", "$", "aaa", "$"},
   219  
   220  	// Substitution when subexpression isn't found
   221  	{"(x)?", "$1", "123", "123"},
   222  	{"abc", "$1", "123", "123"},
   223  
   224  	// Substitutions involving a (x){0}
   225  	{"(a)(b){0}(c)", ".$1|$3.", "xacxacx", "x.a|c.x.a|c.x"},
   226  	{"(a)(((b))){0}c", ".$1.", "xacxacx", "x.a.x.a.x"},
   227  	{"((a(b){0}){3}){5}(h)", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
   228  	{"((a(b){0}){3}){5}h", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
   229  }
   230  
   231  var replaceLiteralTests = []ReplaceTest{
   232  	// Substitutions
   233  	{"a+", "($0)", "banana", "b($0)n($0)n($0)"},
   234  	{"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
   235  	{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
   236  	{"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
   237  	{"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
   238  	{"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
   239  	{"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
   240  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
   241  	{"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
   242  	{"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
   243  	{"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
   244  	{"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
   245  	{"a+", "${oops", "aaa", "${oops"},
   246  	{"a+", "$$", "aaa", "$$"},
   247  	{"a+", "$", "aaa", "$"},
   248  }
   249  
   250  type ReplaceFuncTest struct {
   251  	pattern       string
   252  	replacement   func(string) string
   253  	input, output string
   254  }
   255  
   256  var replaceFuncTests = []ReplaceFuncTest{
   257  	{"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
   258  	{"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
   259  	{"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
   260  }
   261  
   262  func TestReplaceAll(t *testing.T) {
   263  	for _, tc := range replaceTests {
   264  		re, err := Compile(tc.pattern)
   265  		if err != nil {
   266  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   267  			continue
   268  		}
   269  		actual := re.ReplaceAllString(tc.input, tc.replacement)
   270  		if actual != tc.output {
   271  			t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
   272  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   273  		}
   274  		// now try bytes
   275  		actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
   276  		if actual != tc.output {
   277  			t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
   278  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   279  		}
   280  	}
   281  }
   282  
   283  func TestReplaceAllLiteral(t *testing.T) {
   284  	// Run ReplaceAll tests that do not have $ expansions.
   285  	for _, tc := range replaceTests {
   286  		if strings.Contains(tc.replacement, "$") {
   287  			continue
   288  		}
   289  		re, err := Compile(tc.pattern)
   290  		if err != nil {
   291  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   292  			continue
   293  		}
   294  		actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
   295  		if actual != tc.output {
   296  			t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
   297  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   298  		}
   299  		// now try bytes
   300  		actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
   301  		if actual != tc.output {
   302  			t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
   303  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   304  		}
   305  	}
   306  
   307  	// Run literal-specific tests.
   308  	for _, tc := range replaceLiteralTests {
   309  		re, err := Compile(tc.pattern)
   310  		if err != nil {
   311  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   312  			continue
   313  		}
   314  		actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
   315  		if actual != tc.output {
   316  			t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
   317  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   318  		}
   319  		// now try bytes
   320  		actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
   321  		if actual != tc.output {
   322  			t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
   323  				tc.pattern, tc.input, tc.replacement, actual, tc.output)
   324  		}
   325  	}
   326  }
   327  
   328  func TestReplaceAllFunc(t *testing.T) {
   329  	for _, tc := range replaceFuncTests {
   330  		re, err := Compile(tc.pattern)
   331  		if err != nil {
   332  			t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
   333  			continue
   334  		}
   335  		actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
   336  		if actual != tc.output {
   337  			t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
   338  				tc.pattern, tc.input, actual, tc.output)
   339  		}
   340  		// now try bytes
   341  		actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
   342  		if actual != tc.output {
   343  			t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
   344  				tc.pattern, tc.input, actual, tc.output)
   345  		}
   346  	}
   347  }
   348  
   349  type MetaTest struct {
   350  	pattern, output, literal string
   351  	isLiteral                bool
   352  }
   353  
   354  var metaTests = []MetaTest{
   355  	{``, ``, ``, true},
   356  	{`foo`, `foo`, `foo`, true},
   357  	{`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
   358  	{`foo.\$`, `foo\.\\\$`, `foo`, false},     // has escaped operators and real operators
   359  	{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
   360  }
   361  
   362  var literalPrefixTests = []MetaTest{
   363  	// See golang.org/issue/11175.
   364  	// output is unused.
   365  	{`^0^0$`, ``, `0`, false},
   366  	{`^0^`, ``, ``, false},
   367  	{`^0$`, ``, `0`, true},
   368  	{`$0^`, ``, ``, false},
   369  	{`$0$`, ``, ``, false},
   370  	{`^^0$$`, ``, ``, false},
   371  	{`^$^$`, ``, ``, false},
   372  	{`$$0^^`, ``, ``, false},
   373  }
   374  
   375  func TestQuoteMeta(t *testing.T) {
   376  	for _, tc := range metaTests {
   377  		// Verify that QuoteMeta returns the expected string.
   378  		quoted := QuoteMeta(tc.pattern)
   379  		if quoted != tc.output {
   380  			t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
   381  				tc.pattern, quoted, tc.output)
   382  			continue
   383  		}
   384  
   385  		// Verify that the quoted string is in fact treated as expected
   386  		// by Compile -- i.e. that it matches the original, unquoted string.
   387  		if tc.pattern != "" {
   388  			re, err := Compile(quoted)
   389  			if err != nil {
   390  				t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
   391  				continue
   392  			}
   393  			src := "abc" + tc.pattern + "def"
   394  			repl := "xyz"
   395  			replaced := re.ReplaceAllString(src, repl)
   396  			expected := "abcxyzdef"
   397  			if replaced != expected {
   398  				t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
   399  					tc.pattern, src, repl, replaced, expected)
   400  			}
   401  		}
   402  	}
   403  }
   404  
   405  func TestLiteralPrefix(t *testing.T) {
   406  	for _, tc := range append(metaTests, literalPrefixTests...) {
   407  		// Literal method needs to scan the pattern.
   408  		re := MustCompile(tc.pattern)
   409  		str, complete := re.LiteralPrefix()
   410  		if complete != tc.isLiteral {
   411  			t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
   412  		}
   413  		if str != tc.literal {
   414  			t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
   415  		}
   416  	}
   417  }
   418  
   419  type subexpCase struct {
   420  	input string
   421  	num   int
   422  	names []string
   423  }
   424  
   425  var subexpCases = []subexpCase{
   426  	{``, 0, nil},
   427  	{`.*`, 0, nil},
   428  	{`abba`, 0, nil},
   429  	{`ab(b)a`, 1, []string{"", ""}},
   430  	{`ab(.*)a`, 1, []string{"", ""}},
   431  	{`(.*)ab(.*)a`, 2, []string{"", "", ""}},
   432  	{`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
   433  	{`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
   434  	{`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
   435  	{`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
   436  	{`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
   437  }
   438  
   439  func TestSubexp(t *testing.T) {
   440  	for _, c := range subexpCases {
   441  		re := MustCompile(c.input)
   442  		n := re.NumSubexp()
   443  		if n != c.num {
   444  			t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
   445  			continue
   446  		}
   447  		names := re.SubexpNames()
   448  		if len(names) != 1+n {
   449  			t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
   450  			continue
   451  		}
   452  		if c.names != nil {
   453  			for i := 0; i < 1+n; i++ {
   454  				if names[i] != c.names[i] {
   455  					t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
   456  				}
   457  			}
   458  		}
   459  	}
   460  }
   461  
   462  var splitTests = []struct {
   463  	s   string
   464  	r   string
   465  	n   int
   466  	out []string
   467  }{
   468  	{"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
   469  	{"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
   470  	{"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
   471  	{"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
   472  	{"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
   473  	{"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
   474  	{"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
   475  	{"baabaab", "a*", -1, []string{"b", "b", "b"}},
   476  	{"baabaab", "ba*", -1, []string{"", "", "", ""}},
   477  	{"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
   478  	{"foobar", "f+.*b+", -1, []string{"", "ar"}},
   479  	{"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
   480  	{"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
   481  	{"a,b,c,d,e,f", ",", 0, nil},
   482  	{",", ",", -1, []string{"", ""}},
   483  	{",,,", ",", -1, []string{"", "", "", ""}},
   484  	{"", ",", -1, []string{""}},
   485  	{"", ".*", -1, []string{""}},
   486  	{"", ".+", -1, []string{""}},
   487  	{"", "", -1, []string{}},
   488  	{"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
   489  	{"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
   490  	{":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
   491  }
   492  
   493  func TestSplit(t *testing.T) {
   494  	for i, test := range splitTests {
   495  		re, err := Compile(test.r)
   496  		if err != nil {
   497  			t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error())
   498  			continue
   499  		}
   500  
   501  		split := re.Split(test.s, test.n)
   502  		if !reflect.DeepEqual(split, test.out) {
   503  			t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
   504  		}
   505  
   506  		if QuoteMeta(test.r) == test.r {
   507  			strsplit := strings.SplitN(test.s, test.r, test.n)
   508  			if !reflect.DeepEqual(split, strsplit) {
   509  				t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
   510  			}
   511  		}
   512  	}
   513  }
   514  
   515  // The following sequence of Match calls used to panic. See issue #12980.
   516  func TestParseAndCompile(t *testing.T) {
   517  	expr := "a$"
   518  	s := "a\nb"
   519  
   520  	for i, tc := range []struct {
   521  		reFlags  syntax.Flags
   522  		expMatch bool
   523  	}{
   524  		{syntax.Perl | syntax.OneLine, false},
   525  		{syntax.Perl &^ syntax.OneLine, true},
   526  	} {
   527  		parsed, err := syntax.Parse(expr, tc.reFlags)
   528  		if err != nil {
   529  			t.Fatalf("%d: parse: %v", i, err)
   530  		}
   531  		re, err := Compile(parsed.String())
   532  		if err != nil {
   533  			t.Fatalf("%d: compile: %v", i, err)
   534  		}
   535  		if match := re.MatchString(s); match != tc.expMatch {
   536  			t.Errorf("%d: %q.MatchString(%q)=%t; expected=%t", i, re, s, match, tc.expMatch)
   537  		}
   538  	}
   539  }
   540  
   541  // Check that one-pass cutoff does trigger.
   542  func TestOnePassCutoff(t *testing.T) {
   543  	re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl)
   544  	if err != nil {
   545  		t.Fatalf("parse: %v", err)
   546  	}
   547  	p, err := syntax.Compile(re.Simplify())
   548  	if err != nil {
   549  		t.Fatalf("compile: %v", err)
   550  	}
   551  	if compileOnePass(p) != notOnePass {
   552  		t.Fatalf("makeOnePass succeeded; wanted notOnePass")
   553  	}
   554  }
   555  
   556  // Check that the same machine can be used with the standard matcher
   557  // and then the backtracker when there are no captures.
   558  func TestSwitchBacktrack(t *testing.T) {
   559  	re := MustCompile(`a|b`)
   560  	long := make([]byte, maxBacktrackVector+1)
   561  
   562  	// The following sequence of Match calls used to panic. See issue #10319.
   563  	re.Match(long)     // triggers standard matcher
   564  	re.Match(long[:1]) // triggers backtracker
   565  }
   566  
   567  func BenchmarkLiteral(b *testing.B) {
   568  	x := strings.Repeat("x", 50) + "y"
   569  	b.StopTimer()
   570  	re := MustCompile("y")
   571  	b.StartTimer()
   572  	for i := 0; i < b.N; i++ {
   573  		if !re.MatchString(x) {
   574  			b.Fatalf("no match!")
   575  		}
   576  	}
   577  }
   578  
   579  func BenchmarkNotLiteral(b *testing.B) {
   580  	x := strings.Repeat("x", 50) + "y"
   581  	b.StopTimer()
   582  	re := MustCompile(".y")
   583  	b.StartTimer()
   584  	for i := 0; i < b.N; i++ {
   585  		if !re.MatchString(x) {
   586  			b.Fatalf("no match!")
   587  		}
   588  	}
   589  }
   590  
   591  func BenchmarkMatchClass(b *testing.B) {
   592  	b.StopTimer()
   593  	x := strings.Repeat("xxxx", 20) + "w"
   594  	re := MustCompile("[abcdw]")
   595  	b.StartTimer()
   596  	for i := 0; i < b.N; i++ {
   597  		if !re.MatchString(x) {
   598  			b.Fatalf("no match!")
   599  		}
   600  	}
   601  }
   602  
   603  func BenchmarkMatchClass_InRange(b *testing.B) {
   604  	b.StopTimer()
   605  	// 'b' is between 'a' and 'c', so the charclass
   606  	// range checking is no help here.
   607  	x := strings.Repeat("bbbb", 20) + "c"
   608  	re := MustCompile("[ac]")
   609  	b.StartTimer()
   610  	for i := 0; i < b.N; i++ {
   611  		if !re.MatchString(x) {
   612  			b.Fatalf("no match!")
   613  		}
   614  	}
   615  }
   616  
   617  func BenchmarkReplaceAll(b *testing.B) {
   618  	x := "abcdefghijklmnopqrstuvwxyz"
   619  	b.StopTimer()
   620  	re := MustCompile("[cjrw]")
   621  	b.StartTimer()
   622  	for i := 0; i < b.N; i++ {
   623  		re.ReplaceAllString(x, "")
   624  	}
   625  }
   626  
   627  func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
   628  	b.StopTimer()
   629  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   630  	re := MustCompile("^zbc(d|e)")
   631  	b.StartTimer()
   632  	for i := 0; i < b.N; i++ {
   633  		re.Match(x)
   634  	}
   635  }
   636  
   637  func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
   638  	b.StopTimer()
   639  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   640  	for i := 0; i < 15; i++ {
   641  		x = append(x, x...)
   642  	}
   643  	re := MustCompile("^zbc(d|e)")
   644  	b.StartTimer()
   645  	for i := 0; i < b.N; i++ {
   646  		re.Match(x)
   647  	}
   648  }
   649  
   650  func BenchmarkAnchoredShortMatch(b *testing.B) {
   651  	b.StopTimer()
   652  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   653  	re := MustCompile("^.bc(d|e)")
   654  	b.StartTimer()
   655  	for i := 0; i < b.N; i++ {
   656  		re.Match(x)
   657  	}
   658  }
   659  
   660  func BenchmarkAnchoredLongMatch(b *testing.B) {
   661  	b.StopTimer()
   662  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   663  	for i := 0; i < 15; i++ {
   664  		x = append(x, x...)
   665  	}
   666  	re := MustCompile("^.bc(d|e)")
   667  	b.StartTimer()
   668  	for i := 0; i < b.N; i++ {
   669  		re.Match(x)
   670  	}
   671  }
   672  
   673  func BenchmarkOnePassShortA(b *testing.B) {
   674  	b.StopTimer()
   675  	x := []byte("abcddddddeeeededd")
   676  	re := MustCompile("^.bc(d|e)*$")
   677  	b.StartTimer()
   678  	for i := 0; i < b.N; i++ {
   679  		re.Match(x)
   680  	}
   681  }
   682  
   683  func BenchmarkNotOnePassShortA(b *testing.B) {
   684  	b.StopTimer()
   685  	x := []byte("abcddddddeeeededd")
   686  	re := MustCompile(".bc(d|e)*$")
   687  	b.StartTimer()
   688  	for i := 0; i < b.N; i++ {
   689  		re.Match(x)
   690  	}
   691  }
   692  
   693  func BenchmarkOnePassShortB(b *testing.B) {
   694  	b.StopTimer()
   695  	x := []byte("abcddddddeeeededd")
   696  	re := MustCompile("^.bc(?:d|e)*$")
   697  	b.StartTimer()
   698  	for i := 0; i < b.N; i++ {
   699  		re.Match(x)
   700  	}
   701  }
   702  
   703  func BenchmarkNotOnePassShortB(b *testing.B) {
   704  	b.StopTimer()
   705  	x := []byte("abcddddddeeeededd")
   706  	re := MustCompile(".bc(?:d|e)*$")
   707  	b.StartTimer()
   708  	for i := 0; i < b.N; i++ {
   709  		re.Match(x)
   710  	}
   711  }
   712  
   713  func BenchmarkOnePassLongPrefix(b *testing.B) {
   714  	b.StopTimer()
   715  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   716  	re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$")
   717  	b.StartTimer()
   718  	for i := 0; i < b.N; i++ {
   719  		re.Match(x)
   720  	}
   721  }
   722  
   723  func BenchmarkOnePassLongNotPrefix(b *testing.B) {
   724  	b.StopTimer()
   725  	x := []byte("abcdefghijklmnopqrstuvwxyz")
   726  	re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$")
   727  	b.StartTimer()
   728  	for i := 0; i < b.N; i++ {
   729  		re.Match(x)
   730  	}
   731  }
   732  
   733  func BenchmarkMatchParallelShared(b *testing.B) {
   734  	x := []byte("this is a long line that contains foo bar baz")
   735  	re := MustCompile("foo (ba+r)? baz")
   736  	b.ResetTimer()
   737  	b.RunParallel(func(pb *testing.PB) {
   738  		for pb.Next() {
   739  			re.Match(x)
   740  		}
   741  	})
   742  }
   743  
   744  func BenchmarkMatchParallelCopied(b *testing.B) {
   745  	x := []byte("this is a long line that contains foo bar baz")
   746  	re := MustCompile("foo (ba+r)? baz")
   747  	b.ResetTimer()
   748  	b.RunParallel(func(pb *testing.PB) {
   749  		re := re.Copy()
   750  		for pb.Next() {
   751  			re.Match(x)
   752  		}
   753  	})
   754  }