gitee.com/quant1x/gox@v1.21.2/util/arraystack/arraystack_test.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package arraystack 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func TestStackPush(t *testing.T) { 13 stack := New() 14 if actualValue := stack.Empty(); actualValue != true { 15 t.Errorf("Got %v expected %v", actualValue, true) 16 } 17 stack.Push(1) 18 stack.Push(2) 19 stack.Push(3) 20 21 if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 { 22 t.Errorf("Got %v expected %v", actualValue, "[3,2,1]") 23 } 24 if actualValue := stack.Empty(); actualValue != false { 25 t.Errorf("Got %v expected %v", actualValue, false) 26 } 27 if actualValue := stack.Size(); actualValue != 3 { 28 t.Errorf("Got %v expected %v", actualValue, 3) 29 } 30 if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { 31 t.Errorf("Got %v expected %v", actualValue, 3) 32 } 33 } 34 35 func TestStackPeek(t *testing.T) { 36 stack := New() 37 if actualValue, ok := stack.Peek(); actualValue != nil || ok { 38 t.Errorf("Got %v expected %v", actualValue, nil) 39 } 40 stack.Push(1) 41 stack.Push(2) 42 stack.Push(3) 43 if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { 44 t.Errorf("Got %v expected %v", actualValue, 3) 45 } 46 } 47 48 func TestStackPop(t *testing.T) { 49 stack := New() 50 stack.Push(1) 51 stack.Push(2) 52 stack.Push(3) 53 stack.Pop() 54 if actualValue, ok := stack.Peek(); actualValue != 2 || !ok { 55 t.Errorf("Got %v expected %v", actualValue, 2) 56 } 57 if actualValue, ok := stack.Pop(); actualValue != 2 || !ok { 58 t.Errorf("Got %v expected %v", actualValue, 2) 59 } 60 if actualValue, ok := stack.Pop(); actualValue != 1 || !ok { 61 t.Errorf("Got %v expected %v", actualValue, 1) 62 } 63 if actualValue, ok := stack.Pop(); actualValue != nil || ok { 64 t.Errorf("Got %v expected %v", actualValue, nil) 65 } 66 if actualValue := stack.Empty(); actualValue != true { 67 t.Errorf("Got %v expected %v", actualValue, true) 68 } 69 if actualValue := stack.Values(); len(actualValue) != 0 { 70 t.Errorf("Got %v expected %v", actualValue, "[]") 71 } 72 } 73 74 func TestStackIteratorOnEmpty(t *testing.T) { 75 stack := New() 76 it := stack.Iterator() 77 for it.Next() { 78 t.Errorf("Shouldn't iterate on empty stack") 79 } 80 } 81 82 func TestStackIteratorNext(t *testing.T) { 83 stack := New() 84 stack.Push("a") 85 stack.Push("b") 86 stack.Push("c") 87 88 it := stack.Iterator() 89 count := 0 90 for it.Next() { 91 count++ 92 index := it.Index() 93 value := it.Value() 94 switch index { 95 case 0: 96 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 97 t.Errorf("Got %v expected %v", actualValue, expectedValue) 98 } 99 case 1: 100 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 101 t.Errorf("Got %v expected %v", actualValue, expectedValue) 102 } 103 case 2: 104 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 105 t.Errorf("Got %v expected %v", actualValue, expectedValue) 106 } 107 default: 108 t.Errorf("Too many") 109 } 110 if actualValue, expectedValue := index, count-1; actualValue != expectedValue { 111 t.Errorf("Got %v expected %v", actualValue, expectedValue) 112 } 113 } 114 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 115 t.Errorf("Got %v expected %v", actualValue, expectedValue) 116 } 117 } 118 119 func TestStackIteratorPrev(t *testing.T) { 120 stack := New() 121 stack.Push("a") 122 stack.Push("b") 123 stack.Push("c") 124 125 it := stack.Iterator() 126 for it.Next() { 127 } 128 count := 0 129 for it.Prev() { 130 count++ 131 index := it.Index() 132 value := it.Value() 133 switch index { 134 case 0: 135 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 136 t.Errorf("Got %v expected %v", actualValue, expectedValue) 137 } 138 case 1: 139 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 140 t.Errorf("Got %v expected %v", actualValue, expectedValue) 141 } 142 case 2: 143 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 144 t.Errorf("Got %v expected %v", actualValue, expectedValue) 145 } 146 default: 147 t.Errorf("Too many") 148 } 149 if actualValue, expectedValue := index, 3-count; actualValue != expectedValue { 150 t.Errorf("Got %v expected %v", actualValue, expectedValue) 151 } 152 } 153 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 154 t.Errorf("Got %v expected %v", actualValue, expectedValue) 155 } 156 } 157 158 func TestStackIteratorBegin(t *testing.T) { 159 stack := New() 160 it := stack.Iterator() 161 it.Begin() 162 stack.Push("a") 163 stack.Push("b") 164 stack.Push("c") 165 for it.Next() { 166 } 167 it.Begin() 168 it.Next() 169 if index, value := it.Index(), it.Value(); index != 0 || value != "c" { 170 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") 171 } 172 } 173 174 func TestStackIteratorEnd(t *testing.T) { 175 stack := New() 176 it := stack.Iterator() 177 178 if index := it.Index(); index != -1 { 179 t.Errorf("Got %v expected %v", index, -1) 180 } 181 182 it.End() 183 if index := it.Index(); index != 0 { 184 t.Errorf("Got %v expected %v", index, 0) 185 } 186 187 stack.Push("a") 188 stack.Push("b") 189 stack.Push("c") 190 it.End() 191 if index := it.Index(); index != stack.Size() { 192 t.Errorf("Got %v expected %v", index, stack.Size()) 193 } 194 195 it.Prev() 196 if index, value := it.Index(), it.Value(); index != stack.Size()-1 || value != "a" { 197 t.Errorf("Got %v,%v expected %v,%v", index, value, stack.Size()-1, "a") 198 } 199 } 200 201 func TestStackIteratorFirst(t *testing.T) { 202 stack := New() 203 it := stack.Iterator() 204 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { 205 t.Errorf("Got %v expected %v", actualValue, expectedValue) 206 } 207 stack.Push("a") 208 stack.Push("b") 209 stack.Push("c") 210 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 211 t.Errorf("Got %v expected %v", actualValue, expectedValue) 212 } 213 if index, value := it.Index(), it.Value(); index != 0 || value != "c" { 214 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") 215 } 216 } 217 218 func TestStackIteratorLast(t *testing.T) { 219 stack := New() 220 it := stack.Iterator() 221 if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue { 222 t.Errorf("Got %v expected %v", actualValue, expectedValue) 223 } 224 stack.Push("a") 225 stack.Push("b") 226 stack.Push("c") 227 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 228 t.Errorf("Got %v expected %v", actualValue, expectedValue) 229 } 230 if index, value := it.Index(), it.Value(); index != 2 || value != "a" { 231 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "a") 232 } 233 } 234 235 func TestStackSerialization(t *testing.T) { 236 stack := New() 237 stack.Push("a") 238 stack.Push("b") 239 stack.Push("c") 240 241 var err error 242 assert := func() { 243 if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue { 244 t.Errorf("Got %v expected %v", actualValue, expectedValue) 245 } 246 if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue { 247 t.Errorf("Got %v expected %v", actualValue, expectedValue) 248 } 249 if err != nil { 250 t.Errorf("Got error %v", err) 251 } 252 } 253 254 assert() 255 256 json, err := stack.ToJSON() 257 assert() 258 259 err = stack.FromJSON(json) 260 assert() 261 } 262 263 func benchmarkPush(b *testing.B, stack *Stack, size int) { 264 for i := 0; i < b.N; i++ { 265 for n := 0; n < size; n++ { 266 stack.Push(n) 267 } 268 } 269 } 270 271 func benchmarkPop(b *testing.B, stack *Stack, size int) { 272 for i := 0; i < b.N; i++ { 273 for n := 0; n < size; n++ { 274 stack.Pop() 275 } 276 } 277 } 278 279 func BenchmarkArrayStackPop100(b *testing.B) { 280 b.StopTimer() 281 size := 100 282 stack := New() 283 for n := 0; n < size; n++ { 284 stack.Push(n) 285 } 286 b.StartTimer() 287 benchmarkPop(b, stack, size) 288 } 289 290 func BenchmarkArrayStackPop1000(b *testing.B) { 291 b.StopTimer() 292 size := 1000 293 stack := New() 294 for n := 0; n < size; n++ { 295 stack.Push(n) 296 } 297 b.StartTimer() 298 benchmarkPop(b, stack, size) 299 } 300 301 func BenchmarkArrayStackPop10000(b *testing.B) { 302 b.StopTimer() 303 size := 10000 304 stack := New() 305 for n := 0; n < size; n++ { 306 stack.Push(n) 307 } 308 b.StartTimer() 309 benchmarkPop(b, stack, size) 310 } 311 312 func BenchmarkArrayStackPop100000(b *testing.B) { 313 b.StopTimer() 314 size := 100000 315 stack := New() 316 for n := 0; n < size; n++ { 317 stack.Push(n) 318 } 319 b.StartTimer() 320 benchmarkPop(b, stack, size) 321 } 322 323 func BenchmarkArrayStackPush100(b *testing.B) { 324 b.StopTimer() 325 size := 100 326 stack := New() 327 b.StartTimer() 328 benchmarkPush(b, stack, size) 329 } 330 331 func BenchmarkArrayStackPush1000(b *testing.B) { 332 b.StopTimer() 333 size := 1000 334 stack := New() 335 for n := 0; n < size; n++ { 336 stack.Push(n) 337 } 338 b.StartTimer() 339 benchmarkPush(b, stack, size) 340 } 341 342 func BenchmarkArrayStackPush10000(b *testing.B) { 343 b.StopTimer() 344 size := 10000 345 stack := New() 346 for n := 0; n < size; n++ { 347 stack.Push(n) 348 } 349 b.StartTimer() 350 benchmarkPush(b, stack, size) 351 } 352 353 func BenchmarkArrayStackPush100000(b *testing.B) { 354 b.StopTimer() 355 size := 100000 356 stack := New() 357 for n := 0; n < size; n++ { 358 stack.Push(n) 359 } 360 b.StartTimer() 361 benchmarkPush(b, stack, size) 362 }