1 | n/a | # |
---|
2 | n/a | # Copyright (c) 2008-2012 Stefan Krah. All rights reserved. |
---|
3 | n/a | # |
---|
4 | n/a | # Redistribution and use in source and binary forms, with or without |
---|
5 | n/a | # modification, are permitted provided that the following conditions |
---|
6 | n/a | # are met: |
---|
7 | n/a | # |
---|
8 | n/a | # 1. Redistributions of source code must retain the above copyright |
---|
9 | n/a | # notice, this list of conditions and the following disclaimer. |
---|
10 | n/a | # |
---|
11 | n/a | # 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | n/a | # notice, this list of conditions and the following disclaimer in the |
---|
13 | n/a | # documentation and/or other materials provided with the distribution. |
---|
14 | n/a | # |
---|
15 | n/a | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
---|
16 | n/a | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | n/a | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | n/a | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | n/a | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | n/a | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | n/a | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | n/a | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | n/a | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | n/a | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | n/a | # SUCH DAMAGE. |
---|
26 | n/a | # |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | # Generate test cases for deccheck.py. |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | # |
---|
33 | n/a | # Grammar from http://speleotrove.com/decimal/daconvs.html |
---|
34 | n/a | # |
---|
35 | n/a | # sign ::= '+' | '-' |
---|
36 | n/a | # digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | |
---|
37 | n/a | # '8' | '9' |
---|
38 | n/a | # indicator ::= 'e' | 'E' |
---|
39 | n/a | # digits ::= digit [digit]... |
---|
40 | n/a | # decimal-part ::= digits '.' [digits] | ['.'] digits |
---|
41 | n/a | # exponent-part ::= indicator [sign] digits |
---|
42 | n/a | # infinity ::= 'Infinity' | 'Inf' |
---|
43 | n/a | # nan ::= 'NaN' [digits] | 'sNaN' [digits] |
---|
44 | n/a | # numeric-value ::= decimal-part [exponent-part] | infinity |
---|
45 | n/a | # numeric-string ::= [sign] numeric-value | [sign] nan |
---|
46 | n/a | # |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | from random import randrange, sample |
---|
50 | n/a | from fractions import Fraction |
---|
51 | n/a | from randfloat import un_randfloat, bin_randfloat, tern_randfloat |
---|
52 | n/a | |
---|
53 | n/a | |
---|
54 | n/a | def sign(): |
---|
55 | n/a | if randrange(2): |
---|
56 | n/a | if randrange(2): return '+' |
---|
57 | n/a | return '' |
---|
58 | n/a | return '-' |
---|
59 | n/a | |
---|
60 | n/a | def indicator(): |
---|
61 | n/a | return "eE"[randrange(2)] |
---|
62 | n/a | |
---|
63 | n/a | def digits(maxprec): |
---|
64 | n/a | if maxprec == 0: return '' |
---|
65 | n/a | return str(randrange(10**maxprec)) |
---|
66 | n/a | |
---|
67 | n/a | def dot(): |
---|
68 | n/a | if randrange(2): return '.' |
---|
69 | n/a | return '' |
---|
70 | n/a | |
---|
71 | n/a | def decimal_part(maxprec): |
---|
72 | n/a | if randrange(100) > 60: # integers |
---|
73 | n/a | return digits(maxprec) |
---|
74 | n/a | if randrange(2): |
---|
75 | n/a | intlen = randrange(1, maxprec+1) |
---|
76 | n/a | fraclen = maxprec-intlen |
---|
77 | n/a | intpart = digits(intlen) |
---|
78 | n/a | fracpart = digits(fraclen) |
---|
79 | n/a | return ''.join((intpart, '.', fracpart)) |
---|
80 | n/a | else: |
---|
81 | n/a | return ''.join((dot(), digits(maxprec))) |
---|
82 | n/a | |
---|
83 | n/a | def expdigits(maxexp): |
---|
84 | n/a | return str(randrange(maxexp)) |
---|
85 | n/a | |
---|
86 | n/a | def exponent_part(maxexp): |
---|
87 | n/a | return ''.join((indicator(), sign(), expdigits(maxexp))) |
---|
88 | n/a | |
---|
89 | n/a | def infinity(): |
---|
90 | n/a | if randrange(2): return 'Infinity' |
---|
91 | n/a | return 'Inf' |
---|
92 | n/a | |
---|
93 | n/a | def nan(): |
---|
94 | n/a | d = '' |
---|
95 | n/a | if randrange(2): |
---|
96 | n/a | d = digits(randrange(99)) |
---|
97 | n/a | if randrange(2): |
---|
98 | n/a | return ''.join(('NaN', d)) |
---|
99 | n/a | else: |
---|
100 | n/a | return ''.join(('sNaN', d)) |
---|
101 | n/a | |
---|
102 | n/a | def numeric_value(maxprec, maxexp): |
---|
103 | n/a | if randrange(100) > 90: |
---|
104 | n/a | return infinity() |
---|
105 | n/a | exp_part = '' |
---|
106 | n/a | if randrange(100) > 60: |
---|
107 | n/a | exp_part = exponent_part(maxexp) |
---|
108 | n/a | return ''.join((decimal_part(maxprec), exp_part)) |
---|
109 | n/a | |
---|
110 | n/a | def numeric_string(maxprec, maxexp): |
---|
111 | n/a | if randrange(100) > 95: |
---|
112 | n/a | return ''.join((sign(), nan())) |
---|
113 | n/a | else: |
---|
114 | n/a | return ''.join((sign(), numeric_value(maxprec, maxexp))) |
---|
115 | n/a | |
---|
116 | n/a | def randdec(maxprec, maxexp): |
---|
117 | n/a | return numeric_string(maxprec, maxexp) |
---|
118 | n/a | |
---|
119 | n/a | def rand_adjexp(maxprec, maxadjexp): |
---|
120 | n/a | d = digits(maxprec) |
---|
121 | n/a | maxexp = maxadjexp-len(d)+1 |
---|
122 | n/a | if maxexp == 0: maxexp = 1 |
---|
123 | n/a | exp = str(randrange(maxexp-2*(abs(maxexp)), maxexp)) |
---|
124 | n/a | return ''.join((sign(), d, 'E', exp)) |
---|
125 | n/a | |
---|
126 | n/a | |
---|
127 | n/a | def ndigits(n): |
---|
128 | n/a | if n < 1: return 0 |
---|
129 | n/a | return randrange(10**(n-1), 10**n) |
---|
130 | n/a | |
---|
131 | n/a | def randtuple(maxprec, maxexp): |
---|
132 | n/a | n = randrange(100) |
---|
133 | n/a | sign = randrange(2) |
---|
134 | n/a | coeff = ndigits(maxprec) |
---|
135 | n/a | if n >= 95: |
---|
136 | n/a | coeff = () |
---|
137 | n/a | exp = 'F' |
---|
138 | n/a | elif n >= 85: |
---|
139 | n/a | coeff = tuple(map(int, str(ndigits(maxprec)))) |
---|
140 | n/a | exp = "nN"[randrange(2)] |
---|
141 | n/a | else: |
---|
142 | n/a | coeff = tuple(map(int, str(ndigits(maxprec)))) |
---|
143 | n/a | exp = randrange(-maxexp, maxexp) |
---|
144 | n/a | return (sign, coeff, exp) |
---|
145 | n/a | |
---|
146 | n/a | def from_triple(sign, coeff, exp): |
---|
147 | n/a | return ''.join((str(sign*coeff), indicator(), str(exp))) |
---|
148 | n/a | |
---|
149 | n/a | |
---|
150 | n/a | # Close to 10**n |
---|
151 | n/a | def un_close_to_pow10(prec, maxexp, itr=None): |
---|
152 | n/a | if itr is None: |
---|
153 | n/a | lst = range(prec+30) |
---|
154 | n/a | else: |
---|
155 | n/a | lst = sample(range(prec+30), itr) |
---|
156 | n/a | nines = [10**n - 1 for n in lst] |
---|
157 | n/a | pow10 = [10**n for n in lst] |
---|
158 | n/a | for coeff in nines: |
---|
159 | n/a | yield coeff |
---|
160 | n/a | yield -coeff |
---|
161 | n/a | yield from_triple(1, coeff, randrange(2*maxexp)) |
---|
162 | n/a | yield from_triple(-1, coeff, randrange(2*maxexp)) |
---|
163 | n/a | for coeff in pow10: |
---|
164 | n/a | yield coeff |
---|
165 | n/a | yield -coeff |
---|
166 | n/a | |
---|
167 | n/a | # Close to 10**n |
---|
168 | n/a | def bin_close_to_pow10(prec, maxexp, itr=None): |
---|
169 | n/a | if itr is None: |
---|
170 | n/a | lst = range(prec+30) |
---|
171 | n/a | else: |
---|
172 | n/a | lst = sample(range(prec+30), itr) |
---|
173 | n/a | nines = [10**n - 1 for n in lst] |
---|
174 | n/a | pow10 = [10**n for n in lst] |
---|
175 | n/a | for coeff in nines: |
---|
176 | n/a | yield coeff, 1 |
---|
177 | n/a | yield -coeff, -1 |
---|
178 | n/a | yield 1, coeff |
---|
179 | n/a | yield -1, -coeff |
---|
180 | n/a | yield from_triple(1, coeff, randrange(2*maxexp)), 1 |
---|
181 | n/a | yield from_triple(-1, coeff, randrange(2*maxexp)), -1 |
---|
182 | n/a | yield 1, from_triple(1, coeff, -randrange(2*maxexp)) |
---|
183 | n/a | yield -1, from_triple(-1, coeff, -randrange(2*maxexp)) |
---|
184 | n/a | for coeff in pow10: |
---|
185 | n/a | yield coeff, -1 |
---|
186 | n/a | yield -coeff, 1 |
---|
187 | n/a | yield 1, -coeff |
---|
188 | n/a | yield -coeff, 1 |
---|
189 | n/a | |
---|
190 | n/a | # Close to 1: |
---|
191 | n/a | def close_to_one_greater(prec, emax, emin): |
---|
192 | n/a | rprec = 10**prec |
---|
193 | n/a | return ''.join(("1.", '0'*randrange(prec), |
---|
194 | n/a | str(randrange(rprec)))) |
---|
195 | n/a | |
---|
196 | n/a | def close_to_one_less(prec, emax, emin): |
---|
197 | n/a | rprec = 10**prec |
---|
198 | n/a | return ''.join(("0.9", '9'*randrange(prec), |
---|
199 | n/a | str(randrange(rprec)))) |
---|
200 | n/a | |
---|
201 | n/a | # Close to 0: |
---|
202 | n/a | def close_to_zero_greater(prec, emax, emin): |
---|
203 | n/a | rprec = 10**prec |
---|
204 | n/a | return ''.join(("0.", '0'*randrange(prec), |
---|
205 | n/a | str(randrange(rprec)))) |
---|
206 | n/a | |
---|
207 | n/a | def close_to_zero_less(prec, emax, emin): |
---|
208 | n/a | rprec = 10**prec |
---|
209 | n/a | return ''.join(("-0.", '0'*randrange(prec), |
---|
210 | n/a | str(randrange(rprec)))) |
---|
211 | n/a | |
---|
212 | n/a | # Close to emax: |
---|
213 | n/a | def close_to_emax_less(prec, emax, emin): |
---|
214 | n/a | rprec = 10**prec |
---|
215 | n/a | return ''.join(("9.", '9'*randrange(prec), |
---|
216 | n/a | str(randrange(rprec)), "E", str(emax))) |
---|
217 | n/a | |
---|
218 | n/a | def close_to_emax_greater(prec, emax, emin): |
---|
219 | n/a | rprec = 10**prec |
---|
220 | n/a | return ''.join(("1.", '0'*randrange(prec), |
---|
221 | n/a | str(randrange(rprec)), "E", str(emax+1))) |
---|
222 | n/a | |
---|
223 | n/a | # Close to emin: |
---|
224 | n/a | def close_to_emin_greater(prec, emax, emin): |
---|
225 | n/a | rprec = 10**prec |
---|
226 | n/a | return ''.join(("1.", '0'*randrange(prec), |
---|
227 | n/a | str(randrange(rprec)), "E", str(emin))) |
---|
228 | n/a | |
---|
229 | n/a | def close_to_emin_less(prec, emax, emin): |
---|
230 | n/a | rprec = 10**prec |
---|
231 | n/a | return ''.join(("9.", '9'*randrange(prec), |
---|
232 | n/a | str(randrange(rprec)), "E", str(emin-1))) |
---|
233 | n/a | |
---|
234 | n/a | # Close to etiny: |
---|
235 | n/a | def close_to_etiny_greater(prec, emax, emin): |
---|
236 | n/a | rprec = 10**prec |
---|
237 | n/a | etiny = emin - (prec - 1) |
---|
238 | n/a | return ''.join(("1.", '0'*randrange(prec), |
---|
239 | n/a | str(randrange(rprec)), "E", str(etiny))) |
---|
240 | n/a | |
---|
241 | n/a | def close_to_etiny_less(prec, emax, emin): |
---|
242 | n/a | rprec = 10**prec |
---|
243 | n/a | etiny = emin - (prec - 1) |
---|
244 | n/a | return ''.join(("9.", '9'*randrange(prec), |
---|
245 | n/a | str(randrange(rprec)), "E", str(etiny-1))) |
---|
246 | n/a | |
---|
247 | n/a | |
---|
248 | n/a | def close_to_min_etiny_greater(prec, max_prec, min_emin): |
---|
249 | n/a | rprec = 10**prec |
---|
250 | n/a | etiny = min_emin - (max_prec - 1) |
---|
251 | n/a | return ''.join(("1.", '0'*randrange(prec), |
---|
252 | n/a | str(randrange(rprec)), "E", str(etiny))) |
---|
253 | n/a | |
---|
254 | n/a | def close_to_min_etiny_less(prec, max_prec, min_emin): |
---|
255 | n/a | rprec = 10**prec |
---|
256 | n/a | etiny = min_emin - (max_prec - 1) |
---|
257 | n/a | return ''.join(("9.", '9'*randrange(prec), |
---|
258 | n/a | str(randrange(rprec)), "E", str(etiny-1))) |
---|
259 | n/a | |
---|
260 | n/a | |
---|
261 | n/a | close_funcs = [ |
---|
262 | n/a | close_to_one_greater, close_to_one_less, close_to_zero_greater, |
---|
263 | n/a | close_to_zero_less, close_to_emax_less, close_to_emax_greater, |
---|
264 | n/a | close_to_emin_greater, close_to_emin_less, close_to_etiny_greater, |
---|
265 | n/a | close_to_etiny_less, close_to_min_etiny_greater, close_to_min_etiny_less |
---|
266 | n/a | ] |
---|
267 | n/a | |
---|
268 | n/a | |
---|
269 | n/a | def un_close_numbers(prec, emax, emin, itr=None): |
---|
270 | n/a | if itr is None: |
---|
271 | n/a | itr = 1000 |
---|
272 | n/a | for _ in range(itr): |
---|
273 | n/a | for func in close_funcs: |
---|
274 | n/a | yield func(prec, emax, emin) |
---|
275 | n/a | |
---|
276 | n/a | def bin_close_numbers(prec, emax, emin, itr=None): |
---|
277 | n/a | if itr is None: |
---|
278 | n/a | itr = 1000 |
---|
279 | n/a | for _ in range(itr): |
---|
280 | n/a | for func1 in close_funcs: |
---|
281 | n/a | for func2 in close_funcs: |
---|
282 | n/a | yield func1(prec, emax, emin), func2(prec, emax, emin) |
---|
283 | n/a | for func in close_funcs: |
---|
284 | n/a | yield randdec(prec, emax), func(prec, emax, emin) |
---|
285 | n/a | yield func(prec, emax, emin), randdec(prec, emax) |
---|
286 | n/a | |
---|
287 | n/a | def tern_close_numbers(prec, emax, emin, itr): |
---|
288 | n/a | if itr is None: |
---|
289 | n/a | itr = 1000 |
---|
290 | n/a | for _ in range(itr): |
---|
291 | n/a | for func1 in close_funcs: |
---|
292 | n/a | for func2 in close_funcs: |
---|
293 | n/a | for func3 in close_funcs: |
---|
294 | n/a | yield (func1(prec, emax, emin), func2(prec, emax, emin), |
---|
295 | n/a | func3(prec, emax, emin)) |
---|
296 | n/a | for func in close_funcs: |
---|
297 | n/a | yield (randdec(prec, emax), func(prec, emax, emin), |
---|
298 | n/a | func(prec, emax, emin)) |
---|
299 | n/a | yield (func(prec, emax, emin), randdec(prec, emax), |
---|
300 | n/a | func(prec, emax, emin)) |
---|
301 | n/a | yield (func(prec, emax, emin), func(prec, emax, emin), |
---|
302 | n/a | randdec(prec, emax)) |
---|
303 | n/a | for func in close_funcs: |
---|
304 | n/a | yield (randdec(prec, emax), randdec(prec, emax), |
---|
305 | n/a | func(prec, emax, emin)) |
---|
306 | n/a | yield (randdec(prec, emax), func(prec, emax, emin), |
---|
307 | n/a | randdec(prec, emax)) |
---|
308 | n/a | yield (func(prec, emax, emin), randdec(prec, emax), |
---|
309 | n/a | randdec(prec, emax)) |
---|
310 | n/a | |
---|
311 | n/a | |
---|
312 | n/a | # If itr == None, test all digit lengths up to prec + 30 |
---|
313 | n/a | def un_incr_digits(prec, maxexp, itr): |
---|
314 | n/a | if itr is None: |
---|
315 | n/a | lst = range(prec+30) |
---|
316 | n/a | else: |
---|
317 | n/a | lst = sample(range(prec+30), itr) |
---|
318 | n/a | for m in lst: |
---|
319 | n/a | yield from_triple(1, ndigits(m), 0) |
---|
320 | n/a | yield from_triple(-1, ndigits(m), 0) |
---|
321 | n/a | yield from_triple(1, ndigits(m), randrange(maxexp)) |
---|
322 | n/a | yield from_triple(-1, ndigits(m), randrange(maxexp)) |
---|
323 | n/a | |
---|
324 | n/a | # If itr == None, test all digit lengths up to prec + 30 |
---|
325 | n/a | # Also output decimals im tuple form. |
---|
326 | n/a | def un_incr_digits_tuple(prec, maxexp, itr): |
---|
327 | n/a | if itr is None: |
---|
328 | n/a | lst = range(prec+30) |
---|
329 | n/a | else: |
---|
330 | n/a | lst = sample(range(prec+30), itr) |
---|
331 | n/a | for m in lst: |
---|
332 | n/a | yield from_triple(1, ndigits(m), 0) |
---|
333 | n/a | yield from_triple(-1, ndigits(m), 0) |
---|
334 | n/a | yield from_triple(1, ndigits(m), randrange(maxexp)) |
---|
335 | n/a | yield from_triple(-1, ndigits(m), randrange(maxexp)) |
---|
336 | n/a | # test from tuple |
---|
337 | n/a | yield (0, tuple(map(int, str(ndigits(m)))), 0) |
---|
338 | n/a | yield (1, tuple(map(int, str(ndigits(m)))), 0) |
---|
339 | n/a | yield (0, tuple(map(int, str(ndigits(m)))), randrange(maxexp)) |
---|
340 | n/a | yield (1, tuple(map(int, str(ndigits(m)))), randrange(maxexp)) |
---|
341 | n/a | |
---|
342 | n/a | # If itr == None, test all combinations of digit lengths up to prec + 30 |
---|
343 | n/a | def bin_incr_digits(prec, maxexp, itr): |
---|
344 | n/a | if itr is None: |
---|
345 | n/a | lst1 = range(prec+30) |
---|
346 | n/a | lst2 = range(prec+30) |
---|
347 | n/a | else: |
---|
348 | n/a | lst1 = sample(range(prec+30), itr) |
---|
349 | n/a | lst2 = sample(range(prec+30), itr) |
---|
350 | n/a | for m in lst1: |
---|
351 | n/a | x = from_triple(1, ndigits(m), 0) |
---|
352 | n/a | yield x, x |
---|
353 | n/a | x = from_triple(-1, ndigits(m), 0) |
---|
354 | n/a | yield x, x |
---|
355 | n/a | x = from_triple(1, ndigits(m), randrange(maxexp)) |
---|
356 | n/a | yield x, x |
---|
357 | n/a | x = from_triple(-1, ndigits(m), randrange(maxexp)) |
---|
358 | n/a | yield x, x |
---|
359 | n/a | for m in lst1: |
---|
360 | n/a | for n in lst2: |
---|
361 | n/a | x = from_triple(1, ndigits(m), 0) |
---|
362 | n/a | y = from_triple(1, ndigits(n), 0) |
---|
363 | n/a | yield x, y |
---|
364 | n/a | x = from_triple(-1, ndigits(m), 0) |
---|
365 | n/a | y = from_triple(1, ndigits(n), 0) |
---|
366 | n/a | yield x, y |
---|
367 | n/a | x = from_triple(1, ndigits(m), 0) |
---|
368 | n/a | y = from_triple(-1, ndigits(n), 0) |
---|
369 | n/a | yield x, y |
---|
370 | n/a | x = from_triple(-1, ndigits(m), 0) |
---|
371 | n/a | y = from_triple(-1, ndigits(n), 0) |
---|
372 | n/a | yield x, y |
---|
373 | n/a | x = from_triple(1, ndigits(m), randrange(maxexp)) |
---|
374 | n/a | y = from_triple(1, ndigits(n), randrange(maxexp)) |
---|
375 | n/a | yield x, y |
---|
376 | n/a | x = from_triple(-1, ndigits(m), randrange(maxexp)) |
---|
377 | n/a | y = from_triple(1, ndigits(n), randrange(maxexp)) |
---|
378 | n/a | yield x, y |
---|
379 | n/a | x = from_triple(1, ndigits(m), randrange(maxexp)) |
---|
380 | n/a | y = from_triple(-1, ndigits(n), randrange(maxexp)) |
---|
381 | n/a | yield x, y |
---|
382 | n/a | x = from_triple(-1, ndigits(m), randrange(maxexp)) |
---|
383 | n/a | y = from_triple(-1, ndigits(n), randrange(maxexp)) |
---|
384 | n/a | yield x, y |
---|
385 | n/a | |
---|
386 | n/a | |
---|
387 | n/a | def randsign(): |
---|
388 | n/a | return (1, -1)[randrange(2)] |
---|
389 | n/a | |
---|
390 | n/a | # If itr == None, test all combinations of digit lengths up to prec + 30 |
---|
391 | n/a | def tern_incr_digits(prec, maxexp, itr): |
---|
392 | n/a | if itr is None: |
---|
393 | n/a | lst1 = range(prec+30) |
---|
394 | n/a | lst2 = range(prec+30) |
---|
395 | n/a | lst3 = range(prec+30) |
---|
396 | n/a | else: |
---|
397 | n/a | lst1 = sample(range(prec+30), itr) |
---|
398 | n/a | lst2 = sample(range(prec+30), itr) |
---|
399 | n/a | lst3 = sample(range(prec+30), itr) |
---|
400 | n/a | for m in lst1: |
---|
401 | n/a | for n in lst2: |
---|
402 | n/a | for p in lst3: |
---|
403 | n/a | x = from_triple(randsign(), ndigits(m), 0) |
---|
404 | n/a | y = from_triple(randsign(), ndigits(n), 0) |
---|
405 | n/a | z = from_triple(randsign(), ndigits(p), 0) |
---|
406 | n/a | yield x, y, z |
---|
407 | n/a | |
---|
408 | n/a | |
---|
409 | n/a | # Tests for the 'logical' functions |
---|
410 | n/a | def bindigits(prec): |
---|
411 | n/a | z = 0 |
---|
412 | n/a | for i in range(prec): |
---|
413 | n/a | z += randrange(2) * 10**i |
---|
414 | n/a | return z |
---|
415 | n/a | |
---|
416 | n/a | def logical_un_incr_digits(prec, itr): |
---|
417 | n/a | if itr is None: |
---|
418 | n/a | lst = range(prec+30) |
---|
419 | n/a | else: |
---|
420 | n/a | lst = sample(range(prec+30), itr) |
---|
421 | n/a | for m in lst: |
---|
422 | n/a | yield from_triple(1, bindigits(m), 0) |
---|
423 | n/a | |
---|
424 | n/a | def logical_bin_incr_digits(prec, itr): |
---|
425 | n/a | if itr is None: |
---|
426 | n/a | lst1 = range(prec+30) |
---|
427 | n/a | lst2 = range(prec+30) |
---|
428 | n/a | else: |
---|
429 | n/a | lst1 = sample(range(prec+30), itr) |
---|
430 | n/a | lst2 = sample(range(prec+30), itr) |
---|
431 | n/a | for m in lst1: |
---|
432 | n/a | x = from_triple(1, bindigits(m), 0) |
---|
433 | n/a | yield x, x |
---|
434 | n/a | for m in lst1: |
---|
435 | n/a | for n in lst2: |
---|
436 | n/a | x = from_triple(1, bindigits(m), 0) |
---|
437 | n/a | y = from_triple(1, bindigits(n), 0) |
---|
438 | n/a | yield x, y |
---|
439 | n/a | |
---|
440 | n/a | |
---|
441 | n/a | def randint(): |
---|
442 | n/a | p = randrange(1, 100) |
---|
443 | n/a | return ndigits(p) * (1,-1)[randrange(2)] |
---|
444 | n/a | |
---|
445 | n/a | def randfloat(): |
---|
446 | n/a | p = randrange(1, 100) |
---|
447 | n/a | s = numeric_value(p, 383) |
---|
448 | n/a | try: |
---|
449 | n/a | f = float(numeric_value(p, 383)) |
---|
450 | n/a | except ValueError: |
---|
451 | n/a | f = 0.0 |
---|
452 | n/a | return f |
---|
453 | n/a | |
---|
454 | n/a | def randcomplex(): |
---|
455 | n/a | real = randfloat() |
---|
456 | n/a | if randrange(100) > 30: |
---|
457 | n/a | imag = 0.0 |
---|
458 | n/a | else: |
---|
459 | n/a | imag = randfloat() |
---|
460 | n/a | return complex(real, imag) |
---|
461 | n/a | |
---|
462 | n/a | def randfraction(): |
---|
463 | n/a | num = randint() |
---|
464 | n/a | denom = randint() |
---|
465 | n/a | if denom == 0: |
---|
466 | n/a | denom = 1 |
---|
467 | n/a | return Fraction(num, denom) |
---|
468 | n/a | |
---|
469 | n/a | number_funcs = [randint, randfloat, randcomplex, randfraction] |
---|
470 | n/a | |
---|
471 | n/a | def un_random_mixed_op(itr=None): |
---|
472 | n/a | if itr is None: |
---|
473 | n/a | itr = 1000 |
---|
474 | n/a | for _ in range(itr): |
---|
475 | n/a | for func in number_funcs: |
---|
476 | n/a | yield func() |
---|
477 | n/a | # Test garbage input |
---|
478 | n/a | for x in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
479 | n/a | yield x |
---|
480 | n/a | |
---|
481 | n/a | def bin_random_mixed_op(prec, emax, emin, itr=None): |
---|
482 | n/a | if itr is None: |
---|
483 | n/a | itr = 1000 |
---|
484 | n/a | for _ in range(itr): |
---|
485 | n/a | for func in number_funcs: |
---|
486 | n/a | yield randdec(prec, emax), func() |
---|
487 | n/a | yield func(), randdec(prec, emax) |
---|
488 | n/a | for number in number_funcs: |
---|
489 | n/a | for dec in close_funcs: |
---|
490 | n/a | yield dec(prec, emax, emin), number() |
---|
491 | n/a | # Test garbage input |
---|
492 | n/a | for x in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
493 | n/a | for y in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
494 | n/a | yield x, y |
---|
495 | n/a | |
---|
496 | n/a | def tern_random_mixed_op(prec, emax, emin, itr): |
---|
497 | n/a | if itr is None: |
---|
498 | n/a | itr = 1000 |
---|
499 | n/a | for _ in range(itr): |
---|
500 | n/a | for func in number_funcs: |
---|
501 | n/a | yield randdec(prec, emax), randdec(prec, emax), func() |
---|
502 | n/a | yield randdec(prec, emax), func(), func() |
---|
503 | n/a | yield func(), func(), func() |
---|
504 | n/a | # Test garbage input |
---|
505 | n/a | for x in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
506 | n/a | for y in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
507 | n/a | for z in (['x'], ('y',), {'z'}, {1:'z'}): |
---|
508 | n/a | yield x, y, z |
---|
509 | n/a | |
---|
510 | n/a | def all_unary(prec, exp_range, itr): |
---|
511 | n/a | for a in un_close_to_pow10(prec, exp_range, itr): |
---|
512 | n/a | yield (a,) |
---|
513 | n/a | for a in un_close_numbers(prec, exp_range, -exp_range, itr): |
---|
514 | n/a | yield (a,) |
---|
515 | n/a | for a in un_incr_digits_tuple(prec, exp_range, itr): |
---|
516 | n/a | yield (a,) |
---|
517 | n/a | for a in un_randfloat(): |
---|
518 | n/a | yield (a,) |
---|
519 | n/a | for a in un_random_mixed_op(itr): |
---|
520 | n/a | yield (a,) |
---|
521 | n/a | for a in logical_un_incr_digits(prec, itr): |
---|
522 | n/a | yield (a,) |
---|
523 | n/a | for _ in range(100): |
---|
524 | n/a | yield (randdec(prec, exp_range),) |
---|
525 | n/a | for _ in range(100): |
---|
526 | n/a | yield (randtuple(prec, exp_range),) |
---|
527 | n/a | |
---|
528 | n/a | def unary_optarg(prec, exp_range, itr): |
---|
529 | n/a | for _ in range(100): |
---|
530 | n/a | yield randdec(prec, exp_range), None |
---|
531 | n/a | yield randdec(prec, exp_range), None, None |
---|
532 | n/a | |
---|
533 | n/a | def all_binary(prec, exp_range, itr): |
---|
534 | n/a | for a, b in bin_close_to_pow10(prec, exp_range, itr): |
---|
535 | n/a | yield a, b |
---|
536 | n/a | for a, b in bin_close_numbers(prec, exp_range, -exp_range, itr): |
---|
537 | n/a | yield a, b |
---|
538 | n/a | for a, b in bin_incr_digits(prec, exp_range, itr): |
---|
539 | n/a | yield a, b |
---|
540 | n/a | for a, b in bin_randfloat(): |
---|
541 | n/a | yield a, b |
---|
542 | n/a | for a, b in bin_random_mixed_op(prec, exp_range, -exp_range, itr): |
---|
543 | n/a | yield a, b |
---|
544 | n/a | for a, b in logical_bin_incr_digits(prec, itr): |
---|
545 | n/a | yield a, b |
---|
546 | n/a | for _ in range(100): |
---|
547 | n/a | yield randdec(prec, exp_range), randdec(prec, exp_range) |
---|
548 | n/a | |
---|
549 | n/a | def binary_optarg(prec, exp_range, itr): |
---|
550 | n/a | for _ in range(100): |
---|
551 | n/a | yield randdec(prec, exp_range), randdec(prec, exp_range), None |
---|
552 | n/a | yield randdec(prec, exp_range), randdec(prec, exp_range), None, None |
---|
553 | n/a | |
---|
554 | n/a | def all_ternary(prec, exp_range, itr): |
---|
555 | n/a | for a, b, c in tern_close_numbers(prec, exp_range, -exp_range, itr): |
---|
556 | n/a | yield a, b, c |
---|
557 | n/a | for a, b, c in tern_incr_digits(prec, exp_range, itr): |
---|
558 | n/a | yield a, b, c |
---|
559 | n/a | for a, b, c in tern_randfloat(): |
---|
560 | n/a | yield a, b, c |
---|
561 | n/a | for a, b, c in tern_random_mixed_op(prec, exp_range, -exp_range, itr): |
---|
562 | n/a | yield a, b, c |
---|
563 | n/a | for _ in range(100): |
---|
564 | n/a | a = randdec(prec, 2*exp_range) |
---|
565 | n/a | b = randdec(prec, 2*exp_range) |
---|
566 | n/a | c = randdec(prec, 2*exp_range) |
---|
567 | n/a | yield a, b, c |
---|
568 | n/a | |
---|
569 | n/a | def ternary_optarg(prec, exp_range, itr): |
---|
570 | n/a | for _ in range(100): |
---|
571 | n/a | a = randdec(prec, 2*exp_range) |
---|
572 | n/a | b = randdec(prec, 2*exp_range) |
---|
573 | n/a | c = randdec(prec, 2*exp_range) |
---|
574 | n/a | yield a, b, c, None |
---|
575 | n/a | yield a, b, c, None, None |
---|