github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/unicode/utf8/utf8_test.go (about) 1 // 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 utf8_test 6 7 import ( 8 "bytes" 9 "testing" 10 "unicode" 11 . "unicode/utf8" 12 ) 13 14 // Validate the constants redefined from unicode. 15 func init() { 16 if MaxRune != unicode.MaxRune { 17 panic("utf8.MaxRune is wrong") 18 } 19 if RuneError != unicode.ReplacementChar { 20 panic("utf8.RuneError is wrong") 21 } 22 } 23 24 // Validate the constants redefined from unicode. 25 func TestConstants(t *testing.T) { 26 if MaxRune != unicode.MaxRune { 27 t.Errorf("utf8.MaxRune is wrong: %x should be %x", MaxRune, unicode.MaxRune) 28 } 29 if RuneError != unicode.ReplacementChar { 30 t.Errorf("utf8.RuneError is wrong: %x should be %x", RuneError, unicode.ReplacementChar) 31 } 32 } 33 34 type Utf8Map struct { 35 r rune 36 str string 37 } 38 39 var utf8map = []Utf8Map{ 40 {0x0000, "\x00"}, 41 {0x0001, "\x01"}, 42 {0x007e, "\x7e"}, 43 {0x007f, "\x7f"}, 44 {0x0080, "\xc2\x80"}, 45 {0x0081, "\xc2\x81"}, 46 {0x00bf, "\xc2\xbf"}, 47 {0x00c0, "\xc3\x80"}, 48 {0x00c1, "\xc3\x81"}, 49 {0x00c8, "\xc3\x88"}, 50 {0x00d0, "\xc3\x90"}, 51 {0x00e0, "\xc3\xa0"}, 52 {0x00f0, "\xc3\xb0"}, 53 {0x00f8, "\xc3\xb8"}, 54 {0x00ff, "\xc3\xbf"}, 55 {0x0100, "\xc4\x80"}, 56 {0x07ff, "\xdf\xbf"}, 57 {0x0800, "\xe0\xa0\x80"}, 58 {0x0801, "\xe0\xa0\x81"}, 59 {0xd7ff, "\xed\x9f\xbf"}, // last code point before surrogate half. 60 {0xe000, "\xee\x80\x80"}, // first code point after surrogate half. 61 {0xfffe, "\xef\xbf\xbe"}, 62 {0xffff, "\xef\xbf\xbf"}, 63 {0x10000, "\xf0\x90\x80\x80"}, 64 {0x10001, "\xf0\x90\x80\x81"}, 65 {0x10fffe, "\xf4\x8f\xbf\xbe"}, 66 {0x10ffff, "\xf4\x8f\xbf\xbf"}, 67 {0xFFFD, "\xef\xbf\xbd"}, 68 } 69 70 var surrogateMap = []Utf8Map{ 71 {0xd800, "\xed\xa0\x80"}, // surrogate min decodes to (RuneError, 1) 72 {0xdfff, "\xed\xbf\xbf"}, // surrogate max decodes to (RuneError, 1) 73 } 74 75 var testStrings = []string{ 76 "", 77 "abcd", 78 "☺☻☹", 79 "日a本b語ç日ð本Ê語þ日¥本¼語i日©", 80 "日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©", 81 "\x80\x80\x80\x80", 82 } 83 84 func TestFullRune(t *testing.T) { 85 for _, m := range utf8map { 86 b := []byte(m.str) 87 if !FullRune(b) { 88 t.Errorf("FullRune(%q) (%U) = false, want true", b, m.r) 89 } 90 s := m.str 91 if !FullRuneInString(s) { 92 t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.r) 93 } 94 b1 := b[0 : len(b)-1] 95 if FullRune(b1) { 96 t.Errorf("FullRune(%q) = true, want false", b1) 97 } 98 s1 := string(b1) 99 if FullRuneInString(s1) { 100 t.Errorf("FullRune(%q) = true, want false", s1) 101 } 102 } 103 for _, s := range []string{"\xc0", "\xc1"} { 104 b := []byte(s) 105 if !FullRune(b) { 106 t.Errorf("FullRune(%q) = false, want true", s) 107 } 108 if !FullRuneInString(s) { 109 t.Errorf("FullRuneInString(%q) = false, want true", s) 110 } 111 } 112 } 113 114 func TestEncodeRune(t *testing.T) { 115 for _, m := range utf8map { 116 b := []byte(m.str) 117 var buf [10]byte 118 n := EncodeRune(buf[0:], m.r) 119 b1 := buf[0:n] 120 if !bytes.Equal(b, b1) { 121 t.Errorf("EncodeRune(%#04x) = %q want %q", m.r, b1, b) 122 } 123 } 124 } 125 126 func TestDecodeRune(t *testing.T) { 127 for _, m := range utf8map { 128 b := []byte(m.str) 129 r, size := DecodeRune(b) 130 if r != m.r || size != len(b) { 131 t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b)) 132 } 133 s := m.str 134 r, size = DecodeRuneInString(s) 135 if r != m.r || size != len(b) { 136 t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b)) 137 } 138 139 // there's an extra byte that bytes left behind - make sure trailing byte works 140 r, size = DecodeRune(b[0:cap(b)]) 141 if r != m.r || size != len(b) { 142 t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b)) 143 } 144 s = m.str + "\x00" 145 r, size = DecodeRuneInString(s) 146 if r != m.r || size != len(b) { 147 t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b)) 148 } 149 150 // make sure missing bytes fail 151 wantsize := 1 152 if wantsize >= len(b) { 153 wantsize = 0 154 } 155 r, size = DecodeRune(b[0 : len(b)-1]) 156 if r != RuneError || size != wantsize { 157 t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], r, size, RuneError, wantsize) 158 } 159 s = m.str[0 : len(m.str)-1] 160 r, size = DecodeRuneInString(s) 161 if r != RuneError || size != wantsize { 162 t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, wantsize) 163 } 164 165 // make sure bad sequences fail 166 if len(b) == 1 { 167 b[0] = 0x80 168 } else { 169 b[len(b)-1] = 0x7F 170 } 171 r, size = DecodeRune(b) 172 if r != RuneError || size != 1 { 173 t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, RuneError, 1) 174 } 175 s = string(b) 176 r, size = DecodeRuneInString(s) 177 if r != RuneError || size != 1 { 178 t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, 1) 179 } 180 181 } 182 } 183 184 func TestDecodeSurrogateRune(t *testing.T) { 185 for _, m := range surrogateMap { 186 b := []byte(m.str) 187 r, size := DecodeRune(b) 188 if r != RuneError || size != 1 { 189 t.Errorf("DecodeRune(%q) = %x, %d want %x, %d", b, r, size, RuneError, 1) 190 } 191 s := m.str 192 r, size = DecodeRuneInString(s) 193 if r != RuneError || size != 1 { 194 t.Errorf("DecodeRuneInString(%q) = %x, %d want %x, %d", b, r, size, RuneError, 1) 195 } 196 } 197 } 198 199 // Check that DecodeRune and DecodeLastRune correspond to 200 // the equivalent range loop. 201 func TestSequencing(t *testing.T) { 202 for _, ts := range testStrings { 203 for _, m := range utf8map { 204 for _, s := range []string{ts + m.str, m.str + ts, ts + m.str + ts} { 205 testSequence(t, s) 206 } 207 } 208 } 209 } 210 211 // Check that a range loop and a []int conversion visit the same runes. 212 // Not really a test of this package, but the assumption is used here and 213 // it's good to verify 214 func TestIntConversion(t *testing.T) { 215 for _, ts := range testStrings { 216 runes := []rune(ts) 217 if RuneCountInString(ts) != len(runes) { 218 t.Errorf("%q: expected %d runes; got %d", ts, len(runes), RuneCountInString(ts)) 219 break 220 } 221 i := 0 222 for _, r := range ts { 223 if r != runes[i] { 224 t.Errorf("%q[%d]: expected %c (%U); got %c (%U)", ts, i, runes[i], runes[i], r, r) 225 } 226 i++ 227 } 228 } 229 } 230 231 func testSequence(t *testing.T, s string) { 232 type info struct { 233 index int 234 r rune 235 } 236 index := make([]info, len(s)) 237 b := []byte(s) 238 si := 0 239 j := 0 240 for i, r := range s { 241 if si != i { 242 t.Errorf("Sequence(%q) mismatched index %d, want %d", s, si, i) 243 return 244 } 245 index[j] = info{i, r} 246 j++ 247 r1, size1 := DecodeRune(b[i:]) 248 if r != r1 { 249 t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], r1, r) 250 return 251 } 252 r2, size2 := DecodeRuneInString(s[i:]) 253 if r != r2 { 254 t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], r2, r) 255 return 256 } 257 if size1 != size2 { 258 t.Errorf("DecodeRune/DecodeRuneInString(%q) size mismatch %d/%d", s[i:], size1, size2) 259 return 260 } 261 si += size1 262 } 263 j-- 264 for si = len(s); si > 0; { 265 r1, size1 := DecodeLastRune(b[0:si]) 266 r2, size2 := DecodeLastRuneInString(s[0:si]) 267 if size1 != size2 { 268 t.Errorf("DecodeLastRune/DecodeLastRuneInString(%q, %d) size mismatch %d/%d", s, si, size1, size2) 269 return 270 } 271 if r1 != index[j].r { 272 t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, r1, index[j].r) 273 return 274 } 275 if r2 != index[j].r { 276 t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, r2, index[j].r) 277 return 278 } 279 si -= size1 280 if si != index[j].index { 281 t.Errorf("DecodeLastRune(%q) index mismatch at %d, want %d", s, si, index[j].index) 282 return 283 } 284 j-- 285 } 286 if si != 0 { 287 t.Errorf("DecodeLastRune(%q) finished at %d, not 0", s, si) 288 } 289 } 290 291 // Check that negative runes encode as U+FFFD. 292 func TestNegativeRune(t *testing.T) { 293 errorbuf := make([]byte, UTFMax) 294 errorbuf = errorbuf[0:EncodeRune(errorbuf, RuneError)] 295 buf := make([]byte, UTFMax) 296 buf = buf[0:EncodeRune(buf, -1)] 297 if !bytes.Equal(buf, errorbuf) { 298 t.Errorf("incorrect encoding [% x] for -1; expected [% x]", buf, errorbuf) 299 } 300 } 301 302 type RuneCountTest struct { 303 in string 304 out int 305 } 306 307 var runecounttests = []RuneCountTest{ 308 {"abcd", 4}, 309 {"☺☻☹", 3}, 310 {"1,2,3,4", 7}, 311 {"\xe2\x00", 2}, 312 {"\xe2\x80", 2}, 313 {"a\xe2\x80", 3}, 314 } 315 316 func TestRuneCount(t *testing.T) { 317 for _, tt := range runecounttests { 318 if out := RuneCountInString(tt.in); out != tt.out { 319 t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out) 320 } 321 if out := RuneCount([]byte(tt.in)); out != tt.out { 322 t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out) 323 } 324 } 325 } 326 327 type RuneLenTest struct { 328 r rune 329 size int 330 } 331 332 var runelentests = []RuneLenTest{ 333 {0, 1}, 334 {'e', 1}, 335 {'é', 2}, 336 {'☺', 3}, 337 {RuneError, 3}, 338 {MaxRune, 4}, 339 {0xD800, -1}, 340 {0xDFFF, -1}, 341 {MaxRune + 1, -1}, 342 {-1, -1}, 343 } 344 345 func TestRuneLen(t *testing.T) { 346 for _, tt := range runelentests { 347 if size := RuneLen(tt.r); size != tt.size { 348 t.Errorf("RuneLen(%#U) = %d, want %d", tt.r, size, tt.size) 349 } 350 } 351 } 352 353 type ValidTest struct { 354 in string 355 out bool 356 } 357 358 var validTests = []ValidTest{ 359 {"", true}, 360 {"a", true}, 361 {"abc", true}, 362 {"Ж", true}, 363 {"ЖЖ", true}, 364 {"брэд-ЛГТМ", true}, 365 {"☺☻☹", true}, 366 {"aa\xe2", false}, 367 {string([]byte{66, 250}), false}, 368 {string([]byte{66, 250, 67}), false}, 369 {"a\uFFFDb", true}, 370 {string("\xF4\x8F\xBF\xBF"), true}, // U+10FFFF 371 {string("\xF4\x90\x80\x80"), false}, // U+10FFFF+1; out of range 372 {string("\xF7\xBF\xBF\xBF"), false}, // 0x1FFFFF; out of range 373 {string("\xFB\xBF\xBF\xBF\xBF"), false}, // 0x3FFFFFF; out of range 374 {string("\xc0\x80"), false}, // U+0000 encoded in two bytes: incorrect 375 {string("\xed\xa0\x80"), false}, // U+D800 high surrogate (sic) 376 {string("\xed\xbf\xbf"), false}, // U+DFFF low surrogate (sic) 377 } 378 379 func TestValid(t *testing.T) { 380 for _, tt := range validTests { 381 if Valid([]byte(tt.in)) != tt.out { 382 t.Errorf("Valid(%q) = %v; want %v", tt.in, !tt.out, tt.out) 383 } 384 if ValidString(tt.in) != tt.out { 385 t.Errorf("ValidString(%q) = %v; want %v", tt.in, !tt.out, tt.out) 386 } 387 } 388 } 389 390 type ValidRuneTest struct { 391 r rune 392 ok bool 393 } 394 395 var validrunetests = []ValidRuneTest{ 396 {0, true}, 397 {'e', true}, 398 {'é', true}, 399 {'☺', true}, 400 {RuneError, true}, 401 {MaxRune, true}, 402 {0xD7FF, true}, 403 {0xD800, false}, 404 {0xDFFF, false}, 405 {0xE000, true}, 406 {MaxRune + 1, false}, 407 {-1, false}, 408 } 409 410 func TestValidRune(t *testing.T) { 411 for _, tt := range validrunetests { 412 if ok := ValidRune(tt.r); ok != tt.ok { 413 t.Errorf("ValidRune(%#U) = %t, want %t", tt.r, ok, tt.ok) 414 } 415 } 416 } 417 418 func BenchmarkRuneCountTenASCIIChars(b *testing.B) { 419 s := []byte("0123456789") 420 for i := 0; i < b.N; i++ { 421 RuneCount(s) 422 } 423 } 424 425 func BenchmarkRuneCountTenJapaneseChars(b *testing.B) { 426 s := []byte("日本語日本語日本語日") 427 for i := 0; i < b.N; i++ { 428 RuneCount(s) 429 } 430 } 431 432 func BenchmarkRuneCountInStringTenASCIIChars(b *testing.B) { 433 for i := 0; i < b.N; i++ { 434 RuneCountInString("0123456789") 435 } 436 } 437 438 func BenchmarkRuneCountInStringTenJapaneseChars(b *testing.B) { 439 for i := 0; i < b.N; i++ { 440 RuneCountInString("日本語日本語日本語日") 441 } 442 } 443 444 func BenchmarkValidTenASCIIChars(b *testing.B) { 445 s := []byte("0123456789") 446 for i := 0; i < b.N; i++ { 447 Valid(s) 448 } 449 } 450 451 func BenchmarkValidTenJapaneseChars(b *testing.B) { 452 s := []byte("日本語日本語日本語日") 453 for i := 0; i < b.N; i++ { 454 Valid(s) 455 } 456 } 457 458 func BenchmarkValidStringTenASCIIChars(b *testing.B) { 459 for i := 0; i < b.N; i++ { 460 ValidString("0123456789") 461 } 462 } 463 464 func BenchmarkValidStringTenJapaneseChars(b *testing.B) { 465 for i := 0; i < b.N; i++ { 466 ValidString("日本語日本語日本語日") 467 } 468 } 469 470 func BenchmarkEncodeASCIIRune(b *testing.B) { 471 buf := make([]byte, UTFMax) 472 for i := 0; i < b.N; i++ { 473 EncodeRune(buf, 'a') 474 } 475 } 476 477 func BenchmarkEncodeJapaneseRune(b *testing.B) { 478 buf := make([]byte, UTFMax) 479 for i := 0; i < b.N; i++ { 480 EncodeRune(buf, '本') 481 } 482 } 483 484 func BenchmarkDecodeASCIIRune(b *testing.B) { 485 a := []byte{'a'} 486 for i := 0; i < b.N; i++ { 487 DecodeRune(a) 488 } 489 } 490 491 func BenchmarkDecodeJapaneseRune(b *testing.B) { 492 nihon := []byte("本") 493 for i := 0; i < b.N; i++ { 494 DecodeRune(nihon) 495 } 496 } 497 498 func BenchmarkFullASCIIRune(b *testing.B) { 499 a := []byte{'a'} 500 for i := 0; i < b.N; i++ { 501 FullRune(a) 502 } 503 } 504 505 func BenchmarkFullJapaneseRune(b *testing.B) { 506 nihon := []byte("本") 507 for i := 0; i < b.N; i++ { 508 FullRune(nihon) 509 } 510 }