[Python-checkins] python/nondist/sandbox/decimal test_Decimal.py, 1.10, 1.11

facundobatista at users.sourceforge.net facundobatista at users.sourceforge.net
Tue Mar 23 20:57:51 EST 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv841

Modified Files:
	test_Decimal.py 
Log Message:
Added arithmetic operators test and corrected minor method case issues.

Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** test_Decimal.py	20 Mar 2004 19:33:07 -0000	1.10
--- test_Decimal.py	24 Mar 2004 01:57:49 -0000	1.11
***************
*** 15,20 ****
  """
  
! # 0.1.1     2003.3.11  facundobatista: Added Explicit Construction tests
! # 0.1.0     2003.3.11  facundobatista: Placed the structure to run separate test groups
  
  # ToDo:
--- 15,25 ----
  """
  
! # 0.1.3  2003.3.23  facundobatista: Added arithmetic operators test and corrected
! #                   minor method case issues.
! # 0.1.2  2003.3.20  facundobatista: Added from_float to explicit construction test
! #                   cases and all implicit construction test cases. Also upgraded
! #                   method names to new GvR definiton.
! # 0.1.1  2003.3.11  facundobatista: Added Explicit Construction tests
! # 0.1.0  2003.3.11  facundobatista: Placed the structure to run separate test groups
  
  # ToDo:
***************
*** 22,25 ****
--- 27,32 ----
  # Add the behaviour tests according to the PEP
  
+ from __future__ import division
+ 
  import unittest
  import glob
***************
*** 633,637 ****
              pass
  
!     def testIC_FromFloat(self):
          '''Implicit construction with float.'''
          d = Decimal(5)
--- 640,644 ----
              pass
  
!     def test_from_float(self):
          '''Implicit construction with float.'''
          d = Decimal(5)
***************
*** 643,647 ****
              pass
  
!     def testIC_FromDecimal(self):
          '''Implicit construction with Decimal.'''
  
--- 650,654 ----
              pass
  
!     def test_from_Decimal(self):
          '''Implicit construction with Decimal.'''
  
