queue_test.go 3.9 KB

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