| 1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package strings_test
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
"bytes"
|
| 9 |
|
|
"io"
|
| 10 |
|
|
"reflect"
|
| 11 |
|
|
. "strings"
|
| 12 |
|
|
"testing"
|
| 13 |
|
|
"unicode"
|
| 14 |
|
|
"unicode/utf8"
|
| 15 |
|
|
"unsafe"
|
| 16 |
|
|
)
|
| 17 |
|
|
|
| 18 |
|
|
func eq(a, b []string) bool {
|
| 19 |
|
|
if len(a) != len(b) {
|
| 20 |
|
|
return false
|
| 21 |
|
|
}
|
| 22 |
|
|
for i := 0; i < len(a); i++ {
|
| 23 |
|
|
if a[i] != b[i] {
|
| 24 |
|
|
return false
|
| 25 |
|
|
}
|
| 26 |
|
|
}
|
| 27 |
|
|
return true
|
| 28 |
|
|
}
|
| 29 |
|
|
|
| 30 |
|
|
var abcd = "abcd"
|
| 31 |
|
|
var faces = "☺☻☹"
|
| 32 |
|
|
var commas = "1,2,3,4"
|
| 33 |
|
|
var dots = "1....2....3....4"
|
| 34 |
|
|
|
| 35 |
|
|
type IndexTest struct {
|
| 36 |
|
|
s string
|
| 37 |
|
|
sep string
|
| 38 |
|
|
out int
|
| 39 |
|
|
}
|
| 40 |
|
|
|
| 41 |
|
|
var indexTests = []IndexTest{
|
| 42 |
|
|
{"", "", 0},
|
| 43 |
|
|
{"", "a", -1},
|
| 44 |
|
|
{"", "foo", -1},
|
| 45 |
|
|
{"fo", "foo", -1},
|
| 46 |
|
|
{"foo", "foo", 0},
|
| 47 |
|
|
{"oofofoofooo", "f", 2},
|
| 48 |
|
|
{"oofofoofooo", "foo", 4},
|
| 49 |
|
|
{"barfoobarfoo", "foo", 3},
|
| 50 |
|
|
{"foo", "", 0},
|
| 51 |
|
|
{"foo", "o", 1},
|
| 52 |
|
|
{"abcABCabc", "A", 3},
|
| 53 |
|
|
// cases with one byte strings - test special case in Index()
|
| 54 |
|
|
{"", "a", -1},
|
| 55 |
|
|
{"x", "a", -1},
|
| 56 |
|
|
{"x", "x", 0},
|
| 57 |
|
|
{"abc", "a", 0},
|
| 58 |
|
|
{"abc", "b", 1},
|
| 59 |
|
|
{"abc", "c", 2},
|
| 60 |
|
|
{"abc", "x", -1},
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
var lastIndexTests = []IndexTest{
|
| 64 |
|
|
{"", "", 0},
|
| 65 |
|
|
{"", "a", -1},
|
| 66 |
|
|
{"", "foo", -1},
|
| 67 |
|
|
{"fo", "foo", -1},
|
| 68 |
|
|
{"foo", "foo", 0},
|
| 69 |
|
|
{"foo", "f", 0},
|
| 70 |
|
|
{"oofofoofooo", "f", 7},
|
| 71 |
|
|
{"oofofoofooo", "foo", 7},
|
| 72 |
|
|
{"barfoobarfoo", "foo", 9},
|
| 73 |
|
|
{"foo", "", 3},
|
| 74 |
|
|
{"foo", "o", 2},
|
| 75 |
|
|
{"abcABCabc", "A", 3},
|
| 76 |
|
|
{"abcABCabc", "a", 6},
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
var indexAnyTests = []IndexTest{
|
| 80 |
|
|
{"", "", -1},
|
| 81 |
|
|
{"", "a", -1},
|
| 82 |
|
|
{"", "abc", -1},
|
| 83 |
|
|
{"a", "", -1},
|
| 84 |
|
|
{"a", "a", 0},
|
| 85 |
|
|
{"aaa", "a", 0},
|
| 86 |
|
|
{"abc", "xyz", -1},
|
| 87 |
|
|
{"abc", "xcz", 2},
|
| 88 |
|
|
{"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
|
| 89 |
|
|
{"aRegExp*", ".(|)*+?^$[]", 7},
|
| 90 |
|
|
{dots + dots + dots, " ", -1},
|
| 91 |
|
|
}
|
| 92 |
|
|
var lastIndexAnyTests = []IndexTest{
|
| 93 |
|
|
{"", "", -1},
|
| 94 |
|
|
{"", "a", -1},
|
| 95 |
|
|
{"", "abc", -1},
|
| 96 |
|
|
{"a", "", -1},
|
| 97 |
|
|
{"a", "a", 0},
|
| 98 |
|
|
{"aaa", "a", 2},
|
| 99 |
|
|
{"abc", "xyz", -1},
|
| 100 |
|
|
{"abc", "ab", 1},
|
| 101 |
|
|
{"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
|
| 102 |
|
|
{"a.RegExp*", ".(|)*+?^$[]", 8},
|
| 103 |
|
|
{dots + dots + dots, " ", -1},
|
| 104 |
|
|
}
|
| 105 |
|
|
|
| 106 |
|
|
// Execute f on each test case. funcName should be the name of f; it's used
|
| 107 |
|
|
// in failure reports.
|
| 108 |
|
|
func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
|
| 109 |
|
|
for _, test := range testCases {
|
| 110 |
|
|
actual := f(test.s, test.sep)
|
| 111 |
|
|
if actual != test.out {
|
| 112 |
|
|
t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
|
| 113 |
|
|
}
|
| 114 |
|
|
}
|
| 115 |
|
|
}
|
| 116 |
|
|
|
| 117 |
|
|
func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
|
| 118 |
|
|
func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
|
| 119 |
|
|
func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
|
| 120 |
|
|
func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
|
| 121 |
|
|
|
| 122 |
|
|
var indexRuneTests = []struct {
|
| 123 |
|
|
s string
|
| 124 |
|
|
rune rune
|
| 125 |
|
|
out int
|
| 126 |
|
|
}{
|
| 127 |
|
|
{"a A x", 'A', 2},
|
| 128 |
|
|
{"some_text=some_value", '=', 9},
|
| 129 |
|
|
{"☺a", 'a', 3},
|
| 130 |
|
|
{"a☻☺b", '☺', 4},
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
func TestIndexRune(t *testing.T) {
|
| 134 |
|
|
for _, test := range indexRuneTests {
|
| 135 |
|
|
if actual := IndexRune(test.s, test.rune); actual != test.out {
|
| 136 |
|
|
t.Errorf("IndexRune(%q,%d)= %v; want %v", test.s, test.rune, actual, test.out)
|
| 137 |
|
|
}
|
| 138 |
|
|
}
|
| 139 |
|
|
}
|
| 140 |
|
|
|
| 141 |
|
|
const benchmarkString = "some_text=some☺value"
|
| 142 |
|
|
|
| 143 |
|
|
func BenchmarkIndexRune(b *testing.B) {
|
| 144 |
|
|
if got := IndexRune(benchmarkString, '☺'); got != 14 {
|
| 145 |
|
|
b.Fatalf("wrong index: expected 14, got=%d", got)
|
| 146 |
|
|
}
|
| 147 |
|
|
for i := 0; i < b.N; i++ {
|
| 148 |
|
|
IndexRune(benchmarkString, '☺')
|
| 149 |
|
|
}
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
func BenchmarkIndexRuneFastPath(b *testing.B) {
|
| 153 |
|
|
if got := IndexRune(benchmarkString, 'v'); got != 17 {
|
| 154 |
|
|
b.Fatalf("wrong index: expected 17, got=%d", got)
|
| 155 |
|
|
}
|
| 156 |
|
|
for i := 0; i < b.N; i++ {
|
| 157 |
|
|
IndexRune(benchmarkString, 'v')
|
| 158 |
|
|
}
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
func BenchmarkIndex(b *testing.B) {
|
| 162 |
|
|
if got := Index(benchmarkString, "v"); got != 17 {
|
| 163 |
|
|
b.Fatalf("wrong index: expected 17, got=%d", got)
|
| 164 |
|
|
}
|
| 165 |
|
|
for i := 0; i < b.N; i++ {
|
| 166 |
|
|
Index(benchmarkString, "v")
|
| 167 |
|
|
}
|
| 168 |
|
|
}
|
| 169 |
|
|
|
| 170 |
|
|
var explodetests = []struct {
|
| 171 |
|
|
s string
|
| 172 |
|
|
n int
|
| 173 |
|
|
a []string
|
| 174 |
|
|
}{
|
| 175 |
|
|
{"", -1, []string{}},
|
| 176 |
|
|
{abcd, 4, []string{"a", "b", "c", "d"}},
|
| 177 |
|
|
{faces, 3, []string{"☺", "☻", "☹"}},
|
| 178 |
|
|
{abcd, 2, []string{"a", "bcd"}},
|
| 179 |
|
|
}
|
| 180 |
|
|
|
| 181 |
|
|
func TestExplode(t *testing.T) {
|
| 182 |
|
|
for _, tt := range explodetests {
|
| 183 |
|
|
a := SplitN(tt.s, "", tt.n)
|
| 184 |
|
|
if !eq(a, tt.a) {
|
| 185 |
|
|
t.Errorf("explode(%q, %d) = %v; want %v", tt.s, tt.n, a, tt.a)
|
| 186 |
|
|
continue
|
| 187 |
|
|
}
|
| 188 |
|
|
s := Join(a, "")
|
| 189 |
|
|
if s != tt.s {
|
| 190 |
|
|
t.Errorf(`Join(explode(%q, %d), "") = %q`, tt.s, tt.n, s)
|
| 191 |
|
|
}
|
| 192 |
|
|
}
|
| 193 |
|
|
}
|
| 194 |
|
|
|
| 195 |
|
|
type SplitTest struct {
|
| 196 |
|
|
s string
|
| 197 |
|
|
sep string
|
| 198 |
|
|
n int
|
| 199 |
|
|
a []string
|
| 200 |
|
|
}
|
| 201 |
|
|
|
| 202 |
|
|
var splittests = []SplitTest{
|
| 203 |
|
|
{abcd, "a", 0, nil},
|
| 204 |
|
|
{abcd, "a", -1, []string{"", "bcd"}},
|
| 205 |
|
|
{abcd, "z", -1, []string{"abcd"}},
|
| 206 |
|
|
{abcd, "", -1, []string{"a", "b", "c", "d"}},
|
| 207 |
|
|
{commas, ",", -1, []string{"1", "2", "3", "4"}},
|
| 208 |
|
|
{dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
|
| 209 |
|
|
{faces, "☹", -1, []string{"☺☻", ""}},
|
| 210 |
|
|
{faces, "~", -1, []string{faces}},
|
| 211 |
|
|
{faces, "", -1, []string{"☺", "☻", "☹"}},
|
| 212 |
|
|
{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
|
| 213 |
|
|
{"1 2", " ", 3, []string{"1", "2"}},
|
| 214 |
|
|
{"123", "", 2, []string{"1", "23"}},
|
| 215 |
|
|
{"123", "", 17, []string{"1", "2", "3"}},
|
| 216 |
|
|
}
|
| 217 |
|
|
|
| 218 |
|
|
func TestSplit(t *testing.T) {
|
| 219 |
|
|
for _, tt := range splittests {
|
| 220 |
|
|
a := SplitN(tt.s, tt.sep, tt.n)
|
| 221 |
|
|
if !eq(a, tt.a) {
|
| 222 |
|
|
t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
|
| 223 |
|
|
continue
|
| 224 |
|
|
}
|
| 225 |
|
|
if tt.n == 0 {
|
| 226 |
|
|
continue
|
| 227 |
|
|
}
|
| 228 |
|
|
s := Join(a, tt.sep)
|
| 229 |
|
|
if s != tt.s {
|
| 230 |
|
|
t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
|
| 231 |
|
|
}
|
| 232 |
|
|
if tt.n < 0 {
|
| 233 |
|
|
b := Split(tt.s, tt.sep)
|
| 234 |
|
|
if !reflect.DeepEqual(a, b) {
|
| 235 |
|
|
t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
| 236 |
|
|
}
|
| 237 |
|
|
}
|
| 238 |
|
|
}
|
| 239 |
|
|
}
|
| 240 |
|
|
|
| 241 |
|
|
var splitaftertests = []SplitTest{
|
| 242 |
|
|
{abcd, "a", -1, []string{"a", "bcd"}},
|
| 243 |
|
|
{abcd, "z", -1, []string{"abcd"}},
|
| 244 |
|
|
{abcd, "", -1, []string{"a", "b", "c", "d"}},
|
| 245 |
|
|
{commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
|
| 246 |
|
|
{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
|
| 247 |
|
|
{faces, "☹", -1, []string{"☺☻☹", ""}},
|
| 248 |
|
|
{faces, "~", -1, []string{faces}},
|
| 249 |
|
|
{faces, "", -1, []string{"☺", "☻", "☹"}},
|
| 250 |
|
|
{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
|
| 251 |
|
|
{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
|
| 252 |
|
|
{"1 2", " ", 3, []string{"1 ", "2"}},
|
| 253 |
|
|
{"123", "", 2, []string{"1", "23"}},
|
| 254 |
|
|
{"123", "", 17, []string{"1", "2", "3"}},
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
func TestSplitAfter(t *testing.T) {
|
| 258 |
|
|
for _, tt := range splitaftertests {
|
| 259 |
|
|
a := SplitAfterN(tt.s, tt.sep, tt.n)
|
| 260 |
|
|
if !eq(a, tt.a) {
|
| 261 |
|
|
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
|
| 262 |
|
|
continue
|
| 263 |
|
|
}
|
| 264 |
|
|
s := Join(a, "")
|
| 265 |
|
|
if s != tt.s {
|
| 266 |
|
|
t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
|
| 267 |
|
|
}
|
| 268 |
|
|
if tt.n < 0 {
|
| 269 |
|
|
b := SplitAfter(tt.s, tt.sep)
|
| 270 |
|
|
if !reflect.DeepEqual(a, b) {
|
| 271 |
|
|
t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
| 272 |
|
|
}
|
| 273 |
|
|
}
|
| 274 |
|
|
}
|
| 275 |
|
|
}
|
| 276 |
|
|
|
| 277 |
|
|
type FieldsTest struct {
|
| 278 |
|
|
s string
|
| 279 |
|
|
a []string
|
| 280 |
|
|
}
|
| 281 |
|
|
|
| 282 |
|
|
var fieldstests = []FieldsTest{
|
| 283 |
|
|
{"", []string{}},
|
| 284 |
|
|
{" ", []string{}},
|
| 285 |
|
|
{" \t ", []string{}},
|
| 286 |
|
|
{" abc ", []string{"abc"}},
|
| 287 |
|
|
{"1 2 3 4", []string{"1", "2", "3", "4"}},
|
| 288 |
|
|
{"1 2 3 4", []string{"1", "2", "3", "4"}},
|
| 289 |
|
|
{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
|
| 290 |
|
|
{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
|
| 291 |
|
|
{"\u2000\u2001\u2002", []string{}},
|
| 292 |
|
|
{"\n™\t™\n", []string{"™", "™"}},
|
| 293 |
|
|
{faces, []string{faces}},
|
| 294 |
|
|
}
|
| 295 |
|
|
|
| 296 |
|
|
func TestFields(t *testing.T) {
|
| 297 |
|
|
for _, tt := range fieldstests {
|
| 298 |
|
|
a := Fields(tt.s)
|
| 299 |
|
|
if !eq(a, tt.a) {
|
| 300 |
|
|
t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
|
| 301 |
|
|
continue
|
| 302 |
|
|
}
|
| 303 |
|
|
}
|
| 304 |
|
|
}
|
| 305 |
|
|
|
| 306 |
|
|
var FieldsFuncTests = []FieldsTest{
|
| 307 |
|
|
{"", []string{}},
|
| 308 |
|
|
{"XX", []string{}},
|
| 309 |
|
|
{"XXhiXXX", []string{"hi"}},
|
| 310 |
|
|
{"aXXbXXXcX", []string{"a", "b", "c"}},
|
| 311 |
|
|
}
|
| 312 |
|
|
|
| 313 |
|
|
func TestFieldsFunc(t *testing.T) {
|
| 314 |
|
|
pred := func(c rune) bool { return c == 'X' }
|
| 315 |
|
|
for _, tt := range FieldsFuncTests {
|
| 316 |
|
|
a := FieldsFunc(tt.s, pred)
|
| 317 |
|
|
if !eq(a, tt.a) {
|
| 318 |
|
|
t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
|
| 319 |
|
|
}
|
| 320 |
|
|
}
|
| 321 |
|
|
}
|
| 322 |
|
|
|
| 323 |
|
|
// Test case for any function which accepts and returns a single string.
|
| 324 |
|
|
type StringTest struct {
|
| 325 |
|
|
in, out string
|
| 326 |
|
|
}
|
| 327 |
|
|
|
| 328 |
|
|
// Execute f on each test case. funcName should be the name of f; it's used
|
| 329 |
|
|
// in failure reports.
|
| 330 |
|
|
func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
|
| 331 |
|
|
for _, tc := range testCases {
|
| 332 |
|
|
actual := f(tc.in)
|
| 333 |
|
|
if actual != tc.out {
|
| 334 |
|
|
t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
|
| 335 |
|
|
}
|
| 336 |
|
|
}
|
| 337 |
|
|
}
|
| 338 |
|
|
|
| 339 |
|
|
var upperTests = []StringTest{
|
| 340 |
|
|
{"", ""},
|
| 341 |
|
|
{"abc", "ABC"},
|
| 342 |
|
|
{"AbC123", "ABC123"},
|
| 343 |
|
|
{"azAZ09_", "AZAZ09_"},
|
| 344 |
|
|
{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
|
| 345 |
|
|
}
|
| 346 |
|
|
|
| 347 |
|
|
var lowerTests = []StringTest{
|
| 348 |
|
|
{"", ""},
|
| 349 |
|
|
{"abc", "abc"},
|
| 350 |
|
|
{"AbC123", "abc123"},
|
| 351 |
|
|
{"azAZ09_", "azaz09_"},
|
| 352 |
|
|
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
|
| 353 |
|
|
}
|
| 354 |
|
|
|
| 355 |
|
|
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
|
| 356 |
|
|
|
| 357 |
|
|
var trimSpaceTests = []StringTest{
|
| 358 |
|
|
{"", ""},
|
| 359 |
|
|
{"abc", "abc"},
|
| 360 |
|
|
{space + "abc" + space, "abc"},
|
| 361 |
|
|
{" ", ""},
|
| 362 |
|
|
{" \t\r\n \t\t\r\r\n\n ", ""},
|
| 363 |
|
|
{" \t\r\n x\t\t\r\r\n\n ", "x"},
|
| 364 |
|
|
{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
|
| 365 |
|
|
{"1 \t\r\n2", "1 \t\r\n2"},
|
| 366 |
|
|
{" x\x80", "x\x80"},
|
| 367 |
|
|
{" x\xc0", "x\xc0"},
|
| 368 |
|
|
{"x \xc0\xc0 ", "x \xc0\xc0"},
|
| 369 |
|
|
{"x \xc0", "x \xc0"},
|
| 370 |
|
|
{"x \xc0 ", "x \xc0"},
|
| 371 |
|
|
{"x \xc0\xc0 ", "x \xc0\xc0"},
|
| 372 |
|
|
{"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
|
| 373 |
|
|
{"x ☺ ", "x ☺"},
|
| 374 |
|
|
}
|
| 375 |
|
|
|
| 376 |
|
|
func tenRunes(ch rune) string {
|
| 377 |
|
|
r := make([]rune, 10)
|
| 378 |
|
|
for i := range r {
|
| 379 |
|
|
r[i] = ch
|
| 380 |
|
|
}
|
| 381 |
|
|
return string(r)
|
| 382 |
|
|
}
|
| 383 |
|
|
|
| 384 |
|
|
// User-defined self-inverse mapping function
|
| 385 |
|
|
func rot13(r rune) rune {
|
| 386 |
|
|
step := rune(13)
|
| 387 |
|
|
if r >= 'a' && r <= 'z' {
|
| 388 |
|
|
return ((r - 'a' + step) % 26) + 'a'
|
| 389 |
|
|
}
|
| 390 |
|
|
if r >= 'A' && r <= 'Z' {
|
| 391 |
|
|
return ((r - 'A' + step) % 26) + 'A'
|
| 392 |
|
|
}
|
| 393 |
|
|
return r
|
| 394 |
|
|
}
|
| 395 |
|
|
|
| 396 |
|
|
func TestMap(t *testing.T) {
|
| 397 |
|
|
// Run a couple of awful growth/shrinkage tests
|
| 398 |
|
|
a := tenRunes('a')
|
| 399 |
|
|
// 1. Grow. This triggers two reallocations in Map.
|
| 400 |
|
|
maxRune := func(rune) rune { return unicode.MaxRune }
|
| 401 |
|
|
m := Map(maxRune, a)
|
| 402 |
|
|
expect := tenRunes(unicode.MaxRune)
|
| 403 |
|
|
if m != expect {
|
| 404 |
|
|
t.Errorf("growing: expected %q got %q", expect, m)
|
| 405 |
|
|
}
|
| 406 |
|
|
|
| 407 |
|
|
// 2. Shrink
|
| 408 |
|
|
minRune := func(rune) rune { return 'a' }
|
| 409 |
|
|
m = Map(minRune, tenRunes(unicode.MaxRune))
|
| 410 |
|
|
expect = a
|
| 411 |
|
|
if m != expect {
|
| 412 |
|
|
t.Errorf("shrinking: expected %q got %q", expect, m)
|
| 413 |
|
|
}
|
| 414 |
|
|
|
| 415 |
|
|
// 3. Rot13
|
| 416 |
|
|
m = Map(rot13, "a to zed")
|
| 417 |
|
|
expect = "n gb mrq"
|
| 418 |
|
|
if m != expect {
|
| 419 |
|
|
t.Errorf("rot13: expected %q got %q", expect, m)
|
| 420 |
|
|
}
|
| 421 |
|
|
|
| 422 |
|
|
// 4. Rot13^2
|
| 423 |
|
|
m = Map(rot13, Map(rot13, "a to zed"))
|
| 424 |
|
|
expect = "a to zed"
|
| 425 |
|
|
if m != expect {
|
| 426 |
|
|
t.Errorf("rot13: expected %q got %q", expect, m)
|
| 427 |
|
|
}
|
| 428 |
|
|
|
| 429 |
|
|
// 5. Drop
|
| 430 |
|
|
dropNotLatin := func(r rune) rune {
|
| 431 |
|
|
if unicode.Is(unicode.Latin, r) {
|
| 432 |
|
|
return r
|
| 433 |
|
|
}
|
| 434 |
|
|
return -1
|
| 435 |
|
|
}
|
| 436 |
|
|
m = Map(dropNotLatin, "Hello, 세계")
|
| 437 |
|
|
expect = "Hello"
|
| 438 |
|
|
if m != expect {
|
| 439 |
|
|
t.Errorf("drop: expected %q got %q", expect, m)
|
| 440 |
|
|
}
|
| 441 |
|
|
|
| 442 |
|
|
// 6. Identity
|
| 443 |
|
|
identity := func(r rune) rune {
|
| 444 |
|
|
return r
|
| 445 |
|
|
}
|
| 446 |
|
|
orig := "Input string that we expect not to be copied."
|
| 447 |
|
|
m = Map(identity, orig)
|
| 448 |
|
|
if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
|
| 449 |
|
|
(*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
|
| 450 |
|
|
t.Error("unexpected copy during identity map")
|
| 451 |
|
|
}
|
| 452 |
|
|
}
|
| 453 |
|
|
|
| 454 |
|
|
func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
|
| 455 |
|
|
|
| 456 |
|
|
func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
|
| 457 |
|
|
|
| 458 |
|
|
func BenchmarkMapNoChanges(b *testing.B) {
|
| 459 |
|
|
identity := func(r rune) rune {
|
| 460 |
|
|
return r
|
| 461 |
|
|
}
|
| 462 |
|
|
for i := 0; i < b.N; i++ {
|
| 463 |
|
|
Map(identity, "Some string that won't be modified.")
|
| 464 |
|
|
}
|
| 465 |
|
|
}
|
| 466 |
|
|
|
| 467 |
|
|
func TestSpecialCase(t *testing.T) {
|
| 468 |
|
|
lower := "abcçdefgğhıijklmnoöprsştuüvyz"
|
| 469 |
|
|
upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
|
| 470 |
|
|
u := ToUpperSpecial(unicode.TurkishCase, upper)
|
| 471 |
|
|
if u != upper {
|
| 472 |
|
|
t.Errorf("Upper(upper) is %s not %s", u, upper)
|
| 473 |
|
|
}
|
| 474 |
|
|
u = ToUpperSpecial(unicode.TurkishCase, lower)
|
| 475 |
|
|
if u != upper {
|
| 476 |
|
|
t.Errorf("Upper(lower) is %s not %s", u, upper)
|
| 477 |
|
|
}
|
| 478 |
|
|
l := ToLowerSpecial(unicode.TurkishCase, lower)
|
| 479 |
|
|
if l != lower {
|
| 480 |
|
|
t.Errorf("Lower(lower) is %s not %s", l, lower)
|
| 481 |
|
|
}
|
| 482 |
|
|
l = ToLowerSpecial(unicode.TurkishCase, upper)
|
| 483 |
|
|
if l != lower {
|
| 484 |
|
|
t.Errorf("Lower(upper) is %s not %s", l, lower)
|
| 485 |
|
|
}
|
| 486 |
|
|
}
|
| 487 |
|
|
|
| 488 |
|
|
func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
|
| 489 |
|
|
|
| 490 |
|
|
var trimTests = []struct {
|
| 491 |
|
|
f string
|
| 492 |
|
|
in, cutset, out string
|
| 493 |
|
|
}{
|
| 494 |
|
|
{"Trim", "abba", "a", "bb"},
|
| 495 |
|
|
{"Trim", "abba", "ab", ""},
|
| 496 |
|
|
{"TrimLeft", "abba", "ab", ""},
|
| 497 |
|
|
{"TrimRight", "abba", "ab", ""},
|
| 498 |
|
|
{"TrimLeft", "abba", "a", "bba"},
|
| 499 |
|
|
{"TrimRight", "abba", "a", "abb"},
|
| 500 |
|
|
{"Trim", "", "<>", "tag"},
|
| 501 |
|
|
{"Trim", "* listitem", " *", "listitem"},
|
| 502 |
|
|
{"Trim", `"quote"`, `"`, "quote"},
|
| 503 |
|
|
{"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
|
| 504 |
|
|
//empty string tests
|
| 505 |
|
|
{"Trim", "abba", "", "abba"},
|
| 506 |
|
|
{"Trim", "", "123", ""},
|
| 507 |
|
|
{"Trim", "", "", ""},
|
| 508 |
|
|
{"TrimLeft", "abba", "", "abba"},
|
| 509 |
|
|
{"TrimLeft", "", "123", ""},
|
| 510 |
|
|
{"TrimLeft", "", "", ""},
|
| 511 |
|
|
{"TrimRight", "abba", "", "abba"},
|
| 512 |
|
|
{"TrimRight", "", "123", ""},
|
| 513 |
|
|
{"TrimRight", "", "", ""},
|
| 514 |
|
|
{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
|
| 515 |
|
|
}
|
| 516 |
|
|
|
| 517 |
|
|
func TestTrim(t *testing.T) {
|
| 518 |
|
|
for _, tc := range trimTests {
|
| 519 |
|
|
name := tc.f
|
| 520 |
|
|
var f func(string, string) string
|
| 521 |
|
|
switch name {
|
| 522 |
|
|
case "Trim":
|
| 523 |
|
|
f = Trim
|
| 524 |
|
|
case "TrimLeft":
|
| 525 |
|
|
f = TrimLeft
|
| 526 |
|
|
case "TrimRight":
|
| 527 |
|
|
f = TrimRight
|
| 528 |
|
|
default:
|
| 529 |
|
|
t.Errorf("Undefined trim function %s", name)
|
| 530 |
|
|
}
|
| 531 |
|
|
actual := f(tc.in, tc.cutset)
|
| 532 |
|
|
if actual != tc.out {
|
| 533 |
|
|
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
|
| 534 |
|
|
}
|
| 535 |
|
|
}
|
| 536 |
|
|
}
|
| 537 |
|
|
|
| 538 |
|
|
type predicate struct {
|
| 539 |
|
|
f func(rune) bool
|
| 540 |
|
|
name string
|
| 541 |
|
|
}
|
| 542 |
|
|
|
| 543 |
|
|
var isSpace = predicate{unicode.IsSpace, "IsSpace"}
|
| 544 |
|
|
var isDigit = predicate{unicode.IsDigit, "IsDigit"}
|
| 545 |
|
|
var isUpper = predicate{unicode.IsUpper, "IsUpper"}
|
| 546 |
|
|
var isValidRune = predicate{
|
| 547 |
|
|
func(r rune) bool {
|
| 548 |
|
|
return r != utf8.RuneError
|
| 549 |
|
|
},
|
| 550 |
|
|
"IsValidRune",
|
| 551 |
|
|
}
|
| 552 |
|
|
|
| 553 |
|
|
func not(p predicate) predicate {
|
| 554 |
|
|
return predicate{
|
| 555 |
|
|
func(r rune) bool {
|
| 556 |
|
|
return !p.f(r)
|
| 557 |
|
|
},
|
| 558 |
|
|
"not " + p.name,
|
| 559 |
|
|
}
|
| 560 |
|
|
}
|
| 561 |
|
|
|
| 562 |
|
|
var trimFuncTests = []struct {
|
| 563 |
|
|
f predicate
|
| 564 |
|
|
in, out string
|
| 565 |
|
|
}{
|
| 566 |
|
|
{isSpace, space + " hello " + space, "hello"},
|
| 567 |
|
|
{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
|
| 568 |
|
|
{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
|
| 569 |
|
|
{not(isSpace), "hello" + space + "hello", space},
|
| 570 |
|
|
{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
|
| 571 |
|
|
{isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
|
| 572 |
|
|
{not(isValidRune), "\xc0a\xc0", "a"},
|
| 573 |
|
|
}
|
| 574 |
|
|
|
| 575 |
|
|
func TestTrimFunc(t *testing.T) {
|
| 576 |
|
|
for _, tc := range trimFuncTests {
|
| 577 |
|
|
actual := TrimFunc(tc.in, tc.f.f)
|
| 578 |
|
|
if actual != tc.out {
|
| 579 |
|
|
t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
|
| 580 |
|
|
}
|
| 581 |
|
|
}
|
| 582 |
|
|
}
|
| 583 |
|
|
|
| 584 |
|
|
var indexFuncTests = []struct {
|
| 585 |
|
|
in string
|
| 586 |
|
|
f predicate
|
| 587 |
|
|
first, last int
|
| 588 |
|
|
}{
|
| 589 |
|
|
{"", isValidRune, -1, -1},
|
| 590 |
|
|
{"abc", isDigit, -1, -1},
|
| 591 |
|
|
{"0123", isDigit, 0, 3},
|
| 592 |
|
|
{"a1b", isDigit, 1, 1},
|
| 593 |
|
|
{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
|
| 594 |
|
|
{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
|
| 595 |
|
|
{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
|
| 596 |
|
|
{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
|
| 597 |
|
|
|
| 598 |
|
|
// tests of invalid UTF-8
|
| 599 |
|
|
{"\x801", isDigit, 1, 1},
|
| 600 |
|
|
{"\x80abc", isDigit, -1, -1},
|
| 601 |
|
|
{"\xc0a\xc0", isValidRune, 1, 1},
|
| 602 |
|
|
{"\xc0a\xc0", not(isValidRune), 0, 2},
|
| 603 |
|
|
{"\xc0☺\xc0", not(isValidRune), 0, 4},
|
| 604 |
|
|
{"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
|
| 605 |
|
|
{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
|
| 606 |
|
|
{"a\xe0\x80cd", not(isValidRune), 1, 2},
|
| 607 |
|
|
{"\x80\x80\x80\x80", not(isValidRune), 0, 3},
|
| 608 |
|
|
}
|
| 609 |
|
|
|
| 610 |
|
|
func TestIndexFunc(t *testing.T) {
|
| 611 |
|
|
for _, tc := range indexFuncTests {
|
| 612 |
|
|
first := IndexFunc(tc.in, tc.f.f)
|
| 613 |
|
|
if first != tc.first {
|
| 614 |
|
|
t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
|
| 615 |
|
|
}
|
| 616 |
|
|
last := LastIndexFunc(tc.in, tc.f.f)
|
| 617 |
|
|
if last != tc.last {
|
| 618 |
|
|
t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
|
| 619 |
|
|
}
|
| 620 |
|
|
}
|
| 621 |
|
|
}
|
| 622 |
|
|
|
| 623 |
|
|
func equal(m string, s1, s2 string, t *testing.T) bool {
|
| 624 |
|
|
if s1 == s2 {
|
| 625 |
|
|
return true
|
| 626 |
|
|
}
|
| 627 |
|
|
e1 := Split(s1, "")
|
| 628 |
|
|
e2 := Split(s2, "")
|
| 629 |
|
|
for i, c1 := range e1 {
|
| 630 |
|
|
if i > len(e2) {
|
| 631 |
|
|
break
|
| 632 |
|
|
}
|
| 633 |
|
|
r1, _ := utf8.DecodeRuneInString(c1)
|
| 634 |
|
|
r2, _ := utf8.DecodeRuneInString(e2[i])
|
| 635 |
|
|
if r1 != r2 {
|
| 636 |
|
|
t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
|
| 637 |
|
|
}
|
| 638 |
|
|
}
|
| 639 |
|
|
return false
|
| 640 |
|
|
}
|
| 641 |
|
|
|
| 642 |
|
|
func TestCaseConsistency(t *testing.T) {
|
| 643 |
|
|
// Make a string of all the runes.
|
| 644 |
|
|
numRunes := int(unicode.MaxRune + 1)
|
| 645 |
|
|
if testing.Short() {
|
| 646 |
|
|
numRunes = 1000
|
| 647 |
|
|
}
|
| 648 |
|
|
a := make([]rune, numRunes)
|
| 649 |
|
|
for i := range a {
|
| 650 |
|
|
a[i] = rune(i)
|
| 651 |
|
|
}
|
| 652 |
|
|
s := string(a)
|
| 653 |
|
|
// convert the cases.
|
| 654 |
|
|
upper := ToUpper(s)
|
| 655 |
|
|
lower := ToLower(s)
|
| 656 |
|
|
|
| 657 |
|
|
// Consistency checks
|
| 658 |
|
|
if n := utf8.RuneCountInString(upper); n != numRunes {
|
| 659 |
|
|
t.Error("rune count wrong in upper:", n)
|
| 660 |
|
|
}
|
| 661 |
|
|
if n := utf8.RuneCountInString(lower); n != numRunes {
|
| 662 |
|
|
t.Error("rune count wrong in lower:", n)
|
| 663 |
|
|
}
|
| 664 |
|
|
if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
|
| 665 |
|
|
t.Error("ToUpper(upper) consistency fail")
|
| 666 |
|
|
}
|
| 667 |
|
|
if !equal("ToLower(lower)", ToLower(lower), lower, t) {
|
| 668 |
|
|
t.Error("ToLower(lower) consistency fail")
|
| 669 |
|
|
}
|
| 670 |
|
|
/*
|
| 671 |
|
|
These fail because of non-one-to-oneness of the data, such as multiple
|
| 672 |
|
|
upper case 'I' mapping to 'i'. We comment them out but keep them for
|
| 673 |
|
|
interest.
|
| 674 |
|
|
For instance: CAPITAL LETTER I WITH DOT ABOVE:
|
| 675 |
|
|
unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
|
| 676 |
|
|
|
| 677 |
|
|
if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
|
| 678 |
|
|
t.Error("ToUpper(lower) consistency fail");
|
| 679 |
|
|
}
|
| 680 |
|
|
if !equal("ToLower(upper)", ToLower(upper), lower, t) {
|
| 681 |
|
|
t.Error("ToLower(upper) consistency fail");
|
| 682 |
|
|
}
|
| 683 |
|
|
*/
|
| 684 |
|
|
}
|
| 685 |
|
|
|
| 686 |
|
|
var RepeatTests = []struct {
|
| 687 |
|
|
in, out string
|
| 688 |
|
|
count int
|
| 689 |
|
|
}{
|
| 690 |
|
|
{"", "", 0},
|
| 691 |
|
|
{"", "", 1},
|
| 692 |
|
|
{"", "", 2},
|
| 693 |
|
|
{"-", "", 0},
|
| 694 |
|
|
{"-", "-", 1},
|
| 695 |
|
|
{"-", "----------", 10},
|
| 696 |
|
|
{"abc ", "abc abc abc ", 3},
|
| 697 |
|
|
}
|
| 698 |
|
|
|
| 699 |
|
|
func TestRepeat(t *testing.T) {
|
| 700 |
|
|
for _, tt := range RepeatTests {
|
| 701 |
|
|
a := Repeat(tt.in, tt.count)
|
| 702 |
|
|
if !equal("Repeat(s)", a, tt.out, t) {
|
| 703 |
|
|
t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
|
| 704 |
|
|
continue
|
| 705 |
|
|
}
|
| 706 |
|
|
}
|
| 707 |
|
|
}
|
| 708 |
|
|
|
| 709 |
|
|
func runesEqual(a, b []rune) bool {
|
| 710 |
|
|
if len(a) != len(b) {
|
| 711 |
|
|
return false
|
| 712 |
|
|
}
|
| 713 |
|
|
for i, r := range a {
|
| 714 |
|
|
if r != b[i] {
|
| 715 |
|
|
return false
|
| 716 |
|
|
}
|
| 717 |
|
|
}
|
| 718 |
|
|
return true
|
| 719 |
|
|
}
|
| 720 |
|
|
|
| 721 |
|
|
var RunesTests = []struct {
|
| 722 |
|
|
in string
|
| 723 |
|
|
out []rune
|
| 724 |
|
|
lossy bool
|
| 725 |
|
|
}{
|
| 726 |
|
|
{"", []rune{}, false},
|
| 727 |
|
|
{" ", []rune{32}, false},
|
| 728 |
|
|
{"ABC", []rune{65, 66, 67}, false},
|
| 729 |
|
|
{"abc", []rune{97, 98, 99}, false},
|
| 730 |
|
|
{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
|
| 731 |
|
|
{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
|
| 732 |
|
|
{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
|
| 733 |
|
|
}
|
| 734 |
|
|
|
| 735 |
|
|
func TestRunes(t *testing.T) {
|
| 736 |
|
|
for _, tt := range RunesTests {
|
| 737 |
|
|
a := []rune(tt.in)
|
| 738 |
|
|
if !runesEqual(a, tt.out) {
|
| 739 |
|
|
t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
|
| 740 |
|
|
continue
|
| 741 |
|
|
}
|
| 742 |
|
|
if !tt.lossy {
|
| 743 |
|
|
// can only test reassembly if we didn't lose information
|
| 744 |
|
|
s := string(a)
|
| 745 |
|
|
if s != tt.in {
|
| 746 |
|
|
t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
|
| 747 |
|
|
}
|
| 748 |
|
|
}
|
| 749 |
|
|
}
|
| 750 |
|
|
}
|
| 751 |
|
|
|
| 752 |
|
|
func TestReadByte(t *testing.T) {
|
| 753 |
|
|
testStrings := []string{"", abcd, faces, commas}
|
| 754 |
|
|
for _, s := range testStrings {
|
| 755 |
|
|
reader := NewReader(s)
|
| 756 |
|
|
if e := reader.UnreadByte(); e == nil {
|
| 757 |
|
|
t.Errorf("Unreading %q at beginning: expected error", s)
|
| 758 |
|
|
}
|
| 759 |
|
|
var res bytes.Buffer
|
| 760 |
|
|
for {
|
| 761 |
|
|
b, e := reader.ReadByte()
|
| 762 |
|
|
if e == io.EOF {
|
| 763 |
|
|
break
|
| 764 |
|
|
}
|
| 765 |
|
|
if e != nil {
|
| 766 |
|
|
t.Errorf("Reading %q: %s", s, e)
|
| 767 |
|
|
break
|
| 768 |
|
|
}
|
| 769 |
|
|
res.WriteByte(b)
|
| 770 |
|
|
// unread and read again
|
| 771 |
|
|
e = reader.UnreadByte()
|
| 772 |
|
|
if e != nil {
|
| 773 |
|
|
t.Errorf("Unreading %q: %s", s, e)
|
| 774 |
|
|
break
|
| 775 |
|
|
}
|
| 776 |
|
|
b1, e := reader.ReadByte()
|
| 777 |
|
|
if e != nil {
|
| 778 |
|
|
t.Errorf("Reading %q after unreading: %s", s, e)
|
| 779 |
|
|
break
|
| 780 |
|
|
}
|
| 781 |
|
|
if b1 != b {
|
| 782 |
|
|
t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
|
| 783 |
|
|
break
|
| 784 |
|
|
}
|
| 785 |
|
|
}
|
| 786 |
|
|
if res.String() != s {
|
| 787 |
|
|
t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
|
| 788 |
|
|
}
|
| 789 |
|
|
}
|
| 790 |
|
|
}
|
| 791 |
|
|
|
| 792 |
|
|
func TestReadRune(t *testing.T) {
|
| 793 |
|
|
testStrings := []string{"", abcd, faces, commas}
|
| 794 |
|
|
for _, s := range testStrings {
|
| 795 |
|
|
reader := NewReader(s)
|
| 796 |
|
|
if e := reader.UnreadRune(); e == nil {
|
| 797 |
|
|
t.Errorf("Unreading %q at beginning: expected error", s)
|
| 798 |
|
|
}
|
| 799 |
|
|
res := ""
|
| 800 |
|
|
for {
|
| 801 |
|
|
r, z, e := reader.ReadRune()
|
| 802 |
|
|
if e == io.EOF {
|
| 803 |
|
|
break
|
| 804 |
|
|
}
|
| 805 |
|
|
if e != nil {
|
| 806 |
|
|
t.Errorf("Reading %q: %s", s, e)
|
| 807 |
|
|
break
|
| 808 |
|
|
}
|
| 809 |
|
|
res += string(r)
|
| 810 |
|
|
// unread and read again
|
| 811 |
|
|
e = reader.UnreadRune()
|
| 812 |
|
|
if e != nil {
|
| 813 |
|
|
t.Errorf("Unreading %q: %s", s, e)
|
| 814 |
|
|
break
|
| 815 |
|
|
}
|
| 816 |
|
|
r1, z1, e := reader.ReadRune()
|
| 817 |
|
|
if e != nil {
|
| 818 |
|
|
t.Errorf("Reading %q after unreading: %s", s, e)
|
| 819 |
|
|
break
|
| 820 |
|
|
}
|
| 821 |
|
|
if r1 != r {
|
| 822 |
|
|
t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
|
| 823 |
|
|
break
|
| 824 |
|
|
}
|
| 825 |
|
|
if z1 != z {
|
| 826 |
|
|
t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
|
| 827 |
|
|
break
|
| 828 |
|
|
}
|
| 829 |
|
|
}
|
| 830 |
|
|
if res != s {
|
| 831 |
|
|
t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
|
| 832 |
|
|
}
|
| 833 |
|
|
}
|
| 834 |
|
|
}
|
| 835 |
|
|
|
| 836 |
|
|
var ReplaceTests = []struct {
|
| 837 |
|
|
in string
|
| 838 |
|
|
old, new string
|
| 839 |
|
|
n int
|
| 840 |
|
|
out string
|
| 841 |
|
|
}{
|
| 842 |
|
|
{"hello", "l", "L", 0, "hello"},
|
| 843 |
|
|
{"hello", "l", "L", -1, "heLLo"},
|
| 844 |
|
|
{"hello", "x", "X", -1, "hello"},
|
| 845 |
|
|
{"", "x", "X", -1, ""},
|
| 846 |
|
|
{"radar", "r", "", -1, "ada"},
|
| 847 |
|
|
{"", "", "<>", -1, "<>"},
|
| 848 |
|
|
{"banana", "a", "<>", -1, "b<>n<>n<>"},
|
| 849 |
|
|
{"banana", "a", "<>", 1, "b<>nana"},
|
| 850 |
|
|
{"banana", "a", "<>", 1000, "b<>n<>n<>"},
|
| 851 |
|
|
{"banana", "an", "<>", -1, "b<><>a"},
|
| 852 |
|
|
{"banana", "ana", "<>", -1, "b<>na"},
|
| 853 |
|
|
{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
|
| 854 |
|
|
{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
|
| 855 |
|
|
{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
|
| 856 |
|
|
{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
|
| 857 |
|
|
{"banana", "", "<>", 1, "<>banana"},
|
| 858 |
|
|
{"banana", "a", "a", -1, "banana"},
|
| 859 |
|
|
{"banana", "a", "a", 1, "banana"},
|
| 860 |
|
|
{"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
|
| 861 |
|
|
}
|
| 862 |
|
|
|
| 863 |
|
|
func TestReplace(t *testing.T) {
|
| 864 |
|
|
for _, tt := range ReplaceTests {
|
| 865 |
|
|
if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
|
| 866 |
|
|
t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
|
| 867 |
|
|
}
|
| 868 |
|
|
}
|
| 869 |
|
|
}
|
| 870 |
|
|
|
| 871 |
|
|
var TitleTests = []struct {
|
| 872 |
|
|
in, out string
|
| 873 |
|
|
}{
|
| 874 |
|
|
{"", ""},
|
| 875 |
|
|
{"a", "A"},
|
| 876 |
|
|
{" aaa aaa aaa ", " Aaa Aaa Aaa "},
|
| 877 |
|
|
{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
|
| 878 |
|
|
{"123a456", "123a456"},
|
| 879 |
|
|
{"double-blind", "Double-Blind"},
|
| 880 |
|
|
{"ÿøû", "Ÿøû"},
|
| 881 |
|
|
}
|
| 882 |
|
|
|
| 883 |
|
|
func TestTitle(t *testing.T) {
|
| 884 |
|
|
for _, tt := range TitleTests {
|
| 885 |
|
|
if s := Title(tt.in); s != tt.out {
|
| 886 |
|
|
t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
|
| 887 |
|
|
}
|
| 888 |
|
|
}
|
| 889 |
|
|
}
|
| 890 |
|
|
|
| 891 |
|
|
var ContainsTests = []struct {
|
| 892 |
|
|
str, substr string
|
| 893 |
|
|
expected bool
|
| 894 |
|
|
}{
|
| 895 |
|
|
{"abc", "bc", true},
|
| 896 |
|
|
{"abc", "bcd", false},
|
| 897 |
|
|
{"abc", "", true},
|
| 898 |
|
|
{"", "a", false},
|
| 899 |
|
|
}
|
| 900 |
|
|
|
| 901 |
|
|
func TestContains(t *testing.T) {
|
| 902 |
|
|
for _, ct := range ContainsTests {
|
| 903 |
|
|
if Contains(ct.str, ct.substr) != ct.expected {
|
| 904 |
|
|
t.Errorf("Contains(%s, %s) = %v, want %v",
|
| 905 |
|
|
ct.str, ct.substr, !ct.expected, ct.expected)
|
| 906 |
|
|
}
|
| 907 |
|
|
}
|
| 908 |
|
|
}
|
| 909 |
|
|
|
| 910 |
|
|
var ContainsAnyTests = []struct {
|
| 911 |
|
|
str, substr string
|
| 912 |
|
|
expected bool
|
| 913 |
|
|
}{
|
| 914 |
|
|
{"", "", false},
|
| 915 |
|
|
{"", "a", false},
|
| 916 |
|
|
{"", "abc", false},
|
| 917 |
|
|
{"a", "", false},
|
| 918 |
|
|
{"a", "a", true},
|
| 919 |
|
|
{"aaa", "a", true},
|
| 920 |
|
|
{"abc", "xyz", false},
|
| 921 |
|
|
{"abc", "xcz", true},
|
| 922 |
|
|
{"a☺b☻c☹d", "uvw☻xyz", true},
|
| 923 |
|
|
{"aRegExp*", ".(|)*+?^$[]", true},
|
| 924 |
|
|
{dots + dots + dots, " ", false},
|
| 925 |
|
|
}
|
| 926 |
|
|
|
| 927 |
|
|
func TestContainsAny(t *testing.T) {
|
| 928 |
|
|
for _, ct := range ContainsAnyTests {
|
| 929 |
|
|
if ContainsAny(ct.str, ct.substr) != ct.expected {
|
| 930 |
|
|
t.Errorf("ContainsAny(%s, %s) = %v, want %v",
|
| 931 |
|
|
ct.str, ct.substr, !ct.expected, ct.expected)
|
| 932 |
|
|
}
|
| 933 |
|
|
}
|
| 934 |
|
|
}
|
| 935 |
|
|
|
| 936 |
|
|
var ContainsRuneTests = []struct {
|
| 937 |
|
|
str string
|
| 938 |
|
|
r rune
|
| 939 |
|
|
expected bool
|
| 940 |
|
|
}{
|
| 941 |
|
|
{"", 'a', false},
|
| 942 |
|
|
{"a", 'a', true},
|
| 943 |
|
|
{"aaa", 'a', true},
|
| 944 |
|
|
{"abc", 'y', false},
|
| 945 |
|
|
{"abc", 'c', true},
|
| 946 |
|
|
{"a☺b☻c☹d", 'x', false},
|
| 947 |
|
|
{"a☺b☻c☹d", '☻', true},
|
| 948 |
|
|
{"aRegExp*", '*', true},
|
| 949 |
|
|
}
|
| 950 |
|
|
|
| 951 |
|
|
func TestContainsRune(t *testing.T) {
|
| 952 |
|
|
for _, ct := range ContainsRuneTests {
|
| 953 |
|
|
if ContainsRune(ct.str, ct.r) != ct.expected {
|
| 954 |
|
|
t.Errorf("ContainsRune(%s, %s) = %v, want %v",
|
| 955 |
|
|
ct.str, ct.r, !ct.expected, ct.expected)
|
| 956 |
|
|
}
|
| 957 |
|
|
}
|
| 958 |
|
|
}
|
| 959 |
|
|
|
| 960 |
|
|
var EqualFoldTests = []struct {
|
| 961 |
|
|
s, t string
|
| 962 |
|
|
out bool
|
| 963 |
|
|
}{
|
| 964 |
|
|
{"abc", "abc", true},
|
| 965 |
|
|
{"ABcd", "ABcd", true},
|
| 966 |
|
|
{"123abc", "123ABC", true},
|
| 967 |
|
|
{"αβδ", "ΑΒΔ", true},
|
| 968 |
|
|
{"abc", "xyz", false},
|
| 969 |
|
|
{"abc", "XYZ", false},
|
| 970 |
|
|
{"abcdefghijk", "abcdefghijX", false},
|
| 971 |
|
|
{"abcdefghijk", "abcdefghij\u212A", true},
|
| 972 |
|
|
{"abcdefghijK", "abcdefghij\u212A", true},
|
| 973 |
|
|
{"abcdefghijkz", "abcdefghij\u212Ay", false},
|
| 974 |
|
|
{"abcdefghijKz", "abcdefghij\u212Ay", false},
|
| 975 |
|
|
}
|
| 976 |
|
|
|
| 977 |
|
|
func TestEqualFold(t *testing.T) {
|
| 978 |
|
|
for _, tt := range EqualFoldTests {
|
| 979 |
|
|
if out := EqualFold(tt.s, tt.t); out != tt.out {
|
| 980 |
|
|
t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
|
| 981 |
|
|
}
|
| 982 |
|
|
if out := EqualFold(tt.t, tt.s); out != tt.out {
|
| 983 |
|
|
t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
|
| 984 |
|
|
}
|
| 985 |
|
|
}
|
| 986 |
|
|
}
|