queue_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. func TestGrowShrink1(t *testing.T) {
  107. var q Queue
  108. for i := 0; i < size; i++ {
  109. q.PushBack(i)
  110. ensureLength(t, &q, i+1)
  111. }
  112. for i := 0; q.Len() > 0; i++ {
  113. x := q.PopFront().(int)
  114. if x != i {
  115. t.Errorf("q.PopFront() = %d, want %d", x, i)
  116. }
  117. ensureLength(t, &q, size-i-1)
  118. }
  119. }
  120. func TestGrowShrink2(t *testing.T) {
  121. var q Queue
  122. for i := 0; i < size; i++ {
  123. q.PushFront(i)
  124. ensureLength(t, &q, i+1)
  125. }
  126. for i := 0; q.Len() > 0; i++ {
  127. x := q.PopBack().(int)
  128. if x != i {
  129. t.Errorf("q.PopBack() = %d, want %d", x, i)
  130. }
  131. ensureLength(t, &q, size-i-1)
  132. }
  133. }
  134. const size = 1024
  135. func BenchmarkPushFrontQueue(b *testing.B) {
  136. for i := 0; i < b.N; i++ {
  137. var q Queue
  138. for n := 0; n < size; n++ {
  139. q.PushFront(n)
  140. }
  141. }
  142. }
  143. func BenchmarkPushFrontList(b *testing.B) {
  144. for i := 0; i < b.N; i++ {
  145. var q list.List
  146. for n := 0; n < size; n++ {
  147. q.PushFront(n)
  148. }
  149. }
  150. }
  151. func BenchmarkPushBackQueue(b *testing.B) {
  152. for i := 0; i < b.N; i++ {
  153. var q Queue
  154. for n := 0; n < size; n++ {
  155. q.PushBack(n)
  156. }
  157. }
  158. }
  159. func BenchmarkPushBackList(b *testing.B) {
  160. for i := 0; i < b.N; i++ {
  161. var q list.List
  162. for n := 0; n < size; n++ {
  163. q.PushBack(n)
  164. }
  165. }
  166. }
  167. func BenchmarkPushBackChannel(b *testing.B) {
  168. for i := 0; i < b.N; i++ {
  169. q := make(chan interface{}, size)
  170. for n := 0; n < size; n++ {
  171. q <- n
  172. }
  173. close(q)
  174. }
  175. }
  176. var rands []float32
  177. func makeRands() {
  178. if rands != nil {
  179. return
  180. }
  181. rand.Seed(64738)
  182. for i := 0; i < 4*size; i++ {
  183. rands = append(rands, rand.Float32())
  184. }
  185. }
  186. func BenchmarkRandomQueue(b *testing.B) {
  187. makeRands()
  188. b.ResetTimer()
  189. for i := 0; i < b.N; i++ {
  190. var q Queue
  191. for n := 0; n < 4*size; n += 4 {
  192. if rands[n] < 0.8 {
  193. q.PushBack(n)
  194. }
  195. if rands[n+1] < 0.8 {
  196. q.PushFront(n)
  197. }
  198. if rands[n+2] < 0.5 {
  199. q.PopFront()
  200. }
  201. if rands[n+3] < 0.5 {
  202. q.PopBack()
  203. }
  204. }
  205. }
  206. }
  207. func BenchmarkRandomList(b *testing.B) {
  208. makeRands()
  209. b.ResetTimer()
  210. for i := 0; i < b.N; i++ {
  211. var q list.List
  212. for n := 0; n < 4*size; n += 4 {
  213. if rands[n] < 0.8 {
  214. q.PushBack(n)
  215. }
  216. if rands[n+1] < 0.8 {
  217. q.PushFront(n)
  218. }
  219. if rands[n+2] < 0.5 {
  220. if e := q.Front(); e != nil {
  221. q.Remove(e)
  222. }
  223. }
  224. if rands[n+3] < 0.5 {
  225. if e := q.Back(); e != nil {
  226. q.Remove(e)
  227. }
  228. }
  229. }
  230. }
  231. }
  232. func BenchmarkGrowShrinkQueue(b *testing.B) {
  233. for i := 0; i < b.N; i++ {
  234. var q Queue
  235. for n := 0; n < size; n++ {
  236. q.PushBack(i)
  237. }
  238. for n := 0; n < size; n++ {
  239. q.PopFront()
  240. }
  241. }
  242. }
  243. func BenchmarkGrowShrinkList(b *testing.B) {
  244. for i := 0; i < b.N; i++ {
  245. var q list.List
  246. for n := 0; n < size; n++ {
  247. q.PushBack(i)
  248. }
  249. for n := 0; n < size; n++ {
  250. if e := q.Front(); e != nil {
  251. q.Remove(e)
  252. }
  253. }
  254. }
  255. }