Skip to content

Instantly share code, notes, and snippets.

@mcclane
Last active November 15, 2018 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcclane/8e72a282613adbe4b03cfda33cd692e7 to your computer and use it in GitHub Desktop.
Save mcclane/8e72a282613adbe4b03cfda33cd692e7 to your computer and use it in GitHub Desktop.
import unittest
import random
from heap import *
class TestHeap(unittest.TestCase):
def test_01_enqueue(self):
"""Test simple enqueue operations and build heap"""
test_heap = MaxHeap(7)
test_heap.build_heap([2, 9, 7, 6, 5, 8])
insert = test_heap.enqueue(10)
self.assertTrue(insert)
self.assertEqual(test_heap.contents(), [10, 6, 9, 2, 5, 7, 8])
def test_enqueue_full(self):
"""Test enqueue when the heap is full"""
test_heap = MaxHeap(7)
test_heap.build_heap([7,6,5,4,3,2,1])
self.assertEqual(test_heap.dequeue(), 7)
self.assertTrue(test_heap.enqueue(4), True)
self.assertFalse(test_heap.enqueue(999))
def test_peek(self):
"""Test peaking the queue"""
test_heap = MaxHeap(7)
test_heap.build_heap([2, 9, 7, 6, 5, 8])
insert = test_heap.enqueue(10)
self.assertTrue(insert)
self.assertEqual(test_heap.contents(), [10, 6, 9, 2, 5, 7, 8])
self.assertEqual(test_heap.peek(), 10)
def test_02_dequeue(self):
"""Test dequeue operation"""
test_heap = MaxHeap()
test_heap.build_heap([2, 9, 7, 6, 5, 8])
self.assertEqual(test_heap.dequeue(), 9)
self.assertEqual(test_heap.get_size(), 5)
self.assertEqual(test_heap.contents(), [8, 6, 7, 2, 5])
def test_03_heap_contents(self):
"""Test gettitng the contents of the heap"""
test_heap = MaxHeap(8)
test_heap.build_heap([1, 2, 3])
self.assertEqual(test_heap.contents(), [3, 2, 1])
def test_04_build_heap(self):
"""Test building a heap"""
test_heap = MaxHeap(8)
test_heap.build_heap([2, 9, 7, 6, 5, 8])
self.assertEqual(test_heap.contents(), [9, 6, 8, 2, 5, 7])
def test_05_is_empty(self):
"""make sure the heap is empty"""
test_heap = MaxHeap(5)
self.assertTrue(test_heap.is_empty())
test_heap.enqueue(10)
self.assertFalse(test_heap.is_empty())
def test_06_is_full(self):
"""Make sure the heap is full when its capacity is reached"""
test_heap = MaxHeap(5)
self.assertFalse(test_heap.is_full())
test_heap.build_heap([1, 2, 3, 4, 5])
self.assertTrue(test_heap.is_full())
def test_07_get_heap_cap(self):
"""Make test getting the capacity of the heap"""
test_heap = MaxHeap(7)
test_heap.build_heap([2, 9, 7, 6, 5, 8])
insert = test_heap.enqueue(10)
self.assertEqual(test_heap.get_capacity(), 7)
self.assertTrue(insert)
def test_08_get_size(self):
"""Test getting the size of the heap"""
test_heap = MaxHeap()
test_heap.build_heap([2, 9, 7, 6, 5, 8])
self.assertEqual(test_heap.get_size(), 6)
def test_09_perc_down(self):
"""Test the perc_down method of the heap"""
test_heap = MaxHeap()
test_heap.build_heap([2, 9, 8, 6, 5, 7])
test_heap.perc_down(1)
self.assertEqual(test_heap.contents(), [9, 6, 8, 2, 5, 7])
def test_10_perc_up(self):
"""Test the perc up method of the heap"""
test_heap = MaxHeap()
test_heap.build_heap([2, 9, 8, 6, 5, 7])
test_heap.perc_up(6)
self.assertEqual(test_heap.contents(), [9, 6, 8, 2, 5, 7])
def test_11_heap_sort_ascending(self):
"""Test the heap sort"""
test_heap = MaxHeap()
list1 = [2, 9, 7, 6, 5, 8]
test_heap.heap_sort_ascending(list1)
self.assertEqual(list1, [2, 5, 6, 7, 8, 9])
def test_empty_build_heap(self):
"""Test building an empty heap"""
test_heap = MaxHeap()
l = []
test_heap.build_heap(l)
self.assertEqual(test_heap.contents(), [])
def test_build_heap_increase_capacity(self):
"""Test building the heap with an increasing capacity"""
test_heap = MaxHeap(2)
l = [1,2,3,4]
self.assertEqual(test_heap.get_capacity(), 2)
test_heap.build_heap(l)
self.assertEqual(test_heap.get_capacity(), 4)
def test_heap_long(self):
test_heap = MaxHeap(1000)
nums = [ i for i in range(0, 1000) ]
for i in nums:
test_heap.enqueue(i)
for i in nums[::-1]:
self.assertEqual(test_heap.dequeue(), i)
def test_heap_sort_long(self):
n = 1000
nums = random.sample(range(10000000), n)
randoms_answer = nums.copy()
randoms_answer.sort()
test_heap = MaxHeap()
test_heap.heap_sort_ascending(nums)
self.assertListEqual(randoms_answer, nums)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment