queue_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if e := q.PopFront(); e != nil {
  22. t.Errorf("q.PopFront() = %v, want %v", e, nil)
  23. }
  24. if e := q.PopBack(); e != nil {
  25. t.Errorf("q.PopBack() = %v, want %v", e, nil)
  26. }
  27. }
  28. func TestNew(t *testing.T) {
  29. q := New()
  30. ensureEmpty(t, q)
  31. }
  32. func ensureSingleton(t *testing.T, q *Queue) {
  33. if l := q.Len(); l != 1 {
  34. t.Errorf("q.Len() = %d, want %d", l, 1)
  35. }
  36. if e := q.Front(); e != 42 {
  37. t.Errorf("q.Front() = %v, want %v", e, 42)
  38. }
  39. if e := q.Back(); e != 42 {
  40. t.Errorf("q.Back() = %v, want %v", e, 42)
  41. }
  42. }
  43. func TestSingleton(t *testing.T) {
  44. q := New()
  45. ensureEmpty(t, q)
  46. q.PushFront(42)
  47. ensureSingleton(t, q)
  48. q.PopFront()
  49. ensureEmpty(t, q)
  50. q.PushBack(42)
  51. ensureSingleton(t, q)
  52. q.PopBack()
  53. ensureEmpty(t, q)
  54. q.PushFront(42)
  55. ensureSingleton(t, q)
  56. q.PopBack()
  57. ensureEmpty(t, q)
  58. q.PushBack(42)
  59. ensureSingleton(t, q)
  60. q.PopFront()
  61. ensureEmpty(t, q)
  62. }
  63. func TestDuos(t *testing.T) {
  64. q := New()
  65. ensureEmpty(t, q)
  66. q.PushFront(42)
  67. ensureSingleton(t, q)
  68. q.PushBack(43)
  69. if l := q.Len(); l != 2 {
  70. t.Errorf("q.Len() = %d, want %d", l, 2)
  71. }
  72. if e := q.Front(); e != 42 {
  73. t.Errorf("q.Front() = %v, want %v", e, 42)
  74. }
  75. if e := q.Back(); e != 43 {
  76. t.Errorf("q.Back() = %v, want %v", e, 43)
  77. }
  78. }
  79. func ensureLength(t *testing.T, q *Queue, len int) {
  80. if l := q.Len(); l != len {
  81. t.Errorf("q.Len() = %d, want %d", l, len)
  82. }
  83. }
  84. func TestZeroValue(t *testing.T) {
  85. var q Queue
  86. q.PushFront(1)
  87. ensureLength(t, &q, 1)
  88. q.PushFront(2)
  89. ensureLength(t, &q, 2)
  90. q.PushFront(3)
  91. ensureLength(t, &q, 3)
  92. q.PushFront(4)
  93. ensureLength(t, &q, 4)
  94. q.PushFront(5)
  95. ensureLength(t, &q, 5)
  96. q.PushBack(6)
  97. ensureLength(t, &q, 6)
  98. q.PushBack(7)
  99. ensureLength(t, &q, 7)
  100. q.PushBack(8)
  101. ensureLength(t, &q, 8)
  102. q.PushBack(9)
  103. ensureLength(t, &q, 9)
  104. const want = "[5, 4, 3, 2, 1, 6, 7, 8, 9]"
  105. if s := q.String(); s != want {
  106. t.Errorf("q.String() = %s, want %s", s, want)
  107. }
  108. }
  109. func BenchmarkPushFrontQueue(b *testing.B) {
  110. var q Queue
  111. for i := 0; i < b.N; i++ {
  112. q.PushFront(i)
  113. }
  114. }
  115. func BenchmarkPushFrontList(b *testing.B) {
  116. var q list.List
  117. for i := 0; i < b.N; i++ {
  118. q.PushFront(i)
  119. }
  120. }
  121. func BenchmarkPushBackQueue(b *testing.B) {
  122. var q Queue
  123. for i := 0; i < b.N; i++ {
  124. q.PushBack(i)
  125. }
  126. }
  127. func BenchmarkPushBackList(b *testing.B) {
  128. var q list.List
  129. for i := 0; i < b.N; i++ {
  130. q.PushBack(i)
  131. }
  132. }
  133. func BenchmarkPushBackChannel(b *testing.B) {
  134. q := make(chan interface{}, b.N)
  135. for i := 0; i < b.N; i++ {
  136. q <- i
  137. }
  138. close(q)
  139. }
  140. func BenchmarkRandomQueue(b *testing.B) {
  141. var q Queue
  142. rand.Seed(64738)
  143. for i := 0; i < b.N; i++ {
  144. if rand.Float32() < 0.8 {
  145. q.PushBack(i)
  146. }
  147. if rand.Float32() < 0.8 {
  148. q.PushFront(i)
  149. }
  150. if rand.Float32() < 0.5 {
  151. q.PopFront()
  152. }
  153. if rand.Float32() < 0.5 {
  154. q.PopBack()
  155. }
  156. }
  157. }
  158. func BenchmarkRandomList(b *testing.B) {
  159. var q list.List
  160. rand.Seed(64738)
  161. for i := 0; i < b.N; i++ {
  162. if rand.Float32() < 0.8 {
  163. q.PushBack(i)
  164. }
  165. if rand.Float32() < 0.8 {
  166. q.PushFront(i)
  167. }
  168. if rand.Float32() < 0.5 {
  169. if e := q.Front(); e != nil {
  170. q.Remove(e)
  171. }
  172. }
  173. if rand.Float32() < 0.5 {
  174. if e := q.Back(); e != nil {
  175. q.Remove(e)
  176. }
  177. }
  178. }
  179. }