• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
2
3import unittest
4
5class UnaryOpTestCase(unittest.TestCase):
6
7    def test_negative(self):
8        self.assertTrue(-2 == 0 - 2)
9        self.assertEqual(-0, 0)
10        self.assertEqual(--2, 2)
11        self.assertTrue(-2 == 0 - 2)
12        self.assertTrue(-2.0 == 0 - 2.0)
13        self.assertTrue(-2j == 0 - 2j)
14
15    def test_positive(self):
16        self.assertEqual(+2, 2)
17        self.assertEqual(+0, 0)
18        self.assertEqual(++2, 2)
19        self.assertEqual(+2, 2)
20        self.assertEqual(+2.0, 2.0)
21        self.assertEqual(+2j, 2j)
22
23    def test_invert(self):
24        self.assertTrue(-2 == 0 - 2)
25        self.assertEqual(-0, 0)
26        self.assertEqual(--2, 2)
27        self.assertTrue(-2 == 0 - 2)
28
29    def test_no_overflow(self):
30        nines = "9" * 32
31        self.assertTrue(eval("+" + nines) == 10**32-1)
32        self.assertTrue(eval("-" + nines) == -(10**32-1))
33        self.assertTrue(eval("~" + nines) == ~(10**32-1))
34
35    def test_negation_of_exponentiation(self):
36        # Make sure '**' does the right thing; these form a
37        # regression test for SourceForge bug #456756.
38        self.assertEqual(-2 ** 3, -8)
39        self.assertEqual((-2) ** 3, -8)
40        self.assertEqual(-2 ** 4, -16)
41        self.assertEqual((-2) ** 4, 16)
42
43    def test_bad_types(self):
44        for op in '+', '-', '~':
45            self.assertRaises(TypeError, eval, op + "b'a'")
46            self.assertRaises(TypeError, eval, op + "'a'")
47
48        self.assertRaises(TypeError, eval, "~2j")
49        self.assertRaises(TypeError, eval, "~2.0")
50
51
52if __name__ == "__main__":
53    unittest.main()
54