buffer_test.go 17 KB

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