PyXR

c:\python24\lib \ test \ test_unary.py



0001 """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
0002 
0003 import unittest
0004 from test.test_support import run_unittest, have_unicode
0005 
0006 class UnaryOpTestCase(unittest.TestCase):
0007 
0008     def test_negative(self):
0009         self.assert_(-2 == 0 - 2)
0010         self.assert_(-0 == 0)
0011         self.assert_(--2 == 2)
0012         self.assert_(-2L == 0 - 2L)
0013         self.assert_(-2.0 == 0 - 2.0)
0014         self.assert_(-2j == 0 - 2j)
0015 
0016     def test_positive(self):
0017         self.assert_(+2 == 2)
0018         self.assert_(+0 == 0)
0019         self.assert_(++2 == 2)
0020         self.assert_(+2L == 2L)
0021         self.assert_(+2.0 == 2.0)
0022         self.assert_(+2j == 2j)
0023 
0024     def test_invert(self):
0025         self.assert_(-2 == 0 - 2)
0026         self.assert_(-0 == 0)
0027         self.assert_(--2 == 2)
0028         self.assert_(-2L == 0 - 2L)
0029 
0030     def test_no_overflow(self):
0031         nines = "9" * 32
0032         self.assert_(eval("+" + nines) == eval("+" + nines + "L"))
0033         self.assert_(eval("-" + nines) == eval("-" + nines + "L"))
0034         self.assert_(eval("~" + nines) == eval("~" + nines + "L"))
0035 
0036     def test_negation_of_exponentiation(self):
0037         # Make sure '**' does the right thing; these form a
0038         # regression test for SourceForge bug #456756.
0039         self.assertEqual(-2 ** 3, -8)
0040         self.assertEqual((-2) ** 3, -8)
0041         self.assertEqual(-2 ** 4, -16)
0042         self.assertEqual((-2) ** 4, 16)
0043 
0044     def test_bad_types(self):
0045         for op in '+', '-', '~':
0046             self.assertRaises(TypeError, eval, op + "'a'")
0047             if have_unicode:
0048                 self.assertRaises(TypeError, eval, op + "u'a'")
0049 
0050         self.assertRaises(TypeError, eval, "~2j")
0051         self.assertRaises(TypeError, eval, "~2.0")
0052 
0053 
0054 def test_main():
0055     run_unittest(UnaryOpTestCase)
0056 
0057 
0058 if __name__ == "__main__":
0059     test_main()
0060 

Generated by PyXR 0.9.4
SourceForge.net Logo