summaryrefslogtreecommitdiff
path: root/libgo/go/strings/replace_test.go
blob: e337856c64dc288b6979cce25074b7579f3d2407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package strings_test

import (
	"bytes"
	"fmt"
	"log"
	. "strings"
	"testing"
)

var _ = log.Printf

type ReplacerTest struct {
	r   *Replacer
	in  string
	out string
}

var htmlEscaper = NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;", "\"", "&quot;")

// The http package's old HTML escaping function.
func oldhtmlEscape(s string) string {
	s = Replace(s, "&", "&amp;", -1)
	s = Replace(s, "<", "&lt;", -1)
	s = Replace(s, ">", "&gt;", -1)
	s = Replace(s, "\"", "&quot;", -1)
	s = Replace(s, "'", "&apos;", -1)
	return s
}

var replacer = NewReplacer("aaa", "3[aaa]", "aa", "2[aa]", "a", "1[a]", "i", "i",
	"longerst", "most long", "longer", "medium", "long", "short",
	"X", "Y", "Y", "Z")

var capitalLetters = NewReplacer("a", "A", "b", "B")

var blankToXReplacer = NewReplacer("", "X", "o", "O")

var ReplacerTests = []ReplacerTest{
	// byte->string
	{htmlEscaper, "No changes", "No changes"},
	{htmlEscaper, "I <3 escaping & stuff", "I &lt;3 escaping &amp; stuff"},
	{htmlEscaper, "&&&", "&amp;&amp;&amp;"},

	// generic
	{replacer, "fooaaabar", "foo3[aaa]b1[a]r"},
	{replacer, "long, longerst, longer", "short, most long, medium"},
	{replacer, "XiX", "YiY"},

	// byte->byte
	{capitalLetters, "brad", "BrAd"},
	{capitalLetters, Repeat("a", (32<<10)+123), Repeat("A", (32<<10)+123)},

	// hitting "" special case
	{blankToXReplacer, "oo", "XOXOX"},
}

func TestReplacer(t *testing.T) {
	for i, tt := range ReplacerTests {
		if s := tt.r.Replace(tt.in); s != tt.out {
			t.Errorf("%d. Replace(%q) = %q, want %q", i, tt.in, s, tt.out)
		}
		var buf bytes.Buffer
		n, err := tt.r.WriteString(&buf, tt.in)
		if err != nil {
			t.Errorf("%d. WriteString: %v", i, err)
			continue
		}
		got := buf.String()
		if got != tt.out {
			t.Errorf("%d. WriteString(%q) wrote %q, want %q", i, tt.in, got, tt.out)
			continue
		}
		if n != len(tt.out) {
			t.Errorf("%d. WriteString(%q) wrote correct string but reported %d bytes; want %d (%q)",
				i, tt.in, n, len(tt.out), tt.out)
		}
	}
}

// pickAlgorithmTest is a test that verifies that given input for a
// Replacer that we pick the correct algorithm.
type pickAlgorithmTest struct {
	r    *Replacer
	want string // name of algorithm
}

var pickAlgorithmTests = []pickAlgorithmTest{
	{capitalLetters, "*strings.byteReplacer"},
	{NewReplacer("12", "123"), "*strings.genericReplacer"},
	{NewReplacer("1", "12"), "*strings.byteStringReplacer"},
	{htmlEscaper, "*strings.byteStringReplacer"},
}

func TestPickAlgorithm(t *testing.T) {
	for i, tt := range pickAlgorithmTests {
		got := fmt.Sprintf("%T", tt.r.Replacer())
		if got != tt.want {
			t.Errorf("%d. algorithm = %s, want %s", i, got, tt.want)
		}
	}
}

func BenchmarkGenericMatch(b *testing.B) {
	str := Repeat("A", 100) + Repeat("B", 100)
	generic := NewReplacer("a", "A", "b", "B", "12", "123") // varying lengths forces generic
	for i := 0; i < b.N; i++ {
		generic.Replace(str)
	}
}

func BenchmarkByteByteNoMatch(b *testing.B) {
	str := Repeat("A", 100) + Repeat("B", 100)
	for i := 0; i < b.N; i++ {
		capitalLetters.Replace(str)
	}
}

func BenchmarkByteByteMatch(b *testing.B) {
	str := Repeat("a", 100) + Repeat("b", 100)
	for i := 0; i < b.N; i++ {
		capitalLetters.Replace(str)
	}
}

func BenchmarkByteStringMatch(b *testing.B) {
	str := "<" + Repeat("a", 99) + Repeat("b", 99) + ">"
	for i := 0; i < b.N; i++ {
		htmlEscaper.Replace(str)
	}
}

func BenchmarkHTMLEscapeNew(b *testing.B) {
	str := "I <3 to escape HTML & other text too."
	for i := 0; i < b.N; i++ {
		htmlEscaper.Replace(str)
	}
}

func BenchmarkHTMLEscapeOld(b *testing.B) {
	str := "I <3 to escape HTML & other text too."
	for i := 0; i < b.N; i++ {
		oldhtmlEscape(str)
	}
}

// BenchmarkByteByteReplaces compares byteByteImpl against multiple Replaces.
func BenchmarkByteByteReplaces(b *testing.B) {
	str := Repeat("a", 100) + Repeat("b", 100)
	for i := 0; i < b.N; i++ {
		Replace(Replace(str, "a", "A", -1), "b", "B", -1)
	}
}

// BenchmarkByteByteMap compares byteByteImpl against Map.
func BenchmarkByteByteMap(b *testing.B) {
	str := Repeat("a", 100) + Repeat("b", 100)
	fn := func(r int) int {
		switch r {
		case 'a':
			return int('A')
		case 'b':
			return int('B')
		}
		return r
	}
	for i := 0; i < b.N; i++ {
		Map(fn, str)
	}
}