[Python-checkins] python/nondist/sandbox/decimal test_Decimal.py, 1.17, 1.18

facundobatista at users.sourceforge.net facundobatista at users.sourceforge.net
Mon Jun 21 17:52:54 EDT 2004


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

Modified Files:
	test_Decimal.py 
Log Message:
New test cases. Several fixes. Using updated Cowlishaw test cases. Deprecated test cases for deprecated functionality.

Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** test_Decimal.py	20 Jun 2004 01:56:04 -0000	1.17
--- test_Decimal.py	21 Jun 2004 21:52:52 -0000	1.18
***************
*** 15,49 ****
  """
  
! # 0.1.8  2003.06.19  fb: Adjusted threading test case. Taken out the immutability
  #                    test. Added tearDown method to DecimalTest class. Some fixes 
  #                    because of the name changes.
! # 0.1.7  2003.04.05  fb: Adjusted several test cases. Eliminated interaction
  #                    with float in comparations and min/max test cases.
! # 0.1.6  2003.04.04  fb: Extended explicit construction test case from tuples.
! # 0.1.5  2003.04.02  fb: Adjusted explicit construction test cases.
! # 0.1.4  2003.03.28  fb: Added Use of Context and Decimal Usability test cases.
  #                    Corrected tests using try/except/else.
! # 0.1.3  2003.03.23  fb: Added arithmetic operators test and corrected minor
  #                    method case issues.
! # 0.1.2  2003.03.20  fb: 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.03.11  fb: Added Explicit Construction tests
! # 0.1.0  2003.03.11  fb: Placed the structure to run separate test groups
  #
  # fb = facundobatista
  
- # ToDo:
- #
- # Add the behaviour tests according to the PEP
- 
  from __future__ import division
  
  import unittest
  import glob
- from Decimal import *
- 
  import os, sys
  
  from test.test_support import TestSkipped, run_unittest
  
--- 15,49 ----
  """
  
! # 0.2.0  2004.06.21  fb: Using the new test cases from Cowlishaw. Deprecated trim
! #                    test case and use of result in inexact test case. Added
! #                    as_tuple() test case.
! # 0.1.9  2004.06.20  fb: More fixes because of the name changes. Added test case for
! #                    context.create_decimal().
! # 0.1.8  2004.06.19  fb: Adjusted threading test case. Taken out the immutability
  #                    test. Added tearDown method to DecimalTest class. Some fixes 
  #                    because of the name changes.
! # 0.1.7  2004.04.05  fb: Adjusted several test cases. Eliminated interaction
  #                    with float in comparations and min/max test cases.
! # 0.1.6  2004.04.04  fb: Extended explicit construction test case from tuples.
! # 0.1.5  2004.04.02  fb: Adjusted explicit construction test cases.
! # 0.1.4  2004.03.28  fb: Added Use of Context and Decimal Usability test cases.
  #                    Corrected tests using try/except/else.
! # 0.1.3  2004.03.23  fb: Added arithmetic operators test and corrected minor
  #                    method case issues.
! # 0.1.2  2004.03.20  fb: 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  2004.03.11  fb: Added Explicit Construction tests
! # 0.1.0  2004.03.11  fb: Placed the structure to run separate test groups
  #
  # fb = facundobatista
  
  from __future__ import division
  
  import unittest
  import glob
  import os, sys
  
+ from Decimal import *
  from test.test_support import TestSkipped, run_unittest
  
***************
*** 67,71 ****
                'division_undefined' : DivisionUndefined,
                'inexact' : Inexact,
-               'insufficient_storage' : InsufficientStorage,
                'invalid_context' : InvalidContext,
                'invalid_operation' : InvalidOperation,
--- 67,70 ----
***************
*** 77,81 ****
  
  
- 
  def Nonfunction(*args):
      """Doesn't do anything."""
--- 76,79 ----
***************
*** 98,101 ****
--- 96,101 ----
                 'remaindernear':'remainder_near',
                 'divideint':'divide_int',
+                'squareroot':'sqrt',
+                'apply':'_apply',
                }
  
***************
*** 108,112 ****
          global dir
          self.context = Context()
-         #self.filelist = glob.glob(dir+'*.decTest')
          for key in DefaultContext.trap_enablers.keys():
              DefaultContext.trap_enablers[key] = 1
--- 108,111 ----
***************
*** 198,206 ****
              val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
              return val
!         funct = nameAdapter.get(funct, funct)
!         fname = funct
!         funct = getattr(self.context, funct)
          if fname == 'rescale':
              return 
          vals = []
          conglomerate = ''
--- 197,204 ----
              val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
              return val
!         fname = nameAdapter.get(funct, funct)
          if fname == 'rescale':
              return 
