[Python-checkins] python/nondist/sandbox/decimal test_Decimal.py,
1.11, 1.12
facundobatista at users.sourceforge.net
facundobatista at users.sourceforge.net
Sun Mar 28 10:39:41 EST 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6260
Modified Files:
test_Decimal.py
Log Message:
Added two groups of test classes and corrected some minor try/except issues.
Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_Decimal.py 24 Mar 2004 01:57:49 -0000 1.11
--- test_Decimal.py 28 Mar 2004 15:39:38 -0000 1.12
***************
*** 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:
--- 15,29 ----
"""
! # 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:
***************
*** 615,619 ****
except TypeError:
pass
!
def test_from_int(self):
'''Implicit construction with int or long.'''
--- 619,625 ----
except TypeError:
pass
! else:
! self.fail('Did not raised an error!')
!
def test_from_int(self):
'''Implicit construction with int or long.'''
***************
*** 629,632 ****
--- 635,640 ----
except TypeError:
pass
+ else:
+ self.fail('Did not raised an error!')
def test_from_string(self):
***************
*** 639,642 ****
--- 647,652 ----
except TypeError:
pass
+ else:
+ self.fail('Did not raised an error!')
def test_from_float(self):
***************
*** 649,652 ****
--- 659,664 ----
except TypeError:
pass
+ else:
+ self.fail('Did not raised an error!')
def test_from_Decimal(self):
***************
*** 901,904 ****
--- 913,1141 ----
+ # The following are two functions used to test threading in the next class
+
+ def thfunc1(cls):
+ d1 = Decimal(1)
+ d3 = Decimal(3)
+ cls.assertEqual(d1/d3, Decimal('0.333333333'))
+ cls.event.wait()
+ cls.assertEqual(d1/d3, Decimal('0.333333333'))
+ return
+
+ def thfunc2(cls):
+ d1 = Decimal(1)
+ d3 = Decimal(3)
+ cls.assertEqual(d1/d3, Decimal('0.333333333'))
+ thiscontext = getcontext()
+ thiscontext.prec = 18
+ cls.assertEqual(d1/d3, Decimal('0.333333333333333333'))
+ cls.event.set()
+ return
+
+
+ class DecimalUseOfContextTest(unittest.TestCase):
+ '''Unit tests for Use of Context cases in Decimal.'''
+
+ import threading
+ # Take care executing this test from IDLE, there's an issue in threading
+ # that hangs IDLE and I couldn't find it
+
+ def test_threading(self):
+ '''Test the "threading isolation" of a Context.'''
+
+ self.event = threading.Event()
+
+ th1 = threading.Thread(target=thfunc1, args=(self,))
+ th2 = threading.Thread(target=thfunc2, args=(self,))
+
+ th1.start()
+ th2.start()
+ return
+
+
+ class DecimalUsabilityTest(unittest.TestCase):
+ '''Unit tests for Usability cases of Decimal.'''
+
+ def test_comparison_operators(self):
+ '''Testing ==, !=, <, >, <=, >=, cmp.'''
+
+ da = Decimal('23.42')
+ db = Decimal('23.42')
+ dc = Decimal('45')
+
+ #two Decimals
+ self.failUnless(dc > da)
+ self.failUnless(dc >= da)
+ self.failUnless(da < dc)
+ self.failUnless(da <= dc)
+ self.failUnless(da == db)
+ self.failUnless(da != dc)
+ self.failUnless(da <= db)
+ self.failUnless(da >= db)
+ self.assertEqual(cmp(dc,da), 1)
+ self.assertEqual(cmp(da,dc), -1)
+ self.assertEqual(cmp(da,db), 0)
+
+ #a Decimal and an int
+ self.failUnless(dc > 23)
+ self.failUnless(23 < dc)
+ self.failUnless(dc == 45)
+ self.assertEqual(cmp(dc,23), 1)
+ self.assertEqual(cmp(23,dc), -1)
+ self.assertEqual(cmp(dc,45), 0)
+
+ #a Decimal and a float
+ self.failUnless(dc > 23.42)
+ self.failUnless(23.42 < dc)
+ self.failUnless(da == 23.42)
+ self.assertEqual(cmp(dc,23.42), 1)
+ self.assertEqual(cmp(23.42,dc), -1)
+ self.assertEqual(cmp(da,23.42), 0)
+
+ #a Decimal and uncomparable
+ self.assertRaises(TypeError, da == 'ugly')
+ self.assertRaises(TypeError, da == '32.7')
+ self.assertRaises(TypeError, da == object)
+
+ def test_copy_methods(self):
+ '''Test copy and deepcopy.'''
+
+ import copy
+ d = Decimal('43.24')
+
+ #copy
+ c = copy.copy(d)
+ self.assertEqual(c, d)
+ self.assertNotEqual(id(c), id(d))
+
+ #deepcopy
+ dc = copy.deepcopy(d)
+ self.assertEqual(dc, d)
+ self.assertNotEqual(id(dc), id(d))
+
+ def test_hash_method(self):
+ '''Test hash.'''
+
+ #just that it's hashable
+ hash(Decimal(23))
+
+ #the same hash that to an int
+ self.assertEqual(hash(Decimal(23)), hash(23))
+
+ #the same hash that to an "exact float"
+ self.assertEqual(hash(Decimal('32.32')), hash(32.32))
+
+ #the same hash that to an "inexact float"
+ self.assertEqual(hash(Decimal('23.72')), hash(23.72))
+
+ def test_minmax_methods(self):
+ '''Test min and max between Decimal and others.'''
+
+ d1 = Decimal('15.32')
+ d2 = Decimal('28.5')
+ l1 = 15
+ l2 = 28
+ f1 = 15.32
+ f2 = 28.5
+
+ #between Decimals
+ self.failUnless(min(d1,d2) is d1)
+ self.failUnless(min(d2,d1) is d1)
+ self.failUnless(max(d1,d2) is d2)
+ self.failUnless(max(d2,d1) is d2)
+
+ #between Decimal and long
+ self.failUnless(min(d1,l2) is d1)
+ self.failUnless(min(l2,d1) is d1)
+ self.failUnless(max(l1,d2) is d2)
+ self.failUnless(max(d2,l1) is d2)
+
+ #between Decimal and float
+ self.failUnless(min(d1,f2) is d1)
+ self.failUnless(min(f2,d1) is d1)
+ self.failUnless(max(f1,d2) is d2)
+ self.failUnless(max(d2,f1) is d2)
+
+ def test_as_boolean(self):
+ '''Test that it can be used as boolean.'''
+
+ #as false
+ self.failIf(Decimal(0))
+
+ #as true
+ self.failUnless(Decimal('0.372'))
+
+ def test_tostring_methods(self):
+ '''Test str and repr methods.'''
+
+ d = Decimal('15.32')
+
+ #str
+ self.assertEqual(str(d), '15.32')
+
+ #repr
+ self.assertEqual(repr(d), 'Decimal( (0, (1, 5, 3, 2), -2) )')
+
+ def test_tonum_methods(self):
+ '''Test float, int and long methods.'''
+
+ d1 = Decimal('66')
+ d2 = Decimal('15.32')
+
+ #int
+ self.assertEqual(int(d1), 66)
+ self.assertEqual(int(d2), 15)
+
+ #long
+ self.assertEqual(long(d1), 66)
+ self.assertEqual(long(d2), 15)
+
+ #float
+ self.assertEqual(float(d1), 66)
+ self.assertEqual(float(d2), 15.32)
+
+ def test_round_trip(self):
+ '''Testing that d == eval(repr(d)) with d as Decimal.'''
+
+ #with zero
+ d = Decimal( (0, (0,), 0) )
+ self.assertEqual(d, eval(repr(d)))
+
+ #int
+ d = Decimal( (1, (4, 5), 0) )
+ self.assertEqual(d, eval(repr(d)))
+
+ #float
+ d = Decimal( (0, (4, 5, 3, 4), -2) )
+ self.assertEqual(d, eval(repr(d)))
+
+ #weird
+ d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
+ self.assertEqual(d, eval(repr(d)))
+
+ def test_immutability(self):
+ '''Try to change internal objects and see if immutable.'''
+
+ d = Decimal(42)
+
+ #the used attributes
+ try:
+ d._exp = 20
+ d._int = 3
+ d._sign = 1
+ except TypeError:
+ pass
+ else:
+ self.fail('Did not raised an error!')
+
+ #some new attribute
+ try:
+ d.newone = None
+ except TypeError:
+ pass
+ else:
+ self.fail('Did not raised an error!')
+
+
def test_main(which=None):
***************
*** 917,920 ****
--- 1154,1160 ----
run_unittest(DecimalImplicitConstructionTest)
run_unittest(DecimalArithmeticOperatorsTest)
+ run_unittest(DecimalUseOfContextTest)
+ run_unittest(DecimalUsabilityTest)
+
return
More information about the Python-checkins
mailing list