~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/strings/strings_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import (
8
8
        "bytes"
9
9
        "io"
 
10
        "math/rand"
10
11
        "reflect"
11
12
        . "strings"
12
13
        "testing"
311
312
}
312
313
 
313
314
func TestFieldsFunc(t *testing.T) {
 
315
        for _, tt := range fieldstests {
 
316
                a := FieldsFunc(tt.s, unicode.IsSpace)
 
317
                if !eq(a, tt.a) {
 
318
                        t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
 
319
                        continue
 
320
                }
 
321
        }
314
322
        pred := func(c rune) bool { return c == 'X' }
315
323
        for _, tt := range FieldsFuncTests {
316
324
                a := FieldsFunc(tt.s, pred)
488
496
func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
489
497
 
490
498
var trimTests = []struct {
491
 
        f               string
492
 
        in, cutset, out string
 
499
        f            string
 
500
        in, arg, out string
493
501
}{
494
502
        {"Trim", "abba", "a", "bb"},
495
503
        {"Trim", "abba", "ab", ""},
512
520
        {"TrimRight", "", "123", ""},
513
521
        {"TrimRight", "", "", ""},
514
522
        {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
 
523
        {"TrimPrefix", "aabb", "a", "abb"},
 
524
        {"TrimPrefix", "aabb", "b", "aabb"},
 
525
        {"TrimSuffix", "aabb", "a", "aabb"},
 
526
        {"TrimSuffix", "aabb", "b", "aab"},
515
527
}
516
528
 
517
529
func TestTrim(t *testing.T) {
525
537
                        f = TrimLeft
526
538
                case "TrimRight":
527
539
                        f = TrimRight
 
540
                case "TrimPrefix":
 
541
                        f = TrimPrefix
 
542
                case "TrimSuffix":
 
543
                        f = TrimSuffix
528
544
                default:
529
545
                        t.Errorf("Undefined trim function %s", name)
530
546
                }
531
 
                actual := f(tc.in, tc.cutset)
 
547
                actual := f(tc.in, tc.arg)
532
548
                if actual != tc.out {
533
 
                        t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
 
549
                        t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
534
550
                }
535
551
        }
536
552
}
951
967
func TestContainsRune(t *testing.T) {
952
968
        for _, ct := range ContainsRuneTests {
953
969
                if ContainsRune(ct.str, ct.r) != ct.expected {
954
 
                        t.Errorf("ContainsRune(%s, %s) = %v, want %v",
 
970
                        t.Errorf("ContainsRune(%q, %q) = %v, want %v",
955
971
                                ct.str, ct.r, !ct.expected, ct.expected)
956
972
                }
957
973
        }
984
1000
                }
985
1001
        }
986
1002
}
 
1003
 
 
1004
func makeBenchInputHard() string {
 
1005
        tokens := [...]string{
 
1006
                "<a>", "<p>", "<b>", "<strong>",
 
1007
                "</a>", "</p>", "</b>", "</strong>",
 
1008
                "hello", "world",
 
1009
        }
 
1010
        x := make([]byte, 0, 1<<20)
 
1011
        for len(x) < 1<<20 {
 
1012
                i := rand.Intn(len(tokens))
 
1013
                x = append(x, tokens[i]...)
 
1014
        }
 
1015
        return string(x)
 
1016
}
 
1017
 
 
1018
var benchInputHard = makeBenchInputHard()
 
1019
 
 
1020
func benchmarkIndexHard(b *testing.B, sep string) {
 
1021
        for i := 0; i < b.N; i++ {
 
1022
                Index(benchInputHard, sep)
 
1023
        }
 
1024
}
 
1025
 
 
1026
func benchmarkCountHard(b *testing.B, sep string) {
 
1027
        for i := 0; i < b.N; i++ {
 
1028
                Count(benchInputHard, sep)
 
1029
        }
 
1030
}
 
1031
 
 
1032
func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
 
1033
func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
 
1034
func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
 
1035
 
 
1036
func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
 
1037
func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
 
1038
func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
 
1039
 
 
1040
var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
 
1041
var benchNeedleTorture = Repeat("ABC", 1<<10+1)
 
1042
 
 
1043
func BenchmarkIndexTorture(b *testing.B) {
 
1044
        for i := 0; i < b.N; i++ {
 
1045
                Index(benchInputTorture, benchNeedleTorture)
 
1046
        }
 
1047
}
 
1048
 
 
1049
func BenchmarkCountTorture(b *testing.B) {
 
1050
        for i := 0; i < b.N; i++ {
 
1051
                Count(benchInputTorture, benchNeedleTorture)
 
1052
        }
 
1053
}
 
1054
 
 
1055
func BenchmarkCountTortureOverlapping(b *testing.B) {
 
1056
        A := Repeat("ABC", 1<<20)
 
1057
        B := Repeat("ABC", 1<<10)
 
1058
        for i := 0; i < b.N; i++ {
 
1059
                Count(A, B)
 
1060
        }
 
1061
}
 
1062
 
 
1063
var makeFieldsInput = func() string {
 
1064
        x := make([]byte, 1<<20)
 
1065
        // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
 
1066
        for i := range x {
 
1067
                switch rand.Intn(10) {
 
1068
                case 0:
 
1069
                        x[i] = ' '
 
1070
                case 1:
 
1071
                        if i > 0 && x[i-1] == 'x' {
 
1072
                                copy(x[i-1:], "χ")
 
1073
                                break
 
1074
                        }
 
1075
                        fallthrough
 
1076
                default:
 
1077
                        x[i] = 'x'
 
1078
                }
 
1079
        }
 
1080
        return string(x)
 
1081
}
 
1082
 
 
1083
var fieldsInput = makeFieldsInput()
 
1084
 
 
1085
func BenchmarkFields(b *testing.B) {
 
1086
        b.SetBytes(int64(len(fieldsInput)))
 
1087
        for i := 0; i < b.N; i++ {
 
1088
                Fields(fieldsInput)
 
1089
        }
 
1090
}
 
1091
 
 
1092
func BenchmarkFieldsFunc(b *testing.B) {
 
1093
        b.SetBytes(int64(len(fieldsInput)))
 
1094
        for i := 0; i < b.N; i++ {
 
1095
                FieldsFunc(fieldsInput, unicode.IsSpace)
 
1096
        }
 
1097
}
 
1098
 
 
1099
func BenchmarkSplit1(b *testing.B) {
 
1100
        for i := 0; i < b.N; i++ {
 
1101
                Split(benchInputHard, "")
 
1102
        }
 
1103
}
 
1104
 
 
1105
func BenchmarkSplit2(b *testing.B) {
 
1106
        for i := 0; i < b.N; i++ {
 
1107
                Split(benchInputHard, "/")
 
1108
        }
 
1109
}
 
1110
 
 
1111
func BenchmarkSplit3(b *testing.B) {
 
1112
        for i := 0; i < b.N; i++ {
 
1113
                Split(benchInputHard, "hello")
 
1114
        }
 
1115
}