+         funct = getattr(self.context, fname)
          vals = []
          conglomerate = ''
***************
*** 227,231 ****
                          self.context.trap_enablers[error] = 1
                          try:
!                             funct(self.context.new(v))
                          except error:
                              pass
--- 225,229 ----
                          self.context.trap_enablers[error] = 1
                          try:
!                             funct(self.context.create_decimal(v))
                          except error:
                              pass
***************
*** 236,245 ****
                              self.fail("Did not raise %s in %s" % (error, s))
                          self.context.trap_enablers[error] = 0                
!                 v = self.context.new(v)
!             #elif fname == 'rescale' and i == 1:
!             #    try:
!             #        v = int(Decimal(v))
!             #    except ValueError:
!             #        v = float(v)
              else:
                  v = Decimal(v)
--- 234,238 ----
                              self.fail("Did not raise %s in %s" % (error, s))
                          self.context.trap_enablers[error] = 0                
!                 v = self.context.create_decimal(v)
              else:
                  v = Decimal(v)
***************
*** 285,289 ****
  
      def getexceptions(self):
-         return self.context.all_flags()
          L = []
          for exception in ExceptionList:
--- 278,281 ----
***************
*** 305,309 ****
          self.context.Emax = exp
      def change_clamp(self, clamp):
!         self.context.clamp = clamp
  
      def test_abs(self):
--- 297,301 ----
          self.context.Emax = exp
      def change_clamp(self, clamp):
!         self.context._clamp = clamp
  
      def test_abs(self):
***************
*** 482,492 ****
          self.eval_file(dir + 'tointegral' + '.decTest')
  
-     def test_trim(self):
-         """Tests the Decimal class on Cowlishaw's trim tests.
- 
-         See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
-         """
-         self.eval_file(dir + 'trim' + '.decTest')
- 
  #
  # The following classes test the behaviour of Decimal according to PEP 327
--- 474,477 ----
***************
*** 647,650 ****
--- 632,676 ----
          self.assertNotEqual(id(d), id(e))
  
+     def test_context_create_decimal(self):
+         '''Explicit construction through context.create_decimal().'''
+         nc = copy.copy(getcontext())
+         nc.prec = 3
+ 
+         # empty
+         self.assertRaises(TypeError, nc.create_decimal)
+ 
+         # from None
+         self.assertRaises(TypeError, nc.create_decimal, None)
+ 
+         # from int
+         d = nc.create_decimal(456)
+         self.failUnless(isinstance(d, Decimal))
+         self.assertRaises(ValueError, nc.create_decimal, 45678)
+ 
+         # from string
+         d = Decimal('456789')
+         self.assertEqual(str(d), '456789')
+         d = nc.create_decimal('456789')
+         self.assertEqual(str(d), '4.57E+5')
+ 
+         # from float
+         d = Decimal.from_float(1.1)
+         self.assertEqual(str(d), '1.100000000000000088817841970012523233890533447265625')
+         d = nc.create_decimal(1.1)
+         self.assertEqual(str(d), '1.10')
+ 
+         # from tuples
+         d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
+         self.assertEqual(str(d), '-4.34913534E-17')
+         d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
+         self.assertEqual(str(d), '-4.35E-17')
+ 
+         # from Decimal
+         prevdec = Decimal(500000123)
+         d = Decimal(prevdec)
+         self.assertEqual(str(d), '500000123')
+         d = nc.create_decimal(prevdec)
+         self.assertEqual(str(d), '5.00E+8')
+ 
  
  class DecimalImplicitConstructionTest(unittest.TestCase):
***************
*** 1106,1110 ****
  
          #repr
!         self.assertEqual(repr(d), 'Decimal( (0, (1, 5, 3, 2), -2) )')
          
      def test_tonum_methods(self):
--- 1132,1136 ----
  
          #repr
!         self.assertEqual(repr(d), 'Decimal("15.32")')
          
      def test_tonum_methods(self):
***************
*** 1144,1147 ****
--- 1170,1193 ----
          d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
          self.assertEqual(d, eval(repr(d)))
+ 
+     def test_as_tuple(self):
+         '''Test as_tuple to show the internals.'''
+ 
+         #with zero
+         d = Decimal(0)
+         self.assertEqual(d.as_tuple(), (0, (0,), 0) )
+ 
+         #int
+         d = Decimal(-45)
+         self.assertEqual(d.as_tuple(), (1, (4, 5), 0) )
+         
+         #complicated string
+         d = Decimal("-4.34913534E-17")
+         self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
+ 
+         #inf
+         d = Decimal("Infinity")
+         self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
+ 
  ##
  ##  As is impossible to make a real immutable instance in Python,




More information about the Python-checkins mailing list