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