2001-08-12 02:22:27 +00:00
|
|
|
"""Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
|
|
|
|
|
|
|
|
import unittest
|
2008-05-20 21:35:26 +00:00
|
|
|
from test.support import run_unittest
|
2001-08-12 02:22:27 +00:00
|
|
|
|
|
|
|
class UnaryOpTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_negative(self):
|
2009-06-30 23:06:06 +00:00
|
|
|
self.assertTrue(-2 == 0 - 2)
|
2007-07-11 12:20:59 +00:00
|
|
|
self.assertEqual(-0, 0)
|
|
|
|
self.assertEqual(--2, 2)
|
2009-06-30 23:06:06 +00:00
|
|
|
self.assertTrue(-2 == 0 - 2)
|
|
|
|
self.assertTrue(-2.0 == 0 - 2.0)
|
|
|
|
self.assertTrue(-2j == 0 - 2j)
|
2001-08-12 02:22:27 +00:00
|
|
|
|
|
|
|
def test_positive(self):
|
2007-07-11 12:20:59 +00:00
|
|
|
self.assertEqual(+2, 2)
|
|
|
|
self.assertEqual(+0, 0)
|
|
|
|
self.assertEqual(++2, 2)
|
|
|
|
self.assertEqual(+2, 2)
|
|
|
|
self.assertEqual(+2.0, 2.0)
|
|
|
|
self.assertEqual(+2j, 2j)
|
2001-08-12 02:22:27 +00:00
|
|
|
|
|
|
|
def test_invert(self):
|
2009-06-30 23:06:06 +00:00
|
|
|
self.assertTrue(-2 == 0 - 2)
|
2007-07-11 12:20:59 +00:00
|
|
|
self.assertEqual(-0, 0)
|
|
|
|
self.assertEqual(--2, 2)
|
2009-06-30 23:06:06 +00:00
|
|
|
self.assertTrue(-2 == 0 - 2)
|
2001-08-12 02:22:27 +00:00
|
|
|
|
2001-08-27 21:45:32 +00:00
|
|
|
def test_no_overflow(self):
|
|
|
|
nines = "9" * 32
|
2009-06-30 23:06:06 +00:00
|
|
|
self.assertTrue(eval("+" + nines) == 10**32-1)
|
|
|
|
self.assertTrue(eval("-" + nines) == -(10**32-1))
|
|
|
|
self.assertTrue(eval("~" + nines) == ~(10**32-1))
|
2001-08-12 02:22:27 +00:00
|
|
|
|
2001-08-30 19:15:20 +00:00
|
|
|
def test_negation_of_exponentiation(self):
|
|
|
|
# Make sure '**' does the right thing; these form a
|
|
|
|
# regression test for SourceForge bug #456756.
|
|
|
|
self.assertEqual(-2 ** 3, -8)
|
|
|
|
self.assertEqual((-2) ** 3, -8)
|
|
|
|
self.assertEqual(-2 ** 4, -16)
|
|
|
|
self.assertEqual((-2) ** 4, 16)
|
|
|
|
|
2001-08-12 02:22:27 +00:00
|
|
|
def test_bad_types(self):
|
|
|
|
for op in '+', '-', '~':
|
2007-05-18 16:50:52 +00:00
|
|
|
self.assertRaises(TypeError, eval, op + "b'a'")
|
2001-08-12 02:22:27 +00:00
|
|
|
self.assertRaises(TypeError, eval, op + "'a'")
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, eval, "~2j")
|
|
|
|
self.assertRaises(TypeError, eval, "~2.0")
|
|
|
|
|
2001-09-20 21:33:42 +00:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(UnaryOpTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|