[Python-checkins] python/nondist/sandbox/decimal test_Decimal.py,
1.8, 1.9
facundobatista at users.sourceforge.net
facundobatista at users.sourceforge.net
Thu Mar 11 21:07:33 EST 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23980
Modified Files:
test_Decimal.py
Log Message:
Incorpored the structure to run two group of tests and added the Explicit Construction tests
Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** test_Decimal.py 9 Mar 2004 02:43:34 -0000 1.8
--- test_Decimal.py 12 Mar 2004 02:07:30 -0000 1.9
***************
*** 5,13 ****
These are the test cases for the Decimal module.
! By now, they use the tests provided by Mike Cowlishaw to test the arithmetic
! behaviour of the module.
"""
! # 0.1.1 2003.3.08 facundobatista: Commented out test.test_support
# ToDo:
--- 5,20 ----
These are the test cases for the Decimal module.
! There are two groups of tests, Arithmetic and Behaviour. The former test
! the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter
! test the pythonic behaviour according to PEP 327.
!
! This test module can be called from command line with one parameter (Arithmetic
! or Behaviour) to test each part, or without parameter to test both parts. If
! you're working through IDLE, you can import this test module and call test_main()
! with the corresponding argument.
"""
! # 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:
***************
*** 19,27 ****
from Decimal import *
! import os
- #Commented out by Facundo Batista to make the test work
- #from test.test_support import TestSkipped, run_unittest
TESTDATADIR = 'tests'
--- 26,33 ----
from Decimal import *
! import os, sys
+ from test.test_support import TestSkipped, run_unittest
TESTDATADIR = 'tests'
***************
*** 446,458 ****
self.eval_file(dir + 'trim' + '.decTest')
- def test_main():
- # run_unittest(DecimalTest)
- unittest.main()
if __name__ == '__main__':
! test_main()
--- 452,588 ----
self.eval_file(dir + 'trim' + '.decTest')
+ #
+ # The following classes test the behaviour of Decimal according to PEP 327
+ #
+ # - Explicit Construction
+ # - etc...
+ #
+ class DecimalExplicitConstructionTest(unittest.TestCase):
+ '''Unit tests for Explicit Construction cases of Decimal.'''
+ def test_Empty(self):
+ '''Explicit construction without parameters.'''
+ self.assertRaises(TypeError, Decimal)
+ def test_FromNone(self):
+ '''Explicit construction passing None as value.'''
+ self.assertRaises(TypeError, Decimal, None)
+ def test_FromInt(self):
+ '''Explicit construction with int or long.'''
+
+ #positive
+ d = Decimal(45)
+ self.assertEqual(str(d), '45')
+
+ #very large positive
+ d = Decimal(5000000123)
+ self.assertEqual(str(d), '5000000123')
+
+ #negative
+ d = Decimal(-45)
+ self.assertEqual(str(d), '-45')
+
+ #zero
+ d = Decimal(0)
+ self.assertEqual(str(d), '0')
+
+ def test_FromString(self):
+ '''Explicit construction with string.'''
+
+ #empty
+ self.assertRaises(ValueError, Decimal, '')
+
+ #int
+ d = Decimal('45')
+ self.assertEqual(str(d), '45')
+
+ #float
+ d = Decimal('45.34')
+ self.assertEqual(str(d), '45.34')
+
+ #engineer notation
+ d = Decimal('45e2')
+ self.assertEqual(str(d), '4.5E+3')
+
+ #just not a number
+ self.assertRaises(ValueError, Decimal, 'ugly')
+
+ def test_FromFloat(self):
+ '''Explicit construction with float.'''
+
+ # There is still no agreement here
+
+ def test_FromTuples(self):
+ '''Explicit construction with tuples.'''
+
+ #zero
+ d = Decimal( (0, (0,), 0) )
+ self.assertEqual(str(d), '0')
+
+ #int
+ d = Decimal( (1, (4, 5), 0) )
+ self.assertEqual(str(d), '-45')
+
+ #float
+ d = Decimal( (0, (4, 5, 3, 4), -2) )
+ self.assertEqual(str(d), '45.34')
+
+ #weird
+ d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
+ self.assertEqual(str(d), '-4.34913534E-17')
+
+ def test_FromDecimal(self):
+ '''Explicit construction with Decimal.'''
+
+ #positive
+ d = Decimal(45)
+ e = Decimal(d)
+ self.assertEqual(str(e), '45')
+ self.assertNotEqual(id(d), id(e))
+
+ #very large positive
+ d = Decimal(5000000123)
+ e = Decimal(d)
+ self.assertEqual(str(e), '5000000123')
+ self.assertNotEqual(id(d), id(e))
+
+ #negative
+ d = Decimal(-45)
+ e = Decimal(d)
+ self.assertEqual(str(e), '-45')
+ self.assertNotEqual(id(d), id(e))
+
+ #zero
+ d = Decimal(0)
+ e = Decimal(d)
+ self.assertEqual(str(e), '0')
+ self.assertNotEqual(id(d), id(e))
+
+
+ def test_main(which=None):
+ """ Execute the tests.
+
+ If called with "Arithmetic", just executes the arithmetic tests.
+ If called with "Behaviour", just executes the pythonic behaviour tests.
+ Otherwise, executes both of them.
+ """
+
+ if which == "Arithmetic":
+ run_unittest(DecimalTest)
+ elif which == "Behaviour":
+ run_unittest(DecimalExplicitConstructionTest)
+ else:
+ run_unittest(DecimalTest)
+ run_unittest(DecimalExplicitConstructionTest)
+ return
if __name__ == '__main__':
! if len(sys.argv) == 1:
! test_main()
! elif len(sys.argv) == 2:
! test_main(sys.argv[1])
! else:
! raise ValueError, "test called with wrong arguments, use test_Decimal [Arithmetic|Behaviour]"
More information about the Python-checkins
mailing list