Back to index

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

List of all members.

Public Member Functions

def test_comparison_operators
def test_decimal_float_comparison
def test_copy_and_deepcopy_methods
def test_hash_method
def test_min_and_max_methods
def test_as_nonzero
def test_tostring_methods
def test_tonum_methods
def test_eval_round_trip
def test_as_tuple
def test_immutability_operations
def test_subclassing
def test_implicit_context
def test_conversions_from_int

Detailed Description

Unit tests for Usability cases of Decimal.

Definition at line 1248 of file test_decimal.py.


Member Function Documentation

Definition at line 1406 of file test_decimal.py.

01406 
01407     def test_as_nonzero(self):
01408         #as false
01409         self.assertFalse(Decimal(0))
01410         #as true
01411         self.assertTrue(Decimal('0.372'))

Here is the call graph for this function:

Definition at line 1539 of file test_decimal.py.

01539 
01540     def test_as_tuple(self):
01541 
01542         #with zero
01543         d = Decimal(0)
01544         self.assertEqual(d.as_tuple(), (0, (0,), 0) )
01545 
01546         #int
01547         d = Decimal(-45)
01548         self.assertEqual(d.as_tuple(), (1, (4, 5), 0) )
01549 
01550         #complicated string
01551         d = Decimal("-4.34913534E-17")
01552         self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
01553 
01554         #inf
01555         d = Decimal("Infinity")
01556         self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
01557 
01558         #leading zeros in coefficient should be stripped
01559         d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
01560         self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) )
01561         d = Decimal( (1, (0, 0, 0), 37) )
01562         self.assertEqual(d.as_tuple(), (1, (0,), 37))
01563         d = Decimal( (1, (), 37) )
01564         self.assertEqual(d.as_tuple(), (1, (0,), 37))
01565 
01566         #leading zeros in NaN diagnostic info should be stripped
01567         d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
01568         self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') )
01569         d = Decimal( (1, (0, 0, 0), 'N') )
01570         self.assertEqual(d.as_tuple(), (1, (), 'N') )
01571         d = Decimal( (1, (), 'n') )
01572         self.assertEqual(d.as_tuple(), (1, (), 'n') )
01573 
01574         #coefficient in infinity should be ignored
01575         d = Decimal( (0, (4, 5, 3, 4), 'F') )
01576         self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
01577         d = Decimal( (1, (0, 2, 7, 1), 'F') )
01578         self.assertEqual(d.as_tuple(), (1, (0,), 'F'))

Here is the call graph for this function:

Definition at line 1251 of file test_decimal.py.

01251 
01252     def test_comparison_operators(self):
01253 
01254         da = Decimal('23.42')
01255         db = Decimal('23.42')
01256         dc = Decimal('45')
01257 
01258         #two Decimals
01259         self.assertGreater(dc, da)
01260         self.assertGreaterEqual(dc, da)
01261         self.assertLess(da, dc)
01262         self.assertLessEqual(da, dc)
01263         self.assertEqual(da, db)
01264         self.assertNotEqual(da, dc)
01265         self.assertLessEqual(da, db)
01266         self.assertGreaterEqual(da, db)
01267 
01268         #a Decimal and an int
01269         self.assertGreater(dc, 23)
01270         self.assertLess(23, dc)
01271         self.assertEqual(dc, 45)
01272 
01273         #a Decimal and uncomparable
01274         self.assertNotEqual(da, 'ugly')
01275         self.assertNotEqual(da, 32.7)
01276         self.assertNotEqual(da, object())
01277         self.assertNotEqual(da, object)
01278 
01279         # sortable
01280         a = list(map(Decimal, range(100)))
01281         b =  a[:]
01282         random.shuffle(a)
01283         a.sort()
01284         self.assertEqual(a, b)

Here is the call graph for this function:

Definition at line 1672 of file test_decimal.py.

