Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package regexp | 5 package regexp |
6 | 6 |
7 import ( | 7 import ( |
8 "reflect" | 8 "reflect" |
9 "strings" | 9 "strings" |
10 "testing" | 10 "testing" |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 if c.names != nil { | 410 if c.names != nil { |
411 for i := 0; i < 1+n; i++ { | 411 for i := 0; i < 1+n; i++ { |
412 if names[i] != c.names[i] { | 412 if names[i] != c.names[i] { |
413 t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i]) | 413 t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i]) |
414 } | 414 } |
415 } | 415 } |
416 } | 416 } |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 var splitTests = []struct { | |
421 s string | |
422 r string | |
423 n int | |
424 out []string | |
425 }{ | |
426 {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}}, | |
427 {"foo:and:bar", ":", 1, []string{"foo:and:bar"}}, | |
428 {"foo:and:bar", ":", 2, []string{"foo", "and:bar"}}, | |
429 {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}}, | |
430 {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}}, | |
431 {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}}, | |
432 {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}}, | |
433 {"baabaab", "a*", -1, []string{"b", "b", "b"}}, | |
434 {"baabaab", "ba*", -1, []string{"", "", "", ""}}, | |
435 {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}}, | |
436 {"foobar", "f+.*b+", -1, []string{"", "ar"}}, | |
437 {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}}, | |
438 {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}}, | |
439 {"a,b,c,d,e,f", ",", 0, nil}, | |
440 {",", ",", -1, []string{"", ""}}, | |
441 {",,,", ",", -1, []string{"", "", "", ""}}, | |
442 {"", ",", -1, []string{""}}, | |
443 {"", ".*", -1, []string{""}}, | |
444 {"", ".+", -1, []string{""}}, | |
445 {"", "", -1, []string{}}, | |
446 {"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}}, | |
447 {"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}}, | |
448 {":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}}, | |
449 } | |
450 | |
420 func TestSplit(t *testing.T) { | 451 func TestSplit(t *testing.T) { |
421 » var tests = []struct { | 452 » for i, test := range splitTests { |
422 » » s string | |
423 » » r string | |
424 » » n int | |
425 » » out []string | |
426 » }{ | |
427 » » {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}}, | |
428 » » {"foo:and:bar", ":", 1, []string{"foo", "and:bar"}}, | |
429 » » {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}}, | |
430 » » {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}}, | |
431 » » {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}}, | |
432 » » {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}}, | |
433 » » {"baabaab", "a*", -1, []string{"", "b", "b", "b", ""}}, | |
434 » » {"baabaab", "ba*", -1, []string{"", "", "", ""}}, | |
435 » » {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r", ""}}, | |
436 » » {"foobar", "f+.*b+", -1, []string{"", "ar"}}, | |
437 » » {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}}, | |
438 » » {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c", "d,e,f"}}, | |
439 » » {"a,b,c,d,e,f", ",", 0, []string{"a,b,c,d,e,f"}}, | |
440 » » {"", ",", -1, []string{""}}, | |
441 » » {"", ".*", -1, []string{"", ""}}, | |
442 » » {"", ".+", -1, []string{""}}, | |
443 » » {"", "", -1, []string{"", ""}}, | |
444 » » {"foobar", "", -1, []string{"", "f", "o", "o", "b", "a", "r", "" }}, | |
445 » } | |
446 | |
447 » for i, test := range tests { | |
448 re, err := Compile(test.r) | 453 re, err := Compile(test.r) |
449 if err != nil { | 454 if err != nil { |
450 » » » t.Errorf("Split: test %d: expression doesn't compile: %s ; error: %s", i, test.r, err.Error()) | 455 » » » t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Er ror()) |
r
2012/11/19 20:39:47
needlessly verbose.
#%d: %q: compile error: %s
| |
451 continue | 456 continue |
452 } | 457 } |
453 | 458 |
454 split := re.Split(test.s, test.n) | 459 split := re.Split(test.s, test.n) |
455 if !reflect.DeepEqual(split, test.out) { | 460 if !reflect.DeepEqual(split, test.out) { |
456 » » » t.Errorf("Split: test %d: split = %#v; want = %#v", i, s plit, test.out) | 461 » » » t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, t est.out) |
r
2012/11/19 20:39:47
here and above s/Split: test %d/#%d: %q:/
the diag
| |
462 » » } | |
463 | |
464 » » if QuoteMeta(test.r) == test.r { | |
465 » » » strsplit := strings.SplitN(test.s, test.r, test.n) | |
466 » » » if !reflect.DeepEqual(split, strsplit) { | |
467 » » » » t.Errorf("#%d: Split(%q, %q, %d): regexp vs stri ngs mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit ) | |
468 » » » } | |
457 } | 469 } |
458 } | 470 } |
459 } | 471 } |
460 | 472 |
461 func BenchmarkLiteral(b *testing.B) { | 473 func BenchmarkLiteral(b *testing.B) { |
462 x := strings.Repeat("x", 50) + "y" | 474 x := strings.Repeat("x", 50) + "y" |
463 b.StopTimer() | 475 b.StopTimer() |
464 re := MustCompile("y") | 476 re := MustCompile("y") |
465 b.StartTimer() | 477 b.StartTimer() |
466 for i := 0; i < b.N; i++ { | 478 for i := 0; i < b.N; i++ { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
556 x := []byte("abcdefghijklmnopqrstuvwxyz") | 568 x := []byte("abcdefghijklmnopqrstuvwxyz") |
557 for i := 0; i < 15; i++ { | 569 for i := 0; i < 15; i++ { |
558 x = append(x, x...) | 570 x = append(x, x...) |
559 } | 571 } |
560 re := MustCompile("^.bc(d|e)") | 572 re := MustCompile("^.bc(d|e)") |
561 b.StartTimer() | 573 b.StartTimer() |
562 for i := 0; i < b.N; i++ { | 574 for i := 0; i < b.N; i++ { |
563 re.Match(x) | 575 re.Match(x) |
564 } | 576 } |
565 } | 577 } |
LEFT | RIGHT |