***************
*** 653,656 ****
--- 660,905 ----
  
  
+ class DecimalArithmeticOperatorsTest(unittest.TestCase):
+     '''Unit tests for all arithmetic operators, binary and unary.'''
+ 
+     def test_addition(self):
+         '''Test addition in all its ways.'''
+ 
+         d1 = Decimal('-11.1')
+         d2 = Decimal('22.2')
+ 
+         #two Decimals
+         self.assertEqual(d1+d2, Decimal('11.1'))
+         self.assertEqual(d2+d1, Decimal('11.1'))
+ 
+         #with other type, left
+         c = d1 + 5
+         self.assertEqual(c, Decimal('-6.1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 5 + d1
+         self.assertEqual(c, Decimal('-6.1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 += d2
+         self.assertEqual(d1, Decimal('11.1'))
+ 
+         #inline with other type
+         d1 += 5
+         self.assertEqual(d1, Decimal('-6.1'))
+ 
+     def test_subtraction(self):
+         '''Test subtraction in all its ways.'''
+ 
+         d1 = Decimal('-11.1')
+         d2 = Decimal('22.2')
+ 
+         #two Decimals
+         self.assertEqual(d1-d2, Decimal('-33.3'))
+         self.assertEqual(d2-d1, Decimal('33.3'))
+ 
+         #with other type, left
+         c = d1 - 5
+         self.assertEqual(c, Decimal('-16.1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 5 - d1
+         self.assertEqual(c, Decimal('6.1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 -= d2
+         self.assertEqual(d1, Decimal('-33.3'))
+ 
+         #inline with other type
+         d1 -= 5
+         self.assertEqual(d1, Decimal('-16.1'))
+ 
+     def test_multiplication(self):
+         '''Test multiplication in all its ways.'''
+ 
+         d1 = Decimal('-5')
+         d2 = Decimal('3')
+ 
+         #two Decimals
+         self.assertEqual(d1*d2, Decimal('-15'))
+         self.assertEqual(d2*d1, Decimal('-15'))
+ 
+         #with other type, left
+         c = d1 * 5
+         self.assertEqual(c, Decimal('-25'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 5 * d1
+         self.assertEqual(c, Decimal('-25'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 *= d2
+         self.assertEqual(d1, Decimal('-15'))
+ 
+         #inline with other type
+         d1 *= 5
+         self.assertEqual(d1, Decimal('-25'))
+ 
+     def test_division(self):
+         '''Test division in all its ways.'''
+ 
+         d1 = Decimal('-5')
+         d2 = Decimal('2')
+ 
+         #two Decimals
+         self.assertEqual(d1/d2, Decimal('-2.5'))
+         self.assertEqual(d2/d1, Decimal('-0.4'))
+ 
+         #with other type, left
+         c = d1 / 4
+         self.assertEqual(c, Decimal('-1.25'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 4 / d1
+         self.assertEqual(c, Decimal('-0.8'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 /= d2
+         self.assertEqual(d1, Decimal('-2.5'))
+ 
+         #inline with other type
+         d1 /= 4
+         self.assertEqual(d1, Decimal('-1.25'))
+ 
+     def test_floor_division(self):
+         '''Test floor division in all its ways.'''
+ 
+         d1 = Decimal('5')
+         d2 = Decimal('2')
+ 
+         #two Decimals
+         self.assertEqual(d1//d2, Decimal('2'))
+         self.assertEqual(d2//d1, Decimal('0'))
+ 
+         #with other type, left
+         c = d1 // 4
+         self.assertEqual(c, Decimal('1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 7 // d1
+         self.assertEqual(c, Decimal('1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 /= d2
+         self.assertEqual(d1, Decimal('2'))
+ 
+         #inline with other type
+         d1 /= 4
+         self.assertEqual(d1, Decimal('1'))
+ 
+     def test_powering(self):
+         '''Test powering in all its ways.'''
+ 
+         d1 = Decimal('5')
+         d2 = Decimal('2')
+ 
+         #two Decimals
+         self.assertEqual(d1**d2, Decimal('25'))
+         self.assertEqual(d2**d1, Decimal('32'))
+ 
+         #with other type, left
+         c = d1 ** 4
+         self.assertEqual(c, Decimal('625'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 7 ** d1
+         self.assertEqual(c, Decimal('16807'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 **= d2
+         self.assertEqual(d1, Decimal('25'))
+ 
+         #inline with other type
+         d1 **= 4
+         self.assertEqual(d1, Decimal('625'))
+ 
+     def test_module(self):
+         '''Test module in all its ways.'''
+ 
+         d1 = Decimal('5')
+         d2 = Decimal('2')
+ 
+         #two Decimals
+         self.assertEqual(d1%d2, Decimal('1'))
+         self.assertEqual(d2%d1, Decimal('2'))
+ 
+         #with other type, left
+         c = d1 % 4
+         self.assertEqual(c, Decimal('1'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #with other type, right
+         c = 7 % d1
+         self.assertEqual(c, Decimal('2'))
+         self.assertEqual(type(c), type(d1))
+ 
+         #inline with decimal
+         d1 %= d2
+         self.assertEqual(d1, Decimal('1'))
+ 
+         #inline with other type
+         d1 %= 4
+         self.assertEqual(d1, Decimal('1'))
+ 
+     def test_floor_div_module(self):
+         '''Test floor division with module in all its ways.'''
+ 
+         d1 = Decimal('5')
+         d2 = Decimal('2')
+ 
+         #two Decimals
+         (p, q) = divmod(d1, d2)
+         self.assertEqual(p, Decimal('2'))
+         self.assertEqual(q, Decimal('1'))
+         self.assertEqual(type(p), type(d1))
+         self.assertEqual(type(q), type(d1))
+ 
+         #with other type, left
+         (p, q) = divmod(d1, 4)
+         self.assertEqual(p, Decimal('1'))
+         self.assertEqual(q, Decimal('1'))
+         self.assertEqual(type(p), type(d1))
+         self.assertEqual(type(q), type(d1))
+ 
+         #with other type, right
+         (p, q) = divmod(7, d1)
+         self.assertEqual(p, Decimal('1'))
+         self.assertEqual(q, Decimal('2'))
+         self.assertEqual(type(p), type(d1))
+         self.assertEqual(type(q), type(d1))
+ 
+     def test_unary_operators(self):
+         '''Testing -, +, abs.'''
+ 
+         #test '+'
+         d = Decimal(45)
+         self.assertEqual(+d, d)
+         self.assertEqual(id(+d), id(d))
+ 
+         #test '-'
+         self.assertEqual(-Decimal(45), Decimal(-45))
+ 
+         #test abs
+         self.assertEqual(abs(Decimal(45)), abs(Decimal(-45)))
+ 
+ 
+ 
  def test_main(which=None):
      """ Execute the tests.
***************
*** 661,671 ****
      """
  
!     if which == "Arithmetic":
!         run_unittest(DecimalTest)
!     elif which == "Behaviour":
!         run_unittest(DecimalExplicitConstructionTest)
!     else:
          run_unittest(DecimalTest)
          run_unittest(DecimalExplicitConstructionTest)
      return
  
--- 910,920 ----
      """
  
!     if which == "Arithmetic" or which is None:
          run_unittest(DecimalTest)
+ 
+     if which == "Behaviour" or which is None:
          run_unittest(DecimalExplicitConstructionTest)
+         run_unittest(DecimalImplicitConstructionTest)
+         run_unittest(DecimalArithmeticOperatorsTest)
      return
  




More information about the Python-checkins mailing list