[Python-checkins] r64120 - python/trunk/Lib/test/test_heapq.py

raymond.hettinger python-checkins at python.org
Wed Jun 11 15:14:51 CEST 2008


Author: raymond.hettinger
Date: Wed Jun 11 15:14:50 2008
New Revision: 64120

Log:
Add test for heapq using both __lt__ and __le__.

Modified:
   python/trunk/Lib/test/test_heapq.py

Modified: python/trunk/Lib/test/test_heapq.py
==============================================================================
--- python/trunk/Lib/test/test_heapq.py	(original)
+++ python/trunk/Lib/test/test_heapq.py	Wed Jun 11 15:14:50 2008
@@ -196,6 +196,27 @@
 class TestHeapC(TestHeap):
     module = c_heapq
 
+    def test_comparison_operator(self):
+        # Issue 3501: Make sure heapq works with both __lt__ and __le__
+        def hsort(data, comp):
+            data = map(comp, data)
+            self.module.heapify(data)
+            return [self.module.heappop(data).x for i in range(len(data))]
+        class LT:
+            def __init__(self, x):
+                self.x = x
+            def __lt__(self, other):
+                return self.x > other.x
+        class LE:
+            def __init__(self, x):
+                self.x = x
+            def __lt__(self, other):
+                return self.x >= other.x
+        data = [random.random() for i in range(100)]
+        target = sorted(data, reverse=True)
+        self.assertEqual(hsort(data, LT), target)
+        self.assertEqual(hsort(data, LE), target)
+
 
 #==============================================================================
 


More information about the Python-checkins mailing list