01672 
01673     def test_conversions_from_int(self):
01674         # Check that methods taking a second Decimal argument will
01675         # always accept an integer in place of a Decimal.
01676         self.assertEqual(Decimal(4).compare(3),
01677                          Decimal(4).compare(Decimal(3)))
01678         self.assertEqual(Decimal(4).compare_signal(3),
01679                          Decimal(4).compare_signal(Decimal(3)))
01680         self.assertEqual(Decimal(4).compare_total(3),
01681                          Decimal(4).compare_total(Decimal(3)))
01682         self.assertEqual(Decimal(4).compare_total_mag(3),
01683                          Decimal(4).compare_total_mag(Decimal(3)))
01684         self.assertEqual(Decimal(10101).logical_and(1001),
01685                          Decimal(10101).logical_and(Decimal(1001)))
01686         self.assertEqual(Decimal(10101).logical_or(1001),
01687                          Decimal(10101).logical_or(Decimal(1001)))
01688         self.assertEqual(Decimal(10101).logical_xor(1001),
01689                          Decimal(10101).logical_xor(Decimal(1001)))
01690         self.assertEqual(Decimal(567).max(123),
01691                          Decimal(567).max(Decimal(123)))
01692         self.assertEqual(Decimal(567).max_mag(123),
01693                          Decimal(567).max_mag(Decimal(123)))
01694         self.assertEqual(Decimal(567).min(123),
01695                          Decimal(567).min(Decimal(123)))
01696         self.assertEqual(Decimal(567).min_mag(123),
01697                          Decimal(567).min_mag(Decimal(123)))
01698         self.assertEqual(Decimal(567).next_toward(123),
01699                          Decimal(567).next_toward(Decimal(123)))
01700         self.assertEqual(Decimal(1234).quantize(100),
01701                          Decimal(1234).quantize(Decimal(100)))
01702         self.assertEqual(Decimal(768).remainder_near(1234),
01703                          Decimal(768).remainder_near(Decimal(1234)))
01704         self.assertEqual(Decimal(123).rotate(1),
01705                          Decimal(123).rotate(Decimal(1)))
01706         self.assertEqual(Decimal(1234).same_quantum(1000),
01707                          Decimal(1234).same_quantum(Decimal(1000)))
01708         self.assertEqual(Decimal('9.123').scaleb(-100),
01709                          Decimal('9.123').scaleb(Decimal(-100)))
01710         self.assertEqual(Decimal(456).shift(-1),
01711                          Decimal(456).shift(Decimal(-1)))
01712 
01713         self.assertEqual(Decimal(-12).fma(Decimal(45), 67),
01714                          Decimal(-12).fma(Decimal(45), Decimal(67)))
01715         self.assertEqual(Decimal(-12).fma(45, 67),
01716                          Decimal(-12).fma(Decimal(45), Decimal(67)))
01717         self.assertEqual(Decimal(-12).fma(45, Decimal(67)),
01718                          Decimal(-12).fma(Decimal(45), Decimal(67)))
01719 

Here is the call graph for this function:

Definition at line 1302 of file test_decimal.py.

01302 
01303     def test_copy_and_deepcopy_methods(self):
01304         d = Decimal('43.24')
01305         c = copy.copy(d)
01306         self.assertEqual(id(c), id(d))
01307         dc = copy.deepcopy(d)
01308         self.assertEqual(id(dc), id(d))

Here is the call graph for this function:

Definition at line 1285 of file test_decimal.py.

01285 
01286     def test_decimal_float_comparison(self):
01287         da = Decimal('0.25')
01288         db = Decimal('3.0')
01289         self.assertLess(da, 3.0)
01290         self.assertLessEqual(da, 3.0)
01291         self.assertGreater(db, 0.25)
01292         self.assertGreaterEqual(db, 0.25)
01293         self.assertNotEqual(da, 1.5)
01294         self.assertEqual(da, 0.25)
01295         self.assertGreater(3.0, da)
01296         self.assertGreaterEqual(3.0, da)
01297         self.assertLess(0.25, db)
01298         self.assertLessEqual(0.25, db)
01299         self.assertNotEqual(0.25, db)
01300         self.assertEqual(3.0, db)
01301         self.assertNotEqual(0.1, Decimal('0.1'))

Here is the call graph for this function:

Definition at line 1521 of file test_decimal.py.

01521 
01522     def test_eval_round_trip(self):
01523 
01524         #with zero
01525         d = Decimal( (0, (0,), 0) )
01526         self.assertEqual(d, eval(repr(d)))
01527 
01528         #int
01529         d = Decimal( (1, (4, 5), 0) )
01530         self.assertEqual(d, eval(repr(d)))
01531 
01532         #float
01533         d = Decimal( (0, (4, 5, 3, 4), -2) )
01534         self.assertEqual(d, eval(repr(d)))
01535 
01536         #weird
01537         d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
01538         self.assertEqual(d, eval(repr(d)))

