annotate Lib/test/test_int.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children c502a2dc0345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
1 import sys
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
2
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
3 import unittest
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
4 from test.test_support import run_unittest, have_unicode
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
5 import math
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
6
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
7 L = [
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
8 ('0', 0),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
9 ('1', 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
10 ('9', 9),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
11 ('10', 10),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
12 ('99', 99),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
13 ('100', 100),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
14 ('314', 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
15 (' 314', 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
16 ('314 ', 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
17 (' \t\t 314 \t\t ', 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
18 (repr(sys.maxint), sys.maxint),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
19 (' 1x', ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
20 (' 1 ', 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
21 (' 1\02 ', ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
22 ('', ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
23 (' ', ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
24 (' \t\t ', ValueError)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
25 ]
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
26 if have_unicode:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
27 L += [
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
28 (unicode('0'), 0),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
29 (unicode('1'), 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
30 (unicode('9'), 9),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
31 (unicode('10'), 10),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
32 (unicode('99'), 99),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
33 (unicode('100'), 100),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
34 (unicode('314'), 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
35 (unicode(' 314'), 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
36 (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
37 (unicode(' \t\t 314 \t\t '), 314),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
38 (unicode(' 1x'), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
39 (unicode(' 1 '), 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
40 (unicode(' 1\02 '), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
41 (unicode(''), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
42 (unicode(' '), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
43 (unicode(' \t\t '), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
44 (unichr(0x200), ValueError),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
45 ]
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
46
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
47 class IntTestCases(unittest.TestCase):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
48
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
49 def test_basic(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
50 self.assertEqual(int(314), 314)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
51 self.assertEqual(int(3.14), 3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
52 self.assertEqual(int(314L), 314)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
53 # Check that conversion from float truncates towards zero
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
54 self.assertEqual(int(-3.14), -3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
55 self.assertEqual(int(3.9), 3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
56 self.assertEqual(int(-3.9), -3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
57 self.assertEqual(int(3.5), 3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
58 self.assertEqual(int(-3.5), -3)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
59 # Different base:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
60 self.assertEqual(int("10",16), 16L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
61 if have_unicode:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
62 self.assertEqual(int(unicode("10"),16), 16L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
63 # Test conversion from strings and various anomalies
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
64 for s, v in L:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
65 for sign in "", "+", "-":
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
66 for prefix in "", " ", "\t", " \t\t ":
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
67 ss = prefix + sign + s
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
68 vv = v
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
69 if sign == "-" and v is not ValueError:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
70 vv = -v
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
71 try:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
72 self.assertEqual(int(ss), vv)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
73 except v:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
74 pass
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
75
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
76 s = repr(-1-sys.maxint)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
77 x = int(s)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
78 self.assertEqual(x+1, -sys.maxint)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
79 self.assertIsInstance(x, int)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
80 # should return long
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
81 self.assertEqual(int(s[1:]), sys.maxint+1)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
82
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
83 # should return long
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
84 x = int(1e100)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
85 self.assertIsInstance(x, long)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
86 x = int(-1e100)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
87 self.assertIsInstance(x, long)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
88
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
89
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
90 # SF bug 434186: 0x80000000/2 != 0x80000000>>1.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
91 # Worked by accident in Windows release build, but failed in debug build.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
92 # Failed in all Linux builds.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
93 x = -1-sys.maxint
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
94 self.assertEqual(x >> 1, x//2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
95
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
96 self.assertRaises(ValueError, int, '123\0')
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
97 self.assertRaises(ValueError, int, '53', 40)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
98
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
99 # SF bug 1545497: embedded NULs were not detected with
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
100 # explicit base
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
101 self.assertRaises(ValueError, int, '123\0', 10)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
102 self.assertRaises(ValueError, int, '123\x00 245', 20)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
103
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
104 x = int('1' * 600)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
105 self.assertIsInstance(x, long)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
106
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
107 if have_unicode:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
108 x = int(unichr(0x661) * 600)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
109 self.assertIsInstance(x, long)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
110
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
111 self.assertRaises(TypeError, int, 1, 12)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
112
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
113 self.assertEqual(int('0123', 0), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
114 self.assertEqual(int('0x123', 16), 291)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
115
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
116 # Bug 1679: "0x" is not a valid hex literal
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
117 self.assertRaises(ValueError, int, "0x", 16)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
118 self.assertRaises(ValueError, int, "0x", 0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
119
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
120 self.assertRaises(ValueError, int, "0o", 8)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
121 self.assertRaises(ValueError, int, "0o", 0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
122
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
123 self.assertRaises(ValueError, int, "0b", 2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
124 self.assertRaises(ValueError, int, "0b", 0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
125
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
126
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
127 # SF bug 1334662: int(string, base) wrong answers
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
128 # Various representations of 2**32 evaluated to 0
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
129 # rather than 2**32 in previous versions
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
130
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
131 self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
132 self.assertEqual(int('102002022201221111211', 3), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
133 self.assertEqual(int('10000000000000000', 4), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
134 self.assertEqual(int('32244002423141', 5), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
135 self.assertEqual(int('1550104015504', 6), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
136 self.assertEqual(int('211301422354', 7), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
137 self.assertEqual(int('40000000000', 8), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
138 self.assertEqual(int('12068657454', 9), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
139 self.assertEqual(int('4294967296', 10), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
140 self.assertEqual(int('1904440554', 11), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
141 self.assertEqual(int('9ba461594', 12), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
142 self.assertEqual(int('535a79889', 13), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
143 self.assertEqual(int('2ca5b7464', 14), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
144 self.assertEqual(int('1a20dcd81', 15), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
145 self.assertEqual(int('100000000', 16), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
146 self.assertEqual(int('a7ffda91', 17), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
147 self.assertEqual(int('704he7g4', 18), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
148 self.assertEqual(int('4f5aff66', 19), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
149 self.assertEqual(int('3723ai4g', 20), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
150 self.assertEqual(int('281d55i4', 21), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
151 self.assertEqual(int('1fj8b184', 22), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
152 self.assertEqual(int('1606k7ic', 23), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
153 self.assertEqual(int('mb994ag', 24), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
154 self.assertEqual(int('hek2mgl', 25), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
155 self.assertEqual(int('dnchbnm', 26), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
156 self.assertEqual(int('b28jpdm', 27), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
157 self.assertEqual(int('8pfgih4', 28), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
158 self.assertEqual(int('76beigg', 29), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
159 self.assertEqual(int('5qmcpqg', 30), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
160 self.assertEqual(int('4q0jto4', 31), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
161 self.assertEqual(int('4000000', 32), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
162 self.assertEqual(int('3aokq94', 33), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
163 self.assertEqual(int('2qhxjli', 34), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
164 self.assertEqual(int('2br45qb', 35), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
165 self.assertEqual(int('1z141z4', 36), 4294967296L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
166
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
167 # tests with base 0
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
168 # this fails on 3.0, but in 2.x the old octal syntax is allowed
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
169 self.assertEqual(int(' 0123 ', 0), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
170 self.assertEqual(int(' 0123 ', 0), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
171 self.assertEqual(int('000', 0), 0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
172 self.assertEqual(int('0o123', 0), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
173 self.assertEqual(int('0x123', 0), 291)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
174 self.assertEqual(int('0b100', 0), 4)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
175 self.assertEqual(int(' 0O123 ', 0), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
176 self.assertEqual(int(' 0X123 ', 0), 291)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
177 self.assertEqual(int(' 0B100 ', 0), 4)
61935
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
178 self.assertEqual(int('0', 0), 0)
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
179 self.assertEqual(int('+0', 0), 0)
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
180 self.assertEqual(int('-0', 0), 0)
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
181 self.assertEqual(int('00', 0), 0)
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
182 self.assertRaises(ValueError, int, '08', 0)
c4f73975a440 Merged revisions 81551 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
183 self.assertRaises(ValueError, int, '-012395', 0)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
184
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
185 # without base still base 10
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
186 self.assertEqual(int('0123'), 123)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
187 self.assertEqual(int('0123', 10), 123)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
188
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
189 # tests with prefix and base != 0
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
190 self.assertEqual(int('0x123', 16), 291)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
191 self.assertEqual(int('0o123', 8), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
192 self.assertEqual(int('0b100', 2), 4)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
193 self.assertEqual(int('0X123', 16), 291)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
194 self.assertEqual(int('0O123', 8), 83)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
195 self.assertEqual(int('0B100', 2), 4)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
196
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
197 # the code has special checks for the first character after the
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
198 # type prefix
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
199 self.assertRaises(ValueError, int, '0b2', 2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
200 self.assertRaises(ValueError, int, '0b02', 2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
201 self.assertRaises(ValueError, int, '0B2', 2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
202 self.assertRaises(ValueError, int, '0B02', 2)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
203 self.assertRaises(ValueError, int, '0o8', 8)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
204 self.assertRaises(ValueError, int, '0o08', 8)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
205 self.assertRaises(ValueError, int, '0O8', 8)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
206 self.assertRaises(ValueError, int, '0O08', 8)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
207 self.assertRaises(ValueError, int, '0xg', 16)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
208 self.assertRaises(ValueError, int, '0x0g', 16)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
209 self.assertRaises(ValueError, int, '0Xg', 16)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
210 self.assertRaises(ValueError, int, '0X0g', 16)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
211
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
212 # SF bug 1334662: int(string, base) wrong answers
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
213 # Checks for proper evaluation of 2**32 + 1
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
214 self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
215 self.assertEqual(int('102002022201221111212', 3), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
216 self.assertEqual(int('10000000000000001', 4), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
217 self.assertEqual(int('32244002423142', 5), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
218 self.assertEqual(int('1550104015505', 6), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
219 self.assertEqual(int('211301422355', 7), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
220 self.assertEqual(int('40000000001', 8), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
221 self.assertEqual(int('12068657455', 9), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
222 self.assertEqual(int('4294967297', 10), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
223 self.assertEqual(int('1904440555', 11), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
224 self.assertEqual(int('9ba461595', 12), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
225 self.assertEqual(int('535a7988a', 13), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
226 self.assertEqual(int('2ca5b7465', 14), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
227 self.assertEqual(int('1a20dcd82', 15), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
228 self.assertEqual(int('100000001', 16), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
229 self.assertEqual(int('a7ffda92', 17), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
230 self.assertEqual(int('704he7g5', 18), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
231 self.assertEqual(int('4f5aff67', 19), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
232 self.assertEqual(int('3723ai4h', 20), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
233 self.assertEqual(int('281d55i5', 21), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
234 self.assertEqual(int('1fj8b185', 22), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
235 self.assertEqual(int('1606k7id', 23), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
236 self.assertEqual(int('mb994ah', 24), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
237 self.assertEqual(int('hek2mgm', 25), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
238 self.assertEqual(int('dnchbnn', 26), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
239 self.assertEqual(int('b28jpdn', 27), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
240 self.assertEqual(int('8pfgih5', 28), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
241 self.assertEqual(int('76beigh', 29), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
242 self.assertEqual(int('5qmcpqh', 30), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
243 self.assertEqual(int('4q0jto5', 31), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
244 self.assertEqual(int('4000001', 32), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
245 self.assertEqual(int('3aokq95', 33), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
246 self.assertEqual(int('2qhxjlj', 34), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
247 self.assertEqual(int('2br45qc', 35), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
248 self.assertEqual(int('1z141z5', 36), 4294967297L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
249
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
250 def test_bit_length(self):
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
251 tiny = 1e-10
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
252 for x in xrange(-65000, 65000):
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
253 k = x.bit_length()
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
254 # Check equivalence with Python version
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
255 self.assertEqual(k, len(bin(x).lstrip('-0b')))
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
256 # Behaviour as specified in the docs
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
257 if x != 0:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53289
diff changeset
258 self.assertTrue(2**(k-1) <= abs(x) < 2**k)
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
259 else:
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
260 self.assertEqual(k, 0)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
261 # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
262 if x != 0:
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
263 # When x is an exact power of 2, numeric errors can
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
264 # cause floor(log(x)/log(2)) to be one too small; for
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
265 # small x this can be fixed by adding a small quantity
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
266 # to the quotient before taking the floor.
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
267 self.assertEqual(k, 1 + math.floor(
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
268 math.log(abs(x))/math.log(2) + tiny))
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
269
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
270 self.assertEqual((0).bit_length(), 0)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
271 self.assertEqual((1).bit_length(), 1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
272 self.assertEqual((-1).bit_length(), 1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
273 self.assertEqual((2).bit_length(), 2)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
274 self.assertEqual((-2).bit_length(), 2)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
275 for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64]:
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
276 a = 2**i
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
277 self.assertEqual((a-1).bit_length(), i)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
278 self.assertEqual((1-a).bit_length(), i)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
279 self.assertEqual((a).bit_length(), i+1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
280 self.assertEqual((-a).bit_length(), i+1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
281 self.assertEqual((a+1).bit_length(), i+1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
282 self.assertEqual((-a-1).bit_length(), i+1)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
283
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
284 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
285 "test requires IEEE 754 doubles")
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
286 def test_float_conversion(self):
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
287 # values exactly representable as floats
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
288 exact_values = [-2, -1, 0, 1, 2, 2**52, 2**53-1, 2**53, 2**53+2,
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
289 2**53+4, 2**54-4, 2**54-2, 2**63, -2**63, 2**64,
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
290 -2**64, 10**20, 10**21, 10**22]
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
291 for value in exact_values:
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
292 self.assertEqual(int(float(int(value))), value)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
293
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
294 # test round-half-to-even
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
295 self.assertEqual(int(float(2**53+1)), 2**53)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
296 self.assertEqual(int(float(2**53+2)), 2**53+2)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
297 self.assertEqual(int(float(2**53+3)), 2**53+4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
298 self.assertEqual(int(float(2**53+5)), 2**53+4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
299 self.assertEqual(int(float(2**53+6)), 2**53+6)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
300 self.assertEqual(int(float(2**53+7)), 2**53+8)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
301
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
302 self.assertEqual(int(float(-2**53-1)), -2**53)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
303 self.assertEqual(int(float(-2**53-2)), -2**53-2)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
304 self.assertEqual(int(float(-2**53-3)), -2**53-4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
305 self.assertEqual(int(float(-2**53-5)), -2**53-4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
306 self.assertEqual(int(float(-2**53-6)), -2**53-6)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
307 self.assertEqual(int(float(-2**53-7)), -2**53-8)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
308
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
309 self.assertEqual(int(float(2**54-2)), 2**54-2)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
310 self.assertEqual(int(float(2**54-1)), 2**54)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
311 self.assertEqual(int(float(2**54+2)), 2**54)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
312 self.assertEqual(int(float(2**54+3)), 2**54+4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
313 self.assertEqual(int(float(2**54+5)), 2**54+4)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
314 self.assertEqual(int(float(2**54+6)), 2**54+8)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
315 self.assertEqual(int(float(2**54+10)), 2**54+8)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
316 self.assertEqual(int(float(2**54+11)), 2**54+12)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
317
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
318 def test_intconversion(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
319 # Test __int__()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
320 class ClassicMissingMethods:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
321 pass
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
322 self.assertRaises(AttributeError, int, ClassicMissingMethods())
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
323
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
324 class MissingMethods(object):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
325 pass
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
326 self.assertRaises(TypeError, int, MissingMethods())
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
327
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
328 class Foo0:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
329 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
330 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
331
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
332 class Foo1(object):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
333 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
334 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
335
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
336 class Foo2(int):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
337 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
338 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
339
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
340 class Foo3(int):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
341 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
342 return self
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
343
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
344 class Foo4(int):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
345 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
346 return 42L
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
347
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
348 class Foo5(int):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
349 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
350 return 42.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
351
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
352 self.assertEqual(int(Foo0()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
353 self.assertEqual(int(Foo1()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
354 self.assertEqual(int(Foo2()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
355 self.assertEqual(int(Foo3()), 0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
356 self.assertEqual(int(Foo4()), 42L)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
357 self.assertRaises(TypeError, int, Foo5())
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
358
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
359 class Classic:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
360 pass
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
361 for base in (object, Classic):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
362 class IntOverridesTrunc(base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
363 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
364 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
365 def __trunc__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
366 return -12
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
367 self.assertEqual(int(IntOverridesTrunc()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
368
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
369 class JustTrunc(base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
370 def __trunc__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
371 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
372 self.assertEqual(int(JustTrunc()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
373
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
374 for trunc_result_base in (object, Classic):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
375 class Integral(trunc_result_base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
376 def __int__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
377 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
378
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
379 class TruncReturnsNonInt(base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
380 def __trunc__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
381 return Integral()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
382 self.assertEqual(int(TruncReturnsNonInt()), 42)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
383
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
384 class NonIntegral(trunc_result_base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
385 def __trunc__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
386 # Check that we avoid infinite recursion.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
387 return NonIntegral()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
388
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
389 class TruncReturnsNonIntegral(base):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
390 def __trunc__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
391 return NonIntegral()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
392 try:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
393 int(TruncReturnsNonIntegral())
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
394 except TypeError as e:
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61934
diff changeset
395 self.assertEqual(str(e),
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
396 "__trunc__ returned non-Integral"
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
397 " (type NonIntegral)")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
398 else:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
399 self.fail("Failed to raise TypeError with %s" %
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
400 ((base, trunc_result_base),))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
401
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
402 def test_main():
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
403 run_unittest(IntTestCases)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
404
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
405 if __name__ == "__main__":
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
diff changeset
406 test_main()