Back to index

python3.2  3.2.2
test_unary.py
Go to the documentation of this file.
00001 """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
00002 
00003 import unittest
00004 from test.support import run_unittest
00005 
00006 class UnaryOpTestCase(unittest.TestCase):
00007 
00008     def test_negative(self):
00009         self.assertTrue(-2 == 0 - 2)
00010         self.assertEqual(-0, 0)
00011         self.assertEqual(--2, 2)
00012         self.assertTrue(-2 == 0 - 2)
00013         self.assertTrue(-2.0 == 0 - 2.0)
00014         self.assertTrue(-2j == 0 - 2j)
00015 
00016     def test_positive(self):
00017         self.assertEqual(+2, 2)
00018         self.assertEqual(+0, 0)
00019         self.assertEqual(++2, 2)
00020         self.assertEqual(+2, 2)
00021         self.assertEqual(+2.0, 2.0)
00022         self.assertEqual(+2j, 2j)
00023 
00024     def test_invert(self):
00025         self.assertTrue(-2 == 0 - 2)
00026         self.assertEqual(-0, 0)
00027         self.assertEqual(--2, 2)
00028         self.assertTrue(-2 == 0 - 2)
00029 
00030     def test_no_overflow(self):
00031         nines = "9" * 32
00032         self.assertTrue(eval("+" + nines) == 10**32-1)
00033         self.assertTrue(eval("-" + nines) == -(10**32-1))
00034         self.assertTrue(eval("~" + nines) == ~(10**32-1))
00035 
00036     def test_negation_of_exponentiation(self):
00037         # Make sure '**' does the right thing; these form a
00038         # regression test for SourceForge bug #456756.
00039         self.assertEqual(-2 ** 3, -8)
00040         self.assertEqual((-2) ** 3, -8)
00041         self.assertEqual(-2 ** 4, -16)
00042         self.assertEqual((-2) ** 4, 16)
00043 
00044     def test_bad_types(self):
00045         for op in '+', '-', '~':
00046             self.assertRaises(TypeError, eval, op + "b'a'")
00047             self.assertRaises(TypeError, eval, op + "'a'")
00048 
00049         self.assertRaises(TypeError, eval, "~2j")
00050         self.assertRaises(TypeError, eval, "~2.0")
00051 
00052 
00053 def test_main():
00054     run_unittest(UnaryOpTestCase)
00055 
00056 
00057 if __name__ == "__main__":
00058     test_main()