queue_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 TestQueueGetOutOfRangePanics(t *testing.T) {
  61. q := New()
  62. q.Add(1)
  63. q.Add(2)
  64. q.Add(3)
  65. assertPanics(t, "should panic when negative index", func() {
  66. q.Get(-1)
  67. })
  68. assertPanics(t, "should panic when index greater than length", func() {
  69. q.Get(4)
  70. })
  71. }
  72. func TestQueuePeekOutOfRangePanics(t *testing.T) {
  73. q := New()
  74. assertPanics(t, "should panic when peeking empty queue", func() {
  75. q.Peek()
  76. })
  77. q.Add(1)
  78. q.Remove()
  79. assertPanics(t, "should panic when peeking emptied queue", func() {
  80. q.Peek()
  81. })
  82. }
  83. func TestQueueRemoveOutOfRangePanics(t *testing.T) {
  84. q := New()
  85. assertPanics(t, "should panic when removing empty queue", func() {
  86. q.Remove()
  87. })
  88. q.Add(1)
  89. q.Remove()
  90. assertPanics(t, "should panic when removing emptied queue", func() {
  91. q.Remove()
  92. })
  93. }
  94. func assertPanics(t *testing.T, name string, f func()) {
  95. defer func() {
  96. if r := recover(); r == nil {
  97. t.Errorf("%s: didn't panic as expected", name)
  98. } else {
  99. t.Logf("%s: got panic as expected: %v", name, r)
  100. }
  101. }()
  102. f()
  103. }
  104. // General warning: Go's benchmark utility (go test -bench .) increases the number of
  105. // iterations until the benchmarks take a reasonable amount of time to run; memory usage
  106. // is *NOT* considered. On my machine, these benchmarks hit around ~1GB before they've had
  107. // enough, but if you have less than that available and start swapping, then all bets are off.
  108. func BenchmarkQueueSerial(b *testing.B) {
  109. q := New()
  110. for i := 0; i < b.N; i++ {
  111. q.Add(nil)
  112. }
  113. for i := 0; i < b.N; i++ {
  114. q.Peek()
  115. q.Remove()
  116. }
  117. }
  118. func BenchmarkQueueGet(b *testing.B) {
  119. q := New()
  120. for i := 0; i < b.N; i++ {
  121. q.Add(i)
  122. }
  123. b.ResetTimer()
  124. for i := 0; i < b.N; i++ {
  125. q.Get(i)
  126. }
  127. }
  128. func BenchmarkQueueTickTock(b *testing.B) {
  129. q := New()
  130. for i := 0; i < b.N; i++ {
  131. q.Add(nil)
  132. q.Peek()
  133. q.Remove()
  134. }
  135. }