ยปCore Development>Code coverage>Modules/_decimal/tests/randfloat.py

Python code coverage for Modules/_decimal/tests/randfloat.py

#countcontent
1n/a# Copyright (c) 2010 Python Software Foundation. All Rights Reserved.
2n/a# Adapted from Python's Lib/test/test_strtod.py (by Mark Dickinson)
3n/a
4n/a# More test cases for deccheck.py.
5n/a
6n/aimport random
7n/a
8n/aTEST_SIZE = 2
9n/a
10n/a
11n/adef test_short_halfway_cases():
12n/a # exact halfway cases with a small number of significant digits
13n/a for k in 0, 5, 10, 15, 20:
14n/a # upper = smallest integer >= 2**54/5**k
15n/a upper = -(-2**54//5**k)
16n/a # lower = smallest odd number >= 2**53/5**k
17n/a lower = -(-2**53//5**k)
18n/a if lower % 2 == 0:
19n/a lower += 1
20n/a for i in range(10 * TEST_SIZE):
21n/a # Select a random odd n in [2**53/5**k,
22n/a # 2**54/5**k). Then n * 10**k gives a halfway case
23n/a # with small number of significant digits.
24n/a n, e = random.randrange(lower, upper, 2), k
25n/a
26n/a # Remove any additional powers of 5.
27n/a while n % 5 == 0:
28n/a n, e = n // 5, e + 1
29n/a assert n % 10 in (1, 3, 7, 9)
30n/a
31n/a # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0,
32n/a # until n * 2**p2 has more than 20 significant digits.
33n/a digits, exponent = n, e
34n/a while digits < 10**20:
35n/a s = '{}e{}'.format(digits, exponent)
36n/a yield s
37n/a # Same again, but with extra trailing zeros.
38n/a s = '{}e{}'.format(digits * 10**40, exponent - 40)
39n/a yield s
40n/a digits *= 2
41n/a
42n/a # Try numbers of the form n * 5**p2 * 10**(e - p5), p5
43n/a # >= 0, with n * 5**p5 < 10**20.
44n/a digits, exponent = n, e
45n/a while digits < 10**20:
46n/a s = '{}e{}'.format(digits, exponent)
47n/a yield s
48n/a # Same again, but with extra trailing zeros.
49n/a s = '{}e{}'.format(digits * 10**40, exponent - 40)
50n/a yield s
51n/a digits *= 5
52n/a exponent -= 1
53n/a
54n/adef test_halfway_cases():
55n/a # test halfway cases for the round-half-to-even rule
56n/a for i in range(1000):
57n/a for j in range(TEST_SIZE):
58n/a # bit pattern for a random finite positive (or +0.0) float
59n/a bits = random.randrange(2047*2**52)
60n/a
61n/a # convert bit pattern to a number of the form m * 2**e
62n/a e, m = divmod(bits, 2**52)
63n/a if e:
64n/a m, e = m + 2**52, e - 1
65n/a e -= 1074
66n/a
67n/a # add 0.5 ulps
68n/a m, e = 2*m + 1, e - 1
69n/a
70n/a # convert to a decimal string
71n/a if e >= 0:
72n/a digits = m << e
73n/a exponent = 0
74n/a else:
75n/a # m * 2**e = (m * 5**-e) * 10**e
76n/a digits = m * 5**-e
77n/a exponent = e
78n/a s = '{}e{}'.format(digits, exponent)
79n/a yield s
80n/a
81n/adef test_boundaries():
82n/a # boundaries expressed as triples (n, e, u), where
83n/a # n*10**e is an approximation to the boundary value and
84n/a # u*10**e is 1ulp
85n/a boundaries = [
86n/a (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0)
87n/a (17976931348623159077, 289, 1995), # overflow boundary (2.**1024)
88n/a (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022)
89n/a (0, -327, 4941), # zero
90n/a ]
91n/a for n, e, u in boundaries:
92n/a for j in range(1000):
93n/a for i in range(TEST_SIZE):
94n/a digits = n + random.randrange(-3*u, 3*u)
95n/a exponent = e
96n/a s = '{}e{}'.format(digits, exponent)
97n/a yield s
98n/a n *= 10
99n/a u *= 10
100n/a e -= 1
101n/a
102n/adef test_underflow_boundary():
103n/a # test values close to 2**-1075, the underflow boundary; similar
104n/a # to boundary_tests, except that the random error doesn't scale
105n/a # with n
106n/a for exponent in range(-400, -320):
107n/a base = 10**-exponent // 2**1075
108n/a for j in range(TEST_SIZE):
109n/a digits = base + random.randrange(-1000, 1000)
110n/a s = '{}e{}'.format(digits, exponent)
111n/a yield s
112n/a
113n/adef test_bigcomp():
114n/a for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50:
115n/a dig10 = 10**ndigs
116n/a for i in range(100 * TEST_SIZE):
117n/a digits = random.randrange(dig10)
118n/a exponent = random.randrange(-400, 400)
119n/a s = '{}e{}'.format(digits, exponent)
120n/a yield s
121n/a
122n/adef test_parsing():
123n/a # make '0' more likely to be chosen than other digits
124n/a digits = '000000123456789'
125n/a signs = ('+', '-', '')
126n/a
127n/a # put together random short valid strings
128n/a # \d*[.\d*]?e
129n/a for i in range(1000):
130n/a for j in range(TEST_SIZE):
131n/a s = random.choice(signs)
132n/a intpart_len = random.randrange(5)
133n/a s += ''.join(random.choice(digits) for _ in range(intpart_len))
134n/a if random.choice([True, False]):
135n/a s += '.'
136n/a fracpart_len = random.randrange(5)
137n/a s += ''.join(random.choice(digits)
138n/a for _ in range(fracpart_len))
139n/a else:
140n/a fracpart_len = 0
141n/a if random.choice([True, False]):
142n/a s += random.choice(['e', 'E'])
143n/a s += random.choice(signs)
144n/a exponent_len = random.randrange(1, 4)
145n/a s += ''.join(random.choice(digits)
146n/a for _ in range(exponent_len))
147n/a
148n/a if intpart_len + fracpart_len:
149n/a yield s
150n/a
151n/atest_particular = [
152n/a # squares
153n/a '1.00000000100000000025',
154n/a '1.0000000000000000000000000100000000000000000000000' #...
155n/a '00025',
156n/a '1.0000000000000000000000000000000000000000000010000' #...
157n/a '0000000000000000000000000000000000000000025',
158n/a '1.0000000000000000000000000000000000000000000000000' #...
159n/a '000001000000000000000000000000000000000000000000000' #...
160n/a '000000000025',
161n/a '0.99999999900000000025',
162n/a '0.9999999999999999999999999999999999999999999999999' #...
163n/a '999000000000000000000000000000000000000000000000000' #...
164n/a '000025',
165n/a '0.9999999999999999999999999999999999999999999999999' #...
166n/a '999999999999999999999999999999999999999999999999999' #...
167n/a '999999999999999999999999999999999999999990000000000' #...
168n/a '000000000000000000000000000000000000000000000000000' #...
169n/a '000000000000000000000000000000000000000000000000000' #...
170n/a '0000000000000000000000000000025',
171n/a
172n/a '1.0000000000000000000000000000000000000000000000000' #...
173n/a '000000000000000000000000000000000000000000000000000' #...
174n/a '100000000000000000000000000000000000000000000000000' #...
175n/a '000000000000000000000000000000000000000000000000001',
176n/a '1.0000000000000000000000000000000000000000000000000' #...
177n/a '000000000000000000000000000000000000000000000000000' #...
178n/a '500000000000000000000000000000000000000000000000000' #...
179n/a '000000000000000000000000000000000000000000000000005',
180n/a '1.0000000000000000000000000000000000000000000000000' #...
181n/a '000000000100000000000000000000000000000000000000000' #...
182n/a '000000000000000000250000000000000002000000000000000' #...
183n/a '000000000000000000000000000000000000000000010000000' #...
184n/a '000000000000000000000000000000000000000000000000000' #...
185n/a '0000000000000000001',
186n/a '1.0000000000000000000000000000000000000000000000000' #...
187n/a '000000000100000000000000000000000000000000000000000' #...
188n/a '000000000000000000249999999999999999999999999999999' #...
189n/a '999999999999979999999999999999999999999999999999999' #...
190n/a '999999999999999999999900000000000000000000000000000' #...
191n/a '000000000000000000000000000000000000000000000000000' #...
192n/a '00000000000000000000000001',
193n/a
194n/a '0.9999999999999999999999999999999999999999999999999' #...
195n/a '999999999900000000000000000000000000000000000000000' #...
196n/a '000000000000000000249999999999999998000000000000000' #...
197n/a '000000000000000000000000000000000000000000010000000' #...
198n/a '000000000000000000000000000000000000000000000000000' #...
199n/a '0000000000000000001',
200n/a '0.9999999999999999999999999999999999999999999999999' #...
201n/a '999999999900000000000000000000000000000000000000000' #...
202n/a '000000000000000000250000001999999999999999999999999' #...
203n/a '999999999999999999999999999999999990000000000000000' #...
204n/a '000000000000000000000000000000000000000000000000000' #...
205n/a '1',
206n/a
207n/a # tough cases for ln etc.
208n/a '1.000000000000000000000000000000000000000000000000' #...
209n/a '00000000000000000000000000000000000000000000000000' #...
210n/a '00100000000000000000000000000000000000000000000000' #...
211n/a '00000000000000000000000000000000000000000000000000' #...
212n/a '0001',
213n/a '0.999999999999999999999999999999999999999999999999' #...
214n/a '99999999999999999999999999999999999999999999999999' #...
215n/a '99899999999999999999999999999999999999999999999999' #...
216n/a '99999999999999999999999999999999999999999999999999' #...
217n/a '99999999999999999999999999999999999999999999999999' #...
218n/a '9999'
219n/a ]
220n/a
221n/a
222n/aTESTCASES = [
223n/a [x for x in test_short_halfway_cases()],
224n/a [x for x in test_halfway_cases()],
225n/a [x for x in test_boundaries()],
226n/a [x for x in test_underflow_boundary()],
227n/a [x for x in test_bigcomp()],
228n/a [x for x in test_parsing()],
229n/a test_particular
230n/a]
231n/a
232n/adef un_randfloat():
233n/a for i in range(1000):
234n/a l = random.choice(TESTCASES[:6])
235n/a yield random.choice(l)
236n/a for v in test_particular:
237n/a yield v
238n/a
239n/adef bin_randfloat():
240n/a for i in range(1000):
241n/a l1 = random.choice(TESTCASES)
242n/a l2 = random.choice(TESTCASES)
243n/a yield random.choice(l1), random.choice(l2)
244n/a
245n/adef tern_randfloat():
246n/a for i in range(1000):
247n/a l1 = random.choice(TESTCASES)
248n/a l2 = random.choice(TESTCASES)
249n/a l3 = random.choice(TESTCASES)
250n/a yield random.choice(l1), random.choice(l2), random.choice(l3)