LEFT | RIGHT |
1 # test interactions betwen int, float, Decimal and Fraction | 1 # test interactions betwen int, float, Decimal and Fraction |
2 | 2 |
3 import unittest | 3 import unittest |
4 import random | 4 import random |
5 import math | 5 import math |
6 import sys | 6 import sys |
| 7 import operator |
7 from test.support import run_unittest | 8 from test.support import run_unittest |
8 | 9 |
9 from decimal import Decimal as D | 10 from decimal import Decimal as D |
10 from fractions import Fraction as F | 11 from fractions import Fraction as F |
11 | 12 |
12 # Constants related to the hash implementation; hash(x) is based | 13 # Constants related to the hash implementation; hash(x) is based |
13 # on the reduction of x modulo the prime _PyHASH_MASK. | 14 # on the reduction of x modulo the prime _PyHASH_MODULUS. |
14 _PyHASH_MASK = sys._hash_info.modulus | 15 _PyHASH_MODULUS = sys.hash_info.modulus |
| 16 _PyHASH_INF = sys.hash_info.inf |
15 | 17 |
16 class HashTest(unittest.TestCase): | 18 class HashTest(unittest.TestCase): |
17 def check_equal_hash(self, x, y): | 19 def check_equal_hash(self, x, y): |
| 20 # check both that x and y are equal and that their hashes are equal |
18 self.assertEqual(hash(x), hash(y), | 21 self.assertEqual(hash(x), hash(y), |
19 "got different hashes for {!r} and {!r}".format(x, y)) | 22 "got different hashes for {!r} and {!r}".format(x, y)) |
| 23 self.assertEqual(x, y) |
20 | 24 |
21 def test_bools(self): | 25 def test_bools(self): |
22 self.check_equal_hash(False, 0) | 26 self.check_equal_hash(False, 0) |
23 self.check_equal_hash(True, 1) | 27 self.check_equal_hash(True, 1) |
24 | 28 |
25 def test_integers(self): | 29 def test_integers(self): |
26 # check that equal values hash equal | 30 # check that equal values hash equal |
27 | 31 |
28 # exact integers | 32 # exact integers |
29 for i in range(-1000, 1000): | 33 for i in range(-1000, 1000): |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 for _ in range(1000): | 83 for _ in range(1000): |
80 x = random.random() * math.exp(random.random()*200.0 - 100.0) | 84 x = random.random() * math.exp(random.random()*200.0 - 100.0) |
81 self.check_equal_hash(x, D.from_float(x)) | 85 self.check_equal_hash(x, D.from_float(x)) |
82 self.check_equal_hash(x, F.from_float(x)) | 86 self.check_equal_hash(x, F.from_float(x)) |
83 | 87 |
84 def test_complex(self): | 88 def test_complex(self): |
85 # complex numbers with zero imaginary part should hash equal to | 89 # complex numbers with zero imaginary part should hash equal to |
86 # the corresponding float | 90 # the corresponding float |
87 | 91 |
88 test_values = [0.0, -0.0, 1.0, -1.0, 0.40625, -5136.5, | 92 test_values = [0.0, -0.0, 1.0, -1.0, 0.40625, -5136.5, |
89 float('inf'), float('-inf'), float('nan'), | 93 float('inf'), float('-inf')] |
90 float('-nan')] | |
91 | 94 |
92 for zero in -0.0, 0.0: | 95 for zero in -0.0, 0.0: |
93 for value in test_values: | 96 for value in test_values: |
94 self.check_equal_hash(value, complex(value, zero)) | 97 self.check_equal_hash(value, complex(value, zero)) |
95 | 98 |
96 def test_decimals(self): | 99 def test_decimals(self): |
97 # check that Decimal instances that have different representations | 100 # check that Decimal instances that have different representations |
98 # but equal values give the same hash | 101 # but equal values give the same hash |
99 zeros = ['0', '-0', '0.0', '-0.0e10', '000e-10'] | 102 zeros = ['0', '-0', '0.0', '-0.0e10', '000e-10'] |
100 for zero in zeros: | 103 for zero in zeros: |
101 self.check_equal_hash(D(zero), D(0)) | 104 self.check_equal_hash(D(zero), D(0)) |
102 | 105 |
103 self.check_equal_hash(D('1.00'), D(1)) | 106 self.check_equal_hash(D('1.00'), D(1)) |
104 self.check_equal_hash(D('1.00000'), D(1)) | 107 self.check_equal_hash(D('1.00000'), D(1)) |
105 self.check_equal_hash(D('-1.00'), D(-1)) | 108 self.check_equal_hash(D('-1.00'), D(-1)) |
106 self.check_equal_hash(D('-1.00000'), D(-1)) | 109 self.check_equal_hash(D('-1.00000'), D(-1)) |
107 self.check_equal_hash(D('123e2'), D(12300)) | 110 self.check_equal_hash(D('123e2'), D(12300)) |
108 self.check_equal_hash(D('1230e1'), D(12300)) | 111 self.check_equal_hash(D('1230e1'), D(12300)) |
109 self.check_equal_hash(D('12300'), D(12300)) | 112 self.check_equal_hash(D('12300'), D(12300)) |
110 self.check_equal_hash(D('12300.0'), D(12300)) | 113 self.check_equal_hash(D('12300.0'), D(12300)) |
111 self.check_equal_hash(D('12300.00'), D(12300)) | 114 self.check_equal_hash(D('12300.00'), D(12300)) |
112 self.check_equal_hash(D('12300.000'), D(12300)) | 115 self.check_equal_hash(D('12300.000'), D(12300)) |
113 | 116 |
114 def test_fractions(self): | 117 def test_fractions(self): |
115 # hashes are based on reduction modulo _PyHASH_MASK, so just check | 118 # check special case for fractions where either the numerator |
116 # that we can still compute a hash without error for Fractions | 119 # or the denominator is a multiple of _PyHASH_MODULUS |
117 # whose reduction modulo _PyHASH_MASK is infinite. | 120 self.assertEqual(hash(F(1, _PyHASH_MODULUS)), _PyHASH_INF) |
118 hash(F(1, _PyHASH_MASK)) | 121 self.assertEqual(hash(F(-1, 3*_PyHASH_MODULUS)), -_PyHASH_INF) |
119 hash(F(-12345, _PyHASH_MASK)) | 122 self.assertEqual(hash(F(7*_PyHASH_MODULUS, 1)), 0) |
120 hash(F(_PyHASH_MASK, 5*_PyHASH_MASK)) | 123 self.assertEqual(hash(F(-_PyHASH_MODULUS, 1)), 0) |
121 | 124 |
122 def test_hash_normalization(self): | 125 def test_hash_normalization(self): |
123 # Test for a bug encountered while changing long_hash. | 126 # Test for a bug encountered while changing long_hash. |
124 # | 127 # |
125 # Given objects x and y, it should be possible for y's | 128 # Given objects x and y, it should be possible for y's |
126 # __hash__ method to return hash(x) in order to ensure that | 129 # __hash__ method to return hash(x) in order to ensure that |
127 # hash(x) == hash(y). But hash(x) is not exactly equal to the | 130 # hash(x) == hash(y). But hash(x) is not exactly equal to the |
128 # result of x.__hash__(): there's some internal normalization | 131 # result of x.__hash__(): there's some internal normalization |
129 # to make sure that the result fits in a C long, and is not | 132 # to make sure that the result fits in a C long, and is not |
130 # equal to the invalid hash value -1. This internal | 133 # equal to the invalid hash value -1. This internal |
131 # normalization must therefore not change the result of | 134 # normalization must therefore not change the result of |
132 # hash(x) for any x. | 135 # hash(x) for any x. |
133 | 136 |
134 class HalibutProxy: | 137 class HalibutProxy: |
135 def __hash__(self): | 138 def __hash__(self): |
136 return hash('halibut') | 139 return hash('halibut') |
137 def __eq__(self, other): | 140 def __eq__(self, other): |
138 return other == 'halibut' | 141 return other == 'halibut' |
139 | 142 |
140 x = {'halibut', HalibutProxy()} | 143 x = {'halibut', HalibutProxy()} |
141 self.assertEqual(len(x), 1) | 144 self.assertEqual(len(x), 1) |
142 | 145 |
| 146 class ComparisonTest(unittest.TestCase): |
| 147 def test_mixed_comparisons(self): |
| 148 |
| 149 # ordered list of distinct test values of various types: |
| 150 # int, float, Fraction, Decimal |
| 151 test_values = [ |
| 152 float('-inf'), |
| 153 D('-1e999999999'), |
| 154 -1e308, |
| 155 F(-22, 7), |
| 156 -3.14, |
| 157 -2, |
| 158 0.0, |
| 159 1e-320, |
| 160 True, |
| 161 F('1.2'), |
| 162 D('1.3'), |
| 163 float('1.4'), |
| 164 F(275807, 195025), |
| 165 D('1.414213562373095048801688724'), |
| 166 F(114243, 80782), |
| 167 F(473596569, 84615), |
| 168 7e200, |
| 169 D('infinity'), |
| 170 ]············ |
| 171 for i, first in enumerate(test_values): |
| 172 for second in test_values[i+1:]: |
| 173 self.assertLess(first, second) |
| 174 self.assertLessEqual(first, second) |
| 175 self.assertGreater(second, first) |
| 176 self.assertGreaterEqual(second, first) |
| 177 |
| 178 def test_complex(self): |
| 179 # comparisons with complex are special: equality and inequality |
| 180 # comparisons should always succeed, but order comparisons should |
| 181 # raise TypeError. |
| 182 z = 1.0 + 0j |
| 183 w = -3.14 + 2.7j |
| 184 |
| 185 for v in 1, 1.0, F(1), D(1), complex(1): |
| 186 self.assertEqual(z, v) |
| 187 self.assertEqual(v, z) |
| 188 |
| 189 for v in 2, 2.0, F(2), D(2), complex(2): |
| 190 self.assertNotEqual(z, v) |
| 191 self.assertNotEqual(v, z) |
| 192 self.assertNotEqual(w, v) |
| 193 self.assertNotEqual(v, w) |
| 194 |
| 195 for v in (1, 1.0, F(1), D(1), complex(1), |
| 196 2, 2.0, F(2), D(2), complex(2), w): |
| 197 for op in operator.le, operator.lt, operator.ge, operator.gt: |
| 198 self.assertRaises(TypeError, op, z, v) |
| 199 self.assertRaises(TypeError, op, v, z) |
| 200 |
| 201 |
143 | 202 |
144 def test_main(): | 203 def test_main(): |
145 run_unittest(HashTest) | 204 run_unittest(HashTest, ComparisonTest) |
146 | 205 |
147 if __name__ == '__main__': | 206 if __name__ == '__main__': |
148 test_main() | 207 test_main() |
LEFT | RIGHT |