memio - buffer_test.go
1 package memio
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7 )
8
9 var (
10 _ io.Reader = &Buffer{}
11 _ io.Writer = &Buffer{}
12 _ io.WriterTo = &Buffer{}
13 _ io.ReaderAt = &Buffer{}
14 _ io.WriterAt = &Buffer{}
15 )
16
17 func TestBufferRead(t *testing.T) {
18 reader := Buffer("Hello, World!")
19 toRead := make([]byte, 5)
20 if n, err := reader.Read(toRead); n != 5 {
21 t.Errorf("expecting to read 5 bytes, read %d", n)
22 return
23 } else if err != nil {
24 t.Errorf("got error: %q", err.Error())
25 return
26 } else if string(toRead) != "Hello" {
27 t.Errorf("expecting %q, got %q", "Hello", string(toRead))
28 return
29 }
30 if n, err := reader.Read(toRead); n != 5 {
31 t.Errorf("expecting to read 5 bytes, read %d", n)
32 return
33 } else if err != nil {
34 t.Errorf("got error: %q", err.Error())
35 return
36 } else if string(toRead) != ", Wor" {
37 t.Errorf("expecting %q, got %q", ", Wor", string(toRead))
38 return
39 }
40 if n, err := reader.Read(toRead); n != 3 {
41 t.Errorf("expecting to read 3 bytes, read %d", n)
42 return
43 } else if err != nil {
44 t.Errorf("got error: %q", err.Error())
45 return
46 } else if string(toRead) != "ld!or" {
47 t.Errorf("expecting %q, got %q", "ld!or", string(toRead))
48 return
49 }
50 if n, err := reader.Read(toRead); n != 0 {
51 t.Errorf("expecting to read 0 bytes, read %d", n)
52 return
53 } else if err != io.EOF {
54 t.Errorf("expecting EOF")
55 }
56 }
57
58 func TestBufferWrite(t *testing.T) {
59 data := []byte("Beep ")
60 writer := Buffer(data)[:0]
61 if n, err := writer.Write([]byte("J")); n != 1 {
62 t.Errorf("expecting to write 1 byte, wrote %d", n)
63 return
64 } else if err != nil {
65 t.Errorf("got error: %q", err.Error())
66 return
67 } else if string(data) != "Jeep " {
68 t.Errorf("expecting %q, got %q", "Jeep ", string(data))
69 return
70 }
71 if n, err := writer.Write([]byte("ohn")); n != 3 {
72 t.Errorf("expecting to write 3 bytes, wrote %d", n)
73 return
74 } else if err != nil {
75 t.Errorf("got error: %q", err.Error())
76 return
77 } else if string(data) != "John " {
78 t.Errorf("expecting %q, got %q", "John ", string(data))
79 return
80 }
81 if n, err := writer.Write([]byte("ny")); n != 2 {
82 t.Errorf("expecting to write 2 bytes, wrote %d", n)
83 return
84 } else if err != nil {
85 t.Errorf("got error: %q", err.Error())
86 return
87 } else if string(data) != "Johnny" {
88 t.Errorf("expecting %q, got %q", "Johnny", string(data))
89 return
90 }
91 }
92
93 func TestBufferReadFrom(t *testing.T) {
94 for n, test := range [...]struct {
95 byteReader
96 limit int64
97 initial Buffer
98 result []byte
99 }{
100 /*
101 {
102 3,
103 23,
104 make(Buffer, 10, 10),
105 []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1},
106 },
107 {
108 3,
109 31,
110 make(Buffer, 0, 0),
111 []byte{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1},
112 },
113 */
114 } {
115 m, err := test.initial.ReadFrom(io.LimitReader(&test.byteReader, test.limit))
116 if err != nil {
117 t.Errorf("test %d: unexpected error: %s", n+1, err)
118 } else if m != test.limit {
119 t.Errorf("test %d: expecting to read %d bytes, read %d", n+1, test.limit, m)
120 } else if !bytes.Equal(test.result, test.initial) {
121 t.Errorf("test %d: expecting %v, got %v", n+1, test.result, test.initial)
122 }
123 }
124 }
125