annotate Lib/test/test_decimal.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 228cfb6e2900
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 # Copyright (c) 2004 Python Software Foundation.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 # All rights reserved.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 # Written by Eric Price <eprice at tjhsst.edu>
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 # and Facundo Batista <facundo at taniquetil.com.ar>
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 # and Raymond Hettinger <python at rcn.com>
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 # and Aahz (aahz at pobox.com)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 # and Tim Peters
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 """
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 These are the test cases for the Decimal module.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 There are two groups of tests, Arithmetic and Behaviour. The former test
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 test the pythonic behaviour according to PEP 327.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 Cowlishaw's tests can be downloaded from:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 www2.hursley.ibm.com/decimal/dectest.zip
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 This test module can be called from command line with one parameter (Arithmetic
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22 or Behaviour) to test each part, or without parameter to test both parts. If
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 you're working through IDLE, you can import this test module and call test_main()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24 with the corresponding argument.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25 """
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 import glob
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44923
diff changeset
28 import math
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 import os, sys
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30 import pickle, copy
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44923
diff changeset
31 import unittest
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 from decimal import *
51293
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
33 import numbers
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
34 from test.test_support import (run_unittest, run_doctest, is_resource_enabled)
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
35 import random
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
36 try:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
37 import threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
38 except ImportError:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
39 threading = None
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
41 # Useful Test Constant
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
42 Signals = getcontext().flags.keys()
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
43
37317
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
44 # Tests are built around these assumed context defaults.
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
45 # test_main() restores the original context.
37550
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
46 def init():
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
47 global ORIGINAL_CONTEXT
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
48 ORIGINAL_CONTEXT = getcontext().copy()
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
49 DefaultTestContext = Context(
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
50 prec = 9,
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
51 rounding = ROUND_HALF_EVEN,
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
52 traps = dict.fromkeys(Signals, 0)
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
53 )
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
54 setcontext(DefaultTestContext)
32481
b835d5c61d86 * Make the tests independent of the default precision.
Raymond Hettinger <python@rcn.com>
parents: 32477
diff changeset
55
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 TESTDATADIR = 'decimaltestdata'
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
57 if __name__ == '__main__':
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
58 file = sys.argv[0]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
59 else:
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
60 file = __file__
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
61 testdir = os.path.dirname(file) or os.curdir
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
62 directory = testdir + os.sep + TESTDATADIR + os.sep
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
64 skip_expected = not os.path.isdir(directory)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 # Make sure it actually raises errors when not expected and caught in flags
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 # Slower, since it runs some things several times.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 EXTENDEDERRORTEST = False
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 #Map the test cases' error names to the actual errors
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 ErrorNames = {'clamped' : Clamped,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
72 'conversion_syntax' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 'division_by_zero' : DivisionByZero,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
74 'division_impossible' : InvalidOperation,
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
75 'division_undefined' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 'inexact' : Inexact,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
77 'invalid_context' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 'invalid_operation' : InvalidOperation,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 'overflow' : Overflow,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 'rounded' : Rounded,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 'subnormal' : Subnormal,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 'underflow' : Underflow}
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 def Nonfunction(*args):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 """Doesn't do anything."""
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 return None
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 RoundingDict = {'ceiling' : ROUND_CEILING, #Maps test-case names to roundings.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 'down' : ROUND_DOWN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 'floor' : ROUND_FLOOR,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 'half_down' : ROUND_HALF_DOWN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 'half_even' : ROUND_HALF_EVEN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 'half_up' : ROUND_HALF_UP,
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
95 'up' : ROUND_UP,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
96 '05up' : ROUND_05UP}
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 # Name adapter to be able to change the Decimal and Context
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 # interface without changing the test files from Cowlishaw
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
100 nameAdapter = {'and':'logical_and',
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 'apply':'_apply',
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
102 'class':'number_class',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
103 'comparesig':'compare_signal',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
104 'comparetotal':'compare_total',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
105 'comparetotmag':'compare_total_mag',
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
106 'copy':'copy_decimal',
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
107 'copyabs':'copy_abs',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
108 'copynegate':'copy_negate',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
109 'copysign':'copy_sign',
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
110 'divideint':'divide_int',
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
111 'invert':'logical_invert',
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
112 'iscanonical':'is_canonical',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
113 'isfinite':'is_finite',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
114 'isinfinite':'is_infinite',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
115 'isnan':'is_nan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
116 'isnormal':'is_normal',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
117 'isqnan':'is_qnan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
118 'issigned':'is_signed',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
119 'issnan':'is_snan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
120 'issubnormal':'is_subnormal',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
121 'iszero':'is_zero',
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
122 'maxmag':'max_mag',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
123 'minmag':'min_mag',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
124 'nextminus':'next_minus',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
125 'nextplus':'next_plus',
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
126 'nexttoward':'next_toward',
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
127 'or':'logical_or',
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
128 'reduce':'normalize',
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
129 'remaindernear':'remainder_near',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
130 'samequantum':'same_quantum',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
131 'squareroot':'sqrt',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
132 'toeng':'to_eng_string',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
133 'tointegral':'to_integral_value',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
134 'tointegralx':'to_integral_exact',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
135 'tosci':'to_sci_string',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
136 'xor':'logical_xor',
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 }
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
139 # The following functions return True/False rather than a Decimal instance
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
140
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
141 LOGICAL_FUNCTIONS = (
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
142 'is_canonical',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
143 'is_finite',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
144 'is_infinite',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
145 'is_nan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
146 'is_normal',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
147 'is_qnan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
148 'is_signed',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
149 'is_snan',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
150 'is_subnormal',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
151 'is_zero',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
152 'same_quantum',
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
153 )
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
154
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
155 # For some operations (currently exp, ln, log10, power), the decNumber
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
156 # reference implementation imposes additional restrictions on the
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
157 # context and operands. These restrictions are not part of the
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
158 # specification; however, the effect of these restrictions does show
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
159 # up in some of the testcases. We skip testcases that violate these
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
160 # restrictions, since Decimal behaves differently from decNumber for
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
161 # these testcases so these testcases would otherwise fail.
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
162
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
163 decNumberRestricted = ('power', 'ln', 'log10', 'exp')
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
164 DEC_MAX_MATH = 999999
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
165 def outside_decNumber_bounds(v, context):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
166 if (context.prec > DEC_MAX_MATH or
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
167 context.Emax > DEC_MAX_MATH or
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
168 -context.Emin > DEC_MAX_MATH):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
169 return True
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
170 if not v._is_special and v and (
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
171 v.adjusted() > DEC_MAX_MATH or
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
172 v.adjusted() < 1-2*DEC_MAX_MATH):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
173 return True
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
174 return False
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
175
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 class DecimalTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 """Class which tests the Decimal class against the test cases.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 Changed for unittest.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 """
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 def setUp(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182 self.context = Context()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 self.ignore_list = ['#']
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
184 # Basically, a # means return NaN InvalidOperation.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 # Different from a sNaN in trim
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 self.ChangeDict = {'precision' : self.change_precision,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 'rounding' : self.change_rounding_method,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 'maxexponent' : self.change_max_exponent,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 'minexponent' : self.change_min_exponent,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 'clamp' : self.change_clamp}
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 def eval_file(self, file):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 global skip_expected
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 if skip_expected:
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
196 raise unittest.SkipTest
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 for line in open(file).xreadlines():
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 line = line.replace('\r\n', '').replace('\n', '')
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
200 #print line
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 t = self.eval_line(line)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 except DecimalException, exception:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 #Exception raised where there shoudn't have been one.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 def eval_line(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 if s.find(' -> ') >= 0 and s[:2] != '--' and not s.startswith(' --'):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 s = (s.split('->')[0] + '->' +
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 s.split('->')[1].split('--')[0]).strip()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 s = s.split('--')[0].strip()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 for ignore in self.ignore_list:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217 if s.find(ignore) >= 0:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 #print s.split()[0], 'NotImplemented--', ignore
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 if not s:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 elif ':' in s:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 return self.eval_directive(s)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 return self.eval_equation(s)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 def eval_directive(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 funct, value = map(lambda x: x.strip().lower(), s.split(':'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 if funct == 'rounding':
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 value = RoundingDict[value]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 value = int(value)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 except ValueError:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 pass
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237 funct = self.ChangeDict.get(funct, Nonfunction)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 funct(value)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 def eval_equation(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 #global DEFAULT_PRECISION
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 #print DEFAULT_PRECISION
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
243
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
244 if not TEST_ALL and random.random() < 0.90:
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
245 return
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
246
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248 Sides = s.split('->')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249 L = Sides[0].strip().split()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250 id = L[0]
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
251 if DEBUG:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
252 print "Test ", id,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253 funct = L[1].lower()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
254 valstemp = L[2:]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
255 L = Sides[1].strip().split()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
256 ans = L[0]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
257 exceptions = L[1:]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
258 except (TypeError, AttributeError, IndexError):
32561
6f351b1bcf53 * Update the test suite to reflect that ConversionSyntax was no longer
Raymond Hettinger <python@rcn.com>
parents: 32560
diff changeset
259 raise InvalidOperation
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
260 def FixQuotes(val):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
261 val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
262 val = val.replace("'", '').replace('"', '')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
264 return val
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
265 fname = nameAdapter.get(funct, funct)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
266 if fname == 'rescale':
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
267 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268 funct = getattr(self.context, fname)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
269 vals = []
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
270 conglomerate = ''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
271 quote = 0
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
272 theirexceptions = [ErrorNames[x.lower()] for x in exceptions]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
273
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
274 for exception in Signals:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
275 self.context.traps[exception] = 1 #Catch these bugs...
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
276 for exception in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
277 self.context.traps[exception] = 0
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
278 for i, val in enumerate(valstemp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
279 if val.count("'") % 2 == 1:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 quote = 1 - quote
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 if quote:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 conglomerate = conglomerate + ' ' + val
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 continue
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285 val = conglomerate + val
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 conglomerate = ''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 v = FixQuotes(val)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 if fname in ('to_sci_string', 'to_eng_string'):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 if EXTENDEDERRORTEST:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 for error in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
291 self.context.traps[error] = 1
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 funct(self.context.create_decimal(v))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 except error:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
295 pass
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
296 except Signals, e:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 self.fail("Raised %s in %s when %s disabled" % \
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 (e, s, error))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 self.fail("Did not raise %s in %s" % (error, s))
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
301 self.context.traps[error] = 0
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 v = self.context.create_decimal(v)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 else:
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
304 v = Decimal(v, self.context)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 vals.append(v)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 ans = FixQuotes(ans)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
309 # skip tests that are related to bounds imposed in the decNumber
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
310 # reference implementation
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
311 if fname in decNumberRestricted:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
312 if fname == 'power':
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
313 if not (vals[1]._isinteger() and
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
314 -1999999997 <= vals[1] <= 999999999):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
315 if outside_decNumber_bounds(vals[0], self.context) or \
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
316 outside_decNumber_bounds(vals[1], self.context):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
317 #print "Skipping test %s" % s
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
318 return
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
319 else:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
320 if outside_decNumber_bounds(vals[0], self.context):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
321 #print "Skipping test %s" % s
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
322 return
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
323
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
324
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325 if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 for error in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
327 self.context.traps[error] = 1
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 funct(*vals)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 except error:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 pass
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
332 except Signals, e:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 self.fail("Raised %s in %s when %s disabled" % \
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 (e, s, error))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 self.fail("Did not raise %s in %s" % (error, s))
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
337 self.context.traps[error] = 0
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
338 if DEBUG:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
339 print "--", self.context
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 result = str(funct(*vals))
43454
9d6d9ac95ae1 Made the various is_* operations return booleans. This was discussed
Facundo Batista <facundobatista@gmail.com>
parents: 43409
diff changeset
342 if fname in LOGICAL_FUNCTIONS:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 result = str(int(eval(result))) # 'True', 'False' -> '1', '0'
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
344 except Signals, error:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345 self.fail("Raised %s in %s" % (error, s))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 except: #Catch any error long enough to state the test case.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 print "ERROR:", s
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 raise
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
350 myexceptions = self.getexceptions()
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
351 self.context.clear_flags()
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
352
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 myexceptions.sort()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 theirexceptions.sort()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
356 self.assertEqual(result, ans,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
357 'Incorrect answer for ' + s + ' -- got ' + result)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
358 self.assertEqual(myexceptions, theirexceptions,
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
359 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 def getexceptions(self):
33156
2b8bdb069e0a * Dynamically build a list of files to be tested (necessary because
Raymond Hettinger <python@rcn.com>
parents: 33052
diff changeset
363 return [e for e in Signals if self.context.flags[e]]
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
364
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365 def change_precision(self, prec):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
366 self.context.prec = prec
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367 def change_rounding_method(self, rounding):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 self.context.rounding = rounding
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 def change_min_exponent(self, exp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370 self.context.Emin = exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371 def change_max_exponent(self, exp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 self.context.Emax = exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 def change_clamp(self, clamp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 self.context._clamp = clamp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
375
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
376
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 # The following classes test the behaviour of Decimal according to PEP 327
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 class DecimalExplicitConstructionTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 '''Unit tests for Explicit Construction cases of Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 def test_explicit_empty(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 self.assertEqual(Decimal(), Decimal("0"))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386 def test_explicit_from_None(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 self.assertRaises(TypeError, Decimal, None)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 def test_explicit_from_int(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 #positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 d = Decimal(45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393 self.assertEqual(str(d), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 #very large positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 d = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
397 self.assertEqual(str(d), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
398
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 #negative
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 self.assertEqual(str(d), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 self.assertEqual(str(d), '0')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
406
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 def test_explicit_from_string(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 #empty
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 self.assertEqual(str(Decimal('')), 'NaN')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 self.assertEqual(str(Decimal('45')), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416 self.assertEqual(str(Decimal('45.34')), '45.34')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 #engineer notation
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419 self.assertEqual(str(Decimal('45e2')), '4.5E+3')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
420
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
421 #just not a number
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422 self.assertEqual(str(Decimal('ugly')), 'NaN')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423
44650
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
424 #leading and trailing whitespace permitted
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
425 self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4')
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
426 self.assertEqual(str(Decimal(' -7.89')), '-7.89')
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
427
46087
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
428 #unicode strings should be permitted
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
429 self.assertEqual(str(Decimal(u'0E-017')), '0E-17')
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
430 self.assertEqual(str(Decimal(u'45')), '45')
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
431 self.assertEqual(str(Decimal(u'-Inf')), '-Infinity')
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
432 self.assertEqual(str(Decimal(u'NaN123')), 'NaN123')
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
433
47872
65af544da121 Replace occurrences of '\d' with '[0-9]' in Decimal regex, to make sure
Mark Dickinson <dickinsm@gmail.com>
parents: 46625
diff changeset
434 #but alternate unicode digits should not
65af544da121 Replace occurrences of '\d' with '[0-9]' in Decimal regex, to make sure
Mark Dickinson <dickinsm@gmail.com>
parents: 46625
diff changeset
435 self.assertEqual(str(Decimal(u'\uff11')), 'NaN')
65af544da121 Replace occurrences of '\d' with '[0-9]' in Decimal regex, to make sure
Mark Dickinson <dickinsm@gmail.com>
parents: 46625
diff changeset
436
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
437 def test_explicit_from_tuples(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
438
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
439 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
440 d = Decimal( (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
441 self.assertEqual(str(d), '0')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
442
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
443 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
444 d = Decimal( (1, (4, 5), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
445 self.assertEqual(str(d), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
446
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
447 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
448 d = Decimal( (0, (4, 5, 3, 4), -2) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 self.assertEqual(str(d), '45.34')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 #weird
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 self.assertEqual(str(d), '-4.34913534E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 #wrong number of items
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 #bad sign
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
43602
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
460 self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
461 self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463 #bad exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
464 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
43602
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
465 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
466 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') )
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
467
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 #bad coefficients
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
469 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
470 self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
43602
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
471 self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
43993
a4a140486691 Major change in the internal structure of the Decimal
Facundo Batista <facundobatista@gmail.com>
parents: 43602
diff changeset
472 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) )
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
473
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
474 def test_explicit_from_Decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 #positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 d = Decimal(45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 self.assertEqual(str(e), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482 #very large positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 d = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 self.assertEqual(str(e), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 #negative
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 self.assertEqual(str(e), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
493
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
496 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
497 self.assertEqual(str(e), '0')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
498 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
499
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500 def test_explicit_context_create_decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
501
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
502 nc = copy.copy(getcontext())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503 nc.prec = 3
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
504
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
505 # empty
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
506 d = Decimal()
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
507 self.assertEqual(str(d), '0')
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
508 d = nc.create_decimal()
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
509 self.assertEqual(str(d), '0')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
510
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 # from None
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 self.assertRaises(TypeError, nc.create_decimal, None)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 # from int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 d = nc.create_decimal(456)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 self.failUnless(isinstance(d, Decimal))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 self.assertEqual(nc.create_decimal(45678),
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 nc.create_decimal('457E+2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520 # from string
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 d = Decimal('456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
522 self.assertEqual(str(d), '456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 d = nc.create_decimal('456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 self.assertEqual(str(d), '4.57E+5')
44650
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
525 # leading and trailing whitespace should result in a NaN;
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
526 # spaces are already checked in Cowlishaw's test-suite, so
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
527 # here we just check that a trailing newline results in a NaN
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
528 self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
530 # from tuples
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
531 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
532 self.assertEqual(str(d), '-4.34913534E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
533 d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
534 self.assertEqual(str(d), '-4.35E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 # from Decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537 prevdec = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 d = Decimal(prevdec)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
539 self.assertEqual(str(d), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 d = nc.create_decimal(prevdec)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
541 self.assertEqual(str(d), '5.00E+8')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544 class DecimalImplicitConstructionTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
545 '''Unit tests for Implicit Construction cases of Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
546
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
547 def test_implicit_from_None(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 self.assertRaises(TypeError, eval, 'Decimal(5) + None', globals())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 def test_implicit_from_int(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 #normal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 self.assertEqual(str(Decimal(5) + 45), '50')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
553 #exceeding precision
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
554 self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
555
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
556 def test_implicit_from_string(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557 self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', globals())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
559 def test_implicit_from_float(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
560 self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', globals())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
561
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562 def test_implicit_from_Decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563 self.assertEqual(Decimal(5) + Decimal(45), Decimal(50))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
564
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
565 def test_rop(self):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
566 # Allow other classes to be trained to interact with Decimals
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
567 class E:
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
568 def __divmod__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
569 return 'divmod ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
570 def __rdivmod__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
571 return str(other) + ' rdivmod'
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
572 def __lt__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
573 return 'lt ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
574 def __gt__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
575 return 'gt ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
576 def __le__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
577 return 'le ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
578 def __ge__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
579 return 'ge ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
580 def __eq__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
581 return 'eq ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
582 def __ne__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
583 return 'ne ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
584
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
585 self.assertEqual(divmod(E(), Decimal(10)), 'divmod 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
586 self.assertEqual(divmod(Decimal(10), E()), '10 rdivmod')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
587 self.assertEqual(eval('Decimal(10) < E()'), 'gt 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
588 self.assertEqual(eval('Decimal(10) > E()'), 'lt 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
589 self.assertEqual(eval('Decimal(10) <= E()'), 'ge 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
590 self.assertEqual(eval('Decimal(10) >= E()'), 'le 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
591 self.assertEqual(eval('Decimal(10) == E()'), 'eq 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
592 self.assertEqual(eval('Decimal(10) != E()'), 'ne 10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
593
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
594 # insert operator methods and then exercise them
37271
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
595 oplist = [
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
596 ('+', '__add__', '__radd__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
597 ('-', '__sub__', '__rsub__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
598 ('*', '__mul__', '__rmul__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
599 ('%', '__mod__', '__rmod__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
600 ('//', '__floordiv__', '__rfloordiv__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
601 ('**', '__pow__', '__rpow__')
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
602 ]
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
603 if 1/2 == 0:
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
604 # testing with classic division, so add __div__
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
605 oplist.append(('/', '__div__', '__rdiv__'))
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
606 else:
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
607 # testing with -Qnew, so add __truediv__
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
608 oplist.append(('/', '__truediv__', '__rtruediv__'))
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37271
diff changeset
609
37271
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
610 for sym, lop, rop in oplist:
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
611 setattr(E, lop, lambda self, other: 'str' + lop + str(other))
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
612 setattr(E, rop, lambda self, other: str(other) + rop + 'str')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
613 self.assertEqual(eval('E()' + sym + 'Decimal(10)'),
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
614 'str' + lop + '10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
615 self.assertEqual(eval('Decimal(10)' + sym + 'E()'),
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
616 '10' + rop + 'str')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
618
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
619 class DecimalFormatTest(unittest.TestCase):
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
620 '''Unit tests for the format function.'''
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
621 def test_formatting(self):
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
622 # triples giving a format, a Decimal, and the expected result
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
623 test_values = [
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
624 ('e', '0E-15', '0e-15'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
625 ('e', '2.3E-15', '2.3e-15'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
626 ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
627 ('e', '2.30000E-15', '2.30000e-15'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
628 ('e', '1.23456789123456789e40', '1.23456789123456789e+40'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
629 ('e', '1.5', '1.5e+0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
630 ('e', '0.15', '1.5e-1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
631 ('e', '0.015', '1.5e-2'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
632 ('e', '0.0000000000015', '1.5e-12'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
633 ('e', '15.0', '1.50e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
634 ('e', '-15', '-1.5e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
635 ('e', '0', '0e+0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
636 ('e', '0E1', '0e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
637 ('e', '0.0', '0e-1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
638 ('e', '0.00', '0e-2'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
639 ('.6e', '0E-15', '0.000000e-9'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
640 ('.6e', '0', '0.000000e+6'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
641 ('.6e', '9.999999', '9.999999e+0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
642 ('.6e', '9.9999999', '1.000000e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
643 ('.6e', '-1.23e5', '-1.230000e+5'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
644 ('.6e', '1.23456789e-3', '1.234568e-3'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
645 ('f', '0', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
646 ('f', '0.0', '0.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
647 ('f', '0E-2', '0.00'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
648 ('f', '0.00E-8', '0.0000000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
649 ('f', '0E1', '0'), # loses exponent information
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
650 ('f', '3.2E1', '32'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
651 ('f', '3.2E2', '320'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
652 ('f', '3.20E2', '320'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
653 ('f', '3.200E2', '320.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
654 ('f', '3.2E-6', '0.0000032'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
655 ('.6f', '0E-15', '0.000000'), # all zeros treated equally
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
656 ('.6f', '0E1', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
657 ('.6f', '0', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
658 ('.0f', '0', '0'), # no decimal point
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
659 ('.0f', '0e-2', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
660 ('.0f', '3.14159265', '3'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
661 ('.1f', '3.14159265', '3.1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
662 ('.4f', '3.14159265', '3.1416'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
663 ('.6f', '3.14159265', '3.141593'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
664 ('.7f', '3.14159265', '3.1415926'), # round-half-even!
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
665 ('.8f', '3.14159265', '3.14159265'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
666 ('.9f', '3.14159265', '3.141592650'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
667
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
668 ('g', '0', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
669 ('g', '0.0', '0.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
670 ('g', '0E1', '0e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
671 ('G', '0E1', '0E+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
672 ('g', '0E-5', '0.00000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
673 ('g', '0E-6', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
674 ('g', '0E-7', '0e-7'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
675 ('g', '-0E2', '-0e+2'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
676 ('.0g', '3.14159265', '3'), # 0 sig fig -> 1 sig fig
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
677 ('.1g', '3.14159265', '3'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
678 ('.2g', '3.14159265', '3.1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
679 ('.5g', '3.14159265', '3.1416'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
680 ('.7g', '3.14159265', '3.141593'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
681 ('.8g', '3.14159265', '3.1415926'), # round-half-even!
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
682 ('.9g', '3.14159265', '3.14159265'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
683 ('.10g', '3.14159265', '3.14159265'), # don't pad
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
684
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
685 ('%', '0E1', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
686 ('%', '0E0', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
687 ('%', '0E-1', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
688 ('%', '0E-2', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
689 ('%', '0E-3', '0.0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
690 ('%', '0E-4', '0.00%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
691
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
692 ('.3%', '0', '0.000%'), # all zeros treated equally
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
693 ('.3%', '0E10', '0.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
694 ('.3%', '0E-10', '0.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
695 ('.3%', '2.34', '234.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
696 ('.3%', '1.234567', '123.457%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
697 ('.0%', '1.23', '123%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
698
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
699 ('e', 'NaN', 'NaN'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
700 ('f', '-NaN123', '-NaN123'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
701 ('+g', 'NaN456', '+NaN456'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
702 ('.3e', 'Inf', 'Infinity'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
703 ('.16f', '-Inf', '-Infinity'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
704 ('.0g', '-sNaN', '-sNaN'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
705
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
706 ('', '1.00', '1.00'),
52188
6b738b6f7fc6 Fix bug in Decimal __format__ method that swapped left and right
Mark Dickinson <dickinsm@gmail.com>
parents: 51293
diff changeset
707
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
708 # test alignment and padding
52188
6b738b6f7fc6 Fix bug in Decimal __format__ method that swapped left and right
Mark Dickinson <dickinsm@gmail.com>
parents: 51293
diff changeset
709 ('<6', '123', '123 '),
6b738b6f7fc6 Fix bug in Decimal __format__ method that swapped left and right
Mark Dickinson <dickinsm@gmail.com>
parents: 51293
diff changeset
710 ('>6', '123', ' 123'),
6b738b6f7fc6 Fix bug in Decimal __format__ method that swapped left and right
Mark Dickinson <dickinsm@gmail.com>
parents: 51293
diff changeset
711 ('^6', '123', ' 123 '),
6b738b6f7fc6 Fix bug in Decimal __format__ method that swapped left and right
Mark Dickinson <dickinsm@gmail.com>
parents: 51293
diff changeset
712 ('=+6', '123', '+ 123'),
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
713 ('#<10', 'NaN', 'NaN#######'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
714 ('#<10', '-4.3', '-4.3######'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
715 ('#<+10', '0.0130', '+0.0130###'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
716 ('#< 10', '0.0130', ' 0.0130###'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
717 ('@>10', '-Inf', '@-Infinity'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
718 ('#>5', '-Inf', '-Infinity'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
719 ('?^5', '123', '?123?'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
720 ('%^6', '123', '%123%%'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
721 (' ^6', '-45.6', '-45.6 '),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
722 ('/=10', '-45.6', '-/////45.6'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
723 ('/=+10', '45.6', '+/////45.6'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
724 ('/= 10', '45.6', ' /////45.6'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
725
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
726 # thousands separator
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
727 (',', '1234567', '1,234,567'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
728 (',', '123456', '123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
729 (',', '12345', '12,345'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
730 (',', '1234', '1,234'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
731 (',', '123', '123'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
732 (',', '12', '12'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
733 (',', '1', '1'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
734 (',', '0', '0'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
735 (',', '-1234567', '-1,234,567'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
736 (',', '-123456', '-123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
737 ('7,', '123456', '123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
738 ('8,', '123456', '123,456 '),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
739 ('08,', '123456', '0,123,456'), # special case: extra 0 needed
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
740 ('+08,', '123456', '+123,456'), # but not if there's a sign
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
741 (' 08,', '123456', ' 123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
742 ('08,', '-123456', '-123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
743 ('+09,', '123456', '+0,123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
744 # ... with fractional part...
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
745 ('07,', '1234.56', '1,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
746 ('08,', '1234.56', '1,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
747 ('09,', '1234.56', '01,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
748 ('010,', '1234.56', '001,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
749 ('011,', '1234.56', '0,001,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
750 ('012,', '1234.56', '0,001,234.56'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
751 ('08,.1f', '1234.5', '01,234.5'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
752 # no thousands separators in fraction part
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
753 (',', '1.23456789', '1.23456789'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
754 (',%', '123.456789', '12,345.6789%'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
755 (',e', '123456', '1.23456e+5'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
756 (',E', '123456', '1.23456E+5'),
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
757 ]
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
758 for fmt, d, result in test_values:
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
759 self.assertEqual(format(Decimal(d), fmt), result)
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
760
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
761 def test_n_format(self):
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
762 try:
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
763 from locale import CHAR_MAX
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
764 except ImportError:
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
765 return
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
766
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
767 # Set up some localeconv-like dictionaries
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
768 en_US = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
769 'decimal_point' : '.',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
770 'grouping' : [3, 3, 0],
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
771 'thousands_sep': ','
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
772 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
773
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
774 fr_FR = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
775 'decimal_point' : ',',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
776 'grouping' : [CHAR_MAX],
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
777 'thousands_sep' : ''
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
778 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
779
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
780 ru_RU = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
781 'decimal_point' : ',',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
782 'grouping' : [3, 3, 0],
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
783 'thousands_sep' : ' '
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
784 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
785
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
786 crazy = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
787 'decimal_point' : '&',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
788 'grouping' : [1, 4, 2, CHAR_MAX],
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
789 'thousands_sep' : '-'
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
790 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
791
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
792
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
793 def get_fmt(x, locale, fmt='n'):
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
794 return Decimal.__format__(Decimal(x), fmt, _localeconv=locale)
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
795
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
796 self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
797 self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
798 self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
799 self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
800
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
801 self.assertEqual(get_fmt(123456789, en_US), '123,456,789')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
802 self.assertEqual(get_fmt(123456789, fr_FR), '123456789')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
803 self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
804 self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
805
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
806 self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
807 self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
808 self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
809 self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8')
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
810
52200
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
811 # zero padding
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
812 self.assertEqual(get_fmt(1234, fr_FR, '03n'), '1234')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
813 self.assertEqual(get_fmt(1234, fr_FR, '04n'), '1234')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
814 self.assertEqual(get_fmt(1234, fr_FR, '05n'), '01234')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
815 self.assertEqual(get_fmt(1234, fr_FR, '06n'), '001234')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
816
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
817 self.assertEqual(get_fmt(12345, en_US, '05n'), '12,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
818 self.assertEqual(get_fmt(12345, en_US, '06n'), '12,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
819 self.assertEqual(get_fmt(12345, en_US, '07n'), '012,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
820 self.assertEqual(get_fmt(12345, en_US, '08n'), '0,012,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
821 self.assertEqual(get_fmt(12345, en_US, '09n'), '0,012,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
822 self.assertEqual(get_fmt(12345, en_US, '010n'), '00,012,345')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
823
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
824 self.assertEqual(get_fmt(123456, crazy, '06n'), '1-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
825 self.assertEqual(get_fmt(123456, crazy, '07n'), '1-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
826 self.assertEqual(get_fmt(123456, crazy, '08n'), '1-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
827 self.assertEqual(get_fmt(123456, crazy, '09n'), '01-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
828 self.assertEqual(get_fmt(123456, crazy, '010n'), '0-01-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
829 self.assertEqual(get_fmt(123456, crazy, '011n'), '0-01-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
830 self.assertEqual(get_fmt(123456, crazy, '012n'), '00-01-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
831 self.assertEqual(get_fmt(123456, crazy, '013n'), '000-01-2345-6')
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
832
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
833
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 class DecimalArithmeticOperatorsTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835 '''Unit tests for all arithmetic operators, binary and unary.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837 def test_addition(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 d1 = Decimal('-11.1')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840 d2 = Decimal('22.2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 self.assertEqual(d1+d2, Decimal('11.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 self.assertEqual(d2+d1, Decimal('11.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 c = d1 + 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 self.assertEqual(c, Decimal('-6.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
849 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
851 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 c = 5 + d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 self.assertEqual(c, Decimal('-6.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
855
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 d1 += d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 self.assertEqual(d1, Decimal('11.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 d1 += 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862 self.assertEqual(d1, Decimal('16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 def test_subtraction(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 d1 = Decimal('-11.1')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 d2 = Decimal('22.2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 self.assertEqual(d1-d2, Decimal('-33.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871 self.assertEqual(d2-d1, Decimal('33.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 c = d1 - 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 self.assertEqual(c, Decimal('-16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
877
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
878 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
879 c = 5 - d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
880 self.assertEqual(c, Decimal('16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
881 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 d1 -= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 self.assertEqual(d1, Decimal('-33.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888 d1 -= 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889 self.assertEqual(d1, Decimal('-38.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 def test_multiplication(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 d1 = Decimal('-5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 d2 = Decimal('3')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 self.assertEqual(d1*d2, Decimal('-15'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 self.assertEqual(d2*d1, Decimal('-15'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901 c = d1 * 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 self.assertEqual(c, Decimal('-25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 c = 5 * d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 self.assertEqual(c, Decimal('-25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 d1 *= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 self.assertEqual(d1, Decimal('-15'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 d1 *= 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 self.assertEqual(d1, Decimal('-75'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 def test_division(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 d1 = Decimal('-5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 self.assertEqual(d1/d2, Decimal('-2.5'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925 self.assertEqual(d2/d1, Decimal('-0.4'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 c = d1 / 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 self.assertEqual(c, Decimal('-1.25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 c = 4 / d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 self.assertEqual(c, Decimal('-0.8'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 d1 /= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 self.assertEqual(d1, Decimal('-2.5'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 d1 /= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 self.assertEqual(d1, Decimal('-0.625'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945 def test_floor_division(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 self.assertEqual(d1//d2, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 self.assertEqual(d2//d1, Decimal('0'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 c = d1 // 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 c = 7 // d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 d1 //= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 self.assertEqual(d1, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 d1 //= 2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 def test_powering(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 self.assertEqual(d1**d2, Decimal('25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 self.assertEqual(d2**d1, Decimal('32'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 c = d1 ** 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 self.assertEqual(c, Decimal('625'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 c = 7 ** d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 self.assertEqual(c, Decimal('16807'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992 d1 **= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 self.assertEqual(d1, Decimal('25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 d1 **= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 self.assertEqual(d1, Decimal('390625'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 def test_module(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 self.assertEqual(d1%d2, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 self.assertEqual(d2%d1, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 c = d1 % 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 c = 7 % d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 self.assertEqual(c, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 d1 %= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 d1 %= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 def test_floor_div_module(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 (p, q) = divmod(d1, d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 self.assertEqual(p, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 self.assertEqual(q, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 self.assertEqual(type(q), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 (p, q) = divmod(d1, 4)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 self.assertEqual(p, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 self.assertEqual(q, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 self.assertEqual(type(q), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 (p, q) = divmod(7, d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 self.assertEqual(p, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 self.assertEqual(q, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 self.assertEqual(type(q), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052 def test_unary_operators(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 self.assertEqual(+Decimal(45), Decimal(+45)) # +
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 self.assertEqual(-Decimal(45), Decimal(-45)) # -
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056
45201
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1057 def test_nan_comparisons(self):
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1058 n = Decimal('NaN')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1059 s = Decimal('sNaN')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1060 i = Decimal('Inf')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1061 f = Decimal('2')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1062 for x, y in [(n, n), (n, i), (i, n), (n, f), (f, n),
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1063 (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s)]:
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1064 self.assert_(x != y)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1065 self.assert_(not (x == y))
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1066 self.assert_(not (x < y))
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1067 self.assert_(not (x <= y))
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1068 self.assert_(not (x > y))
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1069 self.assert_(not (x >= y))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 # The following are two functions used to test threading in the next class
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 def thfunc1(cls):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 d1 = Decimal(1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 d3 = Decimal(3)
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1076 test1 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 cls.synchro.wait()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1078 test2 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 cls.finish1.set()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1080
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1081 cls.assertEqual(test1, Decimal('0.3333333333333333333333333333'))
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1082 cls.assertEqual(test2, Decimal('0.3333333333333333333333333333'))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 def thfunc2(cls):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 d1 = Decimal(1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 d3 = Decimal(3)
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1088 test1 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 thiscontext = getcontext()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 thiscontext.prec = 18
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1091 test2 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 cls.synchro.set()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 cls.finish2.set()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1094
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1095 cls.assertEqual(test1, Decimal('0.3333333333333333333333333333'))
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1096 cls.assertEqual(test2, Decimal('0.333333333333333333'))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 class DecimalUseOfContextTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 '''Unit tests for Use of Context cases in Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1103 try:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1104 import threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1105 except ImportError:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1106 threading = None
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1107
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 # Take care executing this test from IDLE, there's an issue in threading
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 # that hangs IDLE and I couldn't find it
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 def test_threading(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 #Test the "threading isolation" of a Context.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 self.synchro = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 self.finish1 = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 self.finish2 = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 th1 = threading.Thread(target=thfunc1, args=(self,))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 th2 = threading.Thread(target=thfunc2, args=(self,))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 th1.start()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 th2.start()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 self.finish1.wait()
43407
44aa4ffdc1ac Fix obvious typo in threaded test.
Thomas Wouters <thomas@python.org>
parents: 43383
diff changeset
1125 self.finish2.wait()
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1128 if threading is None:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1129 del test_threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1130
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132 class DecimalUsabilityTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133 '''Unit tests for Usability cases of Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 def test_comparison_operators(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 da = Decimal('23.42')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138 db = Decimal('23.42')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 dc = Decimal('45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1141 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 self.failUnless(dc > da)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143 self.failUnless(dc >= da)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1144 self.failUnless(da < dc)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1145 self.failUnless(da <= dc)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146 self.failUnless(da == db)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 self.failUnless(da != dc)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 self.failUnless(da <= db)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149 self.failUnless(da >= db)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 self.assertEqual(cmp(dc,da), 1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 self.assertEqual(cmp(da,dc), -1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 self.assertEqual(cmp(da,db), 0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154 #a Decimal and an int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 self.failUnless(dc > 23)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1156 self.failUnless(23 < dc)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1157 self.failUnless(dc == 45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 self.assertEqual(cmp(dc,23), 1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 self.assertEqual(cmp(23,dc), -1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160 self.assertEqual(cmp(dc,45), 0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 #a Decimal and uncomparable
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1163 self.assertNotEqual(da, 'ugly')
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1164 self.assertNotEqual(da, 32.7)
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1165 self.assertNotEqual(da, object())
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1166 self.assertNotEqual(da, object)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1168 # sortable
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1169 a = map(Decimal, xrange(100))
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1170 b = a[:]
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1171 random.shuffle(a)
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1172 a.sort()
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1173 self.assertEqual(a, b)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1175 # with None
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1176 self.assertFalse(Decimal(1) < None)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1177 self.assertTrue(Decimal(1) > None)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1178
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179 def test_copy_and_deepcopy_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180 d = Decimal('43.24')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 c = copy.copy(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182 self.assertEqual(id(c), id(d))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 dc = copy.deepcopy(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184 self.assertEqual(id(dc), id(d))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1185
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186 def test_hash_method(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 #just that it's hashable
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 hash(Decimal(23))
43409
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1189
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1190 test_values = [Decimal(sign*(2**m + n))
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1191 for m in [0, 14, 15, 16, 17, 30, 31,
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1192 32, 33, 62, 63, 64, 65, 66]
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1193 for n in range(-10, 10)
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1194 for sign in [-1, 1]]
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1195 test_values.extend([
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1196 Decimal("-0"), # zeros
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1197 Decimal("0.00"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1198 Decimal("-0.000"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1199 Decimal("0E10"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1200 Decimal("-0E12"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1201 Decimal("10.0"), # negative exponent
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1202 Decimal("-23.00000"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1203 Decimal("1230E100"), # positive exponent
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1204 Decimal("-4.5678E50"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1205 # a value for which hash(n) != hash(n % (2**64-1))
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1206 # in Python pre-2.6
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1207 Decimal(2**64 + 2**32 - 1),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1208 # selection of values which fail with the old (before
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1209 # version 2.6) long.__hash__
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1210 Decimal("1.634E100"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1211 Decimal("90.697E100"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1212 Decimal("188.83E100"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1213 Decimal("1652.9E100"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1214 Decimal("56531E100"),
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1215 ])
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1216
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1217 # check that hash(d) == hash(int(d)) for integral values
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1218 for value in test_values:
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1219 self.assertEqual(hash(value), hash(int(value)))
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1220
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 #the same hash that to an int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 self.assertEqual(hash(Decimal(23)), hash(23))
34760
4db5ea9577ba Decimal special values did not hash properly.
Raymond Hettinger <python@rcn.com>
parents: 34184
diff changeset
1223 self.assertRaises(TypeError, hash, Decimal('NaN'))
4db5ea9577ba Decimal special values did not hash properly.
Raymond Hettinger <python@rcn.com>
parents: 34184
diff changeset
1224 self.assert_(hash(Decimal('Inf')))
4db5ea9577ba Decimal special values did not hash properly.
Raymond Hettinger <python@rcn.com>
parents: 34184
diff changeset
1225 self.assert_(hash(Decimal('-Inf')))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1226
44593
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1227 # check that the value of the hash doesn't depend on the
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1228 # current context (issue #1757)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1229 c = getcontext()
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1230 old_precision = c.prec
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1231 x = Decimal("123456789.1")
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1232
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1233 c.prec = 6
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1234 h1 = hash(x)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1235 c.prec = 10
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1236 h2 = hash(x)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1237 c.prec = 16
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1238 h3 = hash(x)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1239
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1240 self.assertEqual(h1, h2)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1241 self.assertEqual(h1, h3)
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1242 c.prec = old_precision
057de46ea17e Issue #1757: The hash of a Decimal instance is no longer affected
Facundo Batista <facundobatista@gmail.com>
parents: 43993
diff changeset
1243
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244 def test_min_and_max_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246 d1 = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247 d2 = Decimal('28.5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 l1 = 15
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 l2 = 28
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1251 #between Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1252 self.failUnless(min(d1,d2) is d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1253 self.failUnless(min(d2,d1) is d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254 self.failUnless(max(d1,d2) is d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255 self.failUnless(max(d2,d1) is d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1256
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1257 #between Decimal and long
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1258 self.failUnless(min(d1,l2) is d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1259 self.failUnless(min(l2,d1) is d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1260 self.failUnless(max(l1,d2) is d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1261 self.failUnless(max(d2,l1) is d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1262
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263 def test_as_nonzero(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264 #as false
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 self.failIf(Decimal(0))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 #as true
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 self.failUnless(Decimal('0.372'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269 def test_tostring_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270 #Test str and repr methods.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1271
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 d = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 self.assertEqual(str(d), '15.32') # str
45321
33122ff83405 Fix decimal repr which should have used single quotes like other reprs.
Raymond Hettinger <python@rcn.com>
parents: 45201
diff changeset
1274 self.assertEqual(repr(d), "Decimal('15.32')") # repr
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275
46087
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1276 # result type of string methods should be str, not unicode
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1277 unicode_inputs = [u'123.4', u'0.5E2', u'Infinity', u'sNaN',
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1278 u'-0.0E100', u'-NaN001', u'-Inf']
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1279
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1280 for u in unicode_inputs:
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1281 d = Decimal(u)
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1282 self.assertEqual(type(str(d)), str)
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1283 self.assertEqual(type(repr(d)), str)
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1284 self.assertEqual(type(d.to_eng_string()), str)
e81cac73daf2 Issue #2482: Make sure that the coefficient of a Decimal
Mark Dickinson <dickinsm@gmail.com>
parents: 46082
diff changeset
1285
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 def test_tonum_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287 #Test float, int and long methods.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1288
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1289 d1 = Decimal('66')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290 d2 = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1291
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1293 self.assertEqual(int(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1294 self.assertEqual(int(d2), 15)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1295
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1296 #long
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 self.assertEqual(long(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1298 self.assertEqual(long(d2), 15)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1300 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301 self.assertEqual(float(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 self.assertEqual(float(d2), 15.32)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1304 def test_eval_round_trip(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 #with zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 d = Decimal( (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311 d = Decimal( (1, (4, 5), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1312 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 d = Decimal( (0, (4, 5, 3, 4), -2) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 #weird
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 def test_as_tuple(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324 #with zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326 self.assertEqual(d.as_tuple(), (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 self.assertEqual(d.as_tuple(), (1, (4, 5), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1331
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332 #complicated string
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 d = Decimal("-4.34913534E-17")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334 self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336 #inf
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337 d = Decimal("Infinity")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338 self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1339
43602
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1340 #leading zeros in coefficient should be stripped
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1341 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1342 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1343 d = Decimal( (1, (0, 0, 0), 37) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1344 self.assertEqual(d.as_tuple(), (1, (0,), 37))
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1345 d = Decimal( (1, (), 37) )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1346 self.assertEqual(d.as_tuple(), (1, (0,), 37))
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1347
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1348 #leading zeros in NaN diagnostic info should be stripped
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1349 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1350 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1351 d = Decimal( (1, (0, 0, 0), 'N') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1352 self.assertEqual(d.as_tuple(), (1, (), 'N') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1353 d = Decimal( (1, (), 'n') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1354 self.assertEqual(d.as_tuple(), (1, (), 'n') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1355
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1356 #coefficient in infinity should be ignored
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1357 d = Decimal( (0, (4, 5, 3, 4), 'F') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1358 self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1359 d = Decimal( (1, (0, 2, 7, 1), 'F') )
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1360 self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
80744c8d34d8 The constructor from tuple was way too permissive: it allowed bad
Facundo Batista <facundobatista@gmail.com>
parents: 43454
diff changeset
1361
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362 def test_immutability_operations(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 # Do operations and check that it didn't change change internal objects.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1365 d1 = Decimal('-25e55')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 b1 = Decimal('-25e55')
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1367 d2 = Decimal('33e+33')
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1368 b2 = Decimal('33e+33')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 def checkSameDec(operation, useOther=False):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 if useOther:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 eval("d1." + operation + "(d2)")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376 self.assertEqual(d2._sign, b2._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1377 self.assertEqual(d2._int, b2._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378 self.assertEqual(d2._exp, b2._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 eval("d1." + operation + "()")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 Decimal(d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 checkSameDec("__abs__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 checkSameDec("__add__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 checkSameDec("__div__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 checkSameDec("__divmod__", True)
45201
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1395 checkSameDec("__eq__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1396 checkSameDec("__ne__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1397 checkSameDec("__le__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1398 checkSameDec("__lt__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1399 checkSameDec("__ge__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1400 checkSameDec("__gt__", True)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1401 checkSameDec("__float__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402 checkSameDec("__floordiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 checkSameDec("__hash__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404 checkSameDec("__int__")
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1405 checkSameDec("__trunc__")
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1406 checkSameDec("__long__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1407 checkSameDec("__mod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1408 checkSameDec("__mul__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409 checkSameDec("__neg__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410 checkSameDec("__nonzero__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 checkSameDec("__pos__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412 checkSameDec("__pow__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 checkSameDec("__radd__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414 checkSameDec("__rdiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 checkSameDec("__rdivmod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 checkSameDec("__repr__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417 checkSameDec("__rfloordiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 checkSameDec("__rmod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 checkSameDec("__rmul__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420 checkSameDec("__rpow__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 checkSameDec("__rsub__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1422 checkSameDec("__str__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423 checkSameDec("__sub__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 checkSameDec("__truediv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1425 checkSameDec("adjusted")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1426 checkSameDec("as_tuple")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 checkSameDec("compare", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428 checkSameDec("max", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 checkSameDec("min", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 checkSameDec("normalize")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1431 checkSameDec("quantize", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 checkSameDec("remainder_near", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433 checkSameDec("same_quantum", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434 checkSameDec("sqrt")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435 checkSameDec("to_eng_string")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 checkSameDec("to_integral")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437
43383
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1438 def test_subclassing(self):
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1439 # Different behaviours when subclassing Decimal
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1440
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1441 class MyDecimal(Decimal):
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1442 pass
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1443
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1444 d1 = MyDecimal(1)
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1445 d2 = MyDecimal(2)
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1446 d = d1 + d2
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1447 self.assertTrue(type(d) is Decimal)
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1448
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1449 d = d1.max(d2)
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1450 self.assertTrue(type(d) is Decimal)
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1451
46082
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1452 def test_implicit_context(self):
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1453 # Check results when context given implicitly. (Issue 2478)
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1454 c = getcontext()
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1455 self.assertEqual(str(Decimal(0).sqrt()),
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1456 str(c.sqrt(Decimal(0))))
22bd02293efa Issue #2478: Decimal(sqrt(0)) failed when the decimal context
Mark Dickinson <dickinsm@gmail.com>
parents: 45998
diff changeset
1457
43383
8890be9386b7 The methods always return Decimal classes, even if they're
Facundo Batista <facundobatista@gmail.com>
parents: 43360
diff changeset
1458
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459 class DecimalPythonAPItests(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1460
51293
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1461 def test_abc(self):
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1462 self.assert_(issubclass(Decimal, numbers.Number))
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1463 self.assert_(not issubclass(Decimal, numbers.Real))
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1464 self.assert_(isinstance(Decimal(0), numbers.Number))
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1465 self.assert_(not isinstance(Decimal(0), numbers.Real))
5c254795aa07 Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50712
diff changeset
1466
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467 def test_pickle(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1468 d = Decimal('-3.141590000')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 p = pickle.dumps(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 e = pickle.loads(p)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471 self.assertEqual(d, e)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1473 def test_int(self):
34023
a8ff6ed856e8 SF bug #1071588 coercing decimal to int doesn't work between -1 and 1
Raymond Hettinger <python@rcn.com>
parents: 33405
diff changeset
1474 for x in range(-250, 250):
a8ff6ed856e8 SF bug #1071588 coercing decimal to int doesn't work between -1 and 1
Raymond Hettinger <python@rcn.com>
parents: 33405
diff changeset
1475 s = '%0.2f' % (x / 100.0)
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1476 # should work the same as for floats
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1477 self.assertEqual(int(Decimal(s)), int(float(s)))
34023
a8ff6ed856e8 SF bug #1071588 coercing decimal to int doesn't work between -1 and 1
Raymond Hettinger <python@rcn.com>
parents: 33405
diff changeset
1478 # should work the same as to_integral in the ROUND_DOWN mode
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1479 d = Decimal(s)
34023
a8ff6ed856e8 SF bug #1071588 coercing decimal to int doesn't work between -1 and 1
Raymond Hettinger <python@rcn.com>
parents: 33405
diff changeset
1480 r = d.to_integral(ROUND_DOWN)
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1481 self.assertEqual(Decimal(int(d)), r)
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1482
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1483 def test_trunc(self):
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1484 for x in range(-250, 250):
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1485 s = '%0.2f' % (x / 100.0)
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1486 # should work the same as for floats
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1487 self.assertEqual(int(Decimal(s)), int(float(s)))
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1488 # should work the same as to_integral in the ROUND_DOWN mode
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1489 d = Decimal(s)
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1490 r = d.to_integral(ROUND_DOWN)
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44923
diff changeset
1491 self.assertEqual(Decimal(math.trunc(d)), r)
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1492
50496
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1493 def test_from_float(self):
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1494
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1495 class MyDecimal(Decimal):
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1496 pass
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1497
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1498 r = MyDecimal.from_float(0.1)
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1499 self.assertEqual(type(r), MyDecimal)
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1500 self.assertEqual(str(r),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1501 '0.1000000000000000055511151231257827021181583404541015625')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1502 bigint = 12345678901234567890123456789
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1503 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1504 self.assert_(MyDecimal.from_float(float('nan')).is_qnan())
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1505 self.assert_(MyDecimal.from_float(float('inf')).is_infinite())
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1506 self.assert_(MyDecimal.from_float(float('-inf')).is_infinite())
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1507 self.assertEqual(str(MyDecimal.from_float(float('nan'))),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1508 str(Decimal('NaN')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1509 self.assertEqual(str(MyDecimal.from_float(float('inf'))),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1510 str(Decimal('Infinity')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1511 self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1512 str(Decimal('-Infinity')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1513 self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1514 for i in range(200):
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1515 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1516 self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1517
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1518 def test_create_decimal_from_float(self):
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1519 context = Context(prec=5, rounding=ROUND_DOWN)
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1520 self.assertEqual(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1521 context.create_decimal_from_float(math.pi),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1522 Decimal('3.1415')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1523 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1524 context = Context(prec=5, rounding=ROUND_UP)
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1525 self.assertEqual(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1526 context.create_decimal_from_float(math.pi),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1527 Decimal('3.1416')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1528 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1529 context = Context(prec=5, traps=[Inexact])
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1530 self.assertRaises(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1531 Inexact,
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1532 context.create_decimal_from_float,
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1533 math.pi
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1534 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1535 self.assertEqual(repr(context.create_decimal_from_float(-0.0)),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1536 "Decimal('-0')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1537 self.assertEqual(repr(context.create_decimal_from_float(1.0)),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1538 "Decimal('1')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1539 self.assertEqual(repr(context.create_decimal_from_float(10)),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1540 "Decimal('10')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1541
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1542 class ContextAPItests(unittest.TestCase):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1543
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1544 def test_pickle(self):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1545 c = Context()
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1546 e = pickle.loads(pickle.dumps(c))
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1547 for k in vars(c):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1548 v1 = vars(c)[k]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1549 v2 = vars(e)[k]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1550 self.assertEqual(v1, v2)
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1551
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1552 def test_equality_with_other_types(self):
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1553 self.assert_(Decimal(10) in ['a', 1.0, Decimal(10), (1,2), {}])
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1554 self.assert_(Decimal(10) not in ['a', 1.0, (1,2), {}])
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1555
33052
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1556 def test_copy(self):
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1557 # All copies should be deep
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1558 c = Context()
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1559 d = c.copy()
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1560 self.assertNotEqual(id(c), id(d))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1561 self.assertNotEqual(id(c.flags), id(d.flags))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1562 self.assertNotEqual(id(c.traps), id(d.traps))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1563
39707
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1564 class WithStatementTest(unittest.TestCase):
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1565 # Can't do these as docstrings until Python 2.6
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1566 # as doctest can't handle __future__ statements
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1567
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1568 def test_localcontext(self):
39721
2f086d58ca10 Make decimal.ContextManager a private implementation detail of decimal.localcontext()
Nick Coghlan <ncoghlan@gmail.com>
parents: 39707
diff changeset
1569 # Use a copy of the current context in the block
39707
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1570 orig_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1571 with localcontext() as enter_ctx:
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1572 set_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1573 final_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1574 self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1575 self.assert_(orig_ctx is not set_ctx, 'did not copy the context')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1576 self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1577
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1578 def test_localcontextarg(self):
39721
2f086d58ca10 Make decimal.ContextManager a private implementation detail of decimal.localcontext()
Nick Coghlan <ncoghlan@gmail.com>
parents: 39707
diff changeset
1579 # Use a copy of the supplied context in the block
39707
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1580 orig_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1581 new_ctx = Context(prec=42)
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1582 with localcontext(new_ctx) as enter_ctx:
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1583 set_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1584 final_ctx = getcontext()
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1585 self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1586 self.assert_(set_ctx.prec == new_ctx.prec, 'did not set correct context')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1587 self.assert_(new_ctx is not set_ctx, 'did not copy the context')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1588 self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
a83957c91fdb Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
1589
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1590 class ContextFlags(unittest.TestCase):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1591 def test_flags_irrelevant(self):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1592 # check that the result (numeric result + flags raised) of an
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1593 # arithmetic operation doesn't depend on the current flags
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1594
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1595 context = Context(prec=9, Emin = -999999999, Emax = 999999999,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1596 rounding=ROUND_HALF_EVEN, traps=[], flags=[])
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1597
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1598 # operations that raise various flags, in the form (function, arglist)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1599 operations = [
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1600 (context._apply, [Decimal("100E-1000000009")]),
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1601 (context.sqrt, [Decimal(2)]),
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1602 (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1603 (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1604 (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1605 ]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1606
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1607 # try various flags individually, then a whole lot at once
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1608 flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1609 [Inexact, Rounded, Underflow, Clamped, Subnormal]]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1610
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1611 for fn, args in operations:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1612 # find answer and flags raised using a clean context
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1613 context.clear_flags()
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1614 ans = fn(*args)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1615 flags = [k for k, v in context.flags.items() if v]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1616
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1617 for extra_flags in flagsets:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1618 # set flags, before calling operation
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1619 context.clear_flags()
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1620 for flag in extra_flags:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1621 context._raise_error(flag)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1622 new_ans = fn(*args)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1623
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1624 # flags that we expect to be set after the operation
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1625 expected_flags = list(flags)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1626 for flag in extra_flags:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1627 if flag not in expected_flags:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1628 expected_flags.append(flag)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1629 expected_flags.sort()
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1630
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1631 # flags we actually got
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1632 new_flags = [k for k,v in context.flags.items() if v]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1633 new_flags.sort()
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1634
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1635 self.assertEqual(ans, new_ans,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1636 "operation produces different answers depending on flags set: " +
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1637 "expected %s, got %s." % (ans, new_ans))
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1638 self.assertEqual(new_flags, expected_flags,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1639 "operation raises different flags depending on flags set: " +
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1640 "expected %s, got %s" % (expected_flags, new_flags))
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1641
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1642 def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1643 """ Execute the tests.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1644
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
1645 Runs all arithmetic tests if arith is True or if the "decimal" resource
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
1646 is enabled in regrtest.py
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 """
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
1648
37550
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
1649 init()
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1650 global TEST_ALL, DEBUG
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
1651 TEST_ALL = arith or is_resource_enabled('decimal')
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1652 DEBUG = debug
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
1653
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1654 if todo_tests is None:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1655 test_classes = [
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1656 DecimalExplicitConstructionTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1657 DecimalImplicitConstructionTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1658 DecimalArithmeticOperatorsTest,
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
1659 DecimalFormatTest,
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1660 DecimalUseOfContextTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1661 DecimalUsabilityTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1662 DecimalPythonAPItests,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1663 ContextAPItests,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1664 DecimalTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1665 WithStatementTest,
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1666 ContextFlags
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1667 ]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1668 else:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1669 test_classes = [DecimalTest]
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1670
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1671 # Dynamically build custom test definition for each file in the test
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1672 # directory and add the definitions to the DecimalTest class. This
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1673 # procedure insures that new files do not get skipped.
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1674 for filename in os.listdir(directory):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1675 if '.decTest' not in filename or filename.startswith("."):
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1676 continue
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1677 head, tail = filename.split('.')
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1678 if todo_tests is not None and head not in todo_tests:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1679 continue
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1680 tester = lambda self, f=filename: self.eval_file(directory + f)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1681 setattr(DecimalTest, 'test_' + head, tester)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1682 del filename, head, tail, tester
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1683
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684
37317
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
1685 try:
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
1686 run_unittest(*test_classes)
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1687 if todo_tests is None:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1688 import decimal as DecimalModule
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1689 run_doctest(DecimalModule, verbose)
37317
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
1690 finally:
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
1691 setcontext(ORIGINAL_CONTEXT)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1692
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1693 if __name__ == '__main__':
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1694 import optparse
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1695 p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]")
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1696 p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test')
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1697 p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests')
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1698 (opt, args) = p.parse_args()
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1699
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1700 if opt.skip:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1701 test_main(arith=False, verbose=True)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1702 elif args:
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1703 test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
1704 else:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1705 test_main(arith=True, verbose=True)