PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/specs/data/fibonacci.ds

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