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