blob: 7dd651e8db28e123a45b5a5b9303a66738cca8c7 [file] [log] [blame]
Jeremy Hylton08a64032001-08-12 02:22:27 +00001"""Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
2
3import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest
Jeremy Hylton08a64032001-08-12 02:22:27 +00005
6class UnaryOpTestCase(unittest.TestCase):
7
8 def test_negative(self):
9 self.assert_(-2 == 0 - 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +000010 self.assertEqual(-0, 0)
11 self.assertEqual(--2, 2)
Guido van Rossume2a383d2007-01-15 16:59:06 +000012 self.assert_(-2 == 0 - 2)
Jeremy Hylton08a64032001-08-12 02:22:27 +000013 self.assert_(-2.0 == 0 - 2.0)
14 self.assert_(-2j == 0 - 2j)
15
16 def test_positive(self):
Guido van Rossume61fd5b2007-07-11 12:20:59 +000017 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 Hylton08a64032001-08-12 02:22:27 +000023
24 def test_invert(self):
25 self.assert_(-2 == 0 - 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +000026 self.assertEqual(-0, 0)
27 self.assertEqual(--2, 2)
Guido van Rossume2a383d2007-01-15 16:59:06 +000028 self.assert_(-2 == 0 - 2)
Jeremy Hylton08a64032001-08-12 02:22:27 +000029
Tim Petersc15a8282001-08-27 21:45:32 +000030 def test_no_overflow(self):
31 nines = "9" * 32
Guido van Rossume2a383d2007-01-15 16:59:06 +000032 self.assert_(eval("+" + nines) == 10**32-1)
33 self.assert_(eval("-" + nines) == -(10**32-1))
34 self.assert_(eval("~" + nines) == ~(10**32-1))
Jeremy Hylton08a64032001-08-12 02:22:27 +000035
Fred Drake702ca4f2001-08-30 19:15:20 +000036 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 Hylton08a64032001-08-12 02:22:27 +000044 def test_bad_types(self):
45 for op in '+', '-', '~':
Walter Dörwald4e618b72007-05-18 16:50:52 +000046 self.assertRaises(TypeError, eval, op + "b'a'")
Jeremy Hylton08a64032001-08-12 02:22:27 +000047 self.assertRaises(TypeError, eval, op + "'a'")
Jeremy Hylton08a64032001-08-12 02:22:27 +000048
49 self.assertRaises(TypeError, eval, "~2j")
50 self.assertRaises(TypeError, eval, "~2.0")
51
Fred Drake2e2be372001-09-20 21:33:42 +000052
53def test_main():
54 run_unittest(UnaryOpTestCase)
55
56
57if __name__ == "__main__":
58 test_main()