def test_large_operands(self): x = Range(10 ** 20, 10 ** 20 + 10, 3) self.assertEqual(len(x), 4) self.assertEqual(len(list(x)), 4) x = Range(10 ** 20 + 10, 10 ** 20, 3) self.assertEqual(len(x), 0) self.assertEqual(len(list(x)), 0) # self.assertFalse(x) assert Bool(False) == x.__bool__() x = Range(10 ** 20, 10 ** 20 + 10, -3) self.assertEqual(len(x), 0) self.assertEqual(len(list(x)), 0) # self.assertFalse(x) assert Bool(False) == x.__bool__() x = Range(10 ** 20 + 10, 10 ** 20, -3) self.assertEqual(len(x), 4) self.assertEqual(len(list(x)), 4) # self.assertTrue(x) assert Bool(True) == x.__bool__() # Now test Range() with longs for x in [Range(-(2 ** 100)), Range(0, -(2 ** 100)), Range(0, 2 ** 100, -1)]: self.assertEqual(list(x), []) # self.assertFalse(x) assert Bool(False) == x.__bool__() a = int(10 * sys.maxsize) b = int(100 * sys.maxsize) c = int(50 * sys.maxsize) self.assertEqual(list(Range(a, a + 2)), [a, a + 1]) self.assertEqual(list(Range(a + 2, a, -1)), [a + 2, a + 1]) self.assertEqual(list(Range(a + 4, a, -2)), [a + 4, a + 2]) seq = list(Range(a, b, c)) self.assertIn(a, seq) self.assertNotIn(b, seq) self.assertEqual(len(seq), 2) self.assertEqual(seq[0], a) self.assertEqual(seq[-1], a + c) seq = list(Range(b, a, -c)) self.assertIn(b, seq) self.assertNotIn(a, seq) self.assertEqual(len(seq), 2) self.assertEqual(seq[0], b) self.assertEqual(seq[-1], b - c) seq = list(Range(-a, -b, -c)) self.assertIn(-a, seq) self.assertNotIn(-b, seq) self.assertEqual(len(seq), 2) self.assertEqual(seq[0], -a) self.assertEqual(seq[-1], -a - c)
def test_large_Range(self): # Check long Ranges (len > sys.maxsize) # len() is expected to fail due to limitations of the __len__ protocol def _Range_len(x): try: length = len(x) except OverflowError: step = x[1] - x[0] length = 1 + ((x[-1] - x[0]) // step) return length a = -sys.maxsize b = sys.maxsize expected_len = b - a x = Range(a, b) self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) assert Bool(True) == x.__bool__() self.assertEqual(_Range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize + 1 self.assertEqual(x[idx], a + idx) self.assertEqual(x[idx : idx + 1][0], a + idx) # noqa: E203 with self.assertRaises(IndexError): x[-expected_len - 1] with self.assertRaises(IndexError): x[expected_len] a = 0 b = 2 * sys.maxsize expected_len = b - a x = Range(a, b) self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) assert Bool(True) == x.__bool__() self.assertEqual(_Range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize + 1 self.assertEqual(x[idx], a + idx) self.assertEqual(x[idx : idx + 1][0], a + idx) # noqa: E203 with self.assertRaises(IndexError): x[-expected_len - 1] with self.assertRaises(IndexError): x[expected_len] a = 0 b = sys.maxsize ** 10 c = 2 * sys.maxsize expected_len = 1 + (b - a) // c x = Range(a, b, c) self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) assert Bool(True) == x.__bool__() self.assertEqual(_Range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize + 1 self.assertEqual(x[idx], a + (idx * c)) self.assertEqual(x[idx : idx + 1][0], a + (idx * c)) # noqa: E203 with self.assertRaises(IndexError): x[-expected_len - 1] with self.assertRaises(IndexError): x[expected_len] a = sys.maxsize ** 10 b = 0 c = -2 * sys.maxsize expected_len = 1 + (b - a) // c x = Range(a, b, c) self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) assert Bool(True) == x.__bool__() self.assertEqual(_Range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize + 1 self.assertEqual(x[idx], a + (idx * c)) self.assertEqual(x[idx : idx + 1][0], a + (idx * c)) # noqa: E203 with self.assertRaises(IndexError): x[-expected_len - 1] with self.assertRaises(IndexError): x[expected_len]