blob: 3423d4e526e2fe404967aa6bb7ba244be7b24d5e [file] [log] [blame]
Ezio Melotti4969f702011-03-15 05:59:46 +02001# test interactions between int, float, Decimal and Fraction
Mark Dickinsondc787d22010-05-23 13:33:13 +00002
3import unittest
4import random
5import math
6import sys
7import operator
8from test.support import run_unittest
9
10from decimal import Decimal as D
11from fractions import Fraction as F
12
13# Constants related to the hash implementation; hash(x) is based
14# on the reduction of x modulo the prime _PyHASH_MODULUS.
15_PyHASH_MODULUS = sys.hash_info.modulus
16_PyHASH_INF = sys.hash_info.inf
17
18class HashTest(unittest.TestCase):
19 def check_equal_hash(self, x, y):
20 # check both that x and y are equal and that their hashes are equal
21 self.assertEqual(hash(x), hash(y),
22 "got different hashes for {!r} and {!r}".format(x, y))
23 self.assertEqual(x, y)
24
25 def test_bools(self):
26 self.check_equal_hash(False, 0)
27 self.check_equal_hash(True, 1)
28
29 def test_integers(self):
30 # check that equal values hash equal
31
32 # exact integers
33 for i in range(-1000, 1000):
34 self.check_equal_hash(i, float(i))
35 self.check_equal_hash(i, D(i))
36 self.check_equal_hash(i, F(i))
37
38 # the current hash is based on reduction modulo 2**n-1 for some
39 # n, so pay special attention to numbers of the form 2**n and 2**n-1.
40 for i in range(100):
41 n = 2**i - 1
42 if n == int(float(n)):
43 self.check_equal_hash(n, float(n))
44 self.check_equal_hash(-n, -float(n))
45 self.check_equal_hash(n, D(n))
46 self.check_equal_hash(n, F(n))
47 self.check_equal_hash(-n, D(-n))
48 self.check_equal_hash(-n, F(-n))
49
50 n = 2**i
51 self.check_equal_hash(n, float(n))
52 self.check_equal_hash(-n, -float(n))
53 self.check_equal_hash(n, D(n))
54 self.check_equal_hash(n, F(n))
55 self.check_equal_hash(-n, D(-n))
56 self.check_equal_hash(-n, F(-n))
57
58 # random values of various sizes
59 for _ in range(1000):
60 e = random.randrange(300)
61 n = random.randrange(-10**e, 10**e)
62 self.check_equal_hash(n, D(n))
63 self.check_equal_hash(n, F(n))
64 if n == int(float(n)):
65 self.check_equal_hash(n, float(n))
66
67 def test_binary_floats(self):
68 # check that floats hash equal to corresponding Fractions and Decimals
69
70 # floats that are distinct but numerically equal should hash the same
71 self.check_equal_hash(0.0, -0.0)
72
73 # zeros
74 self.check_equal_hash(0.0, D(0))
75 self.check_equal_hash(-0.0, D(0))
76 self.check_equal_hash(-0.0, D('-0.0'))
77 self.check_equal_hash(0.0, F(0))
78
79 # infinities and nans
80 self.check_equal_hash(float('inf'), D('inf'))
81 self.check_equal_hash(float('-inf'), D('-inf'))
82
83 for _ in range(1000):
84 x = random.random() * math.exp(random.random()*200.0 - 100.0)
85 self.check_equal_hash(x, D.from_float(x))
86 self.check_equal_hash(x, F.from_float(x))
87
88 def test_complex(self):
89 # complex numbers with zero imaginary part should hash equal to
90 # the corresponding float
91
92 test_values = [0.0, -0.0, 1.0, -1.0, 0.40625, -5136.5,
93 float('inf'), float('-inf')]
94
95 for zero in -0.0, 0.0:
96 for value in test_values:
97 self.check_equal_hash(value, complex(value, zero))
98
99 def test_decimals(self):
100 # check that Decimal instances that have different representations
101 # but equal values give the same hash
102 zeros = ['0', '-0', '0.0', '-0.0e10', '000e-10']
103 for zero in zeros:
104 self.check_equal_hash(D(zero), D(0))
105
106 self.check_equal_hash(D('1.00'), D(1))
107 self.check_equal_hash(D('1.00000'), D(1))
108 self.check_equal_hash(D('-1.00'), D(-1))
109 self.check_equal_hash(D('-1.00000'), D(-1))
110 self.check_equal_hash(D('123e2'), D(12300))
111 self.check_equal_hash(D('1230e1'), D(12300))
112 self.check_equal_hash(D('12300'), D(12300))
113 self.check_equal_hash(D('12300.0'), D(12300))
114 self.check_equal_hash(D('12300.00'), D(12300))
115 self.check_equal_hash(D('12300.000'), D(12300))
116
117 def test_fractions(self):
118 # check special case for fractions where either the numerator
119 # or the denominator is a multiple of _PyHASH_MODULUS
120 self.assertEqual(hash(F(1, _PyHASH_MODULUS)), _PyHASH_INF)
121 self.assertEqual(hash(F(-1, 3*_PyHASH_MODULUS)), -_PyHASH_INF)
122 self.assertEqual(hash(F(7*_PyHASH_MODULUS, 1)), 0)
123 self.assertEqual(hash(F(-_PyHASH_MODULUS, 1)), 0)
124
125 def test_hash_normalization(self):
126 # Test for a bug encountered while changing long_hash.
127 #
128 # Given objects x and y, it should be possible for y's
129 # __hash__ method to return hash(x) in order to ensure that
130 # hash(x) == hash(y). But hash(x) is not exactly equal to the
131 # result of x.__hash__(): there's some internal normalization
132 # to make sure that the result fits in a C long, and is not
133 # equal to the invalid hash value -1. This internal
134 # normalization must therefore not change the result of
135 # hash(x) for any x.
136
137 class HalibutProxy:
138 def __hash__(self):
139 return hash('halibut')
140 def __eq__(self, other):
141 return other == 'halibut'
142
143 x = {'halibut', HalibutProxy()}
144 self.assertEqual(len(x), 1)
145
Mark Dickinson08ade6f2010-06-11 10:44:52 +0000146class 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'),
Stefan Krah1919b7e2012-03-21 18:25:23 +0100153 D('-1e425000000'),
Mark Dickinson08ade6f2010-06-11 10:44:52 +0000154 -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
Mark Dickinsondc787d22010-05-23 13:33:13 +0000201
202def test_main():
Mark Dickinson08ade6f2010-06-11 10:44:52 +0000203 run_unittest(HashTest, ComparisonTest)
Mark Dickinsondc787d22010-05-23 13:33:13 +0000204
205if __name__ == '__main__':
206 test_main()