blob: 6c071f1a7455653fc980f2a9487726701a90c2b4 [file] [log] [blame]
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001# Copyright (c) 2004 Python Software Foundation.
2# All rights reserved.
3
4# Written by Eric Price <eprice at tjhsst.edu>
5# and Facundo Batista <facundo at taniquetil.com.ar>
6# and Raymond Hettinger <python at rcn.com>
7# and Aahz (aahz at pobox.com)
8# and Tim Peters
9
10"""
11These are the test cases for the Decimal module.
12
13There are two groups of tests, Arithmetic and Behaviour. The former test
14the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter
15test the pythonic behaviour according to PEP 327.
16
17Cowlishaw's tests can be downloaded from:
18
19 www2.hursley.ibm.com/decimal/dectest.zip
20
21This test module can be called from command line with one parameter (Arithmetic
22or Behaviour) to test each part, or without parameter to test both parts. If
23you're working through IDLE, you can import this test module and call test_main()
24with the corresponding argument.
25"""
26
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000027import glob
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +000028import math
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000029import os, sys
30import pickle, copy
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +000031import unittest
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000032from decimal import *
Raymond Hettinger2c8585b2009-02-03 03:37:03 +000033import numbers
Tim Peters46cc7022006-03-31 04:11:16 +000034from test.test_support import (TestSkipped, run_unittest, run_doctest,
35 is_resource_enabled)
Raymond Hettinger0aeac102004-07-05 22:53:03 +000036import random
Raymond Hettinger7e71fa52004-12-18 19:07:19 +000037try:
38 import threading
39except ImportError:
40 threading = None
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000041
Raymond Hettingerfed52962004-07-14 15:41:57 +000042# Useful Test Constant
43Signals = getcontext().flags.keys()
44
Tim Peters46cc7022006-03-31 04:11:16 +000045# Tests are built around these assumed context defaults.
46# test_main() restores the original context.
Neal Norwitzce4a9c92006-04-09 08:36:46 +000047def init():
48 global ORIGINAL_CONTEXT
49 ORIGINAL_CONTEXT = getcontext().copy()
Facundo Batistaee340e52008-05-02 17:39:00 +000050 DefaultTestContext = Context(
51 prec = 9,
52 rounding = ROUND_HALF_EVEN,
53 traps = dict.fromkeys(Signals, 0)
54 )
55 setcontext(DefaultTestContext)
Raymond Hettinger6ea48452004-07-03 12:26:21 +000056
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000057TESTDATADIR = 'decimaltestdata'
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +000058if __name__ == '__main__':
59 file = sys.argv[0]
60else:
61 file = __file__
62testdir = os.path.dirname(file) or os.curdir
Raymond Hettinger267b8682005-03-27 10:47:39 +000063directory = testdir + os.sep + TESTDATADIR + os.sep
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000064
Raymond Hettinger267b8682005-03-27 10:47:39 +000065skip_expected = not os.path.isdir(directory)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000066
67# Make sure it actually raises errors when not expected and caught in flags
68# Slower, since it runs some things several times.
69EXTENDEDERRORTEST = False
70
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000071#Map the test cases' error names to the actual errors
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000072ErrorNames = {'clamped' : Clamped,
Raymond Hettinger5aa478b2004-07-09 10:02:53 +000073 'conversion_syntax' : InvalidOperation,
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000074 'division_by_zero' : DivisionByZero,
Raymond Hettinger5aa478b2004-07-09 10:02:53 +000075 'division_impossible' : InvalidOperation,
76 'division_undefined' : InvalidOperation,
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000077 'inexact' : Inexact,
Raymond Hettinger5aa478b2004-07-09 10:02:53 +000078 'invalid_context' : InvalidOperation,
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000079 'invalid_operation' : InvalidOperation,
80 'overflow' : Overflow,
81 'rounded' : Rounded,
82 'subnormal' : Subnormal,
83 'underflow' : Underflow}
84
85
86def Nonfunction(*args):
87 """Doesn't do anything."""
88 return None
89
90RoundingDict = {'ceiling' : ROUND_CEILING, #Maps test-case names to roundings.
91 'down' : ROUND_DOWN,
92 'floor' : ROUND_FLOOR,
93 'half_down' : ROUND_HALF_DOWN,
94 'half_even' : ROUND_HALF_EVEN,
95 'half_up' : ROUND_HALF_UP,
Facundo Batista353750c2007-09-13 18:13:15 +000096 'up' : ROUND_UP,
97 '05up' : ROUND_05UP}
Raymond Hettinger7c85fa42004-07-01 11:01:35 +000098
99# Name adapter to be able to change the Decimal and Context
100# interface without changing the test files from Cowlishaw
Facundo Batista1a191df2007-10-02 17:01:24 +0000101nameAdapter = {'and':'logical_and',
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000102 'apply':'_apply',
Facundo Batista353750c2007-09-13 18:13:15 +0000103 'class':'number_class',
104 'comparesig':'compare_signal',
105 'comparetotal':'compare_total',
106 'comparetotmag':'compare_total_mag',
Facundo Batista353750c2007-09-13 18:13:15 +0000107 'copy':'copy_decimal',
Facundo Batista1a191df2007-10-02 17:01:24 +0000108 'copyabs':'copy_abs',
Facundo Batista353750c2007-09-13 18:13:15 +0000109 'copynegate':'copy_negate',
110 'copysign':'copy_sign',
Facundo Batista1a191df2007-10-02 17:01:24 +0000111 'divideint':'divide_int',
Facundo Batista353750c2007-09-13 18:13:15 +0000112 'invert':'logical_invert',
Facundo Batista1a191df2007-10-02 17:01:24 +0000113 'iscanonical':'is_canonical',
114 'isfinite':'is_finite',
115 'isinfinite':'is_infinite',
116 'isnan':'is_nan',
117 'isnormal':'is_normal',
118 'isqnan':'is_qnan',
119 'issigned':'is_signed',
120 'issnan':'is_snan',
121 'issubnormal':'is_subnormal',
122 'iszero':'is_zero',
Facundo Batista353750c2007-09-13 18:13:15 +0000123 'maxmag':'max_mag',
124 'minmag':'min_mag',
125 'nextminus':'next_minus',
126 'nextplus':'next_plus',
127 'nexttoward':'next_toward',
Facundo Batista1a191df2007-10-02 17:01:24 +0000128 'or':'logical_or',
Facundo Batista353750c2007-09-13 18:13:15 +0000129 'reduce':'normalize',
Facundo Batista1a191df2007-10-02 17:01:24 +0000130 'remaindernear':'remainder_near',
131 'samequantum':'same_quantum',
132 'squareroot':'sqrt',
133 'toeng':'to_eng_string',
134 'tointegral':'to_integral_value',
135 'tointegralx':'to_integral_exact',
136 'tosci':'to_sci_string',
137 'xor':'logical_xor',
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000138 }
139
Facundo Batista1a191df2007-10-02 17:01:24 +0000140# The following functions return True/False rather than a Decimal instance
141
142LOGICAL_FUNCTIONS = (
143 'is_canonical',
144 'is_finite',
145 'is_infinite',
146 'is_nan',
147 'is_normal',
148 'is_qnan',
149 'is_signed',
150 'is_snan',
151 'is_subnormal',
152 'is_zero',
153 'same_quantum',
154 )
155
Facundo Batista353750c2007-09-13 18:13:15 +0000156# For some operations (currently exp, ln, log10, power), the decNumber
157# reference implementation imposes additional restrictions on the
158# context and operands. These restrictions are not part of the
159# specification; however, the effect of these restrictions does show
160# up in some of the testcases. We skip testcases that violate these
161# restrictions, since Decimal behaves differently from decNumber for
162# these testcases so these testcases would otherwise fail.
163
164decNumberRestricted = ('power', 'ln', 'log10', 'exp')
165DEC_MAX_MATH = 999999
166def outside_decNumber_bounds(v, context):
167 if (context.prec > DEC_MAX_MATH or
168 context.Emax > DEC_MAX_MATH or
169 -context.Emin > DEC_MAX_MATH):
170 return True
171 if not v._is_special and v and (
Facundo Batista353750c2007-09-13 18:13:15 +0000172 v.adjusted() > DEC_MAX_MATH or
173 v.adjusted() < 1-2*DEC_MAX_MATH):
174 return True
175 return False
176
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000177class DecimalTest(unittest.TestCase):
178 """Class which tests the Decimal class against the test cases.
179
180 Changed for unittest.
181 """
182 def setUp(self):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000183 self.context = Context()
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000184 self.ignore_list = ['#']
185 # Basically, a # means return NaN InvalidOperation.
186 # Different from a sNaN in trim
187
188 self.ChangeDict = {'precision' : self.change_precision,
189 'rounding' : self.change_rounding_method,
190 'maxexponent' : self.change_max_exponent,
191 'minexponent' : self.change_min_exponent,
192 'clamp' : self.change_clamp}
193
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000194 def eval_file(self, file):
195 global skip_expected
196 if skip_expected:
197 raise TestSkipped
198 return
199 for line in open(file).xreadlines():
200 line = line.replace('\r\n', '').replace('\n', '')
Raymond Hettinger5aa478b2004-07-09 10:02:53 +0000201 #print line
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000202 try:
203 t = self.eval_line(line)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000204 except DecimalException, exception:
205 #Exception raised where there shoudn't have been one.
206 self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
207
208 return
209
210 def eval_line(self, s):
211 if s.find(' -> ') >= 0 and s[:2] != '--' and not s.startswith(' --'):
212 s = (s.split('->')[0] + '->' +
213 s.split('->')[1].split('--')[0]).strip()
214 else:
215 s = s.split('--')[0].strip()
216
217 for ignore in self.ignore_list:
218 if s.find(ignore) >= 0:
219 #print s.split()[0], 'NotImplemented--', ignore
220 return
221 if not s:
222 return
223 elif ':' in s:
224 return self.eval_directive(s)
225 else:
226 return self.eval_equation(s)
227
228 def eval_directive(self, s):
229 funct, value = map(lambda x: x.strip().lower(), s.split(':'))
230 if funct == 'rounding':
231 value = RoundingDict[value]
232 else:
233 try:
234 value = int(value)
235 except ValueError:
236 pass
237
238 funct = self.ChangeDict.get(funct, Nonfunction)
239 funct(value)
240
241 def eval_equation(self, s):
242 #global DEFAULT_PRECISION
243 #print DEFAULT_PRECISION
Raymond Hettingered20ad82004-09-04 20:09:13 +0000244
245 if not TEST_ALL and random.random() < 0.90:
246 return
247
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000248 try:
249 Sides = s.split('->')
250 L = Sides[0].strip().split()
251 id = L[0]
Facundo Batista353750c2007-09-13 18:13:15 +0000252 if DEBUG:
253 print "Test ", id,
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000254 funct = L[1].lower()
255 valstemp = L[2:]
256 L = Sides[1].strip().split()
257 ans = L[0]
258 exceptions = L[1:]
259 except (TypeError, AttributeError, IndexError):
Raymond Hettingerd87ac8f2004-07-09 10:52:54 +0000260 raise InvalidOperation
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000261 def FixQuotes(val):
262 val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote')
263 val = val.replace("'", '').replace('"', '')
264 val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
265 return val
266 fname = nameAdapter.get(funct, funct)
267 if fname == 'rescale':
268 return
269 funct = getattr(self.context, fname)
270 vals = []
271 conglomerate = ''
272 quote = 0
273 theirexceptions = [ErrorNames[x.lower()] for x in exceptions]
274
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +0000275 for exception in Signals:
Raymond Hettingerbf440692004-07-10 14:14:37 +0000276 self.context.traps[exception] = 1 #Catch these bugs...
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000277 for exception in theirexceptions:
Raymond Hettingerbf440692004-07-10 14:14:37 +0000278 self.context.traps[exception] = 0
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000279 for i, val in enumerate(valstemp):
280 if val.count("'") % 2 == 1:
281 quote = 1 - quote
282 if quote:
283 conglomerate = conglomerate + ' ' + val
284 continue
285 else:
286 val = conglomerate + val
287 conglomerate = ''
288 v = FixQuotes(val)
289 if fname in ('to_sci_string', 'to_eng_string'):
290 if EXTENDEDERRORTEST:
291 for error in theirexceptions:
Raymond Hettingerbf440692004-07-10 14:14:37 +0000292 self.context.traps[error] = 1
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000293 try:
294 funct(self.context.create_decimal(v))
295 except error:
296 pass
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +0000297 except Signals, e:
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000298 self.fail("Raised %s in %s when %s disabled" % \
299 (e, s, error))
300 else:
301 self.fail("Did not raise %s in %s" % (error, s))
Raymond Hettingerbf440692004-07-10 14:14:37 +0000302 self.context.traps[error] = 0
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000303 v = self.context.create_decimal(v)
304 else:
Facundo Batista353750c2007-09-13 18:13:15 +0000305 v = Decimal(v, self.context)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000306 vals.append(v)
307
308 ans = FixQuotes(ans)
309
Facundo Batista353750c2007-09-13 18:13:15 +0000310 # skip tests that are related to bounds imposed in the decNumber
311 # reference implementation
312 if fname in decNumberRestricted:
313 if fname == 'power':
314 if not (vals[1]._isinteger() and
315 -1999999997 <= vals[1] <= 999999999):
316 if outside_decNumber_bounds(vals[0], self.context) or \
317 outside_decNumber_bounds(vals[1], self.context):
318 #print "Skipping test %s" % s
319 return
320 else:
321 if outside_decNumber_bounds(vals[0], self.context):
322 #print "Skipping test %s" % s
323 return
324
325
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000326 if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'):
327 for error in theirexceptions:
Raymond Hettingerbf440692004-07-10 14:14:37 +0000328 self.context.traps[error] = 1
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000329 try:
330 funct(*vals)
331 except error:
332 pass
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +0000333 except Signals, e:
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000334 self.fail("Raised %s in %s when %s disabled" % \
335 (e, s, error))
336 else:
337 self.fail("Did not raise %s in %s" % (error, s))
Raymond Hettingerbf440692004-07-10 14:14:37 +0000338 self.context.traps[error] = 0
Facundo Batista353750c2007-09-13 18:13:15 +0000339 if DEBUG:
340 print "--", self.context
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000341 try:
342 result = str(funct(*vals))
Facundo Batista1a191df2007-10-02 17:01:24 +0000343 if fname in LOGICAL_FUNCTIONS:
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000344 result = str(int(eval(result))) # 'True', 'False' -> '1', '0'
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +0000345 except Signals, error:
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000346 self.fail("Raised %s in %s" % (error, s))
347 except: #Catch any error long enough to state the test case.
348 print "ERROR:", s
349 raise
350
351 myexceptions = self.getexceptions()
Raymond Hettingerbf440692004-07-10 14:14:37 +0000352 self.context.clear_flags()
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000353
354 myexceptions.sort()
355 theirexceptions.sort()
356
357 self.assertEqual(result, ans,
358 'Incorrect answer for ' + s + ' -- got ' + result)
359 self.assertEqual(myexceptions, theirexceptions,
Facundo Batista353750c2007-09-13 18:13:15 +0000360 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000361 return
362
363 def getexceptions(self):
Raymond Hettingerf63ba432004-08-17 05:42:09 +0000364 return [e for e in Signals if self.context.flags[e]]
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000365
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000366 def change_precision(self, prec):
367 self.context.prec = prec
368 def change_rounding_method(self, rounding):
369 self.context.rounding = rounding
370 def change_min_exponent(self, exp):
371 self.context.Emin = exp
372 def change_max_exponent(self, exp):
373 self.context.Emax = exp
374 def change_clamp(self, clamp):
375 self.context._clamp = clamp
376
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000377
378
379# The following classes test the behaviour of Decimal according to PEP 327
380
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000381class DecimalExplicitConstructionTest(unittest.TestCase):
382 '''Unit tests for Explicit Construction cases of Decimal.'''
383
384 def test_explicit_empty(self):
385 self.assertEqual(Decimal(), Decimal("0"))
386
387 def test_explicit_from_None(self):
388 self.assertRaises(TypeError, Decimal, None)
389
390 def test_explicit_from_int(self):
391
392 #positive
393 d = Decimal(45)
394 self.assertEqual(str(d), '45')
395
396 #very large positive
397 d = Decimal(500000123)
398 self.assertEqual(str(d), '500000123')
399
400 #negative
401 d = Decimal(-45)
402 self.assertEqual(str(d), '-45')
403
404 #zero
405 d = Decimal(0)
406 self.assertEqual(str(d), '0')
407
408 def test_explicit_from_string(self):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000409
410 #empty
411 self.assertEqual(str(Decimal('')), 'NaN')
412
413 #int
414 self.assertEqual(str(Decimal('45')), '45')
415
416 #float
417 self.assertEqual(str(Decimal('45.34')), '45.34')
418
419 #engineer notation
420 self.assertEqual(str(Decimal('45e2')), '4.5E+3')
421
422 #just not a number
423 self.assertEqual(str(Decimal('ugly')), 'NaN')
424
Mark Dickinson59bc20b2008-01-12 01:56:00 +0000425 #leading and trailing whitespace permitted
426 self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4')
427 self.assertEqual(str(Decimal(' -7.89')), '-7.89')
428
Mark Dickinson8e85ffa2008-03-25 18:47:59 +0000429 #unicode strings should be permitted
430 self.assertEqual(str(Decimal(u'0E-017')), '0E-17')
431 self.assertEqual(str(Decimal(u'45')), '45')
432 self.assertEqual(str(Decimal(u'-Inf')), '-Infinity')
433 self.assertEqual(str(Decimal(u'NaN123')), 'NaN123')
434
Mark Dickinson70c32892008-07-02 09:37:01 +0000435 #but alternate unicode digits should not
436 self.assertEqual(str(Decimal(u'\uff11')), 'NaN')
437
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000438 def test_explicit_from_tuples(self):
439
440 #zero
441 d = Decimal( (0, (0,), 0) )
442 self.assertEqual(str(d), '0')
443
444 #int
445 d = Decimal( (1, (4, 5), 0) )
446 self.assertEqual(str(d), '-45')
447
448 #float
449 d = Decimal( (0, (4, 5, 3, 4), -2) )
450 self.assertEqual(str(d), '45.34')
451
452 #weird
453 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
454 self.assertEqual(str(d), '-4.34913534E-17')
455
456 #wrong number of items
457 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) )
458
459 #bad sign
460 self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
Facundo Batista9b5e2312007-10-19 19:25:57 +0000461 self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
462 self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000463
464 #bad exp
465 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
Facundo Batista9b5e2312007-10-19 19:25:57 +0000466 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
467 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') )
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000468
469 #bad coefficients
470 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) )
471 self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
Facundo Batista9b5e2312007-10-19 19:25:57 +0000472 self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
Facundo Batista72bc54f2007-11-23 17:59:00 +0000473 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) )
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000474
475 def test_explicit_from_Decimal(self):
476
477 #positive
478 d = Decimal(45)
479 e = Decimal(d)
480 self.assertEqual(str(e), '45')
481 self.assertNotEqual(id(d), id(e))
482
483 #very large positive
484 d = Decimal(500000123)
485 e = Decimal(d)
486 self.assertEqual(str(e), '500000123')
487 self.assertNotEqual(id(d), id(e))
488
489 #negative
490 d = Decimal(-45)
491 e = Decimal(d)
492 self.assertEqual(str(e), '-45')
493 self.assertNotEqual(id(d), id(e))
494
495 #zero
496 d = Decimal(0)
497 e = Decimal(d)
498 self.assertEqual(str(e), '0')
499 self.assertNotEqual(id(d), id(e))
500
501 def test_explicit_context_create_decimal(self):
502
503 nc = copy.copy(getcontext())
504 nc.prec = 3
505
506 # empty
Raymond Hettingerfed52962004-07-14 15:41:57 +0000507 d = Decimal()
508 self.assertEqual(str(d), '0')
509 d = nc.create_decimal()
510 self.assertEqual(str(d), '0')
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000511
512 # from None
513 self.assertRaises(TypeError, nc.create_decimal, None)
514
515 # from int
516 d = nc.create_decimal(456)
517 self.failUnless(isinstance(d, Decimal))
518 self.assertEqual(nc.create_decimal(45678),
519 nc.create_decimal('457E+2'))
520
521 # from string
522 d = Decimal('456789')
523 self.assertEqual(str(d), '456789')
524 d = nc.create_decimal('456789')
525 self.assertEqual(str(d), '4.57E+5')
Mark Dickinson59bc20b2008-01-12 01:56:00 +0000526 # leading and trailing whitespace should result in a NaN;
527 # spaces are already checked in Cowlishaw's test-suite, so
528 # here we just check that a trailing newline results in a NaN
529 self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN')
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000530
531 # from tuples
532 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
533 self.assertEqual(str(d), '-4.34913534E-17')
534 d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
535 self.assertEqual(str(d), '-4.35E-17')
536
537 # from Decimal
538 prevdec = Decimal(500000123)
539 d = Decimal(prevdec)
540 self.assertEqual(str(d), '500000123')
541 d = nc.create_decimal(prevdec)
542 self.assertEqual(str(d), '5.00E+8')
543
544
545class DecimalImplicitConstructionTest(unittest.TestCase):
546 '''Unit tests for Implicit Construction cases of Decimal.'''
547
548 def test_implicit_from_None(self):
549 self.assertRaises(TypeError, eval, 'Decimal(5) + None', globals())
550
551 def test_implicit_from_int(self):
552 #normal
553 self.assertEqual(str(Decimal(5) + 45), '50')
554 #exceeding precision
555 self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000))
556
557 def test_implicit_from_string(self):
558 self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', globals())
559
560 def test_implicit_from_float(self):
561 self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', globals())
562
563 def test_implicit_from_Decimal(self):
564 self.assertEqual(Decimal(5) + Decimal(45), Decimal(50))
565
Raymond Hettinger267b8682005-03-27 10:47:39 +0000566 def test_rop(self):
567 # Allow other classes to be trained to interact with Decimals
568 class E:
569 def __divmod__(self, other):
570 return 'divmod ' + str(other)
571 def __rdivmod__(self, other):
572 return str(other) + ' rdivmod'
573 def __lt__(self, other):
574 return 'lt ' + str(other)
575 def __gt__(self, other):
576 return 'gt ' + str(other)
577 def __le__(self, other):
578 return 'le ' + str(other)
579 def __ge__(self, other):
580 return 'ge ' + str(other)
581 def __eq__(self, other):
582 return 'eq ' + str(other)
583 def __ne__(self, other):
584 return 'ne ' + str(other)
585
586 self.assertEqual(divmod(E(), Decimal(10)), 'divmod 10')
587 self.assertEqual(divmod(Decimal(10), E()), '10 rdivmod')
588 self.assertEqual(eval('Decimal(10) < E()'), 'gt 10')
589 self.assertEqual(eval('Decimal(10) > E()'), 'lt 10')
590 self.assertEqual(eval('Decimal(10) <= E()'), 'ge 10')
591 self.assertEqual(eval('Decimal(10) >= E()'), 'le 10')
592 self.assertEqual(eval('Decimal(10) == E()'), 'eq 10')
593 self.assertEqual(eval('Decimal(10) != E()'), 'ne 10')
594
595 # insert operator methods and then exercise them
Georg Brandl96c3f7f2006-03-28 08:06:35 +0000596 oplist = [
597 ('+', '__add__', '__radd__'),
598 ('-', '__sub__', '__rsub__'),
599 ('*', '__mul__', '__rmul__'),
600 ('%', '__mod__', '__rmod__'),
601 ('//', '__floordiv__', '__rfloordiv__'),
602 ('**', '__pow__', '__rpow__')
603 ]
604 if 1/2 == 0:
605 # testing with classic division, so add __div__
606 oplist.append(('/', '__div__', '__rdiv__'))
607 else:
608 # testing with -Qnew, so add __truediv__
609 oplist.append(('/', '__truediv__', '__rtruediv__'))
Anthony Baxter4ef3a232006-03-30 12:59:11 +0000610
Georg Brandl96c3f7f2006-03-28 08:06:35 +0000611 for sym, lop, rop in oplist:
Raymond Hettinger267b8682005-03-27 10:47:39 +0000612 setattr(E, lop, lambda self, other: 'str' + lop + str(other))
613 setattr(E, rop, lambda self, other: str(other) + rop + 'str')
614 self.assertEqual(eval('E()' + sym + 'Decimal(10)'),
615 'str' + lop + '10')
616 self.assertEqual(eval('Decimal(10)' + sym + 'E()'),
617 '10' + rop + 'str')
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000618
Mark Dickinson277859d2009-03-17 23:03:46 +0000619
Mark Dickinson1ddf1d82008-02-29 02:16:37 +0000620class DecimalFormatTest(unittest.TestCase):
621 '''Unit tests for the format function.'''
622 def test_formatting(self):
623 # triples giving a format, a Decimal, and the expected result
624 test_values = [
625 ('e', '0E-15', '0e-15'),
626 ('e', '2.3E-15', '2.3e-15'),
627 ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros
628 ('e', '2.30000E-15', '2.30000e-15'),
629 ('e', '1.23456789123456789e40', '1.23456789123456789e+40'),
630 ('e', '1.5', '1.5e+0'),
631 ('e', '0.15', '1.5e-1'),
632 ('e', '0.015', '1.5e-2'),
633 ('e', '0.0000000000015', '1.5e-12'),
634 ('e', '15.0', '1.50e+1'),
635 ('e', '-15', '-1.5e+1'),
636 ('e', '0', '0e+0'),
637 ('e', '0E1', '0e+1'),
638 ('e', '0.0', '0e-1'),
639 ('e', '0.00', '0e-2'),
640 ('.6e', '0E-15', '0.000000e-9'),
641 ('.6e', '0', '0.000000e+6'),
642 ('.6e', '9.999999', '9.999999e+0'),
643 ('.6e', '9.9999999', '1.000000e+1'),
644 ('.6e', '-1.23e5', '-1.230000e+5'),
645 ('.6e', '1.23456789e-3', '1.234568e-3'),
646 ('f', '0', '0'),
647 ('f', '0.0', '0.0'),
648 ('f', '0E-2', '0.00'),
649 ('f', '0.00E-8', '0.0000000000'),
650 ('f', '0E1', '0'), # loses exponent information
651 ('f', '3.2E1', '32'),
652 ('f', '3.2E2', '320'),
653 ('f', '3.20E2', '320'),
654 ('f', '3.200E2', '320.0'),
655 ('f', '3.2E-6', '0.0000032'),
656 ('.6f', '0E-15', '0.000000'), # all zeros treated equally
657 ('.6f', '0E1', '0.000000'),
658 ('.6f', '0', '0.000000'),
659 ('.0f', '0', '0'), # no decimal point
660 ('.0f', '0e-2', '0'),
661 ('.0f', '3.14159265', '3'),
662 ('.1f', '3.14159265', '3.1'),
663 ('.4f', '3.14159265', '3.1416'),
664 ('.6f', '3.14159265', '3.141593'),
665 ('.7f', '3.14159265', '3.1415926'), # round-half-even!
666 ('.8f', '3.14159265', '3.14159265'),
667 ('.9f', '3.14159265', '3.141592650'),
668
669 ('g', '0', '0'),
670 ('g', '0.0', '0.0'),
671 ('g', '0E1', '0e+1'),
672 ('G', '0E1', '0E+1'),
673 ('g', '0E-5', '0.00000'),
674 ('g', '0E-6', '0.000000'),
675 ('g', '0E-7', '0e-7'),
676 ('g', '-0E2', '-0e+2'),
677 ('.0g', '3.14159265', '3'), # 0 sig fig -> 1 sig fig
678 ('.1g', '3.14159265', '3'),
679 ('.2g', '3.14159265', '3.1'),
680 ('.5g', '3.14159265', '3.1416'),
681 ('.7g', '3.14159265', '3.141593'),
682 ('.8g', '3.14159265', '3.1415926'), # round-half-even!
683 ('.9g', '3.14159265', '3.14159265'),
684 ('.10g', '3.14159265', '3.14159265'), # don't pad
685
686 ('%', '0E1', '0%'),
687 ('%', '0E0', '0%'),
688 ('%', '0E-1', '0%'),
689 ('%', '0E-2', '0%'),
690 ('%', '0E-3', '0.0%'),
691 ('%', '0E-4', '0.00%'),
692
693 ('.3%', '0', '0.000%'), # all zeros treated equally
694 ('.3%', '0E10', '0.000%'),
695 ('.3%', '0E-10', '0.000%'),
696 ('.3%', '2.34', '234.000%'),
697 ('.3%', '1.234567', '123.457%'),
698 ('.0%', '1.23', '123%'),
699
700 ('e', 'NaN', 'NaN'),
701 ('f', '-NaN123', '-NaN123'),
702 ('+g', 'NaN456', '+NaN456'),
703 ('.3e', 'Inf', 'Infinity'),
704 ('.16f', '-Inf', '-Infinity'),
705 ('.0g', '-sNaN', '-sNaN'),
706
707 ('', '1.00', '1.00'),
Mark Dickinsonb065e522009-03-17 18:01:03 +0000708
Mark Dickinson277859d2009-03-17 23:03:46 +0000709 # test alignment and padding
Mark Dickinsonb065e522009-03-17 18:01:03 +0000710 ('<6', '123', '123 '),
711 ('>6', '123', ' 123'),
712 ('^6', '123', ' 123 '),
713 ('=+6', '123', '+ 123'),
Mark Dickinson277859d2009-03-17 23:03:46 +0000714 ('#<10', 'NaN', 'NaN#######'),
715 ('#<10', '-4.3', '-4.3######'),
716 ('#<+10', '0.0130', '+0.0130###'),
717 ('#< 10', '0.0130', ' 0.0130###'),
718 ('@>10', '-Inf', '@-Infinity'),
719 ('#>5', '-Inf', '-Infinity'),
720 ('?^5', '123', '?123?'),
721 ('%^6', '123', '%123%%'),
722 (' ^6', '-45.6', '-45.6 '),
723 ('/=10', '-45.6', '-/////45.6'),
724 ('/=+10', '45.6', '+/////45.6'),
725 ('/= 10', '45.6', ' /////45.6'),
726
727 # thousands separator
728 (',', '1234567', '1,234,567'),
729 (',', '123456', '123,456'),
730 (',', '12345', '12,345'),
731 (',', '1234', '1,234'),
732 (',', '123', '123'),
733 (',', '12', '12'),
734 (',', '1', '1'),
735 (',', '0', '0'),
736 (',', '-1234567', '-1,234,567'),
737 (',', '-123456', '-123,456'),
738 ('7,', '123456', '123,456'),
739 ('8,', '123456', '123,456 '),
740 ('08,', '123456', '0,123,456'), # special case: extra 0 needed
741 ('+08,', '123456', '+123,456'), # but not if there's a sign
742 (' 08,', '123456', ' 123,456'),
743 ('08,', '-123456', '-123,456'),
744 ('+09,', '123456', '+0,123,456'),
745 # ... with fractional part...
746 ('07,', '1234.56', '1,234.56'),
747 ('08,', '1234.56', '1,234.56'),
748 ('09,', '1234.56', '01,234.56'),
749 ('010,', '1234.56', '001,234.56'),
750 ('011,', '1234.56', '0,001,234.56'),
751 ('012,', '1234.56', '0,001,234.56'),
752 ('08,.1f', '1234.5', '01,234.5'),
753 # no thousands separators in fraction part
754 (',', '1.23456789', '1.23456789'),
755 (',%', '123.456789', '12,345.6789%'),
756 (',e', '123456', '1.23456e+5'),
757 (',E', '123456', '1.23456E+5'),
Mark Dickinson1ddf1d82008-02-29 02:16:37 +0000758 ]
759 for fmt, d, result in test_values:
760 self.assertEqual(format(Decimal(d), fmt), result)
761
Mark Dickinson277859d2009-03-17 23:03:46 +0000762 def test_n_format(self):
763 try:
764 from locale import CHAR_MAX
765 except ImportError:
766 return
767
768 # Set up some localeconv-like dictionaries
769 en_US = {
770 'decimal_point' : '.',
771 'grouping' : [3, 3, 0],
772 'thousands_sep': ','
773 }
774
775 fr_FR = {
776 'decimal_point' : ',',
777 'grouping' : [CHAR_MAX],
778 'thousands_sep' : ''
779 }
780
781 ru_RU = {
782 'decimal_point' : ',',
783 'grouping' : [3, 3, 0],
784 'thousands_sep' : ' '
785 }
786
787 crazy = {
788 'decimal_point' : '&',
789 'grouping' : [1, 4, 2, CHAR_MAX],
790 'thousands_sep' : '-'
791 }
792
793
794 def get_fmt(x, locale, fmt='n'):
795 return Decimal.__format__(Decimal(x), fmt, _localeconv=locale)
796
797 self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7')
798 self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7')
799 self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7')
800 self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7')
801
802 self.assertEqual(get_fmt(123456789, en_US), '123,456,789')
803 self.assertEqual(get_fmt(123456789, fr_FR), '123456789')
804 self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789')
805 self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3')
806
807 self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8')
808 self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8')
809 self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8')
810 self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8')
811
812
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000813class DecimalArithmeticOperatorsTest(unittest.TestCase):
814 '''Unit tests for all arithmetic operators, binary and unary.'''
815
816 def test_addition(self):
817
818 d1 = Decimal('-11.1')
819 d2 = Decimal('22.2')
820
821 #two Decimals
822 self.assertEqual(d1+d2, Decimal('11.1'))
823 self.assertEqual(d2+d1, Decimal('11.1'))
824
825 #with other type, left
826 c = d1 + 5
827 self.assertEqual(c, Decimal('-6.1'))
828 self.assertEqual(type(c), type(d1))
829
830 #with other type, right
831 c = 5 + d1
832 self.assertEqual(c, Decimal('-6.1'))
833 self.assertEqual(type(c), type(d1))
834
835 #inline with decimal
836 d1 += d2
837 self.assertEqual(d1, Decimal('11.1'))
838
839 #inline with other type
840 d1 += 5
841 self.assertEqual(d1, Decimal('16.1'))
842
843 def test_subtraction(self):
844
845 d1 = Decimal('-11.1')
846 d2 = Decimal('22.2')
847
848 #two Decimals
849 self.assertEqual(d1-d2, Decimal('-33.3'))
850 self.assertEqual(d2-d1, Decimal('33.3'))
851
852 #with other type, left
853 c = d1 - 5
854 self.assertEqual(c, Decimal('-16.1'))
855 self.assertEqual(type(c), type(d1))
856
857 #with other type, right
858 c = 5 - d1
859 self.assertEqual(c, Decimal('16.1'))
860 self.assertEqual(type(c), type(d1))
861
862 #inline with decimal
863 d1 -= d2
864 self.assertEqual(d1, Decimal('-33.3'))
865
866 #inline with other type
867 d1 -= 5
868 self.assertEqual(d1, Decimal('-38.3'))
869
870 def test_multiplication(self):
871
872 d1 = Decimal('-5')
873 d2 = Decimal('3')
874
875 #two Decimals
876 self.assertEqual(d1*d2, Decimal('-15'))
877 self.assertEqual(d2*d1, Decimal('-15'))
878
879 #with other type, left
880 c = d1 * 5
881 self.assertEqual(c, Decimal('-25'))
882 self.assertEqual(type(c), type(d1))
883
884 #with other type, right
885 c = 5 * d1
886 self.assertEqual(c, Decimal('-25'))
887 self.assertEqual(type(c), type(d1))
888
889 #inline with decimal
890 d1 *= d2
891 self.assertEqual(d1, Decimal('-15'))
892
893 #inline with other type
894 d1 *= 5
895 self.assertEqual(d1, Decimal('-75'))
896
897 def test_division(self):
898
899 d1 = Decimal('-5')
900 d2 = Decimal('2')
901
902 #two Decimals
903 self.assertEqual(d1/d2, Decimal('-2.5'))
904 self.assertEqual(d2/d1, Decimal('-0.4'))
905
906 #with other type, left
907 c = d1 / 4
908 self.assertEqual(c, Decimal('-1.25'))
909 self.assertEqual(type(c), type(d1))
910
911 #with other type, right
912 c = 4 / d1
913 self.assertEqual(c, Decimal('-0.8'))
914 self.assertEqual(type(c), type(d1))
915
916 #inline with decimal
917 d1 /= d2
918 self.assertEqual(d1, Decimal('-2.5'))
919
920 #inline with other type
921 d1 /= 4
922 self.assertEqual(d1, Decimal('-0.625'))
923
924 def test_floor_division(self):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000925
926 d1 = Decimal('5')
927 d2 = Decimal('2')
928
929 #two Decimals
930 self.assertEqual(d1//d2, Decimal('2'))
931 self.assertEqual(d2//d1, Decimal('0'))
932
933 #with other type, left
934 c = d1 // 4
935 self.assertEqual(c, Decimal('1'))
936 self.assertEqual(type(c), type(d1))
937
938 #with other type, right
939 c = 7 // d1
940 self.assertEqual(c, Decimal('1'))
941 self.assertEqual(type(c), type(d1))
942
943 #inline with decimal
944 d1 //= d2
945 self.assertEqual(d1, Decimal('2'))
946
947 #inline with other type
948 d1 //= 2
949 self.assertEqual(d1, Decimal('1'))
950
951 def test_powering(self):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000952
953 d1 = Decimal('5')
954 d2 = Decimal('2')
955
956 #two Decimals
957 self.assertEqual(d1**d2, Decimal('25'))
958 self.assertEqual(d2**d1, Decimal('32'))
959
960 #with other type, left
961 c = d1 ** 4
962 self.assertEqual(c, Decimal('625'))
963 self.assertEqual(type(c), type(d1))
964
965 #with other type, right
966 c = 7 ** d1
967 self.assertEqual(c, Decimal('16807'))
968 self.assertEqual(type(c), type(d1))
969
970 #inline with decimal
971 d1 **= d2
972 self.assertEqual(d1, Decimal('25'))
973
974 #inline with other type
975 d1 **= 4
976 self.assertEqual(d1, Decimal('390625'))
977
978 def test_module(self):
979
980 d1 = Decimal('5')
981 d2 = Decimal('2')
982
983 #two Decimals
984 self.assertEqual(d1%d2, Decimal('1'))
985 self.assertEqual(d2%d1, Decimal('2'))
986
987 #with other type, left
988 c = d1 % 4
989 self.assertEqual(c, Decimal('1'))
990 self.assertEqual(type(c), type(d1))
991
992 #with other type, right
993 c = 7 % d1
994 self.assertEqual(c, Decimal('2'))
995 self.assertEqual(type(c), type(d1))
996
997 #inline with decimal
998 d1 %= d2
999 self.assertEqual(d1, Decimal('1'))
1000
1001 #inline with other type
1002 d1 %= 4
1003 self.assertEqual(d1, Decimal('1'))
1004
1005 def test_floor_div_module(self):
1006
1007 d1 = Decimal('5')
1008 d2 = Decimal('2')
1009
1010 #two Decimals
1011 (p, q) = divmod(d1, d2)
1012 self.assertEqual(p, Decimal('2'))
1013 self.assertEqual(q, Decimal('1'))
1014 self.assertEqual(type(p), type(d1))
1015 self.assertEqual(type(q), type(d1))
1016
1017 #with other type, left
1018 (p, q) = divmod(d1, 4)
1019 self.assertEqual(p, Decimal('1'))
1020 self.assertEqual(q, Decimal('1'))
1021 self.assertEqual(type(p), type(d1))
1022 self.assertEqual(type(q), type(d1))
1023
1024 #with other type, right
1025 (p, q) = divmod(7, d1)
1026 self.assertEqual(p, Decimal('1'))
1027 self.assertEqual(q, Decimal('2'))
1028 self.assertEqual(type(p), type(d1))
1029 self.assertEqual(type(q), type(d1))
1030
1031 def test_unary_operators(self):
1032 self.assertEqual(+Decimal(45), Decimal(+45)) # +
1033 self.assertEqual(-Decimal(45), Decimal(-45)) # -
1034 self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs
1035
Mark Dickinson2fc92632008-02-06 22:10:50 +00001036 def test_nan_comparisons(self):
1037 n = Decimal('NaN')
1038 s = Decimal('sNaN')
1039 i = Decimal('Inf')
1040 f = Decimal('2')
1041 for x, y in [(n, n), (n, i), (i, n), (n, f), (f, n),
1042 (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s)]:
1043 self.assert_(x != y)
1044 self.assert_(not (x == y))
1045 self.assert_(not (x < y))
1046 self.assert_(not (x <= y))
1047 self.assert_(not (x > y))
1048 self.assert_(not (x >= y))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001049
1050# The following are two functions used to test threading in the next class
1051
1052def thfunc1(cls):
1053 d1 = Decimal(1)
1054 d3 = Decimal(3)
Facundo Batista64156672008-03-22 02:45:37 +00001055 test1 = d1/d3
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001056 cls.synchro.wait()
Facundo Batista64156672008-03-22 02:45:37 +00001057 test2 = d1/d3
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001058 cls.finish1.set()
Facundo Batista64156672008-03-22 02:45:37 +00001059
Facundo Batistaee340e52008-05-02 17:39:00 +00001060 cls.assertEqual(test1, Decimal('0.3333333333333333333333333333'))
1061 cls.assertEqual(test2, Decimal('0.3333333333333333333333333333'))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001062 return
1063
1064def thfunc2(cls):
1065 d1 = Decimal(1)
1066 d3 = Decimal(3)
Facundo Batista64156672008-03-22 02:45:37 +00001067 test1 = d1/d3
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001068 thiscontext = getcontext()
1069 thiscontext.prec = 18
Facundo Batista64156672008-03-22 02:45:37 +00001070 test2 = d1/d3
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001071 cls.synchro.set()
1072 cls.finish2.set()
Facundo Batista64156672008-03-22 02:45:37 +00001073
Facundo Batistaee340e52008-05-02 17:39:00 +00001074 cls.assertEqual(test1, Decimal('0.3333333333333333333333333333'))
Facundo Batista64156672008-03-22 02:45:37 +00001075 cls.assertEqual(test2, Decimal('0.333333333333333333'))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001076 return
1077
1078
1079class DecimalUseOfContextTest(unittest.TestCase):
1080 '''Unit tests for Use of Context cases in Decimal.'''
1081
Raymond Hettinger7e71fa52004-12-18 19:07:19 +00001082 try:
1083 import threading
1084 except ImportError:
1085 threading = None
1086
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001087 # Take care executing this test from IDLE, there's an issue in threading
1088 # that hangs IDLE and I couldn't find it
1089
1090 def test_threading(self):
1091 #Test the "threading isolation" of a Context.
1092
1093 self.synchro = threading.Event()
1094 self.finish1 = threading.Event()
1095 self.finish2 = threading.Event()
1096
1097 th1 = threading.Thread(target=thfunc1, args=(self,))
1098 th2 = threading.Thread(target=thfunc2, args=(self,))
1099
1100 th1.start()
1101 th2.start()
1102
1103 self.finish1.wait()
Thomas Woutersb3e6e8c2007-09-19 17:27:29 +00001104 self.finish2.wait()
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001105 return
1106
Raymond Hettinger7e71fa52004-12-18 19:07:19 +00001107 if threading is None:
1108 del test_threading
1109
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001110
1111class DecimalUsabilityTest(unittest.TestCase):
1112 '''Unit tests for Usability cases of Decimal.'''
1113
1114 def test_comparison_operators(self):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001115
1116 da = Decimal('23.42')
1117 db = Decimal('23.42')
1118 dc = Decimal('45')
1119
1120 #two Decimals
1121 self.failUnless(dc > da)
1122 self.failUnless(dc >= da)
1123 self.failUnless(da < dc)
1124 self.failUnless(da <= dc)
1125 self.failUnless(da == db)
1126 self.failUnless(da != dc)
1127 self.failUnless(da <= db)
1128 self.failUnless(da >= db)
1129 self.assertEqual(cmp(dc,da), 1)
1130 self.assertEqual(cmp(da,dc), -1)
1131 self.assertEqual(cmp(da,db), 0)
1132
1133 #a Decimal and an int
1134 self.failUnless(dc > 23)
1135 self.failUnless(23 < dc)
1136 self.failUnless(dc == 45)
1137 self.assertEqual(cmp(dc,23), 1)
1138 self.assertEqual(cmp(23,dc), -1)
1139 self.assertEqual(cmp(dc,45), 0)
1140
1141 #a Decimal and uncomparable
Raymond Hettinger0aeac102004-07-05 22:53:03 +00001142 self.assertNotEqual(da, 'ugly')
1143 self.assertNotEqual(da, 32.7)
1144 self.assertNotEqual(da, object())
1145 self.assertNotEqual(da, object)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001146
Raymond Hettinger0aeac102004-07-05 22:53:03 +00001147 # sortable
1148 a = map(Decimal, xrange(100))
1149 b = a[:]
1150 random.shuffle(a)
1151 a.sort()
1152 self.assertEqual(a, b)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001153
Facundo Batista353750c2007-09-13 18:13:15 +00001154 # with None
1155 self.assertFalse(Decimal(1) < None)
1156 self.assertTrue(Decimal(1) > None)
1157
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001158 def test_copy_and_deepcopy_methods(self):
1159 d = Decimal('43.24')
1160 c = copy.copy(d)
1161 self.assertEqual(id(c), id(d))
1162 dc = copy.deepcopy(d)
1163 self.assertEqual(id(dc), id(d))
1164
1165 def test_hash_method(self):
1166 #just that it's hashable
1167 hash(Decimal(23))
Facundo Batista8c202442007-09-19 17:53:25 +00001168
1169 test_values = [Decimal(sign*(2**m + n))
1170 for m in [0, 14, 15, 16, 17, 30, 31,
1171 32, 33, 62, 63, 64, 65, 66]
1172 for n in range(-10, 10)
1173 for sign in [-1, 1]]
1174 test_values.extend([
1175 Decimal("-0"), # zeros
1176 Decimal("0.00"),
1177 Decimal("-0.000"),
1178 Decimal("0E10"),
1179 Decimal("-0E12"),
1180 Decimal("10.0"), # negative exponent
1181 Decimal("-23.00000"),
1182 Decimal("1230E100"), # positive exponent
1183 Decimal("-4.5678E50"),
1184 # a value for which hash(n) != hash(n % (2**64-1))
1185 # in Python pre-2.6
1186 Decimal(2**64 + 2**32 - 1),
1187 # selection of values which fail with the old (before
1188 # version 2.6) long.__hash__
1189 Decimal("1.634E100"),
1190 Decimal("90.697E100"),
1191 Decimal("188.83E100"),
1192 Decimal("1652.9E100"),
1193 Decimal("56531E100"),
1194 ])
1195
1196 # check that hash(d) == hash(int(d)) for integral values
1197 for value in test_values:
1198 self.assertEqual(hash(value), hash(int(value)))
1199
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001200 #the same hash that to an int
1201 self.assertEqual(hash(Decimal(23)), hash(23))
Raymond Hettingerbea3f6f2005-03-15 04:59:17 +00001202 self.assertRaises(TypeError, hash, Decimal('NaN'))
1203 self.assert_(hash(Decimal('Inf')))
1204 self.assert_(hash(Decimal('-Inf')))
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001205
Facundo Batista52b25792008-01-08 12:25:20 +00001206 # check that the value of the hash doesn't depend on the
1207 # current context (issue #1757)
1208 c = getcontext()
1209 old_precision = c.prec
1210 x = Decimal("123456789.1")
1211
1212 c.prec = 6
1213 h1 = hash(x)
1214 c.prec = 10
1215 h2 = hash(x)
1216 c.prec = 16
1217 h3 = hash(x)
1218
1219 self.assertEqual(h1, h2)
1220 self.assertEqual(h1, h3)
1221 c.prec = old_precision
1222
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001223 def test_min_and_max_methods(self):
1224
1225 d1 = Decimal('15.32')
1226 d2 = Decimal('28.5')
1227 l1 = 15
1228 l2 = 28
1229
1230 #between Decimals
1231 self.failUnless(min(d1,d2) is d1)
1232 self.failUnless(min(d2,d1) is d1)
1233 self.failUnless(max(d1,d2) is d2)
1234 self.failUnless(max(d2,d1) is d2)
1235
1236 #between Decimal and long
1237 self.failUnless(min(d1,l2) is d1)
1238 self.failUnless(min(l2,d1) is d1)
1239 self.failUnless(max(l1,d2) is d2)
1240 self.failUnless(max(d2,l1) is d2)
1241
1242 def test_as_nonzero(self):
1243 #as false
1244 self.failIf(Decimal(0))
1245 #as true
1246 self.failUnless(Decimal('0.372'))
1247
1248 def test_tostring_methods(self):
1249 #Test str and repr methods.
1250
1251 d = Decimal('15.32')
1252 self.assertEqual(str(d), '15.32') # str
Raymond Hettingerabe32372008-02-14 02:41:22 +00001253 self.assertEqual(repr(d), "Decimal('15.32')") # repr
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001254
Mark Dickinson8e85ffa2008-03-25 18:47:59 +00001255 # result type of string methods should be str, not unicode
1256 unicode_inputs = [u'123.4', u'0.5E2', u'Infinity', u'sNaN',
1257 u'-0.0E100', u'-NaN001', u'-Inf']
1258
1259 for u in unicode_inputs:
1260 d = Decimal(u)
1261 self.assertEqual(type(str(d)), str)
1262 self.assertEqual(type(repr(d)), str)
1263 self.assertEqual(type(d.to_eng_string()), str)
1264
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001265 def test_tonum_methods(self):
1266 #Test float, int and long methods.
1267
1268 d1 = Decimal('66')
1269 d2 = Decimal('15.32')
1270
1271 #int
1272 self.assertEqual(int(d1), 66)
1273 self.assertEqual(int(d2), 15)
1274
1275 #long
1276 self.assertEqual(long(d1), 66)
1277 self.assertEqual(long(d2), 15)
1278
1279 #float
1280 self.assertEqual(float(d1), 66)
1281 self.assertEqual(float(d2), 15.32)
1282
1283 def test_eval_round_trip(self):
1284
1285 #with zero
1286 d = Decimal( (0, (0,), 0) )
1287 self.assertEqual(d, eval(repr(d)))
1288
1289 #int
1290 d = Decimal( (1, (4, 5), 0) )
1291 self.assertEqual(d, eval(repr(d)))
1292
1293 #float
1294 d = Decimal( (0, (4, 5, 3, 4), -2) )
1295 self.assertEqual(d, eval(repr(d)))
1296
1297 #weird
1298 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
1299 self.assertEqual(d, eval(repr(d)))
1300
1301 def test_as_tuple(self):
1302
1303 #with zero
1304 d = Decimal(0)
1305 self.assertEqual(d.as_tuple(), (0, (0,), 0) )
1306
1307 #int
1308 d = Decimal(-45)
1309 self.assertEqual(d.as_tuple(), (1, (4, 5), 0) )
1310
1311 #complicated string
1312 d = Decimal("-4.34913534E-17")
1313 self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
1314
1315 #inf
1316 d = Decimal("Infinity")
1317 self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
1318
Facundo Batista9b5e2312007-10-19 19:25:57 +00001319 #leading zeros in coefficient should be stripped
1320 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
1321 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) )
1322 d = Decimal( (1, (0, 0, 0), 37) )
1323 self.assertEqual(d.as_tuple(), (1, (0,), 37))
1324 d = Decimal( (1, (), 37) )
1325 self.assertEqual(d.as_tuple(), (1, (0,), 37))
1326
1327 #leading zeros in NaN diagnostic info should be stripped
1328 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
1329 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') )
1330 d = Decimal( (1, (0, 0, 0), 'N') )
1331 self.assertEqual(d.as_tuple(), (1, (), 'N') )
1332 d = Decimal( (1, (), 'n') )
1333 self.assertEqual(d.as_tuple(), (1, (), 'n') )
1334
1335 #coefficient in infinity should be ignored
1336 d = Decimal( (0, (4, 5, 3, 4), 'F') )
1337 self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
1338 d = Decimal( (1, (0, 2, 7, 1), 'F') )
1339 self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
1340
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001341 def test_immutability_operations(self):
1342 # Do operations and check that it didn't change change internal objects.
1343
1344 d1 = Decimal('-25e55')
1345 b1 = Decimal('-25e55')
Facundo Batista353750c2007-09-13 18:13:15 +00001346 d2 = Decimal('33e+33')
1347 b2 = Decimal('33e+33')
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001348
1349 def checkSameDec(operation, useOther=False):
1350 if useOther:
1351 eval("d1." + operation + "(d2)")
1352 self.assertEqual(d1._sign, b1._sign)
1353 self.assertEqual(d1._int, b1._int)
1354 self.assertEqual(d1._exp, b1._exp)
1355 self.assertEqual(d2._sign, b2._sign)
1356 self.assertEqual(d2._int, b2._int)
1357 self.assertEqual(d2._exp, b2._exp)
1358 else:
1359 eval("d1." + operation + "()")
1360 self.assertEqual(d1._sign, b1._sign)
1361 self.assertEqual(d1._int, b1._int)
1362 self.assertEqual(d1._exp, b1._exp)
1363 return
1364
1365 Decimal(d1)
1366 self.assertEqual(d1._sign, b1._sign)
1367 self.assertEqual(d1._int, b1._int)
1368 self.assertEqual(d1._exp, b1._exp)
1369
1370 checkSameDec("__abs__")
1371 checkSameDec("__add__", True)
1372 checkSameDec("__div__", True)
1373 checkSameDec("__divmod__", True)
Mark Dickinson2fc92632008-02-06 22:10:50 +00001374 checkSameDec("__eq__", True)
1375 checkSameDec("__ne__", True)
1376 checkSameDec("__le__", True)
1377 checkSameDec("__lt__", True)
1378 checkSameDec("__ge__", True)
1379 checkSameDec("__gt__", True)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001380 checkSameDec("__float__")
1381 checkSameDec("__floordiv__", True)
1382 checkSameDec("__hash__")
1383 checkSameDec("__int__")
Raymond Hettinger5a053642008-01-24 19:05:29 +00001384 checkSameDec("__trunc__")
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001385 checkSameDec("__long__")
1386 checkSameDec("__mod__", True)
1387 checkSameDec("__mul__", True)
1388 checkSameDec("__neg__")
1389 checkSameDec("__nonzero__")
1390 checkSameDec("__pos__")
1391 checkSameDec("__pow__", True)
1392 checkSameDec("__radd__", True)
1393 checkSameDec("__rdiv__", True)
1394 checkSameDec("__rdivmod__", True)
1395 checkSameDec("__repr__")
1396 checkSameDec("__rfloordiv__", True)
1397 checkSameDec("__rmod__", True)
1398 checkSameDec("__rmul__", True)
1399 checkSameDec("__rpow__", True)
1400 checkSameDec("__rsub__", True)
1401 checkSameDec("__str__")
1402 checkSameDec("__sub__", True)
1403 checkSameDec("__truediv__", True)
1404 checkSameDec("adjusted")
1405 checkSameDec("as_tuple")
1406 checkSameDec("compare", True)
1407 checkSameDec("max", True)
1408 checkSameDec("min", True)
1409 checkSameDec("normalize")
1410 checkSameDec("quantize", True)
1411 checkSameDec("remainder_near", True)
1412 checkSameDec("same_quantum", True)
1413 checkSameDec("sqrt")
1414 checkSameDec("to_eng_string")
1415 checkSameDec("to_integral")
1416
Facundo Batista6c398da2007-09-17 17:30:13 +00001417 def test_subclassing(self):
1418 # Different behaviours when subclassing Decimal
1419
1420 class MyDecimal(Decimal):
1421 pass
1422
1423 d1 = MyDecimal(1)
1424 d2 = MyDecimal(2)
1425 d = d1 + d2
1426 self.assertTrue(type(d) is Decimal)
1427
1428 d = d1.max(d2)
1429 self.assertTrue(type(d) is Decimal)
1430
Mark Dickinson3b24ccb2008-03-25 14:33:23 +00001431 def test_implicit_context(self):
1432 # Check results when context given implicitly. (Issue 2478)
1433 c = getcontext()
1434 self.assertEqual(str(Decimal(0).sqrt()),
1435 str(c.sqrt(Decimal(0))))
1436
Facundo Batista6c398da2007-09-17 17:30:13 +00001437
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001438class DecimalPythonAPItests(unittest.TestCase):
1439
Raymond Hettinger2c8585b2009-02-03 03:37:03 +00001440 def test_abc(self):
1441 self.assert_(issubclass(Decimal, numbers.Number))
1442 self.assert_(not issubclass(Decimal, numbers.Real))
1443 self.assert_(isinstance(Decimal(0), numbers.Number))
1444 self.assert_(not isinstance(Decimal(0), numbers.Real))
1445
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001446 def test_pickle(self):
1447 d = Decimal('-3.141590000')
1448 p = pickle.dumps(d)
1449 e = pickle.loads(p)
1450 self.assertEqual(d, e)
1451
Raymond Hettinger5548be22004-07-05 18:49:38 +00001452 def test_int(self):
Raymond Hettinger605ed022004-11-24 07:28:48 +00001453 for x in range(-250, 250):
1454 s = '%0.2f' % (x / 100.0)
Raymond Hettinger5548be22004-07-05 18:49:38 +00001455 # should work the same as for floats
1456 self.assertEqual(int(Decimal(s)), int(float(s)))
Raymond Hettinger605ed022004-11-24 07:28:48 +00001457 # should work the same as to_integral in the ROUND_DOWN mode
Raymond Hettinger5548be22004-07-05 18:49:38 +00001458 d = Decimal(s)
Raymond Hettinger605ed022004-11-24 07:28:48 +00001459 r = d.to_integral(ROUND_DOWN)
Raymond Hettinger5548be22004-07-05 18:49:38 +00001460 self.assertEqual(Decimal(int(d)), r)
1461
Raymond Hettinger5a053642008-01-24 19:05:29 +00001462 def test_trunc(self):
1463 for x in range(-250, 250):
1464 s = '%0.2f' % (x / 100.0)
1465 # should work the same as for floats
1466 self.assertEqual(int(Decimal(s)), int(float(s)))
1467 # should work the same as to_integral in the ROUND_DOWN mode
1468 d = Decimal(s)
1469 r = d.to_integral(ROUND_DOWN)
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +00001470 self.assertEqual(Decimal(math.trunc(d)), r)
Raymond Hettinger5a053642008-01-24 19:05:29 +00001471
Raymond Hettingerf4d85972009-01-03 19:02:23 +00001472 def test_from_float(self):
1473
1474 class MyDecimal(Decimal):
1475 pass
1476
1477 r = MyDecimal.from_float(0.1)
1478 self.assertEqual(type(r), MyDecimal)
1479 self.assertEqual(str(r),
1480 '0.1000000000000000055511151231257827021181583404541015625')
1481 bigint = 12345678901234567890123456789
1482 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
1483 self.assert_(MyDecimal.from_float(float('nan')).is_qnan())
1484 self.assert_(MyDecimal.from_float(float('inf')).is_infinite())
1485 self.assert_(MyDecimal.from_float(float('-inf')).is_infinite())
1486 self.assertEqual(str(MyDecimal.from_float(float('nan'))),
1487 str(Decimal('NaN')))
1488 self.assertEqual(str(MyDecimal.from_float(float('inf'))),
1489 str(Decimal('Infinity')))
1490 self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
1491 str(Decimal('-Infinity')))
1492 self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
1493 for i in range(200):
1494 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
1495 self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip
1496
1497 def test_create_decimal_from_float(self):
1498 context = Context(prec=5, rounding=ROUND_DOWN)
1499 self.assertEqual(
1500 context.create_decimal_from_float(math.pi),
1501 Decimal('3.1415')
1502 )
1503 context = Context(prec=5, rounding=ROUND_UP)
1504 self.assertEqual(
1505 context.create_decimal_from_float(math.pi),
1506 Decimal('3.1416')
1507 )
1508 context = Context(prec=5, traps=[Inexact])
1509 self.assertRaises(
1510 Inexact,
1511 context.create_decimal_from_float,
1512 math.pi
1513 )
1514 self.assertEqual(repr(context.create_decimal_from_float(-0.0)),
1515 "Decimal('-0')")
1516 self.assertEqual(repr(context.create_decimal_from_float(1.0)),
1517 "Decimal('1')")
1518 self.assertEqual(repr(context.create_decimal_from_float(10)),
1519 "Decimal('10')")
1520
Raymond Hettingerd9c0a7a2004-07-03 10:02:28 +00001521class ContextAPItests(unittest.TestCase):
1522
1523 def test_pickle(self):
1524 c = Context()
1525 e = pickle.loads(pickle.dumps(c))
1526 for k in vars(c):
1527 v1 = vars(c)[k]
1528 v2 = vars(e)[k]
1529 self.assertEqual(v1, v2)
1530
Raymond Hettinger0aeac102004-07-05 22:53:03 +00001531 def test_equality_with_other_types(self):
1532 self.assert_(Decimal(10) in ['a', 1.0, Decimal(10), (1,2), {}])
1533 self.assert_(Decimal(10) not in ['a', 1.0, (1,2), {}])
1534
Raymond Hettinger955d2b22004-08-08 20:17:45 +00001535 def test_copy(self):
1536 # All copies should be deep
1537 c = Context()
1538 d = c.copy()
1539 self.assertNotEqual(id(c), id(d))
1540 self.assertNotEqual(id(c.flags), id(d.flags))
1541 self.assertNotEqual(id(c.traps), id(d.traps))
1542
Nick Coghlan8b6999b2006-08-31 12:00:43 +00001543class WithStatementTest(unittest.TestCase):
1544 # Can't do these as docstrings until Python 2.6
1545 # as doctest can't handle __future__ statements
Nick Coghlan8b6999b2006-08-31 12:00:43 +00001546
1547 def test_localcontext(self):
Nick Coghlanced12182006-09-02 03:54:17 +00001548 # Use a copy of the current context in the block
Nick Coghlan8b6999b2006-08-31 12:00:43 +00001549 orig_ctx = getcontext()
1550 with localcontext() as enter_ctx:
1551 set_ctx = getcontext()
1552 final_ctx = getcontext()
1553 self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
1554 self.assert_(orig_ctx is not set_ctx, 'did not copy the context')
1555 self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
1556
1557 def test_localcontextarg(self):
Nick Coghlanced12182006-09-02 03:54:17 +00001558 # Use a copy of the supplied context in the block
Nick Coghlan8b6999b2006-08-31 12:00:43 +00001559 orig_ctx = getcontext()
1560 new_ctx = Context(prec=42)
1561 with localcontext(new_ctx) as enter_ctx:
1562 set_ctx = getcontext()
1563 final_ctx = getcontext()
1564 self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
1565 self.assert_(set_ctx.prec == new_ctx.prec, 'did not set correct context')
1566 self.assert_(new_ctx is not set_ctx, 'did not copy the context')
1567 self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
1568
Facundo Batista353750c2007-09-13 18:13:15 +00001569class ContextFlags(unittest.TestCase):
1570 def test_flags_irrelevant(self):
1571 # check that the result (numeric result + flags raised) of an
1572 # arithmetic operation doesn't depend on the current flags
1573
1574 context = Context(prec=9, Emin = -999999999, Emax = 999999999,
1575 rounding=ROUND_HALF_EVEN, traps=[], flags=[])
1576
1577 # operations that raise various flags, in the form (function, arglist)
1578 operations = [
1579 (context._apply, [Decimal("100E-1000000009")]),
1580 (context.sqrt, [Decimal(2)]),
1581 (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
1582 (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
1583 (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
1584 ]
1585
1586 # try various flags individually, then a whole lot at once
1587 flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
1588 [Inexact, Rounded, Underflow, Clamped, Subnormal]]
1589
1590 for fn, args in operations:
1591 # find answer and flags raised using a clean context
1592 context.clear_flags()
1593 ans = fn(*args)
1594 flags = [k for k, v in context.flags.items() if v]
1595
1596 for extra_flags in flagsets:
1597 # set flags, before calling operation
1598 context.clear_flags()
1599 for flag in extra_flags:
1600 context._raise_error(flag)
1601 new_ans = fn(*args)
1602
1603 # flags that we expect to be set after the operation
1604 expected_flags = list(flags)
1605 for flag in extra_flags:
1606 if flag not in expected_flags:
1607 expected_flags.append(flag)
1608 expected_flags.sort()
1609
1610 # flags we actually got
1611 new_flags = [k for k,v in context.flags.items() if v]
1612 new_flags.sort()
1613
1614 self.assertEqual(ans, new_ans,
1615 "operation produces different answers depending on flags set: " +
1616 "expected %s, got %s." % (ans, new_ans))
1617 self.assertEqual(new_flags, expected_flags,
1618 "operation raises different flags depending on flags set: " +
1619 "expected %s, got %s" % (expected_flags, new_flags))
1620
1621def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001622 """ Execute the tests.
1623
Raymond Hettingered20ad82004-09-04 20:09:13 +00001624 Runs all arithmetic tests if arith is True or if the "decimal" resource
1625 is enabled in regrtest.py
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001626 """
Raymond Hettingered20ad82004-09-04 20:09:13 +00001627
Neal Norwitzce4a9c92006-04-09 08:36:46 +00001628 init()
Facundo Batista353750c2007-09-13 18:13:15 +00001629 global TEST_ALL, DEBUG
Raymond Hettingered20ad82004-09-04 20:09:13 +00001630 TEST_ALL = arith or is_resource_enabled('decimal')
Facundo Batista353750c2007-09-13 18:13:15 +00001631 DEBUG = debug
Raymond Hettingered20ad82004-09-04 20:09:13 +00001632
Facundo Batista353750c2007-09-13 18:13:15 +00001633 if todo_tests is None:
1634 test_classes = [
1635 DecimalExplicitConstructionTest,
1636 DecimalImplicitConstructionTest,
1637 DecimalArithmeticOperatorsTest,
Mark Dickinson1ddf1d82008-02-29 02:16:37 +00001638 DecimalFormatTest,
Facundo Batista353750c2007-09-13 18:13:15 +00001639 DecimalUseOfContextTest,
1640 DecimalUsabilityTest,
1641 DecimalPythonAPItests,
1642 ContextAPItests,
1643 DecimalTest,
1644 WithStatementTest,
1645 ContextFlags
1646 ]
1647 else:
1648 test_classes = [DecimalTest]
1649
1650 # Dynamically build custom test definition for each file in the test
1651 # directory and add the definitions to the DecimalTest class. This
1652 # procedure insures that new files do not get skipped.
1653 for filename in os.listdir(directory):
1654 if '.decTest' not in filename or filename.startswith("."):
1655 continue
1656 head, tail = filename.split('.')
1657 if todo_tests is not None and head not in todo_tests:
1658 continue
1659 tester = lambda self, f=filename: self.eval_file(directory + f)
1660 setattr(DecimalTest, 'test_' + head, tester)
1661 del filename, head, tail, tester
1662
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001663
Tim Peters46cc7022006-03-31 04:11:16 +00001664 try:
1665 run_unittest(*test_classes)
Facundo Batista353750c2007-09-13 18:13:15 +00001666 if todo_tests is None:
1667 import decimal as DecimalModule
1668 run_doctest(DecimalModule, verbose)
Tim Peters46cc7022006-03-31 04:11:16 +00001669 finally:
1670 setcontext(ORIGINAL_CONTEXT)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001671
1672if __name__ == '__main__':
Facundo Batista353750c2007-09-13 18:13:15 +00001673 import optparse
1674 p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]")
1675 p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test')
1676 p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests')
1677 (opt, args) = p.parse_args()
1678
1679 if opt.skip:
1680 test_main(arith=False, verbose=True)
1681 elif args:
1682 test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
Raymond Hettinger7c85fa42004-07-01 11:01:35 +00001683 else:
Facundo Batista353750c2007-09-13 18:13:15 +00001684 test_main(arith=True, verbose=True)