PageRenderTime 35ms CodeModel.GetById 12ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_int_literal.py

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