fixed_buffer_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 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 http2
  5. import (
  6. "reflect"
  7. "testing"
  8. )
  9. var bufferReadTests = []struct {
  10. buf fixedBuffer
  11. read, wn int
  12. werr error
  13. wp []byte
  14. wbuf fixedBuffer
  15. }{
  16. {
  17. fixedBuffer{[]byte{'a', 0}, 0, 1},
  18. 5, 1, nil, []byte{'a'},
  19. fixedBuffer{[]byte{'a', 0}, 0, 0},
  20. },
  21. {
  22. fixedBuffer{[]byte{0, 'a'}, 1, 2},
  23. 5, 1, nil, []byte{'a'},
  24. fixedBuffer{[]byte{0, 'a'}, 0, 0},
  25. },
  26. {
  27. fixedBuffer{[]byte{'a', 'b'}, 0, 2},
  28. 1, 1, nil, []byte{'a'},
  29. fixedBuffer{[]byte{'a', 'b'}, 1, 2},
  30. },
  31. {
  32. fixedBuffer{[]byte{}, 0, 0},
  33. 5, 0, errReadEmpty, []byte{},
  34. fixedBuffer{[]byte{}, 0, 0},
  35. },
  36. }
  37. func TestBufferRead(t *testing.T) {
  38. for i, tt := range bufferReadTests {
  39. read := make([]byte, tt.read)
  40. n, err := tt.buf.Read(read)
  41. if n != tt.wn {
  42. t.Errorf("#%d: wn = %d want %d", i, n, tt.wn)
  43. continue
  44. }
  45. if err != tt.werr {
  46. t.Errorf("#%d: werr = %v want %v", i, err, tt.werr)
  47. continue
  48. }
  49. read = read[:n]
  50. if !reflect.DeepEqual(read, tt.wp) {
  51. t.Errorf("#%d: read = %+v want %+v", i, read, tt.wp)
  52. }
  53. if !reflect.DeepEqual(tt.buf, tt.wbuf) {
  54. t.Errorf("#%d: buf = %+v want %+v", i, tt.buf, tt.wbuf)
  55. }
  56. }
  57. }
  58. var bufferWriteTests = []struct {
  59. buf fixedBuffer
  60. write, wn int
  61. werr error
  62. wbuf fixedBuffer
  63. }{
  64. {
  65. buf: fixedBuffer{
  66. buf: []byte{},
  67. },
  68. wbuf: fixedBuffer{
  69. buf: []byte{},
  70. },
  71. },
  72. {
  73. buf: fixedBuffer{
  74. buf: []byte{1, 'a'},
  75. },
  76. write: 1,
  77. wn: 1,
  78. wbuf: fixedBuffer{
  79. buf: []byte{0, 'a'},
  80. w: 1,
  81. },
  82. },
  83. {
  84. buf: fixedBuffer{
  85. buf: []byte{'a', 1},
  86. r: 1,
  87. w: 1,
  88. },
  89. write: 2,
  90. wn: 2,
  91. wbuf: fixedBuffer{
  92. buf: []byte{0, 0},
  93. w: 2,
  94. },
  95. },
  96. {
  97. buf: fixedBuffer{
  98. buf: []byte{},
  99. },
  100. write: 5,
  101. werr: errWriteFull,
  102. wbuf: fixedBuffer{
  103. buf: []byte{},
  104. },
  105. },
  106. }
  107. func TestBufferWrite(t *testing.T) {
  108. for i, tt := range bufferWriteTests {
  109. n, err := tt.buf.Write(make([]byte, tt.write))
  110. if n != tt.wn {
  111. t.Errorf("#%d: wrote %d bytes; want %d", i, n, tt.wn)
  112. continue
  113. }
  114. if err != tt.werr {
  115. t.Errorf("#%d: error = %v; want %v", i, err, tt.werr)
  116. continue
  117. }
  118. if !reflect.DeepEqual(tt.buf, tt.wbuf) {
  119. t.Errorf("#%d: buf = %+v; want %+v", i, tt.buf, tt.wbuf)
  120. }
  121. }
  122. }