Here is the call graph for this function:

Definition at line 1309 of file test_decimal.py.

01309 
01310     def test_hash_method(self):
01311         def hashit(d):
01312             a = hash(d)
01313             b = d.__hash__()
01314             self.assertEqual(a, b)
01315             return a
01316 
01317         #just that it's hashable
01318         hashit(Decimal(23))
01319         hashit(Decimal('Infinity'))
01320         hashit(Decimal('-Infinity'))
01321         hashit(Decimal('nan123'))
01322         hashit(Decimal('-NaN'))
01323 
01324         test_values = [Decimal(sign*(2**m + n))
01325                        for m in [0, 14, 15, 16, 17, 30, 31,
01326                                  32, 33, 61, 62, 63, 64, 65, 66]
01327                        for n in range(-10, 10)
01328                        for sign in [-1, 1]]
01329         test_values.extend([
01330                 Decimal("-1"), # ==> -2
01331                 Decimal("-0"), # zeros
01332                 Decimal("0.00"),
01333                 Decimal("-0.000"),
01334                 Decimal("0E10"),
01335                 Decimal("-0E12"),
01336                 Decimal("10.0"), # negative exponent
01337                 Decimal("-23.00000"),
01338                 Decimal("1230E100"), # positive exponent
01339                 Decimal("-4.5678E50"),
01340                 # a value for which hash(n) != hash(n % (2**64-1))
01341                 # in Python pre-2.6
01342                 Decimal(2**64 + 2**32 - 1),
01343                 # selection of values which fail with the old (before
01344                 # version 2.6) long.__hash__
01345                 Decimal("1.634E100"),
01346                 Decimal("90.697E100"),
01347                 Decimal("188.83E100"),
01348                 Decimal("1652.9E100"),
01349                 Decimal("56531E100"),
01350                 ])
01351 
01352         # check that hash(d) == hash(int(d)) for integral values
01353         for value in test_values:
01354             self.assertEqual(hashit(value), hashit(int(value)))
01355 
01356         #the same hash that to an int
01357         self.assertEqual(hashit(Decimal(23)), hashit(23))
01358         self.assertRaises(TypeError, hash, Decimal('sNaN'))
01359         self.assertTrue(hashit(Decimal('Inf')))
01360         self.assertTrue(hashit(Decimal('-Inf')))
01361 
01362         # check that the hashes of a Decimal float match when they
01363         # represent exactly the same values
01364         test_strings = ['inf', '-Inf', '0.0', '-.0e1',
01365                         '34.0', '2.5', '112390.625', '-0.515625']
01366         for s in test_strings:
01367             f = float(s)
01368             d = Decimal(s)
01369             self.assertEqual(hashit(f), hashit(d))
01370 
01371         # check that the value of the hash doesn't depend on the
01372         # current context (issue #1757)
01373         c = getcontext()
01374         old_precision = c.prec
01375         x = Decimal("123456789.1")
01376 
01377         c.prec = 6
01378         h1 = hashit(x)
01379         c.prec = 10
01380         h2 = hashit(x)
01381         c.prec = 16
01382         h3 = hashit(x)
01383 
01384         self.assertEqual(h1, h2)
01385         self.assertEqual(h1, h3)
01386         c.prec = old_precision

Here is the call graph for this function:

Definition at line 1579 of file test_decimal.py.

