-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_test.go
128 lines (116 loc) · 3.19 KB
/
ring_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Some of the test code is in ringbuffer/dbg_test.go, also.
package ringbuffer_test
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
//"math/rand"
"ringbuffer"
"testing"
)
// Error, Errorf, FailNow, Fatal, FatalIf
type kitchenSink struct { // Arbitrary type.
words string
nums [4]int
}
func (pk kitchenSink) String() string {
return "\t** " + pk.words + fmt.Sprintf(" {%3d, %3d, %3d, %3d} **",
pk.nums[0], pk.nums[1], pk.nums[2], pk.nums[3])
}
var ksa = [...]kitchenSink{
kitchenSink{words: "Ignore this message", nums: [...]int{0, 1, 2, 3}},
kitchenSink{nums: [4]int{99, 98, 97, 96}, words: "this and that"},
kitchenSink{nums: [4]int{987654321, 1234567890, 0, -1234567890}, words: "No slices allowed!"},
}
func TestFull(t *testing.T) {
const quantity = 7
Convey("Full 7", t, func() {
var rBuf = ringbuffer.New(quantity)
So(rBuf, ShouldNotBeNil)
So(rBuf.Full(), ShouldBeFalse)
So(rBuf.HasAny(), ShouldBeFalse)
So(rBuf.Leng(), ShouldEqual, 0)
// Read empty ringbuffer:
x := rBuf.Read()
So(rBuf.Full(), ShouldBeFalse)
So(rBuf.HasAny(), ShouldBeFalse)
So(rBuf.Leng(), ShouldEqual, 0)
So(x, ShouldBeNil) /// Return nil on empty.
var i int
for i = 0; i < quantity; i++ {
e := rBuf.Write(i)
if nil != e {
t.Fatalf("Can't write %d of %d\n", i, quantity)
}
So((i < (quantity-1)) && rBuf.Full(), ShouldBeFalse)
}
// Test Dumping:
rBuf.Dump()
// Overflow
e := rBuf.Write(i + 1) // Overflow must return error.
So(e, ShouldNotBeNil)
var rbe *ringbuffer.RingBufferError
So(e, ShouldHaveSameTypeAs, rbe)
///
So(rBuf.Full(), ShouldBeTrue)
So(rBuf.HasAny(), ShouldBeTrue)
So(rBuf.Leng(), ShouldEqual, quantity)
x = rBuf.Read()
So(rBuf.Full(), ShouldBeFalse)
So(rBuf.HasAny(), ShouldBeTrue)
So(rBuf.Leng(), ShouldEqual, quantity-1)
So(x, ShouldEqual, 0)
So(x, ShouldHaveSameTypeAs, quantity)
rBuf.Clear()
So(rBuf.Leng(), ShouldEqual, 0)
So(rBuf.Full(), ShouldBeFalse)
So(rBuf.HasAny(), ShouldBeFalse)
})
}
func TestKitchenSmall(t *testing.T) {
const quantity = 11
var rBuf = ringbuffer.New(quantity) // Create the ring buffer with the specified size.
Convey("Blank Buffer 11", t, func() {
So(rBuf.Leng(), ShouldEqual, 0)
})
for _, va := range ksa { // Add in the kitchenSink structs.
e := rBuf.Write(va)
if nil != e {
t.Fatalf("ksa Oopsie\n")
}
}
Convey("Full Buffer Wrote 11", t, func() {
So(rBuf.Leng(), ShouldEqual, len(ksa))
})
// Now get them all out:
for rBuf.HasAny() {
fmt.Printf("→ %v\n", rBuf.Read())
}
Convey("Empty Buffer 11", t, func() {
So(rBuf.Leng(), ShouldEqual, 0)
})
}
func TestMillion(t *testing.T) {
const big = 1000000 // A million
Convey("A Million elements", t, func() {
var rBuf = ringbuffer.New(big)
So(rBuf, ShouldNotBeNil)
So(0, ShouldEqual, rBuf.Leng())
//fmt.Println("———————→ Million Buffer March ←———————")
nnn := 0
for {
e := rBuf.Write(&kitchenSink{words: "bogosity", nums: [4]int{nnn, 1 + nnn, 2 + nnn, 3 + nnn}})
if nil != e {
//fmt.Printf("Fatal Error Required: %v", e)
break
}
nnn += 4
}
rc := 0
for rBuf.HasAny() {
_ = rBuf.Read()
rc++
}
fmt.Println("Read ", rc, " times.")
})
//fmt.Println("Done")
}