queue_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2013, Peter H. Froehlich. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license
  3. // that can be found in the LICENSE file.
  4. package queue
  5. // TODO: need a lot more tests, and maybe a better way of
  6. // modularizing them; also benchmarks comparing this to
  7. // Go's container.list
  8. import "testing"
  9. func ensureEmpty(t *testing.T, q *Queue) {
  10. if l := q.Len(); l != 0 {
  11. t.Errorf("q.Len() = %d, want %d", l, 0)
  12. }
  13. if e := q.Front(); e != nil {
  14. t.Errorf("q.Front() = %v, want %v", e, nil)
  15. }
  16. if e := q.Back(); e != nil {
  17. t.Errorf("q.Back() = %v, want %v", e, nil)
  18. }
  19. }
  20. func TestNew(t *testing.T) {
  21. q := New()
  22. ensureEmpty(t, q)
  23. }
  24. func ensureSingleton(t *testing.T, q *Queue) {
  25. if l := q.Len(); l != 1 {
  26. t.Errorf("q.Len() = %d, want %d", l, 1)
  27. }
  28. if e := q.Front(); e != 42 {
  29. t.Errorf("q.Front() = %v, want %v", e, 42)
  30. }
  31. if e := q.Back(); e != 42 {
  32. t.Errorf("q.Back() = %v, want %v", e, 42)
  33. }
  34. }
  35. func TestSingleton(t *testing.T) {
  36. q := New()
  37. ensureEmpty(t, q)
  38. q.PushFront(42)
  39. ensureSingleton(t, q)
  40. q.PopFront()
  41. ensureEmpty(t, q)
  42. q.PushBack(42)
  43. ensureSingleton(t, q)
  44. q.PopBack()
  45. ensureEmpty(t, q)
  46. q.PushFront(42)
  47. ensureSingleton(t, q)
  48. q.PopBack()
  49. ensureEmpty(t, q)
  50. q.PushBack(42)
  51. ensureSingleton(t, q)
  52. q.PopFront()
  53. ensureEmpty(t, q)
  54. }
  55. func TestDuos(t *testing.T) {
  56. q := New()
  57. ensureEmpty(t, q)
  58. q.PushFront(42)
  59. ensureSingleton(t, q)
  60. q.PushBack(43)
  61. if l := q.Len(); l != 2 {
  62. t.Errorf("q.Len() = %d, want %d", l, 2)
  63. }
  64. if e := q.Front(); e != 42 {
  65. t.Errorf("q.Front() = %v, want %v", e, 42)
  66. }
  67. if e := q.Back(); e != 43 {
  68. t.Errorf("q.Back() = %v, want %v", e, 43)
  69. }
  70. }
  71. func ensureLength(t *testing.T, q *Queue, len int) {
  72. if l := q.Len(); l != len {
  73. t.Errorf("q.Len() = %d, want %d", l, len)
  74. }
  75. }
  76. func TestZeroValue(t *testing.T) {
  77. var q Queue
  78. q.PushFront(1)
  79. ensureLength(t, &q, 1)
  80. q.PushFront(2)
  81. ensureLength(t, &q, 2)
  82. q.PushFront(3)
  83. ensureLength(t, &q, 3)
  84. q.PushFront(4)
  85. ensureLength(t, &q, 4)
  86. q.PushFront(5)
  87. ensureLength(t, &q, 5)
  88. }