Skip to content

Commit 1764be7

Browse files
committed
New test for upcasting newints to floats.
1 parent 6c63c07 commit 1764be7

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

future/tests/test_int.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def __trunc__(self):
398398
####################################################################
399399
# future-specific tests are below:
400400
####################################################################
401-
401+
402402
# Exception messages in Py2 are 8-bit strings. The following fails,
403403
# even if the testlist strings are wrapped in str() calls...
404404
@expectedFailurePY2
@@ -618,6 +618,43 @@ class longsubclass(long):
618618
print(b.dtype)
619619
assert b.dtype == np.int64 == c.dtype
620620

621+
def test_upcasting_to_floats(self):
622+
"""
623+
Integers should automatically be upcasted to floats for arithmetic
624+
operations.
625+
"""
626+
a = int(3)
627+
628+
# Addition with floats.
629+
self.assertEqual(a + 0.5, 3.5)
630+
self.assertEqual(0.5 + a, 3.5)
631+
self.assertTrue(isinstance(a + 0.5, float))
632+
self.assertTrue(isinstance(0.5 + a, float))
633+
634+
# Subtraction with floats.
635+
self.assertEqual(a - 0.5, 2.5)
636+
self.assertEqual(0.5 - a, -2.5)
637+
self.assertTrue(isinstance(a - 0.5, float))
638+
self.assertTrue(isinstance(0.5 - a, float))
639+
640+
# Multiplication with floats.
641+
self.assertEqual(a * 0.5, 1.5)
642+
self.assertEqual(0.5 * a, 1.5)
643+
self.assertTrue(isinstance(a * 0.5, float))
644+
self.assertTrue(isinstance(0.5 * a, float))
645+
646+
# Division with floats.
647+
self.assertEqual(a / 0.5, 6.0)
648+
self.assertEqual(0.5 / a, 0.5 / 3.0)
649+
self.assertTrue(isinstance(a / 0.5, float))
650+
self.assertTrue(isinstance(0.5 / a, float))
651+
652+
# Modulo with floats.
653+
self.assertEqual(a % 0.5, 0.0)
654+
self.assertEqual(0.5 % a, 0.5)
655+
self.assertTrue(isinstance(a % 0.5, float))
656+
self.assertTrue(isinstance(0.5 % a, float))
657+
621658

622659
if __name__ == "__main__":
623660
unittest.main()

0 commit comments

Comments
 (0)