Guido van Rossum | 9c00f42 | 2003-02-12 17:09:17 +0000 | [diff] [blame] | 1 | """Test correct treatment of hex/oct constants. |
| 2 | |
| 3 | This is complex because of changes due to PEP 237. |
| 4 | |
Walter Dörwald | 3ea7cc3 | 2003-02-12 23:49:57 +0000 | [diff] [blame^] | 5 | Some of these tests will have to change in Python 2.4! |
Guido van Rossum | 9c00f42 | 2003-02-12 17:09:17 +0000 | [diff] [blame] | 6 | """ |
| 7 | |
| 8 | import unittest |
| 9 | from test import test_support |
| 10 | |
| 11 | import warnings |
| 12 | warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning, |
| 13 | "<string>") |
| 14 | |
| 15 | class 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 | |
| 75 | def test_main(): |
| 76 | suite = unittest.TestSuite() |
| 77 | suite.addTest(unittest.makeSuite(TextHexOct)) |
| 78 | test_support.run_suite(suite) |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | test_main() |