Back to index

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

List of all members.

Public Member Functions

def test_explicit_empty
def test_explicit_from_None
def test_explicit_from_int
def test_explicit_from_string
def test_explicit_from_tuples
def test_explicit_from_bool
def test_explicit_from_Decimal
def test_explicit_from_float
def test_explicit_context_create_decimal
def test_unicode_digits

Detailed Description

Unit tests for Explicit Construction cases of Decimal.

Definition at line 414 of file test_decimal.py.


Member Function Documentation

Definition at line 552 of file test_decimal.py.

00552 
00553     def test_explicit_context_create_decimal(self):
00554 
00555         nc = copy.copy(getcontext())
00556         nc.prec = 3
00557 
00558         # empty
00559         d = Decimal()
00560         self.assertEqual(str(d), '0')
00561         d = nc.create_decimal()
00562         self.assertEqual(str(d), '0')
00563 
00564         # from None
00565         self.assertRaises(TypeError, nc.create_decimal, None)
00566 
00567         # from int
00568         d = nc.create_decimal(456)
00569         self.assertIsInstance(d, Decimal)
00570         self.assertEqual(nc.create_decimal(45678),
00571                          nc.create_decimal('457E+2'))
00572 
00573         # from string
00574         d = Decimal('456789')
00575         self.assertEqual(str(d), '456789')
00576         d = nc.create_decimal('456789')
00577         self.assertEqual(str(d), '4.57E+5')
00578         # leading and trailing whitespace should result in a NaN;
00579         # spaces are already checked in Cowlishaw's test-suite, so
00580         # here we just check that a trailing newline results in a NaN
00581         self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN')
00582 
00583         # from tuples
00584         d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
00585         self.assertEqual(str(d), '-4.34913534E-17')
00586         d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
00587         self.assertEqual(str(d), '-4.35E-17')
00588 
00589         # from Decimal
00590         prevdec = Decimal(500000123)
00591         d = Decimal(prevdec)
00592         self.assertEqual(str(d), '500000123')
00593         d = nc.create_decimal(prevdec)
00594         self.assertEqual(str(d), '5.00E+8')

Here is the call graph for this function:

Definition at line 417 of file test_decimal.py.

00417 
00418     def test_explicit_empty(self):
00419         self.assertEqual(Decimal(), Decimal("0"))

Here is the call graph for this function:

Definition at line 499 of file test_decimal.py.

00499 
00500     def test_explicit_from_bool(self):
00501         self.assertIs(bool(Decimal(0)), False)
00502         self.assertIs(bool(Decimal(1)), True)
00503         self.assertEqual(Decimal(False), Decimal(0))
00504         self.assertEqual(Decimal(True), Decimal(1))

Here is the call graph for this function:

Definition at line 505 of file test_decimal.py.

00505 
00506     def test_explicit_from_Decimal(self):
00507 
00508         #positive
00509         d = Decimal(45)
00510         e = Decimal(d)
00511         self.assertEqual(str(e), '45')
00512         self.assertNotEqual(id(d), id(e))
00513 
00514         #very large positive
00515         d = Decimal(500000123)
00516         e = Decimal(d)
00517         self.assertEqual(str(e), '500000123')
00518         self.assertNotEqual(id(d), id(e))
00519 
00520         #negative
00521         d = Decimal(-45)
00522         e = Decimal(d)
00523         self.assertEqual(str(e), '-45')
00524         self.assertNotEqual(id(d), id(e))
00525 
00526         #zero
00527         d = Decimal(0)
00528         e = Decimal(d)
00529         self.assertEqual(str(e), '0')
00530         self.assertNotEqual(id(d), id(e))

Here is the call graph for this function:

Definition at line 532 of file test_decimal.py.

