summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-09-22 21:47:24 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-09-22 21:47:24 (GMT)
commit3e124ae739db8dc6ad43687d6b14466a572a6f8e (patch)
treec3923f748294a32af0b47a455ff91391c3dffd54 /Lib
parent2df811362213d9e987193772d3bf835a204f2727 (diff)
downloadcpython-3e124ae739db8dc6ad43687d6b14466a572a6f8e.zip
cpython-3e124ae739db8dc6ad43687d6b14466a572a6f8e.tar.gz
cpython-3e124ae739db8dc6ad43687d6b14466a572a6f8e.tar.bz2
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
does an O(1) check, if n is an integer. Non-integers aren't affected. Thanks Robert Lehmann.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_range.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index 24836d0..1a40dd1 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -78,6 +78,56 @@ class RangeTest(unittest.TestCase):
with self.assertRaises(TypeError):
range([], 1, -1)
+ def test_types(self):
+ # Non-integer objects *equal* to any of the range's items are supposed
+ # to be contained in the range.
+ self.assertTrue(1.0 in range(3))
+ self.assertTrue(True in range(3))
+ self.assertTrue(1+0j in range(3))
+
+ class C1:
+ def __eq__(self, other): return True
+ self.assertTrue(C1() in range(3))
+
+ # Objects are never coerced into other types for comparison.
+ class C2:
+ def __int__(self): return 1
+ def __index__(self): return 1
+ self.assertFalse(C2() in range(3))
+ # ..except if explicitly told so.
+ self.assertTrue(int(C2()) in range(3))
+
+
+ def test_strided_limits(self):
+ r = range(0, 101, 2)
+ self.assertTrue(0 in r)
+ self.assertFalse(1 in r)
+ self.assertTrue(2 in r)
+ self.assertFalse(99 in r)
+ self.assertTrue(100 in r)
+ self.assertFalse(101 in r)
+
+ r = range(0, -20, -1)
+ self.assertTrue(0 in r)
+ self.assertTrue(-1 in r)
+ self.assertTrue(-19 in r)
+ self.assertFalse(-20 in r)
+
+ r = range(0, -20, -2)
+ self.assertTrue(-18 in r)
+ self.assertFalse(-19 in r)
+ self.assertFalse(-20 in r)
+
+ def test_empty(self):
+ r = range(0)
+ self.assertFalse(0 in r)
+ self.assertFalse(1 in r)
+
+ r = range(0, -10)
+ self.assertFalse(0 in r)
+ self.assertFalse(-1 in r)
+ self.assertFalse(1 in r)
+
def test_main():
test.support.run_unittest(RangeTest)