Back to index

python3.2  3.2.2
Public Member Functions
test.test_decimal.DecimalArithmeticOperatorsTest Class Reference

List of all members.

Public Member Functions

def test_addition
def test_subtraction
def test_multiplication
def test_division
def test_floor_division
def test_powering
def test_module
def test_floor_div_module
def test_unary_operators
def test_nan_comparisons
def test_copy_sign

Detailed Description

Unit tests for all arithmetic operators, binary and unary.

Definition at line 906 of file test_decimal.py.


Member Function Documentation

Definition at line 909 of file test_decimal.py.

00909 
00910     def test_addition(self):
00911 
00912         d1 = Decimal('-11.1')
00913         d2 = Decimal('22.2')
00914 
00915         #two Decimals
00916         self.assertEqual(d1+d2, Decimal('11.1'))
00917         self.assertEqual(d2+d1, Decimal('11.1'))
00918 
00919         #with other type, left
00920         c = d1 + 5
00921         self.assertEqual(c, Decimal('-6.1'))
00922         self.assertEqual(type(c), type(d1))
00923 
00924         #with other type, right
00925         c = 5 + d1
00926         self.assertEqual(c, Decimal('-6.1'))
00927         self.assertEqual(type(c), type(d1))
00928 
00929         #inline with decimal
00930         d1 += d2
00931         self.assertEqual(d1, Decimal('11.1'))
00932 
00933         #inline with other type
00934         d1 += 5
00935         self.assertEqual(d1, Decimal('16.1'))

Here is the call graph for this function:

Definition at line 1181 of file test_decimal.py.

01181 
01182     def test_copy_sign(self):
01183         d = Decimal(1).copy_sign(Decimal(-2))
01184 
01185         self.assertEqual(Decimal(1).copy_sign(-2), d)
01186         self.assertRaises(TypeError, Decimal(1).copy_sign, '-2')
01187 
01188 # The following are two functions used to test threading in the next class

Here is the call graph for this function:

Definition at line 990 of file test_decimal.py.

00990 
00991     def test_division(self):
00992 
00993         d1 = Decimal('-5')
00994         d2 = Decimal('2')
00995 
00996         #two Decimals
00997         self.assertEqual(d1/d2, Decimal('-2.5'))
00998         self.assertEqual(d2/d1, Decimal('-0.4'))
00999 
01000         #with other type, left
01001         c = d1 / 4
01002         self.assertEqual(c, Decimal('-1.25'))
01003         self.assertEqual(type(c), type(d1))
01004 
01005         #with other type, right
01006         c = 4 / d1
01007         self.assertEqual(c, Decimal('-0.8'))
01008         self.assertEqual(type(c), type(d1))
01009 
01010         #inline with decimal
01011         d1 /= d2
01012         self.assertEqual(d1, Decimal('-2.5'))
01013 
01014         #inline with other type
01015         d1 /= 4
01016         self.assertEqual(d1, Decimal('-0.625'))

Here is the call graph for this function:

Definition at line 1098 of file test_decimal.py.

01098 
01099     def test_floor_div_module(self):
01100 
01101         d1 = Decimal('5')
01102         d2 = Decimal('2')
01103 
01104         #two Decimals
01105         (p, q) = divmod(d1, d2)
01106         self.assertEqual(p, Decimal('2'))
01107         self.assertEqual(q, Decimal('1'))
01108         self.assertEqual(type(p), type(d1))
01109         self.assertEqual(type(q), type(d1))
01110 
01111         #with other type, left
01112         (p, q) = divmod(d1, 4)
01113         self.assertEqual(p, Decimal('1'))
01114         self.assertEqual(q, Decimal('1'))
01115         self.assertEqual(type(p), type(d1))
01116         self.assertEqual(type(q), type(d1))
01117 
01118         #with other type, right
01119         (p, q) = divmod(7, d1)
01120         self.assertEqual(p, Decimal('1'))
01121         self.assertEqual(q, Decimal('2'))
01122         self.assertEqual(type(p), type(d1))
01123         self.assertEqual(type(q), type(d1))

