summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_decimal.py
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2007-09-19 03:06:30 (GMT)
committerThomas Wouters <thomas@python.org>2007-09-19 03:06:30 (GMT)
commit1b7f891f416830d0c46ca1c9e1bfe62f05cda655 (patch)
treefc092d34bebe4223a3026d1992bf17cc0ea2b2b0 /Lib/test/test_decimal.py
parent782d6b44a1cc003106bac3a310d3e4ac3768adbd (diff)
downloadcpython-1b7f891f416830d0c46ca1c9e1bfe62f05cda655.zip
cpython-1b7f891f416830d0c46ca1c9e1bfe62f05cda655.tar.gz
cpython-1b7f891f416830d0c46ca1c9e1bfe62f05cda655.tar.bz2
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r58096 | brett.cannon | 2007-09-10 23:38:27 +0200 (Mon, 10 Sep 2007) | 4 lines Fix a possible segfault from recursing too deep to get the repr of a list. Closes issue #1096. ........ r58097 | bill.janssen | 2007-09-10 23:51:02 +0200 (Mon, 10 Sep 2007) | 33 lines More work on SSL support. * Much expanded test suite: All protocols tested against all other protocols. All protocols tested with all certificate options. Tests for bad key and bad cert. Test of STARTTLS functionality. Test of RAND_* functions. * Fixes for threading/malloc bug. * Issue 1065 fixed: sslsocket class renamed to SSLSocket. sslerror class renamed to SSLError. Function "wrap_socket" now used to wrap an existing socket. * Issue 1583946 finally fixed: Support for subjectAltName added. Subject name now returned as proper DN list of RDNs. * SSLError exported from socket as "sslerror". * RAND_* functions properly exported from ssl.py. * Documentation improved: Example of how to create a self-signed certificate. Better indexing. ........ r58098 | guido.van.rossum | 2007-09-11 00:02:25 +0200 (Tue, 11 Sep 2007) | 9 lines Patch # 1140 (my code, approved by Effbot). Make sure the type of the return value of re.sub(x, y, z) is the type of y+x (i.e. unicode if either is unicode, str if they are both str) even if there are no substitutions or if x==z (which triggered various special cases in join_list()). Could be backported to 2.5; no need to port to 3.0. ........ r58099 | guido.van.rossum | 2007-09-11 00:36:02 +0200 (Tue, 11 Sep 2007) | 8 lines Patch # 1026 by Benjamin Aranguren (with Alex Martelli): Backport abc.py and isinstance/issubclass overloading to 2.6. I had to backport test_typechecks.py myself, and make one small change to abc.py to avoid duplicate work when x.__class__ and type(x) are the same. ........ r58100 | bill.janssen | 2007-09-11 01:41:24 +0200 (Tue, 11 Sep 2007) | 3 lines A better way of finding an open port to test with. ........ r58101 | bill.janssen | 2007-09-11 03:09:19 +0200 (Tue, 11 Sep 2007) | 4 lines Make sure test_ssl doesn't reference the ssl module in a context where it can't be imported. ........ r58102 | bill.janssen | 2007-09-11 04:42:07 +0200 (Tue, 11 Sep 2007) | 3 lines Fix some documentation bugs. ........ r58103 | nick.coghlan | 2007-09-11 16:01:18 +0200 (Tue, 11 Sep 2007) | 1 line Always use the -E flag when spawning subprocesses in test_cmd_line (Issue 1056) ........ r58106 | thomas.heller | 2007-09-11 21:17:48 +0200 (Tue, 11 Sep 2007) | 3 lines Disable some tests that fail on the 'ppc Debian unstable' buildbot to find out if they cause the segfault on the 'alpha Debian' machine. ........ r58108 | brett.cannon | 2007-09-11 23:02:28 +0200 (Tue, 11 Sep 2007) | 6 lines Generators had their throw() method allowing string exceptions. That's a no-no. Fixes issue #1147. Need to fix 2.5 to raise a proper warning if a string exception is passed in. ........ r58112 | georg.brandl | 2007-09-12 20:03:51 +0200 (Wed, 12 Sep 2007) | 3 lines New documentation page for the bdb module. (This doesn't need to be merged to Py3k.) ........ r58114 | georg.brandl | 2007-09-12 20:05:57 +0200 (Wed, 12 Sep 2007) | 2 lines Bug #1152: use non-deprecated name in example. ........ r58115 | georg.brandl | 2007-09-12 20:08:33 +0200 (Wed, 12 Sep 2007) | 2 lines Fix #1122: wrong return type documented for various _Size() functions. ........ r58117 | georg.brandl | 2007-09-12 20:10:56 +0200 (Wed, 12 Sep 2007) | 2 lines Fix #1139: PyFile_Encoding really is PyFile_SetEncoding. ........ r58119 | georg.brandl | 2007-09-12 20:29:18 +0200 (Wed, 12 Sep 2007) | 2 lines bug #1154: release memory allocated by "es" PyArg_ParseTuple format specifier. ........ r58121 | bill.janssen | 2007-09-12 20:52:05 +0200 (Wed, 12 Sep 2007) | 1 line root certificate for https://svn.python.org/, used in test_ssl ........ r58122 | georg.brandl | 2007-09-12 21:00:07 +0200 (Wed, 12 Sep 2007) | 3 lines Bug #1153: repr.repr() now doesn't require set and dictionary items to be orderable to properly represent them. ........ r58125 | georg.brandl | 2007-09-12 21:29:28 +0200 (Wed, 12 Sep 2007) | 4 lines #1120: put explicit version in the shebang lines of pydoc, idle and smtpd.py scripts that are installed by setup.py. That way, they work when only "make altinstall" is used. ........ r58139 | mark.summerfield | 2007-09-13 16:54:30 +0200 (Thu, 13 Sep 2007) | 9 lines Replaced variable o with obj in operator.rst because o is easy to confuse. Added a note about Python 3's collections.Mapping etc., above section that describes isMappingType() etc. Added xrefs between os, os.path, fileinput, and open(). ........ r58143 | facundo.batista | 2007-09-13 20:13:15 +0200 (Thu, 13 Sep 2007) | 7 lines Merged the decimal-branch (revisions 54886 to 58140). Decimal is now fully updated to the latests Decimal Specification (v1.66) and the latests test cases (v2.56). Thanks to Mark Dickinson for all his help during this process. ........ r58145 | facundo.batista | 2007-09-13 20:42:09 +0200 (Thu, 13 Sep 2007) | 7 lines Put the parameter watchexp back in (changed watchexp from an int to a bool). Also second argument to watchexp is now converted to Decimal, just as with all the other two-argument operations. Thanks Mark Dickinson. ........ r58147 | andrew.kuchling | 2007-09-14 00:49:34 +0200 (Fri, 14 Sep 2007) | 1 line Add various items ........ r58148 | andrew.kuchling | 2007-09-14 00:50:10 +0200 (Fri, 14 Sep 2007) | 1 line Make target unique ........ r58154 | facundo.batista | 2007-09-14 20:58:34 +0200 (Fri, 14 Sep 2007) | 3 lines Included the new functions, and new descriptions. ........ r58155 | thomas.heller | 2007-09-14 21:40:35 +0200 (Fri, 14 Sep 2007) | 2 lines ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris. Fixes issue #1777530; will backport to release25-maint. ........ r58159 | facundo.batista | 2007-09-14 23:29:52 +0200 (Fri, 14 Sep 2007) | 3 lines Some additions (examples and a bit on the tutorial). ........ r58160 | georg.brandl | 2007-09-15 18:53:36 +0200 (Sat, 15 Sep 2007) | 2 lines Remove bdb from the "undocumented modules" list. ........ r58164 | bill.janssen | 2007-09-17 00:06:00 +0200 (Mon, 17 Sep 2007) | 15 lines Add support for asyncore server-side SSL support. This requires adding the 'makefile' method to ssl.SSLSocket, and importing the requisite fakefile class from socket.py, and making the appropriate changes to it to make it use the SSL connection. Added sample HTTPS server to test_ssl.py, and test that uses it. Change SSL tests to use https://svn.python.org/, instead of www.sf.net and pop.gmail.com. Added utility function to ssl module, get_server_certificate, to wrap up the several things to be done to pull a certificate from a remote server. ........ r58173 | bill.janssen | 2007-09-17 01:16:46 +0200 (Mon, 17 Sep 2007) | 1 line use binary mode when reading files for testAsyncore to make Windows happy ........ r58175 | raymond.hettinger | 2007-09-17 02:55:00 +0200 (Mon, 17 Sep 2007) | 7 lines Sync-up named tuples with the latest version of the ASPN recipe. Allows optional commas in the field-name spec (help when named tuples are used in conjuction with sql queries). Adds the __fields__ attribute for introspection and to support conversion to dictionary form. Adds a __replace__() method similar to str.replace() but using a named field as a target. Clean-up spelling and presentation in doc-strings. ........ r58176 | brett.cannon | 2007-09-17 05:28:34 +0200 (Mon, 17 Sep 2007) | 5 lines Add a bunch of GIL release/acquire points in tp_print implementations and for PyObject_Print(). Closes issue #1164. ........ r58177 | sean.reifschneider | 2007-09-17 07:45:04 +0200 (Mon, 17 Sep 2007) | 2 lines issue1597011: Fix for bz2 module corner-case error due to error checking bug. ........ r58180 | facundo.batista | 2007-09-17 18:26:50 +0200 (Mon, 17 Sep 2007) | 3 lines Decimal is updated, :) ........ r58181 | facundo.batista | 2007-09-17 19:30:13 +0200 (Mon, 17 Sep 2007) | 5 lines The methods always return Decimal classes, even if they're executed through a subclass (thanks Mark Dickinson). Added a bit of testing for this. ........ r58183 | sean.reifschneider | 2007-09-17 22:53:21 +0200 (Mon, 17 Sep 2007) | 2 lines issue1082: Fixing platform and system for Vista. ........ r58185 | andrew.kuchling | 2007-09-18 03:36:16 +0200 (Tue, 18 Sep 2007) | 1 line Add item; sort properly ........ r58186 | raymond.hettinger | 2007-09-18 05:33:19 +0200 (Tue, 18 Sep 2007) | 1 line Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works. ........ r58192 | georg.brandl | 2007-09-18 09:24:40 +0200 (Tue, 18 Sep 2007) | 2 lines A bit of reordering, also show more subheadings in the lang ref index. ........ r58193 | facundo.batista | 2007-09-18 18:53:18 +0200 (Tue, 18 Sep 2007) | 4 lines Speed up of the various division operations (remainder, divide, divideint and divmod). Thanks Mark Dickinson. ........ r58197 | raymond.hettinger | 2007-09-19 00:18:02 +0200 (Wed, 19 Sep 2007) | 1 line Cleanup docs for NamedTuple. ........
Diffstat (limited to 'Lib/test/test_decimal.py')
-rw-r--r--Lib/test/test_decimal.py232
1 files changed, 182 insertions, 50 deletions
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 1923317..41d1dd5 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -65,9 +65,7 @@ skip_expected = not os.path.isdir(directory)
# Slower, since it runs some things several times.
EXTENDEDERRORTEST = False
-
#Map the test cases' error names to the actual errors
-
ErrorNames = {'clamped' : Clamped,
'conversion_syntax' : InvalidOperation,
'division_by_zero' : DivisionByZero,
@@ -92,20 +90,62 @@ RoundingDict = {'ceiling' : ROUND_CEILING, #Maps test-case names to roundings.
'half_down' : ROUND_HALF_DOWN,
'half_even' : ROUND_HALF_EVEN,
'half_up' : ROUND_HALF_UP,
- 'up' : ROUND_UP}
+ 'up' : ROUND_UP,
+ '05up' : ROUND_05UP}
# Name adapter to be able to change the Decimal and Context
# interface without changing the test files from Cowlishaw
nameAdapter = {'toeng':'to_eng_string',
'tosci':'to_sci_string',
'samequantum':'same_quantum',
- 'tointegral':'to_integral',
+ 'tointegral':'to_integral_value',
+ 'tointegralx':'to_integral_exact',
'remaindernear':'remainder_near',
'divideint':'divide_int',
'squareroot':'sqrt',
'apply':'_apply',
+ 'class':'number_class',
+ 'comparesig':'compare_signal',
+ 'comparetotal':'compare_total',
+ 'comparetotmag':'compare_total_mag',
+ 'copyabs':'copy_abs',
+ 'copy':'copy_decimal',
+ 'copynegate':'copy_negate',
+ 'copysign':'copy_sign',
+ 'and':'logical_and',
+ 'or':'logical_or',
+ 'xor':'logical_xor',
+ 'invert':'logical_invert',
+ 'maxmag':'max_mag',
+ 'minmag':'min_mag',
+ 'nextminus':'next_minus',
+ 'nextplus':'next_plus',
+ 'nexttoward':'next_toward',
+ 'reduce':'normalize',
}
+# For some operations (currently exp, ln, log10, power), the decNumber
+# reference implementation imposes additional restrictions on the
+# context and operands. These restrictions are not part of the
+# specification; however, the effect of these restrictions does show
+# up in some of the testcases. We skip testcases that violate these
+# restrictions, since Decimal behaves differently from decNumber for
+# these testcases so these testcases would otherwise fail.
+
+decNumberRestricted = ('power', 'ln', 'log10', 'exp')
+DEC_MAX_MATH = 999999
+def outside_decNumber_bounds(v, context):
+ if (context.prec > DEC_MAX_MATH or
+ context.Emax > DEC_MAX_MATH or
+ -context.Emin > DEC_MAX_MATH):
+ return True
+ if not v._is_special and v and (
+ len(v._int) > DEC_MAX_MATH or
+ v.adjusted() > DEC_MAX_MATH or
+ v.adjusted() < 1-2*DEC_MAX_MATH):
+ return True
+ return False
+
class DecimalTest(unittest.TestCase):
"""Class which tests the Decimal class against the test cases.
@@ -142,10 +182,6 @@ class DecimalTest(unittest.TestCase):
#print line
try:
t = self.eval_line(line)
- except InvalidOperation:
- print('Error in test cases:')
- print(line)
- continue
except DecimalException as exception:
#Exception raised where there shoudn't have been one.
self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
@@ -194,7 +230,8 @@ class DecimalTest(unittest.TestCase):
Sides = s.split('->')
L = Sides[0].strip().split()
id = L[0]
-# print id,
+ if DEBUG:
+ print("Test ", id, end=" ")
funct = L[1].lower()
valstemp = L[2:]
L = Sides[1].strip().split()
@@ -246,11 +283,27 @@ class DecimalTest(unittest.TestCase):
self.context.traps[error] = 0
v = self.context.create_decimal(v)
else:
- v = Decimal(v)
+ v = Decimal(v, self.context)
vals.append(v)
ans = FixQuotes(ans)
+ # skip tests that are related to bounds imposed in the decNumber
+ # reference implementation
+ if fname in decNumberRestricted:
+ if fname == 'power':
+ if not (vals[1]._isinteger() and
+ -1999999997 <= vals[1] <= 999999999):
+ if outside_decNumber_bounds(vals[0], self.context) or \
+ outside_decNumber_bounds(vals[1], self.context):
+ #print "Skipping test %s" % s
+ return
+ else:
+ if outside_decNumber_bounds(vals[0], self.context):
+ #print "Skipping test %s" % s
+ return
+
+
if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'):
for error in theirexceptions:
self.context.traps[error] = 1
@@ -264,6 +317,8 @@ class DecimalTest(unittest.TestCase):
else:
self.fail("Did not raise %s in %s" % (error, s))
self.context.traps[error] = 0
+ if DEBUG:
+ print("--", self.context)
try:
result = str(funct(*vals))
if fname == 'same_quantum':
@@ -283,8 +338,7 @@ class DecimalTest(unittest.TestCase):
self.assertEqual(result, ans,
'Incorrect answer for ' + s + ' -- got ' + result)
self.assertEqual(myexceptions, theirexceptions,
- 'Incorrect flags set in ' + s + ' -- got ' \
- + str(myexceptions))
+ 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions))
return
def getexceptions(self):
@@ -301,17 +355,6 @@ class DecimalTest(unittest.TestCase):
def change_clamp(self, clamp):
self.context._clamp = clamp
-# Dynamically build custom test definition for each file in the test
-# directory and add the definitions to the DecimalTest class. This
-# procedure insures that new files do not get skipped.
-for filename in os.listdir(directory):
- if '.decTest' not in filename:
- continue
- head, tail = filename.split('.')
- tester = lambda self, f=filename: self.eval_file(directory + f)
- setattr(DecimalTest, 'test_' + head, tester)
- del filename, head, tail, tester
-
# The following classes test the behaviour of Decimal according to PEP 327
@@ -798,7 +841,7 @@ class DecimalUseOfContextTest(unittest.TestCase):
th2.start()
self.finish1.wait()
- self.finish1.wait()
+ self.finish2.wait()
return
if threading is None:
@@ -955,8 +998,8 @@ class DecimalUsabilityTest(unittest.TestCase):
d1 = Decimal('-25e55')
b1 = Decimal('-25e55')
- d2 = Decimal('33e-33')
- b2 = Decimal('33e-33')
+ d2 = Decimal('33e+33')
+ b2 = Decimal('33e+33')
def checkSameDec(operation, useOther=False):
if useOther:
@@ -987,7 +1030,6 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec("__floordiv__", True)
checkSameDec("__hash__")
checkSameDec("__int__")
- checkSameDec("__long__")
checkSameDec("__mod__", True)
checkSameDec("__mul__", True)
checkSameDec("__neg__")
@@ -1018,6 +1060,21 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec("to_eng_string")
checkSameDec("to_integral")
+ def test_subclassing(self):
+ # Different behaviours when subclassing Decimal
+
+ class MyDecimal(Decimal):
+ pass
+
+ d1 = MyDecimal(1)
+ d2 = MyDecimal(2)
+ d = d1 + d2
+ self.assertTrue(type(d) is Decimal)
+
+ d = d1.max(d2)
+ self.assertTrue(type(d) is Decimal)
+
+
class DecimalPythonAPItests(unittest.TestCase):
def test_pickle(self):
@@ -1084,7 +1141,59 @@ class WithStatementTest(unittest.TestCase):
self.assert_(new_ctx is not set_ctx, 'did not copy the context')
self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
-def test_main(arith=False, verbose=None):
+class ContextFlags(unittest.TestCase):
+ def test_flags_irrelevant(self):
+ # check that the result (numeric result + flags raised) of an
+ # arithmetic operation doesn't depend on the current flags
+
+ context = Context(prec=9, Emin = -999999999, Emax = 999999999,
+ rounding=ROUND_HALF_EVEN, traps=[], flags=[])
+
+ # operations that raise various flags, in the form (function, arglist)
+ operations = [
+ (context._apply, [Decimal("100E-1000000009")]),
+ (context.sqrt, [Decimal(2)]),
+ (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
+ (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
+ (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
+ ]
+
+ # try various flags individually, then a whole lot at once
+ flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
+ [Inexact, Rounded, Underflow, Clamped, Subnormal]]
+
+ for fn, args in operations:
+ # find answer and flags raised using a clean context
+ context.clear_flags()
+ ans = fn(*args)
+ flags = [k for k, v in context.flags.items() if v]
+
+ for extra_flags in flagsets:
+ # set flags, before calling operation
+ context.clear_flags()
+ for flag in extra_flags:
+ context._raise_error(flag)
+ new_ans = fn(*args)
+
+ # flags that we expect to be set after the operation
+ expected_flags = list(flags)
+ for flag in extra_flags:
+ if flag not in expected_flags:
+ expected_flags.append(flag)
+ expected_flags.sort(key=id)
+
+ # flags we actually got
+ new_flags = [k for k,v in context.flags.items() if v]
+ new_flags.sort(key=id)
+
+ self.assertEqual(ans, new_ans,
+ "operation produces different answers depending on flags set: " +
+ "expected %s, got %s." % (ans, new_ans))
+ self.assertEqual(new_flags, expected_flags,
+ "operation raises different flags depending on flags set: " +
+ "expected %s, got %s" % (expected_flags, new_flags))
+
+def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
""" Execute the tests.
Runs all arithmetic tests if arith is True or if the "decimal" resource
@@ -1092,35 +1201,58 @@ def test_main(arith=False, verbose=None):
"""
init()
- global TEST_ALL
+ global TEST_ALL, DEBUG
TEST_ALL = arith or is_resource_enabled('decimal')
+ DEBUG = debug
+
+ if todo_tests is None:
+ test_classes = [
+ DecimalExplicitConstructionTest,
+ DecimalImplicitConstructionTest,
+ DecimalArithmeticOperatorsTest,
+ DecimalUseOfContextTest,
+ DecimalUsabilityTest,
+ DecimalPythonAPItests,
+ ContextAPItests,
+ DecimalTest,
+ WithStatementTest,
+ ContextFlags
+ ]
+ else:
+ test_classes = [DecimalTest]
+
+ # Dynamically build custom test definition for each file in the test
+ # directory and add the definitions to the DecimalTest class. This
+ # procedure insures that new files do not get skipped.
+ for filename in os.listdir(directory):
+ if '.decTest' not in filename or filename.startswith("."):
+ continue
+ head, tail = filename.split('.')
+ if todo_tests is not None and head not in todo_tests:
+ continue
+ tester = lambda self, f=filename: self.eval_file(directory + f)
+ setattr(DecimalTest, 'test_' + head, tester)
+ del filename, head, tail, tester
- test_classes = [
- DecimalExplicitConstructionTest,
- DecimalImplicitConstructionTest,
- DecimalArithmeticOperatorsTest,
- DecimalUseOfContextTest,
- DecimalUsabilityTest,
- DecimalPythonAPItests,
- ContextAPItests,
- DecimalTest,
- WithStatementTest,
- ]
try:
run_unittest(*test_classes)
- import decimal as DecimalModule
- run_doctest(DecimalModule, verbose)
+ if todo_tests is None:
+ import decimal as DecimalModule
+ run_doctest(DecimalModule, verbose)
finally:
setcontext(ORIGINAL_CONTEXT)
if __name__ == '__main__':
- # Calling with no arguments runs all tests.
- # Calling with "Skip" will skip over 90% of the arithmetic tests.
- if len(sys.argv) == 1:
- test_main(arith=True, verbose=True)
- elif len(sys.argv) == 2:
- arith = sys.argv[1].lower() != 'skip'
- test_main(arith=arith, verbose=True)
+ import optparse
+ p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]")
+ p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test')
+ p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests')
+ (opt, args) = p.parse_args()
+
+ if opt.skip:
+ test_main(arith=False, verbose=True)
+ elif args:
+ test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
else:
- raise ValueError("test called with wrong arguments, use test_Decimal [Skip]")
+ test_main(arith=True, verbose=True)