blob: 4906c2e8c78545c6670c94083aab432a6a251d27 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package deque
2
3import (
Jiri Simsa5293dcb2014-05-10 09:56:38 -07004 "testing"
Jiri Simsa870ddd62014-06-27 14:56:58 -07005
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -07006 "v.io/x/ref/lib/testutil/testutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07007)
8
Suharsh Sivakumard19c95d2015-02-19 14:44:50 -08009//go:generate v23 test generate
Asim Shankarc920db32014-10-16 19:18:21 -070010
Jiri Simsa5293dcb2014-05-10 09:56:38 -070011func TestBasic(t *testing.T) {
12 var q T
13 if q.Size() != 0 {
14 t.Errorf("Expected size to be 0, got %d", q.Size())
15 }
16 item := q.PopFront()
17 if item != nil {
18 t.Errorf("Expected front to be nil, got %v", item)
19 }
20 item = q.PopBack()
21 if item != nil {
22 t.Errorf("Expected back to be nil, got %v", item)
23 }
24
25 q.PushFront(1)
26 item = q.PopFront()
27 if item != 1 {
28 t.Errorf("Expected item to be 1, got %v", item)
29 }
30 item = q.PopFront()
31 if item != nil {
32 t.Errorf("Expected back to be nil, got %v", item)
33 }
34
35 q.PushBack(2)
36 item = q.PopBack()
37 if item != 2 {
38 t.Errorf("Expected item to be 2, got %v, %#v", item, q)
39 }
40 item = q.PopBack()
41 if item != nil {
42 t.Errorf("Expected back to be nil, got %v", item)
43 }
44}
45
46func TestBackToFront(t *testing.T) {
47 var q T
48 for i := 0; i != 100; i++ {
49 q.PushBack(i)
50 }
51 for i := 0; i != 100; i++ {
52 item := q.PopFront()
53 if item != i {
54 t.Errorf("Expected %d, got %v", i, item)
55 }
56 }
57 item := q.PopFront()
58 if item != nil {
59 t.Errorf("Expected nil, got %v", item)
60 }
61}
62
63func TestFrontToBack(t *testing.T) {
64 var q T
65 for i := 0; i != 100; i++ {
66 q.PushFront(i)
67 }
68 for i := 0; i != 100; i++ {
69 item := q.PopBack()
70 if item != i {
71 t.Errorf("Expected %d, got %v", i, item)
72 }
73 }
74 item := q.PopBack()
75 if item != nil {
76 t.Errorf("Expected nil, got %v", item)
77 }
78}
79
80func TestFrontToFront(t *testing.T) {
81 var q T
82 for i := 0; i != 100; i++ {
83 q.PushFront(i)
84 }
85 for i := 99; i >= 0; i-- {
86 item := q.PopFront()
87 if item != i {
88 t.Errorf("Expected %d, got %v", i, item)
89 }
90 }
91 item := q.PopFront()
92 if item != nil {
93 t.Errorf("Expected nil, got %v", item)
94 }
95}
96
97func TestBackToBack(t *testing.T) {
98 var q T
99 for i := 0; i != 100; i++ {
100 q.PushBack(i)
101 }
102 for i := 99; i >= 0; i-- {
103 item := q.PopBack()
104 if item != i {
105 t.Errorf("Expected %d, got %v", i, item)
106 }
107 }
108 item := q.PopBack()
109 if item != nil {
110 t.Errorf("Expected nil, got %v", item)
111 }
112}
113
114func TestClear(t *testing.T) {
115 var q T
116 for i := 0; i != 100; i++ {
117 q.PushBack(i)
118 }
119 q.Clear()
120 size := q.Size()
121 if size != 0 {
122 t.Errorf("Expected size 0, but got %d", size)
123 }
124 item := q.PopFront()
125 if item != nil {
126 t.Errorf("Expected front to be nil, got %v", item)
127 }
128 item = q.PopBack()
129 if item != nil {
130 t.Errorf("Expected back to be nil, got %v", item)
131 }
132}
133
134func TestRandom(t *testing.T) {
135 var q T
136 var contents []int
137 for i := 0; i != 1000; i++ {
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -0700138 switch testutil.Intn(4) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700139 case 0:
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -0700140 i := testutil.Int()
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700141 contents = append([]int{i}, contents...)
142 q.PushFront(i)
143 case 1:
Cosmos Nicolaoua18a1eb2015-03-12 13:15:01 -0700144 i := testutil.Int()
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700145 contents = append(contents, i)
146 q.PushBack(i)
147 case 2:
148 size := len(contents)
149 if q.Size() != size {
150 t.Errorf("Expected size %d, but got %d", size, q.Size())
151 }
152 item := q.PopFront()
153 if size == 0 {
154 if item != nil {
155 t.Errorf("Expected nil, but got %v", item)
156 }
157 } else {
158 if item != contents[0] {
159 t.Errorf("Expected %d, got %d", contents[0], item)
160 }
161 contents = contents[1:]
162 }
163 case 3:
164 size := len(contents)
165 if q.Size() != size {
166 t.Errorf("Expected size %d, got %d", size, q.Size())
167 }
168 item := q.PopBack()
169 if size == 0 {
170 if item != nil {
171 t.Errorf("Expected nil, got %v", item)
172 }
173 } else {
174 if item != contents[len(contents)-1] {
175 t.Errorf("Expected %d, got %d", contents[len(contents)-1], item)
176 }
177 contents = contents[:len(contents)-1]
178 }
179 }
180 }
181}
182
183func TestIter(t *testing.T) {
184 var q T
185
186 // Iterate, quadratic.
187 for i := 0; i != 10; i++ {
188 check := 0
189 q.Iter(func(it interface{}) bool {
190 if it != check {
191 t.Errorf("Expected %d, actual %v", check, it)
192 }
193 check++
194 return true
195 })
196 if check != i {
197 t.Errorf("Queue is too short, expected %d, actual %d", i, check)
198 }
199 q.PushBack(i)
200 }
201
202 // Check short iteration.
203 iterations := 0
204 q.Iter(func(it interface{}) bool {
205 iterations++
206 return it.(int) < 5
207 })
208 if iterations != 6 {
209 t.Errorf("Iteration did not stop correctly,expected 6 iterations, got %d", iterations)
210 }
211}
Jungho Ahnad8d0712015-01-21 09:01:10 -0800212
213func BenchmarkPushFront(b *testing.B) {
214 var q T
215 for i := 0; i < b.N; i++ {
216 q.PushFront(i)
217 }
218}
219
220func BenchmarkPushBack(b *testing.B) {
221 var q T
222 for i := 0; i < b.N; i++ {
223 q.PushBack(i)
224 }
225}
226
227func BenchmarkPopFront(b *testing.B) {
228 var q T
229 for i := 0; i < b.N; i++ {
230 q.PushBack(i)
231 }
232
233 b.ResetTimer()
234 for i := 0; i < b.N; i++ {
235 q.PopFront()
236 }
237}
238
239func BenchmarkPopBack(b *testing.B) {
240 var q T
241 for i := 0; i < b.N; i++ {
242 q.PushFront(i)
243 }
244
245 b.ResetTimer()
246 for i := 0; i < b.N; i++ {
247 q.PopBack()
248 }
249}