Here is the call graph for this function:

Definition at line 1017 of file test_decimal.py.

01017 
01018     def test_floor_division(self):
01019 
01020         d1 = Decimal('5')
01021         d2 = Decimal('2')
01022 
01023         #two Decimals
01024         self.assertEqual(d1//d2, Decimal('2'))
01025         self.assertEqual(d2//d1, Decimal('0'))
01026 
01027         #with other type, left
01028         c = d1 // 4
01029         self.assertEqual(c, Decimal('1'))
01030         self.assertEqual(type(c), type(d1))
01031 
01032         #with other type, right
01033         c = 7 // d1
01034         self.assertEqual(c, Decimal('1'))
01035         self.assertEqual(type(c), type(d1))
01036 
01037         #inline with decimal
01038         d1 //= d2
01039         self.assertEqual(d1, Decimal('2'))
01040 
01041         #inline with other type
01042         d1 //= 2
01043         self.assertEqual(d1, Decimal('1'))

Here is the call graph for this function:

Definition at line 1071 of file test_decimal.py.

01071 
01072     def test_module(self):
01073 
01074         d1 = Decimal('5')
01075         d2 = Decimal('2')
01076 
01077         #two Decimals
01078         self.assertEqual(d1%d2, Decimal('1'))
01079         self.assertEqual(d2%d1, Decimal('2'))
01080 
01081         #with other type, left
01082         c = d1 % 4
01083         self.assertEqual(c, Decimal('1'))
01084         self.assertEqual(type(c), type(d1))
01085 
01086         #with other type, right
01087         c = 7 % d1
01088         self.assertEqual(c, Decimal('2'))
01089         self.assertEqual(type(c), type(d1))
01090 
01091         #inline with decimal
01092         d1 %= d2
01093         self.assertEqual(d1, Decimal('1'))
01094 
01095         #inline with other type
01096         d1 %= 4
01097         self.assertEqual(d1, Decimal('1'))

Here is the call graph for this function:

Definition at line 963 of file test_decimal.py.

00963 
00964     def test_multiplication(self):
00965 
00966         d1 = Decimal('-5')
00967         d2 = Decimal('3')
00968 
00969         #two Decimals
00970         self.assertEqual(d1*d2, Decimal('-15'))
00971         self.assertEqual(d2*d1, Decimal('-15'))
00972 
00973         #with other type, left
00974         c = d1 * 5
00975         self.assertEqual(c, Decimal('-25'))
00976         self.assertEqual(type(c), type(d1))
00977 
00978         #with other type, right
00979         c = 5 * d1
00980         self.assertEqual(c, Decimal('-25'))
00981         self.assertEqual(type(c), type(d1))
00982 
00983         #inline with decimal
00984         d1 *= d2
00985         self.assertEqual(d1, Decimal('-15'))
00986 
00987         #inline with other type
00988         d1 *= 5
00989         self.assertEqual(d1, Decimal('-75'))

Here is the call graph for this function:

Definition at line 1129 of file test_decimal.py.

01129 
01130     def test_nan_comparisons(self):
01131         # comparisons involving signaling nans signal InvalidOperation
01132 
01133         # order comparisons (<, <=, >, >=) involving only quiet nans
01134         # also signal InvalidOperation
01135 
01136         # equality comparisons (==, !=) involving only quiet nans
01137         # don't signal, but return False or True respectively.
01138 
01139         n = Decimal('NaN')
01140         s = Decimal('sNaN')
01141         i = Decimal('Inf')
01142         f = Decimal('2')
01143 
01144         qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n)
01145         snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s)
01146         order_ops = operator.lt, operator.le, operator.gt, operator.ge
01147         equality_ops = operator.eq, operator.ne
01148 
01149         # results when InvalidOperation is not trapped
01150         for x, y in qnan_pairs + snan_pairs:
01151             for op in order_ops + equality_ops:
01152                 got = op(x, y)
01153                 expected = True if op is operator.ne else False
01154                 self.assertIs(expected, got,
01155                               "expected {0!r} for operator.{1}({2!r}, {3!r}); "
01156                               "got {4!r}".format(
01157                         expected, op.__name__, x, y, got))
01158 
01159         # repeat the above, but this time trap the InvalidOperation
01160         with localcontext() as ctx:
01161             ctx.traps[InvalidOperation] = 1
01162 
01163             for x, y in qnan_pairs:
01164                 for op in equality_ops:
01165                     got = op(x, y)
01166                     expected = True if op is operator.ne else False
01167                     self.assertIs(expected, got,
01168                                   "expected {0!r} for "
01169                                   "operator.{1}({2!r}, {3!r}); "
01170                                   "got {4!r}".format(
01171                             expected, op.__name__, x, y, got))
01172 
01173             for x, y in snan_pairs:
01174                 for op in equality_ops:
01175                     self.assertRaises(InvalidOperation, operator.eq, x, y)
01176                     self.assertRaises(InvalidOperation, operator.ne, x, y)
01177 
01178             for x, y in qnan_pairs + snan_pairs:
01179                 for op in order_ops:
01180                     self.assertRaises(InvalidOperation, op, x, y)

