rwcount - rwcount_test.go
1 package rwcount
2
3 import (
4 "testing"
5 )
6
7 type null struct{}
8
9 func (null) Read(p []byte) (int, error) {
10 return len(p), nil
11 }
12
13 func (null) Write(p []byte) (int, error) {
14 return len(p), nil
15 }
16
17 func TestCounts(t *testing.T) {
18 data := make([]byte, 0, 1500)
19 r := Reader{Reader: null{}}
20 w := Writer{Writer: null{}}
21 var total int64
22 for i := 0; i < 1500; i++ {
23 total += int64(i)
24 n, _ := w.Write(data[:i])
25 if n != i {
26 t.Errorf("failed to write %d bytes to buffer, wrote %d", i, n)
27 } else if total != w.Count {
28 t.Errorf("written bytes returned (%d) didn't match expected (%d)", w.Count, total)
29 }
30 n, _ = r.Read(data[:i])
31 if n != i {
32 t.Errorf("failed to read %d bytes from buffer, read %d", i, n)
33 } else if total != r.Count {
34 t.Errorf("read bytes returned (%d) didn't match expected (%d)", r.Count, total)
35 }
36 }
37 }