-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack_test.go
264 lines (210 loc) · 4.73 KB
/
stack_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package linear
import "testing"
import "time"
import "sync"
import "fmt"
func TestConcurrencyManualLock(t *testing.T) {
megaStack := NewStack(false)
var group sync.WaitGroup
//spam push
for i := 0; i <= 100; i++ {
group.Add(1)
go func() {
for times := 0; times < 200; times++ {
megaStack.Lock()
ok := megaStack.Push(times)
if ok == false {
t.Error("insert failed " + string(times))
}
megaStack.Unlock()
time.Sleep(time.Millisecond * 10)
}
group.Done()
}()
}
//spam pop and IsEmpty
for i := 0; i <= 100; i++ {
group.Add(1)
go func() {
for times := 0; times < 200; times++ {
megaStack.Lock()
if megaStack.HasElement() {
_, ok := megaStack.Pop()
if ok == false {
t.Error("lock failed, hasElement() but Pop() failed")
}
}
megaStack.Unlock()
time.Sleep(time.Millisecond * 8)
}
group.Done()
}()
}
group.Wait()
}
func TestConcurrencyAutoLock(t *testing.T) {
megaStack := NewStack(true)
var group sync.WaitGroup
//spam peek
for i := 0; i <= 10; i++ {
go func() {
if megaStack.IsEmpty() == false {
_, ok := megaStack.Peek()
if ok == false {
//we ignore the stack was empty, because of the fast times this will happen, 1 stack vs lots of workers
//TODO learn a better way to do this
}
}
//spam for test coverage
if megaStack.String() == "" {
t.Error("String() failed")
}
//spam for test coverage
if megaStack.Len() < 0 {
t.Error("Len() failed")
}
time.Sleep(time.Millisecond * 3)
}()
}
//spam push
for i := 0; i <= 100; i++ {
group.Add(1)
go func() {
for times := 0; times < 200; times++ {
ok := megaStack.Push(times)
if ok == false {
t.Error("insert failed " + string(times))
}
time.Sleep(time.Millisecond * 10)
}
group.Done()
}()
}
//spam pop and IsEmpty
for i := 0; i <= 100; i++ {
group.Add(1)
go func() {
for times := 0; times < 200; times++ {
if megaStack.HasElement() {
_, ok := megaStack.Pop()
if ok == false {
//we ignore the stack was empty, because of the fast times this will happen, 1 stack vs lots of workers
//TODO learn a better way to do this
}
}
time.Sleep(time.Millisecond * 8)
}
group.Done()
}()
}
group.Wait()
}
func TestStackBasicTypes(t *testing.T) {
for _, r := range fakeTable {
testStackFunctionality(t, r)
}
}
func testStackFunctionality(t *testing.T, toPush []interface{}) {
s := NewStack(false)
for i, v := range toPush {
ok := s.Push(v)
if ok == false {
t.Error("push failed ")
}
len := s.Len()
if len != i+1 {
t.Errorf("len failed, expected %v, got %v, for %v", i, len, toPush)
}
value, ok := s.Peek()
if ok == false {
t.Error("peek failed")
}
if value != v {
t.Errorf("peek failed, expected %v, got %v ", v, value)
}
}
for i := len(toPush) - 1; i >= 0; i-- {
el, ok := s.Pop()
if ok == false {
t.Error("pop failed")
}
if el != toPush[i] {
t.Errorf("pop failed, expected %v, got %v", toPush[i], el)
}
}
if s.HasElement() {
t.Errorf("stack is not empty after all Pop(), size=%v", s.Len())
}
}
func TestInitPeekIsNil(t *testing.T) {
s := NewStack(false)
peek, ok := s.Peek()
if ok {
t.Error("peek should be false when used on an empty stack")
}
if peek != nil {
t.Errorf("expected nil, got %v ", peek)
}
}
func TestStackInitPopIsNil(t *testing.T) {
s := NewStack(false)
var pop, ok = s.Pop()
if ok {
t.Error("Pop should be false when used on an empty stack")
}
if pop != nil {
t.Errorf("expected nil, got %v ", pop)
}
}
func TestStackInitIsEmpty(t *testing.T) {
helperInitIsEmpty("stack", NewStack(false), t)
}
func TestStackSkippingNewShouldPanic(t *testing.T) {
//TODO Should Stack implement lazyInit() method (like list.go has) so this should never happen?
defer func() {
if r := recover(); r == nil {
t.Errorf("the code did not panic")
}
}()
s := Stack{}
c := s.Len()
fmt.Print(c)
}
func TestStackStringer(t *testing.T) {
s := NewStack(false)
v := s.String()
if v != "Stack [0]" {
t.Error("stringer was incorrect for 0 length" + v)
}
s.Push(1)
s.Push(1)
s.Push(1)
v = s.String()
if v != "Stack [3]" {
t.Error("stringer was incorrect for 3 length" + v)
}
}
func BenchmarkStackSync1000(b *testing.B) {
benchStackSync(1000, b)
}
func BenchmarkStackSync100000(b *testing.B) {
benchStackSync(100000, b)
}
func BenchmarkStackSync1000000(b *testing.B) {
benchStackSync(1000000, b)
}
func benchStackSync(count int, b *testing.B) {
for i := 0; i < b.N; i++ {
q := NewStack(false)
for c := 0; c < count; c++ {
if q.Push("a") == false {
b.Error("s push failed")
}
}
for c := 0; c < count; c++ {
if _, ok := q.Pop(); ok == false {
b.Error("s pop failed")
}
}
}
}