Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 1 | """Unittests for heapq.""" |
| 2 | |
Raymond Hettinger | 33ecffb | 2004-06-10 05:03:17 +0000 | [diff] [blame] | 3 | from heapq import heappush, heappop, heapify, heapreplace, nlargest, nsmallest |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 4 | import random |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | from test import test_support |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 7 | import sys |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 9 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 10 | def heapiter(heap): |
| 11 | # An iterator returning a heap's elements, smallest-first. |
| 12 | try: |
| 13 | while 1: |
| 14 | yield heappop(heap) |
| 15 | except IndexError: |
| 16 | pass |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 17 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 18 | class TestHeap(unittest.TestCase): |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 19 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 20 | def test_push_pop(self): |
| 21 | # 1) Push 256 random numbers and pop them off, verifying all's OK. |
| 22 | heap = [] |
| 23 | data = [] |
| 24 | self.check_invariant(heap) |
| 25 | for i in range(256): |
| 26 | item = random.random() |
| 27 | data.append(item) |
| 28 | heappush(heap, item) |
| 29 | self.check_invariant(heap) |
| 30 | results = [] |
| 31 | while heap: |
| 32 | item = heappop(heap) |
| 33 | self.check_invariant(heap) |
| 34 | results.append(item) |
| 35 | data_sorted = data[:] |
| 36 | data_sorted.sort() |
| 37 | self.assertEqual(data_sorted, results) |
| 38 | # 2) Check that the invariant holds for a sorted array |
| 39 | self.check_invariant(results) |
| 40 | |
| 41 | def check_invariant(self, heap): |
| 42 | # Check the heap invariant. |
| 43 | for pos, item in enumerate(heap): |
| 44 | if pos: # pos 0 has no parent |
| 45 | parentpos = (pos-1) >> 1 |
| 46 | self.assert_(heap[parentpos] <= item) |
| 47 | |
| 48 | def test_heapify(self): |
| 49 | for size in range(30): |
| 50 | heap = [random.random() for dummy in range(size)] |
| 51 | heapify(heap) |
| 52 | self.check_invariant(heap) |
| 53 | |
| 54 | def test_naive_nbest(self): |
| 55 | data = [random.randrange(2000) for i in range(1000)] |
| 56 | heap = [] |
| 57 | for item in data: |
| 58 | heappush(heap, item) |
| 59 | if len(heap) > 10: |
| 60 | heappop(heap) |
| 61 | heap.sort() |
| 62 | self.assertEqual(heap, sorted(data)[-10:]) |
| 63 | |
| 64 | def test_nbest(self): |
| 65 | # Less-naive "N-best" algorithm, much faster (if len(data) is big |
| 66 | # enough <wink>) than sorting all of data. However, if we had a max |
| 67 | # heap instead of a min heap, it could go faster still via |
| 68 | # heapify'ing all of data (linear time), then doing 10 heappops |
| 69 | # (10 log-time steps). |
| 70 | data = [random.randrange(2000) for i in range(1000)] |
| 71 | heap = data[:10] |
| 72 | heapify(heap) |
| 73 | for item in data[10:]: |
| 74 | if item > heap[0]: # this gets rarer the longer we run |
| 75 | heapreplace(heap, item) |
| 76 | self.assertEqual(list(heapiter(heap)), sorted(data)[-10:]) |
| 77 | |
| 78 | def test_heapsort(self): |
| 79 | # Exercise everything with repeated heapsort checks |
| 80 | for trial in xrange(100): |
| 81 | size = random.randrange(50) |
| 82 | data = [random.randrange(25) for i in range(size)] |
| 83 | if trial & 1: # Half of the time, use heapify |
| 84 | heap = data[:] |
| 85 | heapify(heap) |
| 86 | else: # The rest of the time, use heappush |
| 87 | heap = [] |
| 88 | for item in data: |
| 89 | heappush(heap, item) |
| 90 | heap_sorted = [heappop(heap) for i in range(size)] |
| 91 | self.assertEqual(heap_sorted, sorted(data)) |
| 92 | |
| 93 | def test_nsmallest(self): |
| 94 | data = [random.randrange(2000) for i in range(1000)] |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 95 | for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100): |
| 96 | self.assertEqual(nsmallest(data, i), sorted(data)[:i]) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 97 | |
| 98 | def test_largest(self): |
| 99 | data = [random.randrange(2000) for i in range(1000)] |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 100 | for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100): |
| 101 | self.assertEqual(nlargest(data, i), sorted(data, reverse=True)[:i]) |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 102 | |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 103 | def test_main(verbose=None): |
| 104 | test_classes = [TestHeap] |
| 105 | test_support.run_unittest(*test_classes) |
| 106 | |
| 107 | # verify reference counting |
| 108 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 109 | import gc |
| 110 | counts = [None] * 5 |
| 111 | for i in xrange(len(counts)): |
| 112 | test_support.run_unittest(*test_classes) |
| 113 | gc.collect() |
| 114 | counts[i] = sys.gettotalrefcount() |
| 115 | print counts |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 116 | |
| 117 | if __name__ == "__main__": |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 118 | test_main(verbose=True) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 119 | |