| 1 |
package runes |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
type indexTest struct { |
| 9 |
s []rune |
| 10 |
sep []rune |
| 11 |
out int |
| 12 |
} |
| 13 |
|
| 14 |
type equalTest struct { |
| 15 |
a []rune |
| 16 |
b []rune |
| 17 |
out bool |
| 18 |
} |
| 19 |
|
| 20 |
func newIndexTest(s, sep string, out int) indexTest { |
| 21 |
return indexTest{[]rune(s), []rune(sep), out} |
| 22 |
} |
| 23 |
func newEqualTest(s, sep string, out bool) equalTest { |
| 24 |
return equalTest{[]rune(s), []rune(sep), out} |
| 25 |
} |
| 26 |
|
| 27 |
var dots = "1....2....3....4" |
| 28 |
|
| 29 |
var indexTests = []indexTest{ |
| 30 |
newIndexTest("", "", 0), |
| 31 |
newIndexTest("", "a", -1), |
| 32 |
newIndexTest("", "foo", -1), |
| 33 |
newIndexTest("fo", "foo", -1), |
| 34 |
newIndexTest("foo", "foo", 0), |
| 35 |
newIndexTest("oofofoofooo", "f", 2), |
| 36 |
newIndexTest("oofofoofooo", "foo", 4), |
| 37 |
newIndexTest("barfoobarfoo", "foo", 3), |
| 38 |
newIndexTest("foo", "", 0), |
| 39 |
newIndexTest("foo", "o", 1), |
| 40 |
newIndexTest("abcABCabc", "A", 3), |
| 41 |
|
| 42 |
newIndexTest("", "a", -1), |
| 43 |
newIndexTest("x", "a", -1), |
| 44 |
newIndexTest("x", "x", 0), |
| 45 |
newIndexTest("abc", "a", 0), |
| 46 |
newIndexTest("abc", "b", 1), |
| 47 |
newIndexTest("abc", "c", 2), |
| 48 |
newIndexTest("abc", "x", -1), |
| 49 |
} |
| 50 |
|
| 51 |
var lastIndexTests = []indexTest{ |
| 52 |
newIndexTest("", "", 0), |
| 53 |
newIndexTest("", "a", -1), |
| 54 |
newIndexTest("", "foo", -1), |
| 55 |
newIndexTest("fo", "foo", -1), |
| 56 |
newIndexTest("foo", "foo", 0), |
| 57 |
newIndexTest("foo", "f", 0), |
| 58 |
newIndexTest("oofofoofooo", "f", 7), |
| 59 |
newIndexTest("oofofoofooo", "foo", 7), |
| 60 |
newIndexTest("barfoobarfoo", "foo", 9), |
| 61 |
newIndexTest("foo", "", 3), |
| 62 |
newIndexTest("foo", "o", 2), |
| 63 |
newIndexTest("abcABCabc", "A", 3), |
| 64 |
newIndexTest("abcABCabc", "a", 6), |
| 65 |
} |
| 66 |
|
| 67 |
var indexAnyTests = []indexTest{ |
| 68 |
newIndexTest("", "", -1), |
| 69 |
newIndexTest("", "a", -1), |
| 70 |
newIndexTest("", "abc", -1), |
| 71 |
newIndexTest("a", "", -1), |
| 72 |
newIndexTest("a", "a", 0), |
| 73 |
newIndexTest("aaa", "a", 0), |
| 74 |
newIndexTest("abc", "xyz", -1), |
| 75 |
newIndexTest("abc", "xcz", 2), |
| 76 |
newIndexTest("a☺b☻c☹d", "uvw☻xyz", 3), |
| 77 |
newIndexTest("aRegExp*", ".(|)*+?^$[]", 7), |
| 78 |
newIndexTest(dots+dots+dots, " ", -1), |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
func runIndexTests(t *testing.T, f func(s, sep []rune) int, funcName string, testCases []indexTest) { |
| 84 |
for _, test := range testCases { |
| 85 |
actual := f(test.s, test.sep) |
| 86 |
if actual != test.out { |
| 87 |
t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) } |
| 93 |
func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) } |
| 94 |
func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) } |
| 95 |
|
| 96 |
var equalTests = []equalTest{ |
| 97 |
newEqualTest("a", "a", true), |
| 98 |
newEqualTest("a", "b", false), |
| 99 |
newEqualTest("a☺b☻c☹d", "uvw☻xyz", false), |
| 100 |
newEqualTest("a☺b☻c☹d", "a☺b☻c☹d", true), |
| 101 |
} |
| 102 |
|
| 103 |
func TestEqual(t *testing.T) { |
| 104 |
for _, test := range equalTests { |
| 105 |
actual := Equal(test.a, test.b) |
| 106 |
if actual != test.out { |
| 107 |
t.Errorf("Equal(%q,%q) = %v; want %v", test.a, test.b, actual, test.out) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func BenchmarkLastIndexRunes(b *testing.B) { |
| 113 |
r := []rune("abcdef") |
| 114 |
n := []rune("cd") |
| 115 |
|
| 116 |
for i := 0; i < b.N; i++ { |
| 117 |
LastIndex(r, n) |
| 118 |
} |
| 119 |
} |
| 120 |
func BenchmarkLastIndexStrings(b *testing.B) { |
| 121 |
r := "abcdef" |
| 122 |
n := "cd" |
| 123 |
|
| 124 |
for i := 0; i < b.N; i++ { |
| 125 |
strings.LastIndex(r, n) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func BenchmarkIndexAnyRunes(b *testing.B) { |
| 130 |
s := []rune("...b...") |
| 131 |
c := []rune("abc") |
| 132 |
|
| 133 |
for i := 0; i < b.N; i++ { |
| 134 |
IndexAny(s, c) |
| 135 |
} |
| 136 |
} |
| 137 |
func BenchmarkIndexAnyStrings(b *testing.B) { |
| 138 |
s := "...b..." |
| 139 |
c := "abc" |
| 140 |
|
| 141 |
for i := 0; i < b.N; i++ { |
| 142 |
strings.IndexAny(s, c) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func BenchmarkIndexRuneRunes(b *testing.B) { |
| 147 |
s := []rune("...b...") |
| 148 |
r := 'b' |
| 149 |
|
| 150 |
for i := 0; i < b.N; i++ { |
| 151 |
IndexRune(s, r) |
| 152 |
} |
| 153 |
} |
| 154 |
func BenchmarkIndexRuneStrings(b *testing.B) { |
| 155 |
s := "...b..." |
| 156 |
r := 'b' |
| 157 |
|
| 158 |
for i := 0; i < b.N; i++ { |
| 159 |
strings.IndexRune(s, r) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func BenchmarkIndexRunes(b *testing.B) { |
| 164 |
r := []rune("abcdef") |
| 165 |
n := []rune("cd") |
| 166 |
|
| 167 |
for i := 0; i < b.N; i++ { |
| 168 |
Index(r, n) |
| 169 |
} |
| 170 |
} |
| 171 |
func BenchmarkIndexStrings(b *testing.B) { |
| 172 |
r := "abcdef" |
| 173 |
n := "cd" |
| 174 |
|
| 175 |
for i := 0; i < b.N; i++ { |
| 176 |
strings.Index(r, n) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func BenchmarkEqualRunes(b *testing.B) { |
| 181 |
x := []rune("abc") |
| 182 |
y := []rune("abc") |
| 183 |
|
| 184 |
for i := 0; i < b.N; i++ { |
| 185 |
if Equal(x, y) { |
| 186 |
continue |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func BenchmarkEqualStrings(b *testing.B) { |
| 192 |
x := "abc" |
| 193 |
y := "abc" |
| 194 |
|
| 195 |
for i := 0; i < b.N; i++ { |
| 196 |
if x == y { |
| 197 |
continue |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func BenchmarkNotEqualRunes(b *testing.B) { |
| 203 |
x := []rune("abc") |
| 204 |
y := []rune("abcd") |
| 205 |
|
| 206 |
for i := 0; i < b.N; i++ { |
| 207 |
if Equal(x, y) { |
| 208 |
continue |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func BenchmarkNotEqualStrings(b *testing.B) { |
| 214 |
x := "abc" |
| 215 |
y := "abcd" |
| 216 |
|
| 217 |
for i := 0; i < b.N; i++ { |
| 218 |
if x == y { |
| 219 |
continue |
| 220 |
} |
| 221 |
} |
| 222 |
} |