PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/bytes/buffer_test.go

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