|
1 """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2""" |
|
2 |
|
3 import unittest |
|
4 from test.test_support import run_unittest, have_unicode |
|
5 |
|
6 class UnaryOpTestCase(unittest.TestCase): |
|
7 |
|
8 def test_negative(self): |
|
9 self.assert_(-2 == 0 - 2) |
|
10 self.assert_(-0 == 0) |
|
11 self.assert_(--2 == 2) |
|
12 self.assert_(-2L == 0 - 2L) |
|
13 self.assert_(-2.0 == 0 - 2.0) |
|
14 self.assert_(-2j == 0 - 2j) |
|
15 |
|
16 def test_positive(self): |
|
17 self.assert_(+2 == 2) |
|
18 self.assert_(+0 == 0) |
|
19 self.assert_(++2 == 2) |
|
20 self.assert_(+2L == 2L) |
|
21 self.assert_(+2.0 == 2.0) |
|
22 self.assert_(+2j == 2j) |
|
23 |
|
24 def test_invert(self): |
|
25 self.assert_(-2 == 0 - 2) |
|
26 self.assert_(-0 == 0) |
|
27 self.assert_(--2 == 2) |
|
28 self.assert_(-2L == 0 - 2L) |
|
29 |
|
30 def test_no_overflow(self): |
|
31 nines = "9" * 32 |
|
32 self.assert_(eval("+" + nines) == eval("+" + nines + "L")) |
|
33 self.assert_(eval("-" + nines) == eval("-" + nines + "L")) |
|
34 self.assert_(eval("~" + nines) == eval("~" + nines + "L")) |
|
35 |
|
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 |
|
44 def test_bad_types(self): |
|
45 for op in '+', '-', '~': |
|
46 self.assertRaises(TypeError, eval, op + "'a'") |
|
47 if have_unicode: |
|
48 self.assertRaises(TypeError, eval, op + "u'a'") |
|
49 |
|
50 self.assertRaises(TypeError, eval, "~2j") |
|
51 self.assertRaises(TypeError, eval, "~2.0") |
|
52 |
|
53 |
|
54 def test_main(): |
|
55 run_unittest(UnaryOpTestCase) |
|
56 |
|
57 |
|
58 if __name__ == "__main__": |
|
59 test_main() |