github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/bytes/buffer_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 bytes_test 6 7 import ( 8 . "bytes" 9 "io" 10 "math/rand" 11 "runtime" 12 "testing" 13 "unicode/utf8" 14 ) 15 16 const N = 10000 // make this bigger for a larger (and slower) test 17 var data string // test data for write tests 18 var testBytes []byte // test data; same as data but as a slice. 19 20 func init() { 21 testBytes = make([]byte, N) 22 for i := 0; i < N; i++ { 23 testBytes[i] = 'a' + byte(i%26) 24 } 25 data = string(testBytes) 26 } 27 28 // Verify that contents of buf match the string s. 29 func check(t *testing.T, testname string, buf *Buffer, s string) { 30 bytes := buf.Bytes() 31 str := buf.String() 32 if buf.Len() != len(bytes) { 33 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes)) 34 } 35 36 if buf.Len() != len(str) { 37 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str)) 38 } 39 40 if buf.Len() != len(s) { 41 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s)) 42 } 43 44 if string(bytes) != s { 45 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s) 46 } 47 } 48 49 // Fill buf through n writes of string fus. 50 // The initial contents of buf corresponds to the string s; 51 // the result is the final contents of buf returned as a string. 52 func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string { 53 check(t, testname+" (fill 1)", buf, s) 54 for ; n > 0; n-- { 55 m, err := buf.WriteString(fus) 56 if m != len(fus) { 57 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus)) 58 } 59 if err != nil { 60 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) 61 } 62 s += fus 63 check(t, testname+" (fill 4)", buf, s) 64 } 65 return s 66 } 67 68 // Fill buf through n writes of byte slice fub. 69 // The initial contents of buf corresponds to the string s; 70 // the result is the final contents of buf returned as a string. 71 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string { 72 check(t, testname+" (fill 1)", buf, s) 73 for ; n > 0; n-- { 74 m, err := buf.Write(fub) 75 if m != len(fub) { 76 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub)) 77 } 78 if err != nil { 79 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) 80 } 81 s += string(fub) 82 check(t, testname+" (fill 4)", buf, s) 83 } 84 return s 85 } 86 87 func TestNewBuffer(t *testing.T) { 88 buf := NewBuffer(testBytes) 89 check(t, "NewBuffer", buf, data) 90 } 91 92 func TestNewBufferString(t *testing.T) { 93 buf := NewBufferString(data) 94 check(t, "NewBufferString", buf, data) 95 } 96 97 // Empty buf through repeated reads into fub. 98 // The initial contents of buf corresponds to the string s. 99 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) { 100 check(t, testname+" (empty 1)", buf, s) 101 102 for { 103 n, err := buf.Read(fub) 104 if n == 0 { 105 break 106 } 107 if err != nil { 108 t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err) 109 } 110 s = s[n:] 111 check(t, testname+" (empty 3)", buf, s) 112 } 113 114 check(t, testname+" (empty 4)", buf, "") 115 } 116 117 func TestBasicOperations(t *testing.T) { 118 var buf Buffer 119 120 for i := 0; i < 5; i++ { 121 check(t, "TestBasicOperations (1)", &buf, "") 122 123 buf.Reset() 124 check(t, "TestBasicOperations (2)", &buf, "") 125 126 buf.Truncate(0) 127 check(t, "TestBasicOperations (3)", &buf, "") 128 129 n, err := buf.Write([]byte(data[0:1])) 130 if n != 1 { 131 t.Errorf("wrote 1 byte, but n == %d", n) 132 } 133 if err != nil { 134 t.Errorf("err should always be nil, but err == %s", err) 135 } 136 check(t, "TestBasicOperations (4)", &buf, "a") 137 138 buf.WriteByte(data[1]) 139 check(t, "TestBasicOperations (5)", &buf, "ab") 140 141 n, err = buf.Write([]byte(data[2:26])) 142 if n != 24 { 143 t.Errorf("wrote 25 bytes, but n == %d", n) 144 } 145 check(t, "TestBasicOperations (6)", &buf, string(data[0:26])) 146 147 buf.Truncate(26) 148 check(t, "TestBasicOperations (7)", &buf, string(data[0:26])) 149 150 buf.Truncate(20) 151 check(t, "TestBasicOperations (8)", &buf, string(data[0:20])) 152 153 empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5)) 154 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)) 155 156 buf.WriteByte(data[1]) 157 c, err := buf.ReadByte() 158 if err != nil { 159 t.Error("ReadByte unexpected eof") 160 } 161 if c != data[1] { 162 t.Errorf("ReadByte wrong value c=%v", c) 163 } 164 c, err = buf.ReadByte() 165 if err == nil { 166 t.Error("ReadByte unexpected not eof") 167 } 168 } 169 } 170 171 func TestLargeStringWrites(t *testing.T) { 172 var buf Buffer 173 limit := 30 174 if testing.Short() { 175 limit = 9 176 } 177 for i := 3; i < limit; i += 3 { 178 s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data) 179 empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i)) 180 } 181 check(t, "TestLargeStringWrites (3)", &buf, "") 182 } 183 184 func TestLargeByteWrites(t *testing.T) { 185 var buf Buffer 186 limit := 30 187 if testing.Short() { 188 limit = 9 189 } 190 for i := 3; i < limit; i += 3 { 191 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes) 192 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i)) 193 } 194 check(t, "TestLargeByteWrites (3)", &buf, "") 195 } 196 197 func TestLargeStringReads(t *testing.T) { 198 var buf Buffer 199 for i := 3; i < 30; i += 3 { 200 s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i]) 201 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) 202 } 203 check(t, "TestLargeStringReads (3)", &buf, "") 204 } 205 206 func TestLargeByteReads(t *testing.T) { 207 var buf Buffer 208 for i := 3; i < 30; i += 3 { 209 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 210 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) 211 } 212 check(t, "TestLargeByteReads (3)", &buf, "") 213 } 214 215 func TestMixedReadsAndWrites(t *testing.T) { 216 var buf Buffer 217 s := "" 218 for i := 0; i < 50; i++ { 219 wlen := rand.Intn(len(data)) 220 if i%2 == 0 { 221 s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen]) 222 } else { 223 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen]) 224 } 225 226 rlen := rand.Intn(len(data)) 227 fub := make([]byte, rlen) 228 n, _ := buf.Read(fub) 229 s = s[n:] 230 } 231 empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len())) 232 } 233 234 func TestNil(t *testing.T) { 235 var b *Buffer 236 if b.String() != "<nil>" { 237 t.Errorf("expected <nil>; got %q", b.String()) 238 } 239 } 240 241 func TestReadFrom(t *testing.T) { 242 var buf Buffer 243 for i := 3; i < 30; i += 3 { 244 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 245 var b Buffer 246 b.ReadFrom(&buf) 247 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data))) 248 } 249 } 250 251 func TestWriteTo(t *testing.T) { 252 var buf Buffer 253 for i := 3; i < 30; i += 3 { 254 s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 255 var b Buffer 256 buf.WriteTo(&b) 257 empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data))) 258 } 259 } 260 261 func TestRuneIO(t *testing.T) { 262 const NRune = 1000 263 // Built a test slice while we write the data 264 b := make([]byte, utf8.UTFMax*NRune) 265 var buf Buffer 266 n := 0 267 for r := rune(0); r < NRune; r++ { 268 size := utf8.EncodeRune(b[n:], r) 269 nbytes, err := buf.WriteRune(r) 270 if err != nil { 271 t.Fatalf("WriteRune(%U) error: %s", r, err) 272 } 273 if nbytes != size { 274 t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes) 275 } 276 n += size 277 } 278 b = b[0:n] 279 280 // Check the resulting bytes 281 if !Equal(buf.Bytes(), b) { 282 t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b) 283 } 284 285 p := make([]byte, utf8.UTFMax) 286 // Read it back with ReadRune 287 for r := rune(0); r < NRune; r++ { 288 size := utf8.EncodeRune(p, r) 289 nr, nbytes, err := buf.ReadRune() 290 if nr != r || nbytes != size || err != nil { 291 t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err) 292 } 293 } 294 295 // Check that UnreadRune works 296 buf.Reset() 297 buf.Write(b) 298 for r := rune(0); r < NRune; r++ { 299 r1, size, _ := buf.ReadRune() 300 if err := buf.UnreadRune(); err != nil { 301 t.Fatalf("UnreadRune(%U) got error %q", r, err) 302 } 303 r2, nbytes, err := buf.ReadRune() 304 if r1 != r2 || r1 != r || nbytes != size || err != nil { 305 t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err) 306 } 307 } 308 } 309 310 func TestNext(t *testing.T) { 311 b := []byte{0, 1, 2, 3, 4} 312 tmp := make([]byte, 5) 313 for i := 0; i <= 5; i++ { 314 for j := i; j <= 5; j++ { 315 for k := 0; k <= 6; k++ { 316 // 0 <= i <= j <= 5; 0 <= k <= 6 317 // Check that if we start with a buffer 318 // of length j at offset i and ask for 319 // Next(k), we get the right bytes. 320 buf := NewBuffer(b[0:j]) 321 n, _ := buf.Read(tmp[0:i]) 322 if n != i { 323 t.Fatalf("Read %d returned %d", i, n) 324 } 325 bb := buf.Next(k) 326 want := k 327 if want > j-i { 328 want = j - i 329 } 330 if len(bb) != want { 331 t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb)) 332 } 333 for l, v := range bb { 334 if v != byte(l+i) { 335 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i) 336 } 337 } 338 } 339 } 340 } 341 } 342 343 var readBytesTests = []struct { 344 buffer string 345 delim byte 346 expected []string 347 err error 348 }{ 349 {"", 0, []string{""}, io.EOF}, 350 {"a\x00", 0, []string{"a\x00"}, nil}, 351 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil}, 352 {"hello\x01world", 1, []string{"hello\x01"}, nil}, 353 {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF}, 354 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil}, 355 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF}, 356 } 357 358 func TestReadBytes(t *testing.T) { 359 for _, test := range readBytesTests { 360 buf := NewBufferString(test.buffer) 361 var err error 362 for _, expected := range test.expected { 363 var bytes []byte 364 bytes, err = buf.ReadBytes(test.delim) 365 if string(bytes) != expected { 366 t.Errorf("expected %q, got %q", expected, bytes) 367 } 368 if err != nil { 369 break 370 } 371 } 372 if err != test.err { 373 t.Errorf("expected error %v, got %v", test.err, err) 374 } 375 } 376 } 377 378 func TestReadString(t *testing.T) { 379 for _, test := range readBytesTests { 380 buf := NewBufferString(test.buffer) 381 var err error 382 for _, expected := range test.expected { 383 var s string 384 s, err = buf.ReadString(test.delim) 385 if s != expected { 386 t.Errorf("expected %q, got %q", expected, s) 387 } 388 if err != nil { 389 break 390 } 391 } 392 if err != test.err { 393 t.Errorf("expected error %v, got %v", test.err, err) 394 } 395 } 396 } 397 398 func BenchmarkReadString(b *testing.B) { 399 const n = 32 << 10 400 401 data := make([]byte, n) 402 data[n-1] = 'x' 403 b.SetBytes(int64(n)) 404 for i := 0; i < b.N; i++ { 405 buf := NewBuffer(data) 406 _, err := buf.ReadString('x') 407 if err != nil { 408 b.Fatal(err) 409 } 410 } 411 } 412 413 func TestGrow(t *testing.T) { 414 x := []byte{'x'} 415 y := []byte{'y'} 416 tmp := make([]byte, 72) 417 for _, startLen := range []int{0, 100, 1000, 10000, 100000} { 418 xBytes := Repeat(x, startLen) 419 for _, growLen := range []int{0, 100, 1000, 10000, 100000} { 420 buf := NewBuffer(xBytes) 421 // If we read, this affects buf.off, which is good to test. 422 readBytes, _ := buf.Read(tmp) 423 buf.Grow(growLen) 424 yBytes := Repeat(y, growLen) 425 // Check no allocation occurs in write, as long as we're single-threaded. 426 var m1, m2 runtime.MemStats 427 runtime.ReadMemStats(&m1) 428 buf.Write(yBytes) 429 runtime.ReadMemStats(&m2) 430 if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { 431 t.Errorf("allocation occurred during write") 432 } 433 // Check that buffer has correct data. 434 if !Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) { 435 t.Errorf("bad initial data at %d %d", startLen, growLen) 436 } 437 if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) { 438 t.Errorf("bad written data at %d %d", startLen, growLen) 439 } 440 } 441 } 442 } 443 444 // Was a bug: used to give EOF reading empty slice at EOF. 445 func TestReadEmptyAtEOF(t *testing.T) { 446 b := new(Buffer) 447 slice := make([]byte, 0) 448 n, err := b.Read(slice) 449 if err != nil { 450 t.Errorf("read error: %v", err) 451 } 452 if n != 0 { 453 t.Errorf("wrong count; got %d want 0", n) 454 } 455 } 456 457 func TestUnreadByte(t *testing.T) { 458 b := new(Buffer) 459 b.WriteString("abcdefghijklmnopqrstuvwxyz") 460 461 _, err := b.ReadBytes('m') 462 if err != nil { 463 t.Fatalf("ReadBytes: %v", err) 464 } 465 466 err = b.UnreadByte() 467 if err != nil { 468 t.Fatalf("UnreadByte: %v", err) 469 } 470 c, err := b.ReadByte() 471 if err != nil { 472 t.Fatalf("ReadByte: %v", err) 473 } 474 if c != 'm' { 475 t.Errorf("ReadByte = %q; want %q", c, 'm') 476 } 477 } 478 479 // Tests that we occasionally compact. Issue 5154. 480 func TestBufferGrowth(t *testing.T) { 481 var b Buffer 482 buf := make([]byte, 1024) 483 b.Write(buf[0:1]) 484 var cap0 int 485 for i := 0; i < 5<<10; i++ { 486 b.Write(buf) 487 b.Read(buf) 488 if i == 0 { 489 cap0 = b.Cap() 490 } 491 } 492 cap1 := b.Cap() 493 // (*Buffer).grow allows for 2x capacity slop before sliding, 494 // so set our error threshold at 3x. 495 if cap1 > cap0*3 { 496 t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0) 497 } 498 } 499 500 // From Issue 5154. 501 func BenchmarkBufferNotEmptyWriteRead(b *testing.B) { 502 buf := make([]byte, 1024) 503 for i := 0; i < b.N; i++ { 504 var b Buffer 505 b.Write(buf[0:1]) 506 for i := 0; i < 5<<10; i++ { 507 b.Write(buf) 508 b.Read(buf) 509 } 510 } 511 } 512 513 // Check that we don't compact too often. From Issue 5154. 514 func BenchmarkBufferFullSmallReads(b *testing.B) { 515 buf := make([]byte, 1024) 516 for i := 0; i < b.N; i++ { 517 var b Buffer 518 b.Write(buf) 519 for b.Len()+20 < b.Cap() { 520 b.Write(buf[:10]) 521 } 522 for i := 0; i < 5<<10; i++ { 523 b.Read(buf[:1]) 524 b.Write(buf[:1]) 525 } 526 } 527 }