queue_test.go 3.1 KB

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