summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-30 06:47:07 (GMT)
committerGitHub <noreply@github.com>2017-03-30 06:47:07 (GMT)
commit918403cfc3304d27e80fb792357f40bb3ba69c4e (patch)
tree7c8a004d82a24b715b3c3b1f61a474d77c9ef0f7 /Lib
parent762bf40438a572a398e500c74e38f9894ea20a45 (diff)
downloadcpython-918403cfc3304d27e80fb792357f40bb3ba69c4e.zip
cpython-918403cfc3304d27e80fb792357f40bb3ba69c4e.tar.gz
cpython-918403cfc3304d27e80fb792357f40bb3ba69c4e.tar.bz2
bpo-29816: Shift operation now has less opportunity to raise OverflowError. (#680)
ValueError always is raised rather than OverflowError for negative counts. Shifting zero with non-negative count always returns zero.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_long.py32
1 files changed, 30 insertions, 2 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index fd15f04..cc48259 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -906,11 +906,24 @@ class LongTest(unittest.TestCase):
self.check_truediv(-x, y)
self.check_truediv(-x, -y)
+ def test_negative_shift_count(self):
+ with self.assertRaises(ValueError):
+ 42 << -3
+ with self.assertRaises(ValueError):
+ 42 << -(1 << 1000)
+ with self.assertRaises(ValueError):
+ 42 >> -3
+ with self.assertRaises(ValueError):
+ 42 >> -(1 << 1000)
+
def test_lshift_of_zero(self):
self.assertEqual(0 << 0, 0)
self.assertEqual(0 << 10, 0)
with self.assertRaises(ValueError):
0 << -1
+ self.assertEqual(0 << (1 << 1000), 0)
+ with self.assertRaises(ValueError):
+ 0 << -(1 << 1000)
@support.cpython_only
def test_huge_lshift_of_zero(self):
@@ -918,8 +931,23 @@ class LongTest(unittest.TestCase):
# Other implementations may have a different boundary for overflow,
# or not raise at all.
self.assertEqual(0 << sys.maxsize, 0)
- with self.assertRaises(OverflowError):
- 0 << (sys.maxsize + 1)
+ self.assertEqual(0 << (sys.maxsize + 1), 0)
+
+ @support.cpython_only
+ @support.bigmemtest(sys.maxsize + 1000, memuse=2/15 * 2, dry_run=False)
+ def test_huge_lshift(self, size):
+ self.assertEqual(1 << (sys.maxsize + 1000), 1 << 1000 << sys.maxsize)
+
+ def test_huge_rshift(self):
+ self.assertEqual(42 >> (1 << 1000), 0)
+ self.assertEqual((-42) >> (1 << 1000), -1)
+
+ @support.cpython_only
+ @support.bigmemtest(sys.maxsize + 500, memuse=2/15, dry_run=False)
+ def test_huge_rshift_of_huge(self, size):
+ huge = ((1 << 500) + 11) << sys.maxsize
+ self.assertEqual(huge >> (sys.maxsize + 1), (1 << 499) + 5)
+ self.assertEqual(huge >> (sys.maxsize + 1000), 0)
def test_small_ints(self):
for i in range(-5, 257):