summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unary.py
blob: 598fca8fcffbbb0864c24283a476af7a4343cdde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""

import unittest
from test.test_support import run_unittest, have_unicode

class UnaryOpTestCase(unittest.TestCase):

    def test_negative(self):
        self.assertTrue(-2 == 0 - 2)
        self.assertTrue(-0 == 0)
        self.assertTrue(--2 == 2)
        self.assertTrue(-2L == 0 - 2L)
        self.assertTrue(-2.0 == 0 - 2.0)
        self.assertTrue(-2j == 0 - 2j)

    def test_positive(self):
        self.assertTrue(+2 == 2)
        self.assertTrue(+0 == 0)
        self.assertTrue(++2 == 2)
        self.assertTrue(+2L == 2L)
        self.assertTrue(+2.0 == 2.0)
        self.assertTrue(+2j == 2j)

    def test_invert(self):
        self.assertTrue(-2 == 0 - 2)
        self.assertTrue(-0 == 0)
        self.assertTrue(--2 == 2)
        self.assertTrue(-2L == 0 - 2L)

    def test_no_overflow(self):
        nines = "9" * 32
        self.assertTrue(eval("+" + nines) == eval("+" + nines + "L"))
        self.assertTrue(eval("-" + nines) == eval("-" + nines + "L"))
        self.assertTrue(eval("~" + nines) == eval("~" + nines + "L"))

    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)

    def test_bad_types(self):
        for op in '+', '-', '~':
            self.assertRaises(TypeError, eval, op + "'a'")
            if have_unicode:
                self.assertRaises(TypeError, eval, op + "u'a'")

        self.assertRaises(TypeError, eval, "~2j")
        self.assertRaises(TypeError, eval, "~2.0")


def test_main():
    run_unittest(UnaryOpTestCase)


if __name__ == "__main__":
    test_main()