buffer_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. package http2
  6. import (
  7. "io"
  8. "reflect"
  9. "testing"
  10. )
  11. var bufferReadTests = []struct {
  12. buf buffer
  13. read, wn int
  14. werr error
  15. wp []byte
  16. wbuf buffer
  17. }{
  18. {
  19. buffer{[]byte{'a', 0}, 0, 1, false, nil},
  20. 5, 1, nil, []byte{'a'},
  21. buffer{[]byte{'a', 0}, 1, 1, false, nil},
  22. },
  23. {
  24. buffer{[]byte{'a', 0}, 0, 1, true, io.EOF},
  25. 5, 1, io.EOF, []byte{'a'},
  26. buffer{[]byte{'a', 0}, 1, 1, true, io.EOF},
  27. },
  28. {
  29. buffer{[]byte{0, 'a'}, 1, 2, false, nil},
  30. 5, 1, nil, []byte{'a'},
  31. buffer{[]byte{0, 'a'}, 2, 2, false, nil},
  32. },
  33. {
  34. buffer{[]byte{0, 'a'}, 1, 2, true, io.EOF},
  35. 5, 1, io.EOF, []byte{'a'},
  36. buffer{[]byte{0, 'a'}, 2, 2, true, io.EOF},
  37. },
  38. {
  39. buffer{[]byte{}, 0, 0, false, nil},
  40. 5, 0, errReadEmpty, []byte{},
  41. buffer{[]byte{}, 0, 0, false, nil},
  42. },
  43. {
  44. buffer{[]byte{}, 0, 0, true, io.EOF},
  45. 5, 0, io.EOF, []byte{},
  46. buffer{[]byte{}, 0, 0, true, io.EOF},
  47. },
  48. }
  49. func TestBufferRead(t *testing.T) {
  50. for i, tt := range bufferReadTests {
  51. read := make([]byte, tt.read)
  52. n, err := tt.buf.Read(read)
  53. if n != tt.wn {
  54. t.Errorf("#%d: wn = %d want %d", i, n, tt.wn)
  55. continue
  56. }
  57. if err != tt.werr {
  58. t.Errorf("#%d: werr = %v want %v", i, err, tt.werr)
  59. continue
  60. }
  61. read = read[:n]
  62. if !reflect.DeepEqual(read, tt.wp) {
  63. t.Errorf("#%d: read = %+v want %+v", i, read, tt.wp)
  64. }
  65. if !reflect.DeepEqual(tt.buf, tt.wbuf) {
  66. t.Errorf("#%d: buf = %+v want %+v", i, tt.buf, tt.wbuf)
  67. }
  68. }
  69. }