Skip to content

Commit 9ea63f7

Browse files
committed
Merge branch 'posita-newint-advanced-div-144' into v0.14.x
2 parents be141c6 + a028655 commit 9ea63f7

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/future/types/newint.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,16 @@ def __rmod__(self, other):
201201

202202
def __divmod__(self, other):
203203
value = super(newint, self).__divmod__(other)
204+
if value is NotImplemented:
205+
mylong = long(self)
206+
return (mylong // other, mylong % other)
204207
return (newint(value[0]), newint(value[1]))
205208

206209
def __rdivmod__(self, other):
207210
value = super(newint, self).__rdivmod__(other)
211+
if value is NotImplemented:
212+
mylong = long(self)
213+
return (other // mylong, other % mylong)
208214
return (newint(value[0]), newint(value[1]))
209215

210216
def __pow__(self, other):

tests/test_future/test_int.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def cpython_only(f):
4949

5050
class IntTestCases(unittest.TestCase):
5151

52+
def setUp(self):
53+
self.longMessage = True
54+
5255
def test_isinstance_int_subclass(self):
5356
"""
5457
Issue #89
@@ -454,6 +457,24 @@ def test_divmod(self):
454457
assert divmod(int(x), int(-y)) == divmod(x, -y)
455458
assert divmod(int(-x), int(-y)) == divmod(-x, -y)
456459

460+
assert divmod(int(x), float(y)) == divmod(x, float(y))
461+
assert divmod(int(-x), float(y)) == divmod(-x, float(y))
462+
assert divmod(int(x), float(-y)) == divmod(x, float(-y))
463+
assert divmod(int(-x), float(-y)) == divmod(-x, float(-y))
464+
465+
def _frange(x, y, step):
466+
_x = x ; i = 0
467+
while _x < y:
468+
yield _x
469+
i += 1 ; _x = x + i * step
470+
471+
for i in range(20):
472+
for d in _frange(0.005, 5.0, 0.005):
473+
self.assertEqual(divmod(int(i), d), divmod(i, d), msg='i={0}; d={1}'.format(i, d))
474+
self.assertEqual(divmod(int(-i), d), divmod(-i, d), msg='i={0}; d={1}'.format(i, d))
475+
self.assertEqual(divmod(int(i), -d), divmod(i, -d), msg='i={0}; d={1}'.format(i, d))
476+
self.assertEqual(divmod(int(-i), -d), divmod(-i, -d), msg='i={0}; d={1}'.format(i, d))
477+
457478
def test_div(self):
458479
"""
459480
Issue #38

tests/test_future/test_int_old_division.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
is not in effect.
77
"""
88

9-
from __future__ import (absolute_import,
9+
from __future__ import (absolute_import,
1010
print_function, unicode_literals)
1111
from future import standard_library
1212
from future.builtins import *
@@ -20,14 +20,18 @@
2020
@unittest.skipIf(not PY2, 'old division tests only for Py2')
2121
class IntTestCasesOldDivision(unittest.TestCase):
2222

23+
def setUp(self):
24+
self.longMessage = True
25+
26+
2327
def test_div(self):
2428
"""
2529
Issue #38
2630
"""
2731
a = int(3)
2832
self.assertEqual(a / 5., 0.6)
2933
self.assertEqual(a / 5, 0)
30-
34+
3135

3236
def test_idiv(self):
3337
a = int(3)
@@ -42,7 +46,7 @@ def test_idiv(self):
4246
c /= 2.0
4347
self.assertEqual(c, -1.5)
4448
self.assertTrue(isinstance(c, float))
45-
49+
4650

4751
def test_truediv(self):
4852
"""
@@ -74,5 +78,24 @@ def test_truediv(self):
7478
self.assertTrue(isinstance(e, int))
7579

7680

81+
def test_divmod(self):
82+
"""
83+
Test int.__divmod__
84+
"""
85+
vals = [10**i for i in range(0, 20)]
86+
for i in range(200):
87+
x = random.choice(vals)
88+
y = random.choice(vals)
89+
self.assertEqual(int(y).__rdivmod__(int(x)), divmod(x, y), msg='x={0}; y={1}'.format(x, y))
90+
self.assertEqual(int(-y).__rdivmod__(int(x)), divmod(x, -y), msg='x={0}; y={1}'.format(x, y))
91+
self.assertEqual(int(y).__rdivmod__(int(-x)), divmod(-x, y), msg='x={0}; y={1}'.format(x, y))
92+
self.assertEqual(int(-y).__rdivmod__(int(-x)), divmod(-x, -y), msg='x={0}; y={1}'.format(x, y))
93+
94+
self.assertEqual(int(x).__rdivmod__(int(y)), long(x).__rdivmod__(y), msg='x={0}; y={1}'.format(x, y))
95+
self.assertEqual(int(-x).__rdivmod__(int(y)), long(-x).__rdivmod__(y), msg='x={0}; y={1}'.format(x, y))
96+
self.assertEqual(int(x).__rdivmod__(int(-y)), long(x).__rdivmod__(-y), msg='x={0}; y={1}'.format(x, y))
97+
self.assertEqual(int(-x).__rdivmod__(int(-y)), long(-x).__rdivmod__(-y), msg='x={0}; y={1}'.format(x, y))
98+
99+
77100
if __name__ == "__main__":
78101
unittest.main()

0 commit comments

Comments
 (0)