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