Here is the call graph for this function:

Definition at line 1044 of file test_decimal.py.

01044 
01045     def test_powering(self):
01046 
01047         d1 = Decimal('5')
01048         d2 = Decimal('2')
01049 
01050         #two Decimals
01051         self.assertEqual(d1**d2, Decimal('25'))
01052         self.assertEqual(d2**d1, Decimal('32'))
01053 
01054         #with other type, left
01055         c = d1 ** 4
01056         self.assertEqual(c, Decimal('625'))
01057         self.assertEqual(type(c), type(d1))
01058 
01059         #with other type, right
01060         c = 7 ** d1
01061         self.assertEqual(c, Decimal('16807'))
01062         self.assertEqual(type(c), type(d1))
01063 
01064         #inline with decimal
01065         d1 **= d2
01066         self.assertEqual(d1, Decimal('25'))
01067 
01068         #inline with other type
01069         d1 **= 4
01070         self.assertEqual(d1, Decimal('390625'))

Here is the call graph for this function:

Definition at line 936 of file test_decimal.py.

00936 
00937     def test_subtraction(self):
00938 
00939         d1 = Decimal('-11.1')
00940         d2 = Decimal('22.2')
00941 
00942         #two Decimals
00943         self.assertEqual(d1-d2, Decimal('-33.3'))
00944         self.assertEqual(d2-d1, Decimal('33.3'))
00945 
00946         #with other type, left
00947         c = d1 - 5
00948         self.assertEqual(c, Decimal('-16.1'))
00949         self.assertEqual(type(c), type(d1))
00950 
00951         #with other type, right
00952         c = 5 - d1
00953         self.assertEqual(c, Decimal('16.1'))
00954         self.assertEqual(type(c), type(d1))
00955 
00956         #inline with decimal
00957         d1 -= d2
00958         self.assertEqual(d1, Decimal('-33.3'))
00959 
00960         #inline with other type
00961         d1 -= 5
00962         self.assertEqual(d1, Decimal('-38.3'))

Here is the call graph for this function:

Definition at line 1124 of file test_decimal.py.

01124 
01125     def test_unary_operators(self):
01126         self.assertEqual(+Decimal(45), Decimal(+45))           #  +
01127         self.assertEqual(-Decimal(45), Decimal(-45))           #  -
01128         self.assertEqual(abs(Decimal(45)), abs(Decimal(-45)))  # abs

Here is the call graph for this function:


The documentation for this class was generated from the following file: