-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_spec.rb
More file actions
66 lines (59 loc) · 2.12 KB
/
heap_spec.rb
File metadata and controls
66 lines (59 loc) · 2.12 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'spec_helper'
RSpec.describe Heap do
subject(:heap) { described_class.new(list) }
let(:list) { [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] }
describe "#index_of_parent" do
it 'get parent' do
expect(heap.index_of_parent(0)).to be_nil
expect(heap.index_of_parent(1)).to eq(0)
expect(heap.index_of_parent(2)).to eq(0)
expect(heap.index_of_parent(3)).to eq(1)
expect(heap.index_of_parent(4)).to eq(1)
end
end
describe "#value_of_parent" do
it 'get parent value' do
expect(heap.value_of_parent(0)).to be_nil
expect(heap.value_of_parent(1)).to eq(16)
expect(heap.value_of_parent(2)).to eq(16)
expect(heap.value_of_parent(3)).to eq(14)
expect(heap.value_of_parent(4)).to eq(14)
end
end
describe "#index_of_left_child" do
it 'get index_of_left_child' do
expect(heap.index_of_left_child(0)).to eq(1)
expect(heap.index_of_left_child(1)).to eq(3)
expect(heap.index_of_left_child(2)).to eq(5)
expect(heap.index_of_left_child(4)).to eq(9)
expect(heap.index_of_left_child(5)).to be_nil
end
end
describe "#value_of_left_child" do
it 'get index_of_left_child' do
expect(heap.value_of_left_child(0)).to eq(14)
expect(heap.value_of_left_child(1)).to eq(8)
expect(heap.value_of_left_child(2)).to eq(9)
expect(heap.value_of_left_child(4)).to eq(1)
expect(heap.value_of_left_child(5)).to be_nil
end
end
describe "#index_of_right_child" do
it 'get index_of_right_child' do
expect(heap.index_of_right_child(0)).to eq(2)
expect(heap.index_of_right_child(1)).to eq(4)
expect(heap.index_of_right_child(2)).to eq(6)
expect(heap.index_of_right_child(4)).to be_nil
expect(heap.index_of_right_child(5)).to be_nil
end
end
describe "#value_of_right_child" do
it 'get value_of_right_child' do
expect(heap.value_of_right_child(0)).to eq(10)
expect(heap.value_of_right_child(1)).to eq(7)
expect(heap.value_of_right_child(2)).to eq(3)
expect(heap.value_of_right_child(4)).to be_nil
expect(heap.value_of_right_child(5)).to be_nil
end
end
end