-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunction_test.go
More file actions
129 lines (104 loc) · 2.36 KB
/
Copy pathfunction_test.go
File metadata and controls
129 lines (104 loc) · 2.36 KB
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
129
package lfring
import (
. "gopkg.in/check.v1"
"testing"
)
// hook up go-check to go testing
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestFindPowerOfTwo(c *C) {
// given
cap1 := uint64(0)
cap2 := uint64(10)
cap3 := uint64(16)
cap4 := uint64(33)
cap5 := ^uint64(0)
// when
res1 := findPowerOfTwo(cap1)
res2 := findPowerOfTwo(cap2)
res3 := findPowerOfTwo(cap3)
res4 := findPowerOfTwo(cap4)
res5 := findPowerOfTwo(cap5)
// then
c.Assert(res1, Equals, uint64(0))
c.Assert(res2, Equals, uint64(16))
c.Assert(res3, Equals, uint64(16))
c.Assert(res4, Equals, uint64(64))
c.Assert(res5, Equals, uint64(0))
}
var bufferSet = []BufferType{NodeBased, Classical}
func (s *MySuite) TestOfferAndPollSuccess(c *C) {
for _, t := range bufferSet {
// given
fakeString := "fake"
buffer := New[*string](t, 10)
// when
result := buffer.Offer(&fakeString)
poll, _ := buffer.Poll()
// then
c.Assert(result, Equals, true)
c.Assert(poll, Equals, &fakeString)
}
}
func (s *MySuite) TestOfferFailedWhenFull(c *C) {
for _, t := range bufferSet {
// given
capacity := 10
buffer := New[int](t, uint64(capacity))
realCapacity := findPowerOfTwo(uint64(capacity + 1))
for i := 0; i < int(realCapacity); i++ {
buffer.Offer(i)
}
// when
offered := buffer.Offer(10)
// then
c.Assert(offered, Equals, false)
}
}
func (s *MySuite) TestPollFailedWhenEmpty(c *C) {
for _, t := range bufferSet {
// given
capacity := 10
buffer := New[int](t, uint64(capacity))
// when
_, success := buffer.Poll()
// then
c.Assert(success, Equals, false)
}
}
func (s *MySuite) TestRingBufferShift(c *C) {
for _, t := range bufferSet {
// given
capacity := 10
buffer := New[int](t, uint64(capacity))
// when
for i := 0; i < 13; i++ {
buffer.Offer(i)
}
// when
buffer.Offer(13)
buffer.Offer(14)
// then
polled, success := buffer.Poll()
c.Assert(success, Equals, true)
c.Assert(polled, Equals, 0)
// when
buffer.Offer(15)
// then
for i := 0; i < 14; i++ {
polled, success := buffer.Poll()
c.Assert(success, Equals, true)
c.Assert(polled, Equals, i+1)
}
// when
buffer.Offer(16)
buffer.Offer(17)
buffer.Offer(18)
// then
polled1, _ := buffer.Poll()
c.Assert(polled1, Equals, 15)
polled2, _ := buffer.Poll()
c.Assert(polled2, Equals, 16)
}
}