forked from prakhar1989/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_test.py
More file actions
38 lines (31 loc) · 1.23 KB
/
heap_test.py
File metadata and controls
38 lines (31 loc) · 1.23 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
import os, sys
sys.path.append(os.path.join(os.getcwd(), os.path.pardir))
import unittest
from heaps.minheap import minheap
from heaps.maxheap import maxheap
import random
class test_heap(unittest.TestCase):
def setUp(self):
self.h = minheap()
self.m = maxheap()
self.a = [random.choice(range(50)) for i in range(10)]
self.h.build_heap(self.a)
self.m.build_heap(self.a)
def test_heap_pop(self):
self.assertEqual(min(self.a), self.h.heappop())
self.assertEqual(max(self.a), self.m.heappop())
def test_max_elements(self):
self.assertEqual(len(self.a), self.h.max_elements())
self.assertEqual(len(self.a), self.m.max_elements())
def test_heap_sort(self):
sorted_h = [self.h.heappop() for i in range(self.h.max_elements())]
sorted_m = [self.m.heappop() for i in range(self.m.max_elements())]
self.assertEqual(sorted_h, sorted(self.a))
self.assertEqual(sorted_m, sorted(self.a, reverse=True))
def test_heap_push_method(self):
self.h.heappush(-1)
self.assertEqual(-1, self.h.heappop())
self.m.heappush(100)
self.assertEqual(100, self.m.heappop())
if __name__ == "__main__":
unittest.main()