annotate Lib/test/test_decimal.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 31a7ff299698
children
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
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44923
diff changeset
27 import math
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 import os, sys
60155
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
29 import operator
32451
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 *
51294
aea90f5f398b Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50786
diff changeset
33 import numbers
59806
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
34 from test.test_support import (run_unittest, run_doctest,
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
35 is_resource_enabled, check_py3k_warnings)
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
36 import random
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
37 try:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
38 import threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
39 except ImportError:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
40 threading = None
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
42 # Useful Test Constant
61143
245e433d60eb Merged revisions 80681 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56971
diff changeset
43 Signals = tuple(getcontext().flags.keys())
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
44
61212
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
45 # Signals ordered with respect to precedence: when an operation
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
46 # produces multiple signals, signals occurring later in the list
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
47 # should be handled before those occurring earlier in the list.
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
48 OrderedSignals = (Clamped, Rounded, Inexact, Subnormal,
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
49 Underflow, Overflow, DivisionByZero, InvalidOperation)
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
50
37317
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
51 # 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
52 # 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
53 def init():
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
54 global ORIGINAL_CONTEXT
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
55 ORIGINAL_CONTEXT = getcontext().copy()
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
56 DefaultTestContext = Context(
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
57 prec = 9,
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
58 rounding = ROUND_HALF_EVEN,
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
59 traps = dict.fromkeys(Signals, 0)
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
60 )
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
61 setcontext(DefaultTestContext)
32481
b835d5c61d86 * Make the tests independent of the default precision.
Raymond Hettinger <python@rcn.com>
parents: 32477
diff changeset
62
60173
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
63 # decorator for skipping tests on non-IEEE 754 platforms
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
64 requires_IEEE_754 = unittest.skipUnless(
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
65 float.__getformat__("double").startswith("IEEE"),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
66 "test requires IEEE 754 doubles")
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
67
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 TESTDATADIR = 'decimaltestdata'
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
69 if __name__ == '__main__':
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
70 file = sys.argv[0]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
71 else:
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
72 file = __file__
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
73 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
74 directory = testdir + os.sep + TESTDATADIR + os.sep
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
76 skip_expected = not os.path.isdir(directory)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77
56838
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
78 # list of individual .decTest test ids that correspond to tests that
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
79 # we're skipping for one reason or another.
62868
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
80 skipped_test_ids = set([
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
81 # Skip implementation-specific scaleb tests.
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
82 'scbx164',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
83 'scbx165',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
84
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
85 # For some operations (currently exp, ln, log10, power), the decNumber
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
86 # reference implementation imposes additional restrictions on the context
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
87 # and operands. These restrictions are not part of the specification;
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
88 # however, the effect of these restrictions does show up in some of the
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
89 # testcases. We skip testcases that violate these restrictions, since
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
90 # Decimal behaves differently from decNumber for these testcases so these
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
91 # testcases would otherwise fail.
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
92 'expx901',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
93 'expx902',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
94 'expx903',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
95 'expx905',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
96 'lnx901',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
97 'lnx902',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
98 'lnx903',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
99 'lnx905',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
100 'logx901',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
101 'logx902',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
102 'logx903',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
103 'logx905',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
104 'powx1183',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
105 'powx1184',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
106 'powx4001',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
107 'powx4002',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
108 'powx4003',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
109 'powx4005',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
110 'powx4008',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
111 'powx4010',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
112 'powx4012',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
113 'powx4014',
3d8a7f701cb1 Fix Decimal speed issue; backport of r82652 from release27-maint.
Mark Dickinson <dickinsm@gmail.com>
parents: 61212
diff changeset
114 ])
56838
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
115
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 # 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
117 # Slower, since it runs some things several times.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 EXTENDEDERRORTEST = False
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 #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
121 ErrorNames = {'clamped' : Clamped,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
122 'conversion_syntax' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 'division_by_zero' : DivisionByZero,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
124 'division_impossible' : InvalidOperation,
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
125 'division_undefined' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 'inexact' : Inexact,
32560
e4b96c3a3e12 Module and tests:
Raymond Hettinger <python@rcn.com>
parents: 32505
diff changeset
127 'invalid_context' : InvalidOperation,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 'invalid_operation' : InvalidOperation,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 'overflow' : Overflow,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 'rounded' : Rounded,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 'subnormal' : Subnormal,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 'underflow' : Underflow}
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 def Nonfunction(*args):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 """Doesn't do anything."""
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 return None
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 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
140 'down' : ROUND_DOWN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 'floor' : ROUND_FLOOR,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 'half_down' : ROUND_HALF_DOWN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 'half_even' : ROUND_HALF_EVEN,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 'half_up' : ROUND_HALF_UP,
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
145 'up' : ROUND_UP,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
146 '05up' : ROUND_05UP}
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 # 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
149 # interface without changing the test files from Cowlishaw
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
150 nameAdapter = {'and':'logical_and',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
151 'apply':'_apply',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
152 'class':'number_class',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
153 'comparesig':'compare_signal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
154 'comparetotal':'compare_total',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
155 'comparetotmag':'compare_total_mag',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
156 'copy':'copy_decimal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
157 'copyabs':'copy_abs',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
158 'copynegate':'copy_negate',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
159 'copysign':'copy_sign',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
160 'divideint':'divide_int',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
161 'invert':'logical_invert',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
162 'iscanonical':'is_canonical',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
163 'isfinite':'is_finite',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
164 'isinfinite':'is_infinite',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
165 'isnan':'is_nan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
166 'isnormal':'is_normal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
167 'isqnan':'is_qnan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
168 'issigned':'is_signed',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
169 'issnan':'is_snan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
170 'issubnormal':'is_subnormal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
171 'iszero':'is_zero',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
172 'maxmag':'max_mag',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
173 'minmag':'min_mag',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
174 'nextminus':'next_minus',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
175 'nextplus':'next_plus',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
176 'nexttoward':'next_toward',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
177 'or':'logical_or',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
178 'reduce':'normalize',
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 'remaindernear':'remainder_near',
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
180 'samequantum':'same_quantum',
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 'squareroot':'sqrt',
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
182 'toeng':'to_eng_string',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
183 'tointegral':'to_integral_value',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
184 'tointegralx':'to_integral_exact',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
185 'tosci':'to_sci_string',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
186 'xor':'logical_xor',
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 }
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
189 # The following functions return True/False rather than a Decimal instance
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
190
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
191 LOGICAL_FUNCTIONS = (
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
192 'is_canonical',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
193 'is_finite',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
194 'is_infinite',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
195 'is_nan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
196 'is_normal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
197 'is_qnan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
198 'is_signed',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
199 'is_snan',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
200 'is_subnormal',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
201 'is_zero',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
202 'same_quantum',
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
203 )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
204
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 class DecimalTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 """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
207
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 Changed for unittest.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 """
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 def setUp(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 self.context = Context()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 self.ignore_list = ['#']
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 # Basically, a # means return NaN InvalidOperation.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 # Different from a sNaN in trim
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 self.ChangeDict = {'precision' : self.change_precision,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217 'rounding' : self.change_rounding_method,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 'maxexponent' : self.change_max_exponent,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 'minexponent' : self.change_min_exponent,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 'clamp' : self.change_clamp}
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 def eval_file(self, file):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 global skip_expected
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 if skip_expected:
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
225 raise unittest.SkipTest
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 return
65339
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
227 with open(file) as f:
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
228 for line in f:
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
229 line = line.replace('\r\n', '').replace('\n', '')
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
230 #print line
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
231 try:
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
232 t = self.eval_line(line)
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
233 except DecimalException as exception:
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
234 #Exception raised where there shouldn't have been one.
65339
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62867
diff changeset
235 self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
32451
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 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239 def eval_line(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 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
241 s = (s.split('->')[0] + '->' +
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 s.split('->')[1].split('--')[0]).strip()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244 s = s.split('--')[0].strip()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246 for ignore in self.ignore_list:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 if s.find(ignore) >= 0:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248 #print s.split()[0], 'NotImplemented--', ignore
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250 if not s:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
252 elif ':' in s:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253 return self.eval_directive(s)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
254 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
255 return self.eval_equation(s)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
256
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
257 def eval_directive(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
258 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
259 if funct == 'rounding':
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
260 value = RoundingDict[value]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
261 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
262 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 value = int(value)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
264 except ValueError:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
265 pass
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
266
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
267 funct = self.ChangeDict.get(funct, Nonfunction)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268 funct(value)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
269
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
270 def eval_equation(self, s):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
271 #global DEFAULT_PRECISION
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
272 #print DEFAULT_PRECISION
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
273
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
274 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
275 return
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
276
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
277 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
278 Sides = s.split('->')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
279 L = Sides[0].strip().split()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 id = L[0]
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
281 if DEBUG:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
282 print "Test ", id,
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 funct = L[1].lower()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 valstemp = L[2:]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285 L = Sides[1].strip().split()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 ans = L[0]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 exceptions = L[1:]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 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
289 raise InvalidOperation
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 def FixQuotes(val):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 val = val.replace("'", '').replace('"', '')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 return val
56838
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
295
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
296 if id in skipped_test_ids:
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
297 return
670caa9775f6 Merged revisions 75286 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55886
diff changeset
298
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299 fname = nameAdapter.get(funct, funct)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 if fname == 'rescale':
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
301 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 funct = getattr(self.context, fname)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 vals = []
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
304 conglomerate = ''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 quote = 0
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 theirexceptions = [ErrorNames[x.lower()] for x in exceptions]
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
308 for exception in Signals:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
309 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
310 for exception in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
311 self.context.traps[exception] = 0
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
312 for i, val in enumerate(valstemp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 if val.count("'") % 2 == 1:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 quote = 1 - quote
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 if quote:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 conglomerate = conglomerate + ' ' + val
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 continue
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 val = conglomerate + val
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320 conglomerate = ''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 v = FixQuotes(val)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 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
323 if EXTENDEDERRORTEST:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 for error in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
325 self.context.traps[error] = 1
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 funct(self.context.create_decimal(v))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 except error:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 pass
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
330 except Signals, e:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 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
332 (e, s, error))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 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
335 self.context.traps[error] = 0
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 v = self.context.create_decimal(v)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337 else:
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
338 v = Decimal(v, self.context)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 vals.append(v)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 ans = FixQuotes(ans)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 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
344 for error in theirexceptions:
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
345 self.context.traps[error] = 1
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 funct(*vals)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 except error:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 pass
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
350 except Signals, e:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 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
352 (e, s, error))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 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
355 self.context.traps[error] = 0
61212
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
356
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
357 # as above, but add traps cumulatively, to check precedence
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
358 ordered_errors = [e for e in OrderedSignals if e in theirexceptions]
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
359 for error in ordered_errors:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
360 self.context.traps[error] = 1
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
361 try:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
362 funct(*vals)
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
363 except error:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
364 pass
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
365 except Signals, e:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
366 self.fail("Raised %s in %s; expected %s" %
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
367 (type(e), s, error))
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
368 else:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
369 self.fail("Did not raise %s in %s" % (error, s))
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
370 # reset traps
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
371 for error in ordered_errors:
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
372 self.context.traps[error] = 0
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
373
ecc32e2f4656 Merged revisions 80753 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61143
diff changeset
374
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
375 if DEBUG:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
376 print "--", self.context
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377 try:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 result = str(funct(*vals))
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
379 if fname in LOGICAL_FUNCTIONS:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 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
381 except Signals, error:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382 self.fail("Raised %s in %s" % (error, s))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 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
384 print "ERROR:", s
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385 raise
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 myexceptions = self.getexceptions()
32576
83cbc025bfc7 Improve Context construction and representation:
Raymond Hettinger <python@rcn.com>
parents: 32561
diff changeset
388 self.context.clear_flags()
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 self.assertEqual(result, ans,
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 'Incorrect answer for ' + s + ' -- got ' + result)
59806
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
392 self.assertItemsEqual(myexceptions, theirexceptions,
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
393 '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
394 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 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
397 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
398
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 def change_precision(self, prec):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 self.context.prec = prec
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 def change_rounding_method(self, rounding):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 self.context.rounding = rounding
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 def change_min_exponent(self, exp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 self.context.Emin = exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 def change_max_exponent(self, exp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
406 self.context.Emax = exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 def change_clamp(self, clamp):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408 self.context._clamp = clamp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410
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 # 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
413
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 class DecimalExplicitConstructionTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 '''Unit tests for Explicit Construction cases of Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 def test_explicit_empty(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 self.assertEqual(Decimal(), Decimal("0"))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
420 def test_explicit_from_None(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
421 self.assertRaises(TypeError, Decimal, None)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423 def test_explicit_from_int(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 #positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 d = Decimal(45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427 self.assertEqual(str(d), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
428
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429 #very large positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
430 d = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
431 self.assertEqual(str(d), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
432
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 #negative
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
435 self.assertEqual(str(d), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
436
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
437 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
438 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
439 self.assertEqual(str(d), '0')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
440
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
441 def test_explicit_from_string(self):
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 #empty
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
444 self.assertEqual(str(Decimal('')), 'NaN')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
445
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
446 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
447 self.assertEqual(str(Decimal('45')), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
448
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 self.assertEqual(str(Decimal('45.34')), '45.34')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 #engineer notation
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 self.assertEqual(str(Decimal('45e2')), '4.5E+3')
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 #just not a number
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 self.assertEqual(str(Decimal('ugly')), 'NaN')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457
44650
7b798548a4ad Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
Mark Dickinson <dickinsm@gmail.com>
parents: 44593
diff changeset
458 #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
459 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
460 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
461
46089
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
462 #unicode strings should be permitted
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
463 self.assertEqual(str(Decimal(u'0E-017')), '0E-17')
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
464 self.assertEqual(str(Decimal(u'45')), '45')
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
465 self.assertEqual(str(Decimal(u'-Inf')), '-Infinity')
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
466 self.assertEqual(str(Decimal(u'NaN123')), 'NaN123')
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
467
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 def test_explicit_from_tuples(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
469
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
470 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
471 d = Decimal( (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
472 self.assertEqual(str(d), '0')
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 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475 d = Decimal( (1, (4, 5), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 self.assertEqual(str(d), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 d = Decimal( (0, (4, 5, 3, 4), -2) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 self.assertEqual(str(d), '45.34')
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 #weird
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 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
484 self.assertEqual(str(d), '-4.34913534E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 #wrong number of items
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 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
488
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 #bad sign
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
491 self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
492 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
493
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 #bad exp
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
496 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
497 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
498
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
499 #bad coefficients
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500 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
501 self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
502 self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
503 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
504
60082
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
505 def test_explicit_from_bool(self):
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
506 self.assertIs(bool(Decimal(0)), False)
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
507 self.assertIs(bool(Decimal(1)), True)
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
508 self.assertEqual(Decimal(False), Decimal(0))
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
509 self.assertEqual(Decimal(True), Decimal(1))
f69769ab0eb8 Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59806
diff changeset
510
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 def test_explicit_from_Decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 #positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 d = Decimal(45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 self.assertEqual(str(e), '45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519 #very large positive
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520 d = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
522 self.assertEqual(str(e), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
525 #negative
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
526 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
527 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
528 self.assertEqual(str(e), '-45')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
530
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
531 #zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
532 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
533 e = Decimal(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
534 self.assertEqual(str(e), '0')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535 self.assertNotEqual(id(d), id(e))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536
60173
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
537 @requires_IEEE_754
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
538 def test_explicit_from_float(self):
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
539 r = Decimal(0.1)
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
540 self.assertEqual(type(r), Decimal)
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
541 self.assertEqual(str(r),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
542 '0.1000000000000000055511151231257827021181583404541015625')
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
543 self.assertTrue(Decimal(float('nan')).is_qnan())
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
544 self.assertTrue(Decimal(float('inf')).is_infinite())
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
545 self.assertTrue(Decimal(float('-inf')).is_infinite())
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
546 self.assertEqual(str(Decimal(float('nan'))),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
547 str(Decimal('NaN')))
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
548 self.assertEqual(str(Decimal(float('inf'))),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
549 str(Decimal('Infinity')))
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
550 self.assertEqual(str(Decimal(float('-inf'))),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
551 str(Decimal('-Infinity')))
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
552 self.assertEqual(str(Decimal(float('-0.0'))),
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
553 str(Decimal('-0')))
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
554 for i in range(200):
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
555 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
556 self.assertEqual(x, float(Decimal(x))) # roundtrip
cca4b72f450b Issue 8257: Decimal constructor to accept float.
Raymond Hettinger <python@rcn.com>
parents: 60156
diff changeset
557
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558 def test_explicit_context_create_decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
559
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
560 nc = copy.copy(getcontext())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
561 nc.prec = 3
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563 # empty
32666
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
564 d = Decimal()
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
565 self.assertEqual(str(d), '0')
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
566 d = nc.create_decimal()
c13d911ae05b * Rename "Signals" to "_signals" making it non-public.
Raymond Hettinger <python@rcn.com>
parents: 32576
diff changeset
567 self.assertEqual(str(d), '0')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
569 # from None
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
570 self.assertRaises(TypeError, nc.create_decimal, None)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
571
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572 # from int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 d = nc.create_decimal(456)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
574 self.assertIsInstance(d, Decimal)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 self.assertEqual(nc.create_decimal(45678),
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 nc.create_decimal('457E+2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578 # from string
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 d = Decimal('456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 self.assertEqual(str(d), '456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 d = nc.create_decimal('456789')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
582 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
583 # 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
584 # 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
585 # 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
586 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
587
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588 # from tuples
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 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
590 self.assertEqual(str(d), '-4.34913534E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
591 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
592 self.assertEqual(str(d), '-4.35E-17')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
593
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
594 # from Decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
595 prevdec = Decimal(500000123)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
596 d = Decimal(prevdec)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
597 self.assertEqual(str(d), '500000123')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
598 d = nc.create_decimal(prevdec)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
599 self.assertEqual(str(d), '5.00E+8')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
600
55519
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
601 def test_unicode_digits(self):
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
602 test_values = {
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
603 u'\uff11': '1',
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
604 u'\u0660.\u0660\u0663\u0667\u0662e-\u0663' : '0.0000372',
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
605 u'-nan\u0c68\u0c6a\u0c66\u0c66' : '-NaN2400',
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
606 }
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
607 for input, expected in test_values.items():
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
608 self.assertEqual(str(Decimal(input)), expected)
8b48b282eba9 Merged revisions 74281 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 52189
diff changeset
609
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
610
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
611 class DecimalImplicitConstructionTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
612 '''Unit tests for Implicit Construction cases of Decimal.'''
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
613
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
614 def test_implicit_from_None(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
615 self.assertRaises(TypeError, eval, 'Decimal(5) + None', globals())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
616
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617 def test_implicit_from_int(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
618 #normal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
619 self.assertEqual(str(Decimal(5) + 45), '50')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
620 #exceeding precision
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
621 self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
622
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 def test_implicit_from_string(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', globals())
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
626 def test_implicit_from_float(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627 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
628
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629 def test_implicit_from_Decimal(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 self.assertEqual(Decimal(5) + Decimal(45), Decimal(50))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631
34813
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
632 def test_rop(self):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
633 # 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
634 class E:
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
635 def __divmod__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
636 return 'divmod ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
637 def __rdivmod__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
638 return str(other) + ' rdivmod'
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
639 def __lt__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
640 return 'lt ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
641 def __gt__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
642 return 'gt ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
643 def __le__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
644 return 'le ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
645 def __ge__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
646 return 'ge ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
647 def __eq__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
648 return 'eq ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
649 def __ne__(self, other):
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
650 return 'ne ' + str(other)
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
651
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
652 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
653 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
654 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
655 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
656 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
657 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
658 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
659 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
660
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
661 # insert operator methods and then exercise them
37271
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
662 oplist = [
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
663 ('+', '__add__', '__radd__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
664 ('-', '__sub__', '__rsub__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
665 ('*', '__mul__', '__rmul__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
666 ('%', '__mod__', '__rmod__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
667 ('//', '__floordiv__', '__rfloordiv__'),
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
668 ('**', '__pow__', '__rpow__')
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
669 ]
59806
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
670 with check_py3k_warnings():
63641
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
671 if 1 / 2 == 0:
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
672 # testing with classic division, so add __div__
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
673 oplist.append(('/', '__div__', '__rdiv__'))
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
674 else:
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
675 # testing with -Qnew, so add __truediv__
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
676 oplist.append(('/', '__truediv__', '__rtruediv__'))
37307
e58a71a8550e whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37271
diff changeset
677
37271
80c4fc03ff9b Make test_decimal work with -Qnew.
Georg Brandl <georg@python.org>
parents: 34813
diff changeset
678 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
679 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
680 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
681 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
682 'str' + lop + '10')
26f3272d9e11 - Fixed decimal operator and comparison methods to return NotImplemented
Raymond Hettinger <python@rcn.com>
parents: 34760
diff changeset
683 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
684 '10' + rop + 'str')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
686
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
687 class DecimalFormatTest(unittest.TestCase):
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
688 '''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
689 def test_formatting(self):
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
690 # 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
691 test_values = [
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
692 ('e', '0E-15', '0e-15'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
693 ('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
694 ('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
695 ('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
696 ('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
697 ('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
698 ('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
699 ('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
700 ('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
701 ('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
702 ('e', '-15', '-1.5e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
703 ('e', '0', '0e+0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
704 ('e', '0E1', '0e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
705 ('e', '0.0', '0e-1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
706 ('e', '0.00', '0e-2'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
707 ('.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
708 ('.6e', '0', '0.000000e+6'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
709 ('.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
710 ('.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
711 ('.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
712 ('.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
713 ('f', '0', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
714 ('f', '0.0', '0.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
715 ('f', '0E-2', '0.00'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
716 ('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
717 ('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
718 ('f', '3.2E1', '32'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
719 ('f', '3.2E2', '320'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
720 ('f', '3.20E2', '320'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
721 ('f', '3.200E2', '320.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
722 ('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
723 ('.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
724 ('.6f', '0E1', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
725 ('.6f', '0', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
726 ('.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
727 ('.0f', '0e-2', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
728 ('.0f', '3.14159265', '3'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
729 ('.1f', '3.14159265', '3.1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
730 ('.4f', '3.14159265', '3.1416'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
731 ('.6f', '3.14159265', '3.141593'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
732 ('.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
733 ('.8f', '3.14159265', '3.14159265'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
734 ('.9f', '3.14159265', '3.141592650'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
735
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
736 ('g', '0', '0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
737 ('g', '0.0', '0.0'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
738 ('g', '0E1', '0e+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
739 ('G', '0E1', '0E+1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
740 ('g', '0E-5', '0.00000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
741 ('g', '0E-6', '0.000000'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
742 ('g', '0E-7', '0e-7'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
743 ('g', '-0E2', '-0e+2'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
744 ('.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
745 ('.1g', '3.14159265', '3'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
746 ('.2g', '3.14159265', '3.1'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
747 ('.5g', '3.14159265', '3.1416'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
748 ('.7g', '3.14159265', '3.141593'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
749 ('.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
750 ('.9g', '3.14159265', '3.14159265'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
751 ('.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
752
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
753 ('%', '0E1', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
754 ('%', '0E0', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
755 ('%', '0E-1', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
756 ('%', '0E-2', '0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
757 ('%', '0E-3', '0.0%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
758 ('%', '0E-4', '0.00%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
759
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
760 ('.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
761 ('.3%', '0E10', '0.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
762 ('.3%', '0E-10', '0.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
763 ('.3%', '2.34', '234.000%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
764 ('.3%', '1.234567', '123.457%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
765 ('.0%', '1.23', '123%'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
766
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
767 ('e', 'NaN', 'NaN'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
768 ('f', '-NaN123', '-NaN123'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
769 ('+g', 'NaN456', '+NaN456'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
770 ('.3e', 'Inf', 'Infinity'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
771 ('.16f', '-Inf', '-Infinity'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
772 ('.0g', '-sNaN', '-sNaN'),
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
773
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
774 ('', '1.00', '1.00'),
52189
fd4c83928080 Merged revisions 70430 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51294
diff changeset
775
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
776 # test alignment and padding
55890
40fd8fd96d96 Issue #6857: Fix Decimal formatting to be consistent with existing float
Mark Dickinson <dickinsm@gmail.com>
parents: 55877
diff changeset
777 ('6', '123', ' 123'),
52189
fd4c83928080 Merged revisions 70430 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51294
diff changeset
778 ('<6', '123', '123 '),
fd4c83928080 Merged revisions 70430 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51294
diff changeset
779 ('>6', '123', ' 123'),
fd4c83928080 Merged revisions 70430 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51294
diff changeset
780 ('^6', '123', ' 123 '),
fd4c83928080 Merged revisions 70430 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51294
diff changeset
781 ('=+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
782 ('#<10', 'NaN', 'NaN#######'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
783 ('#<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
784 ('#<+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
785 ('#< 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
786 ('@>10', '-Inf', '@-Infinity'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
787 ('#>5', '-Inf', '-Infinity'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
788 ('?^5', '123', '?123?'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
789 ('%^6', '123', '%123%%'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
790 (' ^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
791 ('/=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
792 ('/=+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
793 ('/= 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
794
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
795 # thousands separator
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
796 (',', '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
797 (',', '123456', '123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
798 (',', '12345', '12,345'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
799 (',', '1234', '1,234'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
800 (',', '123', '123'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
801 (',', '12', '12'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
802 (',', '1', '1'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
803 (',', '0', '0'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
804 (',', '-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
805 (',', '-123456', '-123,456'),
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
806 ('7,', '123456', '123,456'),
55890
40fd8fd96d96 Issue #6857: Fix Decimal formatting to be consistent with existing float
Mark Dickinson <dickinsm@gmail.com>
parents: 55877
diff changeset
807 ('8,', '123456', ' 123,456'),
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
808 ('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
809 ('+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
810 (' 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
811 ('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
812 ('+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
813 # ... with fractional part...
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
814 ('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
815 ('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
816 ('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
817 ('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
818 ('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
819 ('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
820 ('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
821 # 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
822 (',', '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
823 (',%', '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
824 (',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
825 (',E', '123456', '1.23456E+5'),
55874
e895ef82c086 Merged revisions 74704 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55519
diff changeset
826
e895ef82c086 Merged revisions 74704 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55519
diff changeset
827 # issue 6850
e895ef82c086 Merged revisions 74704 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55519
diff changeset
828 ('a=-7.0', '0.12345', 'aaaa0.1'),
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
829 ]
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
830 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
831 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
832
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
833 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
834 try:
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
835 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
836 except ImportError:
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
837 return
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
838
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
839 # 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
840 en_US = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
841 'decimal_point' : '.',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
842 '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
843 'thousands_sep': ','
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
844 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
845
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
846 fr_FR = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
847 'decimal_point' : ',',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
848 'grouping' : [CHAR_MAX],
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
849 'thousands_sep' : ''
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
850 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
851
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
852 ru_RU = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
853 'decimal_point' : ',',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
854 '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
855 'thousands_sep' : ' '
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
856 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
857
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
858 crazy = {
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
859 'decimal_point' : '&',
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
860 '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
861 'thousands_sep' : '-'
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
862 }
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
863
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
864
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
865 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
866 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
867
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
868 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
869 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
870 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
871 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
872
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
873 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
874 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
875 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
876 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
877
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
878 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
879 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
880 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
881 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
882
52200
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
883 # zero padding
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
884 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
885 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
886 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
887 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
888
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
889 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
890 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
891 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
892 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
893 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
894 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
895
2d94c8fed376 Fix bug in _insert_thousands_sep: too much zero padding could be
Mark Dickinson <dickinsm@gmail.com>
parents: 52195
diff changeset
896 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
897 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
898 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
899 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
900 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
901 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
902 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
903 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
904
52195
5dee93a97558 Issue #2110: Add support for thousands separator and 'n' format specifier
Mark Dickinson <dickinsm@gmail.com>
parents: 52188
diff changeset
905
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 class DecimalArithmeticOperatorsTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 '''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
908
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 def test_addition(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 d1 = Decimal('-11.1')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 d2 = Decimal('22.2')
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 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 self.assertEqual(d1+d2, Decimal('11.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 self.assertEqual(d2+d1, Decimal('11.1'))
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 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 c = d1 + 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 self.assertEqual(c, Decimal('-6.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 c = 5 + d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925 self.assertEqual(c, Decimal('-6.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 d1 += d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 self.assertEqual(d1, Decimal('11.1'))
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 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 d1 += 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 self.assertEqual(d1, Decimal('16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 def test_subtraction(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 d1 = Decimal('-11.1')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 d2 = Decimal('22.2')
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 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 self.assertEqual(d1-d2, Decimal('-33.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 self.assertEqual(d2-d1, Decimal('33.3'))
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 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 c = d1 - 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 self.assertEqual(c, Decimal('-16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 c = 5 - d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 self.assertEqual(c, Decimal('16.1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 d1 -= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 self.assertEqual(d1, Decimal('-33.3'))
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 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 d1 -= 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 self.assertEqual(d1, Decimal('-38.3'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 def test_multiplication(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 d1 = Decimal('-5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 d2 = Decimal('3')
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 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 self.assertEqual(d1*d2, Decimal('-15'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 self.assertEqual(d2*d1, Decimal('-15'))
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 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 c = d1 * 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 self.assertEqual(c, Decimal('-25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 c = 5 * d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 self.assertEqual(c, Decimal('-25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 d1 *= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 self.assertEqual(d1, Decimal('-15'))
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 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 d1 *= 5
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 self.assertEqual(d1, Decimal('-75'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 def test_division(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992 d1 = Decimal('-5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 d2 = Decimal('2')
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 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 self.assertEqual(d1/d2, Decimal('-2.5'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 self.assertEqual(d2/d1, Decimal('-0.4'))
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 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000 c = d1 / 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 self.assertEqual(c, Decimal('-1.25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 c = 4 / d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 self.assertEqual(c, Decimal('-0.8'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 d1 /= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 self.assertEqual(d1, Decimal('-2.5'))
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 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 d1 /= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 self.assertEqual(d1, Decimal('-0.625'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 def test_floor_division(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 d2 = Decimal('2')
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 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 self.assertEqual(d1//d2, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 self.assertEqual(d2//d1, Decimal('0'))
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 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 c = d1 // 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 c = 7 // d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 d1 //= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 self.assertEqual(d1, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 d1 //= 2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044 def test_powering(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 self.assertEqual(d1**d2, Decimal('25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 self.assertEqual(d2**d1, Decimal('32'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 c = d1 ** 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 self.assertEqual(c, Decimal('625'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1057
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1059 c = 7 ** d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1060 self.assertEqual(c, Decimal('16807'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1061 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 d1 **= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 self.assertEqual(d1, Decimal('25'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 d1 **= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 self.assertEqual(d1, Decimal('390625'))
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 def test_module(self):
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 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 self.assertEqual(d1%d2, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 self.assertEqual(d2%d1, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 c = d1 % 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 self.assertEqual(c, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 self.assertEqual(type(c), type(d1))
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 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 c = 7 % d1
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 self.assertEqual(c, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 self.assertEqual(type(c), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 #inline with decimal
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 d1 %= d2
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 #inline with other type
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 d1 %= 4
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 self.assertEqual(d1, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 def test_floor_div_module(self):
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 d1 = Decimal('5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 d2 = Decimal('2')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 #two Decimals
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 (p, q) = divmod(d1, d2)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 self.assertEqual(p, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 self.assertEqual(q, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 self.assertEqual(type(q), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 #with other type, left
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 (p, q) = divmod(d1, 4)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 self.assertEqual(p, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 self.assertEqual(q, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 self.assertEqual(type(q), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 #with other type, right
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 (p, q) = divmod(7, d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 self.assertEqual(p, Decimal('1'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 self.assertEqual(q, Decimal('2'))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 self.assertEqual(type(p), type(d1))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 self.assertEqual(type(q), type(d1))
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 def test_unary_operators(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 self.assertEqual(+Decimal(45), Decimal(+45)) # +
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 self.assertEqual(-Decimal(45), Decimal(-45)) # -
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127 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
1128
45201
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1129 def test_nan_comparisons(self):
60155
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1130 # comparisons involving signaling nans signal InvalidOperation
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1131
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1132 # order comparisons (<, <=, >, >=) involving only quiet nans
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1133 # also signal InvalidOperation
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1134
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1135 # equality comparisons (==, !=) involving only quiet nans
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1136 # don't signal, but return False or True respectively.
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1137
45201
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1138 n = Decimal('NaN')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1139 s = Decimal('sNaN')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1140 i = Decimal('Inf')
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1141 f = Decimal('2')
60155
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1142
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1143 qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1144 snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1145 order_ops = operator.lt, operator.le, operator.gt, operator.ge
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1146 equality_ops = operator.eq, operator.ne
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1147
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1148 # results when InvalidOperation is not trapped
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1149 for x, y in qnan_pairs + snan_pairs:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1150 for op in order_ops + equality_ops:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1151 got = op(x, y)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1152 expected = True if op is operator.ne else False
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1153 self.assertIs(expected, got,
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1154 "expected {0!r} for operator.{1}({2!r}, {3!r}); "
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1155 "got {4!r}".format(
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1156 expected, op.__name__, x, y, got))
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1157
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1158 # repeat the above, but this time trap the InvalidOperation
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1159 with localcontext() as ctx:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1160 ctx.traps[InvalidOperation] = 1
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1161
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1162 for x, y in qnan_pairs:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1163 for op in equality_ops:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1164 got = op(x, y)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1165 expected = True if op is operator.ne else False
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1166 self.assertIs(expected, got,
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1167 "expected {0!r} for "
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1168 "operator.{1}({2!r}, {3!r}); "
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1169 "got {4!r}".format(
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1170 expected, op.__name__, x, y, got))
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1171
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1172 for x, y in snan_pairs:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1173 for op in equality_ops:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1174 self.assertRaises(InvalidOperation, operator.eq, x, y)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1175 self.assertRaises(InvalidOperation, operator.ne, x, y)
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1176
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1177 for x, y in qnan_pairs + snan_pairs:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1178 for op in order_ops:
ae7cf578de5a Issue #7279: Make comparisons involving a Decimal sNaN signal InvalidOperation.
Mark Dickinson <dickinsm@gmail.com>
parents: 60151
diff changeset
1179 self.assertRaises(InvalidOperation, op, x, y)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180
58934
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1181 def test_copy_sign(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1182 d = Decimal(1).copy_sign(Decimal(-2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1183
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1184 self.assertEqual(Decimal(1).copy_sign(-2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1185 self.assertRaises(TypeError, Decimal(1).copy_sign, '-2')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 # 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
1188
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189 def thfunc1(cls):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 d1 = Decimal(1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 d3 = Decimal(3)
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1192 test1 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1193 cls.synchro.wait()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1194 test2 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 cls.finish1.set()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1196
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1197 cls.assertEqual(test1, Decimal('0.3333333333333333333333333333'))
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1198 cls.assertEqual(test2, Decimal('0.3333333333333333333333333333'))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1199 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 def thfunc2(cls):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202 d1 = Decimal(1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 d3 = Decimal(3)
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1204 test1 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 thiscontext = getcontext()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206 thiscontext.prec = 18
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1207 test2 = d1/d3
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1208 cls.synchro.set()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209 cls.finish2.set()
45998
cdeba20ccd6a Small fix that complicated the test actually when that
Facundo Batista <facundobatista@gmail.com>
parents: 45566
diff changeset
1210
46625
c4d90fa935ea Fixed some test structures. Thanks Mark Dickinson.
Facundo Batista <facundobatista@gmail.com>
parents: 46613
diff changeset
1211 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
1212 cls.assertEqual(test2, Decimal('0.333333333333333333'))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1214
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1215
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 class DecimalUseOfContextTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 '''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
1218
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1219 try:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1220 import threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1221 except ImportError:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1222 threading = None
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1223
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 # 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
1225 # 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
1226
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 def test_threading(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 #Test the "threading isolation" of a Context.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1229
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 self.synchro = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231 self.finish1 = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 self.finish2 = threading.Event()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 th1 = threading.Thread(target=thfunc1, args=(self,))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235 th2 = threading.Thread(target=thfunc2, args=(self,))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237 th1.start()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238 th2.start()
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 self.finish1.wait()
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1241 self.finish2.wait()
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1243
34184
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1244 if threading is None:
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1245 del test_threading
36cf94f57415 Bug #1083645
Raymond Hettinger <python@rcn.com>
parents: 34023
diff changeset
1246
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 class DecimalUsabilityTest(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 '''Unit tests for Usability cases of Decimal.'''
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 def test_comparison_operators(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1252
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1253 da = Decimal('23.42')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254 db = Decimal('23.42')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255 dc = Decimal('45')
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 #two Decimals
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1258 self.assertGreater(dc, da)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1259 self.assertGreaterEqual(dc, da)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1260 self.assertLess(da, dc)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1261 self.assertLessEqual(da, dc)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1262 self.assertEqual(da, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1263 self.assertNotEqual(da, dc)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1264 self.assertLessEqual(da, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1265 self.assertGreaterEqual(da, db)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 self.assertEqual(cmp(dc,da), 1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 self.assertEqual(cmp(da,dc), -1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268 self.assertEqual(cmp(da,db), 0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270 #a Decimal and an int
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1271 self.assertGreater(dc, 23)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1272 self.assertLess(23, dc)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1273 self.assertEqual(dc, 45)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1274 self.assertEqual(cmp(dc,23), 1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 self.assertEqual(cmp(23,dc), -1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276 self.assertEqual(cmp(dc,45), 0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 #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
1279 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
1280 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
1281 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
1282 self.assertNotEqual(da, object)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1284 # sortable
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1285 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
1286 b = a[:]
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1287 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
1288 a.sort()
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1289 self.assertEqual(a, b)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1291 # with None
59806
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
1292 with check_py3k_warnings():
63641
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
1293 self.assertFalse(Decimal(1) < None)
657ae877c0bf Merged revisions 79187-79189 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62868
diff changeset
1294 self.assertTrue(Decimal(1) > None)
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1295
60151
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1296 def test_decimal_float_comparison(self):
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1297 da = Decimal('0.25')
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1298 db = Decimal('3.0')
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1299 self.assertLess(da, 3.0)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1300 self.assertLessEqual(da, 3.0)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1301 self.assertGreater(db, 0.25)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1302 self.assertGreaterEqual(db, 0.25)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1303 self.assertNotEqual(da, 1.5)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1304 self.assertEqual(da, 0.25)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1305 self.assertGreater(3.0, da)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1306 self.assertGreaterEqual(3.0, da)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1307 self.assertLess(0.25, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1308 self.assertLessEqual(0.25, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1309 self.assertNotEqual(0.25, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1310 self.assertEqual(3.0, db)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1311 self.assertNotEqual(0.1, Decimal('0.1'))
60151
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1312
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 def test_copy_and_deepcopy_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 d = Decimal('43.24')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 c = copy.copy(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 self.assertEqual(id(c), id(d))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 dc = copy.deepcopy(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 self.assertEqual(id(dc), id(d))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320 def test_hash_method(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321 #just that it's hashable
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 hash(Decimal(23))
60156
17f08d65cc0d Issue #7279: Make Decimal('nan') hashable. Decimal('snan') remains unhashable.
Mark Dickinson <dickinsm@gmail.com>
parents: 60155
diff changeset
1323 hash(Decimal('Infinity'))
17f08d65cc0d Issue #7279: Make Decimal('nan') hashable. Decimal('snan') remains unhashable.
Mark Dickinson <dickinsm@gmail.com>
parents: 60155
diff changeset
1324 hash(Decimal('-Infinity'))
17f08d65cc0d Issue #7279: Make Decimal('nan') hashable. Decimal('snan') remains unhashable.
Mark Dickinson <dickinsm@gmail.com>
parents: 60155
diff changeset
1325 hash(Decimal('nan123'))
17f08d65cc0d Issue #7279: Make Decimal('nan') hashable. Decimal('snan') remains unhashable.
Mark Dickinson <dickinsm@gmail.com>
parents: 60155
diff changeset
1326 hash(Decimal('-NaN'))
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1327
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1328 test_values = [Decimal(sign*(2**m + n))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1329 for m in [0, 14, 15, 16, 17, 30, 31,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1330 32, 33, 62, 63, 64, 65, 66]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1331 for n in range(-10, 10)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1332 for sign in [-1, 1]]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1333 test_values.extend([
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1334 Decimal("-0"), # zeros
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1335 Decimal("0.00"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1336 Decimal("-0.000"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1337 Decimal("0E10"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1338 Decimal("-0E12"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1339 Decimal("10.0"), # negative exponent
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1340 Decimal("-23.00000"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1341 Decimal("1230E100"), # positive exponent
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1342 Decimal("-4.5678E50"),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1343 # a value for which hash(n) != hash(n % (2**64-1))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1344 # in Python pre-2.6
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1345 Decimal(2**64 + 2**32 - 1),
43409
357cf07a23fe Issue #1772851. Optimization of __hash__ to behave better for big big
Facundo Batista <facundobatista@gmail.com>
parents: 43407
diff changeset
1346 # 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
1347 # version 2.6) long.__hash__
44603
b7d1b627a7d5 Fix Decimal hash in Python 2.5 maintenance branch so that hash(x) == hash(int(x))
Mark Dickinson <dickinsm@gmail.com>
parents: 44600
diff changeset
1348 Decimal("1.634E100"),
b7d1b627a7d5 Fix Decimal hash in Python 2.5 maintenance branch so that hash(x) == hash(int(x))
Mark Dickinson <dickinsm@gmail.com>
parents: 44600
diff changeset
1349 Decimal("90.697E100"),
b7d1b627a7d5 Fix Decimal hash in Python 2.5 maintenance branch so that hash(x) == hash(int(x))
Mark Dickinson <dickinsm@gmail.com>
parents: 44600
diff changeset
1350 Decimal("188.83E100"),
b7d1b627a7d5 Fix Decimal hash in Python 2.5 maintenance branch so that hash(x) == hash(int(x))
Mark Dickinson <dickinsm@gmail.com>
parents: 44600
diff changeset
1351 Decimal("1652.9E100"),
b7d1b627a7d5 Fix Decimal hash in Python 2.5 maintenance branch so that hash(x) == hash(int(x))
Mark Dickinson <dickinsm@gmail.com>
parents: 44600
diff changeset
1352 Decimal("56531E100"),
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1353 ])
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1354
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1355 # check that hash(d) == hash(int(d)) for integral values
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1356 for value in test_values:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1357 self.assertEqual(hash(value), hash(int(value)))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1358
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 #the same hash that to an int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 self.assertEqual(hash(Decimal(23)), hash(23))
60156
17f08d65cc0d Issue #7279: Make Decimal('nan') hashable. Decimal('snan') remains unhashable.
Mark Dickinson <dickinsm@gmail.com>
parents: 60155
diff changeset
1361 self.assertRaises(TypeError, hash, Decimal('sNaN'))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1362 self.assertTrue(hash(Decimal('Inf')))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1363 self.assertTrue(hash(Decimal('-Inf')))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364
60151
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1365 # check that the hashes of a Decimal float match when they
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1366 # represent exactly the same values
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1367 test_strings = ['inf', '-Inf', '0.0', '-.0e1',
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1368 '34.0', '2.5', '112390.625', '-0.515625']
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1369 for s in test_strings:
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1370 f = float(s)
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1371 d = Decimal(s)
1306033d8ad2 Issue #2531: Make float-to-decimal comparisons return correct results.
Mark Dickinson <dickinsm@gmail.com>
parents: 60082
diff changeset
1372 self.assertEqual(hash(f), hash(d))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1374 # check that the value of the hash doesn't depend on the
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1375 # current context (issue #1757)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1376 c = getcontext()
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1377 old_precision = c.prec
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1378 x = Decimal("123456789.1")
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1379
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1380 c.prec = 6
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1381 h1 = hash(x)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1382 c.prec = 10
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1383 h2 = hash(x)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1384 c.prec = 16
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1385 h3 = hash(x)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1386
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1387 self.assertEqual(h1, h2)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1388 self.assertEqual(h1, h3)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1389 c.prec = old_precision
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1390
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 def test_min_and_max_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 d1 = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 d2 = Decimal('28.5')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 l1 = 15
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 l2 = 28
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398 #between Decimals
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1399 self.assertIs(min(d1,d2), d1)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1400 self.assertIs(min(d2,d1), d1)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1401 self.assertIs(max(d1,d2), d2)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1402 self.assertIs(max(d2,d1), d2)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404 #between Decimal and long
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1405 self.assertIs(min(d1,l2), d1)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1406 self.assertIs(min(l2,d1), d1)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1407 self.assertIs(max(l1,d2), d2)
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1408 self.assertIs(max(d2,l1), d2)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410 def test_as_nonzero(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 #as false
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1412 self.assertFalse(Decimal(0))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 #as true
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1414 self.assertTrue(Decimal('0.372'))
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 def test_tostring_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417 #Test str and repr methods.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 d = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420 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
1421 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
1422
46089
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1423 # result type of string methods should be str, not unicode
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1424 unicode_inputs = [u'123.4', u'0.5E2', u'Infinity', u'sNaN',
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1425 u'-0.0E100', u'-NaN001', u'-Inf']
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1426
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1427 for u in unicode_inputs:
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1428 d = Decimal(u)
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1429 self.assertEqual(type(str(d)), str)
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1430 self.assertEqual(type(repr(d)), str)
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1431 self.assertEqual(type(d.to_eng_string()), str)
1899e08fa421 Issue #2482: Make sure that the coefficient of a Decimal instance
Mark Dickinson <dickinsm@gmail.com>
parents: 46083
diff changeset
1432
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433 def test_tonum_methods(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434 #Test float, int and long methods.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 d1 = Decimal('66')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437 d2 = Decimal('15.32')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 self.assertEqual(int(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441 self.assertEqual(int(d2), 15)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1443 #long
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1444 self.assertEqual(long(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 self.assertEqual(long(d2), 15)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1448 self.assertEqual(float(d1), 66)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 self.assertEqual(float(d2), 15.32)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450
78754
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1451 def test_nan_to_float(self):
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1452 # Test conversions of decimal NANs to float.
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1453 # See http://bugs.python.org/issue15544
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1454 for s in ('nan', 'nan1234', '-nan', '-nan2468'):
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1455 f = float(Decimal(s))
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1456 self.assertTrue(math.isnan(f))
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1457
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1458 def test_snan_to_float(self):
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1459 for s in ('snan', '-snan', 'snan1357', '-snan1234'):
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1460 d = Decimal(s)
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1461 self.assertRaises(ValueError, float, d)
5dd5f824428c Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
Mark Dickinson <mdickinson@enthought.com>
parents: 68588
diff changeset
1462
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1463 def test_eval_round_trip(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465 #with zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1466 d = Decimal( (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1468
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 d = Decimal( (1, (4, 5), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473 #float
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 d = Decimal( (0, (4, 5, 3, 4), -2) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 #weird
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478 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
1479 self.assertEqual(d, eval(repr(d)))
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 def test_as_tuple(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 #with zero
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484 d = Decimal(0)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485 self.assertEqual(d.as_tuple(), (0, (0,), 0) )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487 #int
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488 d = Decimal(-45)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489 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
1490
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1491 #complicated string
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1492 d = Decimal("-4.34913534E-17")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1493 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
1494
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495 #inf
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 d = Decimal("Infinity")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1499 #leading zeros in coefficient should be stripped
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1500 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1501 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1502 d = Decimal( (1, (0, 0, 0), 37) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1503 self.assertEqual(d.as_tuple(), (1, (0,), 37))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1504 d = Decimal( (1, (), 37) )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1505 self.assertEqual(d.as_tuple(), (1, (0,), 37))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1506
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1507 #leading zeros in NaN diagnostic info should be stripped
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1508 d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1509 self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1510 d = Decimal( (1, (0, 0, 0), 'N') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1511 self.assertEqual(d.as_tuple(), (1, (), 'N') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1512 d = Decimal( (1, (), 'n') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1513 self.assertEqual(d.as_tuple(), (1, (), 'n') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1514
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1515 #coefficient in infinity should be ignored
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1516 d = Decimal( (0, (4, 5, 3, 4), 'F') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1517 self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1518 d = Decimal( (1, (0, 2, 7, 1), 'F') )
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1519 self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1520
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 def test_immutability_operations(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 # 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
1523
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524 d1 = Decimal('-25e55')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525 b1 = Decimal('-25e55')
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1526 d2 = Decimal('33e+33')
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1527 b2 = Decimal('33e+33')
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529 def checkSameDec(operation, useOther=False):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 if useOther:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 eval("d1." + operation + "(d2)")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535 self.assertEqual(d2._sign, b2._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1536 self.assertEqual(d2._int, b2._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1537 self.assertEqual(d2._exp, b2._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1538 else:
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1539 eval("d1." + operation + "()")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1540 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1541 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1543 return
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1544
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1545 Decimal(d1)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1546 self.assertEqual(d1._sign, b1._sign)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1547 self.assertEqual(d1._int, b1._int)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1548 self.assertEqual(d1._exp, b1._exp)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1550 checkSameDec("__abs__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1551 checkSameDec("__add__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552 checkSameDec("__div__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 checkSameDec("__divmod__", True)
45201
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1554 checkSameDec("__eq__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1555 checkSameDec("__ne__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1556 checkSameDec("__le__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1557 checkSameDec("__lt__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1558 checkSameDec("__ge__", True)
f55a4f119141 Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
Mark Dickinson <dickinsm@gmail.com>
parents: 45079
diff changeset
1559 checkSameDec("__gt__", True)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560 checkSameDec("__float__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 checkSameDec("__floordiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 checkSameDec("__hash__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 checkSameDec("__int__")
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1564 checkSameDec("__trunc__")
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 checkSameDec("__long__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 checkSameDec("__mod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1567 checkSameDec("__mul__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1568 checkSameDec("__neg__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 checkSameDec("__nonzero__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570 checkSameDec("__pos__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 checkSameDec("__pow__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 checkSameDec("__radd__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 checkSameDec("__rdiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574 checkSameDec("__rdivmod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 checkSameDec("__repr__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576 checkSameDec("__rfloordiv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 checkSameDec("__rmod__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578 checkSameDec("__rmul__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 checkSameDec("__rpow__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580 checkSameDec("__rsub__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 checkSameDec("__str__")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 checkSameDec("__sub__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 checkSameDec("__truediv__", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 checkSameDec("adjusted")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1585 checkSameDec("as_tuple")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586 checkSameDec("compare", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1587 checkSameDec("max", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588 checkSameDec("min", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1589 checkSameDec("normalize")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1590 checkSameDec("quantize", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1591 checkSameDec("remainder_near", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1592 checkSameDec("same_quantum", True)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1593 checkSameDec("sqrt")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1594 checkSameDec("to_eng_string")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1595 checkSameDec("to_integral")
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1596
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1597 def test_subclassing(self):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1598 # Different behaviours when subclassing Decimal
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1599
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1600 class MyDecimal(Decimal):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1601 pass
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1602
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1603 d1 = MyDecimal(1)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1604 d2 = MyDecimal(2)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1605 d = d1 + d2
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1606 self.assertIs(type(d), Decimal)
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1607
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1608 d = d1.max(d2)
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1609 self.assertIs(type(d), Decimal)
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1610
46083
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1611 def test_implicit_context(self):
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1612 # Check results when context given implicitly. (Issue 2478)
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1613 c = getcontext()
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1614 self.assertEqual(str(Decimal(0).sqrt()),
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1615 str(c.sqrt(Decimal(0))))
0b63999158b0 Decimal.sqrt(0) failed when the context was not
Mark Dickinson <dickinsm@gmail.com>
parents: 44603
diff changeset
1616
56971
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1617 def test_conversions_from_int(self):
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1618 # Check that methods taking a second Decimal argument will
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1619 # always accept an integer in place of a Decimal.
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1620 self.assertEqual(Decimal(4).compare(3),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1621 Decimal(4).compare(Decimal(3)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1622 self.assertEqual(Decimal(4).compare_signal(3),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1623 Decimal(4).compare_signal(Decimal(3)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1624 self.assertEqual(Decimal(4).compare_total(3),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1625 Decimal(4).compare_total(Decimal(3)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1626 self.assertEqual(Decimal(4).compare_total_mag(3),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1627 Decimal(4).compare_total_mag(Decimal(3)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1628 self.assertEqual(Decimal(10101).logical_and(1001),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1629 Decimal(10101).logical_and(Decimal(1001)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1630 self.assertEqual(Decimal(10101).logical_or(1001),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1631 Decimal(10101).logical_or(Decimal(1001)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1632 self.assertEqual(Decimal(10101).logical_xor(1001),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1633 Decimal(10101).logical_xor(Decimal(1001)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1634 self.assertEqual(Decimal(567).max(123),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1635 Decimal(567).max(Decimal(123)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1636 self.assertEqual(Decimal(567).max_mag(123),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1637 Decimal(567).max_mag(Decimal(123)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1638 self.assertEqual(Decimal(567).min(123),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1639 Decimal(567).min(Decimal(123)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1640 self.assertEqual(Decimal(567).min_mag(123),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1641 Decimal(567).min_mag(Decimal(123)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1642 self.assertEqual(Decimal(567).next_toward(123),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1643 Decimal(567).next_toward(Decimal(123)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1644 self.assertEqual(Decimal(1234).quantize(100),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1645 Decimal(1234).quantize(Decimal(100)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1646 self.assertEqual(Decimal(768).remainder_near(1234),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1647 Decimal(768).remainder_near(Decimal(1234)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1648 self.assertEqual(Decimal(123).rotate(1),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1649 Decimal(123).rotate(Decimal(1)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1650 self.assertEqual(Decimal(1234).same_quantum(1000),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1651 Decimal(1234).same_quantum(Decimal(1000)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1652 self.assertEqual(Decimal('9.123').scaleb(-100),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1653 Decimal('9.123').scaleb(Decimal(-100)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1654 self.assertEqual(Decimal(456).shift(-1),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1655 Decimal(456).shift(Decimal(-1)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1656
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1657 self.assertEqual(Decimal(-12).fma(Decimal(45), 67),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1658 Decimal(-12).fma(Decimal(45), Decimal(67)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1659 self.assertEqual(Decimal(-12).fma(45, 67),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1660 Decimal(-12).fma(Decimal(45), Decimal(67)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1661 self.assertEqual(Decimal(-12).fma(45, Decimal(67)),
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1662 Decimal(-12).fma(Decimal(45), Decimal(67)))
53e38005d507 Merged revisions 75943-75945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56838
diff changeset
1663
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
1664
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665 class DecimalPythonAPItests(unittest.TestCase):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666
51294
aea90f5f398b Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50786
diff changeset
1667 def test_abc(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1668 self.assertTrue(issubclass(Decimal, numbers.Number))
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1669 self.assertFalse(issubclass(Decimal, numbers.Real))
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
1670 self.assertIsInstance(Decimal(0), numbers.Number)
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
1671 self.assertNotIsInstance(Decimal(0), numbers.Real)
51294
aea90f5f398b Register decimals as numbers.Number
Raymond Hettinger <python@rcn.com>
parents: 50786
diff changeset
1672
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1673 def test_pickle(self):
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1674 d = Decimal('-3.141590000')
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1675 p = pickle.dumps(d)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1676 e = pickle.loads(p)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1677 self.assertEqual(d, e)
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1678
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1679 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
1680 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
1681 s = '%0.2f' % (x / 100.0)
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1682 # should work the same as for floats
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1683 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
1684 # 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
1685 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
1686 r = d.to_integral(ROUND_DOWN)
32500
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1687 self.assertEqual(Decimal(int(d)), r)
fbbd6727401b Test the logic for int(d).
Raymond Hettinger <python@rcn.com>
parents: 32481
diff changeset
1688
55886
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1689 self.assertRaises(ValueError, int, Decimal('-nan'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1690 self.assertRaises(ValueError, int, Decimal('snan'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1691 self.assertRaises(OverflowError, int, Decimal('inf'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1692 self.assertRaises(OverflowError, int, Decimal('-inf'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1693
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1694 self.assertRaises(ValueError, long, Decimal('-nan'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1695 self.assertRaises(ValueError, long, Decimal('snan'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1696 self.assertRaises(OverflowError, long, Decimal('inf'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1697 self.assertRaises(OverflowError, long, Decimal('-inf'))
fc9a00799795 Merged revisions 74708 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55874
diff changeset
1698
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1699 def test_trunc(self):
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1700 for x in range(-250, 250):
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1701 s = '%0.2f' % (x / 100.0)
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1702 # should work the same as for floats
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1703 self.assertEqual(int(Decimal(s)), int(float(s)))
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1704 # 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
1705 d = Decimal(s)
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1706 r = d.to_integral(ROUND_DOWN)
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44923
diff changeset
1707 self.assertEqual(Decimal(math.trunc(d)), r)
44923
e8efa62eec15 Add support for trunc().
Raymond Hettinger <python@rcn.com>
parents: 44650
diff changeset
1708
50496
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1709 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
1710
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1711 class MyDecimal(Decimal):
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1712 pass
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1713
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1714 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
1715 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
1716 self.assertEqual(str(r),
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1717 '0.1000000000000000055511151231257827021181583404541015625')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1718 bigint = 12345678901234567890123456789
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1719 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1720 self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1721 self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
1722 self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite())
50496
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1723 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
1724 str(Decimal('NaN')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1725 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
1726 str(Decimal('Infinity')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1727 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
1728 str(Decimal('-Infinity')))
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1729 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
1730 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
1731 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
1732 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
1733
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1734 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
1735 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
1736 self.assertEqual(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1737 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
1738 Decimal('3.1415')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1739 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1740 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
1741 self.assertEqual(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1742 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
1743 Decimal('3.1416')
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1744 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1745 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
1746 self.assertRaises(
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1747 Inexact,
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1748 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
1749 math.pi
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1750 )
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1751 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
1752 "Decimal('-0')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1753 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
1754 "Decimal('1')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1755 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
1756 "Decimal('10')")
7a6e3bc60ba6 Issue 4796: Add from_float methods to the decimal module.
Raymond Hettinger <python@rcn.com>
parents: 47872
diff changeset
1757
32477
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1758 class ContextAPItests(unittest.TestCase):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1759
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1760 def test_pickle(self):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1761 c = Context()
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1762 e = pickle.loads(pickle.dumps(c))
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1763 for k in vars(c):
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1764 v1 = vars(c)[k]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1765 v2 = vars(e)[k]
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1766 self.assertEqual(v1, v2)
bc586db9945a Work through several open todos:
Raymond Hettinger <python@rcn.com>
parents: 32451
diff changeset
1767
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1768 def test_equality_with_other_types(self):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1769 self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1770 self.assertNotIn(Decimal(10), ['a', 1.0, (1,2), {}])
32505
191796b7a054 * Add __eq__ and __ne__ so that things like list.index() work properly
Raymond Hettinger <python@rcn.com>
parents: 32500
diff changeset
1771
33052
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1772 def test_copy(self):
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1773 # All copies should be deep
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1774 c = Context()
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1775 d = c.copy()
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1776 self.assertNotEqual(id(c), id(d))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1777 self.assertNotEqual(id(c.flags), id(d.flags))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1778 self.assertNotEqual(id(c.traps), id(d.traps))
985375aca970 Add a test for Context.copy().
Raymond Hettinger <python@rcn.com>
parents: 32667
diff changeset
1779
58934
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1780 def test_abs(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1781 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1782 d = c.abs(Decimal(-1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1783 self.assertEqual(c.abs(-1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1784 self.assertRaises(TypeError, c.abs, '-1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1785
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1786 def test_add(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1787 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1788 d = c.add(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1789 self.assertEqual(c.add(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1790 self.assertEqual(c.add(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1791 self.assertEqual(c.add(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1792 self.assertRaises(TypeError, c.add, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1793 self.assertRaises(TypeError, c.add, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1794
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1795 def test_compare(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1796 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1797 d = c.compare(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1798 self.assertEqual(c.compare(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1799 self.assertEqual(c.compare(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1800 self.assertEqual(c.compare(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1801 self.assertRaises(TypeError, c.compare, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1802 self.assertRaises(TypeError, c.compare, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1803
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1804 def test_compare_signal(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1805 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1806 d = c.compare_signal(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1807 self.assertEqual(c.compare_signal(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1808 self.assertEqual(c.compare_signal(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1809 self.assertEqual(c.compare_signal(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1810 self.assertRaises(TypeError, c.compare_signal, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1811 self.assertRaises(TypeError, c.compare_signal, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1812
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1813 def test_compare_total(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1814 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1815 d = c.compare_total(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1816 self.assertEqual(c.compare_total(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1817 self.assertEqual(c.compare_total(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1818 self.assertEqual(c.compare_total(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1819 self.assertRaises(TypeError, c.compare_total, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1820 self.assertRaises(TypeError, c.compare_total, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1821
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1822 def test_compare_total_mag(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1823 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1824 d = c.compare_total_mag(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1825 self.assertEqual(c.compare_total_mag(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1826 self.assertEqual(c.compare_total_mag(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1827 self.assertEqual(c.compare_total_mag(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1828 self.assertRaises(TypeError, c.compare_total_mag, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1829 self.assertRaises(TypeError, c.compare_total_mag, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1830
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1831 def test_copy_abs(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1832 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1833 d = c.copy_abs(Decimal(-1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1834 self.assertEqual(c.copy_abs(-1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1835 self.assertRaises(TypeError, c.copy_abs, '-1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1836
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1837 def test_copy_decimal(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1838 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1839 d = c.copy_decimal(Decimal(-1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1840 self.assertEqual(c.copy_decimal(-1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1841 self.assertRaises(TypeError, c.copy_decimal, '-1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1842
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1843 def test_copy_negate(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1844 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1845 d = c.copy_negate(Decimal(-1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1846 self.assertEqual(c.copy_negate(-1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1847 self.assertRaises(TypeError, c.copy_negate, '-1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1848
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1849 def test_copy_sign(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1850 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1851 d = c.copy_sign(Decimal(1), Decimal(-2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1852 self.assertEqual(c.copy_sign(1, -2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1853 self.assertEqual(c.copy_sign(Decimal(1), -2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1854 self.assertEqual(c.copy_sign(1, Decimal(-2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1855 self.assertRaises(TypeError, c.copy_sign, '1', -2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1856 self.assertRaises(TypeError, c.copy_sign, 1, '-2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1857
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1858 def test_divide(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1859 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1860 d = c.divide(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1861 self.assertEqual(c.divide(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1862 self.assertEqual(c.divide(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1863 self.assertEqual(c.divide(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1864 self.assertRaises(TypeError, c.divide, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1865 self.assertRaises(TypeError, c.divide, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1866
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1867 def test_divide_int(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1868 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1869 d = c.divide_int(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1870 self.assertEqual(c.divide_int(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1871 self.assertEqual(c.divide_int(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1872 self.assertEqual(c.divide_int(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1873 self.assertRaises(TypeError, c.divide_int, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1874 self.assertRaises(TypeError, c.divide_int, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1875
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1876 def test_divmod(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1877 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1878 d = c.divmod(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1879 self.assertEqual(c.divmod(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1880 self.assertEqual(c.divmod(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1881 self.assertEqual(c.divmod(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1882 self.assertRaises(TypeError, c.divmod, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1883 self.assertRaises(TypeError, c.divmod, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1884
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1885 def test_exp(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1886 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1887 d = c.exp(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1888 self.assertEqual(c.exp(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1889 self.assertRaises(TypeError, c.exp, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1890
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1891 def test_fma(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1892 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1893 d = c.fma(Decimal(2), Decimal(3), Decimal(4))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1894 self.assertEqual(c.fma(2, 3, 4), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1895 self.assertEqual(c.fma(Decimal(2), 3, 4), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1896 self.assertEqual(c.fma(2, Decimal(3), 4), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1897 self.assertEqual(c.fma(2, 3, Decimal(4)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1898 self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1899 self.assertRaises(TypeError, c.fma, '2', 3, 4)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1900 self.assertRaises(TypeError, c.fma, 2, '3', 4)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1901 self.assertRaises(TypeError, c.fma, 2, 3, '4')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1902
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1903 def test_is_finite(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1904 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1905 d = c.is_finite(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1906 self.assertEqual(c.is_finite(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1907 self.assertRaises(TypeError, c.is_finite, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1908
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1909 def test_is_infinite(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1910 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1911 d = c.is_infinite(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1912 self.assertEqual(c.is_infinite(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1913 self.assertRaises(TypeError, c.is_infinite, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1914
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1915 def test_is_nan(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1916 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1917 d = c.is_nan(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1918 self.assertEqual(c.is_nan(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1919 self.assertRaises(TypeError, c.is_nan, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1920
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1921 def test_is_normal(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1922 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1923 d = c.is_normal(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1924 self.assertEqual(c.is_normal(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1925 self.assertRaises(TypeError, c.is_normal, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1926
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1927 def test_is_qnan(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1928 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1929 d = c.is_qnan(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1930 self.assertEqual(c.is_qnan(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1931 self.assertRaises(TypeError, c.is_qnan, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1932
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1933 def test_is_signed(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1934 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1935 d = c.is_signed(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1936 self.assertEqual(c.is_signed(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1937 self.assertRaises(TypeError, c.is_signed, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1938
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1939 def test_is_snan(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1940 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1941 d = c.is_snan(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1942 self.assertEqual(c.is_snan(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1943 self.assertRaises(TypeError, c.is_snan, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1944
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1945 def test_is_subnormal(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1946 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1947 d = c.is_subnormal(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1948 self.assertEqual(c.is_subnormal(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1949 self.assertRaises(TypeError, c.is_subnormal, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1950
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1951 def test_is_zero(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1952 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1953 d = c.is_zero(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1954 self.assertEqual(c.is_zero(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1955 self.assertRaises(TypeError, c.is_zero, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1956
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1957 def test_ln(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1958 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1959 d = c.ln(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1960 self.assertEqual(c.ln(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1961 self.assertRaises(TypeError, c.ln, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1962
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1963 def test_log10(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1964 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1965 d = c.log10(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1966 self.assertEqual(c.log10(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1967 self.assertRaises(TypeError, c.log10, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1968
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1969 def test_logb(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1970 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1971 d = c.logb(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1972 self.assertEqual(c.logb(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1973 self.assertRaises(TypeError, c.logb, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1974
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1975 def test_logical_and(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1976 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1977 d = c.logical_and(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1978 self.assertEqual(c.logical_and(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1979 self.assertEqual(c.logical_and(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1980 self.assertEqual(c.logical_and(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1981 self.assertRaises(TypeError, c.logical_and, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1982 self.assertRaises(TypeError, c.logical_and, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1983
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1984 def test_logical_invert(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1985 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1986 d = c.logical_invert(Decimal(1000))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1987 self.assertEqual(c.logical_invert(1000), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1988 self.assertRaises(TypeError, c.logical_invert, '1000')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1989
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1990 def test_logical_or(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1991 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1992 d = c.logical_or(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1993 self.assertEqual(c.logical_or(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1994 self.assertEqual(c.logical_or(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1995 self.assertEqual(c.logical_or(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1996 self.assertRaises(TypeError, c.logical_or, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1997 self.assertRaises(TypeError, c.logical_or, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1998
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
1999 def test_logical_xor(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2000 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2001 d = c.logical_xor(Decimal(1), Decimal(1))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2002 self.assertEqual(c.logical_xor(1, 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2003 self.assertEqual(c.logical_xor(Decimal(1), 1), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2004 self.assertEqual(c.logical_xor(1, Decimal(1)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2005 self.assertRaises(TypeError, c.logical_xor, '1', 1)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2006 self.assertRaises(TypeError, c.logical_xor, 1, '1')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2007
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2008 def test_max(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2009 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2010 d = c.max(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2011 self.assertEqual(c.max(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2012 self.assertEqual(c.max(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2013 self.assertEqual(c.max(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2014 self.assertRaises(TypeError, c.max, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2015 self.assertRaises(TypeError, c.max, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2016
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2017 def test_max_mag(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2018 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2019 d = c.max_mag(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2020 self.assertEqual(c.max_mag(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2021 self.assertEqual(c.max_mag(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2022 self.assertEqual(c.max_mag(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2023 self.assertRaises(TypeError, c.max_mag, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2024 self.assertRaises(TypeError, c.max_mag, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2025
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2026 def test_min(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2027 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2028 d = c.min(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2029 self.assertEqual(c.min(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2030 self.assertEqual(c.min(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2031 self.assertEqual(c.min(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2032 self.assertRaises(TypeError, c.min, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2033 self.assertRaises(TypeError, c.min, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2034
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2035 def test_min_mag(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2036 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2037 d = c.min_mag(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2038 self.assertEqual(c.min_mag(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2039 self.assertEqual(c.min_mag(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2040 self.assertEqual(c.min_mag(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2041 self.assertRaises(TypeError, c.min_mag, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2042 self.assertRaises(TypeError, c.min_mag, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2043
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2044 def test_minus(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2045 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2046 d = c.minus(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2047 self.assertEqual(c.minus(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2048 self.assertRaises(TypeError, c.minus, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2049
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2050 def test_multiply(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2051 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2052 d = c.multiply(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2053 self.assertEqual(c.multiply(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2054 self.assertEqual(c.multiply(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2055 self.assertEqual(c.multiply(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2056 self.assertRaises(TypeError, c.multiply, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2057 self.assertRaises(TypeError, c.multiply, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2058
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2059 def test_next_minus(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2060 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2061 d = c.next_minus(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2062 self.assertEqual(c.next_minus(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2063 self.assertRaises(TypeError, c.next_minus, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2064
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2065 def test_next_plus(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2066 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2067 d = c.next_plus(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2068 self.assertEqual(c.next_plus(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2069 self.assertRaises(TypeError, c.next_plus, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2070
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2071 def test_next_toward(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2072 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2073 d = c.next_toward(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2074 self.assertEqual(c.next_toward(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2075 self.assertEqual(c.next_toward(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2076 self.assertEqual(c.next_toward(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2077 self.assertRaises(TypeError, c.next_toward, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2078 self.assertRaises(TypeError, c.next_toward, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2079
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2080 def test_normalize(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2081 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2082 d = c.normalize(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2083 self.assertEqual(c.normalize(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2084 self.assertRaises(TypeError, c.normalize, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2085
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2086 def test_number_class(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2087 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2088 self.assertEqual(c.number_class(123), c.number_class(Decimal(123)))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2089 self.assertEqual(c.number_class(0), c.number_class(Decimal(0)))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2090 self.assertEqual(c.number_class(-45), c.number_class(Decimal(-45)))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2091
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2092 def test_power(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2093 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2094 d = c.power(Decimal(1), Decimal(4), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2095 self.assertEqual(c.power(1, 4, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2096 self.assertEqual(c.power(Decimal(1), 4, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2097 self.assertEqual(c.power(1, Decimal(4), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2098 self.assertEqual(c.power(1, 4, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2099 self.assertEqual(c.power(Decimal(1), Decimal(4), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2100 self.assertRaises(TypeError, c.power, '1', 4, 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2101 self.assertRaises(TypeError, c.power, 1, '4', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2102 self.assertRaises(TypeError, c.power, 1, 4, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2103
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2104 def test_plus(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2105 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2106 d = c.plus(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2107 self.assertEqual(c.plus(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2108 self.assertRaises(TypeError, c.plus, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2109
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2110 def test_quantize(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2111 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2112 d = c.quantize(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2113 self.assertEqual(c.quantize(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2114 self.assertEqual(c.quantize(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2115 self.assertEqual(c.quantize(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2116 self.assertRaises(TypeError, c.quantize, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2117 self.assertRaises(TypeError, c.quantize, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2118
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2119 def test_remainder(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2120 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2121 d = c.remainder(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2122 self.assertEqual(c.remainder(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2123 self.assertEqual(c.remainder(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2124 self.assertEqual(c.remainder(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2125 self.assertRaises(TypeError, c.remainder, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2126 self.assertRaises(TypeError, c.remainder, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2127
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2128 def test_remainder_near(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2129 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2130 d = c.remainder_near(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2131 self.assertEqual(c.remainder_near(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2132 self.assertEqual(c.remainder_near(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2133 self.assertEqual(c.remainder_near(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2134 self.assertRaises(TypeError, c.remainder_near, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2135 self.assertRaises(TypeError, c.remainder_near, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2136
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2137 def test_rotate(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2138 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2139 d = c.rotate(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2140 self.assertEqual(c.rotate(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2141 self.assertEqual(c.rotate(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2142 self.assertEqual(c.rotate(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2143 self.assertRaises(TypeError, c.rotate, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2144 self.assertRaises(TypeError, c.rotate, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2145
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2146 def test_sqrt(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2147 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2148 d = c.sqrt(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2149 self.assertEqual(c.sqrt(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2150 self.assertRaises(TypeError, c.sqrt, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2151
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2152 def test_same_quantum(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2153 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2154 d = c.same_quantum(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2155 self.assertEqual(c.same_quantum(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2156 self.assertEqual(c.same_quantum(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2157 self.assertEqual(c.same_quantum(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2158 self.assertRaises(TypeError, c.same_quantum, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2159 self.assertRaises(TypeError, c.same_quantum, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2160
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2161 def test_scaleb(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2162 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2163 d = c.scaleb(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2164 self.assertEqual(c.scaleb(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2165 self.assertEqual(c.scaleb(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2166 self.assertEqual(c.scaleb(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2167 self.assertRaises(TypeError, c.scaleb, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2168 self.assertRaises(TypeError, c.scaleb, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2169
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2170 def test_shift(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2171 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2172 d = c.shift(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2173 self.assertEqual(c.shift(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2174 self.assertEqual(c.shift(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2175 self.assertEqual(c.shift(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2176 self.assertRaises(TypeError, c.shift, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2177 self.assertRaises(TypeError, c.shift, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2178
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2179 def test_subtract(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2180 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2181 d = c.subtract(Decimal(1), Decimal(2))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2182 self.assertEqual(c.subtract(1, 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2183 self.assertEqual(c.subtract(Decimal(1), 2), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2184 self.assertEqual(c.subtract(1, Decimal(2)), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2185 self.assertRaises(TypeError, c.subtract, '1', 2)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2186 self.assertRaises(TypeError, c.subtract, 1, '2')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2187
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2188 def test_to_eng_string(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2189 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2190 d = c.to_eng_string(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2191 self.assertEqual(c.to_eng_string(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2192 self.assertRaises(TypeError, c.to_eng_string, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2193
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2194 def test_to_sci_string(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2195 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2196 d = c.to_sci_string(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2197 self.assertEqual(c.to_sci_string(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2198 self.assertRaises(TypeError, c.to_sci_string, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2199
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2200 def test_to_integral_exact(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2201 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2202 d = c.to_integral_exact(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2203 self.assertEqual(c.to_integral_exact(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2204 self.assertRaises(TypeError, c.to_integral_exact, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2205
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2206 def test_to_integral_value(self):
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2207 c = Context()
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2208 d = c.to_integral_value(Decimal(10))
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2209 self.assertEqual(c.to_integral_value(10), d)
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2210 self.assertRaises(TypeError, c.to_integral_value, '10')
e3d071233bef Issue #7633: Context method in the decimal module (with the exception
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
2211
39729
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2212 class WithStatementTest(unittest.TestCase):
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2213 # Can't do these as docstrings until Python 2.6
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2214 # as doctest can't handle __future__ statements
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2215
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2216 def test_localcontext(self):
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2217 # Use a copy of the current context in the block
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2218 orig_ctx = getcontext()
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2219 with localcontext() as enter_ctx:
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2220 set_ctx = getcontext()
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2221 final_ctx = getcontext()
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2222 self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly')
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2223 self.assertIsNot(orig_ctx, set_ctx, 'did not copy the context')
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2224 self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context')
39729
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2225
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2226 def test_localcontextarg(self):
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2227 # Use a copy of the supplied context in the block
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2228 orig_ctx = getcontext()
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2229 new_ctx = Context(prec=42)
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2230 with localcontext(new_ctx) as enter_ctx:
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2231 set_ctx = getcontext()
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2232 final_ctx = getcontext()
60242
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2233 self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly')
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2234 self.assertEqual(set_ctx.prec, new_ctx.prec, 'did not set correct context')
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2235 self.assertIsNot(new_ctx, set_ctx, 'did not copy the context')
a66545db3b06 Use more specific assert* methods in test_decimal.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60173
diff changeset
2236 self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context')
39729
4cec45bd6663 Backport of decimal module context management updates from rev 51694 to 2.5 release branch
Nick Coghlan <ncoghlan@gmail.com>
parents: 37550
diff changeset
2237
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2238 class ContextFlags(unittest.TestCase):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2239 def test_flags_irrelevant(self):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2240 # check that the result (numeric result + flags raised) of an
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2241 # arithmetic operation doesn't depend on the current flags
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2242
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2243 context = Context(prec=9, Emin = -999999999, Emax = 999999999,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2244 rounding=ROUND_HALF_EVEN, traps=[], flags=[])
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2245
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2246 # operations that raise various flags, in the form (function, arglist)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2247 operations = [
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2248 (context._apply, [Decimal("100E-1000000009")]),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2249 (context.sqrt, [Decimal(2)]),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2250 (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2251 (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2252 (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2253 ]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2254
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2255 # try various flags individually, then a whole lot at once
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2256 flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2257 [Inexact, Rounded, Underflow, Clamped, Subnormal]]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2258
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2259 for fn, args in operations:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2260 # find answer and flags raised using a clean context
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2261 context.clear_flags()
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2262 ans = fn(*args)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2263 flags = [k for k, v in context.flags.items() if v]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2264
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2265 for extra_flags in flagsets:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2266 # set flags, before calling operation
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2267 context.clear_flags()
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2268 for flag in extra_flags:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2269 context._raise_error(flag)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2270 new_ans = fn(*args)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2271
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2272 # flags that we expect to be set after the operation
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2273 expected_flags = list(flags)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2274 for flag in extra_flags:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2275 if flag not in expected_flags:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2276 expected_flags.append(flag)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2277
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2278 # flags we actually got
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2279 new_flags = [k for k,v in context.flags.items() if v]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2280
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2281 self.assertEqual(ans, new_ans,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2282 "operation produces different answers depending on flags set: " +
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2283 "expected %s, got %s." % (ans, new_ans))
59806
e823e27c225e Fix py3k warnings in test_decimal, using unittest.assertItemsEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58934
diff changeset
2284 self.assertItemsEqual(new_flags, expected_flags,
43360
63f3154c731d Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
Facundo Batista <facundobatista@gmail.com>
parents: 39721
diff changeset
2285 "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
2286 "expected %s, got %s" % (expected_flags, new_flags))
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2287
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2288 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
2289 """ Execute the tests.
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2290
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
2291 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
2292 is enabled in regrtest.py
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2293 """
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
2294
37550
0342c5f0f4ff Fix tests so they pass in -R mode
Neal Norwitz <nnorwitz@gmail.com>
parents: 37317
diff changeset
2295 init()
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2296 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
2297 TEST_ALL = arith or is_resource_enabled('decimal')
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2298 DEBUG = debug
33405
21c79340ded5 Change the strategy for coping with time intensive tests from
Raymond Hettinger <python@rcn.com>
parents: 33164
diff changeset
2299
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2300 if todo_tests is None:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2301 test_classes = [
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2302 DecimalExplicitConstructionTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2303 DecimalImplicitConstructionTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2304 DecimalArithmeticOperatorsTest,
45566
bce4c40df7df Add __format__ method to Decimal, to support PEP 3101
Mark Dickinson <dickinsm@gmail.com>
parents: 45321
diff changeset
2305 DecimalFormatTest,
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2306 DecimalUseOfContextTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2307 DecimalUsabilityTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2308 DecimalPythonAPItests,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2309 ContextAPItests,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2310 DecimalTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2311 WithStatementTest,
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2312 ContextFlags
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2313 ]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2314 else:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2315 test_classes = [DecimalTest]
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2316
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2317 # Dynamically build custom test definition for each file in the test
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2318 # directory and add the definitions to the DecimalTest class. This
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2319 # procedure insures that new files do not get skipped.
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2320 for filename in os.listdir(directory):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2321 if '.decTest' not in filename or filename.startswith("."):
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2322 continue
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2323 head, tail = filename.split('.')
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2324 if todo_tests is not None and head not in todo_tests:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2325 continue
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2326 tester = lambda self, f=filename: self.eval_file(directory + f)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2327 setattr(DecimalTest, 'test_' + head, tester)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2328 del filename, head, tail, tester
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2329
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2330
37317
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
2331 try:
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
2332 run_unittest(*test_classes)
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2333 if todo_tests is None:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2334 import decimal as DecimalModule
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2335 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
2336 finally:
f5e792a93c43 test_main(): Restore the decimal context that was in
Tim Peters <tim.peters@gmail.com>
parents: 37307
diff changeset
2337 setcontext(ORIGINAL_CONTEXT)
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2338
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2339 if __name__ == '__main__':
44600
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2340 import optparse
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2341 p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]")
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2342 p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test')
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2343 p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests')
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2344 (opt, args) = p.parse_args()
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2345
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2346 if opt.skip:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2347 test_main(arith=False, verbose=True)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2348 elif args:
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2349 test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
9a60bd8f00d6 Decimal module and test cases were updated to their state of
Facundo Batista <facundobatista@gmail.com>
parents: 39729
diff changeset
2350 else:
32451
d2c1c7ae7cf4 Move Decimal from the sandbox into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2351 test_main(arith=True, verbose=True)