main
1require "spec_helper"
2
3describe Heap do
4 context :min do
5 context "emtpy" do
6 it "returns nil" do
7 heap = Heap.new
8 expect(heap.min).to be_nil
9 expect(heap).to be_empty
10 end
11 end
12
13 context "full" do
14 it "returns the items order from lowest to highest" do
15 n = 10
16 numbers = Array.new(n) { rand(n) }
17 heap = Heap.heapify(numbers.dup)
18
19 results = []
20 results << heap.min until heap.empty?
21 expect(results).to eql(numbers.sort)
22 end
23 end
24 end
25
26 context :max do
27 it "returns nil when empty" do
28 heap = Heap.new
29 expect(heap.max).to be_nil
30 expect(heap).to be_empty
31 end
32
33 it "returns the items in order from highest to lowest" do
34 n = 10
35 numbers = Array.new(n) { rand(n) }
36 heap = Heap.heapify(numbers.dup)
37 results = []
38 results.push(heap.max) until heap.empty?
39 expect(results).to eql(numbers.sort.reverse)
40 end
41 end
42end