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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
package runes
import (
"strings"
"testing"
)
type indexTest struct {
s []rune
sep []rune
out int
}
type equalTest struct {
a []rune
b []rune
out bool
}
func newIndexTest(s, sep string, out int) indexTest {
return indexTest{[]rune(s), []rune(sep), out}
}
func newEqualTest(s, sep string, out bool) equalTest {
return equalTest{[]rune(s), []rune(sep), out}
}
var dots = "1....2....3....4"
var indexTests = []indexTest{
newIndexTest("", "", 0),
newIndexTest("", "a", -1),
newIndexTest("", "foo", -1),
newIndexTest("fo", "foo", -1),
newIndexTest("foo", "foo", 0),
newIndexTest("oofofoofooo", "f", 2),
newIndexTest("oofofoofooo", "foo", 4),
newIndexTest("barfoobarfoo", "foo", 3),
newIndexTest("foo", "", 0),
newIndexTest("foo", "o", 1),
newIndexTest("abcABCabc", "A", 3),
// cases with one byte strings - test special case in Index()
newIndexTest("", "a", -1),
newIndexTest("x", "a", -1),
newIndexTest("x", "x", 0),
newIndexTest("abc", "a", 0),
newIndexTest("abc", "b", 1),
newIndexTest("abc", "c", 2),
newIndexTest("abc", "x", -1),
}
var lastIndexTests = []indexTest{
newIndexTest("", "", 0),
newIndexTest("", "a", -1),
newIndexTest("", "foo", -1),
newIndexTest("fo", "foo", -1),
newIndexTest("foo", "foo", 0),
newIndexTest("foo", "f", 0),
newIndexTest("oofofoofooo", "f", 7),
newIndexTest("oofofoofooo", "foo", 7),
newIndexTest("barfoobarfoo", "foo", 9),
newIndexTest("foo", "", 3),
newIndexTest("foo", "o", 2),
newIndexTest("abcABCabc", "A", 3),
newIndexTest("abcABCabc", "a", 6),
}
var indexAnyTests = []indexTest{
newIndexTest("", "", -1),
newIndexTest("", "a", -1),
newIndexTest("", "abc", -1),
newIndexTest("a", "", -1),
newIndexTest("a", "a", 0),
newIndexTest("aaa", "a", 0),
newIndexTest("abc", "xyz", -1),
newIndexTest("abc", "xcz", 2),
newIndexTest("a☺b☻c☹d", "uvw☻xyz", 3),
newIndexTest("aRegExp*", ".(|)*+?^$[]", 7),
newIndexTest(dots+dots+dots, " ", -1),
}
// Execute f on each test case. funcName should be the name of f; it's used
// in failure reports.
func runIndexTests(t *testing.T, f func(s, sep []rune) int, funcName string, testCases []indexTest) {
for _, test := range testCases {
actual := f(test.s, test.sep)
if actual != test.out {
t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
}
}
}
func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
var equalTests = []equalTest{
newEqualTest("a", "a", true),
newEqualTest("a", "b", false),
newEqualTest("a☺b☻c☹d", "uvw☻xyz", false),
newEqualTest("a☺b☻c☹d", "a☺b☻c☹d", true),
}
func TestEqual(t *testing.T) {
for _, test := range equalTests {
actual := Equal(test.a, test.b)
if actual != test.out {
t.Errorf("Equal(%q,%q) = %v; want %v", test.a, test.b, actual, test.out)
}
}
}
func BenchmarkLastIndexRunes(b *testing.B) {
r := []rune("abcdef")
n := []rune("cd")
for i := 0; i < b.N; i++ {
LastIndex(r, n)
}
}
func BenchmarkLastIndexStrings(b *testing.B) {
r := "abcdef"
n := "cd"
for i := 0; i < b.N; i++ {
strings.LastIndex(r, n)
}
}
func BenchmarkIndexAnyRunes(b *testing.B) {
s := []rune("...b...")
c := []rune("abc")
for i := 0; i < b.N; i++ {
IndexAny(s, c)
}
}
func BenchmarkIndexAnyStrings(b *testing.B) {
s := "...b..."
c := "abc"
for i := 0; i < b.N; i++ {
strings.IndexAny(s, c)
}
}
func BenchmarkIndexRuneRunes(b *testing.B) {
s := []rune("...b...")
r := 'b'
for i := 0; i < b.N; i++ {
IndexRune(s, r)
}
}
func BenchmarkIndexRuneStrings(b *testing.B) {
s := "...b..."
r := 'b'
for i := 0; i < b.N; i++ {
strings.IndexRune(s, r)
}
}
func BenchmarkIndexRunes(b *testing.B) {
r := []rune("abcdef")
n := []rune("cd")
for i := 0; i < b.N; i++ {
Index(r, n)
}
}
func BenchmarkIndexStrings(b *testing.B) {
r := "abcdef"
n := "cd"
for i := 0; i < b.N; i++ {
strings.Index(r, n)
}
}
func BenchmarkEqualRunes(b *testing.B) {
x := []rune("abc")
y := []rune("abc")
for i := 0; i < b.N; i++ {
if Equal(x, y) {
continue
}
}
}
func BenchmarkEqualStrings(b *testing.B) {
x := "abc"
y := "abc"
for i := 0; i < b.N; i++ {
if x == y {
continue
}
}
}
func BenchmarkNotEqualRunes(b *testing.B) {
x := []rune("abc")
y := []rune("abcd")
for i := 0; i < b.N; i++ {
if Equal(x, y) {
continue
}
}
}
func BenchmarkNotEqualStrings(b *testing.B) {
x := "abc"
y := "abcd"
for i := 0; i < b.N; i++ {
if x == y {
continue
}
}
}
|