forked from glycerine/rbuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbuf_test.go
67 lines (53 loc) · 1.82 KB
/
fbuf_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package rbuf
import (
"io"
"testing"
cv "github.com/glycerine/goconvey/convey"
)
func TestFloatBufReadWrite(t *testing.T) {
b := NewFloat64RingBuf(5)
data := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
cv.Convey("Given a Float64RingBuf of size 5", t, func() {
cv.Convey("Write() and ReadFloat64() should put and get floats", func() {
b.Reset()
n, err := b.Write(data[:5])
cv.So(n, cv.ShouldEqual, 5)
cv.So(err, cv.ShouldEqual, nil)
cv.So(b.Readable, cv.ShouldEqual, 5)
cv.So(b.Values(), cv.ShouldResemble, data[:5])
sink := make([]float64, 3)
n, err = b.ReadFloat64(sink)
cv.So(n, cv.ShouldEqual, 3)
cv.So(b.Values(), cv.ShouldResemble, data[3:5])
cv.So(sink, cv.ShouldResemble, data[:3])
})
cv.Convey("Write() more than 5 items should give back ErrShortWrite", func() {
b.Reset()
cv.So(b.Readable, cv.ShouldEqual, 0)
n, err := b.Write(data[:10])
cv.So(n, cv.ShouldEqual, 5)
cv.So(err, cv.ShouldEqual, io.ErrShortWrite)
cv.So(b.Readable, cv.ShouldEqual, 5)
cv.So(b.Values(), cv.ShouldResemble, data[:5])
sink := make([]float64, 3)
n, err = b.ReadFloat64(sink)
cv.So(n, cv.ShouldEqual, 3)
cv.So(b.Values(), cv.ShouldResemble, data[3:5])
cv.So(sink, cv.ShouldResemble, data[:3])
})
cv.Convey("WriteAndMaybeOverwriteOldestData() should auto advance", func() {
b.Reset()
n, err := b.WriteAndMaybeOverwriteOldestData(data[:5])
cv.So(err, cv.ShouldEqual, nil)
cv.So(n, cv.ShouldEqual, 5)
cv.So(b.Values(), cv.ShouldResemble, data[:5])
n, err = b.WriteAndMaybeOverwriteOldestData(data[5:7])
cv.So(n, cv.ShouldEqual, 2)
cv.So(b.Values(), cv.ShouldResemble, data[2:7])
n, err = b.WriteAndMaybeOverwriteOldestData(data[0:9])
cv.So(err, cv.ShouldEqual, nil)
cv.So(n, cv.ShouldEqual, 9)
cv.So(b.Values(), cv.ShouldResemble, data[4:9])
})
})
}