01579 
01580     def test_immutability_operations(self):
01581         # Do operations and check that it didn't change change internal objects.
01582 
01583         d1 = Decimal('-25e55')
01584         b1 = Decimal('-25e55')
01585         d2 = Decimal('33e+33')
01586         b2 = Decimal('33e+33')
01587 
01588         def checkSameDec(operation, useOther=False):
01589             if useOther:
01590                 eval("d1." + operation + "(d2)")
01591                 self.assertEqual(d1._sign, b1._sign)
01592                 self.assertEqual(d1._int, b1._int)
01593                 self.assertEqual(d1._exp, b1._exp)
01594                 self.assertEqual(d2._sign, b2._sign)
01595                 self.assertEqual(d2._int, b2._int)
01596                 self.assertEqual(d2._exp, b2._exp)
01597             else:
01598                 eval("d1." + operation + "()")
01599                 self.assertEqual(d1._sign, b1._sign)
01600                 self.assertEqual(d1._int, b1._int)
01601                 self.assertEqual(d1._exp, b1._exp)
01602             return
01603 
01604         Decimal(d1)
01605         self.assertEqual(d1._sign, b1._sign)
01606         self.assertEqual(d1._int, b1._int)
01607         self.assertEqual(d1._exp, b1._exp)
01608 
01609         checkSameDec("__abs__")
01610         checkSameDec("__add__", True)
01611         checkSameDec("__divmod__", True)
01612         checkSameDec("__eq__", True)
01613         checkSameDec("__ne__", True)
01614         checkSameDec("__le__", True)
01615         checkSameDec("__lt__", True)
01616         checkSameDec("__ge__", True)
01617         checkSameDec("__gt__", True)
01618         checkSameDec("__float__")
01619         checkSameDec("__floordiv__", True)
01620         checkSameDec("__hash__")
01621         checkSameDec("__int__")
01622         checkSameDec("__trunc__")
01623         checkSameDec("__mod__", True)
01624         checkSameDec("__mul__", True)
01625         checkSameDec("__neg__")
01626         checkSameDec("__bool__")
01627         checkSameDec("__pos__")
01628         checkSameDec("__pow__", True)
01629         checkSameDec("__radd__", True)
01630         checkSameDec("__rdivmod__", True)
01631         checkSameDec("__repr__")
01632         checkSameDec("__rfloordiv__", True)
01633         checkSameDec("__rmod__", True)
01634         checkSameDec("__rmul__", True)
01635         checkSameDec("__rpow__", True)
01636         checkSameDec("__rsub__", True)
01637         checkSameDec("__str__")
01638         checkSameDec("__sub__", True)
01639         checkSameDec("__truediv__", True)
01640         checkSameDec("adjusted")
01641         checkSameDec("as_tuple")
01642         checkSameDec("compare", True)
01643         checkSameDec("max", True)
01644         checkSameDec("min", True)
01645         checkSameDec("normalize")
01646         checkSameDec("quantize", True)
01647         checkSameDec("remainder_near", True)
01648         checkSameDec("same_quantum", True)
01649         checkSameDec("sqrt")
01650         checkSameDec("to_eng_string")
01651         checkSameDec("to_integral")

Here is the call graph for this function:

Definition at line 1666 of file test_decimal.py.

01666 
01667     def test_implicit_context(self):
01668         # Check results when context given implicitly.  (Issue 2478)
01669         c = getcontext()
01670         self.assertEqual(str(Decimal(0).sqrt()),
01671                          str(c.sqrt(Decimal(0))))

Here is the call graph for this function:

Definition at line 1387 of file test_decimal.py.

01387 
01388     def test_min_and_max_methods(self):
01389 
01390         d1 = Decimal('15.32')
01391         d2 = Decimal('28.5')
01392         l1 = 15
01393         l2 = 28
01394 
01395         #between Decimals
01396         self.assertIs(min(d1,d2), d1)
01397         self.assertIs(min(d2,d1), d1)
01398         self.assertIs(max(d1,d2), d2)
01399         self.assertIs(max(d2,d1), d2)
01400 
01401         #between Decimal and long
01402         self.assertIs(min(d1,l2), d1)
01403         self.assertIs(min(l2,d1), d1)
01404         self.assertIs(max(l1,d2), d2)
01405         self.assertIs(max(d2,l1), d2)

Here is the call graph for this function:

Definition at line 1652 of file test_decimal.py.

01652 
01653     def test_subclassing(self):
01654         # Different behaviours when subclassing Decimal
01655 
01656         class MyDecimal(Decimal):
01657             pass
01658 
01659         d1 = MyDecimal(1)
01660         d2 = MyDecimal(2)
01661         d = d1 + d2
01662         self.assertIs(type(d), Decimal)
01663 
01664         d = d1.max(d2)
01665         self.assertIs(type(d), Decimal)

Definition at line 1419 of file test_decimal.py.