00532 
00533     def test_explicit_from_float(self):
00534         r = Decimal(0.1)
00535         self.assertEqual(type(r), Decimal)
00536         self.assertEqual(str(r),
00537                 '0.1000000000000000055511151231257827021181583404541015625')
00538         self.assertTrue(Decimal(float('nan')).is_qnan())
00539         self.assertTrue(Decimal(float('inf')).is_infinite())
00540         self.assertTrue(Decimal(float('-inf')).is_infinite())
00541         self.assertEqual(str(Decimal(float('nan'))),
00542                          str(Decimal('NaN')))
00543         self.assertEqual(str(Decimal(float('inf'))),
00544                          str(Decimal('Infinity')))
00545         self.assertEqual(str(Decimal(float('-inf'))),
00546                          str(Decimal('-Infinity')))
00547         self.assertEqual(str(Decimal(float('-0.0'))),
00548                          str(Decimal('-0')))
00549         for i in range(200):
00550             x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
00551             self.assertEqual(x, float(Decimal(x))) # roundtrip

Here is the call graph for this function:

Definition at line 423 of file test_decimal.py.

00423 
00424     def test_explicit_from_int(self):
00425 
00426         #positive
00427         d = Decimal(45)
00428         self.assertEqual(str(d), '45')
00429 
00430         #very large positive
00431         d = Decimal(500000123)
00432         self.assertEqual(str(d), '500000123')
00433 
00434         #negative
00435         d = Decimal(-45)
00436         self.assertEqual(str(d), '-45')
00437 
00438         #zero
00439         d = Decimal(0)
00440         self.assertEqual(str(d), '0')

Here is the call graph for this function:

Definition at line 420 of file test_decimal.py.

00420 
00421     def test_explicit_from_None(self):
00422         self.assertRaises(TypeError, Decimal, None)

Here is the call graph for this function:

Definition at line 441 of file test_decimal.py.

00441 
00442     def test_explicit_from_string(self):
00443 
00444         #empty
00445         self.assertEqual(str(Decimal('')), 'NaN')
00446 
00447         #int
00448         self.assertEqual(str(Decimal('45')), '45')
00449 
00450         #float
00451         self.assertEqual(str(Decimal('45.34')), '45.34')
00452 
00453         #engineer notation
00454         self.assertEqual(str(Decimal('45e2')), '4.5E+3')
00455 
00456         #just not a number
00457         self.assertEqual(str(Decimal('ugly')), 'NaN')
00458 
00459         #leading and trailing whitespace permitted
00460         self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4')
00461         self.assertEqual(str(Decimal('  -7.89')), '-7.89')

Here is the call graph for this function:

Definition at line 462 of file test_decimal.py.

00462 
00463     def test_explicit_from_tuples(self):
00464 
00465         #zero
00466         d = Decimal( (0, (0,), 0) )
00467         self.assertEqual(str(d), '0')
00468 
00469         #int
00470         d = Decimal( (1, (4, 5), 0) )
00471         self.assertEqual(str(d), '-45')
00472 
00473         #float
00474         d = Decimal( (0, (4, 5, 3, 4), -2) )
00475         self.assertEqual(str(d), '45.34')
00476 
00477         #weird
00478         d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
00479         self.assertEqual(str(d), '-4.34913534E-17')
00480 
00481         #wrong number of items
00482         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) )
00483 
00484         #bad sign
00485         self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
00486         self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
00487         self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2))
00488 
00489         #bad exp
00490         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
00491         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
00492         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') )
00493 
00494         #bad coefficients
00495         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) )
00496         self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
00497         self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
00498         self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) )

Here is the call graph for this function:

Definition at line 595 of file test_decimal.py.

00595 
00596     def test_unicode_digits(self):
00597         test_values = {
00598             '\uff11': '1',
00599             '\u0660.\u0660\u0663\u0667\u0662e-\u0663' : '0.0000372',
00600             '-nan\u0c68\u0c6a\u0c66\u0c66' : '-NaN2400',
00601             }
00602         for input, expected in test_values.items():
00603             self.assertEqual(str(Decimal(input)), expected)
00604 

Here is the call graph for this function:


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