Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 1 | """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2""" |
| 2 | |
| 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import run_unittest |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 5 | |
| 6 | class UnaryOpTestCase(unittest.TestCase): |
| 7 | |
| 8 | def test_negative(self): |
| 9 | self.assert_(-2 == 0 - 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 10 | self.assertEqual(-0, 0) |
| 11 | self.assertEqual(--2, 2) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 12 | self.assert_(-2 == 0 - 2) |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 13 | self.assert_(-2.0 == 0 - 2.0) |
| 14 | self.assert_(-2j == 0 - 2j) |
| 15 | |
| 16 | def test_positive(self): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 17 | self.assertEqual(+2, 2) |
| 18 | self.assertEqual(+0, 0) |
| 19 | self.assertEqual(++2, 2) |
| 20 | self.assertEqual(+2, 2) |
| 21 | self.assertEqual(+2.0, 2.0) |
| 22 | self.assertEqual(+2j, 2j) |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 23 | |
| 24 | def test_invert(self): |
| 25 | self.assert_(-2 == 0 - 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 26 | self.assertEqual(-0, 0) |
| 27 | self.assertEqual(--2, 2) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 28 | self.assert_(-2 == 0 - 2) |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 29 | |
Tim Peters | c15a828 | 2001-08-27 21:45:32 +0000 | [diff] [blame] | 30 | def test_no_overflow(self): |
| 31 | nines = "9" * 32 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 32 | self.assert_(eval("+" + nines) == 10**32-1) |
| 33 | self.assert_(eval("-" + nines) == -(10**32-1)) |
| 34 | self.assert_(eval("~" + nines) == ~(10**32-1)) |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 35 | |
Fred Drake | 702ca4f | 2001-08-30 19:15:20 +0000 | [diff] [blame] | 36 | def test_negation_of_exponentiation(self): |
| 37 | # Make sure '**' does the right thing; these form a |
| 38 | # regression test for SourceForge bug #456756. |
| 39 | self.assertEqual(-2 ** 3, -8) |
| 40 | self.assertEqual((-2) ** 3, -8) |
| 41 | self.assertEqual(-2 ** 4, -16) |
| 42 | self.assertEqual((-2) ** 4, 16) |
| 43 | |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 44 | def test_bad_types(self): |
| 45 | for op in '+', '-', '~': |
Walter Dörwald | 4e618b7 | 2007-05-18 16:50:52 +0000 | [diff] [blame] | 46 | self.assertRaises(TypeError, eval, op + "b'a'") |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 47 | self.assertRaises(TypeError, eval, op + "'a'") |
Jeremy Hylton | 08a6403 | 2001-08-12 02:22:27 +0000 | [diff] [blame] | 48 | |
| 49 | self.assertRaises(TypeError, eval, "~2j") |
| 50 | self.assertRaises(TypeError, eval, "~2.0") |
| 51 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 52 | |
| 53 | def test_main(): |
| 54 | run_unittest(UnaryOpTestCase) |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | test_main() |