priorityqueue_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package sortutil
  10. import (
  11. "fmt"
  12. "testing"
  13. )
  14. func TestPriorityQueue(t *testing.T) {
  15. pq := NewPriorityQueue()
  16. if pq.CurrentPriority() != 0 {
  17. t.Error("Unexpected priority:", pq.CurrentPriority())
  18. return
  19. }
  20. pq.Push("test1", 1)
  21. pq.Push("test8", 8)
  22. pq.Push("test2", 2)
  23. pq.Push("test5", 5)
  24. // Check contents:
  25. if res := fmt.Sprint(pq); res != "[ test1 (1) test5 (5) test2 (2) test8 (8) ]" {
  26. t.Error("Unexpected queue layout:", res)
  27. return
  28. }
  29. if pq.CurrentPriority() != 1 {
  30. t.Error("Unexpected priority:", pq.CurrentPriority())
  31. return
  32. }
  33. if pq.Size() != 4 {
  34. t.Error("Unexpected size:", pq.Size())
  35. return
  36. }
  37. if pq.SizeCurrentPriority() != 1 {
  38. t.Error("Unexpected size:", pq.SizeCurrentPriority())
  39. return
  40. }
  41. // Set minpriority function
  42. pq.MinPriority = func() int {
  43. return 1
  44. }
  45. peek := pq.Peek()
  46. if res := pq.Pop(); res != "test1" && res == peek {
  47. t.Error("Unexpected pop result:", res)
  48. return
  49. }
  50. if res := fmt.Sprint(pq); res != "[ test2 (2) test5 (5) test8 (8) ]" {
  51. t.Error("Unexpected queue layout:", res)
  52. return
  53. }
  54. peek = pq.Peek()
  55. if res := pq.Pop(); res != nil && res == peek {
  56. t.Error("Unexpected pop result:", res)
  57. return
  58. }
  59. if res := pq.Size(); res != 0 {
  60. t.Error("Unexpected pop result:", res)
  61. return
  62. }
  63. if res := pq.SizeCurrentPriority(); res != 0 {
  64. t.Error("Unexpected pop result:", res)
  65. return
  66. }
  67. pq.MinPriority = func() int { return -1 }
  68. peek = pq.Peek()
  69. if res := pq.Pop(); res != "test2" && res == peek {
  70. t.Error("Unexpected pop result:", res)
  71. return
  72. }
  73. peek = pq.Peek()
  74. if pq.CurrentPriority() != 5 {
  75. t.Error("Unexpected priority:", pq.CurrentPriority())
  76. return
  77. }
  78. if res := pq.Pop(); res != "test5" && res == peek {
  79. t.Error("Unexpected pop result:", res)
  80. return
  81. }
  82. if pq.CurrentPriority() != 8 {
  83. t.Error("Unexpected priority:", pq.CurrentPriority())
  84. return
  85. }
  86. peek = pq.Peek()
  87. if res := pq.Pop(); res != "test8" && res == peek {
  88. t.Error("Unexpected pop result:", res)
  89. return
  90. }
  91. pq.Push("test2", 9)
  92. if pq.CurrentPriority() != 9 {
  93. t.Error("Unexpected priority:", pq.CurrentPriority())
  94. return
  95. }
  96. pq.Clear()
  97. if pq.CurrentPriority() != 0 {
  98. t.Error("Unexpected priority:", pq.CurrentPriority())
  99. return
  100. }
  101. if res := pq.Size(); res != 0 {
  102. t.Error("Unexpected pop result:", res)
  103. return
  104. }
  105. if res := fmt.Sprint(pq); res != "[ ]" {
  106. t.Error("Unexpected queue layout:", res)
  107. return
  108. }
  109. // Test we can use it as a normal queue
  110. pq.Push("test1", 0)
  111. pq.Push("test8", -1)
  112. pq.Push("test2", 0)
  113. pq.Push("test5", 0)
  114. if res := pq.Size(); res != 4 {
  115. t.Error("Unexpected pop result:", res)
  116. return
  117. }
  118. if res := pq.SizeCurrentPriority(); res != 4 {
  119. t.Error("Unexpected pop result:", res)
  120. return
  121. }
  122. pq.MinPriority = func() int {
  123. return 0
  124. }
  125. peek = pq.Peek()
  126. if res := pq.Pop(); res != "test1" && res == peek {
  127. t.Error("Unexpected pop result:", res)
  128. return
  129. }
  130. if res := pq.Size(); res != 3 {
  131. t.Error("Unexpected pop result:", res)
  132. return
  133. }
  134. peek = pq.Peek()
  135. if res := pq.Pop(); res != "test8" && res == peek {
  136. t.Error("Unexpected pop result:", res)
  137. return
  138. }
  139. peek = pq.Peek()
  140. if res := pq.Pop(); res != "test2" && res == peek {
  141. t.Error("Unexpected pop result:", res)
  142. return
  143. }
  144. peek = pq.Peek()
  145. if res := pq.Pop(); res != "test5" && res == peek {
  146. t.Error("Unexpected pop result:", res)
  147. return
  148. }
  149. if pq.CurrentPriority() != 0 {
  150. t.Error("Unexpected priority:", pq.CurrentPriority())
  151. return
  152. }
  153. }