summaryrefslogtreecommitdiff
path: root/spec/data_structures/heap_spec.rb
blob: d1761da634d5cae377a36a0b6016dee2fc534f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require "spec_helper"

describe Heap do
  context :min do
    context "emtpy" do
      it "returns nil" do
        heap = Heap.new
        expect(heap.min).to be_nil
        expect(heap).to be_empty
      end
    end

    context "full" do
      it "returns the items order from lowest to highest" do
        n = 10
        numbers = Array.new(n) { rand(n) }
        heap = Heap.heapify(numbers.dup)

        results = []
        results << heap.min until heap.empty?
        expect(results).to eql(numbers.sort)
      end
    end
  end

  context :max do
    it "returns nil when empty" do
      heap = Heap.new
      expect(heap.max).to be_nil
      expect(heap).to be_empty
    end

    it "returns the items in order from highest to lowest" do
      n = 10
      numbers = Array.new(n) { rand(n) }
      heap = Heap.heapify(numbers.dup)
      results = []
      results.push(heap.max) until heap.empty?
      expect(results).to eql(numbers.sort.reverse)
    end
  end
end