/specs/data/heap.ds

http://github.com/wilkie/djehuty · Unknown · 134 lines · 114 code · 20 blank · 0 comment · 0 complexity · 583104e391c6b6a1662b8f07e3f774f6 MD5 · raw file

  1. module specs.data.heap;
  2. import data.heap;
  3. import math.random;
  4. describe priorityQueue() {
  5. describe creation() {
  6. it should_work_as_expected() {
  7. PriorityQueue!(int) queue = new PriorityQueue!(int)();
  8. shouldNot(queue is null);
  9. should(queue.length == 0);
  10. }
  11. }
  12. describe add() {
  13. it should_add_an_item_to_an_empty_list() {
  14. PriorityQueue!(int) queue = new PriorityQueue!(int)();
  15. int item = 42;
  16. queue.add(item);
  17. should(queue.length == 1);
  18. should(queue.peek() == item);
  19. }
  20. }
  21. describe peek() {
  22. it should_return_the_first_item_in_min_heap() {
  23. auto queue = new PriorityQueue!(int, MinHeap);
  24. queue.add(10);
  25. queue.add(4);
  26. queue.add(15);
  27. should(queue.length == 3);
  28. should(queue.peek() == 4);
  29. }
  30. it should_return_the_first_item_in_max_heap() {
  31. auto queue = new PriorityQueue!(int, MaxHeap);
  32. queue.add(10);
  33. queue.add(4);
  34. queue.add(15);
  35. should(queue.length == 3);
  36. should(queue.peek() == 15);
  37. }
  38. it should_handle_a_heavy_workload() {
  39. auto queue = new PriorityQueue!(int, MinHeap);
  40. int min;
  41. int val;
  42. Random r = new Random();
  43. val = cast(int)r.next();
  44. queue.add(val);
  45. min = val;
  46. for(int i; i < 10000; i++) {
  47. val = cast(int)r.next();
  48. queue.add(val);
  49. if (val < min) {
  50. min = val;
  51. }
  52. }
  53. should(queue.peek() == min);
  54. int foo;
  55. int last = min;
  56. while (!queue.empty()) {
  57. foo = queue.remove();
  58. should(foo >= last);
  59. last = foo;
  60. }
  61. }
  62. }
  63. describe remove() {
  64. it should_remove_the_first_item_in_min_heap() {
  65. auto queue = new PriorityQueue!(int, MinHeap);
  66. queue.add(10);
  67. queue.add(4);
  68. queue.add(15);
  69. should(queue.length == 3);
  70. should(queue.remove() == 4);
  71. }
  72. it should_remove_the_first_item_in_max_heap() {
  73. auto queue = new PriorityQueue!(int, MaxHeap);
  74. queue.add(10);
  75. queue.add(4);
  76. queue.add(15);
  77. should(queue.length == 3);
  78. should(queue.remove() == 15);
  79. }
  80. }
  81. describe length() {
  82. it should_be_zero_for_an_empty_list() {
  83. auto queue = new PriorityQueue!(int);
  84. should(queue.empty);
  85. should(queue.length == 0);
  86. }
  87. }
  88. describe clear() {
  89. it should_result_in_an_empty_list() {
  90. auto queue = new PriorityQueue!(int);
  91. queue.add(15);
  92. queue.add(10);
  93. queue.add(24);
  94. shouldNot(queue.length == 0);
  95. shouldNot(queue.empty());
  96. queue.clear();
  97. should(queue.length == 0);
  98. should(queue.empty());
  99. }
  100. }
  101. describe empty() {
  102. it should_be_true_when_the_list_is_empty() {
  103. auto queue = new PriorityQueue!(int);
  104. queue.add(10);
  105. shouldNot(queue.empty());
  106. queue.remove();
  107. should(queue.empty());
  108. }
  109. it should_be_true_for_a_new_list() {
  110. auto queue = new PriorityQueue!(int);
  111. should(queue.empty());
  112. }
  113. }
  114. }