queue_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package queue
  2. import "testing"
  3. func TestQueueSimple(t *testing.T) {
  4. q := New()
  5. for i := 0; i < minQueueLen; i++ {
  6. q.Add(i)
  7. }
  8. for i := 0; i < minQueueLen; i++ {
  9. if q.Peek().(int) != i {
  10. t.Error("peek", i, "had value", q.Peek())
  11. }
  12. x := q.Remove()
  13. if x != i {
  14. t.Error("remove", i, "had value", x)
  15. }
  16. }
  17. }
  18. func TestQueueWrapping(t *testing.T) {
  19. q := New()
  20. for i := 0; i < minQueueLen; i++ {
  21. q.Add(i)
  22. }
  23. for i := 0; i < 3; i++ {
  24. q.Remove()
  25. q.Add(minQueueLen + i)
  26. }
  27. for i := 0; i < minQueueLen; i++ {
  28. if q.Peek().(int) != i+3 {
  29. t.Error("peek", i, "had value", q.Peek())
  30. }
  31. q.Remove()
  32. }
  33. }
  34. func TestQueueLength(t *testing.T) {
  35. q := New()
  36. if q.Length() != 0 {
  37. t.Error("empty queue length not 0")
  38. }
  39. for i := 0; i < 1000; i++ {
  40. q.Add(i)
  41. if q.Length() != i+1 {
  42. t.Error("adding: queue with", i, "elements has length", q.Length())
  43. }
  44. }
  45. for i := 0; i < 1000; i++ {
  46. q.Remove()
  47. if q.Length() != 1000-i-1 {
  48. t.Error("removing: queue with", 1000-i-i, "elements has length", q.Length())
  49. }
  50. }
  51. }
  52. func TestQueueGet(t *testing.T) {
  53. q := New()
  54. for i := 0; i < 1000; i++ {
  55. q.Add(i)
  56. for j := 0; j < q.Length(); j++ {
  57. if q.Get(j).(int) != j {
  58. t.Errorf("index %d doesn't contain %d", j, j)
  59. }
  60. }
  61. }
  62. }
  63. func TestQueueGetNegative(t *testing.T) {
  64. q := New()
  65. for i := 0; i < 1000; i++ {
  66. q.Add(i)
  67. for j := 1; j <= q.Length(); j++ {
  68. if q.Get(-j).(int) != q.Length()-j {
  69. t.Errorf("index %d doesn't contain %d", -j, q.Length()-j)
  70. }
  71. }
  72. }
  73. }
  74. func TestQueueGetOutOfRangePanics(t *testing.T) {
  75. q := New()
  76. q.Add(1)
  77. q.Add(2)
  78. q.Add(3)
  79. assertPanics(t, "should panic when negative index", func() {
  80. q.Get(-4)
  81. })
  82. assertPanics(t, "should panic when index greater than length", func() {
  83. q.Get(4)
  84. })
  85. }
  86. func TestQueuePeekOutOfRangePanics(t *testing.T) {
  87. q := New()
  88. assertPanics(t, "should panic when peeking empty queue", func() {
  89. q.Peek()
  90. })
  91. q.Add(1)
  92. q.Remove()
  93. assertPanics(t, "should panic when peeking emptied queue", func() {
  94. q.Peek()
  95. })
  96. }
  97. func TestQueueRemoveOutOfRangePanics(t *testing.T) {
  98. q := New()
  99. assertPanics(t, "should panic when removing empty queue", func() {
  100. q.Remove()
  101. })
  102. q.Add(1)
  103. q.Remove()
  104. assertPanics(t, "should panic when removing emptied queue", func() {
  105. q.Remove()
  106. })
  107. }
  108. func assertPanics(t *testing.T, name string, f func()) {
  109. defer func() {
  110. if r := recover(); r == nil {
  111. t.Errorf("%s: didn't panic as expected", name)
  112. }
  113. }()
  114. f()
  115. }
  116. // General warning: Go's benchmark utility (go test -bench .) increases the number of
  117. // iterations until the benchmarks take a reasonable amount of time to run; memory usage
  118. // is *NOT* considered. On my machine, these benchmarks hit around ~1GB before they've had
  119. // enough, but if you have less than that available and start swapping, then all bets are off.
  120. func BenchmarkQueueSerial(b *testing.B) {
  121. q := New()
  122. for i := 0; i < b.N; i++ {
  123. q.Add(nil)
  124. }
  125. for i := 0; i < b.N; i++ {
  126. q.Peek()
  127. q.Remove()
  128. }
  129. }
  130. func BenchmarkQueueGet(b *testing.B) {
  131. q := New()
  132. for i := 0; i < b.N; i++ {
  133. q.Add(i)
  134. }
  135. b.ResetTimer()
  136. for i := 0; i < b.N; i++ {
  137. q.Get(i)
  138. }
  139. }
  140. func BenchmarkQueueTickTock(b *testing.B) {
  141. q := New()
  142. for i := 0; i < b.N; i++ {
  143. q.Add(nil)
  144. q.Peek()
  145. q.Remove()
  146. }
  147. }