Kaydet (Commit) d7be1186 authored tarafından Neal Norwitz's avatar Neal Norwitz

Exercise some error conditions

üst 739a8f86
...@@ -38,6 +38,10 @@ class TestHeap(unittest.TestCase): ...@@ -38,6 +38,10 @@ class TestHeap(unittest.TestCase):
# 2) Check that the invariant holds for a sorted array # 2) Check that the invariant holds for a sorted array
self.check_invariant(results) self.check_invariant(results)
self.assertRaises(TypeError, heappush, [])
self.assertRaises(TypeError, heappush, None, None)
self.assertRaises(TypeError, heappop, None)
def check_invariant(self, heap): def check_invariant(self, heap):
# Check the heap invariant. # Check the heap invariant.
for pos, item in enumerate(heap): for pos, item in enumerate(heap):
...@@ -51,6 +55,8 @@ class TestHeap(unittest.TestCase): ...@@ -51,6 +55,8 @@ class TestHeap(unittest.TestCase):
heapify(heap) heapify(heap)
self.check_invariant(heap) self.check_invariant(heap)
self.assertRaises(TypeError, heapify, None)
def test_naive_nbest(self): def test_naive_nbest(self):
data = [random.randrange(2000) for i in range(1000)] data = [random.randrange(2000) for i in range(1000)]
heap = [] heap = []
...@@ -75,6 +81,10 @@ class TestHeap(unittest.TestCase): ...@@ -75,6 +81,10 @@ class TestHeap(unittest.TestCase):
heapreplace(heap, item) heapreplace(heap, item)
self.assertEqual(list(heapiter(heap)), sorted(data)[-10:]) self.assertEqual(list(heapiter(heap)), sorted(data)[-10:])
self.assertRaises(TypeError, heapreplace, None)
self.assertRaises(TypeError, heapreplace, None, None)
self.assertRaises(IndexError, heapreplace, [], None)
def test_heapsort(self): def test_heapsort(self):
# Exercise everything with repeated heapsort checks # Exercise everything with repeated heapsort checks
for trial in xrange(100): for trial in xrange(100):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment