github.com/fern4lvarez/piladb@v0.2.0-alpha.20180407/pkg/stack/stack_test.go (about) 1 package stack 2 3 import "testing" 4 5 func TestNewStack(t *testing.T) { 6 stack := NewStack() 7 if stack.head != nil { 8 t.Error("stack.head is not nil") 9 } 10 if stack.tail != nil { 11 t.Error("stack.tail is not nil") 12 } 13 if stack.size != 0 { 14 t.Errorf("stack.size is %v, expected 0", stack.size) 15 } 16 } 17 18 func TestStackPush(t *testing.T) { 19 stack := NewStack() 20 stack.Push(8) 21 22 if stack.head == nil { 23 t.Fatal("stack.head is nil") 24 } 25 if stack.head.data != 8 { 26 t.Errorf("stack.head data is %v, expected 8", stack.head.data) 27 } 28 if stack.head.down != nil { 29 t.Errorf("stack.head.down is %v, expected nil", stack.head.down) 30 } 31 if stack.tail == nil { 32 t.Fatal("stack.tail is nil") 33 } 34 if stack.tail.data != 8 { 35 t.Errorf("stack.tail data is %v, expected 8", stack.tail.data) 36 } 37 if stack.tail.down != nil { 38 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 39 } 40 if stack.size != 1 { 41 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 42 } 43 } 44 45 func TestStackPush_TwoElements(t *testing.T) { 46 stack := NewStack() 47 stack.Push(8) 48 expectedNext := stack.head 49 stack.Push("test") 50 51 if stack.head == nil { 52 t.Fatal("stack.head is nil") 53 } 54 if stack.head.data != "test" { 55 t.Errorf("stack.head data is %v, expected test", stack.head.data) 56 } 57 if stack.head.down != expectedNext { 58 t.Errorf("stack.head.down is %v, expected %v", stack.head.down, expectedNext) 59 } 60 if stack.head.up != nil { 61 t.Errorf("stack.head.up is %v, expected nil", stack.head.up) 62 } 63 if stack.tail == nil { 64 t.Fatal("stack.tail is nil") 65 } 66 if stack.tail.data != 8 { 67 t.Errorf("stack.tail data is %v, expected 8", stack.tail.data) 68 } 69 if stack.tail.down != nil { 70 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 71 } 72 if stack.tail.up != stack.head { 73 t.Errorf("stack.head.up is %v, expected %v", stack.head.up, stack.head) 74 } 75 if stack.size != 2 { 76 t.Errorf("stack.size is %v, expected %v", stack.size, 2) 77 } 78 } 79 80 func TestStackPop(t *testing.T) { 81 stack := NewStack() 82 stack.Push("test") 83 stack.Push(8) 84 85 element, ok := stack.Pop() 86 if !ok { 87 t.Errorf("stack.Pop() not ok") 88 } 89 if element != 8 { 90 t.Errorf("element is %v, expected %v", element, 8) 91 } 92 if stack.head == nil { 93 t.Fatal("stack.head is nil") 94 } 95 if stack.head.data != "test" { 96 t.Errorf("stack.head data is %v, expected %v", stack.head.data, "test") 97 } 98 if stack.head.up != nil { 99 t.Errorf("stack.head.up data is %v, expected %v", stack.head.up, nil) 100 } 101 if stack.tail == nil { 102 t.Fatal("stack.tail is nil") 103 } 104 if stack.tail.data != "test" { 105 t.Errorf("stack.tail.data is %v, expected %v", stack.tail.data, "test") 106 } 107 if stack.tail.down != nil { 108 t.Errorf("stack.tail.data is %v, expected nil", stack.tail.data) 109 } 110 if stack.size != 1 { 111 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 112 } 113 } 114 115 func TestStackPop_False(t *testing.T) { 116 stack := NewStack() 117 118 if _, ok := stack.Pop(); ok { 119 t.Error("stack.Pop() is ok") 120 } 121 } 122 123 func TestStackBase(t *testing.T) { 124 stack := NewStack() 125 stack.Base("test") 126 127 if stack.tail == nil { 128 t.Fatal("stack.tail is nil") 129 } 130 if stack.tail.data != "test" { 131 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "data") 132 } 133 if stack.tail.down != nil { 134 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 135 } 136 if stack.tail.up != nil { 137 t.Errorf("stack.tail.up is %v, expected nil", stack.tail) 138 } 139 if stack.head == nil { 140 t.Fatal("stack.head is nil") 141 } 142 if stack.head.data != "test" { 143 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 144 } 145 if stack.head.down != nil { 146 t.Errorf("stack.head.down is %v, expected nil", stack.head.down) 147 } 148 if stack.size != 1 { 149 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 150 } 151 } 152 153 func TestStackBase_TwoElements(t *testing.T) { 154 stack := NewStack() 155 stack.Base("test") 156 stack.Base(8) 157 158 if stack.tail == nil { 159 t.Fatal("stack.tail is nil") 160 } 161 if stack.tail.data != 8 { 162 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 163 } 164 if stack.tail.down != nil { 165 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 166 } 167 if stack.tail.up.data != "test" { 168 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, "test") 169 } 170 if stack.head == nil { 171 t.Fatal("stack.head is nil") 172 } 173 if stack.head.data != "test" { 174 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 175 } 176 if stack.head.down.data != 8 { 177 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, 8) 178 } 179 if stack.size != 2 { 180 t.Errorf("stack.size is %v, expected %v", stack.size, 2) 181 } 182 } 183 184 func TestStackBase_MoreElements(t *testing.T) { 185 stack := NewStack() 186 stack.Base("test") 187 stack.Base(8) 188 stack.Base(true) 189 190 if stack.tail == nil { 191 t.Fatal("stack.tail is nil") 192 } 193 if stack.tail.data != true { 194 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, true) 195 } 196 if stack.tail.down != nil { 197 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 198 } 199 if stack.tail.up.data != 8 { 200 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, 8) 201 } 202 if stack.head == nil { 203 t.Fatal("stack.head is nil") 204 } 205 if stack.head.data != "test" { 206 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 207 } 208 if stack.head.down.data != 8 { 209 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, 8) 210 } 211 if stack.size != 3 { 212 t.Errorf("stack.size is %v, expected %v", stack.size, 3) 213 } 214 } 215 216 func TestStackBase_NoEmpty(t *testing.T) { 217 stack := NewStack() 218 stack.Push("test") 219 stack.Base(8) 220 221 if stack.tail == nil { 222 t.Fatal("stack.tail is nil") 223 } 224 if stack.tail.data != 8 { 225 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 226 } 227 if stack.tail.down != nil { 228 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 229 } 230 if stack.tail.up.data != "test" { 231 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, "test") 232 } 233 if stack.head == nil { 234 t.Fatal("stack.head is nil") 235 } 236 if stack.head.data != "test" { 237 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 238 } 239 if stack.head.down.data != 8 { 240 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, 8) 241 } 242 if stack.size != 2 { 243 t.Errorf("stack.size is %v, expected %v", stack.size, 2) 244 } 245 } 246 247 func TestStackBase_NoEmptyMore(t *testing.T) { 248 stack := NewStack() 249 stack.Push("test") 250 stack.Push(3.14) 251 stack.Push(false) 252 stack.Base("foo") 253 stack.Base(8) 254 255 if stack.tail == nil { 256 t.Fatal("stack.tail is nil") 257 } 258 if stack.tail.data != 8 { 259 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 260 } 261 if stack.tail.down != nil { 262 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 263 } 264 if stack.tail.up.data != "foo" { 265 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, "foo") 266 } 267 if stack.head == nil { 268 t.Fatal("stack.head is nil") 269 } 270 if stack.head.data != false { 271 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, false) 272 } 273 if stack.head.down.data != 3.14 { 274 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, 3.14) 275 } 276 if stack.size != 5 { 277 t.Errorf("stack.size is %v, expected %v", stack.size, 5) 278 } 279 } 280 281 func TestStackSweep(t *testing.T) { 282 stack := NewStack() 283 stack.Push("test") 284 stack.Push(8) 285 286 element, ok := stack.Sweep() 287 if !ok { 288 t.Errorf("stack.Sweep() not ok") 289 } 290 if element != "test" { 291 t.Errorf("element is %v, expected %v", element, "test") 292 } 293 if stack.tail == nil { 294 t.Fatal("stack.tail is nil") 295 } 296 if stack.tail.data != 8 { 297 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 298 } 299 if stack.tail.down != nil { 300 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 301 } 302 if stack.tail.up != nil { 303 t.Errorf("stack.tail.up is %v, expected nil", stack.tail) 304 } 305 if stack.head == nil { 306 t.Fatal("stack.head is nil") 307 } 308 if stack.head.data != 8 { 309 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, 8) 310 } 311 if stack.head.down != nil { 312 t.Errorf("stack.head.down is %v, expected nil", stack.head.down) 313 } 314 if stack.size != 1 { 315 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 316 } 317 } 318 319 func TestStackSweep_OneElement(t *testing.T) { 320 stack := NewStack() 321 stack.Push("test") 322 323 element, ok := stack.Sweep() 324 if !ok { 325 t.Errorf("stack.Sweep() not ok") 326 } 327 if element != "test" { 328 t.Errorf("element is %v, expected %v", element, "test") 329 } 330 if stack.tail != nil { 331 t.Errorf("stack.tail is %v, expected nil", stack.tail) 332 } 333 if stack.head != nil { 334 t.Errorf("stack.tail is %v, expected nil", stack.head) 335 } 336 if stack.size != 0 { 337 t.Errorf("stack.size is %v, expected %v", stack.size, 0) 338 } 339 } 340 341 func TestStackSweep_More(t *testing.T) { 342 stack := NewStack() 343 stack.Push("test") 344 stack.Push(8) 345 stack.Push(10) 346 347 element, ok := stack.Sweep() 348 if !ok { 349 t.Errorf("stack.Sweep() not ok") 350 } 351 if element != "test" { 352 t.Errorf("element is %v, expected %v", element, "test") 353 } 354 if stack.tail == nil { 355 t.Fatal("stack.tail is nil") 356 } 357 if stack.tail.data != 8 { 358 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 359 } 360 if stack.head == nil { 361 t.Fatal("stack.head is nil") 362 } 363 if stack.head.data != 10 { 364 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, 10) 365 } 366 if stack.head.down != stack.tail { 367 t.Errorf("stack.head.down is %v, expected %v", stack.head.down, stack.tail) 368 } 369 if stack.size != 2 { 370 t.Errorf("stack.size is %v, expected %v", stack.size, 2) 371 } 372 373 element, ok = stack.Sweep() 374 if !ok { 375 t.Errorf("stack.Sweep() not ok") 376 } 377 if element != 8 { 378 t.Errorf("element is %v, expected %v", element, 8) 379 } 380 if stack.tail == nil { 381 t.Fatal("stack.tail is nil") 382 } 383 if stack.tail.data != 10 { 384 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 385 } 386 if stack.head == nil { 387 t.Fatal("stack.head is nil") 388 } 389 if stack.head.data != 10 { 390 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, 10) 391 } 392 if stack.size != 1 { 393 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 394 } 395 } 396 397 func TestStackSweep_AndPush(t *testing.T) { 398 stack := NewStack() 399 stack.Push("test") 400 stack.Push(8) 401 stack.Push(10) 402 403 _, _ = stack.Sweep() 404 stack.Push("foo") 405 406 _, _ = stack.Sweep() 407 stack.Push(0) 408 409 element, ok := stack.Sweep() 410 stack.Push(true) 411 if !ok { 412 t.Errorf("stack.Sweep() not ok") 413 } 414 if element != 10 { 415 t.Errorf("element is %v, expected %v", element, 10) 416 } 417 if stack.tail == nil { 418 t.Fatal("stack.tail is nil") 419 } 420 if stack.tail.data != "foo" { 421 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "foo") 422 } 423 if stack.tail.down != nil { 424 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 425 } 426 if stack.head == nil { 427 t.Fatal("stack.head is nil") 428 } 429 if stack.head.data != true { 430 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, true) 431 } 432 if stack.size != 3 { 433 t.Errorf("stack.size is %v, expected %v", stack.size, 3) 434 } 435 } 436 437 func TestStackSweep_False(t *testing.T) { 438 stack := NewStack() 439 _, ok := stack.Sweep() 440 if ok { 441 t.Error("stack.Sweep() is ok") 442 } 443 } 444 445 func TestStackSweepPush(t *testing.T) { 446 stack := NewStack() 447 stack.Push("test") 448 stack.Push(8) 449 450 element, ok := stack.SweepPush("foo") 451 if !ok { 452 t.Errorf("stack.SweepPush(foo) not ok") 453 } 454 if element != "test" { 455 t.Errorf("element is %v, expected %v", element, "test") 456 } 457 if stack.tail == nil { 458 t.Fatal("stack.tail is nil") 459 } 460 if stack.tail.data != 8 { 461 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, 8) 462 } 463 if stack.tail.down != nil { 464 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 465 } 466 if stack.tail.up.data != "foo" { 467 t.Errorf("stack.tail.up.data is %v, expected %v", stack.tail.up.data, "foo") 468 } 469 if stack.head == nil { 470 t.Fatal("stack.head is nil") 471 } 472 if stack.head.data != "foo" { 473 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "foo") 474 } 475 if stack.head.down.data != 8 { 476 t.Errorf("stack.head.down.data is %v, expected %v", stack.head.down.data, 8) 477 } 478 if stack.size != 2 { 479 t.Errorf("stack.size is %v, expected %v", stack.size, 2) 480 } 481 } 482 483 func TestStackSweepPush_More(t *testing.T) { 484 stack := NewStack() 485 stack.Push(8) 486 stack.Push("{'a':'b'}") 487 stack.Push(23.34) 488 element, ok := stack.SweepPush("foo") 489 if !ok { 490 t.Errorf("stack.SweepPush(foo) not ok") 491 } 492 if element != 8 { 493 t.Errorf("element is %v, expected %v", element, 8) 494 } 495 496 if stack.head == nil { 497 t.Fatal("stack.head is nil") 498 } 499 if stack.head.data != "foo" { 500 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "foo") 501 } 502 if stack.head.up != nil { 503 t.Errorf("stack.head.up is %v, expected nil", stack.head.up) 504 } 505 if stack.head.down.data != 23.34 { 506 t.Errorf("stack.head.down.data is %v, expected %v", stack.head.down, 23.34) 507 } 508 if stack.tail == nil { 509 t.Fatal("stack.tail is nil") 510 } 511 if stack.tail.data != "{'a':'b'}" { 512 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "{'a':'b'}") 513 } 514 if stack.tail.down != nil { 515 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 516 } 517 if stack.size != 3 { 518 t.Errorf("stack.size is %v, expected %v", stack.size, 3) 519 } 520 } 521 522 func TestStackSweepPush_OneElement(t *testing.T) { 523 stack := NewStack() 524 stack.Push(8) 525 element, ok := stack.SweepPush("foo") 526 if !ok { 527 t.Errorf("stack.SweepPush(foo) not ok") 528 } 529 if element != 8 { 530 t.Errorf("element is %v, expected %v", element, 8) 531 } 532 533 if stack.head == nil { 534 t.Fatal("stack.head is nil") 535 } 536 if stack.head.data != "foo" { 537 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "foo") 538 } 539 if stack.head.down != nil { 540 t.Errorf("stack.head.down is %v, expected nil", stack.head.down) 541 } 542 if stack.tail == nil { 543 t.Fatal("stack.tail is nil") 544 } 545 if stack.tail.data != "foo" { 546 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "foo") 547 } 548 if stack.tail.down != nil { 549 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 550 } 551 if stack.size != 1 { 552 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 553 } 554 } 555 556 func TestStackSweepPush_False(t *testing.T) { 557 stack := NewStack() 558 _, ok := stack.SweepPush(8) 559 if ok { 560 t.Error("stack.SweepPush(8) is ok") 561 } 562 } 563 564 func TestStackRotate(t *testing.T) { 565 stack := NewStack() 566 stack.Push("test") 567 stack.Push(true) 568 stack.Push(8) 569 570 ok := stack.Rotate() 571 if !ok { 572 t.Errorf("stack.Rotate() not ok") 573 } 574 if stack.tail == nil { 575 t.Fatal("stack.tail is nil") 576 } 577 if stack.tail.data != true { 578 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, true) 579 } 580 if stack.tail.down != nil { 581 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 582 } 583 if stack.tail.up.data != 8 { 584 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, 8) 585 } 586 if stack.head == nil { 587 t.Fatal("stack.head is nil") 588 } 589 if stack.head.data != "test" { 590 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 591 } 592 if stack.head.down.data != 8 { 593 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, 8) 594 } 595 if stack.head.up != nil { 596 t.Errorf("stack.head.up is %v, expected nil", stack.head.up) 597 } 598 if stack.size != 3 { 599 t.Errorf("stack.size is %v, expected %v", stack.size, 3) 600 } 601 } 602 603 func TestStackRotate_Complete(t *testing.T) { 604 stack := NewStack() 605 stack.Push("test") 606 stack.Push(true) 607 stack.Push(8) 608 609 for i := 0; i < 3; i++ { 610 if ok := stack.Rotate(); !ok { 611 t.Errorf("stack.Rotate() not ok") 612 } 613 } 614 if stack.tail == nil { 615 t.Fatal("stack.tail is nil") 616 } 617 if stack.tail.data != "test" { 618 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "test") 619 } 620 if stack.tail.down != nil { 621 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 622 } 623 if stack.tail.up.data != true { 624 t.Errorf("stack.tail.up is %v, expected %v", stack.tail.up.data, true) 625 } 626 if stack.head == nil { 627 t.Fatal("stack.head is nil") 628 } 629 if stack.head.data != 8 { 630 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, 8) 631 } 632 if stack.head.down.data != true { 633 t.Errorf("stack.head.down is %v, expected %v", stack.head.down.data, true) 634 } 635 if stack.head.up != nil { 636 t.Errorf("stack.head.up is %v, expected nil", stack.head.up) 637 } 638 if stack.size != 3 { 639 t.Errorf("stack.size is %v, expected %v", stack.size, 3) 640 } 641 } 642 643 func TestStackRotate_OneElement(t *testing.T) { 644 stack := NewStack() 645 stack.Push("test") 646 647 ok := stack.Rotate() 648 if !ok { 649 t.Errorf("stack.Rotate() not ok") 650 } 651 if stack.tail == nil { 652 t.Fatal("stack.tail is nil") 653 } 654 if stack.tail.data != "test" { 655 t.Errorf("stack.tail data is %v, expected %v", stack.tail.data, "test") 656 } 657 if stack.tail.down != nil { 658 t.Errorf("stack.tail.down is %v, expected nil", stack.tail.down) 659 } 660 if stack.tail.up != nil { 661 t.Errorf("stack.tail.up is %v, expected nil", stack.tail.up) 662 } 663 if stack.head == nil { 664 t.Fatal("stack.head is nil") 665 } 666 if stack.head.data != "test" { 667 t.Errorf("stack.head.data is %v, expected %v", stack.head.data, "test") 668 } 669 if stack.head.down != nil { 670 t.Errorf("stack.head.down is %v, expected nil", stack.head.down) 671 } 672 if stack.head.up != nil { 673 t.Errorf("stack.head.up is %v, expected nil", stack.head.up) 674 } 675 if stack.size != 1 { 676 t.Errorf("stack.size is %v, expected %v", stack.size, 1) 677 } 678 } 679 680 func TestStackRotate_Empty(t *testing.T) { 681 stack := NewStack() 682 683 ok := stack.Rotate() 684 if ok { 685 t.Errorf("stack.Rotate() is ok") 686 } 687 if stack.tail != nil { 688 t.Errorf("stack.tail is %v, expected nil", stack.tail) 689 } 690 if stack.head != nil { 691 t.Errorf("stack.tail is %v, expected nil", stack.head) 692 } 693 if stack.size != 0 { 694 t.Errorf("stack.size is %v, expected %v", stack.size, 0) 695 } 696 } 697 698 func TestStackSize(t *testing.T) { 699 stack := NewStack() 700 if stack.Size() != 0 { 701 t.Errorf("stack.Size() is %v, expected %v", stack.Size(), 0) 702 } 703 704 for i := 0; i < 15; i++ { 705 stack.Push(i) 706 } 707 if stack.Size() != 15 { 708 t.Errorf("stack.Size() is %v, expected %v", stack.Size(), 15) 709 } 710 711 for i := 0; i < 5; i++ { 712 stack.Pop() 713 } 714 if stack.Size() != 10 { 715 t.Errorf("stack.Size() is %v, expected %v", stack.Size(), 10) 716 } 717 } 718 719 func TestStackPeek(t *testing.T) { 720 stack := NewStack() 721 if stack.Peek() != nil { 722 t.Error("stack.Peek() is not nil") 723 } 724 725 stack.Push(9.35) 726 if stack.Peek() != 9.35 { 727 t.Errorf("stack.Peek() is %v, expected %v", stack.Peek(), 9.35) 728 } 729 730 stack.Push(true) 731 if !stack.Peek().(bool) { 732 t.Errorf("stack.Peek() is %v, expected %v", stack.Peek(), true) 733 } 734 735 stack.Push("one") 736 stack.Push("two") 737 stack.Push("three") 738 stack.Pop() 739 if stack.Peek() != "two" { 740 t.Errorf("stack.Peek() is %v, expected %v", stack.Peek(), "two") 741 } 742 743 } 744 745 func TestStackFlush(t *testing.T) { 746 stack := NewStack() 747 748 stack.Push("one") 749 stack.Push("two") 750 stack.Push("three") 751 752 stack.Flush() 753 754 if stack.Peek() != nil { 755 t.Error("stack.Peek() is not nil") 756 } 757 if stack.Size() != 0 { 758 t.Errorf("stack is not empty") 759 } 760 } 761 762 func TestStackRace(t *testing.T) { 763 stack := NewStack() 764 go func() { stack.Push(1) }() 765 go func() { stack.Pop() }() 766 go func() { stack.Size() }() 767 go func() { stack.Peek() }() 768 go func() { stack.Flush() }() 769 }