Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_numeric_tower.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 7355550d5357 |
| children |
| rev | line source |
|---|---|
|
68489
81d7f78a633a
#11515: Merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62241
diff
changeset
|
1 # test interactions between int, float, Decimal and Fraction |
|
61873
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
2 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
3 import unittest |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
4 import random |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
5 import math |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
6 import sys |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
7 import operator |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
8 from test.support import run_unittest |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
9 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
10 from decimal import Decimal as D |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
11 from fractions import Fraction as F |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
12 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
13 # Constants related to the hash implementation; hash(x) is based |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
14 # on the reduction of x modulo the prime _PyHASH_MODULUS. |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
15 _PyHASH_MODULUS = sys.hash_info.modulus |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
16 _PyHASH_INF = sys.hash_info.inf |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
17 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
18 class HashTest(unittest.TestCase): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
19 def check_equal_hash(self, x, y): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
20 # check both that x and y are equal and that their hashes are equal |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
21 self.assertEqual(hash(x), hash(y), |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
22 "got different hashes for {!r} and {!r}".format(x, y)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
23 self.assertEqual(x, y) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
24 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
25 def test_bools(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
26 self.check_equal_hash(False, 0) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
27 self.check_equal_hash(True, 1) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
28 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
29 def test_integers(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
30 # check that equal values hash equal |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
31 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
32 # exact integers |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
33 for i in range(-1000, 1000): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
34 self.check_equal_hash(i, float(i)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
35 self.check_equal_hash(i, D(i)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
36 self.check_equal_hash(i, F(i)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
37 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
38 # the current hash is based on reduction modulo 2**n-1 for some |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
39 # n, so pay special attention to numbers of the form 2**n and 2**n-1. |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
40 for i in range(100): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
41 n = 2**i - 1 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
42 if n == int(float(n)): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
43 self.check_equal_hash(n, float(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
44 self.check_equal_hash(-n, -float(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
45 self.check_equal_hash(n, D(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
46 self.check_equal_hash(n, F(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
47 self.check_equal_hash(-n, D(-n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
48 self.check_equal_hash(-n, F(-n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
49 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
50 n = 2**i |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
51 self.check_equal_hash(n, float(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
52 self.check_equal_hash(-n, -float(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
53 self.check_equal_hash(n, D(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
54 self.check_equal_hash(n, F(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
55 self.check_equal_hash(-n, D(-n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
56 self.check_equal_hash(-n, F(-n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
57 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
58 # random values of various sizes |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
59 for _ in range(1000): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
60 e = random.randrange(300) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
61 n = random.randrange(-10**e, 10**e) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
62 self.check_equal_hash(n, D(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
63 self.check_equal_hash(n, F(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
64 if n == int(float(n)): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
65 self.check_equal_hash(n, float(n)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
66 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
67 def test_binary_floats(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
68 # check that floats hash equal to corresponding Fractions and Decimals |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
69 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
70 # floats that are distinct but numerically equal should hash the same |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
71 self.check_equal_hash(0.0, -0.0) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
72 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
73 # zeros |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
74 self.check_equal_hash(0.0, D(0)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
75 self.check_equal_hash(-0.0, D(0)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
76 self.check_equal_hash(-0.0, D('-0.0')) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
77 self.check_equal_hash(0.0, F(0)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
78 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
79 # infinities and nans |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
80 self.check_equal_hash(float('inf'), D('inf')) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
81 self.check_equal_hash(float('-inf'), D('-inf')) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
82 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
83 for _ in range(1000): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
84 x = random.random() * math.exp(random.random()*200.0 - 100.0) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
85 self.check_equal_hash(x, D.from_float(x)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
86 self.check_equal_hash(x, F.from_float(x)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
87 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
88 def test_complex(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
89 # complex numbers with zero imaginary part should hash equal to |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
90 # the corresponding float |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
91 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
92 test_values = [0.0, -0.0, 1.0, -1.0, 0.40625, -5136.5, |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
93 float('inf'), float('-inf')] |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
94 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
95 for zero in -0.0, 0.0: |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
96 for value in test_values: |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
97 self.check_equal_hash(value, complex(value, zero)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
98 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
99 def test_decimals(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
100 # check that Decimal instances that have different representations |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
101 # but equal values give the same hash |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
102 zeros = ['0', '-0', '0.0', '-0.0e10', '000e-10'] |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
103 for zero in zeros: |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
104 self.check_equal_hash(D(zero), D(0)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
105 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
106 self.check_equal_hash(D('1.00'), D(1)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
107 self.check_equal_hash(D('1.00000'), D(1)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
108 self.check_equal_hash(D('-1.00'), D(-1)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
109 self.check_equal_hash(D('-1.00000'), D(-1)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
110 self.check_equal_hash(D('123e2'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
111 self.check_equal_hash(D('1230e1'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
112 self.check_equal_hash(D('12300'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
113 self.check_equal_hash(D('12300.0'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
114 self.check_equal_hash(D('12300.00'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
115 self.check_equal_hash(D('12300.000'), D(12300)) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
116 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
117 def test_fractions(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
118 # check special case for fractions where either the numerator |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
119 # or the denominator is a multiple of _PyHASH_MODULUS |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
120 self.assertEqual(hash(F(1, _PyHASH_MODULUS)), _PyHASH_INF) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
121 self.assertEqual(hash(F(-1, 3*_PyHASH_MODULUS)), -_PyHASH_INF) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
122 self.assertEqual(hash(F(7*_PyHASH_MODULUS, 1)), 0) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
123 self.assertEqual(hash(F(-_PyHASH_MODULUS, 1)), 0) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
124 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
125 def test_hash_normalization(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
126 # Test for a bug encountered while changing long_hash. |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
127 # |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
128 # Given objects x and y, it should be possible for y's |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
129 # __hash__ method to return hash(x) in order to ensure that |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
130 # hash(x) == hash(y). But hash(x) is not exactly equal to the |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
131 # result of x.__hash__(): there's some internal normalization |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
132 # to make sure that the result fits in a C long, and is not |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
133 # equal to the invalid hash value -1. This internal |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
134 # normalization must therefore not change the result of |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
135 # hash(x) for any x. |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
136 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
137 class HalibutProxy: |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
138 def __hash__(self): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
139 return hash('halibut') |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
140 def __eq__(self, other): |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
141 return other == 'halibut' |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
142 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
143 x = {'halibut', HalibutProxy()} |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
144 self.assertEqual(len(x), 1) |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
145 |
|
62241
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
146 class ComparisonTest(unittest.TestCase): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
147 def test_mixed_comparisons(self): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
148 |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
149 # ordered list of distinct test values of various types: |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
150 # int, float, Fraction, Decimal |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
151 test_values = [ |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
152 float('-inf'), |
|
75850
7355550d5357
Issue #7652: Integrate the decimal floating point libmpdec library to speed
Stefan Krah <skrah@bytereef.org>
parents:
68489
diff
changeset
|
153 D('-1e425000000'), |
|
62241
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
154 -1e308, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
155 F(-22, 7), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
156 -3.14, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
157 -2, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
158 0.0, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
159 1e-320, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
160 True, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
161 F('1.2'), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
162 D('1.3'), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
163 float('1.4'), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
164 F(275807, 195025), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
165 D('1.414213562373095048801688724'), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
166 F(114243, 80782), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
167 F(473596569, 84615), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
168 7e200, |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
169 D('infinity'), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
170 ] |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
171 for i, first in enumerate(test_values): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
172 for second in test_values[i+1:]: |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
173 self.assertLess(first, second) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
174 self.assertLessEqual(first, second) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
175 self.assertGreater(second, first) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
176 self.assertGreaterEqual(second, first) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
177 |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
178 def test_complex(self): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
179 # comparisons with complex are special: equality and inequality |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
180 # comparisons should always succeed, but order comparisons should |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
181 # raise TypeError. |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
182 z = 1.0 + 0j |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
183 w = -3.14 + 2.7j |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
184 |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
185 for v in 1, 1.0, F(1), D(1), complex(1): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
186 self.assertEqual(z, v) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
187 self.assertEqual(v, z) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
188 |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
189 for v in 2, 2.0, F(2), D(2), complex(2): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
190 self.assertNotEqual(z, v) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
191 self.assertNotEqual(v, z) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
192 self.assertNotEqual(w, v) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
193 self.assertNotEqual(v, w) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
194 |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
195 for v in (1, 1.0, F(1), D(1), complex(1), |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
196 2, 2.0, F(2), D(2), complex(2), w): |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
197 for op in operator.le, operator.lt, operator.ge, operator.gt: |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
198 self.assertRaises(TypeError, op, z, v) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
199 self.assertRaises(TypeError, op, v, z) |
|
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
200 |
|
61873
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
201 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
202 def test_main(): |
|
62241
26685311d313
Issue #8188: Comparisons between Decimal objects and other numeric
Mark Dickinson <dickinsm@gmail.com>
parents:
61873
diff
changeset
|
203 run_unittest(HashTest, ComparisonTest) |
|
61873
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
204 |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
205 if __name__ == '__main__': |
|
0bc8ffe6ba9a
Issue #8188: Introduce a new scheme for computing hashes of numbers
Mark Dickinson <dickinsm@gmail.com>
parents:
diff
changeset
|
206 test_main() |
