1 | n/a | # Copyright (c) 2004 Python Software Foundation. |
---|
2 | n/a | # All rights reserved. |
---|
3 | n/a | |
---|
4 | n/a | # Written by Eric Price <eprice at tjhsst.edu> |
---|
5 | n/a | # and Facundo Batista <facundo at taniquetil.com.ar> |
---|
6 | n/a | # and Raymond Hettinger <python at rcn.com> |
---|
7 | n/a | # and Aahz (aahz at pobox.com) |
---|
8 | n/a | # and Tim Peters |
---|
9 | n/a | |
---|
10 | n/a | """ |
---|
11 | n/a | These are the test cases for the Decimal module. |
---|
12 | n/a | |
---|
13 | n/a | There are two groups of tests, Arithmetic and Behaviour. The former test |
---|
14 | n/a | the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter |
---|
15 | n/a | test the pythonic behaviour according to PEP 327. |
---|
16 | n/a | |
---|
17 | n/a | Cowlishaw's tests can be downloaded from: |
---|
18 | n/a | |
---|
19 | n/a | http://speleotrove.com/decimal/dectest.zip |
---|
20 | n/a | |
---|
21 | n/a | This test module can be called from command line with one parameter (Arithmetic |
---|
22 | n/a | or Behaviour) to test each part, or without parameter to test both parts. If |
---|
23 | n/a | you're working through IDLE, you can import this test module and call test_main() |
---|
24 | n/a | with the corresponding argument. |
---|
25 | n/a | """ |
---|
26 | n/a | |
---|
27 | n/a | import math |
---|
28 | n/a | import os, sys |
---|
29 | n/a | import operator |
---|
30 | n/a | import warnings |
---|
31 | n/a | import pickle, copy |
---|
32 | n/a | import unittest |
---|
33 | n/a | import numbers |
---|
34 | n/a | import locale |
---|
35 | n/a | from test.support import (run_unittest, run_doctest, is_resource_enabled, |
---|
36 | n/a | requires_IEEE_754, requires_docstrings) |
---|
37 | n/a | from test.support import (check_warnings, import_fresh_module, TestFailed, |
---|
38 | n/a | run_with_locale, cpython_only) |
---|
39 | n/a | import random |
---|
40 | n/a | import inspect |
---|
41 | n/a | try: |
---|
42 | n/a | import threading |
---|
43 | n/a | except ImportError: |
---|
44 | n/a | threading = None |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | C = import_fresh_module('decimal', fresh=['_decimal']) |
---|
48 | n/a | P = import_fresh_module('decimal', blocked=['_decimal']) |
---|
49 | n/a | orig_sys_decimal = sys.modules['decimal'] |
---|
50 | n/a | |
---|
51 | n/a | # fractions module must import the correct decimal module. |
---|
52 | n/a | cfractions = import_fresh_module('fractions', fresh=['fractions']) |
---|
53 | n/a | sys.modules['decimal'] = P |
---|
54 | n/a | pfractions = import_fresh_module('fractions', fresh=['fractions']) |
---|
55 | n/a | sys.modules['decimal'] = C |
---|
56 | n/a | fractions = {C:cfractions, P:pfractions} |
---|
57 | n/a | sys.modules['decimal'] = orig_sys_decimal |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | # Useful Test Constant |
---|
61 | n/a | Signals = { |
---|
62 | n/a | C: tuple(C.getcontext().flags.keys()) if C else None, |
---|
63 | n/a | P: tuple(P.getcontext().flags.keys()) |
---|
64 | n/a | } |
---|
65 | n/a | # Signals ordered with respect to precedence: when an operation |
---|
66 | n/a | # produces multiple signals, signals occurring later in the list |
---|
67 | n/a | # should be handled before those occurring earlier in the list. |
---|
68 | n/a | OrderedSignals = { |
---|
69 | n/a | C: [C.Clamped, C.Rounded, C.Inexact, C.Subnormal, C.Underflow, |
---|
70 | n/a | C.Overflow, C.DivisionByZero, C.InvalidOperation, |
---|
71 | n/a | C.FloatOperation] if C else None, |
---|
72 | n/a | P: [P.Clamped, P.Rounded, P.Inexact, P.Subnormal, P.Underflow, |
---|
73 | n/a | P.Overflow, P.DivisionByZero, P.InvalidOperation, |
---|
74 | n/a | P.FloatOperation] |
---|
75 | n/a | } |
---|
76 | n/a | def assert_signals(cls, context, attr, expected): |
---|
77 | n/a | d = getattr(context, attr) |
---|
78 | n/a | cls.assertTrue(all(d[s] if s in expected else not d[s] for s in d)) |
---|
79 | n/a | |
---|
80 | n/a | ROUND_UP = P.ROUND_UP |
---|
81 | n/a | ROUND_DOWN = P.ROUND_DOWN |
---|
82 | n/a | ROUND_CEILING = P.ROUND_CEILING |
---|
83 | n/a | ROUND_FLOOR = P.ROUND_FLOOR |
---|
84 | n/a | ROUND_HALF_UP = P.ROUND_HALF_UP |
---|
85 | n/a | ROUND_HALF_DOWN = P.ROUND_HALF_DOWN |
---|
86 | n/a | ROUND_HALF_EVEN = P.ROUND_HALF_EVEN |
---|
87 | n/a | ROUND_05UP = P.ROUND_05UP |
---|
88 | n/a | |
---|
89 | n/a | RoundingModes = [ |
---|
90 | n/a | ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, |
---|
91 | n/a | ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, |
---|
92 | n/a | ROUND_05UP |
---|
93 | n/a | ] |
---|
94 | n/a | |
---|
95 | n/a | # Tests are built around these assumed context defaults. |
---|
96 | n/a | # test_main() restores the original context. |
---|
97 | n/a | ORIGINAL_CONTEXT = { |
---|
98 | n/a | C: C.getcontext().copy() if C else None, |
---|
99 | n/a | P: P.getcontext().copy() |
---|
100 | n/a | } |
---|
101 | n/a | def init(m): |
---|
102 | n/a | if not m: return |
---|
103 | n/a | DefaultTestContext = m.Context( |
---|
104 | n/a | prec=9, rounding=ROUND_HALF_EVEN, traps=dict.fromkeys(Signals[m], 0) |
---|
105 | n/a | ) |
---|
106 | n/a | m.setcontext(DefaultTestContext) |
---|
107 | n/a | |
---|
108 | n/a | TESTDATADIR = 'decimaltestdata' |
---|
109 | n/a | if __name__ == '__main__': |
---|
110 | n/a | file = sys.argv[0] |
---|
111 | n/a | else: |
---|
112 | n/a | file = __file__ |
---|
113 | n/a | testdir = os.path.dirname(file) or os.curdir |
---|
114 | n/a | directory = testdir + os.sep + TESTDATADIR + os.sep |
---|
115 | n/a | |
---|
116 | n/a | skip_expected = not os.path.isdir(directory) |
---|
117 | n/a | |
---|
118 | n/a | # Make sure it actually raises errors when not expected and caught in flags |
---|
119 | n/a | # Slower, since it runs some things several times. |
---|
120 | n/a | EXTENDEDERRORTEST = False |
---|
121 | n/a | |
---|
122 | n/a | # Test extra functionality in the C version (-DEXTRA_FUNCTIONALITY). |
---|
123 | n/a | EXTRA_FUNCTIONALITY = True if hasattr(C, 'DecClamped') else False |
---|
124 | n/a | requires_extra_functionality = unittest.skipUnless( |
---|
125 | n/a | EXTRA_FUNCTIONALITY, "test requires build with -DEXTRA_FUNCTIONALITY") |
---|
126 | n/a | skip_if_extra_functionality = unittest.skipIf( |
---|
127 | n/a | EXTRA_FUNCTIONALITY, "test requires regular build") |
---|
128 | n/a | |
---|
129 | n/a | |
---|
130 | n/a | class IBMTestCases(unittest.TestCase): |
---|
131 | n/a | """Class which tests the Decimal class against the IBM test cases.""" |
---|
132 | n/a | |
---|
133 | n/a | def setUp(self): |
---|
134 | n/a | self.context = self.decimal.Context() |
---|
135 | n/a | self.readcontext = self.decimal.Context() |
---|
136 | n/a | self.ignore_list = ['#'] |
---|
137 | n/a | |
---|
138 | n/a | # List of individual .decTest test ids that correspond to tests that |
---|
139 | n/a | # we're skipping for one reason or another. |
---|
140 | n/a | self.skipped_test_ids = set([ |
---|
141 | n/a | # Skip implementation-specific scaleb tests. |
---|
142 | n/a | 'scbx164', |
---|
143 | n/a | 'scbx165', |
---|
144 | n/a | |
---|
145 | n/a | # For some operations (currently exp, ln, log10, power), the decNumber |
---|
146 | n/a | # reference implementation imposes additional restrictions on the context |
---|
147 | n/a | # and operands. These restrictions are not part of the specification; |
---|
148 | n/a | # however, the effect of these restrictions does show up in some of the |
---|
149 | n/a | # testcases. We skip testcases that violate these restrictions, since |
---|
150 | n/a | # Decimal behaves differently from decNumber for these testcases so these |
---|
151 | n/a | # testcases would otherwise fail. |
---|
152 | n/a | 'expx901', |
---|
153 | n/a | 'expx902', |
---|
154 | n/a | 'expx903', |
---|
155 | n/a | 'expx905', |
---|
156 | n/a | 'lnx901', |
---|
157 | n/a | 'lnx902', |
---|
158 | n/a | 'lnx903', |
---|
159 | n/a | 'lnx905', |
---|
160 | n/a | 'logx901', |
---|
161 | n/a | 'logx902', |
---|
162 | n/a | 'logx903', |
---|
163 | n/a | 'logx905', |
---|
164 | n/a | 'powx1183', |
---|
165 | n/a | 'powx1184', |
---|
166 | n/a | 'powx4001', |
---|
167 | n/a | 'powx4002', |
---|
168 | n/a | 'powx4003', |
---|
169 | n/a | 'powx4005', |
---|
170 | n/a | 'powx4008', |
---|
171 | n/a | 'powx4010', |
---|
172 | n/a | 'powx4012', |
---|
173 | n/a | 'powx4014', |
---|
174 | n/a | ]) |
---|
175 | n/a | |
---|
176 | n/a | if self.decimal == C: |
---|
177 | n/a | # status has additional Subnormal, Underflow |
---|
178 | n/a | self.skipped_test_ids.add('pwsx803') |
---|
179 | n/a | self.skipped_test_ids.add('pwsx805') |
---|
180 | n/a | # Correct rounding (skipped for decNumber, too) |
---|
181 | n/a | self.skipped_test_ids.add('powx4302') |
---|
182 | n/a | self.skipped_test_ids.add('powx4303') |
---|
183 | n/a | self.skipped_test_ids.add('powx4342') |
---|
184 | n/a | self.skipped_test_ids.add('powx4343') |
---|
185 | n/a | # http://bugs.python.org/issue7049 |
---|
186 | n/a | self.skipped_test_ids.add('pwmx325') |
---|
187 | n/a | self.skipped_test_ids.add('pwmx326') |
---|
188 | n/a | |
---|
189 | n/a | # Map test directives to setter functions. |
---|
190 | n/a | self.ChangeDict = {'precision' : self.change_precision, |
---|
191 | n/a | 'rounding' : self.change_rounding_method, |
---|
192 | n/a | 'maxexponent' : self.change_max_exponent, |
---|
193 | n/a | 'minexponent' : self.change_min_exponent, |
---|
194 | n/a | 'clamp' : self.change_clamp} |
---|
195 | n/a | |
---|
196 | n/a | # Name adapter to be able to change the Decimal and Context |
---|
197 | n/a | # interface without changing the test files from Cowlishaw. |
---|
198 | n/a | self.NameAdapter = {'and':'logical_and', |
---|
199 | n/a | 'apply':'_apply', |
---|
200 | n/a | 'class':'number_class', |
---|
201 | n/a | 'comparesig':'compare_signal', |
---|
202 | n/a | 'comparetotal':'compare_total', |
---|
203 | n/a | 'comparetotmag':'compare_total_mag', |
---|
204 | n/a | 'copy':'copy_decimal', |
---|
205 | n/a | 'copyabs':'copy_abs', |
---|
206 | n/a | 'copynegate':'copy_negate', |
---|
207 | n/a | 'copysign':'copy_sign', |
---|
208 | n/a | 'divideint':'divide_int', |
---|
209 | n/a | 'invert':'logical_invert', |
---|
210 | n/a | 'iscanonical':'is_canonical', |
---|
211 | n/a | 'isfinite':'is_finite', |
---|
212 | n/a | 'isinfinite':'is_infinite', |
---|
213 | n/a | 'isnan':'is_nan', |
---|
214 | n/a | 'isnormal':'is_normal', |
---|
215 | n/a | 'isqnan':'is_qnan', |
---|
216 | n/a | 'issigned':'is_signed', |
---|
217 | n/a | 'issnan':'is_snan', |
---|
218 | n/a | 'issubnormal':'is_subnormal', |
---|
219 | n/a | 'iszero':'is_zero', |
---|
220 | n/a | 'maxmag':'max_mag', |
---|
221 | n/a | 'minmag':'min_mag', |
---|
222 | n/a | 'nextminus':'next_minus', |
---|
223 | n/a | 'nextplus':'next_plus', |
---|
224 | n/a | 'nexttoward':'next_toward', |
---|
225 | n/a | 'or':'logical_or', |
---|
226 | n/a | 'reduce':'normalize', |
---|
227 | n/a | 'remaindernear':'remainder_near', |
---|
228 | n/a | 'samequantum':'same_quantum', |
---|
229 | n/a | 'squareroot':'sqrt', |
---|
230 | n/a | 'toeng':'to_eng_string', |
---|
231 | n/a | 'tointegral':'to_integral_value', |
---|
232 | n/a | 'tointegralx':'to_integral_exact', |
---|
233 | n/a | 'tosci':'to_sci_string', |
---|
234 | n/a | 'xor':'logical_xor'} |
---|
235 | n/a | |
---|
236 | n/a | # Map test-case names to roundings. |
---|
237 | n/a | self.RoundingDict = {'ceiling' : ROUND_CEILING, |
---|
238 | n/a | 'down' : ROUND_DOWN, |
---|
239 | n/a | 'floor' : ROUND_FLOOR, |
---|
240 | n/a | 'half_down' : ROUND_HALF_DOWN, |
---|
241 | n/a | 'half_even' : ROUND_HALF_EVEN, |
---|
242 | n/a | 'half_up' : ROUND_HALF_UP, |
---|
243 | n/a | 'up' : ROUND_UP, |
---|
244 | n/a | '05up' : ROUND_05UP} |
---|
245 | n/a | |
---|
246 | n/a | # Map the test cases' error names to the actual errors. |
---|
247 | n/a | self.ErrorNames = {'clamped' : self.decimal.Clamped, |
---|
248 | n/a | 'conversion_syntax' : self.decimal.InvalidOperation, |
---|
249 | n/a | 'division_by_zero' : self.decimal.DivisionByZero, |
---|
250 | n/a | 'division_impossible' : self.decimal.InvalidOperation, |
---|
251 | n/a | 'division_undefined' : self.decimal.InvalidOperation, |
---|
252 | n/a | 'inexact' : self.decimal.Inexact, |
---|
253 | n/a | 'invalid_context' : self.decimal.InvalidOperation, |
---|
254 | n/a | 'invalid_operation' : self.decimal.InvalidOperation, |
---|
255 | n/a | 'overflow' : self.decimal.Overflow, |
---|
256 | n/a | 'rounded' : self.decimal.Rounded, |
---|
257 | n/a | 'subnormal' : self.decimal.Subnormal, |
---|
258 | n/a | 'underflow' : self.decimal.Underflow} |
---|
259 | n/a | |
---|
260 | n/a | # The following functions return True/False rather than a |
---|
261 | n/a | # Decimal instance. |
---|
262 | n/a | self.LogicalFunctions = ('is_canonical', |
---|
263 | n/a | 'is_finite', |
---|
264 | n/a | 'is_infinite', |
---|
265 | n/a | 'is_nan', |
---|
266 | n/a | 'is_normal', |
---|
267 | n/a | 'is_qnan', |
---|
268 | n/a | 'is_signed', |
---|
269 | n/a | 'is_snan', |
---|
270 | n/a | 'is_subnormal', |
---|
271 | n/a | 'is_zero', |
---|
272 | n/a | 'same_quantum') |
---|
273 | n/a | |
---|
274 | n/a | def read_unlimited(self, v, context): |
---|
275 | n/a | """Work around the limitations of the 32-bit _decimal version. The |
---|
276 | n/a | guaranteed maximum values for prec, Emax etc. are 425000000, |
---|
277 | n/a | but higher values usually work, except for rare corner cases. |
---|
278 | n/a | In particular, all of the IBM tests pass with maximum values |
---|
279 | n/a | of 1070000000.""" |
---|
280 | n/a | if self.decimal == C and self.decimal.MAX_EMAX == 425000000: |
---|
281 | n/a | self.readcontext._unsafe_setprec(1070000000) |
---|
282 | n/a | self.readcontext._unsafe_setemax(1070000000) |
---|
283 | n/a | self.readcontext._unsafe_setemin(-1070000000) |
---|
284 | n/a | return self.readcontext.create_decimal(v) |
---|
285 | n/a | else: |
---|
286 | n/a | return self.decimal.Decimal(v, context) |
---|
287 | n/a | |
---|
288 | n/a | def eval_file(self, file): |
---|
289 | n/a | global skip_expected |
---|
290 | n/a | if skip_expected: |
---|
291 | n/a | raise unittest.SkipTest |
---|
292 | n/a | with open(file) as f: |
---|
293 | n/a | for line in f: |
---|
294 | n/a | line = line.replace('\r\n', '').replace('\n', '') |
---|
295 | n/a | #print line |
---|
296 | n/a | try: |
---|
297 | n/a | t = self.eval_line(line) |
---|
298 | n/a | except self.decimal.DecimalException as exception: |
---|
299 | n/a | #Exception raised where there shouldn't have been one. |
---|
300 | n/a | self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line) |
---|
301 | n/a | |
---|
302 | n/a | |
---|
303 | n/a | def eval_line(self, s): |
---|
304 | n/a | if s.find(' -> ') >= 0 and s[:2] != '--' and not s.startswith(' --'): |
---|
305 | n/a | s = (s.split('->')[0] + '->' + |
---|
306 | n/a | s.split('->')[1].split('--')[0]).strip() |
---|
307 | n/a | else: |
---|
308 | n/a | s = s.split('--')[0].strip() |
---|
309 | n/a | |
---|
310 | n/a | for ignore in self.ignore_list: |
---|
311 | n/a | if s.find(ignore) >= 0: |
---|
312 | n/a | #print s.split()[0], 'NotImplemented--', ignore |
---|
313 | n/a | return |
---|
314 | n/a | if not s: |
---|
315 | n/a | return |
---|
316 | n/a | elif ':' in s: |
---|
317 | n/a | return self.eval_directive(s) |
---|
318 | n/a | else: |
---|
319 | n/a | return self.eval_equation(s) |
---|
320 | n/a | |
---|
321 | n/a | def eval_directive(self, s): |
---|
322 | n/a | funct, value = (x.strip().lower() for x in s.split(':')) |
---|
323 | n/a | if funct == 'rounding': |
---|
324 | n/a | value = self.RoundingDict[value] |
---|
325 | n/a | else: |
---|
326 | n/a | try: |
---|
327 | n/a | value = int(value) |
---|
328 | n/a | except ValueError: |
---|
329 | n/a | pass |
---|
330 | n/a | |
---|
331 | n/a | funct = self.ChangeDict.get(funct, (lambda *args: None)) |
---|
332 | n/a | funct(value) |
---|
333 | n/a | |
---|
334 | n/a | def eval_equation(self, s): |
---|
335 | n/a | |
---|
336 | n/a | if not TEST_ALL and random.random() < 0.90: |
---|
337 | n/a | return |
---|
338 | n/a | |
---|
339 | n/a | self.context.clear_flags() |
---|
340 | n/a | |
---|
341 | n/a | try: |
---|
342 | n/a | Sides = s.split('->') |
---|
343 | n/a | L = Sides[0].strip().split() |
---|
344 | n/a | id = L[0] |
---|
345 | n/a | if DEBUG: |
---|
346 | n/a | print("Test ", id, end=" ") |
---|
347 | n/a | funct = L[1].lower() |
---|
348 | n/a | valstemp = L[2:] |
---|
349 | n/a | L = Sides[1].strip().split() |
---|
350 | n/a | ans = L[0] |
---|
351 | n/a | exceptions = L[1:] |
---|
352 | n/a | except (TypeError, AttributeError, IndexError): |
---|
353 | n/a | raise self.decimal.InvalidOperation |
---|
354 | n/a | def FixQuotes(val): |
---|
355 | n/a | val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote') |
---|
356 | n/a | val = val.replace("'", '').replace('"', '') |
---|
357 | n/a | val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"') |
---|
358 | n/a | return val |
---|
359 | n/a | |
---|
360 | n/a | if id in self.skipped_test_ids: |
---|
361 | n/a | return |
---|
362 | n/a | |
---|
363 | n/a | fname = self.NameAdapter.get(funct, funct) |
---|
364 | n/a | if fname == 'rescale': |
---|
365 | n/a | return |
---|
366 | n/a | funct = getattr(self.context, fname) |
---|
367 | n/a | vals = [] |
---|
368 | n/a | conglomerate = '' |
---|
369 | n/a | quote = 0 |
---|
370 | n/a | theirexceptions = [self.ErrorNames[x.lower()] for x in exceptions] |
---|
371 | n/a | |
---|
372 | n/a | for exception in Signals[self.decimal]: |
---|
373 | n/a | self.context.traps[exception] = 1 #Catch these bugs... |
---|
374 | n/a | for exception in theirexceptions: |
---|
375 | n/a | self.context.traps[exception] = 0 |
---|
376 | n/a | for i, val in enumerate(valstemp): |
---|
377 | n/a | if val.count("'") % 2 == 1: |
---|
378 | n/a | quote = 1 - quote |
---|
379 | n/a | if quote: |
---|
380 | n/a | conglomerate = conglomerate + ' ' + val |
---|
381 | n/a | continue |
---|
382 | n/a | else: |
---|
383 | n/a | val = conglomerate + val |
---|
384 | n/a | conglomerate = '' |
---|
385 | n/a | v = FixQuotes(val) |
---|
386 | n/a | if fname in ('to_sci_string', 'to_eng_string'): |
---|
387 | n/a | if EXTENDEDERRORTEST: |
---|
388 | n/a | for error in theirexceptions: |
---|
389 | n/a | self.context.traps[error] = 1 |
---|
390 | n/a | try: |
---|
391 | n/a | funct(self.context.create_decimal(v)) |
---|
392 | n/a | except error: |
---|
393 | n/a | pass |
---|
394 | n/a | except Signals[self.decimal] as e: |
---|
395 | n/a | self.fail("Raised %s in %s when %s disabled" % \ |
---|
396 | n/a | (e, s, error)) |
---|
397 | n/a | else: |
---|
398 | n/a | self.fail("Did not raise %s in %s" % (error, s)) |
---|
399 | n/a | self.context.traps[error] = 0 |
---|
400 | n/a | v = self.context.create_decimal(v) |
---|
401 | n/a | else: |
---|
402 | n/a | v = self.read_unlimited(v, self.context) |
---|
403 | n/a | vals.append(v) |
---|
404 | n/a | |
---|
405 | n/a | ans = FixQuotes(ans) |
---|
406 | n/a | |
---|
407 | n/a | if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'): |
---|
408 | n/a | for error in theirexceptions: |
---|
409 | n/a | self.context.traps[error] = 1 |
---|
410 | n/a | try: |
---|
411 | n/a | funct(*vals) |
---|
412 | n/a | except error: |
---|
413 | n/a | pass |
---|
414 | n/a | except Signals[self.decimal] as e: |
---|
415 | n/a | self.fail("Raised %s in %s when %s disabled" % \ |
---|
416 | n/a | (e, s, error)) |
---|
417 | n/a | else: |
---|
418 | n/a | self.fail("Did not raise %s in %s" % (error, s)) |
---|
419 | n/a | self.context.traps[error] = 0 |
---|
420 | n/a | |
---|
421 | n/a | # as above, but add traps cumulatively, to check precedence |
---|
422 | n/a | ordered_errors = [e for e in OrderedSignals[self.decimal] if e in theirexceptions] |
---|
423 | n/a | for error in ordered_errors: |
---|
424 | n/a | self.context.traps[error] = 1 |
---|
425 | n/a | try: |
---|
426 | n/a | funct(*vals) |
---|
427 | n/a | except error: |
---|
428 | n/a | pass |
---|
429 | n/a | except Signals[self.decimal] as e: |
---|
430 | n/a | self.fail("Raised %s in %s; expected %s" % |
---|
431 | n/a | (type(e), s, error)) |
---|
432 | n/a | else: |
---|
433 | n/a | self.fail("Did not raise %s in %s" % (error, s)) |
---|
434 | n/a | # reset traps |
---|
435 | n/a | for error in ordered_errors: |
---|
436 | n/a | self.context.traps[error] = 0 |
---|
437 | n/a | |
---|
438 | n/a | |
---|
439 | n/a | if DEBUG: |
---|
440 | n/a | print("--", self.context) |
---|
441 | n/a | try: |
---|
442 | n/a | result = str(funct(*vals)) |
---|
443 | n/a | if fname in self.LogicalFunctions: |
---|
444 | n/a | result = str(int(eval(result))) # 'True', 'False' -> '1', '0' |
---|
445 | n/a | except Signals[self.decimal] as error: |
---|
446 | n/a | self.fail("Raised %s in %s" % (error, s)) |
---|
447 | n/a | except: #Catch any error long enough to state the test case. |
---|
448 | n/a | print("ERROR:", s) |
---|
449 | n/a | raise |
---|
450 | n/a | |
---|
451 | n/a | myexceptions = self.getexceptions() |
---|
452 | n/a | |
---|
453 | n/a | myexceptions.sort(key=repr) |
---|
454 | n/a | theirexceptions.sort(key=repr) |
---|
455 | n/a | |
---|
456 | n/a | self.assertEqual(result, ans, |
---|
457 | n/a | 'Incorrect answer for ' + s + ' -- got ' + result) |
---|
458 | n/a | |
---|
459 | n/a | self.assertEqual(myexceptions, theirexceptions, |
---|
460 | n/a | 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions)) |
---|
461 | n/a | |
---|
462 | n/a | def getexceptions(self): |
---|
463 | n/a | return [e for e in Signals[self.decimal] if self.context.flags[e]] |
---|
464 | n/a | |
---|
465 | n/a | def change_precision(self, prec): |
---|
466 | n/a | if self.decimal == C and self.decimal.MAX_PREC == 425000000: |
---|
467 | n/a | self.context._unsafe_setprec(prec) |
---|
468 | n/a | else: |
---|
469 | n/a | self.context.prec = prec |
---|
470 | n/a | def change_rounding_method(self, rounding): |
---|
471 | n/a | self.context.rounding = rounding |
---|
472 | n/a | def change_min_exponent(self, exp): |
---|
473 | n/a | if self.decimal == C and self.decimal.MAX_PREC == 425000000: |
---|
474 | n/a | self.context._unsafe_setemin(exp) |
---|
475 | n/a | else: |
---|
476 | n/a | self.context.Emin = exp |
---|
477 | n/a | def change_max_exponent(self, exp): |
---|
478 | n/a | if self.decimal == C and self.decimal.MAX_PREC == 425000000: |
---|
479 | n/a | self.context._unsafe_setemax(exp) |
---|
480 | n/a | else: |
---|
481 | n/a | self.context.Emax = exp |
---|
482 | n/a | def change_clamp(self, clamp): |
---|
483 | n/a | self.context.clamp = clamp |
---|
484 | n/a | |
---|
485 | n/a | class CIBMTestCases(IBMTestCases): |
---|
486 | n/a | decimal = C |
---|
487 | n/a | class PyIBMTestCases(IBMTestCases): |
---|
488 | n/a | decimal = P |
---|
489 | n/a | |
---|
490 | n/a | # The following classes test the behaviour of Decimal according to PEP 327 |
---|
491 | n/a | |
---|
492 | n/a | class ExplicitConstructionTest(unittest.TestCase): |
---|
493 | n/a | '''Unit tests for Explicit Construction cases of Decimal.''' |
---|
494 | n/a | |
---|
495 | n/a | def test_explicit_empty(self): |
---|
496 | n/a | Decimal = self.decimal.Decimal |
---|
497 | n/a | self.assertEqual(Decimal(), Decimal("0")) |
---|
498 | n/a | |
---|
499 | n/a | def test_explicit_from_None(self): |
---|
500 | n/a | Decimal = self.decimal.Decimal |
---|
501 | n/a | self.assertRaises(TypeError, Decimal, None) |
---|
502 | n/a | |
---|
503 | n/a | def test_explicit_from_int(self): |
---|
504 | n/a | Decimal = self.decimal.Decimal |
---|
505 | n/a | |
---|
506 | n/a | #positive |
---|
507 | n/a | d = Decimal(45) |
---|
508 | n/a | self.assertEqual(str(d), '45') |
---|
509 | n/a | |
---|
510 | n/a | #very large positive |
---|
511 | n/a | d = Decimal(500000123) |
---|
512 | n/a | self.assertEqual(str(d), '500000123') |
---|
513 | n/a | |
---|
514 | n/a | #negative |
---|
515 | n/a | d = Decimal(-45) |
---|
516 | n/a | self.assertEqual(str(d), '-45') |
---|
517 | n/a | |
---|
518 | n/a | #zero |
---|
519 | n/a | d = Decimal(0) |
---|
520 | n/a | self.assertEqual(str(d), '0') |
---|
521 | n/a | |
---|
522 | n/a | # single word longs |
---|
523 | n/a | for n in range(0, 32): |
---|
524 | n/a | for sign in (-1, 1): |
---|
525 | n/a | for x in range(-5, 5): |
---|
526 | n/a | i = sign * (2**n + x) |
---|
527 | n/a | d = Decimal(i) |
---|
528 | n/a | self.assertEqual(str(d), str(i)) |
---|
529 | n/a | |
---|
530 | n/a | def test_explicit_from_string(self): |
---|
531 | n/a | Decimal = self.decimal.Decimal |
---|
532 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
533 | n/a | localcontext = self.decimal.localcontext |
---|
534 | n/a | |
---|
535 | n/a | #empty |
---|
536 | n/a | self.assertEqual(str(Decimal('')), 'NaN') |
---|
537 | n/a | |
---|
538 | n/a | #int |
---|
539 | n/a | self.assertEqual(str(Decimal('45')), '45') |
---|
540 | n/a | |
---|
541 | n/a | #float |
---|
542 | n/a | self.assertEqual(str(Decimal('45.34')), '45.34') |
---|
543 | n/a | |
---|
544 | n/a | #engineer notation |
---|
545 | n/a | self.assertEqual(str(Decimal('45e2')), '4.5E+3') |
---|
546 | n/a | |
---|
547 | n/a | #just not a number |
---|
548 | n/a | self.assertEqual(str(Decimal('ugly')), 'NaN') |
---|
549 | n/a | |
---|
550 | n/a | #leading and trailing whitespace permitted |
---|
551 | n/a | self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4') |
---|
552 | n/a | self.assertEqual(str(Decimal(' -7.89')), '-7.89') |
---|
553 | n/a | self.assertEqual(str(Decimal(" 3.45679 ")), '3.45679') |
---|
554 | n/a | |
---|
555 | n/a | # underscores |
---|
556 | n/a | self.assertEqual(str(Decimal('1_3.3e4_0')), '1.33E+41') |
---|
557 | n/a | self.assertEqual(str(Decimal('1_0_0_0')), '1000') |
---|
558 | n/a | |
---|
559 | n/a | # unicode whitespace |
---|
560 | n/a | for lead in ["", ' ', '\u00a0', '\u205f']: |
---|
561 | n/a | for trail in ["", ' ', '\u00a0', '\u205f']: |
---|
562 | n/a | self.assertEqual(str(Decimal(lead + '9.311E+28' + trail)), |
---|
563 | n/a | '9.311E+28') |
---|
564 | n/a | |
---|
565 | n/a | with localcontext() as c: |
---|
566 | n/a | c.traps[InvalidOperation] = True |
---|
567 | n/a | # Invalid string |
---|
568 | n/a | self.assertRaises(InvalidOperation, Decimal, "xyz") |
---|
569 | n/a | # Two arguments max |
---|
570 | n/a | self.assertRaises(TypeError, Decimal, "1234", "x", "y") |
---|
571 | n/a | |
---|
572 | n/a | # space within the numeric part |
---|
573 | n/a | self.assertRaises(InvalidOperation, Decimal, "1\u00a02\u00a03") |
---|
574 | n/a | self.assertRaises(InvalidOperation, Decimal, "\u00a01\u00a02\u00a0") |
---|
575 | n/a | |
---|
576 | n/a | # unicode whitespace |
---|
577 | n/a | self.assertRaises(InvalidOperation, Decimal, "\u00a0") |
---|
578 | n/a | self.assertRaises(InvalidOperation, Decimal, "\u00a0\u00a0") |
---|
579 | n/a | |
---|
580 | n/a | # embedded NUL |
---|
581 | n/a | self.assertRaises(InvalidOperation, Decimal, "12\u00003") |
---|
582 | n/a | |
---|
583 | n/a | # underscores don't prevent errors |
---|
584 | n/a | self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003") |
---|
585 | n/a | |
---|
586 | n/a | @cpython_only |
---|
587 | n/a | def test_from_legacy_strings(self): |
---|
588 | n/a | import _testcapi |
---|
589 | n/a | Decimal = self.decimal.Decimal |
---|
590 | n/a | context = self.decimal.Context() |
---|
591 | n/a | |
---|
592 | n/a | s = _testcapi.unicode_legacy_string('9.999999') |
---|
593 | n/a | self.assertEqual(str(Decimal(s)), '9.999999') |
---|
594 | n/a | self.assertEqual(str(context.create_decimal(s)), '9.999999') |
---|
595 | n/a | |
---|
596 | n/a | def test_explicit_from_tuples(self): |
---|
597 | n/a | Decimal = self.decimal.Decimal |
---|
598 | n/a | |
---|
599 | n/a | #zero |
---|
600 | n/a | d = Decimal( (0, (0,), 0) ) |
---|
601 | n/a | self.assertEqual(str(d), '0') |
---|
602 | n/a | |
---|
603 | n/a | #int |
---|
604 | n/a | d = Decimal( (1, (4, 5), 0) ) |
---|
605 | n/a | self.assertEqual(str(d), '-45') |
---|
606 | n/a | |
---|
607 | n/a | #float |
---|
608 | n/a | d = Decimal( (0, (4, 5, 3, 4), -2) ) |
---|
609 | n/a | self.assertEqual(str(d), '45.34') |
---|
610 | n/a | |
---|
611 | n/a | #weird |
---|
612 | n/a | d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) |
---|
613 | n/a | self.assertEqual(str(d), '-4.34913534E-17') |
---|
614 | n/a | |
---|
615 | n/a | #inf |
---|
616 | n/a | d = Decimal( (0, (), "F") ) |
---|
617 | n/a | self.assertEqual(str(d), 'Infinity') |
---|
618 | n/a | |
---|
619 | n/a | #wrong number of items |
---|
620 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) ) |
---|
621 | n/a | |
---|
622 | n/a | #bad sign |
---|
623 | n/a | self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) ) |
---|
624 | n/a | self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) ) |
---|
625 | n/a | self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2)) |
---|
626 | n/a | |
---|
627 | n/a | #bad exp |
---|
628 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') ) |
---|
629 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) ) |
---|
630 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') ) |
---|
631 | n/a | |
---|
632 | n/a | #bad coefficients |
---|
633 | n/a | self.assertRaises(ValueError, Decimal, (1, "xyz", 2) ) |
---|
634 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) ) |
---|
635 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) ) |
---|
636 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) ) |
---|
637 | n/a | self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) ) |
---|
638 | n/a | |
---|
639 | n/a | def test_explicit_from_list(self): |
---|
640 | n/a | Decimal = self.decimal.Decimal |
---|
641 | n/a | |
---|
642 | n/a | d = Decimal([0, [0], 0]) |
---|
643 | n/a | self.assertEqual(str(d), '0') |
---|
644 | n/a | |
---|
645 | n/a | d = Decimal([1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25]) |
---|
646 | n/a | self.assertEqual(str(d), '-4.34913534E-17') |
---|
647 | n/a | |
---|
648 | n/a | d = Decimal([1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25]) |
---|
649 | n/a | self.assertEqual(str(d), '-4.34913534E-17') |
---|
650 | n/a | |
---|
651 | n/a | d = Decimal((1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25)) |
---|
652 | n/a | self.assertEqual(str(d), '-4.34913534E-17') |
---|
653 | n/a | |
---|
654 | n/a | def test_explicit_from_bool(self): |
---|
655 | n/a | Decimal = self.decimal.Decimal |
---|
656 | n/a | |
---|
657 | n/a | self.assertIs(bool(Decimal(0)), False) |
---|
658 | n/a | self.assertIs(bool(Decimal(1)), True) |
---|
659 | n/a | self.assertEqual(Decimal(False), Decimal(0)) |
---|
660 | n/a | self.assertEqual(Decimal(True), Decimal(1)) |
---|
661 | n/a | |
---|
662 | n/a | def test_explicit_from_Decimal(self): |
---|
663 | n/a | Decimal = self.decimal.Decimal |
---|
664 | n/a | |
---|
665 | n/a | #positive |
---|
666 | n/a | d = Decimal(45) |
---|
667 | n/a | e = Decimal(d) |
---|
668 | n/a | self.assertEqual(str(e), '45') |
---|
669 | n/a | |
---|
670 | n/a | #very large positive |
---|
671 | n/a | d = Decimal(500000123) |
---|
672 | n/a | e = Decimal(d) |
---|
673 | n/a | self.assertEqual(str(e), '500000123') |
---|
674 | n/a | |
---|
675 | n/a | #negative |
---|
676 | n/a | d = Decimal(-45) |
---|
677 | n/a | e = Decimal(d) |
---|
678 | n/a | self.assertEqual(str(e), '-45') |
---|
679 | n/a | |
---|
680 | n/a | #zero |
---|
681 | n/a | d = Decimal(0) |
---|
682 | n/a | e = Decimal(d) |
---|
683 | n/a | self.assertEqual(str(e), '0') |
---|
684 | n/a | |
---|
685 | n/a | @requires_IEEE_754 |
---|
686 | n/a | def test_explicit_from_float(self): |
---|
687 | n/a | |
---|
688 | n/a | Decimal = self.decimal.Decimal |
---|
689 | n/a | |
---|
690 | n/a | r = Decimal(0.1) |
---|
691 | n/a | self.assertEqual(type(r), Decimal) |
---|
692 | n/a | self.assertEqual(str(r), |
---|
693 | n/a | '0.1000000000000000055511151231257827021181583404541015625') |
---|
694 | n/a | self.assertTrue(Decimal(float('nan')).is_qnan()) |
---|
695 | n/a | self.assertTrue(Decimal(float('inf')).is_infinite()) |
---|
696 | n/a | self.assertTrue(Decimal(float('-inf')).is_infinite()) |
---|
697 | n/a | self.assertEqual(str(Decimal(float('nan'))), |
---|
698 | n/a | str(Decimal('NaN'))) |
---|
699 | n/a | self.assertEqual(str(Decimal(float('inf'))), |
---|
700 | n/a | str(Decimal('Infinity'))) |
---|
701 | n/a | self.assertEqual(str(Decimal(float('-inf'))), |
---|
702 | n/a | str(Decimal('-Infinity'))) |
---|
703 | n/a | self.assertEqual(str(Decimal(float('-0.0'))), |
---|
704 | n/a | str(Decimal('-0'))) |
---|
705 | n/a | for i in range(200): |
---|
706 | n/a | x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) |
---|
707 | n/a | self.assertEqual(x, float(Decimal(x))) # roundtrip |
---|
708 | n/a | |
---|
709 | n/a | def test_explicit_context_create_decimal(self): |
---|
710 | n/a | Decimal = self.decimal.Decimal |
---|
711 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
712 | n/a | Rounded = self.decimal.Rounded |
---|
713 | n/a | |
---|
714 | n/a | nc = copy.copy(self.decimal.getcontext()) |
---|
715 | n/a | nc.prec = 3 |
---|
716 | n/a | |
---|
717 | n/a | # empty |
---|
718 | n/a | d = Decimal() |
---|
719 | n/a | self.assertEqual(str(d), '0') |
---|
720 | n/a | d = nc.create_decimal() |
---|
721 | n/a | self.assertEqual(str(d), '0') |
---|
722 | n/a | |
---|
723 | n/a | # from None |
---|
724 | n/a | self.assertRaises(TypeError, nc.create_decimal, None) |
---|
725 | n/a | |
---|
726 | n/a | # from int |
---|
727 | n/a | d = nc.create_decimal(456) |
---|
728 | n/a | self.assertIsInstance(d, Decimal) |
---|
729 | n/a | self.assertEqual(nc.create_decimal(45678), |
---|
730 | n/a | nc.create_decimal('457E+2')) |
---|
731 | n/a | |
---|
732 | n/a | # from string |
---|
733 | n/a | d = Decimal('456789') |
---|
734 | n/a | self.assertEqual(str(d), '456789') |
---|
735 | n/a | d = nc.create_decimal('456789') |
---|
736 | n/a | self.assertEqual(str(d), '4.57E+5') |
---|
737 | n/a | # leading and trailing whitespace should result in a NaN; |
---|
738 | n/a | # spaces are already checked in Cowlishaw's test-suite, so |
---|
739 | n/a | # here we just check that a trailing newline results in a NaN |
---|
740 | n/a | self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN') |
---|
741 | n/a | |
---|
742 | n/a | # from tuples |
---|
743 | n/a | d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) |
---|
744 | n/a | self.assertEqual(str(d), '-4.34913534E-17') |
---|
745 | n/a | d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) |
---|
746 | n/a | self.assertEqual(str(d), '-4.35E-17') |
---|
747 | n/a | |
---|
748 | n/a | # from Decimal |
---|
749 | n/a | prevdec = Decimal(500000123) |
---|
750 | n/a | d = Decimal(prevdec) |
---|
751 | n/a | self.assertEqual(str(d), '500000123') |
---|
752 | n/a | d = nc.create_decimal(prevdec) |
---|
753 | n/a | self.assertEqual(str(d), '5.00E+8') |
---|
754 | n/a | |
---|
755 | n/a | # more integers |
---|
756 | n/a | nc.prec = 28 |
---|
757 | n/a | nc.traps[InvalidOperation] = True |
---|
758 | n/a | |
---|
759 | n/a | for v in [-2**63-1, -2**63, -2**31-1, -2**31, 0, |
---|
760 | n/a | 2**31-1, 2**31, 2**63-1, 2**63]: |
---|
761 | n/a | d = nc.create_decimal(v) |
---|
762 | n/a | self.assertTrue(isinstance(d, Decimal)) |
---|
763 | n/a | self.assertEqual(int(d), v) |
---|
764 | n/a | |
---|
765 | n/a | nc.prec = 3 |
---|
766 | n/a | nc.traps[Rounded] = True |
---|
767 | n/a | self.assertRaises(Rounded, nc.create_decimal, 1234) |
---|
768 | n/a | |
---|
769 | n/a | # from string |
---|
770 | n/a | nc.prec = 28 |
---|
771 | n/a | self.assertEqual(str(nc.create_decimal('0E-017')), '0E-17') |
---|
772 | n/a | self.assertEqual(str(nc.create_decimal('45')), '45') |
---|
773 | n/a | self.assertEqual(str(nc.create_decimal('-Inf')), '-Infinity') |
---|
774 | n/a | self.assertEqual(str(nc.create_decimal('NaN123')), 'NaN123') |
---|
775 | n/a | |
---|
776 | n/a | # invalid arguments |
---|
777 | n/a | self.assertRaises(InvalidOperation, nc.create_decimal, "xyz") |
---|
778 | n/a | self.assertRaises(ValueError, nc.create_decimal, (1, "xyz", -25)) |
---|
779 | n/a | self.assertRaises(TypeError, nc.create_decimal, "1234", "5678") |
---|
780 | n/a | # no whitespace and underscore stripping is done with this method |
---|
781 | n/a | self.assertRaises(InvalidOperation, nc.create_decimal, " 1234") |
---|
782 | n/a | self.assertRaises(InvalidOperation, nc.create_decimal, "12_34") |
---|
783 | n/a | |
---|
784 | n/a | # too many NaN payload digits |
---|
785 | n/a | nc.prec = 3 |
---|
786 | n/a | self.assertRaises(InvalidOperation, nc.create_decimal, 'NaN12345') |
---|
787 | n/a | self.assertRaises(InvalidOperation, nc.create_decimal, |
---|
788 | n/a | Decimal('NaN12345')) |
---|
789 | n/a | |
---|
790 | n/a | nc.traps[InvalidOperation] = False |
---|
791 | n/a | self.assertEqual(str(nc.create_decimal('NaN12345')), 'NaN') |
---|
792 | n/a | self.assertTrue(nc.flags[InvalidOperation]) |
---|
793 | n/a | |
---|
794 | n/a | nc.flags[InvalidOperation] = False |
---|
795 | n/a | self.assertEqual(str(nc.create_decimal(Decimal('NaN12345'))), 'NaN') |
---|
796 | n/a | self.assertTrue(nc.flags[InvalidOperation]) |
---|
797 | n/a | |
---|
798 | n/a | def test_explicit_context_create_from_float(self): |
---|
799 | n/a | |
---|
800 | n/a | Decimal = self.decimal.Decimal |
---|
801 | n/a | |
---|
802 | n/a | nc = self.decimal.Context() |
---|
803 | n/a | r = nc.create_decimal(0.1) |
---|
804 | n/a | self.assertEqual(type(r), Decimal) |
---|
805 | n/a | self.assertEqual(str(r), '0.1000000000000000055511151231') |
---|
806 | n/a | self.assertTrue(nc.create_decimal(float('nan')).is_qnan()) |
---|
807 | n/a | self.assertTrue(nc.create_decimal(float('inf')).is_infinite()) |
---|
808 | n/a | self.assertTrue(nc.create_decimal(float('-inf')).is_infinite()) |
---|
809 | n/a | self.assertEqual(str(nc.create_decimal(float('nan'))), |
---|
810 | n/a | str(nc.create_decimal('NaN'))) |
---|
811 | n/a | self.assertEqual(str(nc.create_decimal(float('inf'))), |
---|
812 | n/a | str(nc.create_decimal('Infinity'))) |
---|
813 | n/a | self.assertEqual(str(nc.create_decimal(float('-inf'))), |
---|
814 | n/a | str(nc.create_decimal('-Infinity'))) |
---|
815 | n/a | self.assertEqual(str(nc.create_decimal(float('-0.0'))), |
---|
816 | n/a | str(nc.create_decimal('-0'))) |
---|
817 | n/a | nc.prec = 100 |
---|
818 | n/a | for i in range(200): |
---|
819 | n/a | x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) |
---|
820 | n/a | self.assertEqual(x, float(nc.create_decimal(x))) # roundtrip |
---|
821 | n/a | |
---|
822 | n/a | def test_unicode_digits(self): |
---|
823 | n/a | Decimal = self.decimal.Decimal |
---|
824 | n/a | |
---|
825 | n/a | test_values = { |
---|
826 | n/a | '\uff11': '1', |
---|
827 | n/a | '\u0660.\u0660\u0663\u0667\u0662e-\u0663' : '0.0000372', |
---|
828 | n/a | '-nan\u0c68\u0c6a\u0c66\u0c66' : '-NaN2400', |
---|
829 | n/a | } |
---|
830 | n/a | for input, expected in test_values.items(): |
---|
831 | n/a | self.assertEqual(str(Decimal(input)), expected) |
---|
832 | n/a | |
---|
833 | n/a | class CExplicitConstructionTest(ExplicitConstructionTest): |
---|
834 | n/a | decimal = C |
---|
835 | n/a | class PyExplicitConstructionTest(ExplicitConstructionTest): |
---|
836 | n/a | decimal = P |
---|
837 | n/a | |
---|
838 | n/a | class ImplicitConstructionTest(unittest.TestCase): |
---|
839 | n/a | '''Unit tests for Implicit Construction cases of Decimal.''' |
---|
840 | n/a | |
---|
841 | n/a | def test_implicit_from_None(self): |
---|
842 | n/a | Decimal = self.decimal.Decimal |
---|
843 | n/a | self.assertRaises(TypeError, eval, 'Decimal(5) + None', locals()) |
---|
844 | n/a | |
---|
845 | n/a | def test_implicit_from_int(self): |
---|
846 | n/a | Decimal = self.decimal.Decimal |
---|
847 | n/a | |
---|
848 | n/a | #normal |
---|
849 | n/a | self.assertEqual(str(Decimal(5) + 45), '50') |
---|
850 | n/a | #exceeding precision |
---|
851 | n/a | self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000)) |
---|
852 | n/a | |
---|
853 | n/a | def test_implicit_from_string(self): |
---|
854 | n/a | Decimal = self.decimal.Decimal |
---|
855 | n/a | self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', locals()) |
---|
856 | n/a | |
---|
857 | n/a | def test_implicit_from_float(self): |
---|
858 | n/a | Decimal = self.decimal.Decimal |
---|
859 | n/a | self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', locals()) |
---|
860 | n/a | |
---|
861 | n/a | def test_implicit_from_Decimal(self): |
---|
862 | n/a | Decimal = self.decimal.Decimal |
---|
863 | n/a | self.assertEqual(Decimal(5) + Decimal(45), Decimal(50)) |
---|
864 | n/a | |
---|
865 | n/a | def test_rop(self): |
---|
866 | n/a | Decimal = self.decimal.Decimal |
---|
867 | n/a | |
---|
868 | n/a | # Allow other classes to be trained to interact with Decimals |
---|
869 | n/a | class E: |
---|
870 | n/a | def __divmod__(self, other): |
---|
871 | n/a | return 'divmod ' + str(other) |
---|
872 | n/a | def __rdivmod__(self, other): |
---|
873 | n/a | return str(other) + ' rdivmod' |
---|
874 | n/a | def __lt__(self, other): |
---|
875 | n/a | return 'lt ' + str(other) |
---|
876 | n/a | def __gt__(self, other): |
---|
877 | n/a | return 'gt ' + str(other) |
---|
878 | n/a | def __le__(self, other): |
---|
879 | n/a | return 'le ' + str(other) |
---|
880 | n/a | def __ge__(self, other): |
---|
881 | n/a | return 'ge ' + str(other) |
---|
882 | n/a | def __eq__(self, other): |
---|
883 | n/a | return 'eq ' + str(other) |
---|
884 | n/a | def __ne__(self, other): |
---|
885 | n/a | return 'ne ' + str(other) |
---|
886 | n/a | |
---|
887 | n/a | self.assertEqual(divmod(E(), Decimal(10)), 'divmod 10') |
---|
888 | n/a | self.assertEqual(divmod(Decimal(10), E()), '10 rdivmod') |
---|
889 | n/a | self.assertEqual(eval('Decimal(10) < E()'), 'gt 10') |
---|
890 | n/a | self.assertEqual(eval('Decimal(10) > E()'), 'lt 10') |
---|
891 | n/a | self.assertEqual(eval('Decimal(10) <= E()'), 'ge 10') |
---|
892 | n/a | self.assertEqual(eval('Decimal(10) >= E()'), 'le 10') |
---|
893 | n/a | self.assertEqual(eval('Decimal(10) == E()'), 'eq 10') |
---|
894 | n/a | self.assertEqual(eval('Decimal(10) != E()'), 'ne 10') |
---|
895 | n/a | |
---|
896 | n/a | # insert operator methods and then exercise them |
---|
897 | n/a | oplist = [ |
---|
898 | n/a | ('+', '__add__', '__radd__'), |
---|
899 | n/a | ('-', '__sub__', '__rsub__'), |
---|
900 | n/a | ('*', '__mul__', '__rmul__'), |
---|
901 | n/a | ('/', '__truediv__', '__rtruediv__'), |
---|
902 | n/a | ('%', '__mod__', '__rmod__'), |
---|
903 | n/a | ('//', '__floordiv__', '__rfloordiv__'), |
---|
904 | n/a | ('**', '__pow__', '__rpow__') |
---|
905 | n/a | ] |
---|
906 | n/a | |
---|
907 | n/a | for sym, lop, rop in oplist: |
---|
908 | n/a | setattr(E, lop, lambda self, other: 'str' + lop + str(other)) |
---|
909 | n/a | setattr(E, rop, lambda self, other: str(other) + rop + 'str') |
---|
910 | n/a | self.assertEqual(eval('E()' + sym + 'Decimal(10)'), |
---|
911 | n/a | 'str' + lop + '10') |
---|
912 | n/a | self.assertEqual(eval('Decimal(10)' + sym + 'E()'), |
---|
913 | n/a | '10' + rop + 'str') |
---|
914 | n/a | |
---|
915 | n/a | class CImplicitConstructionTest(ImplicitConstructionTest): |
---|
916 | n/a | decimal = C |
---|
917 | n/a | class PyImplicitConstructionTest(ImplicitConstructionTest): |
---|
918 | n/a | decimal = P |
---|
919 | n/a | |
---|
920 | n/a | class FormatTest(unittest.TestCase): |
---|
921 | n/a | '''Unit tests for the format function.''' |
---|
922 | n/a | def test_formatting(self): |
---|
923 | n/a | Decimal = self.decimal.Decimal |
---|
924 | n/a | |
---|
925 | n/a | # triples giving a format, a Decimal, and the expected result |
---|
926 | n/a | test_values = [ |
---|
927 | n/a | ('e', '0E-15', '0e-15'), |
---|
928 | n/a | ('e', '2.3E-15', '2.3e-15'), |
---|
929 | n/a | ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros |
---|
930 | n/a | ('e', '2.30000E-15', '2.30000e-15'), |
---|
931 | n/a | ('e', '1.23456789123456789e40', '1.23456789123456789e+40'), |
---|
932 | n/a | ('e', '1.5', '1.5e+0'), |
---|
933 | n/a | ('e', '0.15', '1.5e-1'), |
---|
934 | n/a | ('e', '0.015', '1.5e-2'), |
---|
935 | n/a | ('e', '0.0000000000015', '1.5e-12'), |
---|
936 | n/a | ('e', '15.0', '1.50e+1'), |
---|
937 | n/a | ('e', '-15', '-1.5e+1'), |
---|
938 | n/a | ('e', '0', '0e+0'), |
---|
939 | n/a | ('e', '0E1', '0e+1'), |
---|
940 | n/a | ('e', '0.0', '0e-1'), |
---|
941 | n/a | ('e', '0.00', '0e-2'), |
---|
942 | n/a | ('.6e', '0E-15', '0.000000e-9'), |
---|
943 | n/a | ('.6e', '0', '0.000000e+6'), |
---|
944 | n/a | ('.6e', '9.999999', '9.999999e+0'), |
---|
945 | n/a | ('.6e', '9.9999999', '1.000000e+1'), |
---|
946 | n/a | ('.6e', '-1.23e5', '-1.230000e+5'), |
---|
947 | n/a | ('.6e', '1.23456789e-3', '1.234568e-3'), |
---|
948 | n/a | ('f', '0', '0'), |
---|
949 | n/a | ('f', '0.0', '0.0'), |
---|
950 | n/a | ('f', '0E-2', '0.00'), |
---|
951 | n/a | ('f', '0.00E-8', '0.0000000000'), |
---|
952 | n/a | ('f', '0E1', '0'), # loses exponent information |
---|
953 | n/a | ('f', '3.2E1', '32'), |
---|
954 | n/a | ('f', '3.2E2', '320'), |
---|
955 | n/a | ('f', '3.20E2', '320'), |
---|
956 | n/a | ('f', '3.200E2', '320.0'), |
---|
957 | n/a | ('f', '3.2E-6', '0.0000032'), |
---|
958 | n/a | ('.6f', '0E-15', '0.000000'), # all zeros treated equally |
---|
959 | n/a | ('.6f', '0E1', '0.000000'), |
---|
960 | n/a | ('.6f', '0', '0.000000'), |
---|
961 | n/a | ('.0f', '0', '0'), # no decimal point |
---|
962 | n/a | ('.0f', '0e-2', '0'), |
---|
963 | n/a | ('.0f', '3.14159265', '3'), |
---|
964 | n/a | ('.1f', '3.14159265', '3.1'), |
---|
965 | n/a | ('.4f', '3.14159265', '3.1416'), |
---|
966 | n/a | ('.6f', '3.14159265', '3.141593'), |
---|
967 | n/a | ('.7f', '3.14159265', '3.1415926'), # round-half-even! |
---|
968 | n/a | ('.8f', '3.14159265', '3.14159265'), |
---|
969 | n/a | ('.9f', '3.14159265', '3.141592650'), |
---|
970 | n/a | |
---|
971 | n/a | ('g', '0', '0'), |
---|
972 | n/a | ('g', '0.0', '0.0'), |
---|
973 | n/a | ('g', '0E1', '0e+1'), |
---|
974 | n/a | ('G', '0E1', '0E+1'), |
---|
975 | n/a | ('g', '0E-5', '0.00000'), |
---|
976 | n/a | ('g', '0E-6', '0.000000'), |
---|
977 | n/a | ('g', '0E-7', '0e-7'), |
---|
978 | n/a | ('g', '-0E2', '-0e+2'), |
---|
979 | n/a | ('.0g', '3.14159265', '3'), # 0 sig fig -> 1 sig fig |
---|
980 | n/a | ('.0n', '3.14159265', '3'), # same for 'n' |
---|
981 | n/a | ('.1g', '3.14159265', '3'), |
---|
982 | n/a | ('.2g', '3.14159265', '3.1'), |
---|
983 | n/a | ('.5g', '3.14159265', '3.1416'), |
---|
984 | n/a | ('.7g', '3.14159265', '3.141593'), |
---|
985 | n/a | ('.8g', '3.14159265', '3.1415926'), # round-half-even! |
---|
986 | n/a | ('.9g', '3.14159265', '3.14159265'), |
---|
987 | n/a | ('.10g', '3.14159265', '3.14159265'), # don't pad |
---|
988 | n/a | |
---|
989 | n/a | ('%', '0E1', '0%'), |
---|
990 | n/a | ('%', '0E0', '0%'), |
---|
991 | n/a | ('%', '0E-1', '0%'), |
---|
992 | n/a | ('%', '0E-2', '0%'), |
---|
993 | n/a | ('%', '0E-3', '0.0%'), |
---|
994 | n/a | ('%', '0E-4', '0.00%'), |
---|
995 | n/a | |
---|
996 | n/a | ('.3%', '0', '0.000%'), # all zeros treated equally |
---|
997 | n/a | ('.3%', '0E10', '0.000%'), |
---|
998 | n/a | ('.3%', '0E-10', '0.000%'), |
---|
999 | n/a | ('.3%', '2.34', '234.000%'), |
---|
1000 | n/a | ('.3%', '1.234567', '123.457%'), |
---|
1001 | n/a | ('.0%', '1.23', '123%'), |
---|
1002 | n/a | |
---|
1003 | n/a | ('e', 'NaN', 'NaN'), |
---|
1004 | n/a | ('f', '-NaN123', '-NaN123'), |
---|
1005 | n/a | ('+g', 'NaN456', '+NaN456'), |
---|
1006 | n/a | ('.3e', 'Inf', 'Infinity'), |
---|
1007 | n/a | ('.16f', '-Inf', '-Infinity'), |
---|
1008 | n/a | ('.0g', '-sNaN', '-sNaN'), |
---|
1009 | n/a | |
---|
1010 | n/a | ('', '1.00', '1.00'), |
---|
1011 | n/a | |
---|
1012 | n/a | # test alignment and padding |
---|
1013 | n/a | ('6', '123', ' 123'), |
---|
1014 | n/a | ('<6', '123', '123 '), |
---|
1015 | n/a | ('>6', '123', ' 123'), |
---|
1016 | n/a | ('^6', '123', ' 123 '), |
---|
1017 | n/a | ('=+6', '123', '+ 123'), |
---|
1018 | n/a | ('#<10', 'NaN', 'NaN#######'), |
---|
1019 | n/a | ('#<10', '-4.3', '-4.3######'), |
---|
1020 | n/a | ('#<+10', '0.0130', '+0.0130###'), |
---|
1021 | n/a | ('#< 10', '0.0130', ' 0.0130###'), |
---|
1022 | n/a | ('@>10', '-Inf', '@-Infinity'), |
---|
1023 | n/a | ('#>5', '-Inf', '-Infinity'), |
---|
1024 | n/a | ('?^5', '123', '?123?'), |
---|
1025 | n/a | ('%^6', '123', '%123%%'), |
---|
1026 | n/a | (' ^6', '-45.6', '-45.6 '), |
---|
1027 | n/a | ('/=10', '-45.6', '-/////45.6'), |
---|
1028 | n/a | ('/=+10', '45.6', '+/////45.6'), |
---|
1029 | n/a | ('/= 10', '45.6', ' /////45.6'), |
---|
1030 | n/a | ('\x00=10', '-inf', '-\x00Infinity'), |
---|
1031 | n/a | ('\x00^16', '-inf', '\x00\x00\x00-Infinity\x00\x00\x00\x00'), |
---|
1032 | n/a | ('\x00>10', '1.2345', '\x00\x00\x00\x001.2345'), |
---|
1033 | n/a | ('\x00<10', '1.2345', '1.2345\x00\x00\x00\x00'), |
---|
1034 | n/a | |
---|
1035 | n/a | # thousands separator |
---|
1036 | n/a | (',', '1234567', '1,234,567'), |
---|
1037 | n/a | (',', '123456', '123,456'), |
---|
1038 | n/a | (',', '12345', '12,345'), |
---|
1039 | n/a | (',', '1234', '1,234'), |
---|
1040 | n/a | (',', '123', '123'), |
---|
1041 | n/a | (',', '12', '12'), |
---|
1042 | n/a | (',', '1', '1'), |
---|
1043 | n/a | (',', '0', '0'), |
---|
1044 | n/a | (',', '-1234567', '-1,234,567'), |
---|
1045 | n/a | (',', '-123456', '-123,456'), |
---|
1046 | n/a | ('7,', '123456', '123,456'), |
---|
1047 | n/a | ('8,', '123456', ' 123,456'), |
---|
1048 | n/a | ('08,', '123456', '0,123,456'), # special case: extra 0 needed |
---|
1049 | n/a | ('+08,', '123456', '+123,456'), # but not if there's a sign |
---|
1050 | n/a | (' 08,', '123456', ' 123,456'), |
---|
1051 | n/a | ('08,', '-123456', '-123,456'), |
---|
1052 | n/a | ('+09,', '123456', '+0,123,456'), |
---|
1053 | n/a | # ... with fractional part... |
---|
1054 | n/a | ('07,', '1234.56', '1,234.56'), |
---|
1055 | n/a | ('08,', '1234.56', '1,234.56'), |
---|
1056 | n/a | ('09,', '1234.56', '01,234.56'), |
---|
1057 | n/a | ('010,', '1234.56', '001,234.56'), |
---|
1058 | n/a | ('011,', '1234.56', '0,001,234.56'), |
---|
1059 | n/a | ('012,', '1234.56', '0,001,234.56'), |
---|
1060 | n/a | ('08,.1f', '1234.5', '01,234.5'), |
---|
1061 | n/a | # no thousands separators in fraction part |
---|
1062 | n/a | (',', '1.23456789', '1.23456789'), |
---|
1063 | n/a | (',%', '123.456789', '12,345.6789%'), |
---|
1064 | n/a | (',e', '123456', '1.23456e+5'), |
---|
1065 | n/a | (',E', '123456', '1.23456E+5'), |
---|
1066 | n/a | |
---|
1067 | n/a | # issue 6850 |
---|
1068 | n/a | ('a=-7.0', '0.12345', 'aaaa0.1'), |
---|
1069 | n/a | |
---|
1070 | n/a | # issue 22090 |
---|
1071 | n/a | ('<^+15.20%', 'inf', '<<+Infinity%<<<'), |
---|
1072 | n/a | ('\x07>,%', 'sNaN1234567', 'sNaN1234567%'), |
---|
1073 | n/a | ('=10.10%', 'NaN123', ' NaN123%'), |
---|
1074 | n/a | ] |
---|
1075 | n/a | for fmt, d, result in test_values: |
---|
1076 | n/a | self.assertEqual(format(Decimal(d), fmt), result) |
---|
1077 | n/a | |
---|
1078 | n/a | # bytes format argument |
---|
1079 | n/a | self.assertRaises(TypeError, Decimal(1).__format__, b'-020') |
---|
1080 | n/a | |
---|
1081 | n/a | def test_n_format(self): |
---|
1082 | n/a | Decimal = self.decimal.Decimal |
---|
1083 | n/a | |
---|
1084 | n/a | try: |
---|
1085 | n/a | from locale import CHAR_MAX |
---|
1086 | n/a | except ImportError: |
---|
1087 | n/a | self.skipTest('locale.CHAR_MAX not available') |
---|
1088 | n/a | |
---|
1089 | n/a | def make_grouping(lst): |
---|
1090 | n/a | return ''.join([chr(x) for x in lst]) if self.decimal == C else lst |
---|
1091 | n/a | |
---|
1092 | n/a | def get_fmt(x, override=None, fmt='n'): |
---|
1093 | n/a | if self.decimal == C: |
---|
1094 | n/a | return Decimal(x).__format__(fmt, override) |
---|
1095 | n/a | else: |
---|
1096 | n/a | return Decimal(x).__format__(fmt, _localeconv=override) |
---|
1097 | n/a | |
---|
1098 | n/a | # Set up some localeconv-like dictionaries |
---|
1099 | n/a | en_US = { |
---|
1100 | n/a | 'decimal_point' : '.', |
---|
1101 | n/a | 'grouping' : make_grouping([3, 3, 0]), |
---|
1102 | n/a | 'thousands_sep' : ',' |
---|
1103 | n/a | } |
---|
1104 | n/a | |
---|
1105 | n/a | fr_FR = { |
---|
1106 | n/a | 'decimal_point' : ',', |
---|
1107 | n/a | 'grouping' : make_grouping([CHAR_MAX]), |
---|
1108 | n/a | 'thousands_sep' : '' |
---|
1109 | n/a | } |
---|
1110 | n/a | |
---|
1111 | n/a | ru_RU = { |
---|
1112 | n/a | 'decimal_point' : ',', |
---|
1113 | n/a | 'grouping': make_grouping([3, 3, 0]), |
---|
1114 | n/a | 'thousands_sep' : ' ' |
---|
1115 | n/a | } |
---|
1116 | n/a | |
---|
1117 | n/a | crazy = { |
---|
1118 | n/a | 'decimal_point' : '&', |
---|
1119 | n/a | 'grouping': make_grouping([1, 4, 2, CHAR_MAX]), |
---|
1120 | n/a | 'thousands_sep' : '-' |
---|
1121 | n/a | } |
---|
1122 | n/a | |
---|
1123 | n/a | dotsep_wide = { |
---|
1124 | n/a | 'decimal_point' : b'\xc2\xbf'.decode('utf-8'), |
---|
1125 | n/a | 'grouping': make_grouping([3, 3, 0]), |
---|
1126 | n/a | 'thousands_sep' : b'\xc2\xb4'.decode('utf-8') |
---|
1127 | n/a | } |
---|
1128 | n/a | |
---|
1129 | n/a | self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7') |
---|
1130 | n/a | self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7') |
---|
1131 | n/a | self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7') |
---|
1132 | n/a | self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7') |
---|
1133 | n/a | |
---|
1134 | n/a | self.assertEqual(get_fmt(123456789, en_US), '123,456,789') |
---|
1135 | n/a | self.assertEqual(get_fmt(123456789, fr_FR), '123456789') |
---|
1136 | n/a | self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789') |
---|
1137 | n/a | self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3') |
---|
1138 | n/a | |
---|
1139 | n/a | self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8') |
---|
1140 | n/a | self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8') |
---|
1141 | n/a | self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8') |
---|
1142 | n/a | self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8') |
---|
1143 | n/a | |
---|
1144 | n/a | # zero padding |
---|
1145 | n/a | self.assertEqual(get_fmt(1234, fr_FR, '03n'), '1234') |
---|
1146 | n/a | self.assertEqual(get_fmt(1234, fr_FR, '04n'), '1234') |
---|
1147 | n/a | self.assertEqual(get_fmt(1234, fr_FR, '05n'), '01234') |
---|
1148 | n/a | self.assertEqual(get_fmt(1234, fr_FR, '06n'), '001234') |
---|
1149 | n/a | |
---|
1150 | n/a | self.assertEqual(get_fmt(12345, en_US, '05n'), '12,345') |
---|
1151 | n/a | self.assertEqual(get_fmt(12345, en_US, '06n'), '12,345') |
---|
1152 | n/a | self.assertEqual(get_fmt(12345, en_US, '07n'), '012,345') |
---|
1153 | n/a | self.assertEqual(get_fmt(12345, en_US, '08n'), '0,012,345') |
---|
1154 | n/a | self.assertEqual(get_fmt(12345, en_US, '09n'), '0,012,345') |
---|
1155 | n/a | self.assertEqual(get_fmt(12345, en_US, '010n'), '00,012,345') |
---|
1156 | n/a | |
---|
1157 | n/a | self.assertEqual(get_fmt(123456, crazy, '06n'), '1-2345-6') |
---|
1158 | n/a | self.assertEqual(get_fmt(123456, crazy, '07n'), '1-2345-6') |
---|
1159 | n/a | self.assertEqual(get_fmt(123456, crazy, '08n'), '1-2345-6') |
---|
1160 | n/a | self.assertEqual(get_fmt(123456, crazy, '09n'), '01-2345-6') |
---|
1161 | n/a | self.assertEqual(get_fmt(123456, crazy, '010n'), '0-01-2345-6') |
---|
1162 | n/a | self.assertEqual(get_fmt(123456, crazy, '011n'), '0-01-2345-6') |
---|
1163 | n/a | self.assertEqual(get_fmt(123456, crazy, '012n'), '00-01-2345-6') |
---|
1164 | n/a | self.assertEqual(get_fmt(123456, crazy, '013n'), '000-01-2345-6') |
---|
1165 | n/a | |
---|
1166 | n/a | # wide char separator and decimal point |
---|
1167 | n/a | self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'), |
---|
1168 | n/a | '-0\u00b4000\u00b4000\u00b4000\u00b4001\u00bf5') |
---|
1169 | n/a | |
---|
1170 | n/a | @run_with_locale('LC_ALL', 'ps_AF') |
---|
1171 | n/a | def test_wide_char_separator_decimal_point(self): |
---|
1172 | n/a | # locale with wide char separator and decimal point |
---|
1173 | n/a | import locale |
---|
1174 | n/a | Decimal = self.decimal.Decimal |
---|
1175 | n/a | |
---|
1176 | n/a | decimal_point = locale.localeconv()['decimal_point'] |
---|
1177 | n/a | thousands_sep = locale.localeconv()['thousands_sep'] |
---|
1178 | n/a | if decimal_point != '\u066b': |
---|
1179 | n/a | self.skipTest('inappropriate decimal point separator' |
---|
1180 | n/a | '({!a} not {!a})'.format(decimal_point, '\u066b')) |
---|
1181 | n/a | if thousands_sep != '\u066c': |
---|
1182 | n/a | self.skipTest('inappropriate thousands separator' |
---|
1183 | n/a | '({!a} not {!a})'.format(thousands_sep, '\u066c')) |
---|
1184 | n/a | |
---|
1185 | n/a | self.assertEqual(format(Decimal('100000000.123'), 'n'), |
---|
1186 | n/a | '100\u066c000\u066c000\u066b123') |
---|
1187 | n/a | |
---|
1188 | n/a | class CFormatTest(FormatTest): |
---|
1189 | n/a | decimal = C |
---|
1190 | n/a | class PyFormatTest(FormatTest): |
---|
1191 | n/a | decimal = P |
---|
1192 | n/a | |
---|
1193 | n/a | class ArithmeticOperatorsTest(unittest.TestCase): |
---|
1194 | n/a | '''Unit tests for all arithmetic operators, binary and unary.''' |
---|
1195 | n/a | |
---|
1196 | n/a | def test_addition(self): |
---|
1197 | n/a | Decimal = self.decimal.Decimal |
---|
1198 | n/a | |
---|
1199 | n/a | d1 = Decimal('-11.1') |
---|
1200 | n/a | d2 = Decimal('22.2') |
---|
1201 | n/a | |
---|
1202 | n/a | #two Decimals |
---|
1203 | n/a | self.assertEqual(d1+d2, Decimal('11.1')) |
---|
1204 | n/a | self.assertEqual(d2+d1, Decimal('11.1')) |
---|
1205 | n/a | |
---|
1206 | n/a | #with other type, left |
---|
1207 | n/a | c = d1 + 5 |
---|
1208 | n/a | self.assertEqual(c, Decimal('-6.1')) |
---|
1209 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1210 | n/a | |
---|
1211 | n/a | #with other type, right |
---|
1212 | n/a | c = 5 + d1 |
---|
1213 | n/a | self.assertEqual(c, Decimal('-6.1')) |
---|
1214 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1215 | n/a | |
---|
1216 | n/a | #inline with decimal |
---|
1217 | n/a | d1 += d2 |
---|
1218 | n/a | self.assertEqual(d1, Decimal('11.1')) |
---|
1219 | n/a | |
---|
1220 | n/a | #inline with other type |
---|
1221 | n/a | d1 += 5 |
---|
1222 | n/a | self.assertEqual(d1, Decimal('16.1')) |
---|
1223 | n/a | |
---|
1224 | n/a | def test_subtraction(self): |
---|
1225 | n/a | Decimal = self.decimal.Decimal |
---|
1226 | n/a | |
---|
1227 | n/a | d1 = Decimal('-11.1') |
---|
1228 | n/a | d2 = Decimal('22.2') |
---|
1229 | n/a | |
---|
1230 | n/a | #two Decimals |
---|
1231 | n/a | self.assertEqual(d1-d2, Decimal('-33.3')) |
---|
1232 | n/a | self.assertEqual(d2-d1, Decimal('33.3')) |
---|
1233 | n/a | |
---|
1234 | n/a | #with other type, left |
---|
1235 | n/a | c = d1 - 5 |
---|
1236 | n/a | self.assertEqual(c, Decimal('-16.1')) |
---|
1237 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1238 | n/a | |
---|
1239 | n/a | #with other type, right |
---|
1240 | n/a | c = 5 - d1 |
---|
1241 | n/a | self.assertEqual(c, Decimal('16.1')) |
---|
1242 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1243 | n/a | |
---|
1244 | n/a | #inline with decimal |
---|
1245 | n/a | d1 -= d2 |
---|
1246 | n/a | self.assertEqual(d1, Decimal('-33.3')) |
---|
1247 | n/a | |
---|
1248 | n/a | #inline with other type |
---|
1249 | n/a | d1 -= 5 |
---|
1250 | n/a | self.assertEqual(d1, Decimal('-38.3')) |
---|
1251 | n/a | |
---|
1252 | n/a | def test_multiplication(self): |
---|
1253 | n/a | Decimal = self.decimal.Decimal |
---|
1254 | n/a | |
---|
1255 | n/a | d1 = Decimal('-5') |
---|
1256 | n/a | d2 = Decimal('3') |
---|
1257 | n/a | |
---|
1258 | n/a | #two Decimals |
---|
1259 | n/a | self.assertEqual(d1*d2, Decimal('-15')) |
---|
1260 | n/a | self.assertEqual(d2*d1, Decimal('-15')) |
---|
1261 | n/a | |
---|
1262 | n/a | #with other type, left |
---|
1263 | n/a | c = d1 * 5 |
---|
1264 | n/a | self.assertEqual(c, Decimal('-25')) |
---|
1265 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1266 | n/a | |
---|
1267 | n/a | #with other type, right |
---|
1268 | n/a | c = 5 * d1 |
---|
1269 | n/a | self.assertEqual(c, Decimal('-25')) |
---|
1270 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1271 | n/a | |
---|
1272 | n/a | #inline with decimal |
---|
1273 | n/a | d1 *= d2 |
---|
1274 | n/a | self.assertEqual(d1, Decimal('-15')) |
---|
1275 | n/a | |
---|
1276 | n/a | #inline with other type |
---|
1277 | n/a | d1 *= 5 |
---|
1278 | n/a | self.assertEqual(d1, Decimal('-75')) |
---|
1279 | n/a | |
---|
1280 | n/a | def test_division(self): |
---|
1281 | n/a | Decimal = self.decimal.Decimal |
---|
1282 | n/a | |
---|
1283 | n/a | d1 = Decimal('-5') |
---|
1284 | n/a | d2 = Decimal('2') |
---|
1285 | n/a | |
---|
1286 | n/a | #two Decimals |
---|
1287 | n/a | self.assertEqual(d1/d2, Decimal('-2.5')) |
---|
1288 | n/a | self.assertEqual(d2/d1, Decimal('-0.4')) |
---|
1289 | n/a | |
---|
1290 | n/a | #with other type, left |
---|
1291 | n/a | c = d1 / 4 |
---|
1292 | n/a | self.assertEqual(c, Decimal('-1.25')) |
---|
1293 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1294 | n/a | |
---|
1295 | n/a | #with other type, right |
---|
1296 | n/a | c = 4 / d1 |
---|
1297 | n/a | self.assertEqual(c, Decimal('-0.8')) |
---|
1298 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1299 | n/a | |
---|
1300 | n/a | #inline with decimal |
---|
1301 | n/a | d1 /= d2 |
---|
1302 | n/a | self.assertEqual(d1, Decimal('-2.5')) |
---|
1303 | n/a | |
---|
1304 | n/a | #inline with other type |
---|
1305 | n/a | d1 /= 4 |
---|
1306 | n/a | self.assertEqual(d1, Decimal('-0.625')) |
---|
1307 | n/a | |
---|
1308 | n/a | def test_floor_division(self): |
---|
1309 | n/a | Decimal = self.decimal.Decimal |
---|
1310 | n/a | |
---|
1311 | n/a | d1 = Decimal('5') |
---|
1312 | n/a | d2 = Decimal('2') |
---|
1313 | n/a | |
---|
1314 | n/a | #two Decimals |
---|
1315 | n/a | self.assertEqual(d1//d2, Decimal('2')) |
---|
1316 | n/a | self.assertEqual(d2//d1, Decimal('0')) |
---|
1317 | n/a | |
---|
1318 | n/a | #with other type, left |
---|
1319 | n/a | c = d1 // 4 |
---|
1320 | n/a | self.assertEqual(c, Decimal('1')) |
---|
1321 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1322 | n/a | |
---|
1323 | n/a | #with other type, right |
---|
1324 | n/a | c = 7 // d1 |
---|
1325 | n/a | self.assertEqual(c, Decimal('1')) |
---|
1326 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1327 | n/a | |
---|
1328 | n/a | #inline with decimal |
---|
1329 | n/a | d1 //= d2 |
---|
1330 | n/a | self.assertEqual(d1, Decimal('2')) |
---|
1331 | n/a | |
---|
1332 | n/a | #inline with other type |
---|
1333 | n/a | d1 //= 2 |
---|
1334 | n/a | self.assertEqual(d1, Decimal('1')) |
---|
1335 | n/a | |
---|
1336 | n/a | def test_powering(self): |
---|
1337 | n/a | Decimal = self.decimal.Decimal |
---|
1338 | n/a | |
---|
1339 | n/a | d1 = Decimal('5') |
---|
1340 | n/a | d2 = Decimal('2') |
---|
1341 | n/a | |
---|
1342 | n/a | #two Decimals |
---|
1343 | n/a | self.assertEqual(d1**d2, Decimal('25')) |
---|
1344 | n/a | self.assertEqual(d2**d1, Decimal('32')) |
---|
1345 | n/a | |
---|
1346 | n/a | #with other type, left |
---|
1347 | n/a | c = d1 ** 4 |
---|
1348 | n/a | self.assertEqual(c, Decimal('625')) |
---|
1349 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1350 | n/a | |
---|
1351 | n/a | #with other type, right |
---|
1352 | n/a | c = 7 ** d1 |
---|
1353 | n/a | self.assertEqual(c, Decimal('16807')) |
---|
1354 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1355 | n/a | |
---|
1356 | n/a | #inline with decimal |
---|
1357 | n/a | d1 **= d2 |
---|
1358 | n/a | self.assertEqual(d1, Decimal('25')) |
---|
1359 | n/a | |
---|
1360 | n/a | #inline with other type |
---|
1361 | n/a | d1 **= 4 |
---|
1362 | n/a | self.assertEqual(d1, Decimal('390625')) |
---|
1363 | n/a | |
---|
1364 | n/a | def test_module(self): |
---|
1365 | n/a | Decimal = self.decimal.Decimal |
---|
1366 | n/a | |
---|
1367 | n/a | d1 = Decimal('5') |
---|
1368 | n/a | d2 = Decimal('2') |
---|
1369 | n/a | |
---|
1370 | n/a | #two Decimals |
---|
1371 | n/a | self.assertEqual(d1%d2, Decimal('1')) |
---|
1372 | n/a | self.assertEqual(d2%d1, Decimal('2')) |
---|
1373 | n/a | |
---|
1374 | n/a | #with other type, left |
---|
1375 | n/a | c = d1 % 4 |
---|
1376 | n/a | self.assertEqual(c, Decimal('1')) |
---|
1377 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1378 | n/a | |
---|
1379 | n/a | #with other type, right |
---|
1380 | n/a | c = 7 % d1 |
---|
1381 | n/a | self.assertEqual(c, Decimal('2')) |
---|
1382 | n/a | self.assertEqual(type(c), type(d1)) |
---|
1383 | n/a | |
---|
1384 | n/a | #inline with decimal |
---|
1385 | n/a | d1 %= d2 |
---|
1386 | n/a | self.assertEqual(d1, Decimal('1')) |
---|
1387 | n/a | |
---|
1388 | n/a | #inline with other type |
---|
1389 | n/a | d1 %= 4 |
---|
1390 | n/a | self.assertEqual(d1, Decimal('1')) |
---|
1391 | n/a | |
---|
1392 | n/a | def test_floor_div_module(self): |
---|
1393 | n/a | Decimal = self.decimal.Decimal |
---|
1394 | n/a | |
---|
1395 | n/a | d1 = Decimal('5') |
---|
1396 | n/a | d2 = Decimal('2') |
---|
1397 | n/a | |
---|
1398 | n/a | #two Decimals |
---|
1399 | n/a | (p, q) = divmod(d1, d2) |
---|
1400 | n/a | self.assertEqual(p, Decimal('2')) |
---|
1401 | n/a | self.assertEqual(q, Decimal('1')) |
---|
1402 | n/a | self.assertEqual(type(p), type(d1)) |
---|
1403 | n/a | self.assertEqual(type(q), type(d1)) |
---|
1404 | n/a | |
---|
1405 | n/a | #with other type, left |
---|
1406 | n/a | (p, q) = divmod(d1, 4) |
---|
1407 | n/a | self.assertEqual(p, Decimal('1')) |
---|
1408 | n/a | self.assertEqual(q, Decimal('1')) |
---|
1409 | n/a | self.assertEqual(type(p), type(d1)) |
---|
1410 | n/a | self.assertEqual(type(q), type(d1)) |
---|
1411 | n/a | |
---|
1412 | n/a | #with other type, right |
---|
1413 | n/a | (p, q) = divmod(7, d1) |
---|
1414 | n/a | self.assertEqual(p, Decimal('1')) |
---|
1415 | n/a | self.assertEqual(q, Decimal('2')) |
---|
1416 | n/a | self.assertEqual(type(p), type(d1)) |
---|
1417 | n/a | self.assertEqual(type(q), type(d1)) |
---|
1418 | n/a | |
---|
1419 | n/a | def test_unary_operators(self): |
---|
1420 | n/a | Decimal = self.decimal.Decimal |
---|
1421 | n/a | |
---|
1422 | n/a | self.assertEqual(+Decimal(45), Decimal(+45)) # + |
---|
1423 | n/a | self.assertEqual(-Decimal(45), Decimal(-45)) # - |
---|
1424 | n/a | self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs |
---|
1425 | n/a | |
---|
1426 | n/a | def test_nan_comparisons(self): |
---|
1427 | n/a | # comparisons involving signaling nans signal InvalidOperation |
---|
1428 | n/a | |
---|
1429 | n/a | # order comparisons (<, <=, >, >=) involving only quiet nans |
---|
1430 | n/a | # also signal InvalidOperation |
---|
1431 | n/a | |
---|
1432 | n/a | # equality comparisons (==, !=) involving only quiet nans |
---|
1433 | n/a | # don't signal, but return False or True respectively. |
---|
1434 | n/a | Decimal = self.decimal.Decimal |
---|
1435 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
1436 | n/a | localcontext = self.decimal.localcontext |
---|
1437 | n/a | |
---|
1438 | n/a | n = Decimal('NaN') |
---|
1439 | n/a | s = Decimal('sNaN') |
---|
1440 | n/a | i = Decimal('Inf') |
---|
1441 | n/a | f = Decimal('2') |
---|
1442 | n/a | |
---|
1443 | n/a | qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n) |
---|
1444 | n/a | snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s) |
---|
1445 | n/a | order_ops = operator.lt, operator.le, operator.gt, operator.ge |
---|
1446 | n/a | equality_ops = operator.eq, operator.ne |
---|
1447 | n/a | |
---|
1448 | n/a | # results when InvalidOperation is not trapped |
---|
1449 | n/a | for x, y in qnan_pairs + snan_pairs: |
---|
1450 | n/a | for op in order_ops + equality_ops: |
---|
1451 | n/a | got = op(x, y) |
---|
1452 | n/a | expected = True if op is operator.ne else False |
---|
1453 | n/a | self.assertIs(expected, got, |
---|
1454 | n/a | "expected {0!r} for operator.{1}({2!r}, {3!r}); " |
---|
1455 | n/a | "got {4!r}".format( |
---|
1456 | n/a | expected, op.__name__, x, y, got)) |
---|
1457 | n/a | |
---|
1458 | n/a | # repeat the above, but this time trap the InvalidOperation |
---|
1459 | n/a | with localcontext() as ctx: |
---|
1460 | n/a | ctx.traps[InvalidOperation] = 1 |
---|
1461 | n/a | |
---|
1462 | n/a | for x, y in qnan_pairs: |
---|
1463 | n/a | for op in equality_ops: |
---|
1464 | n/a | got = op(x, y) |
---|
1465 | n/a | expected = True if op is operator.ne else False |
---|
1466 | n/a | self.assertIs(expected, got, |
---|
1467 | n/a | "expected {0!r} for " |
---|
1468 | n/a | "operator.{1}({2!r}, {3!r}); " |
---|
1469 | n/a | "got {4!r}".format( |
---|
1470 | n/a | expected, op.__name__, x, y, got)) |
---|
1471 | n/a | |
---|
1472 | n/a | for x, y in snan_pairs: |
---|
1473 | n/a | for op in equality_ops: |
---|
1474 | n/a | self.assertRaises(InvalidOperation, operator.eq, x, y) |
---|
1475 | n/a | self.assertRaises(InvalidOperation, operator.ne, x, y) |
---|
1476 | n/a | |
---|
1477 | n/a | for x, y in qnan_pairs + snan_pairs: |
---|
1478 | n/a | for op in order_ops: |
---|
1479 | n/a | self.assertRaises(InvalidOperation, op, x, y) |
---|
1480 | n/a | |
---|
1481 | n/a | def test_copy_sign(self): |
---|
1482 | n/a | Decimal = self.decimal.Decimal |
---|
1483 | n/a | |
---|
1484 | n/a | d = Decimal(1).copy_sign(Decimal(-2)) |
---|
1485 | n/a | self.assertEqual(Decimal(1).copy_sign(-2), d) |
---|
1486 | n/a | self.assertRaises(TypeError, Decimal(1).copy_sign, '-2') |
---|
1487 | n/a | |
---|
1488 | n/a | class CArithmeticOperatorsTest(ArithmeticOperatorsTest): |
---|
1489 | n/a | decimal = C |
---|
1490 | n/a | class PyArithmeticOperatorsTest(ArithmeticOperatorsTest): |
---|
1491 | n/a | decimal = P |
---|
1492 | n/a | |
---|
1493 | n/a | # The following are two functions used to test threading in the next class |
---|
1494 | n/a | |
---|
1495 | n/a | def thfunc1(cls): |
---|
1496 | n/a | Decimal = cls.decimal.Decimal |
---|
1497 | n/a | InvalidOperation = cls.decimal.InvalidOperation |
---|
1498 | n/a | DivisionByZero = cls.decimal.DivisionByZero |
---|
1499 | n/a | Overflow = cls.decimal.Overflow |
---|
1500 | n/a | Underflow = cls.decimal.Underflow |
---|
1501 | n/a | Inexact = cls.decimal.Inexact |
---|
1502 | n/a | getcontext = cls.decimal.getcontext |
---|
1503 | n/a | localcontext = cls.decimal.localcontext |
---|
1504 | n/a | |
---|
1505 | n/a | d1 = Decimal(1) |
---|
1506 | n/a | d3 = Decimal(3) |
---|
1507 | n/a | test1 = d1/d3 |
---|
1508 | n/a | |
---|
1509 | n/a | cls.finish1.set() |
---|
1510 | n/a | cls.synchro.wait() |
---|
1511 | n/a | |
---|
1512 | n/a | test2 = d1/d3 |
---|
1513 | n/a | with localcontext() as c2: |
---|
1514 | n/a | cls.assertTrue(c2.flags[Inexact]) |
---|
1515 | n/a | cls.assertRaises(DivisionByZero, c2.divide, d1, 0) |
---|
1516 | n/a | cls.assertTrue(c2.flags[DivisionByZero]) |
---|
1517 | n/a | with localcontext() as c3: |
---|
1518 | n/a | cls.assertTrue(c3.flags[Inexact]) |
---|
1519 | n/a | cls.assertTrue(c3.flags[DivisionByZero]) |
---|
1520 | n/a | cls.assertRaises(InvalidOperation, c3.compare, d1, Decimal('sNaN')) |
---|
1521 | n/a | cls.assertTrue(c3.flags[InvalidOperation]) |
---|
1522 | n/a | del c3 |
---|
1523 | n/a | cls.assertFalse(c2.flags[InvalidOperation]) |
---|
1524 | n/a | del c2 |
---|
1525 | n/a | |
---|
1526 | n/a | cls.assertEqual(test1, Decimal('0.333333333333333333333333')) |
---|
1527 | n/a | cls.assertEqual(test2, Decimal('0.333333333333333333333333')) |
---|
1528 | n/a | |
---|
1529 | n/a | c1 = getcontext() |
---|
1530 | n/a | cls.assertTrue(c1.flags[Inexact]) |
---|
1531 | n/a | for sig in Overflow, Underflow, DivisionByZero, InvalidOperation: |
---|
1532 | n/a | cls.assertFalse(c1.flags[sig]) |
---|
1533 | n/a | |
---|
1534 | n/a | def thfunc2(cls): |
---|
1535 | n/a | Decimal = cls.decimal.Decimal |
---|
1536 | n/a | InvalidOperation = cls.decimal.InvalidOperation |
---|
1537 | n/a | DivisionByZero = cls.decimal.DivisionByZero |
---|
1538 | n/a | Overflow = cls.decimal.Overflow |
---|
1539 | n/a | Underflow = cls.decimal.Underflow |
---|
1540 | n/a | Inexact = cls.decimal.Inexact |
---|
1541 | n/a | getcontext = cls.decimal.getcontext |
---|
1542 | n/a | localcontext = cls.decimal.localcontext |
---|
1543 | n/a | |
---|
1544 | n/a | d1 = Decimal(1) |
---|
1545 | n/a | d3 = Decimal(3) |
---|
1546 | n/a | test1 = d1/d3 |
---|
1547 | n/a | |
---|
1548 | n/a | thiscontext = getcontext() |
---|
1549 | n/a | thiscontext.prec = 18 |
---|
1550 | n/a | test2 = d1/d3 |
---|
1551 | n/a | |
---|
1552 | n/a | with localcontext() as c2: |
---|
1553 | n/a | cls.assertTrue(c2.flags[Inexact]) |
---|
1554 | n/a | cls.assertRaises(Overflow, c2.multiply, Decimal('1e425000000'), 999) |
---|
1555 | n/a | cls.assertTrue(c2.flags[Overflow]) |
---|
1556 | n/a | with localcontext(thiscontext) as c3: |
---|
1557 | n/a | cls.assertTrue(c3.flags[Inexact]) |
---|
1558 | n/a | cls.assertFalse(c3.flags[Overflow]) |
---|
1559 | n/a | c3.traps[Underflow] = True |
---|
1560 | n/a | cls.assertRaises(Underflow, c3.divide, Decimal('1e-425000000'), 999) |
---|
1561 | n/a | cls.assertTrue(c3.flags[Underflow]) |
---|
1562 | n/a | del c3 |
---|
1563 | n/a | cls.assertFalse(c2.flags[Underflow]) |
---|
1564 | n/a | cls.assertFalse(c2.traps[Underflow]) |
---|
1565 | n/a | del c2 |
---|
1566 | n/a | |
---|
1567 | n/a | cls.synchro.set() |
---|
1568 | n/a | cls.finish2.set() |
---|
1569 | n/a | |
---|
1570 | n/a | cls.assertEqual(test1, Decimal('0.333333333333333333333333')) |
---|
1571 | n/a | cls.assertEqual(test2, Decimal('0.333333333333333333')) |
---|
1572 | n/a | |
---|
1573 | n/a | cls.assertFalse(thiscontext.traps[Underflow]) |
---|
1574 | n/a | cls.assertTrue(thiscontext.flags[Inexact]) |
---|
1575 | n/a | for sig in Overflow, Underflow, DivisionByZero, InvalidOperation: |
---|
1576 | n/a | cls.assertFalse(thiscontext.flags[sig]) |
---|
1577 | n/a | |
---|
1578 | n/a | class ThreadingTest(unittest.TestCase): |
---|
1579 | n/a | '''Unit tests for thread local contexts in Decimal.''' |
---|
1580 | n/a | |
---|
1581 | n/a | # Take care executing this test from IDLE, there's an issue in threading |
---|
1582 | n/a | # that hangs IDLE and I couldn't find it |
---|
1583 | n/a | |
---|
1584 | n/a | def test_threading(self): |
---|
1585 | n/a | DefaultContext = self.decimal.DefaultContext |
---|
1586 | n/a | |
---|
1587 | n/a | if self.decimal == C and not self.decimal.HAVE_THREADS: |
---|
1588 | n/a | self.skipTest("compiled without threading") |
---|
1589 | n/a | # Test the "threading isolation" of a Context. Also test changing |
---|
1590 | n/a | # the DefaultContext, which acts as a template for the thread-local |
---|
1591 | n/a | # contexts. |
---|
1592 | n/a | save_prec = DefaultContext.prec |
---|
1593 | n/a | save_emax = DefaultContext.Emax |
---|
1594 | n/a | save_emin = DefaultContext.Emin |
---|
1595 | n/a | DefaultContext.prec = 24 |
---|
1596 | n/a | DefaultContext.Emax = 425000000 |
---|
1597 | n/a | DefaultContext.Emin = -425000000 |
---|
1598 | n/a | |
---|
1599 | n/a | self.synchro = threading.Event() |
---|
1600 | n/a | self.finish1 = threading.Event() |
---|
1601 | n/a | self.finish2 = threading.Event() |
---|
1602 | n/a | |
---|
1603 | n/a | th1 = threading.Thread(target=thfunc1, args=(self,)) |
---|
1604 | n/a | th2 = threading.Thread(target=thfunc2, args=(self,)) |
---|
1605 | n/a | |
---|
1606 | n/a | th1.start() |
---|
1607 | n/a | th2.start() |
---|
1608 | n/a | |
---|
1609 | n/a | self.finish1.wait() |
---|
1610 | n/a | self.finish2.wait() |
---|
1611 | n/a | |
---|
1612 | n/a | for sig in Signals[self.decimal]: |
---|
1613 | n/a | self.assertFalse(DefaultContext.flags[sig]) |
---|
1614 | n/a | |
---|
1615 | n/a | DefaultContext.prec = save_prec |
---|
1616 | n/a | DefaultContext.Emax = save_emax |
---|
1617 | n/a | DefaultContext.Emin = save_emin |
---|
1618 | n/a | |
---|
1619 | n/a | @unittest.skipUnless(threading, 'threading required') |
---|
1620 | n/a | class CThreadingTest(ThreadingTest): |
---|
1621 | n/a | decimal = C |
---|
1622 | n/a | @unittest.skipUnless(threading, 'threading required') |
---|
1623 | n/a | class PyThreadingTest(ThreadingTest): |
---|
1624 | n/a | decimal = P |
---|
1625 | n/a | |
---|
1626 | n/a | class UsabilityTest(unittest.TestCase): |
---|
1627 | n/a | '''Unit tests for Usability cases of Decimal.''' |
---|
1628 | n/a | |
---|
1629 | n/a | def test_comparison_operators(self): |
---|
1630 | n/a | |
---|
1631 | n/a | Decimal = self.decimal.Decimal |
---|
1632 | n/a | |
---|
1633 | n/a | da = Decimal('23.42') |
---|
1634 | n/a | db = Decimal('23.42') |
---|
1635 | n/a | dc = Decimal('45') |
---|
1636 | n/a | |
---|
1637 | n/a | #two Decimals |
---|
1638 | n/a | self.assertGreater(dc, da) |
---|
1639 | n/a | self.assertGreaterEqual(dc, da) |
---|
1640 | n/a | self.assertLess(da, dc) |
---|
1641 | n/a | self.assertLessEqual(da, dc) |
---|
1642 | n/a | self.assertEqual(da, db) |
---|
1643 | n/a | self.assertNotEqual(da, dc) |
---|
1644 | n/a | self.assertLessEqual(da, db) |
---|
1645 | n/a | self.assertGreaterEqual(da, db) |
---|
1646 | n/a | |
---|
1647 | n/a | #a Decimal and an int |
---|
1648 | n/a | self.assertGreater(dc, 23) |
---|
1649 | n/a | self.assertLess(23, dc) |
---|
1650 | n/a | self.assertEqual(dc, 45) |
---|
1651 | n/a | |
---|
1652 | n/a | #a Decimal and uncomparable |
---|
1653 | n/a | self.assertNotEqual(da, 'ugly') |
---|
1654 | n/a | self.assertNotEqual(da, 32.7) |
---|
1655 | n/a | self.assertNotEqual(da, object()) |
---|
1656 | n/a | self.assertNotEqual(da, object) |
---|
1657 | n/a | |
---|
1658 | n/a | # sortable |
---|
1659 | n/a | a = list(map(Decimal, range(100))) |
---|
1660 | n/a | b = a[:] |
---|
1661 | n/a | random.shuffle(a) |
---|
1662 | n/a | a.sort() |
---|
1663 | n/a | self.assertEqual(a, b) |
---|
1664 | n/a | |
---|
1665 | n/a | def test_decimal_float_comparison(self): |
---|
1666 | n/a | Decimal = self.decimal.Decimal |
---|
1667 | n/a | |
---|
1668 | n/a | da = Decimal('0.25') |
---|
1669 | n/a | db = Decimal('3.0') |
---|
1670 | n/a | self.assertLess(da, 3.0) |
---|
1671 | n/a | self.assertLessEqual(da, 3.0) |
---|
1672 | n/a | self.assertGreater(db, 0.25) |
---|
1673 | n/a | self.assertGreaterEqual(db, 0.25) |
---|
1674 | n/a | self.assertNotEqual(da, 1.5) |
---|
1675 | n/a | self.assertEqual(da, 0.25) |
---|
1676 | n/a | self.assertGreater(3.0, da) |
---|
1677 | n/a | self.assertGreaterEqual(3.0, da) |
---|
1678 | n/a | self.assertLess(0.25, db) |
---|
1679 | n/a | self.assertLessEqual(0.25, db) |
---|
1680 | n/a | self.assertNotEqual(0.25, db) |
---|
1681 | n/a | self.assertEqual(3.0, db) |
---|
1682 | n/a | self.assertNotEqual(0.1, Decimal('0.1')) |
---|
1683 | n/a | |
---|
1684 | n/a | def test_decimal_complex_comparison(self): |
---|
1685 | n/a | Decimal = self.decimal.Decimal |
---|
1686 | n/a | |
---|
1687 | n/a | da = Decimal('0.25') |
---|
1688 | n/a | db = Decimal('3.0') |
---|
1689 | n/a | self.assertNotEqual(da, (1.5+0j)) |
---|
1690 | n/a | self.assertNotEqual((1.5+0j), da) |
---|
1691 | n/a | self.assertEqual(da, (0.25+0j)) |
---|
1692 | n/a | self.assertEqual((0.25+0j), da) |
---|
1693 | n/a | self.assertEqual((3.0+0j), db) |
---|
1694 | n/a | self.assertEqual(db, (3.0+0j)) |
---|
1695 | n/a | |
---|
1696 | n/a | self.assertNotEqual(db, (3.0+1j)) |
---|
1697 | n/a | self.assertNotEqual((3.0+1j), db) |
---|
1698 | n/a | |
---|
1699 | n/a | self.assertIs(db.__lt__(3.0+0j), NotImplemented) |
---|
1700 | n/a | self.assertIs(db.__le__(3.0+0j), NotImplemented) |
---|
1701 | n/a | self.assertIs(db.__gt__(3.0+0j), NotImplemented) |
---|
1702 | n/a | self.assertIs(db.__le__(3.0+0j), NotImplemented) |
---|
1703 | n/a | |
---|
1704 | n/a | def test_decimal_fraction_comparison(self): |
---|
1705 | n/a | D = self.decimal.Decimal |
---|
1706 | n/a | F = fractions[self.decimal].Fraction |
---|
1707 | n/a | Context = self.decimal.Context |
---|
1708 | n/a | localcontext = self.decimal.localcontext |
---|
1709 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
1710 | n/a | |
---|
1711 | n/a | |
---|
1712 | n/a | emax = C.MAX_EMAX if C else 999999999 |
---|
1713 | n/a | emin = C.MIN_EMIN if C else -999999999 |
---|
1714 | n/a | etiny = C.MIN_ETINY if C else -1999999997 |
---|
1715 | n/a | c = Context(Emax=emax, Emin=emin) |
---|
1716 | n/a | |
---|
1717 | n/a | with localcontext(c): |
---|
1718 | n/a | c.prec = emax |
---|
1719 | n/a | self.assertLess(D(0), F(1,9999999999999999999999999999999999999)) |
---|
1720 | n/a | self.assertLess(F(-1,9999999999999999999999999999999999999), D(0)) |
---|
1721 | n/a | self.assertLess(F(0,1), D("1e" + str(etiny))) |
---|
1722 | n/a | self.assertLess(D("-1e" + str(etiny)), F(0,1)) |
---|
1723 | n/a | self.assertLess(F(0,9999999999999999999999999), D("1e" + str(etiny))) |
---|
1724 | n/a | self.assertLess(D("-1e" + str(etiny)), F(0,9999999999999999999999999)) |
---|
1725 | n/a | |
---|
1726 | n/a | self.assertEqual(D("0.1"), F(1,10)) |
---|
1727 | n/a | self.assertEqual(F(1,10), D("0.1")) |
---|
1728 | n/a | |
---|
1729 | n/a | c.prec = 300 |
---|
1730 | n/a | self.assertNotEqual(D(1)/3, F(1,3)) |
---|
1731 | n/a | self.assertNotEqual(F(1,3), D(1)/3) |
---|
1732 | n/a | |
---|
1733 | n/a | self.assertLessEqual(F(120984237, 9999999999), D("9e" + str(emax))) |
---|
1734 | n/a | self.assertGreaterEqual(D("9e" + str(emax)), F(120984237, 9999999999)) |
---|
1735 | n/a | |
---|
1736 | n/a | self.assertGreater(D('inf'), F(99999999999,123)) |
---|
1737 | n/a | self.assertGreater(D('inf'), F(-99999999999,123)) |
---|
1738 | n/a | self.assertLess(D('-inf'), F(99999999999,123)) |
---|
1739 | n/a | self.assertLess(D('-inf'), F(-99999999999,123)) |
---|
1740 | n/a | |
---|
1741 | n/a | self.assertRaises(InvalidOperation, D('nan').__gt__, F(-9,123)) |
---|
1742 | n/a | self.assertIs(NotImplemented, F(-9,123).__lt__(D('nan'))) |
---|
1743 | n/a | self.assertNotEqual(D('nan'), F(-9,123)) |
---|
1744 | n/a | self.assertNotEqual(F(-9,123), D('nan')) |
---|
1745 | n/a | |
---|
1746 | n/a | def test_copy_and_deepcopy_methods(self): |
---|
1747 | n/a | Decimal = self.decimal.Decimal |
---|
1748 | n/a | |
---|
1749 | n/a | d = Decimal('43.24') |
---|
1750 | n/a | c = copy.copy(d) |
---|
1751 | n/a | self.assertEqual(id(c), id(d)) |
---|
1752 | n/a | dc = copy.deepcopy(d) |
---|
1753 | n/a | self.assertEqual(id(dc), id(d)) |
---|
1754 | n/a | |
---|
1755 | n/a | def test_hash_method(self): |
---|
1756 | n/a | |
---|
1757 | n/a | Decimal = self.decimal.Decimal |
---|
1758 | n/a | localcontext = self.decimal.localcontext |
---|
1759 | n/a | |
---|
1760 | n/a | def hashit(d): |
---|
1761 | n/a | a = hash(d) |
---|
1762 | n/a | b = d.__hash__() |
---|
1763 | n/a | self.assertEqual(a, b) |
---|
1764 | n/a | return a |
---|
1765 | n/a | |
---|
1766 | n/a | #just that it's hashable |
---|
1767 | n/a | hashit(Decimal(23)) |
---|
1768 | n/a | hashit(Decimal('Infinity')) |
---|
1769 | n/a | hashit(Decimal('-Infinity')) |
---|
1770 | n/a | hashit(Decimal('nan123')) |
---|
1771 | n/a | hashit(Decimal('-NaN')) |
---|
1772 | n/a | |
---|
1773 | n/a | test_values = [Decimal(sign*(2**m + n)) |
---|
1774 | n/a | for m in [0, 14, 15, 16, 17, 30, 31, |
---|
1775 | n/a | 32, 33, 61, 62, 63, 64, 65, 66] |
---|
1776 | n/a | for n in range(-10, 10) |
---|
1777 | n/a | for sign in [-1, 1]] |
---|
1778 | n/a | test_values.extend([ |
---|
1779 | n/a | Decimal("-1"), # ==> -2 |
---|
1780 | n/a | Decimal("-0"), # zeros |
---|
1781 | n/a | Decimal("0.00"), |
---|
1782 | n/a | Decimal("-0.000"), |
---|
1783 | n/a | Decimal("0E10"), |
---|
1784 | n/a | Decimal("-0E12"), |
---|
1785 | n/a | Decimal("10.0"), # negative exponent |
---|
1786 | n/a | Decimal("-23.00000"), |
---|
1787 | n/a | Decimal("1230E100"), # positive exponent |
---|
1788 | n/a | Decimal("-4.5678E50"), |
---|
1789 | n/a | # a value for which hash(n) != hash(n % (2**64-1)) |
---|
1790 | n/a | # in Python pre-2.6 |
---|
1791 | n/a | Decimal(2**64 + 2**32 - 1), |
---|
1792 | n/a | # selection of values which fail with the old (before |
---|
1793 | n/a | # version 2.6) long.__hash__ |
---|
1794 | n/a | Decimal("1.634E100"), |
---|
1795 | n/a | Decimal("90.697E100"), |
---|
1796 | n/a | Decimal("188.83E100"), |
---|
1797 | n/a | Decimal("1652.9E100"), |
---|
1798 | n/a | Decimal("56531E100"), |
---|
1799 | n/a | ]) |
---|
1800 | n/a | |
---|
1801 | n/a | # check that hash(d) == hash(int(d)) for integral values |
---|
1802 | n/a | for value in test_values: |
---|
1803 | n/a | self.assertEqual(hashit(value), hashit(int(value))) |
---|
1804 | n/a | |
---|
1805 | n/a | #the same hash that to an int |
---|
1806 | n/a | self.assertEqual(hashit(Decimal(23)), hashit(23)) |
---|
1807 | n/a | self.assertRaises(TypeError, hash, Decimal('sNaN')) |
---|
1808 | n/a | self.assertTrue(hashit(Decimal('Inf'))) |
---|
1809 | n/a | self.assertTrue(hashit(Decimal('-Inf'))) |
---|
1810 | n/a | |
---|
1811 | n/a | # check that the hashes of a Decimal float match when they |
---|
1812 | n/a | # represent exactly the same values |
---|
1813 | n/a | test_strings = ['inf', '-Inf', '0.0', '-.0e1', |
---|
1814 | n/a | '34.0', '2.5', '112390.625', '-0.515625'] |
---|
1815 | n/a | for s in test_strings: |
---|
1816 | n/a | f = float(s) |
---|
1817 | n/a | d = Decimal(s) |
---|
1818 | n/a | self.assertEqual(hashit(f), hashit(d)) |
---|
1819 | n/a | |
---|
1820 | n/a | with localcontext() as c: |
---|
1821 | n/a | # check that the value of the hash doesn't depend on the |
---|
1822 | n/a | # current context (issue #1757) |
---|
1823 | n/a | x = Decimal("123456789.1") |
---|
1824 | n/a | |
---|
1825 | n/a | c.prec = 6 |
---|
1826 | n/a | h1 = hashit(x) |
---|
1827 | n/a | c.prec = 10 |
---|
1828 | n/a | h2 = hashit(x) |
---|
1829 | n/a | c.prec = 16 |
---|
1830 | n/a | h3 = hashit(x) |
---|
1831 | n/a | |
---|
1832 | n/a | self.assertEqual(h1, h2) |
---|
1833 | n/a | self.assertEqual(h1, h3) |
---|
1834 | n/a | |
---|
1835 | n/a | c.prec = 10000 |
---|
1836 | n/a | x = 1100 ** 1248 |
---|
1837 | n/a | self.assertEqual(hashit(Decimal(x)), hashit(x)) |
---|
1838 | n/a | |
---|
1839 | n/a | def test_min_and_max_methods(self): |
---|
1840 | n/a | Decimal = self.decimal.Decimal |
---|
1841 | n/a | |
---|
1842 | n/a | d1 = Decimal('15.32') |
---|
1843 | n/a | d2 = Decimal('28.5') |
---|
1844 | n/a | l1 = 15 |
---|
1845 | n/a | l2 = 28 |
---|
1846 | n/a | |
---|
1847 | n/a | #between Decimals |
---|
1848 | n/a | self.assertIs(min(d1,d2), d1) |
---|
1849 | n/a | self.assertIs(min(d2,d1), d1) |
---|
1850 | n/a | self.assertIs(max(d1,d2), d2) |
---|
1851 | n/a | self.assertIs(max(d2,d1), d2) |
---|
1852 | n/a | |
---|
1853 | n/a | #between Decimal and int |
---|
1854 | n/a | self.assertIs(min(d1,l2), d1) |
---|
1855 | n/a | self.assertIs(min(l2,d1), d1) |
---|
1856 | n/a | self.assertIs(max(l1,d2), d2) |
---|
1857 | n/a | self.assertIs(max(d2,l1), d2) |
---|
1858 | n/a | |
---|
1859 | n/a | def test_as_nonzero(self): |
---|
1860 | n/a | Decimal = self.decimal.Decimal |
---|
1861 | n/a | |
---|
1862 | n/a | #as false |
---|
1863 | n/a | self.assertFalse(Decimal(0)) |
---|
1864 | n/a | #as true |
---|
1865 | n/a | self.assertTrue(Decimal('0.372')) |
---|
1866 | n/a | |
---|
1867 | n/a | def test_tostring_methods(self): |
---|
1868 | n/a | #Test str and repr methods. |
---|
1869 | n/a | Decimal = self.decimal.Decimal |
---|
1870 | n/a | |
---|
1871 | n/a | d = Decimal('15.32') |
---|
1872 | n/a | self.assertEqual(str(d), '15.32') # str |
---|
1873 | n/a | self.assertEqual(repr(d), "Decimal('15.32')") # repr |
---|
1874 | n/a | |
---|
1875 | n/a | def test_tonum_methods(self): |
---|
1876 | n/a | #Test float and int methods. |
---|
1877 | n/a | Decimal = self.decimal.Decimal |
---|
1878 | n/a | |
---|
1879 | n/a | d1 = Decimal('66') |
---|
1880 | n/a | d2 = Decimal('15.32') |
---|
1881 | n/a | |
---|
1882 | n/a | #int |
---|
1883 | n/a | self.assertEqual(int(d1), 66) |
---|
1884 | n/a | self.assertEqual(int(d2), 15) |
---|
1885 | n/a | |
---|
1886 | n/a | #float |
---|
1887 | n/a | self.assertEqual(float(d1), 66) |
---|
1888 | n/a | self.assertEqual(float(d2), 15.32) |
---|
1889 | n/a | |
---|
1890 | n/a | #floor |
---|
1891 | n/a | test_pairs = [ |
---|
1892 | n/a | ('123.00', 123), |
---|
1893 | n/a | ('3.2', 3), |
---|
1894 | n/a | ('3.54', 3), |
---|
1895 | n/a | ('3.899', 3), |
---|
1896 | n/a | ('-2.3', -3), |
---|
1897 | n/a | ('-11.0', -11), |
---|
1898 | n/a | ('0.0', 0), |
---|
1899 | n/a | ('-0E3', 0), |
---|
1900 | n/a | ('89891211712379812736.1', 89891211712379812736), |
---|
1901 | n/a | ] |
---|
1902 | n/a | for d, i in test_pairs: |
---|
1903 | n/a | self.assertEqual(math.floor(Decimal(d)), i) |
---|
1904 | n/a | self.assertRaises(ValueError, math.floor, Decimal('-NaN')) |
---|
1905 | n/a | self.assertRaises(ValueError, math.floor, Decimal('sNaN')) |
---|
1906 | n/a | self.assertRaises(ValueError, math.floor, Decimal('NaN123')) |
---|
1907 | n/a | self.assertRaises(OverflowError, math.floor, Decimal('Inf')) |
---|
1908 | n/a | self.assertRaises(OverflowError, math.floor, Decimal('-Inf')) |
---|
1909 | n/a | |
---|
1910 | n/a | #ceiling |
---|
1911 | n/a | test_pairs = [ |
---|
1912 | n/a | ('123.00', 123), |
---|
1913 | n/a | ('3.2', 4), |
---|
1914 | n/a | ('3.54', 4), |
---|
1915 | n/a | ('3.899', 4), |
---|
1916 | n/a | ('-2.3', -2), |
---|
1917 | n/a | ('-11.0', -11), |
---|
1918 | n/a | ('0.0', 0), |
---|
1919 | n/a | ('-0E3', 0), |
---|
1920 | n/a | ('89891211712379812736.1', 89891211712379812737), |
---|
1921 | n/a | ] |
---|
1922 | n/a | for d, i in test_pairs: |
---|
1923 | n/a | self.assertEqual(math.ceil(Decimal(d)), i) |
---|
1924 | n/a | self.assertRaises(ValueError, math.ceil, Decimal('-NaN')) |
---|
1925 | n/a | self.assertRaises(ValueError, math.ceil, Decimal('sNaN')) |
---|
1926 | n/a | self.assertRaises(ValueError, math.ceil, Decimal('NaN123')) |
---|
1927 | n/a | self.assertRaises(OverflowError, math.ceil, Decimal('Inf')) |
---|
1928 | n/a | self.assertRaises(OverflowError, math.ceil, Decimal('-Inf')) |
---|
1929 | n/a | |
---|
1930 | n/a | #round, single argument |
---|
1931 | n/a | test_pairs = [ |
---|
1932 | n/a | ('123.00', 123), |
---|
1933 | n/a | ('3.2', 3), |
---|
1934 | n/a | ('3.54', 4), |
---|
1935 | n/a | ('3.899', 4), |
---|
1936 | n/a | ('-2.3', -2), |
---|
1937 | n/a | ('-11.0', -11), |
---|
1938 | n/a | ('0.0', 0), |
---|
1939 | n/a | ('-0E3', 0), |
---|
1940 | n/a | ('-3.5', -4), |
---|
1941 | n/a | ('-2.5', -2), |
---|
1942 | n/a | ('-1.5', -2), |
---|
1943 | n/a | ('-0.5', 0), |
---|
1944 | n/a | ('0.5', 0), |
---|
1945 | n/a | ('1.5', 2), |
---|
1946 | n/a | ('2.5', 2), |
---|
1947 | n/a | ('3.5', 4), |
---|
1948 | n/a | ] |
---|
1949 | n/a | for d, i in test_pairs: |
---|
1950 | n/a | self.assertEqual(round(Decimal(d)), i) |
---|
1951 | n/a | self.assertRaises(ValueError, round, Decimal('-NaN')) |
---|
1952 | n/a | self.assertRaises(ValueError, round, Decimal('sNaN')) |
---|
1953 | n/a | self.assertRaises(ValueError, round, Decimal('NaN123')) |
---|
1954 | n/a | self.assertRaises(OverflowError, round, Decimal('Inf')) |
---|
1955 | n/a | self.assertRaises(OverflowError, round, Decimal('-Inf')) |
---|
1956 | n/a | |
---|
1957 | n/a | #round, two arguments; this is essentially equivalent |
---|
1958 | n/a | #to quantize, which is already extensively tested |
---|
1959 | n/a | test_triples = [ |
---|
1960 | n/a | ('123.456', -4, '0E+4'), |
---|
1961 | n/a | ('123.456', -3, '0E+3'), |
---|
1962 | n/a | ('123.456', -2, '1E+2'), |
---|
1963 | n/a | ('123.456', -1, '1.2E+2'), |
---|
1964 | n/a | ('123.456', 0, '123'), |
---|
1965 | n/a | ('123.456', 1, '123.5'), |
---|
1966 | n/a | ('123.456', 2, '123.46'), |
---|
1967 | n/a | ('123.456', 3, '123.456'), |
---|
1968 | n/a | ('123.456', 4, '123.4560'), |
---|
1969 | n/a | ('123.455', 2, '123.46'), |
---|
1970 | n/a | ('123.445', 2, '123.44'), |
---|
1971 | n/a | ('Inf', 4, 'NaN'), |
---|
1972 | n/a | ('-Inf', -23, 'NaN'), |
---|
1973 | n/a | ('sNaN314', 3, 'NaN314'), |
---|
1974 | n/a | ] |
---|
1975 | n/a | for d, n, r in test_triples: |
---|
1976 | n/a | self.assertEqual(str(round(Decimal(d), n)), r) |
---|
1977 | n/a | |
---|
1978 | n/a | def test_nan_to_float(self): |
---|
1979 | n/a | # Test conversions of decimal NANs to float. |
---|
1980 | n/a | # See http://bugs.python.org/issue15544 |
---|
1981 | n/a | Decimal = self.decimal.Decimal |
---|
1982 | n/a | for s in ('nan', 'nan1234', '-nan', '-nan2468'): |
---|
1983 | n/a | f = float(Decimal(s)) |
---|
1984 | n/a | self.assertTrue(math.isnan(f)) |
---|
1985 | n/a | sign = math.copysign(1.0, f) |
---|
1986 | n/a | self.assertEqual(sign, -1.0 if s.startswith('-') else 1.0) |
---|
1987 | n/a | |
---|
1988 | n/a | def test_snan_to_float(self): |
---|
1989 | n/a | Decimal = self.decimal.Decimal |
---|
1990 | n/a | for s in ('snan', '-snan', 'snan1357', '-snan1234'): |
---|
1991 | n/a | d = Decimal(s) |
---|
1992 | n/a | self.assertRaises(ValueError, float, d) |
---|
1993 | n/a | |
---|
1994 | n/a | def test_eval_round_trip(self): |
---|
1995 | n/a | Decimal = self.decimal.Decimal |
---|
1996 | n/a | |
---|
1997 | n/a | #with zero |
---|
1998 | n/a | d = Decimal( (0, (0,), 0) ) |
---|
1999 | n/a | self.assertEqual(d, eval(repr(d))) |
---|
2000 | n/a | |
---|
2001 | n/a | #int |
---|
2002 | n/a | d = Decimal( (1, (4, 5), 0) ) |
---|
2003 | n/a | self.assertEqual(d, eval(repr(d))) |
---|
2004 | n/a | |
---|
2005 | n/a | #float |
---|
2006 | n/a | d = Decimal( (0, (4, 5, 3, 4), -2) ) |
---|
2007 | n/a | self.assertEqual(d, eval(repr(d))) |
---|
2008 | n/a | |
---|
2009 | n/a | #weird |
---|
2010 | n/a | d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) |
---|
2011 | n/a | self.assertEqual(d, eval(repr(d))) |
---|
2012 | n/a | |
---|
2013 | n/a | def test_as_tuple(self): |
---|
2014 | n/a | Decimal = self.decimal.Decimal |
---|
2015 | n/a | |
---|
2016 | n/a | #with zero |
---|
2017 | n/a | d = Decimal(0) |
---|
2018 | n/a | self.assertEqual(d.as_tuple(), (0, (0,), 0) ) |
---|
2019 | n/a | |
---|
2020 | n/a | #int |
---|
2021 | n/a | d = Decimal(-45) |
---|
2022 | n/a | self.assertEqual(d.as_tuple(), (1, (4, 5), 0) ) |
---|
2023 | n/a | |
---|
2024 | n/a | #complicated string |
---|
2025 | n/a | d = Decimal("-4.34913534E-17") |
---|
2026 | n/a | self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) |
---|
2027 | n/a | |
---|
2028 | n/a | # The '0' coefficient is implementation specific to decimal.py. |
---|
2029 | n/a | # It has no meaning in the C-version and is ignored there. |
---|
2030 | n/a | d = Decimal("Infinity") |
---|
2031 | n/a | self.assertEqual(d.as_tuple(), (0, (0,), 'F') ) |
---|
2032 | n/a | |
---|
2033 | n/a | #leading zeros in coefficient should be stripped |
---|
2034 | n/a | d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) ) |
---|
2035 | n/a | self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) ) |
---|
2036 | n/a | d = Decimal( (1, (0, 0, 0), 37) ) |
---|
2037 | n/a | self.assertEqual(d.as_tuple(), (1, (0,), 37)) |
---|
2038 | n/a | d = Decimal( (1, (), 37) ) |
---|
2039 | n/a | self.assertEqual(d.as_tuple(), (1, (0,), 37)) |
---|
2040 | n/a | |
---|
2041 | n/a | #leading zeros in NaN diagnostic info should be stripped |
---|
2042 | n/a | d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') ) |
---|
2043 | n/a | self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') ) |
---|
2044 | n/a | d = Decimal( (1, (0, 0, 0), 'N') ) |
---|
2045 | n/a | self.assertEqual(d.as_tuple(), (1, (), 'N') ) |
---|
2046 | n/a | d = Decimal( (1, (), 'n') ) |
---|
2047 | n/a | self.assertEqual(d.as_tuple(), (1, (), 'n') ) |
---|
2048 | n/a | |
---|
2049 | n/a | # For infinities, decimal.py has always silently accepted any |
---|
2050 | n/a | # coefficient tuple. |
---|
2051 | n/a | d = Decimal( (0, (0,), 'F') ) |
---|
2052 | n/a | self.assertEqual(d.as_tuple(), (0, (0,), 'F')) |
---|
2053 | n/a | d = Decimal( (0, (4, 5, 3, 4), 'F') ) |
---|
2054 | n/a | self.assertEqual(d.as_tuple(), (0, (0,), 'F')) |
---|
2055 | n/a | d = Decimal( (1, (0, 2, 7, 1), 'F') ) |
---|
2056 | n/a | self.assertEqual(d.as_tuple(), (1, (0,), 'F')) |
---|
2057 | n/a | |
---|
2058 | n/a | def test_as_integer_ratio(self): |
---|
2059 | n/a | Decimal = self.decimal.Decimal |
---|
2060 | n/a | |
---|
2061 | n/a | # exceptional cases |
---|
2062 | n/a | self.assertRaises(OverflowError, |
---|
2063 | n/a | Decimal.as_integer_ratio, Decimal('inf')) |
---|
2064 | n/a | self.assertRaises(OverflowError, |
---|
2065 | n/a | Decimal.as_integer_ratio, Decimal('-inf')) |
---|
2066 | n/a | self.assertRaises(ValueError, |
---|
2067 | n/a | Decimal.as_integer_ratio, Decimal('-nan')) |
---|
2068 | n/a | self.assertRaises(ValueError, |
---|
2069 | n/a | Decimal.as_integer_ratio, Decimal('snan123')) |
---|
2070 | n/a | |
---|
2071 | n/a | for exp in range(-4, 2): |
---|
2072 | n/a | for coeff in range(1000): |
---|
2073 | n/a | for sign in '+', '-': |
---|
2074 | n/a | d = Decimal('%s%dE%d' % (sign, coeff, exp)) |
---|
2075 | n/a | pq = d.as_integer_ratio() |
---|
2076 | n/a | p, q = pq |
---|
2077 | n/a | |
---|
2078 | n/a | # check return type |
---|
2079 | n/a | self.assertIsInstance(pq, tuple) |
---|
2080 | n/a | self.assertIsInstance(p, int) |
---|
2081 | n/a | self.assertIsInstance(q, int) |
---|
2082 | n/a | |
---|
2083 | n/a | # check normalization: q should be positive; |
---|
2084 | n/a | # p should be relatively prime to q. |
---|
2085 | n/a | self.assertGreater(q, 0) |
---|
2086 | n/a | self.assertEqual(math.gcd(p, q), 1) |
---|
2087 | n/a | |
---|
2088 | n/a | # check that p/q actually gives the correct value |
---|
2089 | n/a | self.assertEqual(Decimal(p) / Decimal(q), d) |
---|
2090 | n/a | |
---|
2091 | n/a | def test_subclassing(self): |
---|
2092 | n/a | # Different behaviours when subclassing Decimal |
---|
2093 | n/a | Decimal = self.decimal.Decimal |
---|
2094 | n/a | |
---|
2095 | n/a | class MyDecimal(Decimal): |
---|
2096 | n/a | y = None |
---|
2097 | n/a | |
---|
2098 | n/a | d1 = MyDecimal(1) |
---|
2099 | n/a | d2 = MyDecimal(2) |
---|
2100 | n/a | d = d1 + d2 |
---|
2101 | n/a | self.assertIs(type(d), Decimal) |
---|
2102 | n/a | |
---|
2103 | n/a | d = d1.max(d2) |
---|
2104 | n/a | self.assertIs(type(d), Decimal) |
---|
2105 | n/a | |
---|
2106 | n/a | d = copy.copy(d1) |
---|
2107 | n/a | self.assertIs(type(d), MyDecimal) |
---|
2108 | n/a | self.assertEqual(d, d1) |
---|
2109 | n/a | |
---|
2110 | n/a | d = copy.deepcopy(d1) |
---|
2111 | n/a | self.assertIs(type(d), MyDecimal) |
---|
2112 | n/a | self.assertEqual(d, d1) |
---|
2113 | n/a | |
---|
2114 | n/a | # Decimal(Decimal) |
---|
2115 | n/a | d = Decimal('1.0') |
---|
2116 | n/a | x = Decimal(d) |
---|
2117 | n/a | self.assertIs(type(x), Decimal) |
---|
2118 | n/a | self.assertEqual(x, d) |
---|
2119 | n/a | |
---|
2120 | n/a | # MyDecimal(Decimal) |
---|
2121 | n/a | m = MyDecimal(d) |
---|
2122 | n/a | self.assertIs(type(m), MyDecimal) |
---|
2123 | n/a | self.assertEqual(m, d) |
---|
2124 | n/a | self.assertIs(m.y, None) |
---|
2125 | n/a | |
---|
2126 | n/a | # Decimal(MyDecimal) |
---|
2127 | n/a | x = Decimal(m) |
---|
2128 | n/a | self.assertIs(type(x), Decimal) |
---|
2129 | n/a | self.assertEqual(x, d) |
---|
2130 | n/a | |
---|
2131 | n/a | # MyDecimal(MyDecimal) |
---|
2132 | n/a | m.y = 9 |
---|
2133 | n/a | x = MyDecimal(m) |
---|
2134 | n/a | self.assertIs(type(x), MyDecimal) |
---|
2135 | n/a | self.assertEqual(x, d) |
---|
2136 | n/a | self.assertIs(x.y, None) |
---|
2137 | n/a | |
---|
2138 | n/a | def test_implicit_context(self): |
---|
2139 | n/a | Decimal = self.decimal.Decimal |
---|
2140 | n/a | getcontext = self.decimal.getcontext |
---|
2141 | n/a | |
---|
2142 | n/a | # Check results when context given implicitly. (Issue 2478) |
---|
2143 | n/a | c = getcontext() |
---|
2144 | n/a | self.assertEqual(str(Decimal(0).sqrt()), |
---|
2145 | n/a | str(c.sqrt(Decimal(0)))) |
---|
2146 | n/a | |
---|
2147 | n/a | def test_none_args(self): |
---|
2148 | n/a | Decimal = self.decimal.Decimal |
---|
2149 | n/a | Context = self.decimal.Context |
---|
2150 | n/a | localcontext = self.decimal.localcontext |
---|
2151 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
2152 | n/a | DivisionByZero = self.decimal.DivisionByZero |
---|
2153 | n/a | Overflow = self.decimal.Overflow |
---|
2154 | n/a | Underflow = self.decimal.Underflow |
---|
2155 | n/a | Subnormal = self.decimal.Subnormal |
---|
2156 | n/a | Inexact = self.decimal.Inexact |
---|
2157 | n/a | Rounded = self.decimal.Rounded |
---|
2158 | n/a | Clamped = self.decimal.Clamped |
---|
2159 | n/a | |
---|
2160 | n/a | with localcontext(Context()) as c: |
---|
2161 | n/a | c.prec = 7 |
---|
2162 | n/a | c.Emax = 999 |
---|
2163 | n/a | c.Emin = -999 |
---|
2164 | n/a | |
---|
2165 | n/a | x = Decimal("111") |
---|
2166 | n/a | y = Decimal("1e9999") |
---|
2167 | n/a | z = Decimal("1e-9999") |
---|
2168 | n/a | |
---|
2169 | n/a | ##### Unary functions |
---|
2170 | n/a | c.clear_flags() |
---|
2171 | n/a | self.assertEqual(str(x.exp(context=None)), '1.609487E+48') |
---|
2172 | n/a | self.assertTrue(c.flags[Inexact]) |
---|
2173 | n/a | self.assertTrue(c.flags[Rounded]) |
---|
2174 | n/a | c.clear_flags() |
---|
2175 | n/a | self.assertRaises(Overflow, y.exp, context=None) |
---|
2176 | n/a | self.assertTrue(c.flags[Overflow]) |
---|
2177 | n/a | |
---|
2178 | n/a | self.assertIs(z.is_normal(context=None), False) |
---|
2179 | n/a | self.assertIs(z.is_subnormal(context=None), True) |
---|
2180 | n/a | |
---|
2181 | n/a | c.clear_flags() |
---|
2182 | n/a | self.assertEqual(str(x.ln(context=None)), '4.709530') |
---|
2183 | n/a | self.assertTrue(c.flags[Inexact]) |
---|
2184 | n/a | self.assertTrue(c.flags[Rounded]) |
---|
2185 | n/a | c.clear_flags() |
---|
2186 | n/a | self.assertRaises(InvalidOperation, Decimal(-1).ln, context=None) |
---|
2187 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2188 | n/a | |
---|
2189 | n/a | c.clear_flags() |
---|
2190 | n/a | self.assertEqual(str(x.log10(context=None)), '2.045323') |
---|
2191 | n/a | self.assertTrue(c.flags[Inexact]) |
---|
2192 | n/a | self.assertTrue(c.flags[Rounded]) |
---|
2193 | n/a | c.clear_flags() |
---|
2194 | n/a | self.assertRaises(InvalidOperation, Decimal(-1).log10, context=None) |
---|
2195 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2196 | n/a | |
---|
2197 | n/a | c.clear_flags() |
---|
2198 | n/a | self.assertEqual(str(x.logb(context=None)), '2') |
---|
2199 | n/a | self.assertRaises(DivisionByZero, Decimal(0).logb, context=None) |
---|
2200 | n/a | self.assertTrue(c.flags[DivisionByZero]) |
---|
2201 | n/a | |
---|
2202 | n/a | c.clear_flags() |
---|
2203 | n/a | self.assertEqual(str(x.logical_invert(context=None)), '1111000') |
---|
2204 | n/a | self.assertRaises(InvalidOperation, y.logical_invert, context=None) |
---|
2205 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2206 | n/a | |
---|
2207 | n/a | c.clear_flags() |
---|
2208 | n/a | self.assertEqual(str(y.next_minus(context=None)), '9.999999E+999') |
---|
2209 | n/a | self.assertRaises(InvalidOperation, Decimal('sNaN').next_minus, context=None) |
---|
2210 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2211 | n/a | |
---|
2212 | n/a | c.clear_flags() |
---|
2213 | n/a | self.assertEqual(str(y.next_plus(context=None)), 'Infinity') |
---|
2214 | n/a | self.assertRaises(InvalidOperation, Decimal('sNaN').next_plus, context=None) |
---|
2215 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2216 | n/a | |
---|
2217 | n/a | c.clear_flags() |
---|
2218 | n/a | self.assertEqual(str(z.normalize(context=None)), '0') |
---|
2219 | n/a | self.assertRaises(Overflow, y.normalize, context=None) |
---|
2220 | n/a | self.assertTrue(c.flags[Overflow]) |
---|
2221 | n/a | |
---|
2222 | n/a | self.assertEqual(str(z.number_class(context=None)), '+Subnormal') |
---|
2223 | n/a | |
---|
2224 | n/a | c.clear_flags() |
---|
2225 | n/a | self.assertEqual(str(z.sqrt(context=None)), '0E-1005') |
---|
2226 | n/a | self.assertTrue(c.flags[Clamped]) |
---|
2227 | n/a | self.assertTrue(c.flags[Inexact]) |
---|
2228 | n/a | self.assertTrue(c.flags[Rounded]) |
---|
2229 | n/a | self.assertTrue(c.flags[Subnormal]) |
---|
2230 | n/a | self.assertTrue(c.flags[Underflow]) |
---|
2231 | n/a | c.clear_flags() |
---|
2232 | n/a | self.assertRaises(Overflow, y.sqrt, context=None) |
---|
2233 | n/a | self.assertTrue(c.flags[Overflow]) |
---|
2234 | n/a | |
---|
2235 | n/a | c.capitals = 0 |
---|
2236 | n/a | self.assertEqual(str(z.to_eng_string(context=None)), '1e-9999') |
---|
2237 | n/a | c.capitals = 1 |
---|
2238 | n/a | |
---|
2239 | n/a | |
---|
2240 | n/a | ##### Binary functions |
---|
2241 | n/a | c.clear_flags() |
---|
2242 | n/a | ans = str(x.compare(Decimal('Nan891287828'), context=None)) |
---|
2243 | n/a | self.assertEqual(ans, 'NaN1287828') |
---|
2244 | n/a | self.assertRaises(InvalidOperation, x.compare, Decimal('sNaN'), context=None) |
---|
2245 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2246 | n/a | |
---|
2247 | n/a | c.clear_flags() |
---|
2248 | n/a | ans = str(x.compare_signal(8224, context=None)) |
---|
2249 | n/a | self.assertEqual(ans, '-1') |
---|
2250 | n/a | self.assertRaises(InvalidOperation, x.compare_signal, Decimal('NaN'), context=None) |
---|
2251 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2252 | n/a | |
---|
2253 | n/a | c.clear_flags() |
---|
2254 | n/a | ans = str(x.logical_and(101, context=None)) |
---|
2255 | n/a | self.assertEqual(ans, '101') |
---|
2256 | n/a | self.assertRaises(InvalidOperation, x.logical_and, 123, context=None) |
---|
2257 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2258 | n/a | |
---|
2259 | n/a | c.clear_flags() |
---|
2260 | n/a | ans = str(x.logical_or(101, context=None)) |
---|
2261 | n/a | self.assertEqual(ans, '111') |
---|
2262 | n/a | self.assertRaises(InvalidOperation, x.logical_or, 123, context=None) |
---|
2263 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2264 | n/a | |
---|
2265 | n/a | c.clear_flags() |
---|
2266 | n/a | ans = str(x.logical_xor(101, context=None)) |
---|
2267 | n/a | self.assertEqual(ans, '10') |
---|
2268 | n/a | self.assertRaises(InvalidOperation, x.logical_xor, 123, context=None) |
---|
2269 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2270 | n/a | |
---|
2271 | n/a | c.clear_flags() |
---|
2272 | n/a | ans = str(x.max(101, context=None)) |
---|
2273 | n/a | self.assertEqual(ans, '111') |
---|
2274 | n/a | self.assertRaises(InvalidOperation, x.max, Decimal('sNaN'), context=None) |
---|
2275 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2276 | n/a | |
---|
2277 | n/a | c.clear_flags() |
---|
2278 | n/a | ans = str(x.max_mag(101, context=None)) |
---|
2279 | n/a | self.assertEqual(ans, '111') |
---|
2280 | n/a | self.assertRaises(InvalidOperation, x.max_mag, Decimal('sNaN'), context=None) |
---|
2281 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2282 | n/a | |
---|
2283 | n/a | c.clear_flags() |
---|
2284 | n/a | ans = str(x.min(101, context=None)) |
---|
2285 | n/a | self.assertEqual(ans, '101') |
---|
2286 | n/a | self.assertRaises(InvalidOperation, x.min, Decimal('sNaN'), context=None) |
---|
2287 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2288 | n/a | |
---|
2289 | n/a | c.clear_flags() |
---|
2290 | n/a | ans = str(x.min_mag(101, context=None)) |
---|
2291 | n/a | self.assertEqual(ans, '101') |
---|
2292 | n/a | self.assertRaises(InvalidOperation, x.min_mag, Decimal('sNaN'), context=None) |
---|
2293 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2294 | n/a | |
---|
2295 | n/a | c.clear_flags() |
---|
2296 | n/a | ans = str(x.remainder_near(101, context=None)) |
---|
2297 | n/a | self.assertEqual(ans, '10') |
---|
2298 | n/a | self.assertRaises(InvalidOperation, y.remainder_near, 101, context=None) |
---|
2299 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2300 | n/a | |
---|
2301 | n/a | c.clear_flags() |
---|
2302 | n/a | ans = str(x.rotate(2, context=None)) |
---|
2303 | n/a | self.assertEqual(ans, '11100') |
---|
2304 | n/a | self.assertRaises(InvalidOperation, x.rotate, 101, context=None) |
---|
2305 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2306 | n/a | |
---|
2307 | n/a | c.clear_flags() |
---|
2308 | n/a | ans = str(x.scaleb(7, context=None)) |
---|
2309 | n/a | self.assertEqual(ans, '1.11E+9') |
---|
2310 | n/a | self.assertRaises(InvalidOperation, x.scaleb, 10000, context=None) |
---|
2311 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2312 | n/a | |
---|
2313 | n/a | c.clear_flags() |
---|
2314 | n/a | ans = str(x.shift(2, context=None)) |
---|
2315 | n/a | self.assertEqual(ans, '11100') |
---|
2316 | n/a | self.assertRaises(InvalidOperation, x.shift, 10000, context=None) |
---|
2317 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2318 | n/a | |
---|
2319 | n/a | |
---|
2320 | n/a | ##### Ternary functions |
---|
2321 | n/a | c.clear_flags() |
---|
2322 | n/a | ans = str(x.fma(2, 3, context=None)) |
---|
2323 | n/a | self.assertEqual(ans, '225') |
---|
2324 | n/a | self.assertRaises(Overflow, x.fma, Decimal('1e9999'), 3, context=None) |
---|
2325 | n/a | self.assertTrue(c.flags[Overflow]) |
---|
2326 | n/a | |
---|
2327 | n/a | |
---|
2328 | n/a | ##### Special cases |
---|
2329 | n/a | c.rounding = ROUND_HALF_EVEN |
---|
2330 | n/a | ans = str(Decimal('1.5').to_integral(rounding=None, context=None)) |
---|
2331 | n/a | self.assertEqual(ans, '2') |
---|
2332 | n/a | c.rounding = ROUND_DOWN |
---|
2333 | n/a | ans = str(Decimal('1.5').to_integral(rounding=None, context=None)) |
---|
2334 | n/a | self.assertEqual(ans, '1') |
---|
2335 | n/a | ans = str(Decimal('1.5').to_integral(rounding=ROUND_UP, context=None)) |
---|
2336 | n/a | self.assertEqual(ans, '2') |
---|
2337 | n/a | c.clear_flags() |
---|
2338 | n/a | self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral, context=None) |
---|
2339 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2340 | n/a | |
---|
2341 | n/a | c.rounding = ROUND_HALF_EVEN |
---|
2342 | n/a | ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None)) |
---|
2343 | n/a | self.assertEqual(ans, '2') |
---|
2344 | n/a | c.rounding = ROUND_DOWN |
---|
2345 | n/a | ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None)) |
---|
2346 | n/a | self.assertEqual(ans, '1') |
---|
2347 | n/a | ans = str(Decimal('1.5').to_integral_value(rounding=ROUND_UP, context=None)) |
---|
2348 | n/a | self.assertEqual(ans, '2') |
---|
2349 | n/a | c.clear_flags() |
---|
2350 | n/a | self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_value, context=None) |
---|
2351 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2352 | n/a | |
---|
2353 | n/a | c.rounding = ROUND_HALF_EVEN |
---|
2354 | n/a | ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None)) |
---|
2355 | n/a | self.assertEqual(ans, '2') |
---|
2356 | n/a | c.rounding = ROUND_DOWN |
---|
2357 | n/a | ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None)) |
---|
2358 | n/a | self.assertEqual(ans, '1') |
---|
2359 | n/a | ans = str(Decimal('1.5').to_integral_exact(rounding=ROUND_UP, context=None)) |
---|
2360 | n/a | self.assertEqual(ans, '2') |
---|
2361 | n/a | c.clear_flags() |
---|
2362 | n/a | self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_exact, context=None) |
---|
2363 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2364 | n/a | |
---|
2365 | n/a | c.rounding = ROUND_UP |
---|
2366 | n/a | ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None)) |
---|
2367 | n/a | self.assertEqual(ans, '1.501') |
---|
2368 | n/a | c.rounding = ROUND_DOWN |
---|
2369 | n/a | ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None)) |
---|
2370 | n/a | self.assertEqual(ans, '1.500') |
---|
2371 | n/a | ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=ROUND_UP, context=None)) |
---|
2372 | n/a | self.assertEqual(ans, '1.501') |
---|
2373 | n/a | c.clear_flags() |
---|
2374 | n/a | self.assertRaises(InvalidOperation, y.quantize, Decimal('1e-10'), rounding=ROUND_UP, context=None) |
---|
2375 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
2376 | n/a | |
---|
2377 | n/a | with localcontext(Context()) as context: |
---|
2378 | n/a | context.prec = 7 |
---|
2379 | n/a | context.Emax = 999 |
---|
2380 | n/a | context.Emin = -999 |
---|
2381 | n/a | with localcontext(ctx=None) as c: |
---|
2382 | n/a | self.assertEqual(c.prec, 7) |
---|
2383 | n/a | self.assertEqual(c.Emax, 999) |
---|
2384 | n/a | self.assertEqual(c.Emin, -999) |
---|
2385 | n/a | |
---|
2386 | n/a | def test_conversions_from_int(self): |
---|
2387 | n/a | # Check that methods taking a second Decimal argument will |
---|
2388 | n/a | # always accept an integer in place of a Decimal. |
---|
2389 | n/a | Decimal = self.decimal.Decimal |
---|
2390 | n/a | |
---|
2391 | n/a | self.assertEqual(Decimal(4).compare(3), |
---|
2392 | n/a | Decimal(4).compare(Decimal(3))) |
---|
2393 | n/a | self.assertEqual(Decimal(4).compare_signal(3), |
---|
2394 | n/a | Decimal(4).compare_signal(Decimal(3))) |
---|
2395 | n/a | self.assertEqual(Decimal(4).compare_total(3), |
---|
2396 | n/a | Decimal(4).compare_total(Decimal(3))) |
---|
2397 | n/a | self.assertEqual(Decimal(4).compare_total_mag(3), |
---|
2398 | n/a | Decimal(4).compare_total_mag(Decimal(3))) |
---|
2399 | n/a | self.assertEqual(Decimal(10101).logical_and(1001), |
---|
2400 | n/a | Decimal(10101).logical_and(Decimal(1001))) |
---|
2401 | n/a | self.assertEqual(Decimal(10101).logical_or(1001), |
---|
2402 | n/a | Decimal(10101).logical_or(Decimal(1001))) |
---|
2403 | n/a | self.assertEqual(Decimal(10101).logical_xor(1001), |
---|
2404 | n/a | Decimal(10101).logical_xor(Decimal(1001))) |
---|
2405 | n/a | self.assertEqual(Decimal(567).max(123), |
---|
2406 | n/a | Decimal(567).max(Decimal(123))) |
---|
2407 | n/a | self.assertEqual(Decimal(567).max_mag(123), |
---|
2408 | n/a | Decimal(567).max_mag(Decimal(123))) |
---|
2409 | n/a | self.assertEqual(Decimal(567).min(123), |
---|
2410 | n/a | Decimal(567).min(Decimal(123))) |
---|
2411 | n/a | self.assertEqual(Decimal(567).min_mag(123), |
---|
2412 | n/a | Decimal(567).min_mag(Decimal(123))) |
---|
2413 | n/a | self.assertEqual(Decimal(567).next_toward(123), |
---|
2414 | n/a | Decimal(567).next_toward(Decimal(123))) |
---|
2415 | n/a | self.assertEqual(Decimal(1234).quantize(100), |
---|
2416 | n/a | Decimal(1234).quantize(Decimal(100))) |
---|
2417 | n/a | self.assertEqual(Decimal(768).remainder_near(1234), |
---|
2418 | n/a | Decimal(768).remainder_near(Decimal(1234))) |
---|
2419 | n/a | self.assertEqual(Decimal(123).rotate(1), |
---|
2420 | n/a | Decimal(123).rotate(Decimal(1))) |
---|
2421 | n/a | self.assertEqual(Decimal(1234).same_quantum(1000), |
---|
2422 | n/a | Decimal(1234).same_quantum(Decimal(1000))) |
---|
2423 | n/a | self.assertEqual(Decimal('9.123').scaleb(-100), |
---|
2424 | n/a | Decimal('9.123').scaleb(Decimal(-100))) |
---|
2425 | n/a | self.assertEqual(Decimal(456).shift(-1), |
---|
2426 | n/a | Decimal(456).shift(Decimal(-1))) |
---|
2427 | n/a | |
---|
2428 | n/a | self.assertEqual(Decimal(-12).fma(Decimal(45), 67), |
---|
2429 | n/a | Decimal(-12).fma(Decimal(45), Decimal(67))) |
---|
2430 | n/a | self.assertEqual(Decimal(-12).fma(45, 67), |
---|
2431 | n/a | Decimal(-12).fma(Decimal(45), Decimal(67))) |
---|
2432 | n/a | self.assertEqual(Decimal(-12).fma(45, Decimal(67)), |
---|
2433 | n/a | Decimal(-12).fma(Decimal(45), Decimal(67))) |
---|
2434 | n/a | |
---|
2435 | n/a | class CUsabilityTest(UsabilityTest): |
---|
2436 | n/a | decimal = C |
---|
2437 | n/a | class PyUsabilityTest(UsabilityTest): |
---|
2438 | n/a | decimal = P |
---|
2439 | n/a | |
---|
2440 | n/a | class PythonAPItests(unittest.TestCase): |
---|
2441 | n/a | |
---|
2442 | n/a | def test_abc(self): |
---|
2443 | n/a | Decimal = self.decimal.Decimal |
---|
2444 | n/a | |
---|
2445 | n/a | self.assertTrue(issubclass(Decimal, numbers.Number)) |
---|
2446 | n/a | self.assertFalse(issubclass(Decimal, numbers.Real)) |
---|
2447 | n/a | self.assertIsInstance(Decimal(0), numbers.Number) |
---|
2448 | n/a | self.assertNotIsInstance(Decimal(0), numbers.Real) |
---|
2449 | n/a | |
---|
2450 | n/a | def test_pickle(self): |
---|
2451 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2452 | n/a | Decimal = self.decimal.Decimal |
---|
2453 | n/a | |
---|
2454 | n/a | savedecimal = sys.modules['decimal'] |
---|
2455 | n/a | |
---|
2456 | n/a | # Round trip |
---|
2457 | n/a | sys.modules['decimal'] = self.decimal |
---|
2458 | n/a | d = Decimal('-3.141590000') |
---|
2459 | n/a | p = pickle.dumps(d, proto) |
---|
2460 | n/a | e = pickle.loads(p) |
---|
2461 | n/a | self.assertEqual(d, e) |
---|
2462 | n/a | |
---|
2463 | n/a | if C: |
---|
2464 | n/a | # Test interchangeability |
---|
2465 | n/a | x = C.Decimal('-3.123e81723') |
---|
2466 | n/a | y = P.Decimal('-3.123e81723') |
---|
2467 | n/a | |
---|
2468 | n/a | sys.modules['decimal'] = C |
---|
2469 | n/a | sx = pickle.dumps(x, proto) |
---|
2470 | n/a | sys.modules['decimal'] = P |
---|
2471 | n/a | r = pickle.loads(sx) |
---|
2472 | n/a | self.assertIsInstance(r, P.Decimal) |
---|
2473 | n/a | self.assertEqual(r, y) |
---|
2474 | n/a | |
---|
2475 | n/a | sys.modules['decimal'] = P |
---|
2476 | n/a | sy = pickle.dumps(y, proto) |
---|
2477 | n/a | sys.modules['decimal'] = C |
---|
2478 | n/a | r = pickle.loads(sy) |
---|
2479 | n/a | self.assertIsInstance(r, C.Decimal) |
---|
2480 | n/a | self.assertEqual(r, x) |
---|
2481 | n/a | |
---|
2482 | n/a | x = C.Decimal('-3.123e81723').as_tuple() |
---|
2483 | n/a | y = P.Decimal('-3.123e81723').as_tuple() |
---|
2484 | n/a | |
---|
2485 | n/a | sys.modules['decimal'] = C |
---|
2486 | n/a | sx = pickle.dumps(x, proto) |
---|
2487 | n/a | sys.modules['decimal'] = P |
---|
2488 | n/a | r = pickle.loads(sx) |
---|
2489 | n/a | self.assertIsInstance(r, P.DecimalTuple) |
---|
2490 | n/a | self.assertEqual(r, y) |
---|
2491 | n/a | |
---|
2492 | n/a | sys.modules['decimal'] = P |
---|
2493 | n/a | sy = pickle.dumps(y, proto) |
---|
2494 | n/a | sys.modules['decimal'] = C |
---|
2495 | n/a | r = pickle.loads(sy) |
---|
2496 | n/a | self.assertIsInstance(r, C.DecimalTuple) |
---|
2497 | n/a | self.assertEqual(r, x) |
---|
2498 | n/a | |
---|
2499 | n/a | sys.modules['decimal'] = savedecimal |
---|
2500 | n/a | |
---|
2501 | n/a | def test_int(self): |
---|
2502 | n/a | Decimal = self.decimal.Decimal |
---|
2503 | n/a | |
---|
2504 | n/a | for x in range(-250, 250): |
---|
2505 | n/a | s = '%0.2f' % (x / 100.0) |
---|
2506 | n/a | # should work the same as for floats |
---|
2507 | n/a | self.assertEqual(int(Decimal(s)), int(float(s))) |
---|
2508 | n/a | # should work the same as to_integral in the ROUND_DOWN mode |
---|
2509 | n/a | d = Decimal(s) |
---|
2510 | n/a | r = d.to_integral(ROUND_DOWN) |
---|
2511 | n/a | self.assertEqual(Decimal(int(d)), r) |
---|
2512 | n/a | |
---|
2513 | n/a | self.assertRaises(ValueError, int, Decimal('-nan')) |
---|
2514 | n/a | self.assertRaises(ValueError, int, Decimal('snan')) |
---|
2515 | n/a | self.assertRaises(OverflowError, int, Decimal('inf')) |
---|
2516 | n/a | self.assertRaises(OverflowError, int, Decimal('-inf')) |
---|
2517 | n/a | |
---|
2518 | n/a | def test_trunc(self): |
---|
2519 | n/a | Decimal = self.decimal.Decimal |
---|
2520 | n/a | |
---|
2521 | n/a | for x in range(-250, 250): |
---|
2522 | n/a | s = '%0.2f' % (x / 100.0) |
---|
2523 | n/a | # should work the same as for floats |
---|
2524 | n/a | self.assertEqual(int(Decimal(s)), int(float(s))) |
---|
2525 | n/a | # should work the same as to_integral in the ROUND_DOWN mode |
---|
2526 | n/a | d = Decimal(s) |
---|
2527 | n/a | r = d.to_integral(ROUND_DOWN) |
---|
2528 | n/a | self.assertEqual(Decimal(math.trunc(d)), r) |
---|
2529 | n/a | |
---|
2530 | n/a | def test_from_float(self): |
---|
2531 | n/a | |
---|
2532 | n/a | Decimal = self.decimal.Decimal |
---|
2533 | n/a | |
---|
2534 | n/a | class MyDecimal(Decimal): |
---|
2535 | n/a | def __init__(self, _): |
---|
2536 | n/a | self.x = 'y' |
---|
2537 | n/a | |
---|
2538 | n/a | self.assertTrue(issubclass(MyDecimal, Decimal)) |
---|
2539 | n/a | |
---|
2540 | n/a | r = MyDecimal.from_float(0.1) |
---|
2541 | n/a | self.assertEqual(type(r), MyDecimal) |
---|
2542 | n/a | self.assertEqual(str(r), |
---|
2543 | n/a | '0.1000000000000000055511151231257827021181583404541015625') |
---|
2544 | n/a | self.assertEqual(r.x, 'y') |
---|
2545 | n/a | |
---|
2546 | n/a | bigint = 12345678901234567890123456789 |
---|
2547 | n/a | self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint)) |
---|
2548 | n/a | self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan()) |
---|
2549 | n/a | self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite()) |
---|
2550 | n/a | self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite()) |
---|
2551 | n/a | self.assertEqual(str(MyDecimal.from_float(float('nan'))), |
---|
2552 | n/a | str(Decimal('NaN'))) |
---|
2553 | n/a | self.assertEqual(str(MyDecimal.from_float(float('inf'))), |
---|
2554 | n/a | str(Decimal('Infinity'))) |
---|
2555 | n/a | self.assertEqual(str(MyDecimal.from_float(float('-inf'))), |
---|
2556 | n/a | str(Decimal('-Infinity'))) |
---|
2557 | n/a | self.assertRaises(TypeError, MyDecimal.from_float, 'abc') |
---|
2558 | n/a | for i in range(200): |
---|
2559 | n/a | x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) |
---|
2560 | n/a | self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip |
---|
2561 | n/a | |
---|
2562 | n/a | def test_create_decimal_from_float(self): |
---|
2563 | n/a | Decimal = self.decimal.Decimal |
---|
2564 | n/a | Context = self.decimal.Context |
---|
2565 | n/a | Inexact = self.decimal.Inexact |
---|
2566 | n/a | |
---|
2567 | n/a | context = Context(prec=5, rounding=ROUND_DOWN) |
---|
2568 | n/a | self.assertEqual( |
---|
2569 | n/a | context.create_decimal_from_float(math.pi), |
---|
2570 | n/a | Decimal('3.1415') |
---|
2571 | n/a | ) |
---|
2572 | n/a | context = Context(prec=5, rounding=ROUND_UP) |
---|
2573 | n/a | self.assertEqual( |
---|
2574 | n/a | context.create_decimal_from_float(math.pi), |
---|
2575 | n/a | Decimal('3.1416') |
---|
2576 | n/a | ) |
---|
2577 | n/a | context = Context(prec=5, traps=[Inexact]) |
---|
2578 | n/a | self.assertRaises( |
---|
2579 | n/a | Inexact, |
---|
2580 | n/a | context.create_decimal_from_float, |
---|
2581 | n/a | math.pi |
---|
2582 | n/a | ) |
---|
2583 | n/a | self.assertEqual(repr(context.create_decimal_from_float(-0.0)), |
---|
2584 | n/a | "Decimal('-0')") |
---|
2585 | n/a | self.assertEqual(repr(context.create_decimal_from_float(1.0)), |
---|
2586 | n/a | "Decimal('1')") |
---|
2587 | n/a | self.assertEqual(repr(context.create_decimal_from_float(10)), |
---|
2588 | n/a | "Decimal('10')") |
---|
2589 | n/a | |
---|
2590 | n/a | def test_quantize(self): |
---|
2591 | n/a | Decimal = self.decimal.Decimal |
---|
2592 | n/a | Context = self.decimal.Context |
---|
2593 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
2594 | n/a | |
---|
2595 | n/a | c = Context(Emax=99999, Emin=-99999) |
---|
2596 | n/a | self.assertEqual( |
---|
2597 | n/a | Decimal('7.335').quantize(Decimal('.01')), |
---|
2598 | n/a | Decimal('7.34') |
---|
2599 | n/a | ) |
---|
2600 | n/a | self.assertEqual( |
---|
2601 | n/a | Decimal('7.335').quantize(Decimal('.01'), rounding=ROUND_DOWN), |
---|
2602 | n/a | Decimal('7.33') |
---|
2603 | n/a | ) |
---|
2604 | n/a | self.assertRaises( |
---|
2605 | n/a | InvalidOperation, |
---|
2606 | n/a | Decimal("10e99999").quantize, Decimal('1e100000'), context=c |
---|
2607 | n/a | ) |
---|
2608 | n/a | |
---|
2609 | n/a | c = Context() |
---|
2610 | n/a | d = Decimal("0.871831e800") |
---|
2611 | n/a | x = d.quantize(context=c, exp=Decimal("1e797"), rounding=ROUND_DOWN) |
---|
2612 | n/a | self.assertEqual(x, Decimal('8.71E+799')) |
---|
2613 | n/a | |
---|
2614 | n/a | def test_complex(self): |
---|
2615 | n/a | Decimal = self.decimal.Decimal |
---|
2616 | n/a | |
---|
2617 | n/a | x = Decimal("9.8182731e181273") |
---|
2618 | n/a | self.assertEqual(x.real, x) |
---|
2619 | n/a | self.assertEqual(x.imag, 0) |
---|
2620 | n/a | self.assertEqual(x.conjugate(), x) |
---|
2621 | n/a | |
---|
2622 | n/a | x = Decimal("1") |
---|
2623 | n/a | self.assertEqual(complex(x), complex(float(1))) |
---|
2624 | n/a | |
---|
2625 | n/a | self.assertRaises(AttributeError, setattr, x, 'real', 100) |
---|
2626 | n/a | self.assertRaises(AttributeError, setattr, x, 'imag', 100) |
---|
2627 | n/a | self.assertRaises(AttributeError, setattr, x, 'conjugate', 100) |
---|
2628 | n/a | self.assertRaises(AttributeError, setattr, x, '__complex__', 100) |
---|
2629 | n/a | |
---|
2630 | n/a | def test_named_parameters(self): |
---|
2631 | n/a | D = self.decimal.Decimal |
---|
2632 | n/a | Context = self.decimal.Context |
---|
2633 | n/a | localcontext = self.decimal.localcontext |
---|
2634 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
2635 | n/a | Overflow = self.decimal.Overflow |
---|
2636 | n/a | |
---|
2637 | n/a | xc = Context() |
---|
2638 | n/a | xc.prec = 1 |
---|
2639 | n/a | xc.Emax = 1 |
---|
2640 | n/a | xc.Emin = -1 |
---|
2641 | n/a | |
---|
2642 | n/a | with localcontext() as c: |
---|
2643 | n/a | c.clear_flags() |
---|
2644 | n/a | |
---|
2645 | n/a | self.assertEqual(D(9, xc), 9) |
---|
2646 | n/a | self.assertEqual(D(9, context=xc), 9) |
---|
2647 | n/a | self.assertEqual(D(context=xc, value=9), 9) |
---|
2648 | n/a | self.assertEqual(D(context=xc), 0) |
---|
2649 | n/a | xc.clear_flags() |
---|
2650 | n/a | self.assertRaises(InvalidOperation, D, "xyz", context=xc) |
---|
2651 | n/a | self.assertTrue(xc.flags[InvalidOperation]) |
---|
2652 | n/a | self.assertFalse(c.flags[InvalidOperation]) |
---|
2653 | n/a | |
---|
2654 | n/a | xc.clear_flags() |
---|
2655 | n/a | self.assertEqual(D(2).exp(context=xc), 7) |
---|
2656 | n/a | self.assertRaises(Overflow, D(8).exp, context=xc) |
---|
2657 | n/a | self.assertTrue(xc.flags[Overflow]) |
---|
2658 | n/a | self.assertFalse(c.flags[Overflow]) |
---|
2659 | n/a | |
---|
2660 | n/a | xc.clear_flags() |
---|
2661 | n/a | self.assertEqual(D(2).ln(context=xc), D('0.7')) |
---|
2662 | n/a | self.assertRaises(InvalidOperation, D(-1).ln, context=xc) |
---|
2663 | n/a | self.assertTrue(xc.flags[InvalidOperation]) |
---|
2664 | n/a | self.assertFalse(c.flags[InvalidOperation]) |
---|
2665 | n/a | |
---|
2666 | n/a | self.assertEqual(D(0).log10(context=xc), D('-inf')) |
---|
2667 | n/a | self.assertEqual(D(-1).next_minus(context=xc), -2) |
---|
2668 | n/a | self.assertEqual(D(-1).next_plus(context=xc), D('-0.9')) |
---|
2669 | n/a | self.assertEqual(D("9.73").normalize(context=xc), D('1E+1')) |
---|
2670 | n/a | self.assertEqual(D("9999").to_integral(context=xc), 9999) |
---|
2671 | n/a | self.assertEqual(D("-2000").to_integral_exact(context=xc), -2000) |
---|
2672 | n/a | self.assertEqual(D("123").to_integral_value(context=xc), 123) |
---|
2673 | n/a | self.assertEqual(D("0.0625").sqrt(context=xc), D('0.2')) |
---|
2674 | n/a | |
---|
2675 | n/a | self.assertEqual(D("0.0625").compare(context=xc, other=3), -1) |
---|
2676 | n/a | xc.clear_flags() |
---|
2677 | n/a | self.assertRaises(InvalidOperation, |
---|
2678 | n/a | D("0").compare_signal, D('nan'), context=xc) |
---|
2679 | n/a | self.assertTrue(xc.flags[InvalidOperation]) |
---|
2680 | n/a | self.assertFalse(c.flags[InvalidOperation]) |
---|
2681 | n/a | self.assertEqual(D("0.01").max(D('0.0101'), context=xc), D('0.0')) |
---|
2682 | n/a | self.assertEqual(D("0.01").max(D('0.0101'), context=xc), D('0.0')) |
---|
2683 | n/a | self.assertEqual(D("0.2").max_mag(D('-0.3'), context=xc), |
---|
2684 | n/a | D('-0.3')) |
---|
2685 | n/a | self.assertEqual(D("0.02").min(D('-0.03'), context=xc), D('-0.0')) |
---|
2686 | n/a | self.assertEqual(D("0.02").min_mag(D('-0.03'), context=xc), |
---|
2687 | n/a | D('0.0')) |
---|
2688 | n/a | self.assertEqual(D("0.2").next_toward(D('-1'), context=xc), D('0.1')) |
---|
2689 | n/a | xc.clear_flags() |
---|
2690 | n/a | self.assertRaises(InvalidOperation, |
---|
2691 | n/a | D("0.2").quantize, D('1e10'), context=xc) |
---|
2692 | n/a | self.assertTrue(xc.flags[InvalidOperation]) |
---|
2693 | n/a | self.assertFalse(c.flags[InvalidOperation]) |
---|
2694 | n/a | self.assertEqual(D("9.99").remainder_near(D('1.5'), context=xc), |
---|
2695 | n/a | D('-0.5')) |
---|
2696 | n/a | |
---|
2697 | n/a | self.assertEqual(D("9.9").fma(third=D('0.9'), context=xc, other=7), |
---|
2698 | n/a | D('7E+1')) |
---|
2699 | n/a | |
---|
2700 | n/a | self.assertRaises(TypeError, D(1).is_canonical, context=xc) |
---|
2701 | n/a | self.assertRaises(TypeError, D(1).is_finite, context=xc) |
---|
2702 | n/a | self.assertRaises(TypeError, D(1).is_infinite, context=xc) |
---|
2703 | n/a | self.assertRaises(TypeError, D(1).is_nan, context=xc) |
---|
2704 | n/a | self.assertRaises(TypeError, D(1).is_qnan, context=xc) |
---|
2705 | n/a | self.assertRaises(TypeError, D(1).is_snan, context=xc) |
---|
2706 | n/a | self.assertRaises(TypeError, D(1).is_signed, context=xc) |
---|
2707 | n/a | self.assertRaises(TypeError, D(1).is_zero, context=xc) |
---|
2708 | n/a | |
---|
2709 | n/a | self.assertFalse(D("0.01").is_normal(context=xc)) |
---|
2710 | n/a | self.assertTrue(D("0.01").is_subnormal(context=xc)) |
---|
2711 | n/a | |
---|
2712 | n/a | self.assertRaises(TypeError, D(1).adjusted, context=xc) |
---|
2713 | n/a | self.assertRaises(TypeError, D(1).conjugate, context=xc) |
---|
2714 | n/a | self.assertRaises(TypeError, D(1).radix, context=xc) |
---|
2715 | n/a | |
---|
2716 | n/a | self.assertEqual(D(-111).logb(context=xc), 2) |
---|
2717 | n/a | self.assertEqual(D(0).logical_invert(context=xc), 1) |
---|
2718 | n/a | self.assertEqual(D('0.01').number_class(context=xc), '+Subnormal') |
---|
2719 | n/a | self.assertEqual(D('0.21').to_eng_string(context=xc), '0.21') |
---|
2720 | n/a | |
---|
2721 | n/a | self.assertEqual(D('11').logical_and(D('10'), context=xc), 0) |
---|
2722 | n/a | self.assertEqual(D('11').logical_or(D('10'), context=xc), 1) |
---|
2723 | n/a | self.assertEqual(D('01').logical_xor(D('10'), context=xc), 1) |
---|
2724 | n/a | self.assertEqual(D('23').rotate(1, context=xc), 3) |
---|
2725 | n/a | self.assertEqual(D('23').rotate(1, context=xc), 3) |
---|
2726 | n/a | xc.clear_flags() |
---|
2727 | n/a | self.assertRaises(Overflow, |
---|
2728 | n/a | D('23').scaleb, 1, context=xc) |
---|
2729 | n/a | self.assertTrue(xc.flags[Overflow]) |
---|
2730 | n/a | self.assertFalse(c.flags[Overflow]) |
---|
2731 | n/a | self.assertEqual(D('23').shift(-1, context=xc), 0) |
---|
2732 | n/a | |
---|
2733 | n/a | self.assertRaises(TypeError, D.from_float, 1.1, context=xc) |
---|
2734 | n/a | self.assertRaises(TypeError, D(0).as_tuple, context=xc) |
---|
2735 | n/a | |
---|
2736 | n/a | self.assertEqual(D(1).canonical(), 1) |
---|
2737 | n/a | self.assertRaises(TypeError, D("-1").copy_abs, context=xc) |
---|
2738 | n/a | self.assertRaises(TypeError, D("-1").copy_negate, context=xc) |
---|
2739 | n/a | self.assertRaises(TypeError, D(1).canonical, context="x") |
---|
2740 | n/a | self.assertRaises(TypeError, D(1).canonical, xyz="x") |
---|
2741 | n/a | |
---|
2742 | n/a | def test_exception_hierarchy(self): |
---|
2743 | n/a | |
---|
2744 | n/a | decimal = self.decimal |
---|
2745 | n/a | DecimalException = decimal.DecimalException |
---|
2746 | n/a | InvalidOperation = decimal.InvalidOperation |
---|
2747 | n/a | FloatOperation = decimal.FloatOperation |
---|
2748 | n/a | DivisionByZero = decimal.DivisionByZero |
---|
2749 | n/a | Overflow = decimal.Overflow |
---|
2750 | n/a | Underflow = decimal.Underflow |
---|
2751 | n/a | Subnormal = decimal.Subnormal |
---|
2752 | n/a | Inexact = decimal.Inexact |
---|
2753 | n/a | Rounded = decimal.Rounded |
---|
2754 | n/a | Clamped = decimal.Clamped |
---|
2755 | n/a | |
---|
2756 | n/a | self.assertTrue(issubclass(DecimalException, ArithmeticError)) |
---|
2757 | n/a | |
---|
2758 | n/a | self.assertTrue(issubclass(InvalidOperation, DecimalException)) |
---|
2759 | n/a | self.assertTrue(issubclass(FloatOperation, DecimalException)) |
---|
2760 | n/a | self.assertTrue(issubclass(FloatOperation, TypeError)) |
---|
2761 | n/a | self.assertTrue(issubclass(DivisionByZero, DecimalException)) |
---|
2762 | n/a | self.assertTrue(issubclass(DivisionByZero, ZeroDivisionError)) |
---|
2763 | n/a | self.assertTrue(issubclass(Overflow, Rounded)) |
---|
2764 | n/a | self.assertTrue(issubclass(Overflow, Inexact)) |
---|
2765 | n/a | self.assertTrue(issubclass(Overflow, DecimalException)) |
---|
2766 | n/a | self.assertTrue(issubclass(Underflow, Inexact)) |
---|
2767 | n/a | self.assertTrue(issubclass(Underflow, Rounded)) |
---|
2768 | n/a | self.assertTrue(issubclass(Underflow, Subnormal)) |
---|
2769 | n/a | self.assertTrue(issubclass(Underflow, DecimalException)) |
---|
2770 | n/a | |
---|
2771 | n/a | self.assertTrue(issubclass(Subnormal, DecimalException)) |
---|
2772 | n/a | self.assertTrue(issubclass(Inexact, DecimalException)) |
---|
2773 | n/a | self.assertTrue(issubclass(Rounded, DecimalException)) |
---|
2774 | n/a | self.assertTrue(issubclass(Clamped, DecimalException)) |
---|
2775 | n/a | |
---|
2776 | n/a | self.assertTrue(issubclass(decimal.ConversionSyntax, InvalidOperation)) |
---|
2777 | n/a | self.assertTrue(issubclass(decimal.DivisionImpossible, InvalidOperation)) |
---|
2778 | n/a | self.assertTrue(issubclass(decimal.DivisionUndefined, InvalidOperation)) |
---|
2779 | n/a | self.assertTrue(issubclass(decimal.DivisionUndefined, ZeroDivisionError)) |
---|
2780 | n/a | self.assertTrue(issubclass(decimal.InvalidContext, InvalidOperation)) |
---|
2781 | n/a | |
---|
2782 | n/a | class CPythonAPItests(PythonAPItests): |
---|
2783 | n/a | decimal = C |
---|
2784 | n/a | class PyPythonAPItests(PythonAPItests): |
---|
2785 | n/a | decimal = P |
---|
2786 | n/a | |
---|
2787 | n/a | class ContextAPItests(unittest.TestCase): |
---|
2788 | n/a | |
---|
2789 | n/a | def test_none_args(self): |
---|
2790 | n/a | Context = self.decimal.Context |
---|
2791 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
2792 | n/a | DivisionByZero = self.decimal.DivisionByZero |
---|
2793 | n/a | Overflow = self.decimal.Overflow |
---|
2794 | n/a | |
---|
2795 | n/a | c1 = Context() |
---|
2796 | n/a | c2 = Context(prec=None, rounding=None, Emax=None, Emin=None, |
---|
2797 | n/a | capitals=None, clamp=None, flags=None, traps=None) |
---|
2798 | n/a | for c in [c1, c2]: |
---|
2799 | n/a | self.assertEqual(c.prec, 28) |
---|
2800 | n/a | self.assertEqual(c.rounding, ROUND_HALF_EVEN) |
---|
2801 | n/a | self.assertEqual(c.Emax, 999999) |
---|
2802 | n/a | self.assertEqual(c.Emin, -999999) |
---|
2803 | n/a | self.assertEqual(c.capitals, 1) |
---|
2804 | n/a | self.assertEqual(c.clamp, 0) |
---|
2805 | n/a | assert_signals(self, c, 'flags', []) |
---|
2806 | n/a | assert_signals(self, c, 'traps', [InvalidOperation, DivisionByZero, |
---|
2807 | n/a | Overflow]) |
---|
2808 | n/a | |
---|
2809 | n/a | @cpython_only |
---|
2810 | n/a | def test_from_legacy_strings(self): |
---|
2811 | n/a | import _testcapi |
---|
2812 | n/a | c = self.decimal.Context() |
---|
2813 | n/a | |
---|
2814 | n/a | for rnd in RoundingModes: |
---|
2815 | n/a | c.rounding = _testcapi.unicode_legacy_string(rnd) |
---|
2816 | n/a | self.assertEqual(c.rounding, rnd) |
---|
2817 | n/a | |
---|
2818 | n/a | s = _testcapi.unicode_legacy_string('') |
---|
2819 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', s) |
---|
2820 | n/a | |
---|
2821 | n/a | s = _testcapi.unicode_legacy_string('ROUND_\x00UP') |
---|
2822 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', s) |
---|
2823 | n/a | |
---|
2824 | n/a | def test_pickle(self): |
---|
2825 | n/a | |
---|
2826 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2827 | n/a | Context = self.decimal.Context |
---|
2828 | n/a | |
---|
2829 | n/a | savedecimal = sys.modules['decimal'] |
---|
2830 | n/a | |
---|
2831 | n/a | # Round trip |
---|
2832 | n/a | sys.modules['decimal'] = self.decimal |
---|
2833 | n/a | c = Context() |
---|
2834 | n/a | e = pickle.loads(pickle.dumps(c, proto)) |
---|
2835 | n/a | |
---|
2836 | n/a | self.assertEqual(c.prec, e.prec) |
---|
2837 | n/a | self.assertEqual(c.Emin, e.Emin) |
---|
2838 | n/a | self.assertEqual(c.Emax, e.Emax) |
---|
2839 | n/a | self.assertEqual(c.rounding, e.rounding) |
---|
2840 | n/a | self.assertEqual(c.capitals, e.capitals) |
---|
2841 | n/a | self.assertEqual(c.clamp, e.clamp) |
---|
2842 | n/a | self.assertEqual(c.flags, e.flags) |
---|
2843 | n/a | self.assertEqual(c.traps, e.traps) |
---|
2844 | n/a | |
---|
2845 | n/a | # Test interchangeability |
---|
2846 | n/a | combinations = [(C, P), (P, C)] if C else [(P, P)] |
---|
2847 | n/a | for dumper, loader in combinations: |
---|
2848 | n/a | for ri, _ in enumerate(RoundingModes): |
---|
2849 | n/a | for fi, _ in enumerate(OrderedSignals[dumper]): |
---|
2850 | n/a | for ti, _ in enumerate(OrderedSignals[dumper]): |
---|
2851 | n/a | |
---|
2852 | n/a | prec = random.randrange(1, 100) |
---|
2853 | n/a | emin = random.randrange(-100, 0) |
---|
2854 | n/a | emax = random.randrange(1, 100) |
---|
2855 | n/a | caps = random.randrange(2) |
---|
2856 | n/a | clamp = random.randrange(2) |
---|
2857 | n/a | |
---|
2858 | n/a | # One module dumps |
---|
2859 | n/a | sys.modules['decimal'] = dumper |
---|
2860 | n/a | c = dumper.Context( |
---|
2861 | n/a | prec=prec, Emin=emin, Emax=emax, |
---|
2862 | n/a | rounding=RoundingModes[ri], |
---|
2863 | n/a | capitals=caps, clamp=clamp, |
---|
2864 | n/a | flags=OrderedSignals[dumper][:fi], |
---|
2865 | n/a | traps=OrderedSignals[dumper][:ti] |
---|
2866 | n/a | ) |
---|
2867 | n/a | s = pickle.dumps(c, proto) |
---|
2868 | n/a | |
---|
2869 | n/a | # The other module loads |
---|
2870 | n/a | sys.modules['decimal'] = loader |
---|
2871 | n/a | d = pickle.loads(s) |
---|
2872 | n/a | self.assertIsInstance(d, loader.Context) |
---|
2873 | n/a | |
---|
2874 | n/a | self.assertEqual(d.prec, prec) |
---|
2875 | n/a | self.assertEqual(d.Emin, emin) |
---|
2876 | n/a | self.assertEqual(d.Emax, emax) |
---|
2877 | n/a | self.assertEqual(d.rounding, RoundingModes[ri]) |
---|
2878 | n/a | self.assertEqual(d.capitals, caps) |
---|
2879 | n/a | self.assertEqual(d.clamp, clamp) |
---|
2880 | n/a | assert_signals(self, d, 'flags', OrderedSignals[loader][:fi]) |
---|
2881 | n/a | assert_signals(self, d, 'traps', OrderedSignals[loader][:ti]) |
---|
2882 | n/a | |
---|
2883 | n/a | sys.modules['decimal'] = savedecimal |
---|
2884 | n/a | |
---|
2885 | n/a | def test_equality_with_other_types(self): |
---|
2886 | n/a | Decimal = self.decimal.Decimal |
---|
2887 | n/a | |
---|
2888 | n/a | self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}]) |
---|
2889 | n/a | self.assertNotIn(Decimal(10), ['a', 1.0, (1,2), {}]) |
---|
2890 | n/a | |
---|
2891 | n/a | def test_copy(self): |
---|
2892 | n/a | # All copies should be deep |
---|
2893 | n/a | Decimal = self.decimal.Decimal |
---|
2894 | n/a | Context = self.decimal.Context |
---|
2895 | n/a | |
---|
2896 | n/a | c = Context() |
---|
2897 | n/a | d = c.copy() |
---|
2898 | n/a | self.assertNotEqual(id(c), id(d)) |
---|
2899 | n/a | self.assertNotEqual(id(c.flags), id(d.flags)) |
---|
2900 | n/a | self.assertNotEqual(id(c.traps), id(d.traps)) |
---|
2901 | n/a | k1 = set(c.flags.keys()) |
---|
2902 | n/a | k2 = set(d.flags.keys()) |
---|
2903 | n/a | self.assertEqual(k1, k2) |
---|
2904 | n/a | self.assertEqual(c.flags, d.flags) |
---|
2905 | n/a | |
---|
2906 | n/a | def test__clamp(self): |
---|
2907 | n/a | # In Python 3.2, the private attribute `_clamp` was made |
---|
2908 | n/a | # public (issue 8540), with the old `_clamp` becoming a |
---|
2909 | n/a | # property wrapping `clamp`. For the duration of Python 3.2 |
---|
2910 | n/a | # only, the attribute should be gettable/settable via both |
---|
2911 | n/a | # `clamp` and `_clamp`; in Python 3.3, `_clamp` should be |
---|
2912 | n/a | # removed. |
---|
2913 | n/a | Context = self.decimal.Context |
---|
2914 | n/a | c = Context() |
---|
2915 | n/a | self.assertRaises(AttributeError, getattr, c, '_clamp') |
---|
2916 | n/a | |
---|
2917 | n/a | def test_abs(self): |
---|
2918 | n/a | Decimal = self.decimal.Decimal |
---|
2919 | n/a | Context = self.decimal.Context |
---|
2920 | n/a | |
---|
2921 | n/a | c = Context() |
---|
2922 | n/a | d = c.abs(Decimal(-1)) |
---|
2923 | n/a | self.assertEqual(c.abs(-1), d) |
---|
2924 | n/a | self.assertRaises(TypeError, c.abs, '-1') |
---|
2925 | n/a | |
---|
2926 | n/a | def test_add(self): |
---|
2927 | n/a | Decimal = self.decimal.Decimal |
---|
2928 | n/a | Context = self.decimal.Context |
---|
2929 | n/a | |
---|
2930 | n/a | c = Context() |
---|
2931 | n/a | d = c.add(Decimal(1), Decimal(1)) |
---|
2932 | n/a | self.assertEqual(c.add(1, 1), d) |
---|
2933 | n/a | self.assertEqual(c.add(Decimal(1), 1), d) |
---|
2934 | n/a | self.assertEqual(c.add(1, Decimal(1)), d) |
---|
2935 | n/a | self.assertRaises(TypeError, c.add, '1', 1) |
---|
2936 | n/a | self.assertRaises(TypeError, c.add, 1, '1') |
---|
2937 | n/a | |
---|
2938 | n/a | def test_compare(self): |
---|
2939 | n/a | Decimal = self.decimal.Decimal |
---|
2940 | n/a | Context = self.decimal.Context |
---|
2941 | n/a | |
---|
2942 | n/a | c = Context() |
---|
2943 | n/a | d = c.compare(Decimal(1), Decimal(1)) |
---|
2944 | n/a | self.assertEqual(c.compare(1, 1), d) |
---|
2945 | n/a | self.assertEqual(c.compare(Decimal(1), 1), d) |
---|
2946 | n/a | self.assertEqual(c.compare(1, Decimal(1)), d) |
---|
2947 | n/a | self.assertRaises(TypeError, c.compare, '1', 1) |
---|
2948 | n/a | self.assertRaises(TypeError, c.compare, 1, '1') |
---|
2949 | n/a | |
---|
2950 | n/a | def test_compare_signal(self): |
---|
2951 | n/a | Decimal = self.decimal.Decimal |
---|
2952 | n/a | Context = self.decimal.Context |
---|
2953 | n/a | |
---|
2954 | n/a | c = Context() |
---|
2955 | n/a | d = c.compare_signal(Decimal(1), Decimal(1)) |
---|
2956 | n/a | self.assertEqual(c.compare_signal(1, 1), d) |
---|
2957 | n/a | self.assertEqual(c.compare_signal(Decimal(1), 1), d) |
---|
2958 | n/a | self.assertEqual(c.compare_signal(1, Decimal(1)), d) |
---|
2959 | n/a | self.assertRaises(TypeError, c.compare_signal, '1', 1) |
---|
2960 | n/a | self.assertRaises(TypeError, c.compare_signal, 1, '1') |
---|
2961 | n/a | |
---|
2962 | n/a | def test_compare_total(self): |
---|
2963 | n/a | Decimal = self.decimal.Decimal |
---|
2964 | n/a | Context = self.decimal.Context |
---|
2965 | n/a | |
---|
2966 | n/a | c = Context() |
---|
2967 | n/a | d = c.compare_total(Decimal(1), Decimal(1)) |
---|
2968 | n/a | self.assertEqual(c.compare_total(1, 1), d) |
---|
2969 | n/a | self.assertEqual(c.compare_total(Decimal(1), 1), d) |
---|
2970 | n/a | self.assertEqual(c.compare_total(1, Decimal(1)), d) |
---|
2971 | n/a | self.assertRaises(TypeError, c.compare_total, '1', 1) |
---|
2972 | n/a | self.assertRaises(TypeError, c.compare_total, 1, '1') |
---|
2973 | n/a | |
---|
2974 | n/a | def test_compare_total_mag(self): |
---|
2975 | n/a | Decimal = self.decimal.Decimal |
---|
2976 | n/a | Context = self.decimal.Context |
---|
2977 | n/a | |
---|
2978 | n/a | c = Context() |
---|
2979 | n/a | d = c.compare_total_mag(Decimal(1), Decimal(1)) |
---|
2980 | n/a | self.assertEqual(c.compare_total_mag(1, 1), d) |
---|
2981 | n/a | self.assertEqual(c.compare_total_mag(Decimal(1), 1), d) |
---|
2982 | n/a | self.assertEqual(c.compare_total_mag(1, Decimal(1)), d) |
---|
2983 | n/a | self.assertRaises(TypeError, c.compare_total_mag, '1', 1) |
---|
2984 | n/a | self.assertRaises(TypeError, c.compare_total_mag, 1, '1') |
---|
2985 | n/a | |
---|
2986 | n/a | def test_copy_abs(self): |
---|
2987 | n/a | Decimal = self.decimal.Decimal |
---|
2988 | n/a | Context = self.decimal.Context |
---|
2989 | n/a | |
---|
2990 | n/a | c = Context() |
---|
2991 | n/a | d = c.copy_abs(Decimal(-1)) |
---|
2992 | n/a | self.assertEqual(c.copy_abs(-1), d) |
---|
2993 | n/a | self.assertRaises(TypeError, c.copy_abs, '-1') |
---|
2994 | n/a | |
---|
2995 | n/a | def test_copy_decimal(self): |
---|
2996 | n/a | Decimal = self.decimal.Decimal |
---|
2997 | n/a | Context = self.decimal.Context |
---|
2998 | n/a | |
---|
2999 | n/a | c = Context() |
---|
3000 | n/a | d = c.copy_decimal(Decimal(-1)) |
---|
3001 | n/a | self.assertEqual(c.copy_decimal(-1), d) |
---|
3002 | n/a | self.assertRaises(TypeError, c.copy_decimal, '-1') |
---|
3003 | n/a | |
---|
3004 | n/a | def test_copy_negate(self): |
---|
3005 | n/a | Decimal = self.decimal.Decimal |
---|
3006 | n/a | Context = self.decimal.Context |
---|
3007 | n/a | |
---|
3008 | n/a | c = Context() |
---|
3009 | n/a | d = c.copy_negate(Decimal(-1)) |
---|
3010 | n/a | self.assertEqual(c.copy_negate(-1), d) |
---|
3011 | n/a | self.assertRaises(TypeError, c.copy_negate, '-1') |
---|
3012 | n/a | |
---|
3013 | n/a | def test_copy_sign(self): |
---|
3014 | n/a | Decimal = self.decimal.Decimal |
---|
3015 | n/a | Context = self.decimal.Context |
---|
3016 | n/a | |
---|
3017 | n/a | c = Context() |
---|
3018 | n/a | d = c.copy_sign(Decimal(1), Decimal(-2)) |
---|
3019 | n/a | self.assertEqual(c.copy_sign(1, -2), d) |
---|
3020 | n/a | self.assertEqual(c.copy_sign(Decimal(1), -2), d) |
---|
3021 | n/a | self.assertEqual(c.copy_sign(1, Decimal(-2)), d) |
---|
3022 | n/a | self.assertRaises(TypeError, c.copy_sign, '1', -2) |
---|
3023 | n/a | self.assertRaises(TypeError, c.copy_sign, 1, '-2') |
---|
3024 | n/a | |
---|
3025 | n/a | def test_divide(self): |
---|
3026 | n/a | Decimal = self.decimal.Decimal |
---|
3027 | n/a | Context = self.decimal.Context |
---|
3028 | n/a | |
---|
3029 | n/a | c = Context() |
---|
3030 | n/a | d = c.divide(Decimal(1), Decimal(2)) |
---|
3031 | n/a | self.assertEqual(c.divide(1, 2), d) |
---|
3032 | n/a | self.assertEqual(c.divide(Decimal(1), 2), d) |
---|
3033 | n/a | self.assertEqual(c.divide(1, Decimal(2)), d) |
---|
3034 | n/a | self.assertRaises(TypeError, c.divide, '1', 2) |
---|
3035 | n/a | self.assertRaises(TypeError, c.divide, 1, '2') |
---|
3036 | n/a | |
---|
3037 | n/a | def test_divide_int(self): |
---|
3038 | n/a | Decimal = self.decimal.Decimal |
---|
3039 | n/a | Context = self.decimal.Context |
---|
3040 | n/a | |
---|
3041 | n/a | c = Context() |
---|
3042 | n/a | d = c.divide_int(Decimal(1), Decimal(2)) |
---|
3043 | n/a | self.assertEqual(c.divide_int(1, 2), d) |
---|
3044 | n/a | self.assertEqual(c.divide_int(Decimal(1), 2), d) |
---|
3045 | n/a | self.assertEqual(c.divide_int(1, Decimal(2)), d) |
---|
3046 | n/a | self.assertRaises(TypeError, c.divide_int, '1', 2) |
---|
3047 | n/a | self.assertRaises(TypeError, c.divide_int, 1, '2') |
---|
3048 | n/a | |
---|
3049 | n/a | def test_divmod(self): |
---|
3050 | n/a | Decimal = self.decimal.Decimal |
---|
3051 | n/a | Context = self.decimal.Context |
---|
3052 | n/a | |
---|
3053 | n/a | c = Context() |
---|
3054 | n/a | d = c.divmod(Decimal(1), Decimal(2)) |
---|
3055 | n/a | self.assertEqual(c.divmod(1, 2), d) |
---|
3056 | n/a | self.assertEqual(c.divmod(Decimal(1), 2), d) |
---|
3057 | n/a | self.assertEqual(c.divmod(1, Decimal(2)), d) |
---|
3058 | n/a | self.assertRaises(TypeError, c.divmod, '1', 2) |
---|
3059 | n/a | self.assertRaises(TypeError, c.divmod, 1, '2') |
---|
3060 | n/a | |
---|
3061 | n/a | def test_exp(self): |
---|
3062 | n/a | Decimal = self.decimal.Decimal |
---|
3063 | n/a | Context = self.decimal.Context |
---|
3064 | n/a | |
---|
3065 | n/a | c = Context() |
---|
3066 | n/a | d = c.exp(Decimal(10)) |
---|
3067 | n/a | self.assertEqual(c.exp(10), d) |
---|
3068 | n/a | self.assertRaises(TypeError, c.exp, '10') |
---|
3069 | n/a | |
---|
3070 | n/a | def test_fma(self): |
---|
3071 | n/a | Decimal = self.decimal.Decimal |
---|
3072 | n/a | Context = self.decimal.Context |
---|
3073 | n/a | |
---|
3074 | n/a | c = Context() |
---|
3075 | n/a | d = c.fma(Decimal(2), Decimal(3), Decimal(4)) |
---|
3076 | n/a | self.assertEqual(c.fma(2, 3, 4), d) |
---|
3077 | n/a | self.assertEqual(c.fma(Decimal(2), 3, 4), d) |
---|
3078 | n/a | self.assertEqual(c.fma(2, Decimal(3), 4), d) |
---|
3079 | n/a | self.assertEqual(c.fma(2, 3, Decimal(4)), d) |
---|
3080 | n/a | self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d) |
---|
3081 | n/a | self.assertRaises(TypeError, c.fma, '2', 3, 4) |
---|
3082 | n/a | self.assertRaises(TypeError, c.fma, 2, '3', 4) |
---|
3083 | n/a | self.assertRaises(TypeError, c.fma, 2, 3, '4') |
---|
3084 | n/a | |
---|
3085 | n/a | # Issue 12079 for Context.fma ... |
---|
3086 | n/a | self.assertRaises(TypeError, c.fma, |
---|
3087 | n/a | Decimal('Infinity'), Decimal(0), "not a decimal") |
---|
3088 | n/a | self.assertRaises(TypeError, c.fma, |
---|
3089 | n/a | Decimal(1), Decimal('snan'), 1.222) |
---|
3090 | n/a | # ... and for Decimal.fma. |
---|
3091 | n/a | self.assertRaises(TypeError, Decimal('Infinity').fma, |
---|
3092 | n/a | Decimal(0), "not a decimal") |
---|
3093 | n/a | self.assertRaises(TypeError, Decimal(1).fma, |
---|
3094 | n/a | Decimal('snan'), 1.222) |
---|
3095 | n/a | |
---|
3096 | n/a | def test_is_finite(self): |
---|
3097 | n/a | Decimal = self.decimal.Decimal |
---|
3098 | n/a | Context = self.decimal.Context |
---|
3099 | n/a | |
---|
3100 | n/a | c = Context() |
---|
3101 | n/a | d = c.is_finite(Decimal(10)) |
---|
3102 | n/a | self.assertEqual(c.is_finite(10), d) |
---|
3103 | n/a | self.assertRaises(TypeError, c.is_finite, '10') |
---|
3104 | n/a | |
---|
3105 | n/a | def test_is_infinite(self): |
---|
3106 | n/a | Decimal = self.decimal.Decimal |
---|
3107 | n/a | Context = self.decimal.Context |
---|
3108 | n/a | |
---|
3109 | n/a | c = Context() |
---|
3110 | n/a | d = c.is_infinite(Decimal(10)) |
---|
3111 | n/a | self.assertEqual(c.is_infinite(10), d) |
---|
3112 | n/a | self.assertRaises(TypeError, c.is_infinite, '10') |
---|
3113 | n/a | |
---|
3114 | n/a | def test_is_nan(self): |
---|
3115 | n/a | Decimal = self.decimal.Decimal |
---|
3116 | n/a | Context = self.decimal.Context |
---|
3117 | n/a | |
---|
3118 | n/a | c = Context() |
---|
3119 | n/a | d = c.is_nan(Decimal(10)) |
---|
3120 | n/a | self.assertEqual(c.is_nan(10), d) |
---|
3121 | n/a | self.assertRaises(TypeError, c.is_nan, '10') |
---|
3122 | n/a | |
---|
3123 | n/a | def test_is_normal(self): |
---|
3124 | n/a | Decimal = self.decimal.Decimal |
---|
3125 | n/a | Context = self.decimal.Context |
---|
3126 | n/a | |
---|
3127 | n/a | c = Context() |
---|
3128 | n/a | d = c.is_normal(Decimal(10)) |
---|
3129 | n/a | self.assertEqual(c.is_normal(10), d) |
---|
3130 | n/a | self.assertRaises(TypeError, c.is_normal, '10') |
---|
3131 | n/a | |
---|
3132 | n/a | def test_is_qnan(self): |
---|
3133 | n/a | Decimal = self.decimal.Decimal |
---|
3134 | n/a | Context = self.decimal.Context |
---|
3135 | n/a | |
---|
3136 | n/a | c = Context() |
---|
3137 | n/a | d = c.is_qnan(Decimal(10)) |
---|
3138 | n/a | self.assertEqual(c.is_qnan(10), d) |
---|
3139 | n/a | self.assertRaises(TypeError, c.is_qnan, '10') |
---|
3140 | n/a | |
---|
3141 | n/a | def test_is_signed(self): |
---|
3142 | n/a | Decimal = self.decimal.Decimal |
---|
3143 | n/a | Context = self.decimal.Context |
---|
3144 | n/a | |
---|
3145 | n/a | c = Context() |
---|
3146 | n/a | d = c.is_signed(Decimal(10)) |
---|
3147 | n/a | self.assertEqual(c.is_signed(10), d) |
---|
3148 | n/a | self.assertRaises(TypeError, c.is_signed, '10') |
---|
3149 | n/a | |
---|
3150 | n/a | def test_is_snan(self): |
---|
3151 | n/a | Decimal = self.decimal.Decimal |
---|
3152 | n/a | Context = self.decimal.Context |
---|
3153 | n/a | |
---|
3154 | n/a | c = Context() |
---|
3155 | n/a | d = c.is_snan(Decimal(10)) |
---|
3156 | n/a | self.assertEqual(c.is_snan(10), d) |
---|
3157 | n/a | self.assertRaises(TypeError, c.is_snan, '10') |
---|
3158 | n/a | |
---|
3159 | n/a | def test_is_subnormal(self): |
---|
3160 | n/a | Decimal = self.decimal.Decimal |
---|
3161 | n/a | Context = self.decimal.Context |
---|
3162 | n/a | |
---|
3163 | n/a | c = Context() |
---|
3164 | n/a | d = c.is_subnormal(Decimal(10)) |
---|
3165 | n/a | self.assertEqual(c.is_subnormal(10), d) |
---|
3166 | n/a | self.assertRaises(TypeError, c.is_subnormal, '10') |
---|
3167 | n/a | |
---|
3168 | n/a | def test_is_zero(self): |
---|
3169 | n/a | Decimal = self.decimal.Decimal |
---|
3170 | n/a | Context = self.decimal.Context |
---|
3171 | n/a | |
---|
3172 | n/a | c = Context() |
---|
3173 | n/a | d = c.is_zero(Decimal(10)) |
---|
3174 | n/a | self.assertEqual(c.is_zero(10), d) |
---|
3175 | n/a | self.assertRaises(TypeError, c.is_zero, '10') |
---|
3176 | n/a | |
---|
3177 | n/a | def test_ln(self): |
---|
3178 | n/a | Decimal = self.decimal.Decimal |
---|
3179 | n/a | Context = self.decimal.Context |
---|
3180 | n/a | |
---|
3181 | n/a | c = Context() |
---|
3182 | n/a | d = c.ln(Decimal(10)) |
---|
3183 | n/a | self.assertEqual(c.ln(10), d) |
---|
3184 | n/a | self.assertRaises(TypeError, c.ln, '10') |
---|
3185 | n/a | |
---|
3186 | n/a | def test_log10(self): |
---|
3187 | n/a | Decimal = self.decimal.Decimal |
---|
3188 | n/a | Context = self.decimal.Context |
---|
3189 | n/a | |
---|
3190 | n/a | c = Context() |
---|
3191 | n/a | d = c.log10(Decimal(10)) |
---|
3192 | n/a | self.assertEqual(c.log10(10), d) |
---|
3193 | n/a | self.assertRaises(TypeError, c.log10, '10') |
---|
3194 | n/a | |
---|
3195 | n/a | def test_logb(self): |
---|
3196 | n/a | Decimal = self.decimal.Decimal |
---|
3197 | n/a | Context = self.decimal.Context |
---|
3198 | n/a | |
---|
3199 | n/a | c = Context() |
---|
3200 | n/a | d = c.logb(Decimal(10)) |
---|
3201 | n/a | self.assertEqual(c.logb(10), d) |
---|
3202 | n/a | self.assertRaises(TypeError, c.logb, '10') |
---|
3203 | n/a | |
---|
3204 | n/a | def test_logical_and(self): |
---|
3205 | n/a | Decimal = self.decimal.Decimal |
---|
3206 | n/a | Context = self.decimal.Context |
---|
3207 | n/a | |
---|
3208 | n/a | c = Context() |
---|
3209 | n/a | d = c.logical_and(Decimal(1), Decimal(1)) |
---|
3210 | n/a | self.assertEqual(c.logical_and(1, 1), d) |
---|
3211 | n/a | self.assertEqual(c.logical_and(Decimal(1), 1), d) |
---|
3212 | n/a | self.assertEqual(c.logical_and(1, Decimal(1)), d) |
---|
3213 | n/a | self.assertRaises(TypeError, c.logical_and, '1', 1) |
---|
3214 | n/a | self.assertRaises(TypeError, c.logical_and, 1, '1') |
---|
3215 | n/a | |
---|
3216 | n/a | def test_logical_invert(self): |
---|
3217 | n/a | Decimal = self.decimal.Decimal |
---|
3218 | n/a | Context = self.decimal.Context |
---|
3219 | n/a | |
---|
3220 | n/a | c = Context() |
---|
3221 | n/a | d = c.logical_invert(Decimal(1000)) |
---|
3222 | n/a | self.assertEqual(c.logical_invert(1000), d) |
---|
3223 | n/a | self.assertRaises(TypeError, c.logical_invert, '1000') |
---|
3224 | n/a | |
---|
3225 | n/a | def test_logical_or(self): |
---|
3226 | n/a | Decimal = self.decimal.Decimal |
---|
3227 | n/a | Context = self.decimal.Context |
---|
3228 | n/a | |
---|
3229 | n/a | c = Context() |
---|
3230 | n/a | d = c.logical_or(Decimal(1), Decimal(1)) |
---|
3231 | n/a | self.assertEqual(c.logical_or(1, 1), d) |
---|
3232 | n/a | self.assertEqual(c.logical_or(Decimal(1), 1), d) |
---|
3233 | n/a | self.assertEqual(c.logical_or(1, Decimal(1)), d) |
---|
3234 | n/a | self.assertRaises(TypeError, c.logical_or, '1', 1) |
---|
3235 | n/a | self.assertRaises(TypeError, c.logical_or, 1, '1') |
---|
3236 | n/a | |
---|
3237 | n/a | def test_logical_xor(self): |
---|
3238 | n/a | Decimal = self.decimal.Decimal |
---|
3239 | n/a | Context = self.decimal.Context |
---|
3240 | n/a | |
---|
3241 | n/a | c = Context() |
---|
3242 | n/a | d = c.logical_xor(Decimal(1), Decimal(1)) |
---|
3243 | n/a | self.assertEqual(c.logical_xor(1, 1), d) |
---|
3244 | n/a | self.assertEqual(c.logical_xor(Decimal(1), 1), d) |
---|
3245 | n/a | self.assertEqual(c.logical_xor(1, Decimal(1)), d) |
---|
3246 | n/a | self.assertRaises(TypeError, c.logical_xor, '1', 1) |
---|
3247 | n/a | self.assertRaises(TypeError, c.logical_xor, 1, '1') |
---|
3248 | n/a | |
---|
3249 | n/a | def test_max(self): |
---|
3250 | n/a | Decimal = self.decimal.Decimal |
---|
3251 | n/a | Context = self.decimal.Context |
---|
3252 | n/a | |
---|
3253 | n/a | c = Context() |
---|
3254 | n/a | d = c.max(Decimal(1), Decimal(2)) |
---|
3255 | n/a | self.assertEqual(c.max(1, 2), d) |
---|
3256 | n/a | self.assertEqual(c.max(Decimal(1), 2), d) |
---|
3257 | n/a | self.assertEqual(c.max(1, Decimal(2)), d) |
---|
3258 | n/a | self.assertRaises(TypeError, c.max, '1', 2) |
---|
3259 | n/a | self.assertRaises(TypeError, c.max, 1, '2') |
---|
3260 | n/a | |
---|
3261 | n/a | def test_max_mag(self): |
---|
3262 | n/a | Decimal = self.decimal.Decimal |
---|
3263 | n/a | Context = self.decimal.Context |
---|
3264 | n/a | |
---|
3265 | n/a | c = Context() |
---|
3266 | n/a | d = c.max_mag(Decimal(1), Decimal(2)) |
---|
3267 | n/a | self.assertEqual(c.max_mag(1, 2), d) |
---|
3268 | n/a | self.assertEqual(c.max_mag(Decimal(1), 2), d) |
---|
3269 | n/a | self.assertEqual(c.max_mag(1, Decimal(2)), d) |
---|
3270 | n/a | self.assertRaises(TypeError, c.max_mag, '1', 2) |
---|
3271 | n/a | self.assertRaises(TypeError, c.max_mag, 1, '2') |
---|
3272 | n/a | |
---|
3273 | n/a | def test_min(self): |
---|
3274 | n/a | Decimal = self.decimal.Decimal |
---|
3275 | n/a | Context = self.decimal.Context |
---|
3276 | n/a | |
---|
3277 | n/a | c = Context() |
---|
3278 | n/a | d = c.min(Decimal(1), Decimal(2)) |
---|
3279 | n/a | self.assertEqual(c.min(1, 2), d) |
---|
3280 | n/a | self.assertEqual(c.min(Decimal(1), 2), d) |
---|
3281 | n/a | self.assertEqual(c.min(1, Decimal(2)), d) |
---|
3282 | n/a | self.assertRaises(TypeError, c.min, '1', 2) |
---|
3283 | n/a | self.assertRaises(TypeError, c.min, 1, '2') |
---|
3284 | n/a | |
---|
3285 | n/a | def test_min_mag(self): |
---|
3286 | n/a | Decimal = self.decimal.Decimal |
---|
3287 | n/a | Context = self.decimal.Context |
---|
3288 | n/a | |
---|
3289 | n/a | c = Context() |
---|
3290 | n/a | d = c.min_mag(Decimal(1), Decimal(2)) |
---|
3291 | n/a | self.assertEqual(c.min_mag(1, 2), d) |
---|
3292 | n/a | self.assertEqual(c.min_mag(Decimal(1), 2), d) |
---|
3293 | n/a | self.assertEqual(c.min_mag(1, Decimal(2)), d) |
---|
3294 | n/a | self.assertRaises(TypeError, c.min_mag, '1', 2) |
---|
3295 | n/a | self.assertRaises(TypeError, c.min_mag, 1, '2') |
---|
3296 | n/a | |
---|
3297 | n/a | def test_minus(self): |
---|
3298 | n/a | Decimal = self.decimal.Decimal |
---|
3299 | n/a | Context = self.decimal.Context |
---|
3300 | n/a | |
---|
3301 | n/a | c = Context() |
---|
3302 | n/a | d = c.minus(Decimal(10)) |
---|
3303 | n/a | self.assertEqual(c.minus(10), d) |
---|
3304 | n/a | self.assertRaises(TypeError, c.minus, '10') |
---|
3305 | n/a | |
---|
3306 | n/a | def test_multiply(self): |
---|
3307 | n/a | Decimal = self.decimal.Decimal |
---|
3308 | n/a | Context = self.decimal.Context |
---|
3309 | n/a | |
---|
3310 | n/a | c = Context() |
---|
3311 | n/a | d = c.multiply(Decimal(1), Decimal(2)) |
---|
3312 | n/a | self.assertEqual(c.multiply(1, 2), d) |
---|
3313 | n/a | self.assertEqual(c.multiply(Decimal(1), 2), d) |
---|
3314 | n/a | self.assertEqual(c.multiply(1, Decimal(2)), d) |
---|
3315 | n/a | self.assertRaises(TypeError, c.multiply, '1', 2) |
---|
3316 | n/a | self.assertRaises(TypeError, c.multiply, 1, '2') |
---|
3317 | n/a | |
---|
3318 | n/a | def test_next_minus(self): |
---|
3319 | n/a | Decimal = self.decimal.Decimal |
---|
3320 | n/a | Context = self.decimal.Context |
---|
3321 | n/a | |
---|
3322 | n/a | c = Context() |
---|
3323 | n/a | d = c.next_minus(Decimal(10)) |
---|
3324 | n/a | self.assertEqual(c.next_minus(10), d) |
---|
3325 | n/a | self.assertRaises(TypeError, c.next_minus, '10') |
---|
3326 | n/a | |
---|
3327 | n/a | def test_next_plus(self): |
---|
3328 | n/a | Decimal = self.decimal.Decimal |
---|
3329 | n/a | Context = self.decimal.Context |
---|
3330 | n/a | |
---|
3331 | n/a | c = Context() |
---|
3332 | n/a | d = c.next_plus(Decimal(10)) |
---|
3333 | n/a | self.assertEqual(c.next_plus(10), d) |
---|
3334 | n/a | self.assertRaises(TypeError, c.next_plus, '10') |
---|
3335 | n/a | |
---|
3336 | n/a | def test_next_toward(self): |
---|
3337 | n/a | Decimal = self.decimal.Decimal |
---|
3338 | n/a | Context = self.decimal.Context |
---|
3339 | n/a | |
---|
3340 | n/a | c = Context() |
---|
3341 | n/a | d = c.next_toward(Decimal(1), Decimal(2)) |
---|
3342 | n/a | self.assertEqual(c.next_toward(1, 2), d) |
---|
3343 | n/a | self.assertEqual(c.next_toward(Decimal(1), 2), d) |
---|
3344 | n/a | self.assertEqual(c.next_toward(1, Decimal(2)), d) |
---|
3345 | n/a | self.assertRaises(TypeError, c.next_toward, '1', 2) |
---|
3346 | n/a | self.assertRaises(TypeError, c.next_toward, 1, '2') |
---|
3347 | n/a | |
---|
3348 | n/a | def test_normalize(self): |
---|
3349 | n/a | Decimal = self.decimal.Decimal |
---|
3350 | n/a | Context = self.decimal.Context |
---|
3351 | n/a | |
---|
3352 | n/a | c = Context() |
---|
3353 | n/a | d = c.normalize(Decimal(10)) |
---|
3354 | n/a | self.assertEqual(c.normalize(10), d) |
---|
3355 | n/a | self.assertRaises(TypeError, c.normalize, '10') |
---|
3356 | n/a | |
---|
3357 | n/a | def test_number_class(self): |
---|
3358 | n/a | Decimal = self.decimal.Decimal |
---|
3359 | n/a | Context = self.decimal.Context |
---|
3360 | n/a | |
---|
3361 | n/a | c = Context() |
---|
3362 | n/a | self.assertEqual(c.number_class(123), c.number_class(Decimal(123))) |
---|
3363 | n/a | self.assertEqual(c.number_class(0), c.number_class(Decimal(0))) |
---|
3364 | n/a | self.assertEqual(c.number_class(-45), c.number_class(Decimal(-45))) |
---|
3365 | n/a | |
---|
3366 | n/a | def test_plus(self): |
---|
3367 | n/a | Decimal = self.decimal.Decimal |
---|
3368 | n/a | Context = self.decimal.Context |
---|
3369 | n/a | |
---|
3370 | n/a | c = Context() |
---|
3371 | n/a | d = c.plus(Decimal(10)) |
---|
3372 | n/a | self.assertEqual(c.plus(10), d) |
---|
3373 | n/a | self.assertRaises(TypeError, c.plus, '10') |
---|
3374 | n/a | |
---|
3375 | n/a | def test_power(self): |
---|
3376 | n/a | Decimal = self.decimal.Decimal |
---|
3377 | n/a | Context = self.decimal.Context |
---|
3378 | n/a | |
---|
3379 | n/a | c = Context() |
---|
3380 | n/a | d = c.power(Decimal(1), Decimal(4)) |
---|
3381 | n/a | self.assertEqual(c.power(1, 4), d) |
---|
3382 | n/a | self.assertEqual(c.power(Decimal(1), 4), d) |
---|
3383 | n/a | self.assertEqual(c.power(1, Decimal(4)), d) |
---|
3384 | n/a | self.assertEqual(c.power(Decimal(1), Decimal(4)), d) |
---|
3385 | n/a | self.assertRaises(TypeError, c.power, '1', 4) |
---|
3386 | n/a | self.assertRaises(TypeError, c.power, 1, '4') |
---|
3387 | n/a | self.assertEqual(c.power(modulo=5, b=8, a=2), 1) |
---|
3388 | n/a | |
---|
3389 | n/a | def test_quantize(self): |
---|
3390 | n/a | Decimal = self.decimal.Decimal |
---|
3391 | n/a | Context = self.decimal.Context |
---|
3392 | n/a | |
---|
3393 | n/a | c = Context() |
---|
3394 | n/a | d = c.quantize(Decimal(1), Decimal(2)) |
---|
3395 | n/a | self.assertEqual(c.quantize(1, 2), d) |
---|
3396 | n/a | self.assertEqual(c.quantize(Decimal(1), 2), d) |
---|
3397 | n/a | self.assertEqual(c.quantize(1, Decimal(2)), d) |
---|
3398 | n/a | self.assertRaises(TypeError, c.quantize, '1', 2) |
---|
3399 | n/a | self.assertRaises(TypeError, c.quantize, 1, '2') |
---|
3400 | n/a | |
---|
3401 | n/a | def test_remainder(self): |
---|
3402 | n/a | Decimal = self.decimal.Decimal |
---|
3403 | n/a | Context = self.decimal.Context |
---|
3404 | n/a | |
---|
3405 | n/a | c = Context() |
---|
3406 | n/a | d = c.remainder(Decimal(1), Decimal(2)) |
---|
3407 | n/a | self.assertEqual(c.remainder(1, 2), d) |
---|
3408 | n/a | self.assertEqual(c.remainder(Decimal(1), 2), d) |
---|
3409 | n/a | self.assertEqual(c.remainder(1, Decimal(2)), d) |
---|
3410 | n/a | self.assertRaises(TypeError, c.remainder, '1', 2) |
---|
3411 | n/a | self.assertRaises(TypeError, c.remainder, 1, '2') |
---|
3412 | n/a | |
---|
3413 | n/a | def test_remainder_near(self): |
---|
3414 | n/a | Decimal = self.decimal.Decimal |
---|
3415 | n/a | Context = self.decimal.Context |
---|
3416 | n/a | |
---|
3417 | n/a | c = Context() |
---|
3418 | n/a | d = c.remainder_near(Decimal(1), Decimal(2)) |
---|
3419 | n/a | self.assertEqual(c.remainder_near(1, 2), d) |
---|
3420 | n/a | self.assertEqual(c.remainder_near(Decimal(1), 2), d) |
---|
3421 | n/a | self.assertEqual(c.remainder_near(1, Decimal(2)), d) |
---|
3422 | n/a | self.assertRaises(TypeError, c.remainder_near, '1', 2) |
---|
3423 | n/a | self.assertRaises(TypeError, c.remainder_near, 1, '2') |
---|
3424 | n/a | |
---|
3425 | n/a | def test_rotate(self): |
---|
3426 | n/a | Decimal = self.decimal.Decimal |
---|
3427 | n/a | Context = self.decimal.Context |
---|
3428 | n/a | |
---|
3429 | n/a | c = Context() |
---|
3430 | n/a | d = c.rotate(Decimal(1), Decimal(2)) |
---|
3431 | n/a | self.assertEqual(c.rotate(1, 2), d) |
---|
3432 | n/a | self.assertEqual(c.rotate(Decimal(1), 2), d) |
---|
3433 | n/a | self.assertEqual(c.rotate(1, Decimal(2)), d) |
---|
3434 | n/a | self.assertRaises(TypeError, c.rotate, '1', 2) |
---|
3435 | n/a | self.assertRaises(TypeError, c.rotate, 1, '2') |
---|
3436 | n/a | |
---|
3437 | n/a | def test_sqrt(self): |
---|
3438 | n/a | Decimal = self.decimal.Decimal |
---|
3439 | n/a | Context = self.decimal.Context |
---|
3440 | n/a | |
---|
3441 | n/a | c = Context() |
---|
3442 | n/a | d = c.sqrt(Decimal(10)) |
---|
3443 | n/a | self.assertEqual(c.sqrt(10), d) |
---|
3444 | n/a | self.assertRaises(TypeError, c.sqrt, '10') |
---|
3445 | n/a | |
---|
3446 | n/a | def test_same_quantum(self): |
---|
3447 | n/a | Decimal = self.decimal.Decimal |
---|
3448 | n/a | Context = self.decimal.Context |
---|
3449 | n/a | |
---|
3450 | n/a | c = Context() |
---|
3451 | n/a | d = c.same_quantum(Decimal(1), Decimal(2)) |
---|
3452 | n/a | self.assertEqual(c.same_quantum(1, 2), d) |
---|
3453 | n/a | self.assertEqual(c.same_quantum(Decimal(1), 2), d) |
---|
3454 | n/a | self.assertEqual(c.same_quantum(1, Decimal(2)), d) |
---|
3455 | n/a | self.assertRaises(TypeError, c.same_quantum, '1', 2) |
---|
3456 | n/a | self.assertRaises(TypeError, c.same_quantum, 1, '2') |
---|
3457 | n/a | |
---|
3458 | n/a | def test_scaleb(self): |
---|
3459 | n/a | Decimal = self.decimal.Decimal |
---|
3460 | n/a | Context = self.decimal.Context |
---|
3461 | n/a | |
---|
3462 | n/a | c = Context() |
---|
3463 | n/a | d = c.scaleb(Decimal(1), Decimal(2)) |
---|
3464 | n/a | self.assertEqual(c.scaleb(1, 2), d) |
---|
3465 | n/a | self.assertEqual(c.scaleb(Decimal(1), 2), d) |
---|
3466 | n/a | self.assertEqual(c.scaleb(1, Decimal(2)), d) |
---|
3467 | n/a | self.assertRaises(TypeError, c.scaleb, '1', 2) |
---|
3468 | n/a | self.assertRaises(TypeError, c.scaleb, 1, '2') |
---|
3469 | n/a | |
---|
3470 | n/a | def test_shift(self): |
---|
3471 | n/a | Decimal = self.decimal.Decimal |
---|
3472 | n/a | Context = self.decimal.Context |
---|
3473 | n/a | |
---|
3474 | n/a | c = Context() |
---|
3475 | n/a | d = c.shift(Decimal(1), Decimal(2)) |
---|
3476 | n/a | self.assertEqual(c.shift(1, 2), d) |
---|
3477 | n/a | self.assertEqual(c.shift(Decimal(1), 2), d) |
---|
3478 | n/a | self.assertEqual(c.shift(1, Decimal(2)), d) |
---|
3479 | n/a | self.assertRaises(TypeError, c.shift, '1', 2) |
---|
3480 | n/a | self.assertRaises(TypeError, c.shift, 1, '2') |
---|
3481 | n/a | |
---|
3482 | n/a | def test_subtract(self): |
---|
3483 | n/a | Decimal = self.decimal.Decimal |
---|
3484 | n/a | Context = self.decimal.Context |
---|
3485 | n/a | |
---|
3486 | n/a | c = Context() |
---|
3487 | n/a | d = c.subtract(Decimal(1), Decimal(2)) |
---|
3488 | n/a | self.assertEqual(c.subtract(1, 2), d) |
---|
3489 | n/a | self.assertEqual(c.subtract(Decimal(1), 2), d) |
---|
3490 | n/a | self.assertEqual(c.subtract(1, Decimal(2)), d) |
---|
3491 | n/a | self.assertRaises(TypeError, c.subtract, '1', 2) |
---|
3492 | n/a | self.assertRaises(TypeError, c.subtract, 1, '2') |
---|
3493 | n/a | |
---|
3494 | n/a | def test_to_eng_string(self): |
---|
3495 | n/a | Decimal = self.decimal.Decimal |
---|
3496 | n/a | Context = self.decimal.Context |
---|
3497 | n/a | |
---|
3498 | n/a | c = Context() |
---|
3499 | n/a | d = c.to_eng_string(Decimal(10)) |
---|
3500 | n/a | self.assertEqual(c.to_eng_string(10), d) |
---|
3501 | n/a | self.assertRaises(TypeError, c.to_eng_string, '10') |
---|
3502 | n/a | |
---|
3503 | n/a | def test_to_sci_string(self): |
---|
3504 | n/a | Decimal = self.decimal.Decimal |
---|
3505 | n/a | Context = self.decimal.Context |
---|
3506 | n/a | |
---|
3507 | n/a | c = Context() |
---|
3508 | n/a | d = c.to_sci_string(Decimal(10)) |
---|
3509 | n/a | self.assertEqual(c.to_sci_string(10), d) |
---|
3510 | n/a | self.assertRaises(TypeError, c.to_sci_string, '10') |
---|
3511 | n/a | |
---|
3512 | n/a | def test_to_integral_exact(self): |
---|
3513 | n/a | Decimal = self.decimal.Decimal |
---|
3514 | n/a | Context = self.decimal.Context |
---|
3515 | n/a | |
---|
3516 | n/a | c = Context() |
---|
3517 | n/a | d = c.to_integral_exact(Decimal(10)) |
---|
3518 | n/a | self.assertEqual(c.to_integral_exact(10), d) |
---|
3519 | n/a | self.assertRaises(TypeError, c.to_integral_exact, '10') |
---|
3520 | n/a | |
---|
3521 | n/a | def test_to_integral_value(self): |
---|
3522 | n/a | Decimal = self.decimal.Decimal |
---|
3523 | n/a | Context = self.decimal.Context |
---|
3524 | n/a | |
---|
3525 | n/a | c = Context() |
---|
3526 | n/a | d = c.to_integral_value(Decimal(10)) |
---|
3527 | n/a | self.assertEqual(c.to_integral_value(10), d) |
---|
3528 | n/a | self.assertRaises(TypeError, c.to_integral_value, '10') |
---|
3529 | n/a | self.assertRaises(TypeError, c.to_integral_value, 10, 'x') |
---|
3530 | n/a | |
---|
3531 | n/a | class CContextAPItests(ContextAPItests): |
---|
3532 | n/a | decimal = C |
---|
3533 | n/a | class PyContextAPItests(ContextAPItests): |
---|
3534 | n/a | decimal = P |
---|
3535 | n/a | |
---|
3536 | n/a | class ContextWithStatement(unittest.TestCase): |
---|
3537 | n/a | # Can't do these as docstrings until Python 2.6 |
---|
3538 | n/a | # as doctest can't handle __future__ statements |
---|
3539 | n/a | |
---|
3540 | n/a | def test_localcontext(self): |
---|
3541 | n/a | # Use a copy of the current context in the block |
---|
3542 | n/a | getcontext = self.decimal.getcontext |
---|
3543 | n/a | localcontext = self.decimal.localcontext |
---|
3544 | n/a | |
---|
3545 | n/a | orig_ctx = getcontext() |
---|
3546 | n/a | with localcontext() as enter_ctx: |
---|
3547 | n/a | set_ctx = getcontext() |
---|
3548 | n/a | final_ctx = getcontext() |
---|
3549 | n/a | self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') |
---|
3550 | n/a | self.assertIsNot(orig_ctx, set_ctx, 'did not copy the context') |
---|
3551 | n/a | self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') |
---|
3552 | n/a | |
---|
3553 | n/a | def test_localcontextarg(self): |
---|
3554 | n/a | # Use a copy of the supplied context in the block |
---|
3555 | n/a | Context = self.decimal.Context |
---|
3556 | n/a | getcontext = self.decimal.getcontext |
---|
3557 | n/a | localcontext = self.decimal.localcontext |
---|
3558 | n/a | |
---|
3559 | n/a | localcontext = self.decimal.localcontext |
---|
3560 | n/a | orig_ctx = getcontext() |
---|
3561 | n/a | new_ctx = Context(prec=42) |
---|
3562 | n/a | with localcontext(new_ctx) as enter_ctx: |
---|
3563 | n/a | set_ctx = getcontext() |
---|
3564 | n/a | final_ctx = getcontext() |
---|
3565 | n/a | self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') |
---|
3566 | n/a | self.assertEqual(set_ctx.prec, new_ctx.prec, 'did not set correct context') |
---|
3567 | n/a | self.assertIsNot(new_ctx, set_ctx, 'did not copy the context') |
---|
3568 | n/a | self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') |
---|
3569 | n/a | |
---|
3570 | n/a | def test_nested_with_statements(self): |
---|
3571 | n/a | # Use a copy of the supplied context in the block |
---|
3572 | n/a | Decimal = self.decimal.Decimal |
---|
3573 | n/a | Context = self.decimal.Context |
---|
3574 | n/a | getcontext = self.decimal.getcontext |
---|
3575 | n/a | localcontext = self.decimal.localcontext |
---|
3576 | n/a | Clamped = self.decimal.Clamped |
---|
3577 | n/a | Overflow = self.decimal.Overflow |
---|
3578 | n/a | |
---|
3579 | n/a | orig_ctx = getcontext() |
---|
3580 | n/a | orig_ctx.clear_flags() |
---|
3581 | n/a | new_ctx = Context(Emax=384) |
---|
3582 | n/a | with localcontext() as c1: |
---|
3583 | n/a | self.assertEqual(c1.flags, orig_ctx.flags) |
---|
3584 | n/a | self.assertEqual(c1.traps, orig_ctx.traps) |
---|
3585 | n/a | c1.traps[Clamped] = True |
---|
3586 | n/a | c1.Emin = -383 |
---|
3587 | n/a | self.assertNotEqual(orig_ctx.Emin, -383) |
---|
3588 | n/a | self.assertRaises(Clamped, c1.create_decimal, '0e-999') |
---|
3589 | n/a | self.assertTrue(c1.flags[Clamped]) |
---|
3590 | n/a | with localcontext(new_ctx) as c2: |
---|
3591 | n/a | self.assertEqual(c2.flags, new_ctx.flags) |
---|
3592 | n/a | self.assertEqual(c2.traps, new_ctx.traps) |
---|
3593 | n/a | self.assertRaises(Overflow, c2.power, Decimal('3.4e200'), 2) |
---|
3594 | n/a | self.assertFalse(c2.flags[Clamped]) |
---|
3595 | n/a | self.assertTrue(c2.flags[Overflow]) |
---|
3596 | n/a | del c2 |
---|
3597 | n/a | self.assertFalse(c1.flags[Overflow]) |
---|
3598 | n/a | del c1 |
---|
3599 | n/a | self.assertNotEqual(orig_ctx.Emin, -383) |
---|
3600 | n/a | self.assertFalse(orig_ctx.flags[Clamped]) |
---|
3601 | n/a | self.assertFalse(orig_ctx.flags[Overflow]) |
---|
3602 | n/a | self.assertFalse(new_ctx.flags[Clamped]) |
---|
3603 | n/a | self.assertFalse(new_ctx.flags[Overflow]) |
---|
3604 | n/a | |
---|
3605 | n/a | def test_with_statements_gc1(self): |
---|
3606 | n/a | localcontext = self.decimal.localcontext |
---|
3607 | n/a | |
---|
3608 | n/a | with localcontext() as c1: |
---|
3609 | n/a | del c1 |
---|
3610 | n/a | with localcontext() as c2: |
---|
3611 | n/a | del c2 |
---|
3612 | n/a | with localcontext() as c3: |
---|
3613 | n/a | del c3 |
---|
3614 | n/a | with localcontext() as c4: |
---|
3615 | n/a | del c4 |
---|
3616 | n/a | |
---|
3617 | n/a | def test_with_statements_gc2(self): |
---|
3618 | n/a | localcontext = self.decimal.localcontext |
---|
3619 | n/a | |
---|
3620 | n/a | with localcontext() as c1: |
---|
3621 | n/a | with localcontext(c1) as c2: |
---|
3622 | n/a | del c1 |
---|
3623 | n/a | with localcontext(c2) as c3: |
---|
3624 | n/a | del c2 |
---|
3625 | n/a | with localcontext(c3) as c4: |
---|
3626 | n/a | del c3 |
---|
3627 | n/a | del c4 |
---|
3628 | n/a | |
---|
3629 | n/a | def test_with_statements_gc3(self): |
---|
3630 | n/a | Context = self.decimal.Context |
---|
3631 | n/a | localcontext = self.decimal.localcontext |
---|
3632 | n/a | getcontext = self.decimal.getcontext |
---|
3633 | n/a | setcontext = self.decimal.setcontext |
---|
3634 | n/a | |
---|
3635 | n/a | with localcontext() as c1: |
---|
3636 | n/a | del c1 |
---|
3637 | n/a | n1 = Context(prec=1) |
---|
3638 | n/a | setcontext(n1) |
---|
3639 | n/a | with localcontext(n1) as c2: |
---|
3640 | n/a | del n1 |
---|
3641 | n/a | self.assertEqual(c2.prec, 1) |
---|
3642 | n/a | del c2 |
---|
3643 | n/a | n2 = Context(prec=2) |
---|
3644 | n/a | setcontext(n2) |
---|
3645 | n/a | del n2 |
---|
3646 | n/a | self.assertEqual(getcontext().prec, 2) |
---|
3647 | n/a | n3 = Context(prec=3) |
---|
3648 | n/a | setcontext(n3) |
---|
3649 | n/a | self.assertEqual(getcontext().prec, 3) |
---|
3650 | n/a | with localcontext(n3) as c3: |
---|
3651 | n/a | del n3 |
---|
3652 | n/a | self.assertEqual(c3.prec, 3) |
---|
3653 | n/a | del c3 |
---|
3654 | n/a | n4 = Context(prec=4) |
---|
3655 | n/a | setcontext(n4) |
---|
3656 | n/a | del n4 |
---|
3657 | n/a | self.assertEqual(getcontext().prec, 4) |
---|
3658 | n/a | with localcontext() as c4: |
---|
3659 | n/a | self.assertEqual(c4.prec, 4) |
---|
3660 | n/a | del c4 |
---|
3661 | n/a | |
---|
3662 | n/a | class CContextWithStatement(ContextWithStatement): |
---|
3663 | n/a | decimal = C |
---|
3664 | n/a | class PyContextWithStatement(ContextWithStatement): |
---|
3665 | n/a | decimal = P |
---|
3666 | n/a | |
---|
3667 | n/a | class ContextFlags(unittest.TestCase): |
---|
3668 | n/a | |
---|
3669 | n/a | def test_flags_irrelevant(self): |
---|
3670 | n/a | # check that the result (numeric result + flags raised) of an |
---|
3671 | n/a | # arithmetic operation doesn't depend on the current flags |
---|
3672 | n/a | Decimal = self.decimal.Decimal |
---|
3673 | n/a | Context = self.decimal.Context |
---|
3674 | n/a | Inexact = self.decimal.Inexact |
---|
3675 | n/a | Rounded = self.decimal.Rounded |
---|
3676 | n/a | Underflow = self.decimal.Underflow |
---|
3677 | n/a | Clamped = self.decimal.Clamped |
---|
3678 | n/a | Subnormal = self.decimal.Subnormal |
---|
3679 | n/a | |
---|
3680 | n/a | def raise_error(context, flag): |
---|
3681 | n/a | if self.decimal == C: |
---|
3682 | n/a | context.flags[flag] = True |
---|
3683 | n/a | if context.traps[flag]: |
---|
3684 | n/a | raise flag |
---|
3685 | n/a | else: |
---|
3686 | n/a | context._raise_error(flag) |
---|
3687 | n/a | |
---|
3688 | n/a | context = Context(prec=9, Emin = -425000000, Emax = 425000000, |
---|
3689 | n/a | rounding=ROUND_HALF_EVEN, traps=[], flags=[]) |
---|
3690 | n/a | |
---|
3691 | n/a | # operations that raise various flags, in the form (function, arglist) |
---|
3692 | n/a | operations = [ |
---|
3693 | n/a | (context._apply, [Decimal("100E-425000010")]), |
---|
3694 | n/a | (context.sqrt, [Decimal(2)]), |
---|
3695 | n/a | (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]), |
---|
3696 | n/a | (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]), |
---|
3697 | n/a | (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]), |
---|
3698 | n/a | ] |
---|
3699 | n/a | |
---|
3700 | n/a | # try various flags individually, then a whole lot at once |
---|
3701 | n/a | flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal], |
---|
3702 | n/a | [Inexact, Rounded, Underflow, Clamped, Subnormal]] |
---|
3703 | n/a | |
---|
3704 | n/a | for fn, args in operations: |
---|
3705 | n/a | # find answer and flags raised using a clean context |
---|
3706 | n/a | context.clear_flags() |
---|
3707 | n/a | ans = fn(*args) |
---|
3708 | n/a | flags = [k for k, v in context.flags.items() if v] |
---|
3709 | n/a | |
---|
3710 | n/a | for extra_flags in flagsets: |
---|
3711 | n/a | # set flags, before calling operation |
---|
3712 | n/a | context.clear_flags() |
---|
3713 | n/a | for flag in extra_flags: |
---|
3714 | n/a | raise_error(context, flag) |
---|
3715 | n/a | new_ans = fn(*args) |
---|
3716 | n/a | |
---|
3717 | n/a | # flags that we expect to be set after the operation |
---|
3718 | n/a | expected_flags = list(flags) |
---|
3719 | n/a | for flag in extra_flags: |
---|
3720 | n/a | if flag not in expected_flags: |
---|
3721 | n/a | expected_flags.append(flag) |
---|
3722 | n/a | expected_flags.sort(key=id) |
---|
3723 | n/a | |
---|
3724 | n/a | # flags we actually got |
---|
3725 | n/a | new_flags = [k for k,v in context.flags.items() if v] |
---|
3726 | n/a | new_flags.sort(key=id) |
---|
3727 | n/a | |
---|
3728 | n/a | self.assertEqual(ans, new_ans, |
---|
3729 | n/a | "operation produces different answers depending on flags set: " + |
---|
3730 | n/a | "expected %s, got %s." % (ans, new_ans)) |
---|
3731 | n/a | self.assertEqual(new_flags, expected_flags, |
---|
3732 | n/a | "operation raises different flags depending on flags set: " + |
---|
3733 | n/a | "expected %s, got %s" % (expected_flags, new_flags)) |
---|
3734 | n/a | |
---|
3735 | n/a | def test_flag_comparisons(self): |
---|
3736 | n/a | Context = self.decimal.Context |
---|
3737 | n/a | Inexact = self.decimal.Inexact |
---|
3738 | n/a | Rounded = self.decimal.Rounded |
---|
3739 | n/a | |
---|
3740 | n/a | c = Context() |
---|
3741 | n/a | |
---|
3742 | n/a | # Valid SignalDict |
---|
3743 | n/a | self.assertNotEqual(c.flags, c.traps) |
---|
3744 | n/a | self.assertNotEqual(c.traps, c.flags) |
---|
3745 | n/a | |
---|
3746 | n/a | c.flags = c.traps |
---|
3747 | n/a | self.assertEqual(c.flags, c.traps) |
---|
3748 | n/a | self.assertEqual(c.traps, c.flags) |
---|
3749 | n/a | |
---|
3750 | n/a | c.flags[Rounded] = True |
---|
3751 | n/a | c.traps = c.flags |
---|
3752 | n/a | self.assertEqual(c.flags, c.traps) |
---|
3753 | n/a | self.assertEqual(c.traps, c.flags) |
---|
3754 | n/a | |
---|
3755 | n/a | d = {} |
---|
3756 | n/a | d.update(c.flags) |
---|
3757 | n/a | self.assertEqual(d, c.flags) |
---|
3758 | n/a | self.assertEqual(c.flags, d) |
---|
3759 | n/a | |
---|
3760 | n/a | d[Inexact] = True |
---|
3761 | n/a | self.assertNotEqual(d, c.flags) |
---|
3762 | n/a | self.assertNotEqual(c.flags, d) |
---|
3763 | n/a | |
---|
3764 | n/a | # Invalid SignalDict |
---|
3765 | n/a | d = {Inexact:False} |
---|
3766 | n/a | self.assertNotEqual(d, c.flags) |
---|
3767 | n/a | self.assertNotEqual(c.flags, d) |
---|
3768 | n/a | |
---|
3769 | n/a | d = ["xyz"] |
---|
3770 | n/a | self.assertNotEqual(d, c.flags) |
---|
3771 | n/a | self.assertNotEqual(c.flags, d) |
---|
3772 | n/a | |
---|
3773 | n/a | @requires_IEEE_754 |
---|
3774 | n/a | def test_float_operation(self): |
---|
3775 | n/a | Decimal = self.decimal.Decimal |
---|
3776 | n/a | FloatOperation = self.decimal.FloatOperation |
---|
3777 | n/a | localcontext = self.decimal.localcontext |
---|
3778 | n/a | |
---|
3779 | n/a | with localcontext() as c: |
---|
3780 | n/a | ##### trap is off by default |
---|
3781 | n/a | self.assertFalse(c.traps[FloatOperation]) |
---|
3782 | n/a | |
---|
3783 | n/a | # implicit conversion sets the flag |
---|
3784 | n/a | c.clear_flags() |
---|
3785 | n/a | self.assertEqual(Decimal(7.5), 7.5) |
---|
3786 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3787 | n/a | |
---|
3788 | n/a | c.clear_flags() |
---|
3789 | n/a | self.assertEqual(c.create_decimal(7.5), 7.5) |
---|
3790 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3791 | n/a | |
---|
3792 | n/a | # explicit conversion does not set the flag |
---|
3793 | n/a | c.clear_flags() |
---|
3794 | n/a | x = Decimal.from_float(7.5) |
---|
3795 | n/a | self.assertFalse(c.flags[FloatOperation]) |
---|
3796 | n/a | # comparison sets the flag |
---|
3797 | n/a | self.assertEqual(x, 7.5) |
---|
3798 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3799 | n/a | |
---|
3800 | n/a | c.clear_flags() |
---|
3801 | n/a | x = c.create_decimal_from_float(7.5) |
---|
3802 | n/a | self.assertFalse(c.flags[FloatOperation]) |
---|
3803 | n/a | self.assertEqual(x, 7.5) |
---|
3804 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3805 | n/a | |
---|
3806 | n/a | ##### set the trap |
---|
3807 | n/a | c.traps[FloatOperation] = True |
---|
3808 | n/a | |
---|
3809 | n/a | # implicit conversion raises |
---|
3810 | n/a | c.clear_flags() |
---|
3811 | n/a | self.assertRaises(FloatOperation, Decimal, 7.5) |
---|
3812 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3813 | n/a | |
---|
3814 | n/a | c.clear_flags() |
---|
3815 | n/a | self.assertRaises(FloatOperation, c.create_decimal, 7.5) |
---|
3816 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3817 | n/a | |
---|
3818 | n/a | # explicit conversion is silent |
---|
3819 | n/a | c.clear_flags() |
---|
3820 | n/a | x = Decimal.from_float(7.5) |
---|
3821 | n/a | self.assertFalse(c.flags[FloatOperation]) |
---|
3822 | n/a | |
---|
3823 | n/a | c.clear_flags() |
---|
3824 | n/a | x = c.create_decimal_from_float(7.5) |
---|
3825 | n/a | self.assertFalse(c.flags[FloatOperation]) |
---|
3826 | n/a | |
---|
3827 | n/a | def test_float_comparison(self): |
---|
3828 | n/a | Decimal = self.decimal.Decimal |
---|
3829 | n/a | Context = self.decimal.Context |
---|
3830 | n/a | FloatOperation = self.decimal.FloatOperation |
---|
3831 | n/a | localcontext = self.decimal.localcontext |
---|
3832 | n/a | |
---|
3833 | n/a | def assert_attr(a, b, attr, context, signal=None): |
---|
3834 | n/a | context.clear_flags() |
---|
3835 | n/a | f = getattr(a, attr) |
---|
3836 | n/a | if signal == FloatOperation: |
---|
3837 | n/a | self.assertRaises(signal, f, b) |
---|
3838 | n/a | else: |
---|
3839 | n/a | self.assertIs(f(b), True) |
---|
3840 | n/a | self.assertTrue(context.flags[FloatOperation]) |
---|
3841 | n/a | |
---|
3842 | n/a | small_d = Decimal('0.25') |
---|
3843 | n/a | big_d = Decimal('3.0') |
---|
3844 | n/a | small_f = 0.25 |
---|
3845 | n/a | big_f = 3.0 |
---|
3846 | n/a | |
---|
3847 | n/a | zero_d = Decimal('0.0') |
---|
3848 | n/a | neg_zero_d = Decimal('-0.0') |
---|
3849 | n/a | zero_f = 0.0 |
---|
3850 | n/a | neg_zero_f = -0.0 |
---|
3851 | n/a | |
---|
3852 | n/a | inf_d = Decimal('Infinity') |
---|
3853 | n/a | neg_inf_d = Decimal('-Infinity') |
---|
3854 | n/a | inf_f = float('inf') |
---|
3855 | n/a | neg_inf_f = float('-inf') |
---|
3856 | n/a | |
---|
3857 | n/a | def doit(c, signal=None): |
---|
3858 | n/a | # Order |
---|
3859 | n/a | for attr in '__lt__', '__le__': |
---|
3860 | n/a | assert_attr(small_d, big_f, attr, c, signal) |
---|
3861 | n/a | |
---|
3862 | n/a | for attr in '__gt__', '__ge__': |
---|
3863 | n/a | assert_attr(big_d, small_f, attr, c, signal) |
---|
3864 | n/a | |
---|
3865 | n/a | # Equality |
---|
3866 | n/a | assert_attr(small_d, small_f, '__eq__', c, None) |
---|
3867 | n/a | |
---|
3868 | n/a | assert_attr(neg_zero_d, neg_zero_f, '__eq__', c, None) |
---|
3869 | n/a | assert_attr(neg_zero_d, zero_f, '__eq__', c, None) |
---|
3870 | n/a | |
---|
3871 | n/a | assert_attr(zero_d, neg_zero_f, '__eq__', c, None) |
---|
3872 | n/a | assert_attr(zero_d, zero_f, '__eq__', c, None) |
---|
3873 | n/a | |
---|
3874 | n/a | assert_attr(neg_inf_d, neg_inf_f, '__eq__', c, None) |
---|
3875 | n/a | assert_attr(inf_d, inf_f, '__eq__', c, None) |
---|
3876 | n/a | |
---|
3877 | n/a | # Inequality |
---|
3878 | n/a | assert_attr(small_d, big_f, '__ne__', c, None) |
---|
3879 | n/a | |
---|
3880 | n/a | assert_attr(Decimal('0.1'), 0.1, '__ne__', c, None) |
---|
3881 | n/a | |
---|
3882 | n/a | assert_attr(neg_inf_d, inf_f, '__ne__', c, None) |
---|
3883 | n/a | assert_attr(inf_d, neg_inf_f, '__ne__', c, None) |
---|
3884 | n/a | |
---|
3885 | n/a | assert_attr(Decimal('NaN'), float('nan'), '__ne__', c, None) |
---|
3886 | n/a | |
---|
3887 | n/a | def test_containers(c, signal=None): |
---|
3888 | n/a | c.clear_flags() |
---|
3889 | n/a | s = set([100.0, Decimal('100.0')]) |
---|
3890 | n/a | self.assertEqual(len(s), 1) |
---|
3891 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3892 | n/a | |
---|
3893 | n/a | c.clear_flags() |
---|
3894 | n/a | if signal: |
---|
3895 | n/a | self.assertRaises(signal, sorted, [1.0, Decimal('10.0')]) |
---|
3896 | n/a | else: |
---|
3897 | n/a | s = sorted([10.0, Decimal('10.0')]) |
---|
3898 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3899 | n/a | |
---|
3900 | n/a | c.clear_flags() |
---|
3901 | n/a | b = 10.0 in [Decimal('10.0'), 1.0] |
---|
3902 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3903 | n/a | |
---|
3904 | n/a | c.clear_flags() |
---|
3905 | n/a | b = 10.0 in {Decimal('10.0'):'a', 1.0:'b'} |
---|
3906 | n/a | self.assertTrue(c.flags[FloatOperation]) |
---|
3907 | n/a | |
---|
3908 | n/a | nc = Context() |
---|
3909 | n/a | with localcontext(nc) as c: |
---|
3910 | n/a | self.assertFalse(c.traps[FloatOperation]) |
---|
3911 | n/a | doit(c, signal=None) |
---|
3912 | n/a | test_containers(c, signal=None) |
---|
3913 | n/a | |
---|
3914 | n/a | c.traps[FloatOperation] = True |
---|
3915 | n/a | doit(c, signal=FloatOperation) |
---|
3916 | n/a | test_containers(c, signal=FloatOperation) |
---|
3917 | n/a | |
---|
3918 | n/a | def test_float_operation_default(self): |
---|
3919 | n/a | Decimal = self.decimal.Decimal |
---|
3920 | n/a | Context = self.decimal.Context |
---|
3921 | n/a | Inexact = self.decimal.Inexact |
---|
3922 | n/a | FloatOperation= self.decimal.FloatOperation |
---|
3923 | n/a | |
---|
3924 | n/a | context = Context() |
---|
3925 | n/a | self.assertFalse(context.flags[FloatOperation]) |
---|
3926 | n/a | self.assertFalse(context.traps[FloatOperation]) |
---|
3927 | n/a | |
---|
3928 | n/a | context.clear_traps() |
---|
3929 | n/a | context.traps[Inexact] = True |
---|
3930 | n/a | context.traps[FloatOperation] = True |
---|
3931 | n/a | self.assertTrue(context.traps[FloatOperation]) |
---|
3932 | n/a | self.assertTrue(context.traps[Inexact]) |
---|
3933 | n/a | |
---|
3934 | n/a | class CContextFlags(ContextFlags): |
---|
3935 | n/a | decimal = C |
---|
3936 | n/a | class PyContextFlags(ContextFlags): |
---|
3937 | n/a | decimal = P |
---|
3938 | n/a | |
---|
3939 | n/a | class SpecialContexts(unittest.TestCase): |
---|
3940 | n/a | """Test the context templates.""" |
---|
3941 | n/a | |
---|
3942 | n/a | def test_context_templates(self): |
---|
3943 | n/a | BasicContext = self.decimal.BasicContext |
---|
3944 | n/a | ExtendedContext = self.decimal.ExtendedContext |
---|
3945 | n/a | getcontext = self.decimal.getcontext |
---|
3946 | n/a | setcontext = self.decimal.setcontext |
---|
3947 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
3948 | n/a | DivisionByZero = self.decimal.DivisionByZero |
---|
3949 | n/a | Overflow = self.decimal.Overflow |
---|
3950 | n/a | Underflow = self.decimal.Underflow |
---|
3951 | n/a | Clamped = self.decimal.Clamped |
---|
3952 | n/a | |
---|
3953 | n/a | assert_signals(self, BasicContext, 'traps', |
---|
3954 | n/a | [InvalidOperation, DivisionByZero, Overflow, Underflow, Clamped] |
---|
3955 | n/a | ) |
---|
3956 | n/a | |
---|
3957 | n/a | savecontext = getcontext().copy() |
---|
3958 | n/a | basic_context_prec = BasicContext.prec |
---|
3959 | n/a | extended_context_prec = ExtendedContext.prec |
---|
3960 | n/a | |
---|
3961 | n/a | ex = None |
---|
3962 | n/a | try: |
---|
3963 | n/a | BasicContext.prec = ExtendedContext.prec = 441 |
---|
3964 | n/a | for template in BasicContext, ExtendedContext: |
---|
3965 | n/a | setcontext(template) |
---|
3966 | n/a | c = getcontext() |
---|
3967 | n/a | self.assertIsNot(c, template) |
---|
3968 | n/a | self.assertEqual(c.prec, 441) |
---|
3969 | n/a | except Exception as e: |
---|
3970 | n/a | ex = e.__class__ |
---|
3971 | n/a | finally: |
---|
3972 | n/a | BasicContext.prec = basic_context_prec |
---|
3973 | n/a | ExtendedContext.prec = extended_context_prec |
---|
3974 | n/a | setcontext(savecontext) |
---|
3975 | n/a | if ex: |
---|
3976 | n/a | raise ex |
---|
3977 | n/a | |
---|
3978 | n/a | def test_default_context(self): |
---|
3979 | n/a | DefaultContext = self.decimal.DefaultContext |
---|
3980 | n/a | BasicContext = self.decimal.BasicContext |
---|
3981 | n/a | ExtendedContext = self.decimal.ExtendedContext |
---|
3982 | n/a | getcontext = self.decimal.getcontext |
---|
3983 | n/a | setcontext = self.decimal.setcontext |
---|
3984 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
3985 | n/a | DivisionByZero = self.decimal.DivisionByZero |
---|
3986 | n/a | Overflow = self.decimal.Overflow |
---|
3987 | n/a | |
---|
3988 | n/a | self.assertEqual(BasicContext.prec, 9) |
---|
3989 | n/a | self.assertEqual(ExtendedContext.prec, 9) |
---|
3990 | n/a | |
---|
3991 | n/a | assert_signals(self, DefaultContext, 'traps', |
---|
3992 | n/a | [InvalidOperation, DivisionByZero, Overflow] |
---|
3993 | n/a | ) |
---|
3994 | n/a | |
---|
3995 | n/a | savecontext = getcontext().copy() |
---|
3996 | n/a | default_context_prec = DefaultContext.prec |
---|
3997 | n/a | |
---|
3998 | n/a | ex = None |
---|
3999 | n/a | try: |
---|
4000 | n/a | c = getcontext() |
---|
4001 | n/a | saveprec = c.prec |
---|
4002 | n/a | |
---|
4003 | n/a | DefaultContext.prec = 961 |
---|
4004 | n/a | c = getcontext() |
---|
4005 | n/a | self.assertEqual(c.prec, saveprec) |
---|
4006 | n/a | |
---|
4007 | n/a | setcontext(DefaultContext) |
---|
4008 | n/a | c = getcontext() |
---|
4009 | n/a | self.assertIsNot(c, DefaultContext) |
---|
4010 | n/a | self.assertEqual(c.prec, 961) |
---|
4011 | n/a | except Exception as e: |
---|
4012 | n/a | ex = e.__class__ |
---|
4013 | n/a | finally: |
---|
4014 | n/a | DefaultContext.prec = default_context_prec |
---|
4015 | n/a | setcontext(savecontext) |
---|
4016 | n/a | if ex: |
---|
4017 | n/a | raise ex |
---|
4018 | n/a | |
---|
4019 | n/a | class CSpecialContexts(SpecialContexts): |
---|
4020 | n/a | decimal = C |
---|
4021 | n/a | class PySpecialContexts(SpecialContexts): |
---|
4022 | n/a | decimal = P |
---|
4023 | n/a | |
---|
4024 | n/a | class ContextInputValidation(unittest.TestCase): |
---|
4025 | n/a | |
---|
4026 | n/a | def test_invalid_context(self): |
---|
4027 | n/a | Context = self.decimal.Context |
---|
4028 | n/a | DefaultContext = self.decimal.DefaultContext |
---|
4029 | n/a | |
---|
4030 | n/a | c = DefaultContext.copy() |
---|
4031 | n/a | |
---|
4032 | n/a | # prec, Emax |
---|
4033 | n/a | for attr in ['prec', 'Emax']: |
---|
4034 | n/a | setattr(c, attr, 999999) |
---|
4035 | n/a | self.assertEqual(getattr(c, attr), 999999) |
---|
4036 | n/a | self.assertRaises(ValueError, setattr, c, attr, -1) |
---|
4037 | n/a | self.assertRaises(TypeError, setattr, c, attr, 'xyz') |
---|
4038 | n/a | |
---|
4039 | n/a | # Emin |
---|
4040 | n/a | setattr(c, 'Emin', -999999) |
---|
4041 | n/a | self.assertEqual(getattr(c, 'Emin'), -999999) |
---|
4042 | n/a | self.assertRaises(ValueError, setattr, c, 'Emin', 1) |
---|
4043 | n/a | self.assertRaises(TypeError, setattr, c, 'Emin', (1,2,3)) |
---|
4044 | n/a | |
---|
4045 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', -1) |
---|
4046 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', 9) |
---|
4047 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', 1.0) |
---|
4048 | n/a | self.assertRaises(TypeError, setattr, c, 'rounding', 'xyz') |
---|
4049 | n/a | |
---|
4050 | n/a | # capitals, clamp |
---|
4051 | n/a | for attr in ['capitals', 'clamp']: |
---|
4052 | n/a | self.assertRaises(ValueError, setattr, c, attr, -1) |
---|
4053 | n/a | self.assertRaises(ValueError, setattr, c, attr, 2) |
---|
4054 | n/a | self.assertRaises(TypeError, setattr, c, attr, [1,2,3]) |
---|
4055 | n/a | |
---|
4056 | n/a | # Invalid attribute |
---|
4057 | n/a | self.assertRaises(AttributeError, setattr, c, 'emax', 100) |
---|
4058 | n/a | |
---|
4059 | n/a | # Invalid signal dict |
---|
4060 | n/a | self.assertRaises(TypeError, setattr, c, 'flags', []) |
---|
4061 | n/a | self.assertRaises(KeyError, setattr, c, 'flags', {}) |
---|
4062 | n/a | self.assertRaises(KeyError, setattr, c, 'traps', |
---|
4063 | n/a | {'InvalidOperation':0}) |
---|
4064 | n/a | |
---|
4065 | n/a | # Attributes cannot be deleted |
---|
4066 | n/a | for attr in ['prec', 'Emax', 'Emin', 'rounding', 'capitals', 'clamp', |
---|
4067 | n/a | 'flags', 'traps']: |
---|
4068 | n/a | self.assertRaises(AttributeError, c.__delattr__, attr) |
---|
4069 | n/a | |
---|
4070 | n/a | # Invalid attributes |
---|
4071 | n/a | self.assertRaises(TypeError, getattr, c, 9) |
---|
4072 | n/a | self.assertRaises(TypeError, setattr, c, 9) |
---|
4073 | n/a | |
---|
4074 | n/a | # Invalid values in constructor |
---|
4075 | n/a | self.assertRaises(TypeError, Context, rounding=999999) |
---|
4076 | n/a | self.assertRaises(TypeError, Context, rounding='xyz') |
---|
4077 | n/a | self.assertRaises(ValueError, Context, clamp=2) |
---|
4078 | n/a | self.assertRaises(ValueError, Context, capitals=-1) |
---|
4079 | n/a | self.assertRaises(KeyError, Context, flags=["P"]) |
---|
4080 | n/a | self.assertRaises(KeyError, Context, traps=["Q"]) |
---|
4081 | n/a | |
---|
4082 | n/a | # Type error in conversion |
---|
4083 | n/a | self.assertRaises(TypeError, Context, flags=(0,1)) |
---|
4084 | n/a | self.assertRaises(TypeError, Context, traps=(1,0)) |
---|
4085 | n/a | |
---|
4086 | n/a | class CContextInputValidation(ContextInputValidation): |
---|
4087 | n/a | decimal = C |
---|
4088 | n/a | class PyContextInputValidation(ContextInputValidation): |
---|
4089 | n/a | decimal = P |
---|
4090 | n/a | |
---|
4091 | n/a | class ContextSubclassing(unittest.TestCase): |
---|
4092 | n/a | |
---|
4093 | n/a | def test_context_subclassing(self): |
---|
4094 | n/a | decimal = self.decimal |
---|
4095 | n/a | Decimal = decimal.Decimal |
---|
4096 | n/a | Context = decimal.Context |
---|
4097 | n/a | Clamped = decimal.Clamped |
---|
4098 | n/a | DivisionByZero = decimal.DivisionByZero |
---|
4099 | n/a | Inexact = decimal.Inexact |
---|
4100 | n/a | Overflow = decimal.Overflow |
---|
4101 | n/a | Rounded = decimal.Rounded |
---|
4102 | n/a | Subnormal = decimal.Subnormal |
---|
4103 | n/a | Underflow = decimal.Underflow |
---|
4104 | n/a | InvalidOperation = decimal.InvalidOperation |
---|
4105 | n/a | |
---|
4106 | n/a | class MyContext(Context): |
---|
4107 | n/a | def __init__(self, prec=None, rounding=None, Emin=None, Emax=None, |
---|
4108 | n/a | capitals=None, clamp=None, flags=None, |
---|
4109 | n/a | traps=None): |
---|
4110 | n/a | Context.__init__(self) |
---|
4111 | n/a | if prec is not None: |
---|
4112 | n/a | self.prec = prec |
---|
4113 | n/a | if rounding is not None: |
---|
4114 | n/a | self.rounding = rounding |
---|
4115 | n/a | if Emin is not None: |
---|
4116 | n/a | self.Emin = Emin |
---|
4117 | n/a | if Emax is not None: |
---|
4118 | n/a | self.Emax = Emax |
---|
4119 | n/a | if capitals is not None: |
---|
4120 | n/a | self.capitals = capitals |
---|
4121 | n/a | if clamp is not None: |
---|
4122 | n/a | self.clamp = clamp |
---|
4123 | n/a | if flags is not None: |
---|
4124 | n/a | if isinstance(flags, list): |
---|
4125 | n/a | flags = {v:(v in flags) for v in OrderedSignals[decimal] + flags} |
---|
4126 | n/a | self.flags = flags |
---|
4127 | n/a | if traps is not None: |
---|
4128 | n/a | if isinstance(traps, list): |
---|
4129 | n/a | traps = {v:(v in traps) for v in OrderedSignals[decimal] + traps} |
---|
4130 | n/a | self.traps = traps |
---|
4131 | n/a | |
---|
4132 | n/a | c = Context() |
---|
4133 | n/a | d = MyContext() |
---|
4134 | n/a | for attr in ('prec', 'rounding', 'Emin', 'Emax', 'capitals', 'clamp', |
---|
4135 | n/a | 'flags', 'traps'): |
---|
4136 | n/a | self.assertEqual(getattr(c, attr), getattr(d, attr)) |
---|
4137 | n/a | |
---|
4138 | n/a | # prec |
---|
4139 | n/a | self.assertRaises(ValueError, MyContext, **{'prec':-1}) |
---|
4140 | n/a | c = MyContext(prec=1) |
---|
4141 | n/a | self.assertEqual(c.prec, 1) |
---|
4142 | n/a | self.assertRaises(InvalidOperation, c.quantize, Decimal('9e2'), 0) |
---|
4143 | n/a | |
---|
4144 | n/a | # rounding |
---|
4145 | n/a | self.assertRaises(TypeError, MyContext, **{'rounding':'XYZ'}) |
---|
4146 | n/a | c = MyContext(rounding=ROUND_DOWN, prec=1) |
---|
4147 | n/a | self.assertEqual(c.rounding, ROUND_DOWN) |
---|
4148 | n/a | self.assertEqual(c.plus(Decimal('9.9')), 9) |
---|
4149 | n/a | |
---|
4150 | n/a | # Emin |
---|
4151 | n/a | self.assertRaises(ValueError, MyContext, **{'Emin':5}) |
---|
4152 | n/a | c = MyContext(Emin=-1, prec=1) |
---|
4153 | n/a | self.assertEqual(c.Emin, -1) |
---|
4154 | n/a | x = c.add(Decimal('1e-99'), Decimal('2.234e-2000')) |
---|
4155 | n/a | self.assertEqual(x, Decimal('0.0')) |
---|
4156 | n/a | for signal in (Inexact, Underflow, Subnormal, Rounded, Clamped): |
---|
4157 | n/a | self.assertTrue(c.flags[signal]) |
---|
4158 | n/a | |
---|
4159 | n/a | # Emax |
---|
4160 | n/a | self.assertRaises(ValueError, MyContext, **{'Emax':-1}) |
---|
4161 | n/a | c = MyContext(Emax=1, prec=1) |
---|
4162 | n/a | self.assertEqual(c.Emax, 1) |
---|
4163 | n/a | self.assertRaises(Overflow, c.add, Decimal('1e99'), Decimal('2.234e2000')) |
---|
4164 | n/a | if self.decimal == C: |
---|
4165 | n/a | for signal in (Inexact, Overflow, Rounded): |
---|
4166 | n/a | self.assertTrue(c.flags[signal]) |
---|
4167 | n/a | |
---|
4168 | n/a | # capitals |
---|
4169 | n/a | self.assertRaises(ValueError, MyContext, **{'capitals':-1}) |
---|
4170 | n/a | c = MyContext(capitals=0) |
---|
4171 | n/a | self.assertEqual(c.capitals, 0) |
---|
4172 | n/a | x = c.create_decimal('1E222') |
---|
4173 | n/a | self.assertEqual(c.to_sci_string(x), '1e+222') |
---|
4174 | n/a | |
---|
4175 | n/a | # clamp |
---|
4176 | n/a | self.assertRaises(ValueError, MyContext, **{'clamp':2}) |
---|
4177 | n/a | c = MyContext(clamp=1, Emax=99) |
---|
4178 | n/a | self.assertEqual(c.clamp, 1) |
---|
4179 | n/a | x = c.plus(Decimal('1e99')) |
---|
4180 | n/a | self.assertEqual(str(x), '1.000000000000000000000000000E+99') |
---|
4181 | n/a | |
---|
4182 | n/a | # flags |
---|
4183 | n/a | self.assertRaises(TypeError, MyContext, **{'flags':'XYZ'}) |
---|
4184 | n/a | c = MyContext(flags=[Rounded, DivisionByZero]) |
---|
4185 | n/a | for signal in (Rounded, DivisionByZero): |
---|
4186 | n/a | self.assertTrue(c.flags[signal]) |
---|
4187 | n/a | c.clear_flags() |
---|
4188 | n/a | for signal in OrderedSignals[decimal]: |
---|
4189 | n/a | self.assertFalse(c.flags[signal]) |
---|
4190 | n/a | |
---|
4191 | n/a | # traps |
---|
4192 | n/a | self.assertRaises(TypeError, MyContext, **{'traps':'XYZ'}) |
---|
4193 | n/a | c = MyContext(traps=[Rounded, DivisionByZero]) |
---|
4194 | n/a | for signal in (Rounded, DivisionByZero): |
---|
4195 | n/a | self.assertTrue(c.traps[signal]) |
---|
4196 | n/a | c.clear_traps() |
---|
4197 | n/a | for signal in OrderedSignals[decimal]: |
---|
4198 | n/a | self.assertFalse(c.traps[signal]) |
---|
4199 | n/a | |
---|
4200 | n/a | class CContextSubclassing(ContextSubclassing): |
---|
4201 | n/a | decimal = C |
---|
4202 | n/a | class PyContextSubclassing(ContextSubclassing): |
---|
4203 | n/a | decimal = P |
---|
4204 | n/a | |
---|
4205 | n/a | @skip_if_extra_functionality |
---|
4206 | n/a | class CheckAttributes(unittest.TestCase): |
---|
4207 | n/a | |
---|
4208 | n/a | def test_module_attributes(self): |
---|
4209 | n/a | |
---|
4210 | n/a | # Architecture dependent context limits |
---|
4211 | n/a | self.assertEqual(C.MAX_PREC, P.MAX_PREC) |
---|
4212 | n/a | self.assertEqual(C.MAX_EMAX, P.MAX_EMAX) |
---|
4213 | n/a | self.assertEqual(C.MIN_EMIN, P.MIN_EMIN) |
---|
4214 | n/a | self.assertEqual(C.MIN_ETINY, P.MIN_ETINY) |
---|
4215 | n/a | |
---|
4216 | n/a | self.assertTrue(C.HAVE_THREADS is True or C.HAVE_THREADS is False) |
---|
4217 | n/a | self.assertTrue(P.HAVE_THREADS is True or P.HAVE_THREADS is False) |
---|
4218 | n/a | |
---|
4219 | n/a | self.assertEqual(C.__version__, P.__version__) |
---|
4220 | n/a | |
---|
4221 | n/a | self.assertEqual(dir(C), dir(P)) |
---|
4222 | n/a | |
---|
4223 | n/a | def test_context_attributes(self): |
---|
4224 | n/a | |
---|
4225 | n/a | x = [s for s in dir(C.Context()) if '__' in s or not s.startswith('_')] |
---|
4226 | n/a | y = [s for s in dir(P.Context()) if '__' in s or not s.startswith('_')] |
---|
4227 | n/a | self.assertEqual(set(x) - set(y), set()) |
---|
4228 | n/a | |
---|
4229 | n/a | def test_decimal_attributes(self): |
---|
4230 | n/a | |
---|
4231 | n/a | x = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')] |
---|
4232 | n/a | y = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')] |
---|
4233 | n/a | self.assertEqual(set(x) - set(y), set()) |
---|
4234 | n/a | |
---|
4235 | n/a | class Coverage(unittest.TestCase): |
---|
4236 | n/a | |
---|
4237 | n/a | def test_adjusted(self): |
---|
4238 | n/a | Decimal = self.decimal.Decimal |
---|
4239 | n/a | |
---|
4240 | n/a | self.assertEqual(Decimal('1234e9999').adjusted(), 10002) |
---|
4241 | n/a | # XXX raise? |
---|
4242 | n/a | self.assertEqual(Decimal('nan').adjusted(), 0) |
---|
4243 | n/a | self.assertEqual(Decimal('inf').adjusted(), 0) |
---|
4244 | n/a | |
---|
4245 | n/a | def test_canonical(self): |
---|
4246 | n/a | Decimal = self.decimal.Decimal |
---|
4247 | n/a | getcontext = self.decimal.getcontext |
---|
4248 | n/a | |
---|
4249 | n/a | x = Decimal(9).canonical() |
---|
4250 | n/a | self.assertEqual(x, 9) |
---|
4251 | n/a | |
---|
4252 | n/a | c = getcontext() |
---|
4253 | n/a | x = c.canonical(Decimal(9)) |
---|
4254 | n/a | self.assertEqual(x, 9) |
---|
4255 | n/a | |
---|
4256 | n/a | def test_context_repr(self): |
---|
4257 | n/a | c = self.decimal.DefaultContext.copy() |
---|
4258 | n/a | |
---|
4259 | n/a | c.prec = 425000000 |
---|
4260 | n/a | c.Emax = 425000000 |
---|
4261 | n/a | c.Emin = -425000000 |
---|
4262 | n/a | c.rounding = ROUND_HALF_DOWN |
---|
4263 | n/a | c.capitals = 0 |
---|
4264 | n/a | c.clamp = 1 |
---|
4265 | n/a | for sig in OrderedSignals[self.decimal]: |
---|
4266 | n/a | c.flags[sig] = False |
---|
4267 | n/a | c.traps[sig] = False |
---|
4268 | n/a | |
---|
4269 | n/a | s = c.__repr__() |
---|
4270 | n/a | t = "Context(prec=425000000, rounding=ROUND_HALF_DOWN, " \ |
---|
4271 | n/a | "Emin=-425000000, Emax=425000000, capitals=0, clamp=1, " \ |
---|
4272 | n/a | "flags=[], traps=[])" |
---|
4273 | n/a | self.assertEqual(s, t) |
---|
4274 | n/a | |
---|
4275 | n/a | def test_implicit_context(self): |
---|
4276 | n/a | Decimal = self.decimal.Decimal |
---|
4277 | n/a | localcontext = self.decimal.localcontext |
---|
4278 | n/a | |
---|
4279 | n/a | with localcontext() as c: |
---|
4280 | n/a | c.prec = 1 |
---|
4281 | n/a | c.Emax = 1 |
---|
4282 | n/a | c.Emin = -1 |
---|
4283 | n/a | |
---|
4284 | n/a | # abs |
---|
4285 | n/a | self.assertEqual(abs(Decimal("-10")), 10) |
---|
4286 | n/a | # add |
---|
4287 | n/a | self.assertEqual(Decimal("7") + 1, 8) |
---|
4288 | n/a | # divide |
---|
4289 | n/a | self.assertEqual(Decimal("10") / 5, 2) |
---|
4290 | n/a | # divide_int |
---|
4291 | n/a | self.assertEqual(Decimal("10") // 7, 1) |
---|
4292 | n/a | # fma |
---|
4293 | n/a | self.assertEqual(Decimal("1.2").fma(Decimal("0.01"), 1), 1) |
---|
4294 | n/a | self.assertIs(Decimal("NaN").fma(7, 1).is_nan(), True) |
---|
4295 | n/a | # three arg power |
---|
4296 | n/a | self.assertEqual(pow(Decimal(10), 2, 7), 2) |
---|
4297 | n/a | # exp |
---|
4298 | n/a | self.assertEqual(Decimal("1.01").exp(), 3) |
---|
4299 | n/a | # is_normal |
---|
4300 | n/a | self.assertIs(Decimal("0.01").is_normal(), False) |
---|
4301 | n/a | # is_subnormal |
---|
4302 | n/a | self.assertIs(Decimal("0.01").is_subnormal(), True) |
---|
4303 | n/a | # ln |
---|
4304 | n/a | self.assertEqual(Decimal("20").ln(), 3) |
---|
4305 | n/a | # log10 |
---|
4306 | n/a | self.assertEqual(Decimal("20").log10(), 1) |
---|
4307 | n/a | # logb |
---|
4308 | n/a | self.assertEqual(Decimal("580").logb(), 2) |
---|
4309 | n/a | # logical_invert |
---|
4310 | n/a | self.assertEqual(Decimal("10").logical_invert(), 1) |
---|
4311 | n/a | # minus |
---|
4312 | n/a | self.assertEqual(-Decimal("-10"), 10) |
---|
4313 | n/a | # multiply |
---|
4314 | n/a | self.assertEqual(Decimal("2") * 4, 8) |
---|
4315 | n/a | # next_minus |
---|
4316 | n/a | self.assertEqual(Decimal("10").next_minus(), 9) |
---|
4317 | n/a | # next_plus |
---|
4318 | n/a | self.assertEqual(Decimal("10").next_plus(), Decimal('2E+1')) |
---|
4319 | n/a | # normalize |
---|
4320 | n/a | self.assertEqual(Decimal("-10").normalize(), Decimal('-1E+1')) |
---|
4321 | n/a | # number_class |
---|
4322 | n/a | self.assertEqual(Decimal("10").number_class(), '+Normal') |
---|
4323 | n/a | # plus |
---|
4324 | n/a | self.assertEqual(+Decimal("-1"), -1) |
---|
4325 | n/a | # remainder |
---|
4326 | n/a | self.assertEqual(Decimal("10") % 7, 3) |
---|
4327 | n/a | # subtract |
---|
4328 | n/a | self.assertEqual(Decimal("10") - 7, 3) |
---|
4329 | n/a | # to_integral_exact |
---|
4330 | n/a | self.assertEqual(Decimal("1.12345").to_integral_exact(), 1) |
---|
4331 | n/a | |
---|
4332 | n/a | # Boolean functions |
---|
4333 | n/a | self.assertTrue(Decimal("1").is_canonical()) |
---|
4334 | n/a | self.assertTrue(Decimal("1").is_finite()) |
---|
4335 | n/a | self.assertTrue(Decimal("1").is_finite()) |
---|
4336 | n/a | self.assertTrue(Decimal("snan").is_snan()) |
---|
4337 | n/a | self.assertTrue(Decimal("-1").is_signed()) |
---|
4338 | n/a | self.assertTrue(Decimal("0").is_zero()) |
---|
4339 | n/a | self.assertTrue(Decimal("0").is_zero()) |
---|
4340 | n/a | |
---|
4341 | n/a | # Copy |
---|
4342 | n/a | with localcontext() as c: |
---|
4343 | n/a | c.prec = 10000 |
---|
4344 | n/a | x = 1228 ** 1523 |
---|
4345 | n/a | y = -Decimal(x) |
---|
4346 | n/a | |
---|
4347 | n/a | z = y.copy_abs() |
---|
4348 | n/a | self.assertEqual(z, x) |
---|
4349 | n/a | |
---|
4350 | n/a | z = y.copy_negate() |
---|
4351 | n/a | self.assertEqual(z, x) |
---|
4352 | n/a | |
---|
4353 | n/a | z = y.copy_sign(Decimal(1)) |
---|
4354 | n/a | self.assertEqual(z, x) |
---|
4355 | n/a | |
---|
4356 | n/a | def test_divmod(self): |
---|
4357 | n/a | Decimal = self.decimal.Decimal |
---|
4358 | n/a | localcontext = self.decimal.localcontext |
---|
4359 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
4360 | n/a | DivisionByZero = self.decimal.DivisionByZero |
---|
4361 | n/a | |
---|
4362 | n/a | with localcontext() as c: |
---|
4363 | n/a | q, r = divmod(Decimal("10912837129"), 1001) |
---|
4364 | n/a | self.assertEqual(q, Decimal('10901935')) |
---|
4365 | n/a | self.assertEqual(r, Decimal('194')) |
---|
4366 | n/a | |
---|
4367 | n/a | q, r = divmod(Decimal("NaN"), 7) |
---|
4368 | n/a | self.assertTrue(q.is_nan() and r.is_nan()) |
---|
4369 | n/a | |
---|
4370 | n/a | c.traps[InvalidOperation] = False |
---|
4371 | n/a | q, r = divmod(Decimal("NaN"), 7) |
---|
4372 | n/a | self.assertTrue(q.is_nan() and r.is_nan()) |
---|
4373 | n/a | |
---|
4374 | n/a | c.traps[InvalidOperation] = False |
---|
4375 | n/a | c.clear_flags() |
---|
4376 | n/a | q, r = divmod(Decimal("inf"), Decimal("inf")) |
---|
4377 | n/a | self.assertTrue(q.is_nan() and r.is_nan()) |
---|
4378 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
4379 | n/a | |
---|
4380 | n/a | c.clear_flags() |
---|
4381 | n/a | q, r = divmod(Decimal("inf"), 101) |
---|
4382 | n/a | self.assertTrue(q.is_infinite() and r.is_nan()) |
---|
4383 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
4384 | n/a | |
---|
4385 | n/a | c.clear_flags() |
---|
4386 | n/a | q, r = divmod(Decimal(0), 0) |
---|
4387 | n/a | self.assertTrue(q.is_nan() and r.is_nan()) |
---|
4388 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
4389 | n/a | |
---|
4390 | n/a | c.traps[DivisionByZero] = False |
---|
4391 | n/a | c.clear_flags() |
---|
4392 | n/a | q, r = divmod(Decimal(11), 0) |
---|
4393 | n/a | self.assertTrue(q.is_infinite() and r.is_nan()) |
---|
4394 | n/a | self.assertTrue(c.flags[InvalidOperation] and |
---|
4395 | n/a | c.flags[DivisionByZero]) |
---|
4396 | n/a | |
---|
4397 | n/a | def test_power(self): |
---|
4398 | n/a | Decimal = self.decimal.Decimal |
---|
4399 | n/a | localcontext = self.decimal.localcontext |
---|
4400 | n/a | Overflow = self.decimal.Overflow |
---|
4401 | n/a | Rounded = self.decimal.Rounded |
---|
4402 | n/a | |
---|
4403 | n/a | with localcontext() as c: |
---|
4404 | n/a | c.prec = 3 |
---|
4405 | n/a | c.clear_flags() |
---|
4406 | n/a | self.assertEqual(Decimal("1.0") ** 100, Decimal('1.00')) |
---|
4407 | n/a | self.assertTrue(c.flags[Rounded]) |
---|
4408 | n/a | |
---|
4409 | n/a | c.prec = 1 |
---|
4410 | n/a | c.Emax = 1 |
---|
4411 | n/a | c.Emin = -1 |
---|
4412 | n/a | c.clear_flags() |
---|
4413 | n/a | c.traps[Overflow] = False |
---|
4414 | n/a | self.assertEqual(Decimal(10000) ** Decimal("0.5"), Decimal('inf')) |
---|
4415 | n/a | self.assertTrue(c.flags[Overflow]) |
---|
4416 | n/a | |
---|
4417 | n/a | def test_quantize(self): |
---|
4418 | n/a | Decimal = self.decimal.Decimal |
---|
4419 | n/a | localcontext = self.decimal.localcontext |
---|
4420 | n/a | InvalidOperation = self.decimal.InvalidOperation |
---|
4421 | n/a | |
---|
4422 | n/a | with localcontext() as c: |
---|
4423 | n/a | c.prec = 1 |
---|
4424 | n/a | c.Emax = 1 |
---|
4425 | n/a | c.Emin = -1 |
---|
4426 | n/a | c.traps[InvalidOperation] = False |
---|
4427 | n/a | x = Decimal(99).quantize(Decimal("1e1")) |
---|
4428 | n/a | self.assertTrue(x.is_nan()) |
---|
4429 | n/a | |
---|
4430 | n/a | def test_radix(self): |
---|
4431 | n/a | Decimal = self.decimal.Decimal |
---|
4432 | n/a | getcontext = self.decimal.getcontext |
---|
4433 | n/a | |
---|
4434 | n/a | c = getcontext() |
---|
4435 | n/a | self.assertEqual(Decimal("1").radix(), 10) |
---|
4436 | n/a | self.assertEqual(c.radix(), 10) |
---|
4437 | n/a | |
---|
4438 | n/a | def test_rop(self): |
---|
4439 | n/a | Decimal = self.decimal.Decimal |
---|
4440 | n/a | |
---|
4441 | n/a | for attr in ('__radd__', '__rsub__', '__rmul__', '__rtruediv__', |
---|
4442 | n/a | '__rdivmod__', '__rmod__', '__rfloordiv__', '__rpow__'): |
---|
4443 | n/a | self.assertIs(getattr(Decimal("1"), attr)("xyz"), NotImplemented) |
---|
4444 | n/a | |
---|
4445 | n/a | def test_round(self): |
---|
4446 | n/a | # Python3 behavior: round() returns Decimal |
---|
4447 | n/a | Decimal = self.decimal.Decimal |
---|
4448 | n/a | getcontext = self.decimal.getcontext |
---|
4449 | n/a | |
---|
4450 | n/a | c = getcontext() |
---|
4451 | n/a | c.prec = 28 |
---|
4452 | n/a | |
---|
4453 | n/a | self.assertEqual(str(Decimal("9.99").__round__()), "10") |
---|
4454 | n/a | self.assertEqual(str(Decimal("9.99e-5").__round__()), "0") |
---|
4455 | n/a | self.assertEqual(str(Decimal("1.23456789").__round__(5)), "1.23457") |
---|
4456 | n/a | self.assertEqual(str(Decimal("1.2345").__round__(10)), "1.2345000000") |
---|
4457 | n/a | self.assertEqual(str(Decimal("1.2345").__round__(-10)), "0E+10") |
---|
4458 | n/a | |
---|
4459 | n/a | self.assertRaises(TypeError, Decimal("1.23").__round__, "5") |
---|
4460 | n/a | self.assertRaises(TypeError, Decimal("1.23").__round__, 5, 8) |
---|
4461 | n/a | |
---|
4462 | n/a | def test_create_decimal(self): |
---|
4463 | n/a | c = self.decimal.Context() |
---|
4464 | n/a | self.assertRaises(ValueError, c.create_decimal, ["%"]) |
---|
4465 | n/a | |
---|
4466 | n/a | def test_int(self): |
---|
4467 | n/a | Decimal = self.decimal.Decimal |
---|
4468 | n/a | localcontext = self.decimal.localcontext |
---|
4469 | n/a | |
---|
4470 | n/a | with localcontext() as c: |
---|
4471 | n/a | c.prec = 9999 |
---|
4472 | n/a | x = Decimal(1221**1271) / 10**3923 |
---|
4473 | n/a | self.assertEqual(int(x), 1) |
---|
4474 | n/a | self.assertEqual(x.to_integral(), 2) |
---|
4475 | n/a | |
---|
4476 | n/a | def test_copy(self): |
---|
4477 | n/a | Context = self.decimal.Context |
---|
4478 | n/a | |
---|
4479 | n/a | c = Context() |
---|
4480 | n/a | c.prec = 10000 |
---|
4481 | n/a | x = -(1172 ** 1712) |
---|
4482 | n/a | |
---|
4483 | n/a | y = c.copy_abs(x) |
---|
4484 | n/a | self.assertEqual(y, -x) |
---|
4485 | n/a | |
---|
4486 | n/a | y = c.copy_negate(x) |
---|
4487 | n/a | self.assertEqual(y, -x) |
---|
4488 | n/a | |
---|
4489 | n/a | y = c.copy_sign(x, 1) |
---|
4490 | n/a | self.assertEqual(y, -x) |
---|
4491 | n/a | |
---|
4492 | n/a | class CCoverage(Coverage): |
---|
4493 | n/a | decimal = C |
---|
4494 | n/a | class PyCoverage(Coverage): |
---|
4495 | n/a | decimal = P |
---|
4496 | n/a | |
---|
4497 | n/a | class PyFunctionality(unittest.TestCase): |
---|
4498 | n/a | """Extra functionality in decimal.py""" |
---|
4499 | n/a | |
---|
4500 | n/a | def test_py_alternate_formatting(self): |
---|
4501 | n/a | # triples giving a format, a Decimal, and the expected result |
---|
4502 | n/a | Decimal = P.Decimal |
---|
4503 | n/a | localcontext = P.localcontext |
---|
4504 | n/a | |
---|
4505 | n/a | test_values = [ |
---|
4506 | n/a | # Issue 7094: Alternate formatting (specified by #) |
---|
4507 | n/a | ('.0e', '1.0', '1e+0'), |
---|
4508 | n/a | ('#.0e', '1.0', '1.e+0'), |
---|
4509 | n/a | ('.0f', '1.0', '1'), |
---|
4510 | n/a | ('#.0f', '1.0', '1.'), |
---|
4511 | n/a | ('g', '1.1', '1.1'), |
---|
4512 | n/a | ('#g', '1.1', '1.1'), |
---|
4513 | n/a | ('.0g', '1', '1'), |
---|
4514 | n/a | ('#.0g', '1', '1.'), |
---|
4515 | n/a | ('.0%', '1.0', '100%'), |
---|
4516 | n/a | ('#.0%', '1.0', '100.%'), |
---|
4517 | n/a | ] |
---|
4518 | n/a | for fmt, d, result in test_values: |
---|
4519 | n/a | self.assertEqual(format(Decimal(d), fmt), result) |
---|
4520 | n/a | |
---|
4521 | n/a | class PyWhitebox(unittest.TestCase): |
---|
4522 | n/a | """White box testing for decimal.py""" |
---|
4523 | n/a | |
---|
4524 | n/a | def test_py_exact_power(self): |
---|
4525 | n/a | # Rarely exercised lines in _power_exact. |
---|
4526 | n/a | Decimal = P.Decimal |
---|
4527 | n/a | localcontext = P.localcontext |
---|
4528 | n/a | |
---|
4529 | n/a | with localcontext() as c: |
---|
4530 | n/a | c.prec = 8 |
---|
4531 | n/a | x = Decimal(2**16) ** Decimal("-0.5") |
---|
4532 | n/a | self.assertEqual(x, Decimal('0.00390625')) |
---|
4533 | n/a | |
---|
4534 | n/a | x = Decimal(2**16) ** Decimal("-0.6") |
---|
4535 | n/a | self.assertEqual(x, Decimal('0.0012885819')) |
---|
4536 | n/a | |
---|
4537 | n/a | x = Decimal("256e7") ** Decimal("-0.5") |
---|
4538 | n/a | |
---|
4539 | n/a | x = Decimal(152587890625) ** Decimal('-0.0625') |
---|
4540 | n/a | self.assertEqual(x, Decimal("0.2")) |
---|
4541 | n/a | |
---|
4542 | n/a | x = Decimal("152587890625e7") ** Decimal('-0.0625') |
---|
4543 | n/a | |
---|
4544 | n/a | x = Decimal(5**2659) ** Decimal('-0.0625') |
---|
4545 | n/a | |
---|
4546 | n/a | c.prec = 1 |
---|
4547 | n/a | x = Decimal("152587890625") ** Decimal('-0.5') |
---|
4548 | n/a | c.prec = 201 |
---|
4549 | n/a | x = Decimal(2**578) ** Decimal("-0.5") |
---|
4550 | n/a | |
---|
4551 | n/a | def test_py_immutability_operations(self): |
---|
4552 | n/a | # Do operations and check that it didn't change internal objects. |
---|
4553 | n/a | Decimal = P.Decimal |
---|
4554 | n/a | DefaultContext = P.DefaultContext |
---|
4555 | n/a | setcontext = P.setcontext |
---|
4556 | n/a | |
---|
4557 | n/a | c = DefaultContext.copy() |
---|
4558 | n/a | c.traps = dict((s, 0) for s in OrderedSignals[P]) |
---|
4559 | n/a | setcontext(c) |
---|
4560 | n/a | |
---|
4561 | n/a | d1 = Decimal('-25e55') |
---|
4562 | n/a | b1 = Decimal('-25e55') |
---|
4563 | n/a | d2 = Decimal('33e+33') |
---|
4564 | n/a | b2 = Decimal('33e+33') |
---|
4565 | n/a | |
---|
4566 | n/a | def checkSameDec(operation, useOther=False): |
---|
4567 | n/a | if useOther: |
---|
4568 | n/a | eval("d1." + operation + "(d2)") |
---|
4569 | n/a | self.assertEqual(d1._sign, b1._sign) |
---|
4570 | n/a | self.assertEqual(d1._int, b1._int) |
---|
4571 | n/a | self.assertEqual(d1._exp, b1._exp) |
---|
4572 | n/a | self.assertEqual(d2._sign, b2._sign) |
---|
4573 | n/a | self.assertEqual(d2._int, b2._int) |
---|
4574 | n/a | self.assertEqual(d2._exp, b2._exp) |
---|
4575 | n/a | else: |
---|
4576 | n/a | eval("d1." + operation + "()") |
---|
4577 | n/a | self.assertEqual(d1._sign, b1._sign) |
---|
4578 | n/a | self.assertEqual(d1._int, b1._int) |
---|
4579 | n/a | self.assertEqual(d1._exp, b1._exp) |
---|
4580 | n/a | |
---|
4581 | n/a | Decimal(d1) |
---|
4582 | n/a | self.assertEqual(d1._sign, b1._sign) |
---|
4583 | n/a | self.assertEqual(d1._int, b1._int) |
---|
4584 | n/a | self.assertEqual(d1._exp, b1._exp) |
---|
4585 | n/a | |
---|
4586 | n/a | checkSameDec("__abs__") |
---|
4587 | n/a | checkSameDec("__add__", True) |
---|
4588 | n/a | checkSameDec("__divmod__", True) |
---|
4589 | n/a | checkSameDec("__eq__", True) |
---|
4590 | n/a | checkSameDec("__ne__", True) |
---|
4591 | n/a | checkSameDec("__le__", True) |
---|
4592 | n/a | checkSameDec("__lt__", True) |
---|
4593 | n/a | checkSameDec("__ge__", True) |
---|
4594 | n/a | checkSameDec("__gt__", True) |
---|
4595 | n/a | checkSameDec("__float__") |
---|
4596 | n/a | checkSameDec("__floordiv__", True) |
---|
4597 | n/a | checkSameDec("__hash__") |
---|
4598 | n/a | checkSameDec("__int__") |
---|
4599 | n/a | checkSameDec("__trunc__") |
---|
4600 | n/a | checkSameDec("__mod__", True) |
---|
4601 | n/a | checkSameDec("__mul__", True) |
---|
4602 | n/a | checkSameDec("__neg__") |
---|
4603 | n/a | checkSameDec("__bool__") |
---|
4604 | n/a | checkSameDec("__pos__") |
---|
4605 | n/a | checkSameDec("__pow__", True) |
---|
4606 | n/a | checkSameDec("__radd__", True) |
---|
4607 | n/a | checkSameDec("__rdivmod__", True) |
---|
4608 | n/a | checkSameDec("__repr__") |
---|
4609 | n/a | checkSameDec("__rfloordiv__", True) |
---|
4610 | n/a | checkSameDec("__rmod__", True) |
---|
4611 | n/a | checkSameDec("__rmul__", True) |
---|
4612 | n/a | checkSameDec("__rpow__", True) |
---|
4613 | n/a | checkSameDec("__rsub__", True) |
---|
4614 | n/a | checkSameDec("__str__") |
---|
4615 | n/a | checkSameDec("__sub__", True) |
---|
4616 | n/a | checkSameDec("__truediv__", True) |
---|
4617 | n/a | checkSameDec("adjusted") |
---|
4618 | n/a | checkSameDec("as_tuple") |
---|
4619 | n/a | checkSameDec("compare", True) |
---|
4620 | n/a | checkSameDec("max", True) |
---|
4621 | n/a | checkSameDec("min", True) |
---|
4622 | n/a | checkSameDec("normalize") |
---|
4623 | n/a | checkSameDec("quantize", True) |
---|
4624 | n/a | checkSameDec("remainder_near", True) |
---|
4625 | n/a | checkSameDec("same_quantum", True) |
---|
4626 | n/a | checkSameDec("sqrt") |
---|
4627 | n/a | checkSameDec("to_eng_string") |
---|
4628 | n/a | checkSameDec("to_integral") |
---|
4629 | n/a | |
---|
4630 | n/a | def test_py_decimal_id(self): |
---|
4631 | n/a | Decimal = P.Decimal |
---|
4632 | n/a | |
---|
4633 | n/a | d = Decimal(45) |
---|
4634 | n/a | e = Decimal(d) |
---|
4635 | n/a | self.assertEqual(str(e), '45') |
---|
4636 | n/a | self.assertNotEqual(id(d), id(e)) |
---|
4637 | n/a | |
---|
4638 | n/a | def test_py_rescale(self): |
---|
4639 | n/a | # Coverage |
---|
4640 | n/a | Decimal = P.Decimal |
---|
4641 | n/a | localcontext = P.localcontext |
---|
4642 | n/a | |
---|
4643 | n/a | with localcontext() as c: |
---|
4644 | n/a | x = Decimal("NaN")._rescale(3, ROUND_UP) |
---|
4645 | n/a | self.assertTrue(x.is_nan()) |
---|
4646 | n/a | |
---|
4647 | n/a | def test_py__round(self): |
---|
4648 | n/a | # Coverage |
---|
4649 | n/a | Decimal = P.Decimal |
---|
4650 | n/a | |
---|
4651 | n/a | self.assertRaises(ValueError, Decimal("3.1234")._round, 0, ROUND_UP) |
---|
4652 | n/a | |
---|
4653 | n/a | class CFunctionality(unittest.TestCase): |
---|
4654 | n/a | """Extra functionality in _decimal""" |
---|
4655 | n/a | |
---|
4656 | n/a | @requires_extra_functionality |
---|
4657 | n/a | def test_c_ieee_context(self): |
---|
4658 | n/a | # issue 8786: Add support for IEEE 754 contexts to decimal module. |
---|
4659 | n/a | IEEEContext = C.IEEEContext |
---|
4660 | n/a | DECIMAL32 = C.DECIMAL32 |
---|
4661 | n/a | DECIMAL64 = C.DECIMAL64 |
---|
4662 | n/a | DECIMAL128 = C.DECIMAL128 |
---|
4663 | n/a | |
---|
4664 | n/a | def assert_rest(self, context): |
---|
4665 | n/a | self.assertEqual(context.clamp, 1) |
---|
4666 | n/a | assert_signals(self, context, 'traps', []) |
---|
4667 | n/a | assert_signals(self, context, 'flags', []) |
---|
4668 | n/a | |
---|
4669 | n/a | c = IEEEContext(DECIMAL32) |
---|
4670 | n/a | self.assertEqual(c.prec, 7) |
---|
4671 | n/a | self.assertEqual(c.Emax, 96) |
---|
4672 | n/a | self.assertEqual(c.Emin, -95) |
---|
4673 | n/a | assert_rest(self, c) |
---|
4674 | n/a | |
---|
4675 | n/a | c = IEEEContext(DECIMAL64) |
---|
4676 | n/a | self.assertEqual(c.prec, 16) |
---|
4677 | n/a | self.assertEqual(c.Emax, 384) |
---|
4678 | n/a | self.assertEqual(c.Emin, -383) |
---|
4679 | n/a | assert_rest(self, c) |
---|
4680 | n/a | |
---|
4681 | n/a | c = IEEEContext(DECIMAL128) |
---|
4682 | n/a | self.assertEqual(c.prec, 34) |
---|
4683 | n/a | self.assertEqual(c.Emax, 6144) |
---|
4684 | n/a | self.assertEqual(c.Emin, -6143) |
---|
4685 | n/a | assert_rest(self, c) |
---|
4686 | n/a | |
---|
4687 | n/a | # Invalid values |
---|
4688 | n/a | self.assertRaises(OverflowError, IEEEContext, 2**63) |
---|
4689 | n/a | self.assertRaises(ValueError, IEEEContext, -1) |
---|
4690 | n/a | self.assertRaises(ValueError, IEEEContext, 1024) |
---|
4691 | n/a | |
---|
4692 | n/a | @requires_extra_functionality |
---|
4693 | n/a | def test_c_context(self): |
---|
4694 | n/a | Context = C.Context |
---|
4695 | n/a | |
---|
4696 | n/a | c = Context(flags=C.DecClamped, traps=C.DecRounded) |
---|
4697 | n/a | self.assertEqual(c._flags, C.DecClamped) |
---|
4698 | n/a | self.assertEqual(c._traps, C.DecRounded) |
---|
4699 | n/a | |
---|
4700 | n/a | @requires_extra_functionality |
---|
4701 | n/a | def test_constants(self): |
---|
4702 | n/a | # Condition flags |
---|
4703 | n/a | cond = ( |
---|
4704 | n/a | C.DecClamped, C.DecConversionSyntax, C.DecDivisionByZero, |
---|
4705 | n/a | C.DecDivisionImpossible, C.DecDivisionUndefined, |
---|
4706 | n/a | C.DecFpuError, C.DecInexact, C.DecInvalidContext, |
---|
4707 | n/a | C.DecInvalidOperation, C.DecMallocError, |
---|
4708 | n/a | C.DecFloatOperation, C.DecOverflow, C.DecRounded, |
---|
4709 | n/a | C.DecSubnormal, C.DecUnderflow |
---|
4710 | n/a | ) |
---|
4711 | n/a | |
---|
4712 | n/a | # IEEEContext |
---|
4713 | n/a | self.assertEqual(C.DECIMAL32, 32) |
---|
4714 | n/a | self.assertEqual(C.DECIMAL64, 64) |
---|
4715 | n/a | self.assertEqual(C.DECIMAL128, 128) |
---|
4716 | n/a | self.assertEqual(C.IEEE_CONTEXT_MAX_BITS, 512) |
---|
4717 | n/a | |
---|
4718 | n/a | # Conditions |
---|
4719 | n/a | for i, v in enumerate(cond): |
---|
4720 | n/a | self.assertEqual(v, 1<<i) |
---|
4721 | n/a | |
---|
4722 | n/a | self.assertEqual(C.DecIEEEInvalidOperation, |
---|
4723 | n/a | C.DecConversionSyntax| |
---|
4724 | n/a | C.DecDivisionImpossible| |
---|
4725 | n/a | C.DecDivisionUndefined| |
---|
4726 | n/a | C.DecFpuError| |
---|
4727 | n/a | C.DecInvalidContext| |
---|
4728 | n/a | C.DecInvalidOperation| |
---|
4729 | n/a | C.DecMallocError) |
---|
4730 | n/a | |
---|
4731 | n/a | self.assertEqual(C.DecErrors, |
---|
4732 | n/a | C.DecIEEEInvalidOperation| |
---|
4733 | n/a | C.DecDivisionByZero) |
---|
4734 | n/a | |
---|
4735 | n/a | self.assertEqual(C.DecTraps, |
---|
4736 | n/a | C.DecErrors|C.DecOverflow|C.DecUnderflow) |
---|
4737 | n/a | |
---|
4738 | n/a | class CWhitebox(unittest.TestCase): |
---|
4739 | n/a | """Whitebox testing for _decimal""" |
---|
4740 | n/a | |
---|
4741 | n/a | def test_bignum(self): |
---|
4742 | n/a | # Not exactly whitebox, but too slow with pydecimal. |
---|
4743 | n/a | |
---|
4744 | n/a | Decimal = C.Decimal |
---|
4745 | n/a | localcontext = C.localcontext |
---|
4746 | n/a | |
---|
4747 | n/a | b1 = 10**35 |
---|
4748 | n/a | b2 = 10**36 |
---|
4749 | n/a | with localcontext() as c: |
---|
4750 | n/a | c.prec = 1000000 |
---|
4751 | n/a | for i in range(5): |
---|
4752 | n/a | a = random.randrange(b1, b2) |
---|
4753 | n/a | b = random.randrange(1000, 1200) |
---|
4754 | n/a | x = a ** b |
---|
4755 | n/a | y = Decimal(a) ** Decimal(b) |
---|
4756 | n/a | self.assertEqual(x, y) |
---|
4757 | n/a | |
---|
4758 | n/a | def test_invalid_construction(self): |
---|
4759 | n/a | self.assertRaises(TypeError, C.Decimal, 9, "xyz") |
---|
4760 | n/a | |
---|
4761 | n/a | def test_c_input_restriction(self): |
---|
4762 | n/a | # Too large for _decimal to be converted exactly |
---|
4763 | n/a | Decimal = C.Decimal |
---|
4764 | n/a | InvalidOperation = C.InvalidOperation |
---|
4765 | n/a | Context = C.Context |
---|
4766 | n/a | localcontext = C.localcontext |
---|
4767 | n/a | |
---|
4768 | n/a | with localcontext(Context()): |
---|
4769 | n/a | self.assertRaises(InvalidOperation, Decimal, |
---|
4770 | n/a | "1e9999999999999999999") |
---|
4771 | n/a | |
---|
4772 | n/a | def test_c_context_repr(self): |
---|
4773 | n/a | # This test is _decimal-only because flags are not printed |
---|
4774 | n/a | # in the same order. |
---|
4775 | n/a | DefaultContext = C.DefaultContext |
---|
4776 | n/a | FloatOperation = C.FloatOperation |
---|
4777 | n/a | |
---|
4778 | n/a | c = DefaultContext.copy() |
---|
4779 | n/a | |
---|
4780 | n/a | c.prec = 425000000 |
---|
4781 | n/a | c.Emax = 425000000 |
---|
4782 | n/a | c.Emin = -425000000 |
---|
4783 | n/a | c.rounding = ROUND_HALF_DOWN |
---|
4784 | n/a | c.capitals = 0 |
---|
4785 | n/a | c.clamp = 1 |
---|
4786 | n/a | for sig in OrderedSignals[C]: |
---|
4787 | n/a | c.flags[sig] = True |
---|
4788 | n/a | c.traps[sig] = True |
---|
4789 | n/a | c.flags[FloatOperation] = True |
---|
4790 | n/a | c.traps[FloatOperation] = True |
---|
4791 | n/a | |
---|
4792 | n/a | s = c.__repr__() |
---|
4793 | n/a | t = "Context(prec=425000000, rounding=ROUND_HALF_DOWN, " \ |
---|
4794 | n/a | "Emin=-425000000, Emax=425000000, capitals=0, clamp=1, " \ |
---|
4795 | n/a | "flags=[Clamped, InvalidOperation, DivisionByZero, Inexact, " \ |
---|
4796 | n/a | "FloatOperation, Overflow, Rounded, Subnormal, Underflow], " \ |
---|
4797 | n/a | "traps=[Clamped, InvalidOperation, DivisionByZero, Inexact, " \ |
---|
4798 | n/a | "FloatOperation, Overflow, Rounded, Subnormal, Underflow])" |
---|
4799 | n/a | self.assertEqual(s, t) |
---|
4800 | n/a | |
---|
4801 | n/a | def test_c_context_errors(self): |
---|
4802 | n/a | Context = C.Context |
---|
4803 | n/a | InvalidOperation = C.InvalidOperation |
---|
4804 | n/a | Overflow = C.Overflow |
---|
4805 | n/a | FloatOperation = C.FloatOperation |
---|
4806 | n/a | localcontext = C.localcontext |
---|
4807 | n/a | getcontext = C.getcontext |
---|
4808 | n/a | setcontext = C.setcontext |
---|
4809 | n/a | HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) |
---|
4810 | n/a | |
---|
4811 | n/a | c = Context() |
---|
4812 | n/a | |
---|
4813 | n/a | # SignalDict: input validation |
---|
4814 | n/a | self.assertRaises(KeyError, c.flags.__setitem__, 801, 0) |
---|
4815 | n/a | self.assertRaises(KeyError, c.traps.__setitem__, 801, 0) |
---|
4816 | n/a | self.assertRaises(ValueError, c.flags.__delitem__, Overflow) |
---|
4817 | n/a | self.assertRaises(ValueError, c.traps.__delitem__, InvalidOperation) |
---|
4818 | n/a | self.assertRaises(TypeError, setattr, c, 'flags', ['x']) |
---|
4819 | n/a | self.assertRaises(TypeError, setattr, c,'traps', ['y']) |
---|
4820 | n/a | self.assertRaises(KeyError, setattr, c, 'flags', {0:1}) |
---|
4821 | n/a | self.assertRaises(KeyError, setattr, c, 'traps', {0:1}) |
---|
4822 | n/a | |
---|
4823 | n/a | # Test assignment from a signal dict with the correct length but |
---|
4824 | n/a | # one invalid key. |
---|
4825 | n/a | d = c.flags.copy() |
---|
4826 | n/a | del d[FloatOperation] |
---|
4827 | n/a | d["XYZ"] = 91283719 |
---|
4828 | n/a | self.assertRaises(KeyError, setattr, c, 'flags', d) |
---|
4829 | n/a | self.assertRaises(KeyError, setattr, c, 'traps', d) |
---|
4830 | n/a | |
---|
4831 | n/a | # Input corner cases |
---|
4832 | n/a | int_max = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 |
---|
4833 | n/a | gt_max_emax = 10**18 if HAVE_CONFIG_64 else 10**9 |
---|
4834 | n/a | |
---|
4835 | n/a | # prec, Emax, Emin |
---|
4836 | n/a | for attr in ['prec', 'Emax']: |
---|
4837 | n/a | self.assertRaises(ValueError, setattr, c, attr, gt_max_emax) |
---|
4838 | n/a | self.assertRaises(ValueError, setattr, c, 'Emin', -gt_max_emax) |
---|
4839 | n/a | |
---|
4840 | n/a | # prec, Emax, Emin in context constructor |
---|
4841 | n/a | self.assertRaises(ValueError, Context, prec=gt_max_emax) |
---|
4842 | n/a | self.assertRaises(ValueError, Context, Emax=gt_max_emax) |
---|
4843 | n/a | self.assertRaises(ValueError, Context, Emin=-gt_max_emax) |
---|
4844 | n/a | |
---|
4845 | n/a | # Overflow in conversion |
---|
4846 | n/a | self.assertRaises(OverflowError, Context, prec=int_max+1) |
---|
4847 | n/a | self.assertRaises(OverflowError, Context, Emax=int_max+1) |
---|
4848 | n/a | self.assertRaises(OverflowError, Context, Emin=-int_max-2) |
---|
4849 | n/a | self.assertRaises(OverflowError, Context, clamp=int_max+1) |
---|
4850 | n/a | self.assertRaises(OverflowError, Context, capitals=int_max+1) |
---|
4851 | n/a | |
---|
4852 | n/a | # OverflowError, general ValueError |
---|
4853 | n/a | for attr in ('prec', 'Emin', 'Emax', 'capitals', 'clamp'): |
---|
4854 | n/a | self.assertRaises(OverflowError, setattr, c, attr, int_max+1) |
---|
4855 | n/a | self.assertRaises(OverflowError, setattr, c, attr, -int_max-2) |
---|
4856 | n/a | if sys.platform != 'win32': |
---|
4857 | n/a | self.assertRaises(ValueError, setattr, c, attr, int_max) |
---|
4858 | n/a | self.assertRaises(ValueError, setattr, c, attr, -int_max-1) |
---|
4859 | n/a | |
---|
4860 | n/a | # OverflowError: _unsafe_setprec, _unsafe_setemin, _unsafe_setemax |
---|
4861 | n/a | if C.MAX_PREC == 425000000: |
---|
4862 | n/a | self.assertRaises(OverflowError, getattr(c, '_unsafe_setprec'), |
---|
4863 | n/a | int_max+1) |
---|
4864 | n/a | self.assertRaises(OverflowError, getattr(c, '_unsafe_setemax'), |
---|
4865 | n/a | int_max+1) |
---|
4866 | n/a | self.assertRaises(OverflowError, getattr(c, '_unsafe_setemin'), |
---|
4867 | n/a | -int_max-2) |
---|
4868 | n/a | |
---|
4869 | n/a | # ValueError: _unsafe_setprec, _unsafe_setemin, _unsafe_setemax |
---|
4870 | n/a | if C.MAX_PREC == 425000000: |
---|
4871 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setprec'), 0) |
---|
4872 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setprec'), |
---|
4873 | n/a | 1070000001) |
---|
4874 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setemax'), -1) |
---|
4875 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setemax'), |
---|
4876 | n/a | 1070000001) |
---|
4877 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setemin'), |
---|
4878 | n/a | -1070000001) |
---|
4879 | n/a | self.assertRaises(ValueError, getattr(c, '_unsafe_setemin'), 1) |
---|
4880 | n/a | |
---|
4881 | n/a | # capitals, clamp |
---|
4882 | n/a | for attr in ['capitals', 'clamp']: |
---|
4883 | n/a | self.assertRaises(ValueError, setattr, c, attr, -1) |
---|
4884 | n/a | self.assertRaises(ValueError, setattr, c, attr, 2) |
---|
4885 | n/a | self.assertRaises(TypeError, setattr, c, attr, [1,2,3]) |
---|
4886 | n/a | if HAVE_CONFIG_64: |
---|
4887 | n/a | self.assertRaises(ValueError, setattr, c, attr, 2**32) |
---|
4888 | n/a | self.assertRaises(ValueError, setattr, c, attr, 2**32+1) |
---|
4889 | n/a | |
---|
4890 | n/a | # Invalid local context |
---|
4891 | n/a | self.assertRaises(TypeError, exec, 'with localcontext("xyz"): pass', |
---|
4892 | n/a | locals()) |
---|
4893 | n/a | self.assertRaises(TypeError, exec, |
---|
4894 | n/a | 'with localcontext(context=getcontext()): pass', |
---|
4895 | n/a | locals()) |
---|
4896 | n/a | |
---|
4897 | n/a | # setcontext |
---|
4898 | n/a | saved_context = getcontext() |
---|
4899 | n/a | self.assertRaises(TypeError, setcontext, "xyz") |
---|
4900 | n/a | setcontext(saved_context) |
---|
4901 | n/a | |
---|
4902 | n/a | def test_rounding_strings_interned(self): |
---|
4903 | n/a | |
---|
4904 | n/a | self.assertIs(C.ROUND_UP, P.ROUND_UP) |
---|
4905 | n/a | self.assertIs(C.ROUND_DOWN, P.ROUND_DOWN) |
---|
4906 | n/a | self.assertIs(C.ROUND_CEILING, P.ROUND_CEILING) |
---|
4907 | n/a | self.assertIs(C.ROUND_FLOOR, P.ROUND_FLOOR) |
---|
4908 | n/a | self.assertIs(C.ROUND_HALF_UP, P.ROUND_HALF_UP) |
---|
4909 | n/a | self.assertIs(C.ROUND_HALF_DOWN, P.ROUND_HALF_DOWN) |
---|
4910 | n/a | self.assertIs(C.ROUND_HALF_EVEN, P.ROUND_HALF_EVEN) |
---|
4911 | n/a | self.assertIs(C.ROUND_05UP, P.ROUND_05UP) |
---|
4912 | n/a | |
---|
4913 | n/a | @requires_extra_functionality |
---|
4914 | n/a | def test_c_context_errors_extra(self): |
---|
4915 | n/a | Context = C.Context |
---|
4916 | n/a | InvalidOperation = C.InvalidOperation |
---|
4917 | n/a | Overflow = C.Overflow |
---|
4918 | n/a | localcontext = C.localcontext |
---|
4919 | n/a | getcontext = C.getcontext |
---|
4920 | n/a | setcontext = C.setcontext |
---|
4921 | n/a | HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) |
---|
4922 | n/a | |
---|
4923 | n/a | c = Context() |
---|
4924 | n/a | |
---|
4925 | n/a | # Input corner cases |
---|
4926 | n/a | int_max = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 |
---|
4927 | n/a | |
---|
4928 | n/a | # OverflowError, general ValueError |
---|
4929 | n/a | self.assertRaises(OverflowError, setattr, c, '_allcr', int_max+1) |
---|
4930 | n/a | self.assertRaises(OverflowError, setattr, c, '_allcr', -int_max-2) |
---|
4931 | n/a | if sys.platform != 'win32': |
---|
4932 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', int_max) |
---|
4933 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', -int_max-1) |
---|
4934 | n/a | |
---|
4935 | n/a | # OverflowError, general TypeError |
---|
4936 | n/a | for attr in ('_flags', '_traps'): |
---|
4937 | n/a | self.assertRaises(OverflowError, setattr, c, attr, int_max+1) |
---|
4938 | n/a | self.assertRaises(OverflowError, setattr, c, attr, -int_max-2) |
---|
4939 | n/a | if sys.platform != 'win32': |
---|
4940 | n/a | self.assertRaises(TypeError, setattr, c, attr, int_max) |
---|
4941 | n/a | self.assertRaises(TypeError, setattr, c, attr, -int_max-1) |
---|
4942 | n/a | |
---|
4943 | n/a | # _allcr |
---|
4944 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', -1) |
---|
4945 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', 2) |
---|
4946 | n/a | self.assertRaises(TypeError, setattr, c, '_allcr', [1,2,3]) |
---|
4947 | n/a | if HAVE_CONFIG_64: |
---|
4948 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', 2**32) |
---|
4949 | n/a | self.assertRaises(ValueError, setattr, c, '_allcr', 2**32+1) |
---|
4950 | n/a | |
---|
4951 | n/a | # _flags, _traps |
---|
4952 | n/a | for attr in ['_flags', '_traps']: |
---|
4953 | n/a | self.assertRaises(TypeError, setattr, c, attr, 999999) |
---|
4954 | n/a | self.assertRaises(TypeError, setattr, c, attr, 'x') |
---|
4955 | n/a | |
---|
4956 | n/a | def test_c_valid_context(self): |
---|
4957 | n/a | # These tests are for code coverage in _decimal. |
---|
4958 | n/a | DefaultContext = C.DefaultContext |
---|
4959 | n/a | Clamped = C.Clamped |
---|
4960 | n/a | Underflow = C.Underflow |
---|
4961 | n/a | Inexact = C.Inexact |
---|
4962 | n/a | Rounded = C.Rounded |
---|
4963 | n/a | Subnormal = C.Subnormal |
---|
4964 | n/a | |
---|
4965 | n/a | c = DefaultContext.copy() |
---|
4966 | n/a | |
---|
4967 | n/a | # Exercise all getters and setters |
---|
4968 | n/a | c.prec = 34 |
---|
4969 | n/a | c.rounding = ROUND_HALF_UP |
---|
4970 | n/a | c.Emax = 3000 |
---|
4971 | n/a | c.Emin = -3000 |
---|
4972 | n/a | c.capitals = 1 |
---|
4973 | n/a | c.clamp = 0 |
---|
4974 | n/a | |
---|
4975 | n/a | self.assertEqual(c.prec, 34) |
---|
4976 | n/a | self.assertEqual(c.rounding, ROUND_HALF_UP) |
---|
4977 | n/a | self.assertEqual(c.Emin, -3000) |
---|
4978 | n/a | self.assertEqual(c.Emax, 3000) |
---|
4979 | n/a | self.assertEqual(c.capitals, 1) |
---|
4980 | n/a | self.assertEqual(c.clamp, 0) |
---|
4981 | n/a | |
---|
4982 | n/a | self.assertEqual(c.Etiny(), -3033) |
---|
4983 | n/a | self.assertEqual(c.Etop(), 2967) |
---|
4984 | n/a | |
---|
4985 | n/a | # Exercise all unsafe setters |
---|
4986 | n/a | if C.MAX_PREC == 425000000: |
---|
4987 | n/a | c._unsafe_setprec(999999999) |
---|
4988 | n/a | c._unsafe_setemax(999999999) |
---|
4989 | n/a | c._unsafe_setemin(-999999999) |
---|
4990 | n/a | self.assertEqual(c.prec, 999999999) |
---|
4991 | n/a | self.assertEqual(c.Emax, 999999999) |
---|
4992 | n/a | self.assertEqual(c.Emin, -999999999) |
---|
4993 | n/a | |
---|
4994 | n/a | @requires_extra_functionality |
---|
4995 | n/a | def test_c_valid_context_extra(self): |
---|
4996 | n/a | DefaultContext = C.DefaultContext |
---|
4997 | n/a | |
---|
4998 | n/a | c = DefaultContext.copy() |
---|
4999 | n/a | self.assertEqual(c._allcr, 1) |
---|
5000 | n/a | c._allcr = 0 |
---|
5001 | n/a | self.assertEqual(c._allcr, 0) |
---|
5002 | n/a | |
---|
5003 | n/a | def test_c_round(self): |
---|
5004 | n/a | # Restricted input. |
---|
5005 | n/a | Decimal = C.Decimal |
---|
5006 | n/a | InvalidOperation = C.InvalidOperation |
---|
5007 | n/a | localcontext = C.localcontext |
---|
5008 | n/a | MAX_EMAX = C.MAX_EMAX |
---|
5009 | n/a | MIN_ETINY = C.MIN_ETINY |
---|
5010 | n/a | int_max = 2**63-1 if C.MAX_PREC > 425000000 else 2**31-1 |
---|
5011 | n/a | |
---|
5012 | n/a | with localcontext() as c: |
---|
5013 | n/a | c.traps[InvalidOperation] = True |
---|
5014 | n/a | self.assertRaises(InvalidOperation, Decimal("1.23").__round__, |
---|
5015 | n/a | -int_max-1) |
---|
5016 | n/a | self.assertRaises(InvalidOperation, Decimal("1.23").__round__, |
---|
5017 | n/a | int_max) |
---|
5018 | n/a | self.assertRaises(InvalidOperation, Decimal("1").__round__, |
---|
5019 | n/a | int(MAX_EMAX+1)) |
---|
5020 | n/a | self.assertRaises(C.InvalidOperation, Decimal("1").__round__, |
---|
5021 | n/a | -int(MIN_ETINY-1)) |
---|
5022 | n/a | self.assertRaises(OverflowError, Decimal("1.23").__round__, |
---|
5023 | n/a | -int_max-2) |
---|
5024 | n/a | self.assertRaises(OverflowError, Decimal("1.23").__round__, |
---|
5025 | n/a | int_max+1) |
---|
5026 | n/a | |
---|
5027 | n/a | def test_c_format(self): |
---|
5028 | n/a | # Restricted input |
---|
5029 | n/a | Decimal = C.Decimal |
---|
5030 | n/a | HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) |
---|
5031 | n/a | |
---|
5032 | n/a | self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", [], 9) |
---|
5033 | n/a | self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", 9) |
---|
5034 | n/a | self.assertRaises(TypeError, Decimal(1).__format__, []) |
---|
5035 | n/a | |
---|
5036 | n/a | self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10") |
---|
5037 | n/a | maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 |
---|
5038 | n/a | self.assertRaises(ValueError, Decimal("1.23456789").__format__, |
---|
5039 | n/a | "=%d.1" % maxsize) |
---|
5040 | n/a | |
---|
5041 | n/a | def test_c_integral(self): |
---|
5042 | n/a | Decimal = C.Decimal |
---|
5043 | n/a | Inexact = C.Inexact |
---|
5044 | n/a | localcontext = C.localcontext |
---|
5045 | n/a | |
---|
5046 | n/a | x = Decimal(10) |
---|
5047 | n/a | self.assertEqual(x.to_integral(), 10) |
---|
5048 | n/a | self.assertRaises(TypeError, x.to_integral, '10') |
---|
5049 | n/a | self.assertRaises(TypeError, x.to_integral, 10, 'x') |
---|
5050 | n/a | self.assertRaises(TypeError, x.to_integral, 10) |
---|
5051 | n/a | |
---|
5052 | n/a | self.assertEqual(x.to_integral_value(), 10) |
---|
5053 | n/a | self.assertRaises(TypeError, x.to_integral_value, '10') |
---|
5054 | n/a | self.assertRaises(TypeError, x.to_integral_value, 10, 'x') |
---|
5055 | n/a | self.assertRaises(TypeError, x.to_integral_value, 10) |
---|
5056 | n/a | |
---|
5057 | n/a | self.assertEqual(x.to_integral_exact(), 10) |
---|
5058 | n/a | self.assertRaises(TypeError, x.to_integral_exact, '10') |
---|
5059 | n/a | self.assertRaises(TypeError, x.to_integral_exact, 10, 'x') |
---|
5060 | n/a | self.assertRaises(TypeError, x.to_integral_exact, 10) |
---|
5061 | n/a | |
---|
5062 | n/a | with localcontext() as c: |
---|
5063 | n/a | x = Decimal("99999999999999999999999999.9").to_integral_value(ROUND_UP) |
---|
5064 | n/a | self.assertEqual(x, Decimal('100000000000000000000000000')) |
---|
5065 | n/a | |
---|
5066 | n/a | x = Decimal("99999999999999999999999999.9").to_integral_exact(ROUND_UP) |
---|
5067 | n/a | self.assertEqual(x, Decimal('100000000000000000000000000')) |
---|
5068 | n/a | |
---|
5069 | n/a | c.traps[Inexact] = True |
---|
5070 | n/a | self.assertRaises(Inexact, Decimal("999.9").to_integral_exact, ROUND_UP) |
---|
5071 | n/a | |
---|
5072 | n/a | def test_c_funcs(self): |
---|
5073 | n/a | # Invalid arguments |
---|
5074 | n/a | Decimal = C.Decimal |
---|
5075 | n/a | InvalidOperation = C.InvalidOperation |
---|
5076 | n/a | DivisionByZero = C.DivisionByZero |
---|
5077 | n/a | getcontext = C.getcontext |
---|
5078 | n/a | localcontext = C.localcontext |
---|
5079 | n/a | |
---|
5080 | n/a | self.assertEqual(Decimal('9.99e10').to_eng_string(), '99.9E+9') |
---|
5081 | n/a | |
---|
5082 | n/a | self.assertRaises(TypeError, pow, Decimal(1), 2, "3") |
---|
5083 | n/a | self.assertRaises(TypeError, Decimal(9).number_class, "x", "y") |
---|
5084 | n/a | self.assertRaises(TypeError, Decimal(9).same_quantum, 3, "x", "y") |
---|
5085 | n/a | |
---|
5086 | n/a | self.assertRaises( |
---|
5087 | n/a | TypeError, |
---|
5088 | n/a | Decimal("1.23456789").quantize, Decimal('1e-100000'), [] |
---|
5089 | n/a | ) |
---|
5090 | n/a | self.assertRaises( |
---|
5091 | n/a | TypeError, |
---|
5092 | n/a | Decimal("1.23456789").quantize, Decimal('1e-100000'), getcontext() |
---|
5093 | n/a | ) |
---|
5094 | n/a | self.assertRaises( |
---|
5095 | n/a | TypeError, |
---|
5096 | n/a | Decimal("1.23456789").quantize, Decimal('1e-100000'), 10 |
---|
5097 | n/a | ) |
---|
5098 | n/a | self.assertRaises( |
---|
5099 | n/a | TypeError, |
---|
5100 | n/a | Decimal("1.23456789").quantize, Decimal('1e-100000'), ROUND_UP, 1000 |
---|
5101 | n/a | ) |
---|
5102 | n/a | |
---|
5103 | n/a | with localcontext() as c: |
---|
5104 | n/a | c.clear_traps() |
---|
5105 | n/a | |
---|
5106 | n/a | # Invalid arguments |
---|
5107 | n/a | self.assertRaises(TypeError, c.copy_sign, Decimal(1), "x", "y") |
---|
5108 | n/a | self.assertRaises(TypeError, c.canonical, 200) |
---|
5109 | n/a | self.assertRaises(TypeError, c.is_canonical, 200) |
---|
5110 | n/a | self.assertRaises(TypeError, c.divmod, 9, 8, "x", "y") |
---|
5111 | n/a | self.assertRaises(TypeError, c.same_quantum, 9, 3, "x", "y") |
---|
5112 | n/a | |
---|
5113 | n/a | self.assertEqual(str(c.canonical(Decimal(200))), '200') |
---|
5114 | n/a | self.assertEqual(c.radix(), 10) |
---|
5115 | n/a | |
---|
5116 | n/a | c.traps[DivisionByZero] = True |
---|
5117 | n/a | self.assertRaises(DivisionByZero, Decimal(9).__divmod__, 0) |
---|
5118 | n/a | self.assertRaises(DivisionByZero, c.divmod, 9, 0) |
---|
5119 | n/a | self.assertTrue(c.flags[InvalidOperation]) |
---|
5120 | n/a | |
---|
5121 | n/a | c.clear_flags() |
---|
5122 | n/a | c.traps[InvalidOperation] = True |
---|
5123 | n/a | self.assertRaises(InvalidOperation, Decimal(9).__divmod__, 0) |
---|
5124 | n/a | self.assertRaises(InvalidOperation, c.divmod, 9, 0) |
---|
5125 | n/a | self.assertTrue(c.flags[DivisionByZero]) |
---|
5126 | n/a | |
---|
5127 | n/a | c.traps[InvalidOperation] = True |
---|
5128 | n/a | c.prec = 2 |
---|
5129 | n/a | self.assertRaises(InvalidOperation, pow, Decimal(1000), 1, 501) |
---|
5130 | n/a | |
---|
5131 | n/a | def test_va_args_exceptions(self): |
---|
5132 | n/a | Decimal = C.Decimal |
---|
5133 | n/a | Context = C.Context |
---|
5134 | n/a | |
---|
5135 | n/a | x = Decimal("10001111111") |
---|
5136 | n/a | |
---|
5137 | n/a | for attr in ['exp', 'is_normal', 'is_subnormal', 'ln', 'log10', |
---|
5138 | n/a | 'logb', 'logical_invert', 'next_minus', 'next_plus', |
---|
5139 | n/a | 'normalize', 'number_class', 'sqrt', 'to_eng_string']: |
---|
5140 | n/a | func = getattr(x, attr) |
---|
5141 | n/a | self.assertRaises(TypeError, func, context="x") |
---|
5142 | n/a | self.assertRaises(TypeError, func, "x", context=None) |
---|
5143 | n/a | |
---|
5144 | n/a | for attr in ['compare', 'compare_signal', 'logical_and', |
---|
5145 | n/a | 'logical_or', 'max', 'max_mag', 'min', 'min_mag', |
---|
5146 | n/a | 'remainder_near', 'rotate', 'scaleb', 'shift']: |
---|
5147 | n/a | func = getattr(x, attr) |
---|
5148 | n/a | self.assertRaises(TypeError, func, context="x") |
---|
5149 | n/a | self.assertRaises(TypeError, func, "x", context=None) |
---|
5150 | n/a | |
---|
5151 | n/a | self.assertRaises(TypeError, x.to_integral, rounding=None, context=[]) |
---|
5152 | n/a | self.assertRaises(TypeError, x.to_integral, rounding={}, context=[]) |
---|
5153 | n/a | self.assertRaises(TypeError, x.to_integral, [], []) |
---|
5154 | n/a | |
---|
5155 | n/a | self.assertRaises(TypeError, x.to_integral_value, rounding=None, context=[]) |
---|
5156 | n/a | self.assertRaises(TypeError, x.to_integral_value, rounding={}, context=[]) |
---|
5157 | n/a | self.assertRaises(TypeError, x.to_integral_value, [], []) |
---|
5158 | n/a | |
---|
5159 | n/a | self.assertRaises(TypeError, x.to_integral_exact, rounding=None, context=[]) |
---|
5160 | n/a | self.assertRaises(TypeError, x.to_integral_exact, rounding={}, context=[]) |
---|
5161 | n/a | self.assertRaises(TypeError, x.to_integral_exact, [], []) |
---|
5162 | n/a | |
---|
5163 | n/a | self.assertRaises(TypeError, x.fma, 1, 2, context="x") |
---|
5164 | n/a | self.assertRaises(TypeError, x.fma, 1, 2, "x", context=None) |
---|
5165 | n/a | |
---|
5166 | n/a | self.assertRaises(TypeError, x.quantize, 1, [], context=None) |
---|
5167 | n/a | self.assertRaises(TypeError, x.quantize, 1, [], rounding=None) |
---|
5168 | n/a | self.assertRaises(TypeError, x.quantize, 1, [], []) |
---|
5169 | n/a | |
---|
5170 | n/a | c = Context() |
---|
5171 | n/a | self.assertRaises(TypeError, c.power, 1, 2, mod="x") |
---|
5172 | n/a | self.assertRaises(TypeError, c.power, 1, "x", mod=None) |
---|
5173 | n/a | self.assertRaises(TypeError, c.power, "x", 2, mod=None) |
---|
5174 | n/a | |
---|
5175 | n/a | @requires_extra_functionality |
---|
5176 | n/a | def test_c_context_templates(self): |
---|
5177 | n/a | self.assertEqual( |
---|
5178 | n/a | C.BasicContext._traps, |
---|
5179 | n/a | C.DecIEEEInvalidOperation|C.DecDivisionByZero|C.DecOverflow| |
---|
5180 | n/a | C.DecUnderflow|C.DecClamped |
---|
5181 | n/a | ) |
---|
5182 | n/a | self.assertEqual( |
---|
5183 | n/a | C.DefaultContext._traps, |
---|
5184 | n/a | C.DecIEEEInvalidOperation|C.DecDivisionByZero|C.DecOverflow |
---|
5185 | n/a | ) |
---|
5186 | n/a | |
---|
5187 | n/a | @requires_extra_functionality |
---|
5188 | n/a | def test_c_signal_dict(self): |
---|
5189 | n/a | |
---|
5190 | n/a | # SignalDict coverage |
---|
5191 | n/a | Context = C.Context |
---|
5192 | n/a | DefaultContext = C.DefaultContext |
---|
5193 | n/a | |
---|
5194 | n/a | InvalidOperation = C.InvalidOperation |
---|
5195 | n/a | DivisionByZero = C.DivisionByZero |
---|
5196 | n/a | Overflow = C.Overflow |
---|
5197 | n/a | Subnormal = C.Subnormal |
---|
5198 | n/a | Underflow = C.Underflow |
---|
5199 | n/a | Rounded = C.Rounded |
---|
5200 | n/a | Inexact = C.Inexact |
---|
5201 | n/a | Clamped = C.Clamped |
---|
5202 | n/a | |
---|
5203 | n/a | DecClamped = C.DecClamped |
---|
5204 | n/a | DecInvalidOperation = C.DecInvalidOperation |
---|
5205 | n/a | DecIEEEInvalidOperation = C.DecIEEEInvalidOperation |
---|
5206 | n/a | |
---|
5207 | n/a | def assertIsExclusivelySet(signal, signal_dict): |
---|
5208 | n/a | for sig in signal_dict: |
---|
5209 | n/a | if sig == signal: |
---|
5210 | n/a | self.assertTrue(signal_dict[sig]) |
---|
5211 | n/a | else: |
---|
5212 | n/a | self.assertFalse(signal_dict[sig]) |
---|
5213 | n/a | |
---|
5214 | n/a | c = DefaultContext.copy() |
---|
5215 | n/a | |
---|
5216 | n/a | # Signal dict methods |
---|
5217 | n/a | self.assertTrue(Overflow in c.traps) |
---|
5218 | n/a | c.clear_traps() |
---|
5219 | n/a | for k in c.traps.keys(): |
---|
5220 | n/a | c.traps[k] = True |
---|
5221 | n/a | for v in c.traps.values(): |
---|
5222 | n/a | self.assertTrue(v) |
---|
5223 | n/a | c.clear_traps() |
---|
5224 | n/a | for k, v in c.traps.items(): |
---|
5225 | n/a | self.assertFalse(v) |
---|
5226 | n/a | |
---|
5227 | n/a | self.assertFalse(c.flags.get(Overflow)) |
---|
5228 | n/a | self.assertIs(c.flags.get("x"), None) |
---|
5229 | n/a | self.assertEqual(c.flags.get("x", "y"), "y") |
---|
5230 | n/a | self.assertRaises(TypeError, c.flags.get, "x", "y", "z") |
---|
5231 | n/a | |
---|
5232 | n/a | self.assertEqual(len(c.flags), len(c.traps)) |
---|
5233 | n/a | s = sys.getsizeof(c.flags) |
---|
5234 | n/a | s = sys.getsizeof(c.traps) |
---|
5235 | n/a | s = c.flags.__repr__() |
---|
5236 | n/a | |
---|
5237 | n/a | # Set flags/traps. |
---|
5238 | n/a | c.clear_flags() |
---|
5239 | n/a | c._flags = DecClamped |
---|
5240 | n/a | self.assertTrue(c.flags[Clamped]) |
---|
5241 | n/a | |
---|
5242 | n/a | c.clear_traps() |
---|
5243 | n/a | c._traps = DecInvalidOperation |
---|
5244 | n/a | self.assertTrue(c.traps[InvalidOperation]) |
---|
5245 | n/a | |
---|
5246 | n/a | # Set flags/traps from dictionary. |
---|
5247 | n/a | c.clear_flags() |
---|
5248 | n/a | d = c.flags.copy() |
---|
5249 | n/a | d[DivisionByZero] = True |
---|
5250 | n/a | c.flags = d |
---|
5251 | n/a | assertIsExclusivelySet(DivisionByZero, c.flags) |
---|
5252 | n/a | |
---|
5253 | n/a | c.clear_traps() |
---|
5254 | n/a | d = c.traps.copy() |
---|
5255 | n/a | d[Underflow] = True |
---|
5256 | n/a | c.traps = d |
---|
5257 | n/a | assertIsExclusivelySet(Underflow, c.traps) |
---|
5258 | n/a | |
---|
5259 | n/a | # Random constructors |
---|
5260 | n/a | IntSignals = { |
---|
5261 | n/a | Clamped: C.DecClamped, |
---|
5262 | n/a | Rounded: C.DecRounded, |
---|
5263 | n/a | Inexact: C.DecInexact, |
---|
5264 | n/a | Subnormal: C.DecSubnormal, |
---|
5265 | n/a | Underflow: C.DecUnderflow, |
---|
5266 | n/a | Overflow: C.DecOverflow, |
---|
5267 | n/a | DivisionByZero: C.DecDivisionByZero, |
---|
5268 | n/a | InvalidOperation: C.DecIEEEInvalidOperation |
---|
5269 | n/a | } |
---|
5270 | n/a | IntCond = [ |
---|
5271 | n/a | C.DecDivisionImpossible, C.DecDivisionUndefined, C.DecFpuError, |
---|
5272 | n/a | C.DecInvalidContext, C.DecInvalidOperation, C.DecMallocError, |
---|
5273 | n/a | C.DecConversionSyntax, |
---|
5274 | n/a | ] |
---|
5275 | n/a | |
---|
5276 | n/a | lim = len(OrderedSignals[C]) |
---|
5277 | n/a | for r in range(lim): |
---|
5278 | n/a | for t in range(lim): |
---|
5279 | n/a | for round in RoundingModes: |
---|
5280 | n/a | flags = random.sample(OrderedSignals[C], r) |
---|
5281 | n/a | traps = random.sample(OrderedSignals[C], t) |
---|
5282 | n/a | prec = random.randrange(1, 10000) |
---|
5283 | n/a | emin = random.randrange(-10000, 0) |
---|
5284 | n/a | emax = random.randrange(0, 10000) |
---|
5285 | n/a | clamp = random.randrange(0, 2) |
---|
5286 | n/a | caps = random.randrange(0, 2) |
---|
5287 | n/a | cr = random.randrange(0, 2) |
---|
5288 | n/a | c = Context(prec=prec, rounding=round, Emin=emin, Emax=emax, |
---|
5289 | n/a | capitals=caps, clamp=clamp, flags=list(flags), |
---|
5290 | n/a | traps=list(traps)) |
---|
5291 | n/a | |
---|
5292 | n/a | self.assertEqual(c.prec, prec) |
---|
5293 | n/a | self.assertEqual(c.rounding, round) |
---|
5294 | n/a | self.assertEqual(c.Emin, emin) |
---|
5295 | n/a | self.assertEqual(c.Emax, emax) |
---|
5296 | n/a | self.assertEqual(c.capitals, caps) |
---|
5297 | n/a | self.assertEqual(c.clamp, clamp) |
---|
5298 | n/a | |
---|
5299 | n/a | f = 0 |
---|
5300 | n/a | for x in flags: |
---|
5301 | n/a | f |= IntSignals[x] |
---|
5302 | n/a | self.assertEqual(c._flags, f) |
---|
5303 | n/a | |
---|
5304 | n/a | f = 0 |
---|
5305 | n/a | for x in traps: |
---|
5306 | n/a | f |= IntSignals[x] |
---|
5307 | n/a | self.assertEqual(c._traps, f) |
---|
5308 | n/a | |
---|
5309 | n/a | for cond in IntCond: |
---|
5310 | n/a | c._flags = cond |
---|
5311 | n/a | self.assertTrue(c._flags&DecIEEEInvalidOperation) |
---|
5312 | n/a | assertIsExclusivelySet(InvalidOperation, c.flags) |
---|
5313 | n/a | |
---|
5314 | n/a | for cond in IntCond: |
---|
5315 | n/a | c._traps = cond |
---|
5316 | n/a | self.assertTrue(c._traps&DecIEEEInvalidOperation) |
---|
5317 | n/a | assertIsExclusivelySet(InvalidOperation, c.traps) |
---|
5318 | n/a | |
---|
5319 | n/a | def test_invalid_override(self): |
---|
5320 | n/a | Decimal = C.Decimal |
---|
5321 | n/a | |
---|
5322 | n/a | try: |
---|
5323 | n/a | from locale import CHAR_MAX |
---|
5324 | n/a | except ImportError: |
---|
5325 | n/a | self.skipTest('locale.CHAR_MAX not available') |
---|
5326 | n/a | |
---|
5327 | n/a | def make_grouping(lst): |
---|
5328 | n/a | return ''.join([chr(x) for x in lst]) |
---|
5329 | n/a | |
---|
5330 | n/a | def get_fmt(x, override=None, fmt='n'): |
---|
5331 | n/a | return Decimal(x).__format__(fmt, override) |
---|
5332 | n/a | |
---|
5333 | n/a | invalid_grouping = { |
---|
5334 | n/a | 'decimal_point' : ',', |
---|
5335 | n/a | 'grouping' : make_grouping([255, 255, 0]), |
---|
5336 | n/a | 'thousands_sep' : ',' |
---|
5337 | n/a | } |
---|
5338 | n/a | invalid_dot = { |
---|
5339 | n/a | 'decimal_point' : 'xxxxx', |
---|
5340 | n/a | 'grouping' : make_grouping([3, 3, 0]), |
---|
5341 | n/a | 'thousands_sep' : ',' |
---|
5342 | n/a | } |
---|
5343 | n/a | invalid_sep = { |
---|
5344 | n/a | 'decimal_point' : '.', |
---|
5345 | n/a | 'grouping' : make_grouping([3, 3, 0]), |
---|
5346 | n/a | 'thousands_sep' : 'yyyyy' |
---|
5347 | n/a | } |
---|
5348 | n/a | |
---|
5349 | n/a | if CHAR_MAX == 127: # negative grouping in override |
---|
5350 | n/a | self.assertRaises(ValueError, get_fmt, 12345, |
---|
5351 | n/a | invalid_grouping, 'g') |
---|
5352 | n/a | |
---|
5353 | n/a | self.assertRaises(ValueError, get_fmt, 12345, invalid_dot, 'g') |
---|
5354 | n/a | self.assertRaises(ValueError, get_fmt, 12345, invalid_sep, 'g') |
---|
5355 | n/a | |
---|
5356 | n/a | def test_exact_conversion(self): |
---|
5357 | n/a | Decimal = C.Decimal |
---|
5358 | n/a | localcontext = C.localcontext |
---|
5359 | n/a | InvalidOperation = C.InvalidOperation |
---|
5360 | n/a | |
---|
5361 | n/a | with localcontext() as c: |
---|
5362 | n/a | |
---|
5363 | n/a | c.traps[InvalidOperation] = True |
---|
5364 | n/a | |
---|
5365 | n/a | # Clamped |
---|
5366 | n/a | x = "0e%d" % sys.maxsize |
---|
5367 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5368 | n/a | |
---|
5369 | n/a | x = "0e%d" % (-sys.maxsize-1) |
---|
5370 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5371 | n/a | |
---|
5372 | n/a | # Overflow |
---|
5373 | n/a | x = "1e%d" % sys.maxsize |
---|
5374 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5375 | n/a | |
---|
5376 | n/a | # Underflow |
---|
5377 | n/a | x = "1e%d" % (-sys.maxsize-1) |
---|
5378 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5379 | n/a | |
---|
5380 | n/a | def test_from_tuple(self): |
---|
5381 | n/a | Decimal = C.Decimal |
---|
5382 | n/a | localcontext = C.localcontext |
---|
5383 | n/a | InvalidOperation = C.InvalidOperation |
---|
5384 | n/a | Overflow = C.Overflow |
---|
5385 | n/a | Underflow = C.Underflow |
---|
5386 | n/a | |
---|
5387 | n/a | with localcontext() as c: |
---|
5388 | n/a | |
---|
5389 | n/a | c.traps[InvalidOperation] = True |
---|
5390 | n/a | c.traps[Overflow] = True |
---|
5391 | n/a | c.traps[Underflow] = True |
---|
5392 | n/a | |
---|
5393 | n/a | # SSIZE_MAX |
---|
5394 | n/a | x = (1, (), sys.maxsize) |
---|
5395 | n/a | self.assertEqual(str(c.create_decimal(x)), '-0E+999999') |
---|
5396 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5397 | n/a | |
---|
5398 | n/a | x = (1, (0, 1, 2), sys.maxsize) |
---|
5399 | n/a | self.assertRaises(Overflow, c.create_decimal, x) |
---|
5400 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5401 | n/a | |
---|
5402 | n/a | # SSIZE_MIN |
---|
5403 | n/a | x = (1, (), -sys.maxsize-1) |
---|
5404 | n/a | self.assertEqual(str(c.create_decimal(x)), '-0E-1000026') |
---|
5405 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5406 | n/a | |
---|
5407 | n/a | x = (1, (0, 1, 2), -sys.maxsize-1) |
---|
5408 | n/a | self.assertRaises(Underflow, c.create_decimal, x) |
---|
5409 | n/a | self.assertRaises(InvalidOperation, Decimal, x) |
---|
5410 | n/a | |
---|
5411 | n/a | # OverflowError |
---|
5412 | n/a | x = (1, (), sys.maxsize+1) |
---|
5413 | n/a | self.assertRaises(OverflowError, c.create_decimal, x) |
---|
5414 | n/a | self.assertRaises(OverflowError, Decimal, x) |
---|
5415 | n/a | |
---|
5416 | n/a | x = (1, (), -sys.maxsize-2) |
---|
5417 | n/a | self.assertRaises(OverflowError, c.create_decimal, x) |
---|
5418 | n/a | self.assertRaises(OverflowError, Decimal, x) |
---|
5419 | n/a | |
---|
5420 | n/a | # Specials |
---|
5421 | n/a | x = (1, (), "N") |
---|
5422 | n/a | self.assertEqual(str(Decimal(x)), '-sNaN') |
---|
5423 | n/a | x = (1, (0,), "N") |
---|
5424 | n/a | self.assertEqual(str(Decimal(x)), '-sNaN') |
---|
5425 | n/a | x = (1, (0, 1), "N") |
---|
5426 | n/a | self.assertEqual(str(Decimal(x)), '-sNaN1') |
---|
5427 | n/a | |
---|
5428 | n/a | def test_sizeof(self): |
---|
5429 | n/a | Decimal = C.Decimal |
---|
5430 | n/a | HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) |
---|
5431 | n/a | |
---|
5432 | n/a | self.assertGreater(Decimal(0).__sizeof__(), 0) |
---|
5433 | n/a | if HAVE_CONFIG_64: |
---|
5434 | n/a | x = Decimal(10**(19*24)).__sizeof__() |
---|
5435 | n/a | y = Decimal(10**(19*25)).__sizeof__() |
---|
5436 | n/a | self.assertEqual(y, x+8) |
---|
5437 | n/a | else: |
---|
5438 | n/a | x = Decimal(10**(9*24)).__sizeof__() |
---|
5439 | n/a | y = Decimal(10**(9*25)).__sizeof__() |
---|
5440 | n/a | self.assertEqual(y, x+4) |
---|
5441 | n/a | |
---|
5442 | n/a | def test_internal_use_of_overridden_methods(self): |
---|
5443 | n/a | Decimal = C.Decimal |
---|
5444 | n/a | |
---|
5445 | n/a | # Unsound subtyping |
---|
5446 | n/a | class X(float): |
---|
5447 | n/a | def as_integer_ratio(self): |
---|
5448 | n/a | return 1 |
---|
5449 | n/a | def __abs__(self): |
---|
5450 | n/a | return self |
---|
5451 | n/a | |
---|
5452 | n/a | class Y(float): |
---|
5453 | n/a | def __abs__(self): |
---|
5454 | n/a | return [1]*200 |
---|
5455 | n/a | |
---|
5456 | n/a | class I(int): |
---|
5457 | n/a | def bit_length(self): |
---|
5458 | n/a | return [1]*200 |
---|
5459 | n/a | |
---|
5460 | n/a | class Z(float): |
---|
5461 | n/a | def as_integer_ratio(self): |
---|
5462 | n/a | return (I(1), I(1)) |
---|
5463 | n/a | def __abs__(self): |
---|
5464 | n/a | return self |
---|
5465 | n/a | |
---|
5466 | n/a | for cls in X, Y, Z: |
---|
5467 | n/a | self.assertEqual(Decimal.from_float(cls(101.1)), |
---|
5468 | n/a | Decimal.from_float(101.1)) |
---|
5469 | n/a | |
---|
5470 | n/a | @requires_docstrings |
---|
5471 | n/a | @unittest.skipUnless(C, "test requires C version") |
---|
5472 | n/a | class SignatureTest(unittest.TestCase): |
---|
5473 | n/a | """Function signatures""" |
---|
5474 | n/a | |
---|
5475 | n/a | def test_inspect_module(self): |
---|
5476 | n/a | for attr in dir(P): |
---|
5477 | n/a | if attr.startswith('_'): |
---|
5478 | n/a | continue |
---|
5479 | n/a | p_func = getattr(P, attr) |
---|
5480 | n/a | c_func = getattr(C, attr) |
---|
5481 | n/a | if (attr == 'Decimal' or attr == 'Context' or |
---|
5482 | n/a | inspect.isfunction(p_func)): |
---|
5483 | n/a | p_sig = inspect.signature(p_func) |
---|
5484 | n/a | c_sig = inspect.signature(c_func) |
---|
5485 | n/a | |
---|
5486 | n/a | # parameter names: |
---|
5487 | n/a | c_names = list(c_sig.parameters.keys()) |
---|
5488 | n/a | p_names = [x for x in p_sig.parameters.keys() if not |
---|
5489 | n/a | x.startswith('_')] |
---|
5490 | n/a | |
---|
5491 | n/a | self.assertEqual(c_names, p_names, |
---|
5492 | n/a | msg="parameter name mismatch in %s" % p_func) |
---|
5493 | n/a | |
---|
5494 | n/a | c_kind = [x.kind for x in c_sig.parameters.values()] |
---|
5495 | n/a | p_kind = [x[1].kind for x in p_sig.parameters.items() if not |
---|
5496 | n/a | x[0].startswith('_')] |
---|
5497 | n/a | |
---|
5498 | n/a | # parameters: |
---|
5499 | n/a | if attr != 'setcontext': |
---|
5500 | n/a | self.assertEqual(c_kind, p_kind, |
---|
5501 | n/a | msg="parameter kind mismatch in %s" % p_func) |
---|
5502 | n/a | |
---|
5503 | n/a | def test_inspect_types(self): |
---|
5504 | n/a | |
---|
5505 | n/a | POS = inspect._ParameterKind.POSITIONAL_ONLY |
---|
5506 | n/a | POS_KWD = inspect._ParameterKind.POSITIONAL_OR_KEYWORD |
---|
5507 | n/a | |
---|
5508 | n/a | # Type heuristic (type annotations would help!): |
---|
5509 | n/a | pdict = {C: {'other': C.Decimal(1), |
---|
5510 | n/a | 'third': C.Decimal(1), |
---|
5511 | n/a | 'x': C.Decimal(1), |
---|
5512 | n/a | 'y': C.Decimal(1), |
---|
5513 | n/a | 'z': C.Decimal(1), |
---|
5514 | n/a | 'a': C.Decimal(1), |
---|
5515 | n/a | 'b': C.Decimal(1), |
---|
5516 | n/a | 'c': C.Decimal(1), |
---|
5517 | n/a | 'exp': C.Decimal(1), |
---|
5518 | n/a | 'modulo': C.Decimal(1), |
---|
5519 | n/a | 'num': "1", |
---|
5520 | n/a | 'f': 1.0, |
---|
5521 | n/a | 'rounding': C.ROUND_HALF_UP, |
---|
5522 | n/a | 'context': C.getcontext()}, |
---|
5523 | n/a | P: {'other': P.Decimal(1), |
---|
5524 | n/a | 'third': P.Decimal(1), |
---|
5525 | n/a | 'a': P.Decimal(1), |
---|
5526 | n/a | 'b': P.Decimal(1), |
---|
5527 | n/a | 'c': P.Decimal(1), |
---|
5528 | n/a | 'exp': P.Decimal(1), |
---|
5529 | n/a | 'modulo': P.Decimal(1), |
---|
5530 | n/a | 'num': "1", |
---|
5531 | n/a | 'f': 1.0, |
---|
5532 | n/a | 'rounding': P.ROUND_HALF_UP, |
---|
5533 | n/a | 'context': P.getcontext()}} |
---|
5534 | n/a | |
---|
5535 | n/a | def mkargs(module, sig): |
---|
5536 | n/a | args = [] |
---|
5537 | n/a | kwargs = {} |
---|
5538 | n/a | for name, param in sig.parameters.items(): |
---|
5539 | n/a | if name == 'self': continue |
---|
5540 | n/a | if param.kind == POS: |
---|
5541 | n/a | args.append(pdict[module][name]) |
---|
5542 | n/a | elif param.kind == POS_KWD: |
---|
5543 | n/a | kwargs[name] = pdict[module][name] |
---|
5544 | n/a | else: |
---|
5545 | n/a | raise TestFailed("unexpected parameter kind") |
---|
5546 | n/a | return args, kwargs |
---|
5547 | n/a | |
---|
5548 | n/a | def tr(s): |
---|
5549 | n/a | """The C Context docstrings use 'x' in order to prevent confusion |
---|
5550 | n/a | with the article 'a' in the descriptions.""" |
---|
5551 | n/a | if s == 'x': return 'a' |
---|
5552 | n/a | if s == 'y': return 'b' |
---|
5553 | n/a | if s == 'z': return 'c' |
---|
5554 | n/a | return s |
---|
5555 | n/a | |
---|
5556 | n/a | def doit(ty): |
---|
5557 | n/a | p_type = getattr(P, ty) |
---|
5558 | n/a | c_type = getattr(C, ty) |
---|
5559 | n/a | for attr in dir(p_type): |
---|
5560 | n/a | if attr.startswith('_'): |
---|
5561 | n/a | continue |
---|
5562 | n/a | p_func = getattr(p_type, attr) |
---|
5563 | n/a | c_func = getattr(c_type, attr) |
---|
5564 | n/a | if inspect.isfunction(p_func): |
---|
5565 | n/a | p_sig = inspect.signature(p_func) |
---|
5566 | n/a | c_sig = inspect.signature(c_func) |
---|
5567 | n/a | |
---|
5568 | n/a | # parameter names: |
---|
5569 | n/a | p_names = list(p_sig.parameters.keys()) |
---|
5570 | n/a | c_names = [tr(x) for x in c_sig.parameters.keys()] |
---|
5571 | n/a | |
---|
5572 | n/a | self.assertEqual(c_names, p_names, |
---|
5573 | n/a | msg="parameter name mismatch in %s" % p_func) |
---|
5574 | n/a | |
---|
5575 | n/a | p_kind = [x.kind for x in p_sig.parameters.values()] |
---|
5576 | n/a | c_kind = [x.kind for x in c_sig.parameters.values()] |
---|
5577 | n/a | |
---|
5578 | n/a | # 'self' parameter: |
---|
5579 | n/a | self.assertIs(p_kind[0], POS_KWD) |
---|
5580 | n/a | self.assertIs(c_kind[0], POS) |
---|
5581 | n/a | |
---|
5582 | n/a | # remaining parameters: |
---|
5583 | n/a | if ty == 'Decimal': |
---|
5584 | n/a | self.assertEqual(c_kind[1:], p_kind[1:], |
---|
5585 | n/a | msg="parameter kind mismatch in %s" % p_func) |
---|
5586 | n/a | else: # Context methods are positional only in the C version. |
---|
5587 | n/a | self.assertEqual(len(c_kind), len(p_kind), |
---|
5588 | n/a | msg="parameter kind mismatch in %s" % p_func) |
---|
5589 | n/a | |
---|
5590 | n/a | # Run the function: |
---|
5591 | n/a | args, kwds = mkargs(C, c_sig) |
---|
5592 | n/a | try: |
---|
5593 | n/a | getattr(c_type(9), attr)(*args, **kwds) |
---|
5594 | n/a | except Exception as err: |
---|
5595 | n/a | raise TestFailed("invalid signature for %s: %s %s" % (c_func, args, kwds)) |
---|
5596 | n/a | |
---|
5597 | n/a | args, kwds = mkargs(P, p_sig) |
---|
5598 | n/a | try: |
---|
5599 | n/a | getattr(p_type(9), attr)(*args, **kwds) |
---|
5600 | n/a | except Exception as err: |
---|
5601 | n/a | raise TestFailed("invalid signature for %s: %s %s" % (p_func, args, kwds)) |
---|
5602 | n/a | |
---|
5603 | n/a | doit('Decimal') |
---|
5604 | n/a | doit('Context') |
---|
5605 | n/a | |
---|
5606 | n/a | |
---|
5607 | n/a | all_tests = [ |
---|
5608 | n/a | CExplicitConstructionTest, PyExplicitConstructionTest, |
---|
5609 | n/a | CImplicitConstructionTest, PyImplicitConstructionTest, |
---|
5610 | n/a | CFormatTest, PyFormatTest, |
---|
5611 | n/a | CArithmeticOperatorsTest, PyArithmeticOperatorsTest, |
---|
5612 | n/a | CThreadingTest, PyThreadingTest, |
---|
5613 | n/a | CUsabilityTest, PyUsabilityTest, |
---|
5614 | n/a | CPythonAPItests, PyPythonAPItests, |
---|
5615 | n/a | CContextAPItests, PyContextAPItests, |
---|
5616 | n/a | CContextWithStatement, PyContextWithStatement, |
---|
5617 | n/a | CContextFlags, PyContextFlags, |
---|
5618 | n/a | CSpecialContexts, PySpecialContexts, |
---|
5619 | n/a | CContextInputValidation, PyContextInputValidation, |
---|
5620 | n/a | CContextSubclassing, PyContextSubclassing, |
---|
5621 | n/a | CCoverage, PyCoverage, |
---|
5622 | n/a | CFunctionality, PyFunctionality, |
---|
5623 | n/a | CWhitebox, PyWhitebox, |
---|
5624 | n/a | CIBMTestCases, PyIBMTestCases, |
---|
5625 | n/a | ] |
---|
5626 | n/a | |
---|
5627 | n/a | # Delete C tests if _decimal.so is not present. |
---|
5628 | n/a | if not C: |
---|
5629 | n/a | all_tests = all_tests[1::2] |
---|
5630 | n/a | else: |
---|
5631 | n/a | all_tests.insert(0, CheckAttributes) |
---|
5632 | n/a | all_tests.insert(1, SignatureTest) |
---|
5633 | n/a | |
---|
5634 | n/a | |
---|
5635 | n/a | def test_main(arith=None, verbose=None, todo_tests=None, debug=None): |
---|
5636 | n/a | """ Execute the tests. |
---|
5637 | n/a | |
---|
5638 | n/a | Runs all arithmetic tests if arith is True or if the "decimal" resource |
---|
5639 | n/a | is enabled in regrtest.py |
---|
5640 | n/a | """ |
---|
5641 | n/a | |
---|
5642 | n/a | init(C) |
---|
5643 | n/a | init(P) |
---|
5644 | n/a | global TEST_ALL, DEBUG |
---|
5645 | n/a | TEST_ALL = arith if arith is not None else is_resource_enabled('decimal') |
---|
5646 | n/a | DEBUG = debug |
---|
5647 | n/a | |
---|
5648 | n/a | if todo_tests is None: |
---|
5649 | n/a | test_classes = all_tests |
---|
5650 | n/a | else: |
---|
5651 | n/a | test_classes = [CIBMTestCases, PyIBMTestCases] |
---|
5652 | n/a | |
---|
5653 | n/a | # Dynamically build custom test definition for each file in the test |
---|
5654 | n/a | # directory and add the definitions to the DecimalTest class. This |
---|
5655 | n/a | # procedure insures that new files do not get skipped. |
---|
5656 | n/a | for filename in os.listdir(directory): |
---|
5657 | n/a | if '.decTest' not in filename or filename.startswith("."): |
---|
5658 | n/a | continue |
---|
5659 | n/a | head, tail = filename.split('.') |
---|
5660 | n/a | if todo_tests is not None and head not in todo_tests: |
---|
5661 | n/a | continue |
---|
5662 | n/a | tester = lambda self, f=filename: self.eval_file(directory + f) |
---|
5663 | n/a | setattr(CIBMTestCases, 'test_' + head, tester) |
---|
5664 | n/a | setattr(PyIBMTestCases, 'test_' + head, tester) |
---|
5665 | n/a | del filename, head, tail, tester |
---|
5666 | n/a | |
---|
5667 | n/a | |
---|
5668 | n/a | try: |
---|
5669 | n/a | run_unittest(*test_classes) |
---|
5670 | n/a | if todo_tests is None: |
---|
5671 | n/a | from doctest import IGNORE_EXCEPTION_DETAIL |
---|
5672 | n/a | savedecimal = sys.modules['decimal'] |
---|
5673 | n/a | if C: |
---|
5674 | n/a | sys.modules['decimal'] = C |
---|
5675 | n/a | run_doctest(C, verbose, optionflags=IGNORE_EXCEPTION_DETAIL) |
---|
5676 | n/a | sys.modules['decimal'] = P |
---|
5677 | n/a | run_doctest(P, verbose) |
---|
5678 | n/a | sys.modules['decimal'] = savedecimal |
---|
5679 | n/a | finally: |
---|
5680 | n/a | if C: C.setcontext(ORIGINAL_CONTEXT[C]) |
---|
5681 | n/a | P.setcontext(ORIGINAL_CONTEXT[P]) |
---|
5682 | n/a | if not C: |
---|
5683 | n/a | warnings.warn('C tests skipped: no module named _decimal.', |
---|
5684 | n/a | UserWarning) |
---|
5685 | n/a | if not orig_sys_decimal is sys.modules['decimal']: |
---|
5686 | n/a | raise TestFailed("Internal error: unbalanced number of changes to " |
---|
5687 | n/a | "sys.modules['decimal'].") |
---|
5688 | n/a | |
---|
5689 | n/a | |
---|
5690 | n/a | if __name__ == '__main__': |
---|
5691 | n/a | import optparse |
---|
5692 | n/a | p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]") |
---|
5693 | n/a | p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test') |
---|
5694 | n/a | p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests') |
---|
5695 | n/a | (opt, args) = p.parse_args() |
---|
5696 | n/a | |
---|
5697 | n/a | if opt.skip: |
---|
5698 | n/a | test_main(arith=False, verbose=True) |
---|
5699 | n/a | elif args: |
---|
5700 | n/a | test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug) |
---|
5701 | n/a | else: |
---|
5702 | n/a | test_main(arith=True, verbose=True) |
---|