01419 
01420     def test_tonum_methods(self):
01421         #Test float and int methods.
01422 
01423         d1 = Decimal('66')
01424         d2 = Decimal('15.32')
01425 
01426         #int
01427         self.assertEqual(int(d1), 66)
01428         self.assertEqual(int(d2), 15)
01429 
01430         #float
01431         self.assertEqual(float(d1), 66)
01432         self.assertEqual(float(d2), 15.32)
01433 
01434         #floor
01435         test_pairs = [
01436             ('123.00', 123),
01437             ('3.2', 3),
01438             ('3.54', 3),
01439             ('3.899', 3),
01440             ('-2.3', -3),
01441             ('-11.0', -11),
01442             ('0.0', 0),
01443             ('-0E3', 0),
01444             ]
01445         for d, i in test_pairs:
01446             self.assertEqual(math.floor(Decimal(d)), i)
01447         self.assertRaises(ValueError, math.floor, Decimal('-NaN'))
01448         self.assertRaises(ValueError, math.floor, Decimal('sNaN'))
01449         self.assertRaises(ValueError, math.floor, Decimal('NaN123'))
01450         self.assertRaises(OverflowError, math.floor, Decimal('Inf'))
01451         self.assertRaises(OverflowError, math.floor, Decimal('-Inf'))
01452 
01453         #ceiling
01454         test_pairs = [
01455             ('123.00', 123),
01456             ('3.2', 4),
01457             ('3.54', 4),
01458             ('3.899', 4),
01459             ('-2.3', -2),
01460             ('-11.0', -11),
01461             ('0.0', 0),
01462             ('-0E3', 0),
01463             ]
01464         for d, i in test_pairs:
01465             self.assertEqual(math.ceil(Decimal(d)), i)
01466         self.assertRaises(ValueError, math.ceil, Decimal('-NaN'))
01467         self.assertRaises(ValueError, math.ceil, Decimal('sNaN'))
01468         self.assertRaises(ValueError, math.ceil, Decimal('NaN123'))
01469         self.assertRaises(OverflowError, math.ceil, Decimal('Inf'))
01470         self.assertRaises(OverflowError, math.ceil, Decimal('-Inf'))
01471 
01472         #round, single argument
01473         test_pairs = [
01474             ('123.00', 123),
01475             ('3.2', 3),
01476             ('3.54', 4),
01477             ('3.899', 4),
01478             ('-2.3', -2),
01479             ('-11.0', -11),
01480             ('0.0', 0),
01481             ('-0E3', 0),
01482             ('-3.5', -4),
01483             ('-2.5', -2),
01484             ('-1.5', -2),
01485             ('-0.5', 0),
01486             ('0.5', 0),
01487             ('1.5', 2),
01488             ('2.5', 2),
01489             ('3.5', 4),
01490             ]
01491         for d, i in test_pairs:
01492             self.assertEqual(round(Decimal(d)), i)
01493         self.assertRaises(ValueError, round, Decimal('-NaN'))
01494         self.assertRaises(ValueError, round, Decimal('sNaN'))
01495         self.assertRaises(ValueError, round, Decimal('NaN123'))
01496         self.assertRaises(OverflowError, round, Decimal('Inf'))
01497         self.assertRaises(OverflowError, round, Decimal('-Inf'))
01498 
01499         #round, two arguments;  this is essentially equivalent
01500         #to quantize, which is already extensively tested
01501         test_triples = [
01502             ('123.456', -4, '0E+4'),
01503             ('123.456', -3, '0E+3'),
01504             ('123.456', -2, '1E+2'),
01505             ('123.456', -1, '1.2E+2'),
01506             ('123.456', 0, '123'),
01507             ('123.456', 1, '123.5'),
01508             ('123.456', 2, '123.46'),
01509             ('123.456', 3, '123.456'),
01510             ('123.456', 4, '123.4560'),
01511             ('123.455', 2, '123.46'),
01512             ('123.445', 2, '123.44'),
01513             ('Inf', 4, 'NaN'),
01514             ('-Inf', -23, 'NaN'),
01515             ('sNaN314', 3, 'NaN314'),
01516             ]
01517         for d, n, r in test_triples:
01518             self.assertEqual(str(round(Decimal(d), n)), r)
01519 
01520 

Here is the call graph for this function:

Definition at line 1412 of file test_decimal.py.

01412 
01413     def test_tostring_methods(self):
01414         #Test str and repr methods.
01415 
01416         d = Decimal('15.32')
01417         self.assertEqual(str(d), '15.32')               # str
01418         self.assertEqual(repr(d), "Decimal('15.32')")   # repr

Here is the call graph for this function:


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