PageRenderTime 37ms CodeModel.GetById 29ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/specs/data/heap.ds

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