blob: a14a2c2696478940e23df80c90d14a3366ae741b [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
Neal Norwitzdcfdceb2003-02-18 15:45:44 +00008import sys
9platform_long_is_32_bits = sys.maxint == 2147483647
10
Guido van Rossum9c00f422003-02-12 17:09:17 +000011import unittest
12from test import test_support
13
14import warnings
15warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
16 "<string>")
17
18class TextHexOct(unittest.TestCase):
19
20 def test_hex_baseline(self):
21 # Baseline tests
22 self.assertEqual(0x0, 0)
23 self.assertEqual(0x10, 16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000024 if platform_long_is_32_bits:
25 self.assertEqual(0x7fffffff, 2147483647)
26 else:
27 self.assertEqual(0x7fffffffffffffff, 9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000028 # Ditto with a minus sign and parentheses
29 self.assertEqual(-(0x0), 0)
30 self.assertEqual(-(0x10), -16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000031 if platform_long_is_32_bits:
32 self.assertEqual(-(0x7fffffff), -2147483647)
33 else:
34 self.assertEqual(-(0x7fffffffffffffff), -9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000035 # Ditto with a minus sign and NO parentheses
36 self.assertEqual(-0x0, 0)
37 self.assertEqual(-0x10, -16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000038 if platform_long_is_32_bits:
39 self.assertEqual(-0x7fffffff, -2147483647)
40 else:
41 self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000042
43 def test_hex_unsigned(self):
44 # This test is in a <string> so we can ignore the warnings
45 exec """if 1:
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000046 if platform_long_is_32_bits:
47 # Positive-looking constants with negavive values
48 self.assertEqual(0x80000000, -2147483648L)
49 self.assertEqual(0xffffffff, -1)
50 # Ditto with a minus sign and parentheses
51 self.assertEqual(-(0x80000000), 2147483648L)
52 self.assertEqual(-(0xffffffff), 1)
53 # Ditto with a minus sign and NO parentheses
54 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
55 self.assertEqual(-0x80000000, 2147483648L)
56 self.assertEqual(-0xffffffff, 1)
57 else:
58 # Positive-looking constants with negavive values
59 self.assertEqual(0x8000000000000000, -9223372036854775808L)
60 self.assertEqual(0xffffffffffffffff, -1)
61 # Ditto with a minus sign and parentheses
62 self.assertEqual(-(0x8000000000000000), 9223372036854775808L)
63 self.assertEqual(-(0xffffffffffffffff), 1)
64 # Ditto with a minus sign and NO parentheses
65 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
66 self.assertEqual(-0x8000000000000000, 9223372036854775808L)
67 self.assertEqual(-0xffffffffffffffff, 1)
Guido van Rossum9c00f422003-02-12 17:09:17 +000068 \n"""
69
70 def test_oct_baseline(self):
71 # Baseline tests
72 self.assertEqual(00, 0)
73 self.assertEqual(020, 16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000074 if platform_long_is_32_bits:
75 self.assertEqual(017777777777, 2147483647)
76 else:
77 self.assertEqual(0777777777777777777777, 9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000078 # Ditto with a minus sign and parentheses
79 self.assertEqual(-(00), 0)
80 self.assertEqual(-(020), -16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000081 if platform_long_is_32_bits:
82 self.assertEqual(-(017777777777), -2147483647)
83 else:
84 self.assertEqual(-(0777777777777777777777), -9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000085 # Ditto with a minus sign and NO parentheses
86 self.assertEqual(-00, 0)
87 self.assertEqual(-020, -16)
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000088 if platform_long_is_32_bits:
89 self.assertEqual(-017777777777, -2147483647)
90 else:
91 self.assertEqual(-0777777777777777777777, -9223372036854775807)
Guido van Rossum9c00f422003-02-12 17:09:17 +000092
93 def test_oct_unsigned(self):
94 # This test is in a <string> so we can ignore the warnings
95 exec """if 1:
Neal Norwitzdcfdceb2003-02-18 15:45:44 +000096 if platform_long_is_32_bits:
97 # Positive-looking constants with negavive values
98 self.assertEqual(020000000000, -2147483648L)
99 self.assertEqual(037777777777, -1)
100 # Ditto with a minus sign and parentheses
101 self.assertEqual(-(020000000000), 2147483648L)
102 self.assertEqual(-(037777777777), 1)
103 # Ditto with a minus sign and NO parentheses
104 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
105 self.assertEqual(-020000000000, 2147483648L)
106 self.assertEqual(-037777777777, 1)
107 else:
108 # Positive-looking constants with negavive values
109 self.assertEqual(01000000000000000000000, -9223372036854775808L)
110 self.assertEqual(01777777777777777777777, -1)
111 # Ditto with a minus sign and parentheses
112 self.assertEqual(-(01000000000000000000000), 9223372036854775808L)
113 self.assertEqual(-(01777777777777777777777), 1)
114 # Ditto with a minus sign and NO parentheses
115 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
116 self.assertEqual(-01000000000000000000000, 9223372036854775808L)
117 self.assertEqual(-01777777777777777777777, 1)
Guido van Rossum9c00f422003-02-12 17:09:17 +0000118 \n"""
119
120def test_main():
121 suite = unittest.TestSuite()
122 suite.addTest(unittest.makeSuite(TextHexOct))
123 test_support.run_suite(suite)
124
125if __name__ == "__main__":
126 test_main()