ยปCore Development>Code coverage>Lib/test/test_numeric_tower.py

Python code coverage for Lib/test/test_numeric_tower.py

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