test_queueutils.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. from boltons.queueutils import SortedPriorityQueue, HeapPriorityQueue
  3. def _test_priority_queue(queue_type):
  4. pq = queue_type()
  5. item1 = 'a'
  6. item2 = 'b'
  7. item3 = 'c'
  8. pq.add(item1)
  9. pq.remove(item1)
  10. # integer priorities
  11. pq.add(item1, 2)
  12. pq.add(item2, 9)
  13. pq.add(item3, 7)
  14. assert len(pq) == 3
  15. assert item2 == pq.pop()
  16. assert len(pq) == 2
  17. assert item3 == pq.pop()
  18. assert len(pq) == 1
  19. assert item1 == pq.pop()
  20. assert len(pq) == 0
  21. # float priorities
  22. pq.add(item1, 0.2)
  23. pq.add(item2, 0.9)
  24. pq.add(item3, 0.7)
  25. assert len(pq) == 3
  26. assert item2 == pq.pop()
  27. assert len(pq) == 2
  28. assert item3 == pq.pop()
  29. assert len(pq) == 1
  30. assert item1 == pq.pop()
  31. assert len(pq) == 0
  32. try:
  33. pq.pop()
  34. except IndexError:
  35. pass
  36. else:
  37. assert False, 'priority queue should be empty'
  38. return
  39. def test_heap_queue():
  40. _test_priority_queue(HeapPriorityQueue)
  41. def test_sorted_queue():
  42. _test_priority_queue(SortedPriorityQueue)