Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_long.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 873875657ad0 |
| children | 70e115c34f97 |
| rev | line source |
|---|---|
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
1 import unittest |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
2 from test import test_support |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
3 import sys |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
4 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
5 import random |
|
50205
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
6 import math |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
7 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
8 # Used for lazy formatting of failure messages |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
9 class Frm(object): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
10 def __init__(self, format, *args): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
11 self.format = format |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
12 self.args = args |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
13 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
14 def __str__(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
15 return self.format % self.args |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 # SHIFT should match the value in longintrepr.h for best testing. |
|
52233
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
18 SHIFT = sys.long_info.bits_per_digit |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 BASE = 2 ** SHIFT |
|
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 MASK = BASE - 1 |
|
33348
adc66d314c78
Changed Karatsuba cutoff to match current reality.
Tim Peters <tim.peters@gmail.com>
parents:
27638
diff
changeset
|
21 KARATSUBA_CUTOFF = 70 # from longobject.c |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 # Max number of base BASE digits to use in test cases. Doubling |
|
24778
f1d18fcd56e6
Machines-- and Python --are a lot faster in relevant ways since this
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
24 # this will more than double the runtime. |
|
f1d18fcd56e6
Machines-- and Python --are a lot faster in relevant ways since this
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
25 MAXDIGITS = 15 |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
27 # build some special values |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
28 special = map(long, [0, 1, 2, BASE, BASE >> 1]) |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
29 special.append(0x5555555555555555L) |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
30 special.append(0xaaaaaaaaaaaaaaaaL) |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
31 # some solid strings of one bits |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
32 p2 = 4L # 0 and 1 already added |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
33 for i in range(2*SHIFT): |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
34 special.append(p2 - 1) |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
35 p2 = p2 << 1 |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
36 del p2 |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
37 # add complements & negations |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
38 special = special + map(lambda x: ~x, special) + \ |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
39 map(lambda x: -x, special) |
|
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
40 |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
41 L = [ |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
42 ('0', 0), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
43 ('1', 1), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
44 ('9', 9), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
45 ('10', 10), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
46 ('99', 99), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
47 ('100', 100), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
48 ('314', 314), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
49 (' 314', 314), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
50 ('314 ', 314), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
51 (' \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:
44488
diff
changeset
|
52 (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:
44488
diff
changeset
|
53 (' 1x', ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
54 (' 1 ', 1), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
55 (' 1\02 ', ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
56 ('', ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
57 (' ', ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
58 (' \t\t ', ValueError) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
59 ] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
60 if test_support.have_unicode: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
61 L += [ |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
62 (unicode('0'), 0), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
63 (unicode('1'), 1), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
64 (unicode('9'), 9), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
65 (unicode('10'), 10), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
66 (unicode('99'), 99), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
67 (unicode('100'), 100), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
68 (unicode('314'), 314), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
69 (unicode(' 314'), 314), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
70 (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:
44488
diff
changeset
|
71 (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:
44488
diff
changeset
|
72 (unicode(' 1x'), ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
73 (unicode(' 1 '), 1), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
74 (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:
44488
diff
changeset
|
75 (unicode(''), ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
76 (unicode(' '), ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
77 (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:
44488
diff
changeset
|
78 (unichr(0x200), ValueError), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
79 ] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
80 |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
82 class LongTest(unittest.TestCase): |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
84 # Get quasi-random long consisting of ndigits digits (in base BASE). |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
85 # quasi == the most-significant digit will not be 0, and the number |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
86 # is constructed to contain long strings of 0 and 1 bits. These are |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
87 # more likely than random bits to provoke digit-boundary errors. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
88 # The sign of the number is also random. |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
89 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
90 def getran(self, ndigits): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
91 self.assertTrue(ndigits > 0) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
92 nbits_hi = ndigits * SHIFT |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
93 nbits_lo = nbits_hi - SHIFT + 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
94 answer = 0L |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
95 nbits = 0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
96 r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
97 while nbits < nbits_lo: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
98 bits = (r >> 1) + 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
99 bits = min(bits, nbits_hi - nbits) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
100 self.assertTrue(1 <= bits <= SHIFT) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
101 nbits = nbits + bits |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
102 answer = answer << bits |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
103 if r & 1: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
104 answer = answer | ((1 << bits) - 1) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
105 r = int(random.random() * (SHIFT * 2)) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
106 self.assertTrue(nbits_lo <= nbits <= nbits_hi) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
107 if random.random() < 0.5: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
108 answer = -answer |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
109 return answer |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
110 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
111 # Get random long consisting of ndigits random digits (relative to base |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
112 # BASE). The sign bit is also random. |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
114 def getran2(ndigits): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
115 answer = 0L |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
116 for i in xrange(ndigits): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
117 answer = (answer << SHIFT) | random.randint(0, MASK) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
118 if random.random() < 0.5: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
119 answer = -answer |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
120 return answer |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
122 def check_division(self, x, y): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
123 eq = self.assertEqual |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
124 q, r = divmod(x, y) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
125 q2, r2 = x//y, x%y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
126 pab, pba = x*y, y*x |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
127 eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
128 eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
129 eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
130 eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
131 if y > 0: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
132 self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y)) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
133 else: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
134 self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y)) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
136 def test_division(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
137 digits = range(1, MAXDIGITS+1) + range(KARATSUBA_CUTOFF, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
138 KARATSUBA_CUTOFF + 14) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
139 digits.append(KARATSUBA_CUTOFF * 3) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
140 for lenx in digits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
141 x = self.getran(lenx) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
142 for leny in digits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
143 y = self.getran(leny) or 1L |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
144 self.check_division(x, y) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
52233
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
146 # specific numbers chosen to exercise corner cases of the |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
147 # current long division implementation |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
148 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
149 # 30-bit cases involving a quotient digit estimate of BASE+1 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
150 self.check_division(1231948412290879395966702881L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
151 1147341367131428698L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
152 self.check_division(815427756481275430342312021515587883L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
153 707270836069027745L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
154 self.check_division(627976073697012820849443363563599041L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
155 643588798496057020L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
156 self.check_division(1115141373653752303710932756325578065L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
157 1038556335171453937726882627L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
158 # 30-bit cases that require the post-subtraction correction step |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
159 self.check_division(922498905405436751940989320930368494L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
160 949985870686786135626943396L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
161 self.check_division(768235853328091167204009652174031844L, |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
162 1091555541180371554426545266L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
163 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
164 # 15-bit cases involving a quotient digit estimate of BASE+1 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
165 self.check_division(20172188947443L, 615611397L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
166 self.check_division(1020908530270155025L, 950795710L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
167 self.check_division(128589565723112408L, 736393718L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
168 self.check_division(609919780285761575L, 18613274546784L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
169 # 15-bit cases that require the post-subtraction correction step |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
170 self.check_division(710031681576388032L, 26769404391308L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
171 self.check_division(1933622614268221L, 30212853348836L) |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
172 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
173 |
|
9b358eae532a
Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents:
50205
diff
changeset
|
174 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
175 def test_karatsuba(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
176 digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
177 digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100]) |
|
24794
1c63d3082656
Added a test specifically to tickle Karatsuba; it costs no appreciable
Tim Peters <tim.peters@gmail.com>
parents:
24779
diff
changeset
|
178 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
179 bits = [digit * SHIFT for digit in digits] |
|
24794
1c63d3082656
Added a test specifically to tickle Karatsuba; it costs no appreciable
Tim Peters <tim.peters@gmail.com>
parents:
24779
diff
changeset
|
180 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
181 # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) == |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
182 # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
183 for abits in bits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
184 a = (1L << abits) - 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
185 for bbits in bits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
186 if bbits < abits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
187 continue |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
188 b = (1L << bbits) - 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
189 x = a * b |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
190 y = ((1L << (abits + bbits)) - |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
191 (1L << abits) - |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
192 (1L << bbits) + |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
193 1) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
194 self.assertEqual(x, y, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
195 Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y)) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
197 def check_bitop_identities_1(self, x): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
198 eq = self.assertEqual |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
199 eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
200 eq(x | 0, x, Frm("x | 0 != x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
201 eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
202 eq(x & -1, x, Frm("x & -1 != x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
203 eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
204 eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
205 eq(x, ~~x, Frm("x != ~~x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
206 eq(x & x, x, Frm("x & x != x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
207 eq(x | x, x, Frm("x | x != x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
208 eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
209 eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
210 eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
211 eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
212 eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
213 eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
214 for n in xrange(2*SHIFT): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
215 p2 = 2L ** n |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
216 eq(x << n >> n, x, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
217 Frm("x << n >> n != x for x=%r, n=%r", (x, n))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
218 eq(x // p2, x >> n, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
219 Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
220 eq(x * p2, x << n, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
221 Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
222 eq(x & -p2, x >> n << n, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
223 Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
224 eq(x & -p2, x & ~(p2 - 1), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
225 Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2))) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
227 def check_bitop_identities_2(self, x, y): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
228 eq = self.assertEqual |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
229 eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
230 eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
231 eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
232 eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
233 eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
234 eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
235 eq(x ^ y, (x | y) & ~(x & y), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
236 Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
237 eq(x ^ y, (x & ~y) | (~x & y), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
238 Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
239 eq(x ^ y, (x | y) & (~x | ~y), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
240 Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y))) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
242 def check_bitop_identities_3(self, x, y, z): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
243 eq = self.assertEqual |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
244 eq((x & y) & z, x & (y & z), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
245 Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
246 eq((x | y) | z, x | (y | z), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
247 Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
248 eq((x ^ y) ^ z, x ^ (y ^ z), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
249 Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
250 eq(x & (y | z), (x & y) | (x & z), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
251 Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z))) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
252 eq(x | (y & z), (x | y) & (x | z), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
253 Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z))) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
255 def test_bitop_identities(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
256 for x in special: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
257 self.check_bitop_identities_1(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
258 digits = xrange(1, MAXDIGITS+1) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
259 for lenx in digits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
260 x = self.getran(lenx) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
261 self.check_bitop_identities_1(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
262 for leny in digits: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
263 y = self.getran(leny) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
264 self.check_bitop_identities_2(x, y) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
265 self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2)) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
267 def slow_format(self, x, base): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
268 if (x, base) == (0, 8): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
269 # this is an oddball! |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
270 return "0L" |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
271 digits = [] |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
272 sign = 0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
273 if x < 0: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
274 sign, x = 1, -x |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
275 while x: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
276 x, r = divmod(x, base) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
277 digits.append(int(r)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
278 digits.reverse() |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
279 digits = digits or [0] |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
280 return '-'[:sign] + \ |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
281 {8: '0', 10: '', 16: '0x'}[base] + \ |
|
35177
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
35072
diff
changeset
|
282 "".join(map(lambda i: "0123456789abcdef"[i], digits)) + "L" |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
284 def check_format_1(self, x): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
285 for base, mapper in (8, oct), (10, repr), (16, hex): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
286 got = mapper(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
287 expected = self.slow_format(x, base) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
288 msg = Frm("%s returned %r but expected %r for %r", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
289 mapper.__name__, got, expected, x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
290 self.assertEqual(got, expected, msg) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
291 self.assertEqual(long(got, 0), x, Frm('long("%s", 0) != %r', got, x)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
292 # str() has to be checked a little differently since there's no |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
293 # trailing "L" |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
294 got = str(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
295 expected = self.slow_format(x, 10)[:-1] |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
296 msg = Frm("%s returned %r but expected %r for %r", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
297 mapper.__name__, got, expected, x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
298 self.assertEqual(got, expected, msg) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
300 def test_format(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
301 for x in special: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
302 self.check_format_1(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
303 for i in xrange(10): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
304 for lenx in xrange(1, MAXDIGITS+1): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
305 x = self.getran(lenx) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
306 self.check_format_1(x) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
308 def test_long(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
309 self.assertEqual(long(314), 314L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
310 self.assertEqual(long(3.14), 3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
311 self.assertEqual(long(314L), 314L) |
|
49104
974040c8f369
#3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48177
diff
changeset
|
312 # Check that long() of basic types actually returns a long |
|
974040c8f369
#3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48177
diff
changeset
|
313 self.assertEqual(type(long(314)), long) |
|
974040c8f369
#3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48177
diff
changeset
|
314 self.assertEqual(type(long(3.14)), long) |
|
974040c8f369
#3777: long(4.2) returned an int, and broke backward compatibility.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48177
diff
changeset
|
315 self.assertEqual(type(long(314L)), long) |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
316 # 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:
44488
diff
changeset
|
317 self.assertEqual(long(-3.14), -3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
318 self.assertEqual(long(3.9), 3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
319 self.assertEqual(long(-3.9), -3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
320 self.assertEqual(long(3.5), 3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
321 self.assertEqual(long(-3.5), -3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
322 self.assertEqual(long("-3"), -3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
323 if test_support.have_unicode: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
324 self.assertEqual(long(unicode("-3")), -3L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
325 # Different base: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
326 self.assertEqual(long("10",16), 16L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
327 if test_support.have_unicode: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
328 self.assertEqual(long(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:
44488
diff
changeset
|
329 # Check conversions from string (same test set as for int(), and then some) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
330 LL = [ |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
331 ('1' + '0'*20, 10L**20), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
332 ('1' + '0'*100, 10L**100) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
333 ] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
334 L2 = L[:] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
335 if test_support.have_unicode: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
336 L2 += [ |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
337 (unicode('1') + unicode('0')*20, 10L**20), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
338 (unicode('1') + unicode('0')*100, 10L**100), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
339 ] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
340 for s, v in L2 + LL: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
341 for sign in "", "+", "-": |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
342 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:
44488
diff
changeset
|
343 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:
44488
diff
changeset
|
344 vv = v |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
345 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:
44488
diff
changeset
|
346 vv = -v |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
347 try: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
348 self.assertEqual(long(ss), long(vv)) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
349 except v: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
350 pass |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
351 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
352 self.assertRaises(ValueError, long, '123\0') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
353 self.assertRaises(ValueError, long, '53', 40) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
354 self.assertRaises(TypeError, long, 1, 12) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
355 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
356 # SF patch #1638879: 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:
44488
diff
changeset
|
357 # explicit base |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
358 self.assertRaises(ValueError, long, '123\0', 10) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
359 self.assertRaises(ValueError, long, '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:
44488
diff
changeset
|
360 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
361 self.assertEqual(long('100000000000000000000000000000000', 2), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
362 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
363 self.assertEqual(long('102002022201221111211', 3), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
364 self.assertEqual(long('10000000000000000', 4), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
365 self.assertEqual(long('32244002423141', 5), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
366 self.assertEqual(long('1550104015504', 6), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
367 self.assertEqual(long('211301422354', 7), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
368 self.assertEqual(long('40000000000', 8), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
369 self.assertEqual(long('12068657454', 9), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
370 self.assertEqual(long('4294967296', 10), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
371 self.assertEqual(long('1904440554', 11), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
372 self.assertEqual(long('9ba461594', 12), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
373 self.assertEqual(long('535a79889', 13), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
374 self.assertEqual(long('2ca5b7464', 14), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
375 self.assertEqual(long('1a20dcd81', 15), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
376 self.assertEqual(long('100000000', 16), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
377 self.assertEqual(long('a7ffda91', 17), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
378 self.assertEqual(long('704he7g4', 18), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
379 self.assertEqual(long('4f5aff66', 19), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
380 self.assertEqual(long('3723ai4g', 20), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
381 self.assertEqual(long('281d55i4', 21), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
382 self.assertEqual(long('1fj8b184', 22), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
383 self.assertEqual(long('1606k7ic', 23), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
384 self.assertEqual(long('mb994ag', 24), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
385 self.assertEqual(long('hek2mgl', 25), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
386 self.assertEqual(long('dnchbnm', 26), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
387 self.assertEqual(long('b28jpdm', 27), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
388 self.assertEqual(long('8pfgih4', 28), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
389 self.assertEqual(long('76beigg', 29), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
390 self.assertEqual(long('5qmcpqg', 30), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
391 self.assertEqual(long('4q0jto4', 31), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
392 self.assertEqual(long('4000000', 32), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
393 self.assertEqual(long('3aokq94', 33), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
394 self.assertEqual(long('2qhxjli', 34), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
395 self.assertEqual(long('2br45qb', 35), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
396 self.assertEqual(long('1z141z4', 36), 4294967296) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
397 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
398 self.assertEqual(long('100000000000000000000000000000001', 2), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
399 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
400 self.assertEqual(long('102002022201221111212', 3), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
401 self.assertEqual(long('10000000000000001', 4), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
402 self.assertEqual(long('32244002423142', 5), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
403 self.assertEqual(long('1550104015505', 6), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
404 self.assertEqual(long('211301422355', 7), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
405 self.assertEqual(long('40000000001', 8), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
406 self.assertEqual(long('12068657455', 9), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
407 self.assertEqual(long('4294967297', 10), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
408 self.assertEqual(long('1904440555', 11), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
409 self.assertEqual(long('9ba461595', 12), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
410 self.assertEqual(long('535a7988a', 13), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
411 self.assertEqual(long('2ca5b7465', 14), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
412 self.assertEqual(long('1a20dcd82', 15), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
413 self.assertEqual(long('100000001', 16), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
414 self.assertEqual(long('a7ffda92', 17), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
415 self.assertEqual(long('704he7g5', 18), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
416 self.assertEqual(long('4f5aff67', 19), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
417 self.assertEqual(long('3723ai4h', 20), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
418 self.assertEqual(long('281d55i5', 21), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
419 self.assertEqual(long('1fj8b185', 22), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
420 self.assertEqual(long('1606k7id', 23), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
421 self.assertEqual(long('mb994ah', 24), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
422 self.assertEqual(long('hek2mgm', 25), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
423 self.assertEqual(long('dnchbnn', 26), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
424 self.assertEqual(long('b28jpdn', 27), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
425 self.assertEqual(long('8pfgih5', 28), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
426 self.assertEqual(long('76beigh', 29), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
427 self.assertEqual(long('5qmcpqh', 30), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
428 self.assertEqual(long('4q0jto5', 31), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
429 self.assertEqual(long('4000001', 32), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
430 self.assertEqual(long('3aokq95', 33), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
431 self.assertEqual(long('2qhxjlj', 34), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
432 self.assertEqual(long('2br45qc', 35), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
433 self.assertEqual(long('1z141z5', 36), 4294967297) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
434 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
435 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
436 def test_conversion(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
437 # Test __long__() |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
438 class ClassicMissingMethods: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
439 pass |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
440 self.assertRaises(AttributeError, long, ClassicMissingMethods()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
441 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
442 class MissingMethods(object): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
443 pass |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
444 self.assertRaises(TypeError, long, MissingMethods()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
445 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
446 class Foo0: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
447 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
448 return 42L |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
449 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
450 class Foo1(object): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
451 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
452 return 42L |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
453 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
454 class Foo2(long): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
455 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
456 return 42L |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
457 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
458 class Foo3(long): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
459 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
460 return self |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
461 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
462 class Foo4(long): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
463 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
464 return 42 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
465 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
466 class Foo5(long): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
467 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
468 return 42. |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
469 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
470 self.assertEqual(long(Foo0()), 42L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
471 self.assertEqual(long(Foo1()), 42L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
472 self.assertEqual(long(Foo2()), 42L) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
473 self.assertEqual(long(Foo3()), 0) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
474 self.assertEqual(long(Foo4()), 42) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
475 self.assertRaises(TypeError, long, Foo5()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
476 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
477 class Classic: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
478 pass |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
479 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:
44488
diff
changeset
|
480 class LongOverridesTrunc(base): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
481 def __long__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
482 return 42 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
483 def __trunc__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
484 return -12 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
485 self.assertEqual(long(LongOverridesTrunc()), 42) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
486 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
487 class JustTrunc(base): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
488 def __trunc__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
489 return 42 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
490 self.assertEqual(long(JustTrunc()), 42) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
491 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
492 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:
44488
diff
changeset
|
493 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:
44488
diff
changeset
|
494 def __int__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
495 return 42 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
496 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
497 class TruncReturnsNonLong(base): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
498 def __trunc__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
499 return Integral() |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
500 self.assertEqual(long(TruncReturnsNonLong()), 42) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
501 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
502 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:
44488
diff
changeset
|
503 def __trunc__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
504 # 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:
44488
diff
changeset
|
505 return NonIntegral() |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
506 |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
507 class TruncReturnsNonIntegral(base): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
508 def __trunc__(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
509 return NonIntegral() |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
510 try: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
511 long(TruncReturnsNonIntegral()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
512 except TypeError as e: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
513 self.assertEquals(str(e), |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
514 "__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:
44488
diff
changeset
|
515 " (type NonIntegral)") |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
516 else: |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
44488
diff
changeset
|
517 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:
44488
diff
changeset
|
518 ((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:
44488
diff
changeset
|
519 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
520 def test_misc(self): |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
521 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
522 # check the extremes in int<->long conversion |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
523 hugepos = sys.maxint |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
524 hugeneg = -hugepos - 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
525 hugepos_aslong = long(hugepos) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
526 hugeneg_aslong = long(hugeneg) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
527 self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint") |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
528 self.assertEqual(hugeneg, hugeneg_aslong, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
529 "long(-sys.maxint-1) != -sys.maxint-1") |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
530 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
531 # long -> int should not fail for hugepos_aslong or hugeneg_aslong |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
532 x = int(hugepos_aslong) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
533 try: |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
534 self.assertEqual(x, hugepos, |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
535 "converting sys.maxint to long and back to int fails") |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
536 except OverflowError: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
537 self.fail("int(long(sys.maxint)) overflowed!") |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
538 if not isinstance(x, int): |
|
58764
873875657ad0
Fix a few UnboundLocalErrors in test_long.
Georg Brandl <georg@python.org>
parents:
58472
diff
changeset
|
539 self.fail("int(long(sys.maxint)) should have returned int") |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
540 x = int(hugeneg_aslong) |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
541 try: |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
542 self.assertEqual(x, hugeneg, |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
543 "converting -sys.maxint-1 to long and back to int fails") |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
544 except OverflowError: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
545 self.fail("int(long(-sys.maxint-1)) overflowed!") |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
546 if not isinstance(x, int): |
|
58764
873875657ad0
Fix a few UnboundLocalErrors in test_long.
Georg Brandl <georg@python.org>
parents:
58472
diff
changeset
|
547 self.fail("int(long(-sys.maxint-1)) should have returned int") |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
548 # but long -> int should overflow for hugepos+1 and hugeneg-1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
549 x = hugepos_aslong + 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
550 try: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
551 y = int(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
552 except OverflowError: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
553 self.fail("int(long(sys.maxint) + 1) mustn't overflow") |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
554 self.assertIsInstance(y, long, |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
555 "int(long(sys.maxint) + 1) should have returned long") |
|
9209
d3af310f230c
New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents:
8989
diff
changeset
|
556 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
557 x = hugeneg_aslong - 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
558 try: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
559 y = int(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
560 except OverflowError: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
561 self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow") |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
562 self.assertIsInstance(y, long, |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
563 "int(long(-sys.maxint-1) - 1) should have returned long") |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
564 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
565 class long2(long): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
566 pass |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
567 x = long2(1L<<100) |
|
26291
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
24794
diff
changeset
|
568 y = int(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
569 self.assertTrue(type(y) is long, |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
570 "overflowing int conversion must return long not long subtype") |
|
26291
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
24794
diff
changeset
|
571 |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
572 # long -> Py_ssize_t conversion |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
573 class X(object): |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
574 def __getslice__(self, i, j): |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
575 return i, j |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
576 |
|
58198
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
577 self.assertEqual(X()[-5L:7L], (-5, 7)) |
|
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
578 # use the clamping effect to test the smallest and largest longs |
|
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
579 # that fit a Py_ssize_t |
|
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
580 slicemin, slicemax = X()[-2L**100:2L**100] |
|
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
581 self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax)) |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
35177
diff
changeset
|
582 |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
583 # ----------------------------------- tests of auto int->long conversion |
|
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
584 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
585 def test_auto_overflow(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
586 import math, sys |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
587 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
588 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1] |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
589 sqrt = int(math.sqrt(sys.maxint)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
590 special.extend([sqrt-1, sqrt, sqrt+1]) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
591 special.extend([-i for i in special]) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
592 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
593 def checkit(*args): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
594 # Heavy use of nested scopes here! |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
595 self.assertEqual(got, expected, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
596 Frm("for %r expected %r got %r", args, expected, got)) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
597 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
598 for x in special: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
599 longx = long(x) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
600 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
601 expected = -longx |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
602 got = -x |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
603 checkit('-', x) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
604 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
605 for y in special: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
606 longy = long(y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
607 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
608 expected = longx + longy |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
609 got = x + y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
610 checkit(x, '+', y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
611 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
612 expected = longx - longy |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
613 got = x - y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
614 checkit(x, '-', y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
615 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
616 expected = longx * longy |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
617 got = x * y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
618 checkit(x, '*', y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
619 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
620 if y: |
|
58198
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
621 expected = longx / longy |
|
0dd1aa33265f
Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents:
58197
diff
changeset
|
622 got = x / y |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
623 checkit(x, '/', y) |
|
19459
50019cb784e0
Add a test for the new // operator too.
Tim Peters <tim.peters@gmail.com>
parents:
19458
diff
changeset
|
624 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
625 expected = longx // longy |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
626 got = x // y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
627 checkit(x, '//', y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
628 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
629 expected = divmod(longx, longy) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
630 got = divmod(longx, longy) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
631 checkit(x, 'divmod', y) |
|
19458
8f90e1f50c11
SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents:
19452
diff
changeset
|
632 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
633 if abs(y) < 5 and not (x == 0 and y < 0): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
634 expected = longx ** longy |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
635 got = x ** y |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
636 checkit(x, '**', y) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
637 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
638 for z in special: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
639 if z != 0 : |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
640 if y >= 0: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
641 expected = pow(longx, longy, long(z)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
642 got = pow(x, y, z) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
643 checkit('pow', x, y, '%', z) |
|
19666
cf65ebf87560
New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents:
19459
diff
changeset
|
644 else: |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
645 self.assertRaises(TypeError, pow,longx, longy, long(z)) |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
646 |
|
53289
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
647 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
648 "test requires IEEE 754 doubles") |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
649 def test_float_conversion(self): |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
650 import sys |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
651 DBL_MAX = sys.float_info.max |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
652 DBL_MAX_EXP = sys.float_info.max_exp |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
653 DBL_MANT_DIG = sys.float_info.mant_dig |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
654 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
655 exact_values = [0L, 1L, 2L, |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
656 long(2**53-3), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
657 long(2**53-2), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
658 long(2**53-1), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
659 long(2**53), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
660 long(2**53+2), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
661 long(2**54-4), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
662 long(2**54-2), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
663 long(2**54), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
664 long(2**54+4)] |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
665 for x in exact_values: |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
666 self.assertEqual(long(float(x)), x) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
667 self.assertEqual(long(float(-x)), -x) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
668 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
669 # test round-half-even |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
670 for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]: |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
671 for p in xrange(15): |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
672 self.assertEqual(long(float(2L**p*(2**53+x))), 2L**p*(2**53+y)) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
673 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
674 for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
675 (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12), |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
676 (13, 12), (14, 16), (15, 16)]: |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
677 for p in xrange(15): |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
678 self.assertEqual(long(float(2L**p*(2**54+x))), 2L**p*(2**54+y)) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
679 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
680 # behaviour near extremes of floating-point range |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
681 long_dbl_max = long(DBL_MAX) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
682 top_power = 2**DBL_MAX_EXP |
|
53293
cee1fc076344
Nit: integer division should use //, not /
Mark Dickinson <dickinsm@gmail.com>
parents:
53289
diff
changeset
|
683 halfway = (long_dbl_max + top_power)//2 |
|
53289
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
684 self.assertEqual(float(long_dbl_max), DBL_MAX) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
685 self.assertEqual(float(long_dbl_max+1), DBL_MAX) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
686 self.assertEqual(float(halfway-1), DBL_MAX) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
687 self.assertRaises(OverflowError, float, halfway) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
688 self.assertEqual(float(1-halfway), -DBL_MAX) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
689 self.assertRaises(OverflowError, float, -halfway) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
690 self.assertRaises(OverflowError, float, top_power-1) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
691 self.assertRaises(OverflowError, float, top_power) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
692 self.assertRaises(OverflowError, float, top_power+1) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
693 self.assertRaises(OverflowError, float, 2*top_power-1) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
694 self.assertRaises(OverflowError, float, 2*top_power) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
695 self.assertRaises(OverflowError, float, top_power*top_power) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
696 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
697 for p in xrange(100): |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
698 x = long(2**p * (2**53 + 1) + 1) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
699 y = long(2**p * (2**53+ 2)) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
700 self.assertEqual(long(float(x)), y) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
701 |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
702 x = long(2**p * (2**53 + 1)) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
703 y = long(2**p * 2**53) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
704 self.assertEqual(long(float(x)), y) |
|
8f28ce7b8e69
Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents:
52233
diff
changeset
|
705 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
706 def test_float_overflow(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
707 import math |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
708 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
709 for x in -2.0, -1.0, 0.0, 1.0, 2.0: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
710 self.assertEqual(float(long(x)), x) |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
711 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
712 shuge = '12345' * 120 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
713 huge = 1L << 30000 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
714 mhuge = -huge |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
715 namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math} |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
716 for test in ["float(huge)", "float(mhuge)", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
717 "complex(huge)", "complex(mhuge)", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
718 "complex(huge, 1)", "complex(mhuge, 1)", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
719 "complex(1, huge)", "complex(1, mhuge)", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
720 "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
721 "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
722 "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
723 "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
724 "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
725 "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
726 "math.sin(huge)", "math.sin(mhuge)", |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
727 "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better |
|
44488
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44448
diff
changeset
|
728 "math.floor(huge)", "math.floor(mhuge)"]: |
|
19679
6958784b1cef
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19674
diff
changeset
|
729 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
730 self.assertRaises(OverflowError, eval, test, namespace) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
731 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
732 # XXX Perhaps float(shuge) can raise OverflowError on some box? |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
733 # The comparison should not. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
734 self.assertNotEqual(float(shuge), int(shuge), |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
735 "float(shuge) should not equal int(shuge)") |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
736 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
737 def test_logs(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
738 import math |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
739 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
740 LOG10E = math.log10(math.e) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
741 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
742 for exp in range(10) + [100, 1000, 10000]: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
743 value = 10 ** exp |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
744 log10 = math.log10(value) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
745 self.assertAlmostEqual(log10, exp) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
746 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
747 # log10(value) == exp, so log(value) == log10(value)/log10(e) == |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
748 # exp/LOG10E |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
749 expected = exp / LOG10E |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
750 log = math.log(value) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
751 self.assertAlmostEqual(log, expected) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
752 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
753 for bad in -(1L << 10000), -2L, 0L: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
754 self.assertRaises(ValueError, math.log, bad) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
755 self.assertRaises(ValueError, math.log10, bad) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19698
diff
changeset
|
756 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
757 def test_mixed_compares(self): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
758 eq = self.assertEqual |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
759 import math |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
760 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
761 # We're mostly concerned with that mixing floats and longs does the |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
762 # right stuff, even when longs are too large to fit in a float. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
763 # The safest way to check the results is to use an entirely different |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
764 # method, which we do here via a skeletal rational class (which |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
765 # represents all Python ints, longs and floats exactly). |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
766 class Rat: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
767 def __init__(self, value): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
768 if isinstance(value, (int, long)): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
769 self.n = value |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
770 self.d = 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
771 elif isinstance(value, float): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
772 # Convert to exact rational equivalent. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
773 f, e = math.frexp(abs(value)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
774 assert f == 0 or 0.5 <= f < 1.0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
775 # |value| = f * 2**e exactly |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
776 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
777 # Suck up CHUNK bits at a time; 28 is enough so that we suck |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
778 # up all bits in 2 iterations for all known binary double- |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
779 # precision formats, and small enough to fit in an int. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
780 CHUNK = 28 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
781 top = 0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
782 # invariant: |value| = (top + f) * 2**e exactly |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
783 while f: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
784 f = math.ldexp(f, CHUNK) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
785 digit = int(f) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
786 assert digit >> CHUNK == 0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
787 top = (top << CHUNK) | digit |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
788 f -= digit |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
789 assert 0.0 <= f < 1.0 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
790 e -= CHUNK |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
791 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
792 # Now |value| = top * 2**e exactly. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
793 if e >= 0: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
794 n = top << e |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
795 d = 1 |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
796 else: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
797 n = top |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
798 d = 1 << -e |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
799 if value < 0: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
800 n = -n |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
801 self.n = n |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
802 self.d = d |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
803 assert float(n) / float(d) == value |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
804 else: |
|
58764
873875657ad0
Fix a few UnboundLocalErrors in test_long.
Georg Brandl <georg@python.org>
parents:
58472
diff
changeset
|
805 raise TypeError("can't deal with %r" % value) |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
806 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
807 def __cmp__(self, other): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
808 if not isinstance(other, Rat): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
809 other = Rat(other) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
810 return cmp(self.n * other.d, self.d * other.n) |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
811 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
812 cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200] |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
813 # 2**48 is an important boundary in the internals. 2**53 is an |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
814 # important boundary for IEEE double precision. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
815 for t in 2.0**48, 2.0**50, 2.0**53: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
816 cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0, |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
817 long(t-1), long(t), long(t+1)]) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
818 cases.extend([0, 1, 2, sys.maxint, float(sys.maxint)]) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
819 # 1L<<20000 should exceed all double formats. long(1e200) is to |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
820 # check that we get equality with 1e200 above. |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
821 t = long(1e200) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
822 cases.extend([0L, 1L, 2L, 1L << 20000, t-1, t, t+1]) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
823 cases.extend([-x for x in cases]) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
824 for x in cases: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
825 Rx = Rat(x) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
826 for y in cases: |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
827 Ry = Rat(y) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
828 Rcmp = cmp(Rx, Ry) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
829 xycmp = cmp(x, y) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
830 eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
831 eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
832 eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
833 eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
834 eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
835 eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp)) |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
836 eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp)) |
|
33547
702367da2c72
SF bug #513866: Float/long comparison anomaly.
Tim Peters <tim.peters@gmail.com>
parents:
33348
diff
changeset
|
837 |
|
44448
5e0170d3c4da
Bug #1481296: Fixed long(float('nan'))!=0L.
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
838 def test_nan_inf(self): |
|
5e0170d3c4da
Bug #1481296: Fixed long(float('nan'))!=0L.
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
839 self.assertRaises(OverflowError, long, float('inf')) |
|
48177
6c6a030567dd
Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents:
46649
diff
changeset
|
840 self.assertRaises(OverflowError, long, float('-inf')) |
|
6c6a030567dd
Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents:
46649
diff
changeset
|
841 self.assertRaises(ValueError, long, float('nan')) |
|
44448
5e0170d3c4da
Bug #1481296: Fixed long(float('nan'))!=0L.
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
842 |
|
50205
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
843 def test_bit_length(self): |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
844 tiny = 1e-10 |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
845 for x in xrange(-65000, 65000): |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
846 x = long(x) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
847 k = x.bit_length() |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
848 # Check equivalence with Python version |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
849 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:
49104
diff
changeset
|
850 # Behaviour as specified in the docs |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
851 if x != 0: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53293
diff
changeset
|
852 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:
49104
diff
changeset
|
853 else: |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
854 self.assertEqual(k, 0) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
855 # 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:
49104
diff
changeset
|
856 if x != 0: |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
857 # 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:
49104
diff
changeset
|
858 # 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:
49104
diff
changeset
|
859 # 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:
49104
diff
changeset
|
860 # to the quotient before taking the floor. |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
861 self.assertEqual(k, 1 + math.floor( |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
862 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:
49104
diff
changeset
|
863 |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
864 self.assertEqual((0L).bit_length(), 0) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
865 self.assertEqual((1L).bit_length(), 1) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
866 self.assertEqual((-1L).bit_length(), 1) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
867 self.assertEqual((2L).bit_length(), 2) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
868 self.assertEqual((-2L).bit_length(), 2) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
869 for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]: |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
870 a = 2L**i |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
871 self.assertEqual((a-1).bit_length(), i) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
872 self.assertEqual((1-a).bit_length(), i) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
873 self.assertEqual((a).bit_length(), i+1) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
874 self.assertEqual((-a).bit_length(), i+1) |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
875 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:
49104
diff
changeset
|
876 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:
49104
diff
changeset
|
877 |
|
f0db06f49a4e
Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents:
49104
diff
changeset
|
878 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
879 def test_main(): |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
880 test_support.run_unittest(LongTest) |
|
8989
5fb3c683e862
Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
881 |
|
35072
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
882 if __name__ == "__main__": |
|
5fb2163231d3
Port test_long.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
33547
diff
changeset
|
883 test_main() |
