-
Notifications
You must be signed in to change notification settings - Fork 13
/
stacks_test.go
executable file
·242 lines (206 loc) · 5.45 KB
/
stacks_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
package lab4
import (
"fmt"
"math/rand"
"runtime"
"strconv"
"sync"
"testing"
"time"
)
func TestUnsafeStack(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
unsafeStack := new(UnsafeStack)
fmt.Println("Unsafe Stack Test")
testConcurrentStackAccess(unsafeStack)
}
func TestSafeStack(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
safeStack := new(SafeStack)
fmt.Println("Safe Stack Test")
testConcurrentStackAccess(safeStack)
}
func TestCspStack(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
cspStack := NewCspStack()
fmt.Println("CSP Stack Test")
testConcurrentStackAccess(cspStack)
}
func TestSliceStack(t *testing.T) {
sliceStack := NewSliceStack()
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Println("Slice Stack Test")
testConcurrentStackAccess(sliceStack)
}
func TestOpsUnsafeStack(t *testing.T) {
fmt.Println("Test operations UnsafeStack")
unsafeStack := new(UnsafeStack)
testStackOperations(unsafeStack, t)
}
func TestOpsSafeStack(t *testing.T) {
fmt.Println("Test operations SafeStack")
safeStack := new(SafeStack)
testStackOperations(safeStack, t)
}
func TestOpsCspStack(t *testing.T) {
fmt.Println("Test operations CspStack")
cspStack := NewCspStack()
testStackOperations(cspStack, t)
}
func TestOpsSliceStack(t *testing.T) {
fmt.Println("Test operations SliceStack")
sliceStack := NewSliceStack()
testStackOperations(sliceStack, t)
}
func TestOpsAllStacks(t *testing.T) {
fmt.Println("Test operations all stacks")
TestOpsUnsafeStack(t)
TestOpsSafeStack(t)
TestOpsCspStack(t)
TestOpsSliceStack(t)
}
func BenchmarkSafeStack(b *testing.B) {
safeStack := new(SafeStack)
for i := 0; i < b.N; i++ {
benchStackOperations(safeStack)
}
}
func BenchmarkSliceStack(b *testing.B) {
sliceStack := NewSliceStack()
for i := 0; i < b.N; i++ {
benchStackOperations(sliceStack)
}
}
func BenchmarkCspStack(b *testing.B) {
sliceStack := NewCspStack()
for i := 0; i < b.N; i++ {
benchStackOperations(sliceStack)
}
}
const (
NumberOfGoroutines = 4
NumberOfOperations = 10
)
const (
Len = iota
Push
Pop
)
func testConcurrentStackAccess(stack Stack) {
rand.Seed(time.Now().Unix())
var wg sync.WaitGroup
wg.Add(NumberOfGoroutines)
for i := 0; i < NumberOfGoroutines; i++ {
go func(i int) {
for j := 0; j < NumberOfOperations; j++ {
op := rand.Intn(3)
switch op {
case Len:
// fmt.Printf("#%d-%d: Len() was %d\n", i, j, stack.Len())
stack.Len()
case Push:
// data := "Data" + strconv.Itoa(i) + strconv.Itoa(j)
// fmt.Printf("#%d-%d: Push() with value %v\n", i, j, data)
stack.Push("Data" + strconv.Itoa(i) + strconv.Itoa(j))
case Pop:
_ = stack.Pop()
// fmt.Printf("#%d-%d: Pop() gave value %v\n", i, j, value)
}
}
defer wg.Done()
}(i)
}
wg.Wait()
}
func testStackOperations(stack Stack, t *testing.T) {
var length int
var wantLength int
var testName string
// Initial Stack Test
testName = "Initial Stack Test"
wantLength = 0
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
// Pushed One Test
testName = "Pushed One Test"
stack.Push("Item1")
wantLength = 1
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
item1 := stack.Pop()
if item1 != "Item1" {
t.Errorf("\n%s\nAction: Pop()\nWant: Item1\nGot: %v", testName, item1)
}
wantLength = 0
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
// Pushed Three Test
testName = "Pushed Three Test"
stack.Push("Item2")
stack.Push(3)
stack.Push(4.0001)
wantLength = 3
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
item4 := stack.Pop()
if item4 != 4.0001 {
t.Errorf("\n%s\nAction: Pop()\nWant: 4.0001\nGot: %v", testName, item4)
}
wantLength = 2
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
item3 := stack.Pop()
if item3 != 3 {
t.Errorf("\n%s\nAction: Pop()\nWant: 3\nGot: %v", testName, item3)
}
wantLength = 1
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
item2 := stack.Pop()
if item2 != "Item2" {
t.Errorf("\n%s\nAction: Pop()\nWant: Item2\nGot: %v", testName, item2)
}
wantLength = 0
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
item5 := stack.Pop()
if item5 != nil {
t.Errorf("\n%s\nAction: Pop()\nWant: <nil>\nGot: %v", testName, item5)
}
wantLength = 0
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
// Stack Slice Allocation Test
testName = "Stack Slice Allocation Test"
size := 200
for i := 0; i < size; i++ {
stack.Push(i)
}
wantLength = size
if length = stack.Len(); length != wantLength {
t.Errorf("\n%s\nAction: Len()\nWant: %d\nGot: %d", testName, wantLength, length)
}
for j := size - 1; j >= 0; j-- {
if x := stack.Pop(); x != j {
t.Errorf("\n%s\nAction: Pop()\nWant: %d\nGot: %v", testName, j, x)
break
}
}
}
func benchStackOperations(stack Stack) {
const nrOfOps = 10000
for i := 0; i < nrOfOps; i++ {
stack.Push(i)
}
for j := 0; j < nrOfOps; j++ {
stack.Pop()
}
}