-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_test.go
181 lines (167 loc) · 5.52 KB
/
simple_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ringbuffer
import (
"log"
"testing"
"github.com/alecthomas/assert/v2"
)
func TestRingBuffer(t *testing.T) {
var ring RingBuffer[*int] = NewSimpleRingBuffer[*int](3)
log.Printf("testcase 1: no element in the ring buffer")
if ring.Size() != 0 {
t.Errorf("Size of the ring buffer should be 0, got %d", ring.Size())
}
if ring.Front() != nil {
t.Errorf("Front of the ring buffer should be nil, got %v", *ring.Front())
}
if ring.Back() != nil {
t.Errorf("Back of the ring buffer should be nil, got %v", *ring.Back())
}
if ring.Pop() != nil {
t.Errorf("Pop should return nil, got %v", *ring.Pop())
}
log.Printf("testcase 2: push element 1 to the ring buffer")
if ring.Push(refInt(1)) != nil {
t.Errorf("Push should return nil, got %v", ring.Push(refInt(1)))
}
if ring.Size() != 1 {
t.Errorf("Size of the ring buffer should be 1, got %d", ring.Size())
}
if *ring.Front() != 1 {
t.Errorf("Front of the ring buffer should be 1, got %v", *ring.Front())
}
if *ring.Back() != 1 {
t.Errorf("Back of the ring buffer should be 1, got %v", *ring.Back())
}
log.Printf("testcase 3: push element 2 to the ring buffer")
if ring.Push(refInt(2)) != nil {
t.Errorf("Push should return nil, got %v", ring.Push(refInt(2)))
}
if ring.Size() != 2 {
t.Errorf("Size of the ring buffer should be 2, got %d", ring.Size())
}
if *ring.Front() != 1 {
t.Errorf("Front of the ring buffer should be 1, got %v", *ring.Front())
}
if *ring.Back() != 2 {
t.Errorf("Back of the ring buffer should be 2, got %v", *ring.Back())
}
log.Printf("testcase 4: push element 3 to the ring buffer")
if k := ring.Push(refInt(3)); k != nil {
t.Errorf("Push should return nil, got %v", *k)
}
if ring.Size() != 3 {
t.Errorf("Size of the ring buffer should be 3, got %d", ring.Size())
}
if *ring.Front() != 1 {
t.Errorf("Front of the ring buffer should be 1, got %v", *ring.Front())
}
if *ring.Back() != 3 {
t.Errorf("Back of the ring buffer should be 3, got %v", *ring.Back())
}
log.Printf("testcase 5: push element 4 to the ring buffer, element 1 should be removed and returned")
if k := ring.Push(refInt(4)); *k != 1 {
t.Errorf("Push should return 1, got %v", *k)
}
if ring.Size() != 3 {
t.Errorf("Size of the ring buffer should be 3, got %d", ring.Size())
}
if *ring.Front() != 2 {
t.Errorf("Front of the ring buffer should be 2, got %v", *ring.Front())
}
if *ring.Back() != 4 {
t.Errorf("Back of the ring buffer should be 4, got %v", *ring.Back())
}
log.Printf("testcase 6: pop element 2 from the ring buffer")
if k := ring.Pop(); *k != 2 {
t.Errorf("Pop should return 2, got %v", *k)
}
if ring.Size() != 2 {
t.Errorf("Size of the ring buffer should be 2, got %d", ring.Size())
}
if *ring.Front() != 3 {
t.Errorf("Front of the ring buffer should be 3, got %v", *ring.Front())
}
if *ring.Back() != 4 {
t.Errorf("Back of the ring buffer should be 4, got %v", *ring.Back())
}
log.Printf("testcase 7: pop element 3 from the ring buffer")
if k := ring.Pop(); *k != 3 {
t.Errorf("Pop should return 3, got %v", *k)
}
if ring.Size() != 1 {
t.Errorf("Size of the ring buffer should be 1, got %d", ring.Size())
}
if *ring.Front() != 4 {
t.Errorf("Front of the ring buffer should be 4, got %v", *ring.Front())
}
if *ring.Back() != 4 {
t.Errorf("Back of the ring buffer should be 4, got %v", *ring.Back())
}
log.Printf("testcase 8: pop element 4 from the ring buffer, the ring buffer should be empty")
if k := ring.Pop(); *k != 4 {
t.Errorf("Pop should return 4, got %v", *k)
}
if ring.Size() != 0 {
t.Errorf("Size of the ring buffer should be 0, got %d", ring.Size())
}
if ring.Front() != nil {
t.Errorf("Front of the ring buffer should be nil, got %v", *ring.Front())
}
if ring.Back() != nil {
t.Errorf("Back of the ring buffer should be nil, got %v", *ring.Back())
}
if ring.Pop() != nil {
t.Errorf("Pop should return nil, got %v", ring.Pop())
}
log.Printf("testcase 9: push element 5 to the ring buffer")
if k := ring.Push(refInt(5)); k != nil {
t.Errorf("Push should return nil, got %v", *k)
}
if ring.Size() != 1 {
t.Errorf("Size of the ring buffer should be 1, got %d", ring.Size())
}
if *ring.Front() != 5 {
t.Errorf("Front of the ring buffer should be 5, got %v", *ring.Front())
}
if *ring.Back() != 5 {
t.Errorf("Back of the ring buffer should be 5, got %v", *ring.Back())
}
log.Printf("testcase 10: push element 6,7 to the ring buffer, ring buffer should be full")
if k := ring.Push(refInt(6)); k != nil {
t.Errorf("Push should return nil, got %v", *k)
}
if k := ring.Push(refInt(7)); k != nil {
t.Errorf("Push should return nil, got %v", *k)
}
if ring.Size() != 3 {
t.Errorf("Size of the ring buffer should be 3, got %d", ring.Size())
}
log.Printf("testcase 11: push element 8,9,10 to the ring buffer, element 5,6,7 should be removed and returned")
if k := ring.Push(refInt(8)); *k != 5 {
t.Errorf("Push should return 5, got %v", *k)
}
if k := ring.Push(refInt(9)); *k != 6 {
t.Errorf("Push should return 6, got %v", *k)
}
if k := ring.Push(refInt(10)); *k != 7 {
t.Errorf("Push should return 7, got %v", *k)
}
if ring.Size() != 3 {
t.Errorf("Size of the ring buffer should be 3, got %d", ring.Size())
}
if *ring.Front() != 8 {
t.Errorf("Front of the ring buffer should be 8, got %v", *ring.Front())
}
if *ring.Back() != 10 {
t.Errorf("Back of the ring buffer should be 10, got %v", *ring.Back())
}
content := []int{}
iterFunc := func(i *int) {
content = append(content, *i)
}
ring.Iterate(iterFunc)
assert.Equal(t, content, []int{8, 9, 10})
}
func refInt(i int) *int {
return &i
}