2001-08-11 23:22:27 -03:00
|
|
|
"""Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
|
|
|
|
|
|
|
|
import unittest
|
2002-07-23 16:04:11 -03:00
|
|
|
from test.test_support import run_unittest, have_unicode
|
2001-08-11 23:22:27 -03:00
|
|
|
|
|
|
|
class UnaryOpTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_negative(self):
|
|
|
|
self.assert_(-2 == 0 - 2)
|
|
|
|
self.assert_(-0 == 0)
|
|
|
|
self.assert_(--2 == 2)
|
2007-01-15 12:59:06 -04:00
|
|
|
self.assert_(-2 == 0 - 2)
|
2001-08-11 23:22:27 -03:00
|
|
|
self.assert_(-2.0 == 0 - 2.0)
|
|
|
|
self.assert_(-2j == 0 - 2j)
|
|
|
|
|
|
|
|
def test_positive(self):
|
|
|
|
self.assert_(+2 == 2)
|
|
|
|
self.assert_(+0 == 0)
|
|
|
|
self.assert_(++2 == 2)
|
2007-01-15 12:59:06 -04:00
|
|
|
self.assert_(+2 == 2)
|
2001-08-11 23:22:27 -03:00
|
|
|
self.assert_(+2.0 == 2.0)
|
|
|
|
self.assert_(+2j == 2j)
|
|
|
|
|
|
|
|
def test_invert(self):
|
|
|
|
self.assert_(-2 == 0 - 2)
|
|
|
|
self.assert_(-0 == 0)
|
|
|
|
self.assert_(--2 == 2)
|
2007-01-15 12:59:06 -04:00
|
|
|
self.assert_(-2 == 0 - 2)
|
2001-08-11 23:22:27 -03:00
|
|
|
|
2001-08-27 18:45:32 -03:00
|
|
|
def test_no_overflow(self):
|
|
|
|
nines = "9" * 32
|
2007-01-15 12:59:06 -04:00
|
|
|
self.assert_(eval("+" + nines) == 10**32-1)
|
|
|
|
self.assert_(eval("-" + nines) == -(10**32-1))
|
|
|
|
self.assert_(eval("~" + nines) == ~(10**32-1))
|
2001-08-11 23:22:27 -03:00
|
|
|
|
2001-08-30 16:15:20 -03: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-11 23:22:27 -03:00
|
|
|
def test_bad_types(self):
|
|
|
|
for op in '+', '-', '~':
|
|
|
|
self.assertRaises(TypeError, eval, op + "'a'")
|
2002-05-20 11:24:53 -03:00
|
|
|
if have_unicode:
|
|
|
|
self.assertRaises(TypeError, eval, op + "u'a'")
|
2001-08-11 23:22:27 -03:00
|
|
|
|
|
|
|
self.assertRaises(TypeError, eval, "~2j")
|
|
|
|
self.assertRaises(TypeError, eval, "~2.0")
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(UnaryOpTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|