queue_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. var rands []float32
  149. func makeRands() {
  150. if rands != nil {
  151. return
  152. }
  153. rand.Seed(64738)
  154. for i := 0; i < 4*size; i++ {
  155. rands = append(rands, rand.Float32())
  156. }
  157. }
  158. func BenchmarkRandomQueue(b *testing.B) {
  159. makeRands()
  160. for i := 0; i < b.N; i++ {
  161. var q Queue
  162. for n := 0; n < size; n++ {
  163. if rands[n*4] < 0.8 {
  164. q.PushBack(n)
  165. }
  166. if rands[n*4+1] < 0.8 {
  167. q.PushFront(n)
  168. }
  169. if rands[n*4+2] < 0.5 {
  170. q.PopFront()
  171. }
  172. if rands[n*4+3] < 0.5 {
  173. q.PopBack()
  174. }
  175. }
  176. }
  177. }
  178. func BenchmarkRandomList(b *testing.B) {
  179. makeRands()
  180. for i := 0; i < b.N; i++ {
  181. var q list.List
  182. for n := 0; n < size; n++ {
  183. if rands[n*4] < 0.8 {
  184. q.PushBack(n)
  185. }
  186. if rands[n*4+1] < 0.8 {
  187. q.PushFront(n)
  188. }
  189. if rands[n*4+2] < 0.5 {
  190. if e := q.Front(); e != nil {
  191. q.Remove(e)
  192. }
  193. }
  194. if rands[n*4+3] < 0.5 {
  195. if e := q.Back(); e != nil {
  196. q.Remove(e)
  197. }
  198. }
  199. }
  200. }
  201. }