blob: 6de36f59aea33d3ce2254e49e1a1f64c1e6bca2e [file] [log] [blame]
Guido van Rossum9c00f422003-02-12 17:09:17 +00001"""Test correct treatment of hex/oct constants.
2
3This is complex because of changes due to PEP 237.
4
Walter Dörwald3ea7cc32003-02-12 23:49:57 +00005Some of these tests will have to change in Python 2.4!
Guido van Rossum9c00f422003-02-12 17:09:17 +00006"""
7
8import unittest
9from test import test_support
10
11import warnings
12warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
13 "<string>")
14
15class TextHexOct(unittest.TestCase):
16
17 def test_hex_baseline(self):
18 # Baseline tests
19 self.assertEqual(0x0, 0)
20 self.assertEqual(0x10, 16)
21 self.assertEqual(0x7fffffff, 2147483647)
22 # Ditto with a minus sign and parentheses
23 self.assertEqual(-(0x0), 0)
24 self.assertEqual(-(0x10), -16)
25 self.assertEqual(-(0x7fffffff), -2147483647)
26 # Ditto with a minus sign and NO parentheses
27 self.assertEqual(-0x0, 0)
28 self.assertEqual(-0x10, -16)
29 self.assertEqual(-0x7fffffff, -2147483647)
30
31 def test_hex_unsigned(self):
32 # This test is in a <string> so we can ignore the warnings
33 exec """if 1:
34 # Positive-looking constants with negavive values
35 self.assertEqual(0x80000000, -2147483648L)
36 self.assertEqual(0xffffffff, -1)
37 # Ditto with a minus sign and parentheses
38 self.assertEqual(-(0x80000000), 2147483648L)
39 self.assertEqual(-(0xffffffff), 1)
40 # Ditto with a minus sign and NO parentheses
41 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
42 self.assertEqual(-0x80000000, 2147483648L)
43 self.assertEqual(-0xffffffff, 1)
44 \n"""
45
46 def test_oct_baseline(self):
47 # Baseline tests
48 self.assertEqual(00, 0)
49 self.assertEqual(020, 16)
50 self.assertEqual(017777777777, 2147483647)
51 # Ditto with a minus sign and parentheses
52 self.assertEqual(-(00), 0)
53 self.assertEqual(-(020), -16)
54 self.assertEqual(-(017777777777), -2147483647)
55 # Ditto with a minus sign and NO parentheses
56 self.assertEqual(-00, 0)
57 self.assertEqual(-020, -16)
58 self.assertEqual(-017777777777, -2147483647)
59
60 def test_oct_unsigned(self):
61 # This test is in a <string> so we can ignore the warnings
62 exec """if 1:
63 # Positive-looking constants with negavive values
64 self.assertEqual(020000000000, -2147483648L)
65 self.assertEqual(037777777777, -1)
66 # Ditto with a minus sign and parentheses
67 self.assertEqual(-(020000000000), 2147483648L)
68 self.assertEqual(-(037777777777), 1)
69 # Ditto with a minus sign and NO parentheses
70 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
71 self.assertEqual(-020000000000, 2147483648L)
72 self.assertEqual(-037777777777, 1)
73 \n"""
74
75def test_main():
76 suite = unittest.TestSuite()
77 suite.addTest(unittest.makeSuite(TextHexOct))
78 test_support.run_suite(suite)
79
80if __name__ == "__main__":
81 test_main()