move sections
[python/dscho.git] / Lib / test / test_int_literal.py
blobe65cc7c7c040913098bd270e3943a2397dcd0141
1 """Test correct treatment of hex/oct constants.
3 This is complex because of changes due to PEP 237.
4 """
6 import unittest
7 from test import test_support
10 class TestHexOctBin(unittest.TestCase):
12 def test_hex_baseline(self):
13 # A few upper/lowercase tests
14 self.assertEqual(0x0, 0X0)
15 self.assertEqual(0x1, 0X1)
16 self.assertEqual(0x123456789abcdef, 0X123456789abcdef)
17 # Baseline tests
18 self.assertEqual(0x0, 0)
19 self.assertEqual(0x10, 16)
20 self.assertEqual(0x7fffffff, 2147483647)
21 self.assertEqual(0x7fffffffffffffff, 9223372036854775807)
22 # Ditto with a minus sign and parentheses
23 self.assertEqual(-(0x0), 0)
24 self.assertEqual(-(0x10), -16)
25 self.assertEqual(-(0x7fffffff), -2147483647)
26 self.assertEqual(-(0x7fffffffffffffff), -9223372036854775807)
27 # Ditto with a minus sign and NO parentheses
28 self.assertEqual(-0x0, 0)
29 self.assertEqual(-0x10, -16)
30 self.assertEqual(-0x7fffffff, -2147483647)
31 self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
33 def test_hex_unsigned(self):
34 # Positive constants
35 self.assertEqual(0x80000000, 2147483648L)
36 self.assertEqual(0xffffffff, 4294967295L)
37 # Ditto with a minus sign and parentheses
38 self.assertEqual(-(0x80000000), -2147483648L)
39 self.assertEqual(-(0xffffffff), -4294967295L)
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, -4294967295L)
45 # Positive constants
46 self.assertEqual(0x8000000000000000, 9223372036854775808L)
47 self.assertEqual(0xffffffffffffffff, 18446744073709551615L)
48 # Ditto with a minus sign and parentheses
49 self.assertEqual(-(0x8000000000000000), -9223372036854775808L)
50 self.assertEqual(-(0xffffffffffffffff), -18446744073709551615L)
51 # Ditto with a minus sign and NO parentheses
52 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
53 self.assertEqual(-0x8000000000000000, -9223372036854775808L)
54 self.assertEqual(-0xffffffffffffffff, -18446744073709551615L)
56 def test_oct_baseline(self):
57 # Baseline tests
58 self.assertEqual(00, 0)
59 self.assertEqual(020, 16)
60 self.assertEqual(017777777777, 2147483647)
61 self.assertEqual(0777777777777777777777, 9223372036854775807)
62 # Ditto with a minus sign and parentheses
63 self.assertEqual(-(00), 0)
64 self.assertEqual(-(020), -16)
65 self.assertEqual(-(017777777777), -2147483647)
66 self.assertEqual(-(0777777777777777777777), -9223372036854775807)
67 # Ditto with a minus sign and NO parentheses
68 self.assertEqual(-00, 0)
69 self.assertEqual(-020, -16)
70 self.assertEqual(-017777777777, -2147483647)
71 self.assertEqual(-0777777777777777777777, -9223372036854775807)
73 def test_oct_baseline_new(self):
74 # A few upper/lowercase tests
75 self.assertEqual(0o0, 0O0)
76 self.assertEqual(0o1, 0O1)
77 self.assertEqual(0o1234567, 0O1234567)
78 # Baseline tests
79 self.assertEqual(0o0, 0)
80 self.assertEqual(0o20, 16)
81 self.assertEqual(0o17777777777, 2147483647)
82 self.assertEqual(0o777777777777777777777, 9223372036854775807)
83 # Ditto with a minus sign and parentheses
84 self.assertEqual(-(0o0), 0)
85 self.assertEqual(-(0o20), -16)
86 self.assertEqual(-(0o17777777777), -2147483647)
87 self.assertEqual(-(0o777777777777777777777), -9223372036854775807)
88 # Ditto with a minus sign and NO parentheses
89 self.assertEqual(-0o0, 0)
90 self.assertEqual(-0o20, -16)
91 self.assertEqual(-0o17777777777, -2147483647)
92 self.assertEqual(-0o777777777777777777777, -9223372036854775807)
94 def test_oct_unsigned(self):
95 # Positive constants
96 self.assertEqual(020000000000, 2147483648L)
97 self.assertEqual(037777777777, 4294967295L)
98 # Ditto with a minus sign and parentheses
99 self.assertEqual(-(020000000000), -2147483648L)
100 self.assertEqual(-(037777777777), -4294967295L)
101 # Ditto with a minus sign and NO parentheses
102 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
103 self.assertEqual(-020000000000, -2147483648L)
104 self.assertEqual(-037777777777, -4294967295L)
106 # Positive constants
107 self.assertEqual(01000000000000000000000, 9223372036854775808L)
108 self.assertEqual(01777777777777777777777, 18446744073709551615L)
109 # Ditto with a minus sign and parentheses
110 self.assertEqual(-(01000000000000000000000), -9223372036854775808L)
111 self.assertEqual(-(01777777777777777777777), -18446744073709551615L)
112 # Ditto with a minus sign and NO parentheses
113 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
114 self.assertEqual(-01000000000000000000000, -9223372036854775808L)
115 self.assertEqual(-01777777777777777777777, -18446744073709551615L)
117 def test_oct_unsigned_new(self):
118 # Positive constants
119 self.assertEqual(0o20000000000, 2147483648L)
120 self.assertEqual(0o37777777777, 4294967295L)
121 # Ditto with a minus sign and parentheses
122 self.assertEqual(-(0o20000000000), -2147483648L)
123 self.assertEqual(-(0o37777777777), -4294967295L)
124 # Ditto with a minus sign and NO parentheses
125 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
126 self.assertEqual(-0o20000000000, -2147483648L)
127 self.assertEqual(-0o37777777777, -4294967295L)
129 # Positive constants
130 self.assertEqual(0o1000000000000000000000, 9223372036854775808L)
131 self.assertEqual(0o1777777777777777777777, 18446744073709551615L)
132 # Ditto with a minus sign and parentheses
133 self.assertEqual(-(0o1000000000000000000000), -9223372036854775808L)
134 self.assertEqual(-(0o1777777777777777777777), -18446744073709551615L)
135 # Ditto with a minus sign and NO parentheses
136 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
137 self.assertEqual(-0o1000000000000000000000, -9223372036854775808L)
138 self.assertEqual(-0o1777777777777777777777, -18446744073709551615L)
140 def test_bin_baseline(self):
141 # A few upper/lowercase tests
142 self.assertEqual(0b0, 0B0)
143 self.assertEqual(0b1, 0B1)
144 self.assertEqual(0b10101010101, 0B10101010101)
145 # Baseline tests
146 self.assertEqual(0b0, 0)
147 self.assertEqual(0b10000, 16)
148 self.assertEqual(0b1111111111111111111111111111111, 2147483647)
149 self.assertEqual(0b111111111111111111111111111111111111111111111111111111111111111, 9223372036854775807)
150 # Ditto with a minus sign and parentheses
151 self.assertEqual(-(0b0), 0)
152 self.assertEqual(-(0b10000), -16)
153 self.assertEqual(-(0b1111111111111111111111111111111), -2147483647)
154 self.assertEqual(-(0b111111111111111111111111111111111111111111111111111111111111111), -9223372036854775807)
155 # Ditto with a minus sign and NO parentheses
156 self.assertEqual(-0b0, 0)
157 self.assertEqual(-0b10000, -16)
158 self.assertEqual(-0b1111111111111111111111111111111, -2147483647)
159 self.assertEqual(-0b111111111111111111111111111111111111111111111111111111111111111, -9223372036854775807)
161 def test_bin_unsigned(self):
162 # Positive constants
163 self.assertEqual(0b10000000000000000000000000000000, 2147483648L)
164 self.assertEqual(0b11111111111111111111111111111111, 4294967295L)
165 # Ditto with a minus sign and parentheses
166 self.assertEqual(-(0b10000000000000000000000000000000), -2147483648L)
167 self.assertEqual(-(0b11111111111111111111111111111111), -4294967295L)
168 # Ditto with a minus sign and NO parentheses
169 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
170 self.assertEqual(-0b10000000000000000000000000000000, -2147483648L)
171 self.assertEqual(-0b11111111111111111111111111111111, -4294967295L)
173 # Positive constants
174 self.assertEqual(0b1000000000000000000000000000000000000000000000000000000000000000, 9223372036854775808L)
175 self.assertEqual(0b1111111111111111111111111111111111111111111111111111111111111111, 18446744073709551615L)
176 # Ditto with a minus sign and parentheses
177 self.assertEqual(-(0b1000000000000000000000000000000000000000000000000000000000000000), -9223372036854775808L)
178 self.assertEqual(-(0b1111111111111111111111111111111111111111111111111111111111111111), -18446744073709551615L)
179 # Ditto with a minus sign and NO parentheses
180 # This failed in Python 2.2 through 2.2.2 and in 2.3a1
181 self.assertEqual(-0b1000000000000000000000000000000000000000000000000000000000000000, -9223372036854775808L)
182 self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615L)
184 def test_main():
185 test_support.run_unittest(TestHexOctBin)
187 if __name__ == "__main__":
188 test_main()