queue_test.go 2.4 KB

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