Back to index

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

List of all members.

Public Member Functions

def test_abc
def test_pickle
def test_int
def test_trunc
def test_from_float
def test_create_decimal_from_float

Detailed Description

Definition at line 1720 of file test_decimal.py.


Member Function Documentation

Definition at line 1722 of file test_decimal.py.

01722 
01723     def test_abc(self):
01724         self.assertTrue(issubclass(Decimal, numbers.Number))
01725         self.assertFalse(issubclass(Decimal, numbers.Real))
01726         self.assertIsInstance(Decimal(0), numbers.Number)
01727         self.assertNotIsInstance(Decimal(0), numbers.Real)

Here is the call graph for this function:

Definition at line 1784 of file test_decimal.py.

01784 
01785     def test_create_decimal_from_float(self):
01786         context = Context(prec=5, rounding=ROUND_DOWN)
01787         self.assertEqual(
01788             context.create_decimal_from_float(math.pi),
01789             Decimal('3.1415')
01790         )
01791         context = Context(prec=5, rounding=ROUND_UP)
01792         self.assertEqual(
01793             context.create_decimal_from_float(math.pi),
01794             Decimal('3.1416')
01795         )
01796         context = Context(prec=5, traps=[Inexact])
01797         self.assertRaises(
01798             Inexact,
01799             context.create_decimal_from_float,
01800             math.pi
01801         )
01802         self.assertEqual(repr(context.create_decimal_from_float(-0.0)),
01803                          "Decimal('-0')")
01804         self.assertEqual(repr(context.create_decimal_from_float(1.0)),
01805                          "Decimal('1')")
01806         self.assertEqual(repr(context.create_decimal_from_float(10)),
01807                          "Decimal('10')")

Here is the call graph for this function:

Definition at line 1759 of file test_decimal.py.

01759 
01760     def test_from_float(self):
01761 
01762         class  MyDecimal(Decimal):
01763             pass
01764 
01765         r = MyDecimal.from_float(0.1)
01766         self.assertEqual(type(r), MyDecimal)
01767         self.assertEqual(str(r),
01768                 '0.1000000000000000055511151231257827021181583404541015625')
01769         bigint = 12345678901234567890123456789
01770         self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
01771         self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
01772         self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite())
01773         self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite())
01774         self.assertEqual(str(MyDecimal.from_float(float('nan'))),
01775                          str(Decimal('NaN')))
01776         self.assertEqual(str(MyDecimal.from_float(float('inf'))),
01777                          str(Decimal('Infinity')))
01778         self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
01779                          str(Decimal('-Infinity')))
01780         self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
01781         for i in range(200):
01782             x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
01783             self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip

Definition at line 1734 of file test_decimal.py.

01734 
01735     def test_int(self):
01736         for x in range(-250, 250):
01737             s = '%0.2f' % (x / 100.0)
01738             # should work the same as for floats
01739             self.assertEqual(int(Decimal(s)), int(float(s)))
01740             # should work the same as to_integral in the ROUND_DOWN mode
01741             d = Decimal(s)
01742             r = d.to_integral(ROUND_DOWN)
01743             self.assertEqual(Decimal(int(d)), r)
01744 
01745         self.assertRaises(ValueError, int, Decimal('-nan'))
01746         self.assertRaises(ValueError, int, Decimal('snan'))
01747         self.assertRaises(OverflowError, int, Decimal('inf'))
01748         self.assertRaises(OverflowError, int, Decimal('-inf'))

Here is the call graph for this function:

Definition at line 1728 of file test_decimal.py.

01728 
01729     def test_pickle(self):
01730         d = Decimal('-3.141590000')
01731         p = pickle.dumps(d)
01732         e = pickle.loads(p)
01733         self.assertEqual(d, e)

Here is the call graph for this function:

Definition at line 1749 of file test_decimal.py.

01749 
01750     def test_trunc(self):
01751         for x in range(-250, 250):
01752             s = '%0.2f' % (x / 100.0)
01753             # should work the same as for floats
01754             self.assertEqual(int(Decimal(s)), int(float(s)))
01755             # should work the same as to_integral in the ROUND_DOWN mode
01756             d = Decimal(s)
01757             r = d.to_integral(ROUND_DOWN)
01758             self.assertEqual(Decimal(math.trunc(d)), r)

Here is the call graph for this function:


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