|
| 1 | +require 'minitest/autorun' |
| 2 | +require_relative 'max_heap' |
| 3 | + |
| 4 | +class TestMaxHeap < Minitest::Test |
| 5 | + def test_to_array_returns_array_representation |
| 6 | + heap = MaxHeap.new([4, 1, 3, 3, 16, 9, 10, 14, 8, 7]) |
| 7 | + assert heap.to_array == [16, 14, 10, 8, 7, 9, 3, 3, 4, 1] |
| 8 | + end |
| 9 | + |
| 10 | + def test_empty_returns_true_for_empty_heap |
| 11 | + heap = MaxHeap.new |
| 12 | + assert heap.empty? |
| 13 | + end |
| 14 | + |
| 15 | + def test_empty_returns_false_for_non_empty_heap |
| 16 | + heap = MaxHeap.new([1]) |
| 17 | + assert !heap.empty? |
| 18 | + end |
| 19 | + |
| 20 | + def test_max_returns_maximum_heap_element |
| 21 | + heap = MaxHeap.new([4, 1, 3]) |
| 22 | + assert heap.max == 4 |
| 23 | + end |
| 24 | + |
| 25 | + def test_max_returns_nil_if_empty_heap |
| 26 | + heap = MaxHeap.new |
| 27 | + assert heap.max.nil? |
| 28 | + end |
| 29 | + |
| 30 | + def test_extract_max_returns_and_removes_maximum_heap_element |
| 31 | + heap = MaxHeap.new([4, 1, 3]) |
| 32 | + assert heap.extract_max == 4 |
| 33 | + assert heap.to_array == [3, 1] |
| 34 | + end |
| 35 | + |
| 36 | + def test_extract_max_returns_nil_if_empty_heap |
| 37 | + heap = MaxHeap.new |
| 38 | + assert heap.extract_max.nil? |
| 39 | + end |
| 40 | + |
| 41 | + def test_insert_adds_element_to_appropriate_position |
| 42 | + heap = MaxHeap.new([4, 1, 3]) |
| 43 | + heap.insert(2) |
| 44 | + assert heap.to_array == [4, 2, 3, 1] |
| 45 | + end |
| 46 | +end |
0 commit comments