queue_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. import "container/list"
  10. import "math/rand"
  11. func ensureEmpty(t *testing.T, q *Queue) {
  12. if l := q.Len(); l != 0 {
  13. t.Errorf("q.Len() = %d, want %d", l, 0)
  14. }
  15. if e := q.Front(); e != nil {
  16. t.Errorf("q.Front() = %v, want %v", e, nil)
  17. }
  18. if e := q.Back(); e != nil {
  19. t.Errorf("q.Back() = %v, want %v", e, nil)
  20. }
  21. }
  22. func TestNew(t *testing.T) {
  23. q := New()
  24. ensureEmpty(t, q)
  25. }
  26. func ensureSingleton(t *testing.T, q *Queue) {
  27. if l := q.Len(); l != 1 {
  28. t.Errorf("q.Len() = %d, want %d", l, 1)
  29. }
  30. if e := q.Front(); e != 42 {
  31. t.Errorf("q.Front() = %v, want %v", e, 42)
  32. }
  33. if e := q.Back(); e != 42 {
  34. t.Errorf("q.Back() = %v, want %v", e, 42)
  35. }
  36. }
  37. func TestSingleton(t *testing.T) {
  38. q := New()
  39. ensureEmpty(t, q)
  40. q.PushFront(42)
  41. ensureSingleton(t, q)
  42. q.PopFront()
  43. ensureEmpty(t, q)
  44. q.PushBack(42)
  45. ensureSingleton(t, q)
  46. q.PopBack()
  47. ensureEmpty(t, q)
  48. q.PushFront(42)
  49. ensureSingleton(t, q)
  50. q.PopBack()
  51. ensureEmpty(t, q)
  52. q.PushBack(42)
  53. ensureSingleton(t, q)
  54. q.PopFront()
  55. ensureEmpty(t, q)
  56. }
  57. func TestDuos(t *testing.T) {
  58. q := New()
  59. ensureEmpty(t, q)
  60. q.PushFront(42)
  61. ensureSingleton(t, q)
  62. q.PushBack(43)
  63. if l := q.Len(); l != 2 {
  64. t.Errorf("q.Len() = %d, want %d", l, 2)
  65. }
  66. if e := q.Front(); e != 42 {
  67. t.Errorf("q.Front() = %v, want %v", e, 42)
  68. }
  69. if e := q.Back(); e != 43 {
  70. t.Errorf("q.Back() = %v, want %v", e, 43)
  71. }
  72. }
  73. func ensureLength(t *testing.T, q *Queue, len int) {
  74. if l := q.Len(); l != len {
  75. t.Errorf("q.Len() = %d, want %d", l, len)
  76. }
  77. }
  78. func TestZeroValue(t *testing.T) {
  79. var q Queue
  80. q.PushFront(1)
  81. ensureLength(t, &q, 1)
  82. q.PushFront(2)
  83. ensureLength(t, &q, 2)
  84. q.PushFront(3)
  85. ensureLength(t, &q, 3)
  86. q.PushFront(4)
  87. ensureLength(t, &q, 4)
  88. q.PushFront(5)
  89. ensureLength(t, &q, 5)
  90. }
  91. func BenchmarkPushFrontQueue(b *testing.B) {
  92. var q Queue
  93. for i := 0; i < b.N; i++ {
  94. q.PushFront(i)
  95. }
  96. }
  97. func BenchmarkPushFrontList(b *testing.B) {
  98. var q list.List
  99. for i := 0; i < b.N; i++ {
  100. q.PushFront(i)
  101. }
  102. }
  103. func BenchmarkPushBackQueue(b *testing.B) {
  104. var q Queue
  105. for i := 0; i < b.N; i++ {
  106. q.PushBack(i)
  107. }
  108. }
  109. func BenchmarkPushBackList(b *testing.B) {
  110. var q list.List
  111. for i := 0; i < b.N; i++ {
  112. q.PushBack(i)
  113. }
  114. }
  115. func BenchmarkRandomQueue(b *testing.B) {
  116. var q Queue
  117. rand.Seed(64738)
  118. for i := 0; i < b.N; i++ {
  119. if rand.Float32() < 0.8 {
  120. q.PushBack(i)
  121. }
  122. if rand.Float32() < 0.8 {
  123. q.PushFront(i)
  124. }
  125. if rand.Float32() < 0.5 {
  126. q.PopFront()
  127. }
  128. if rand.Float32() < 0.5 {
  129. q.PopBack()
  130. }
  131. }
  132. }
  133. func BenchmarkRandomList(b *testing.B) {
  134. var q list.List
  135. rand.Seed(64738)
  136. for i := 0; i < b.N; i++ {
  137. if rand.Float32() < 0.8 {
  138. q.PushBack(i)
  139. }
  140. if rand.Float32() < 0.8 {
  141. q.PushFront(i)
  142. }
  143. if rand.Float32() < 0.5 {
  144. if e := q.Front(); e != nil {
  145. q.Remove(e)
  146. }
  147. }
  148. if rand.Float32() < 0.5 {
  149. if e := q.Back(); e != nil {
  150. q.Remove(e)
  151. }
  152. }
  153. }
  154. }