-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacknqueue_test.go
More file actions
145 lines (130 loc) · 2.72 KB
/
stacknqueue_test.go
File metadata and controls
145 lines (130 loc) · 2.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package stacknqueue_test
import (
"testing"
sq "github.com/morganhein/stacknqueue"
"sync"
)
func TestQueueNotThreadsafe(t *testing.T) {
q := sq.NewStackNQueue(false)
for i := 0; i < 200; i++ {
q.Queue(i)
}
if q.Len() != 200 {
t.Error("Wrong length count detected. Expected", 200, "got", q.Len())
}
//Test popping from the front
for i := 0; i < 200; i++ {
data := q.Pop()
if (data != i) {
t.Error("Wrong data value detected. Expected ",
i,
" but got ",
data)
}
}
if q.Len() != 0 {
t.Error("Wrong length count detected. Expected", 200, "got", q.Len())
}
//Make sure the StackNQueue is empty
for i := 0; i < 200; i++ {
q.Queue(i)
}
for i := 199; i > -1; i-- {
data := q.Dequeue()
if (data != i) {
t.Error("Wrong data value detected. Expected ",
i,
" but got ",
data)
}
}
if q.Len() != 0 {
t.Error("Wrong length count detected. Expected", 200, "got", q.Len())
}
}
func TestStackNotThreadsafe(t *testing.T) {
q := sq.NewStackNQueue(false)
for i := 0; i < 200; i++ {
q.Push(i)
}
for i := 199; i > -1; i-- {
data := q.Pop()
if data != i {
t.Error("Wrong data value detected. Expected ", i, "got", data)
}
// Test peek functionality
if q.Len() != 0 && q.Peek() != (i - 1) {
t.Error("Wrong next data value detected. Expected ",
i - 1,
" but got ",
data)
}
}
if q.Len() != 0 {
t.Error("Wrong length count detected. Expected", 200, "got", q.Len())
}
}
func TestHelpers(t *testing.T) {
q := sq.NewStackNQueue(false)
for i := 0; i < 200; i++ {
q.Push(i)
}
if q.IsEmpty() {
t.Error("IsEmpty returned incorrect result. Expected to be false but got true.")
}
q.Empty()
if !q.IsEmpty() {
t.Error("IsEmpty returned incorrect result. Expected to be true but got false.")
}
}
func TestQueueThreadsafe(t *testing.T) {
q := sq.NewStackNQueue(true)
var wg sync.WaitGroup
wg.Add(3)
c := make(chan bool, 3)
go fillList(q, 200, &wg, c)
go fillList(q, 200, &wg, c)
go fillList(q, 200, &wg, c)
wg.Wait()
_ = <-c
_ = <-c
_ = <-c
if q.Len() != 600 {
t.Error("Wrong length count detected. Expected", 600, "got", q.Len())
}
wg.Add(2)
go emptyList(q, &wg, c)
go fillList(q, 1500000, &wg, c)
wg.Wait()
if q.Len() != 0 {
t.Error("Wrong length count detected. Expected", 0, "got", q.Len())
}
}
func fillList(q *sq.StackNQueue, size int, wg *sync.WaitGroup, c chan bool) {
defer func() {
c <- true
wg.Done()
}()
for i := 0; i < size; i++ {
q.Queue(i)
}
}
func emptyList(q *sq.StackNQueue, wg *sync.WaitGroup, c chan bool) {
defer wg.Done()
for {
select {
case finished := <-c:
if finished {
emptyHelper(q)
return
}
default:
emptyHelper(q)
}
}
}
func emptyHelper(q *sq.StackNQueue) {
for !q.IsEmpty() {
_ = q.Pop()
}
}