reader_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2012 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. "io/ioutil"
  10. "os"
  11. "sync"
  12. "testing"
  13. )
  14. func TestReader(t *testing.T) {
  15. r := NewReader([]byte("0123456789"))
  16. tests := []struct {
  17. off int64
  18. seek int
  19. n int
  20. want string
  21. wantpos int64
  22. seekerr string
  23. }{
  24. {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
  25. {seek: os.SEEK_SET, off: 1, n: 1, want: "1"},
  26. {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"},
  27. {seek: os.SEEK_SET, off: -1, seekerr: "bytes.Reader.Seek: negative position"},
  28. {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33},
  29. {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1},
  30. {seek: os.SEEK_SET, n: 5, want: "01234"},
  31. {seek: os.SEEK_CUR, n: 5, want: "56789"},
  32. {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"},
  33. }
  34. for i, tt := range tests {
  35. pos, err := r.Seek(tt.off, tt.seek)
  36. if err == nil && tt.seekerr != "" {
  37. t.Errorf("%d. want seek error %q", i, tt.seekerr)
  38. continue
  39. }
  40. if err != nil && err.Error() != tt.seekerr {
  41. t.Errorf("%d. seek error = %q; want %q", i, err.Error(), tt.seekerr)
  42. continue
  43. }
  44. if tt.wantpos != 0 && tt.wantpos != pos {
  45. t.Errorf("%d. pos = %d, want %d", i, pos, tt.wantpos)
  46. }
  47. buf := make([]byte, tt.n)
  48. n, err := r.Read(buf)
  49. if err != nil {
  50. t.Errorf("%d. read = %v", i, err)
  51. continue
  52. }
  53. got := string(buf[:n])
  54. if got != tt.want {
  55. t.Errorf("%d. got %q; want %q", i, got, tt.want)
  56. }
  57. }
  58. }
  59. func TestReadAfterBigSeek(t *testing.T) {
  60. r := NewReader([]byte("0123456789"))
  61. if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil {
  62. t.Fatal(err)
  63. }
  64. if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF {
  65. t.Errorf("Read = %d, %v; want 0, EOF", n, err)
  66. }
  67. }
  68. func TestReaderAt(t *testing.T) {
  69. r := NewReader([]byte("0123456789"))
  70. tests := []struct {
  71. off int64
  72. n int
  73. want string
  74. wanterr interface{}
  75. }{
  76. {0, 10, "0123456789", nil},
  77. {1, 10, "123456789", io.EOF},
  78. {1, 9, "123456789", nil},
  79. {11, 10, "", io.EOF},
  80. {0, 0, "", nil},
  81. {-1, 0, "", "bytes.Reader.ReadAt: negative offset"},
  82. }
  83. for i, tt := range tests {
  84. b := make([]byte, tt.n)
  85. rn, err := r.ReadAt(b, tt.off)
  86. got := string(b[:rn])
  87. if got != tt.want {
  88. t.Errorf("%d. got %q; want %q", i, got, tt.want)
  89. }
  90. if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.wanterr) {
  91. t.Errorf("%d. got error = %v; want %v", i, err, tt.wanterr)
  92. }
  93. }
  94. }
  95. func TestReaderAtConcurrent(t *testing.T) {
  96. // Test for the race detector, to verify ReadAt doesn't mutate
  97. // any state.
  98. r := NewReader([]byte("0123456789"))
  99. var wg sync.WaitGroup
  100. for i := 0; i < 5; i++ {
  101. wg.Add(1)
  102. go func(i int) {
  103. defer wg.Done()
  104. var buf [1]byte
  105. r.ReadAt(buf[:], int64(i))
  106. }(i)
  107. }
  108. wg.Wait()
  109. }
  110. func TestEmptyReaderConcurrent(t *testing.T) {
  111. // Test for the race detector, to verify a Read that doesn't yield any bytes
  112. // is okay to use from multiple goroutines. This was our historic behavior.
  113. // See golang.org/issue/7856
  114. r := NewReader([]byte{})
  115. var wg sync.WaitGroup
  116. for i := 0; i < 5; i++ {
  117. wg.Add(2)
  118. go func() {
  119. defer wg.Done()
  120. var buf [1]byte
  121. r.Read(buf[:])
  122. }()
  123. go func() {
  124. defer wg.Done()
  125. r.Read(nil)
  126. }()
  127. }
  128. wg.Wait()
  129. }
  130. func TestReaderWriteTo(t *testing.T) {
  131. for i := 0; i < 30; i += 3 {
  132. var l int
  133. if i > 0 {
  134. l = len(data) / i
  135. }
  136. s := data[:l]
  137. r := NewReader(testBytes[:l])
  138. var b Buffer
  139. n, err := r.WriteTo(&b)
  140. if expect := int64(len(s)); n != expect {
  141. t.Errorf("got %v; want %v", n, expect)
  142. }
  143. if err != nil {
  144. t.Errorf("for length %d: got error = %v; want nil", l, err)
  145. }
  146. if b.String() != s {
  147. t.Errorf("got string %q; want %q", b.String(), s)
  148. }
  149. if r.Len() != 0 {
  150. t.Errorf("reader contains %v bytes; want 0", r.Len())
  151. }
  152. }
  153. }
  154. func TestReaderLen(t *testing.T) {
  155. const data = "hello world"
  156. r := NewReader([]byte(data))
  157. if got, want := r.Len(), 11; got != want {
  158. t.Errorf("r.Len(): got %d, want %d", got, want)
  159. }
  160. if n, err := r.Read(make([]byte, 10)); err != nil || n != 10 {
  161. t.Errorf("Read failed: read %d %v", n, err)
  162. }
  163. if got, want := r.Len(), 1; got != want {
  164. t.Errorf("r.Len(): got %d, want %d", got, want)
  165. }
  166. if n, err := r.Read(make([]byte, 1)); err != nil || n != 1 {
  167. t.Errorf("Read failed: read %d %v", n, err)
  168. }
  169. if got, want := r.Len(), 0; got != want {
  170. t.Errorf("r.Len(): got %d, want %d", got, want)
  171. }
  172. }
  173. var UnreadRuneErrorTests = []struct {
  174. name string
  175. f func(*Reader)
  176. }{
  177. {"Read", func(r *Reader) { r.Read([]byte{0}) }},
  178. {"ReadByte", func(r *Reader) { r.ReadByte() }},
  179. {"UnreadRune", func(r *Reader) { r.UnreadRune() }},
  180. {"Seek", func(r *Reader) { r.Seek(0, 1) }},
  181. {"WriteTo", func(r *Reader) { r.WriteTo(&Buffer{}) }},
  182. }
  183. func TestUnreadRuneError(t *testing.T) {
  184. for _, tt := range UnreadRuneErrorTests {
  185. reader := NewReader([]byte("0123456789"))
  186. if _, _, err := reader.ReadRune(); err != nil {
  187. // should not happen
  188. t.Fatal(err)
  189. }
  190. tt.f(reader)
  191. err := reader.UnreadRune()
  192. if err == nil {
  193. t.Errorf("Unreading after %s: expected error", tt.name)
  194. }
  195. }
  196. }
  197. func TestReaderDoubleUnreadRune(t *testing.T) {
  198. buf := NewBuffer([]byte("groucho"))
  199. if _, _, err := buf.ReadRune(); err != nil {
  200. // should not happen
  201. t.Fatal(err)
  202. }
  203. if err := buf.UnreadByte(); err != nil {
  204. // should not happen
  205. t.Fatal(err)
  206. }
  207. if err := buf.UnreadByte(); err == nil {
  208. t.Fatal("UnreadByte: expected error, got nil")
  209. }
  210. }
  211. // verify that copying from an empty reader always has the same results,
  212. // regardless of the presence of a WriteTo method.
  213. func TestReaderCopyNothing(t *testing.T) {
  214. type nErr struct {
  215. n int64
  216. err error
  217. }
  218. type justReader struct {
  219. io.Reader
  220. }
  221. type justWriter struct {
  222. io.Writer
  223. }
  224. discard := justWriter{ioutil.Discard} // hide ReadFrom
  225. var with, withOut nErr
  226. with.n, with.err = io.Copy(discard, NewReader(nil))
  227. withOut.n, withOut.err = io.Copy(discard, justReader{NewReader(nil)})
  228. if with != withOut {
  229. t.Errorf("behavior differs: with = %#v; without: %#v", with, withOut)
  230. }
  231. }