1"""Test correct treatment of hex/oct constants.
2
3This is complex because of changes due to PEP 237.
4"""
5
6import unittest
7
8class TestHexOctBin(unittest.TestCase):
9
10    def test_hex_baseline(self):
11        # A few upper/lowercase tests
12        self.assertEqual(0x0, 0X0)
13        self.assertEqual(0x1, 0X1)
14        self.assertEqual(0x123456789abcdef, 0X123456789abcdef)
15        # Baseline tests
16        self.assertEqual(0x0, 0)
17        self.assertEqual(0x10, 16)
18        self.assertEqual(0x7fffffff, 2147483647)
19        self.assertEqual(0x7fffffffffffffff, 9223372036854775807)
20        # Ditto with a minus sign and parentheses
21        self.assertEqual(-(0x0), 0)
22        self.assertEqual(-(0x10), -16)
23        self.assertEqual(-(0x7fffffff), -2147483647)
24        self.assertEqual(-(0x7fffffffffffffff), -9223372036854775807)
25        # Ditto with a minus sign and NO parentheses
26        self.assertEqual(-0x0, 0)
27        self.assertEqual(-0x10, -16)
28        self.assertEqual(-0x7fffffff, -2147483647)
29        self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
30
31    def test_hex_unsigned(self):
32        # Positive constants
33        self.assertEqual(0x80000000, 2147483648)
34        self.assertEqual(0xffffffff, 4294967295)
35        # Ditto with a minus sign and parentheses
36        self.assertEqual(-(0x80000000), -2147483648)
37        self.assertEqual(-(0xffffffff), -4294967295)
38        # Ditto with a minus sign and NO parentheses
39        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
40        self.assertEqual(-0x80000000, -2147483648)
41        self.assertEqual(-0xffffffff, -4294967295)
42
43        # Positive constants
44        self.assertEqual(0x8000000000000000, 9223372036854775808)
45        self.assertEqual(0xffffffffffffffff, 18446744073709551615)
46        # Ditto with a minus sign and parentheses
47        self.assertEqual(-(0x8000000000000000), -9223372036854775808)
48        self.assertEqual(-(0xffffffffffffffff), -18446744073709551615)
49        # Ditto with a minus sign and NO parentheses
50        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
51        self.assertEqual(-0x8000000000000000, -9223372036854775808)
52        self.assertEqual(-0xffffffffffffffff, -18446744073709551615)
53
54    def test_oct_baseline(self):
55        # A few upper/lowercase tests
56        self.assertEqual(0o0, 0O0)
57        self.assertEqual(0o1, 0O1)
58        self.assertEqual(0o1234567, 0O1234567)
59        # Baseline tests
60        self.assertEqual(0o0, 0)
61        self.assertEqual(0o20, 16)
62        self.assertEqual(0o17777777777, 2147483647)
63        self.assertEqual(0o777777777777777777777, 9223372036854775807)
64        # Ditto with a minus sign and parentheses
65        self.assertEqual(-(0o0), 0)
66        self.assertEqual(-(0o20), -16)
67        self.assertEqual(-(0o17777777777), -2147483647)
68        self.assertEqual(-(0o777777777777777777777), -9223372036854775807)
69        # Ditto with a minus sign and NO parentheses
70        self.assertEqual(-0o0, 0)
71        self.assertEqual(-0o20, -16)
72        self.assertEqual(-0o17777777777, -2147483647)
73        self.assertEqual(-0o777777777777777777777, -9223372036854775807)
74
75    def test_oct_unsigned(self):
76        # Positive constants
77        self.assertEqual(0o20000000000, 2147483648)
78        self.assertEqual(0o37777777777, 4294967295)
79        # Ditto with a minus sign and parentheses
80        self.assertEqual(-(0o20000000000), -2147483648)
81        self.assertEqual(-(0o37777777777), -4294967295)
82        # Ditto with a minus sign and NO parentheses
83        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
84        self.assertEqual(-0o20000000000, -2147483648)
85        self.assertEqual(-0o37777777777, -4294967295)
86
87        # Positive constants
88        self.assertEqual(0o1000000000000000000000, 9223372036854775808)
89        self.assertEqual(0o1777777777777777777777, 18446744073709551615)
90        # Ditto with a minus sign and parentheses
91        self.assertEqual(-(0o1000000000000000000000), -9223372036854775808)
92        self.assertEqual(-(0o1777777777777777777777), -18446744073709551615)
93        # Ditto with a minus sign and NO parentheses
94        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
95        self.assertEqual(-0o1000000000000000000000, -9223372036854775808)
96        self.assertEqual(-0o1777777777777777777777, -18446744073709551615)
97
98    def test_bin_baseline(self):
99        # A few upper/lowercase tests
100        self.assertEqual(0b0, 0B0)
101        self.assertEqual(0b1, 0B1)
102        self.assertEqual(0b10101010101, 0B10101010101)
103        # Baseline tests
104        self.assertEqual(0b0, 0)
105        self.assertEqual(0b10000, 16)
106        self.assertEqual(0b1111111111111111111111111111111, 2147483647)
107        self.assertEqual(0b111111111111111111111111111111111111111111111111111111111111111, 9223372036854775807)
108        # Ditto with a minus sign and parentheses
109        self.assertEqual(-(0b0), 0)
110        self.assertEqual(-(0b10000), -16)
111        self.assertEqual(-(0b1111111111111111111111111111111), -2147483647)
112        self.assertEqual(-(0b111111111111111111111111111111111111111111111111111111111111111), -9223372036854775807)
113        # Ditto with a minus sign and NO parentheses
114        self.assertEqual(-0b0, 0)
115        self.assertEqual(-0b10000, -16)
116        self.assertEqual(-0b1111111111111111111111111111111, -2147483647)
117        self.assertEqual(-0b111111111111111111111111111111111111111111111111111111111111111, -9223372036854775807)
118
119    def test_bin_unsigned(self):
120        # Positive constants
121        self.assertEqual(0b10000000000000000000000000000000, 2147483648)
122        self.assertEqual(0b11111111111111111111111111111111, 4294967295)
123        # Ditto with a minus sign and parentheses
124        self.assertEqual(-(0b10000000000000000000000000000000), -2147483648)
125        self.assertEqual(-(0b11111111111111111111111111111111), -4294967295)
126        # Ditto with a minus sign and NO parentheses
127        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
128        self.assertEqual(-0b10000000000000000000000000000000, -2147483648)
129        self.assertEqual(-0b11111111111111111111111111111111, -4294967295)
130
131        # Positive constants
132        self.assertEqual(0b1000000000000000000000000000000000000000000000000000000000000000, 9223372036854775808)
133        self.assertEqual(0b1111111111111111111111111111111111111111111111111111111111111111, 18446744073709551615)
134        # Ditto with a minus sign and parentheses
135        self.assertEqual(-(0b1000000000000000000000000000000000000000000000000000000000000000), -9223372036854775808)
136        self.assertEqual(-(0b1111111111111111111111111111111111111111111111111111111111111111), -18446744073709551615)
137        # Ditto with a minus sign and NO parentheses
138        # This failed in Python 2.2 through 2.2.2 and in 2.3a1
139        self.assertEqual(-0b1000000000000000000000000000000000000000000000000000000000000000, -9223372036854775808)
140        self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615)
141
142if __name__ == "__main__":
143    unittest.main()
144