def testReprFixed0(self): "repr is the underlying _value" A = V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=self.p))) self.assertEqual(repr(A(1)), '1') A = V.ArithmeticClass(Options(dict(arithmetic='integer'))) self.assertEqual(repr(A(1)), '1')
def setUp(self): "initialize guarded and fixed to no guard" F.initialize( Options(dict(arithmetic='fixed', precision=self.p, guard=self.g))) G.initialize( Options(dict(arithmetic='guarded', precision=self.p, guard=self.g)))
def testBadPrecision(self): "test illegal precision" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision=1.1))) self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision=-1))) self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision='abc')))
def testFixedDisplay3(self): "fixed display < precision rounds properly" V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=6, display=6))) self.assertEqual(str(F(20) / F(3)), '6.666666') V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=7, display=6))) self.assertEqual(str(F(20) / F(3)), '6.666667')
def testBadDisplay(self): "test illegal display" self.assertRaises( UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision=4, display=1.1))) self.assertRaises( UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision=4, display=-1))) self.assertRaises( UsageError, V.ArithmeticClass, Options(dict(arithmetic='guarded', precision=4, display='abc')))
def testBigDisplay(self): "display is limited to precision+guard" p = 5 g = 6 d = p + g + 1 A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p, guard=g, display=d))) self.assertEqual(A.display, p+g) self.assertEqual(str(A(20)/A(3)), '6.66666_666666')
def testUnaryOps(self): "test unary + and -" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=4))) x = A(1) y = A(2) self.assertEqual(x-y, -x) self.assertEqual(y-x, +x) self.assertEqual(abs(x-y), x)
def testMulDiv(self): "guarded muldiv" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) f13 = A(1)/A(3) f15 = A(1)/A(5) f17 = A(1)/A(7) self.assertEqual(A.muldiv(f13, f15, f17), f13*f15/f17) self.assertEqual(str(f13*f15/f17), '0.466666667') self.assertEqual(str(A.muldiv(f13, f15, f17)), '0.466666667')
def testMulDiv0(self): "guarded muldiv with g=0" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9, guard=0))) f13 = A(1)/A(3) f15 = A(1)/A(5) f17 = A(1)/A(7) self.assertEqual(A.muldiv(f13, f15, f17)._value, (f13*f15/f17)._value+5) self.assertEqual(A.muldiv(f13, f15, f17, round='down')._value, (f13*f15/f17)._value+5) self.assertEqual(A.muldiv(f13, f15, f17, round='up')._value, (f13*f15/f17)._value+6) self.assertEqual(str(f13*f15/f17), '0.466666664') self.assertEqual(str(A.muldiv(f13, f15, f17)), '0.466666669')
class ValueTestRational(unittest.TestCase): "rational-specific unit tests" R.initialize(Options(dict())) f13 = R(1)/R(3) f15 = R(1)/R(5) f17 = R(1)/R(7) def testRat1(self): "no loss of precision" self.assertEqual(self.f13 * R(3), R(1)) def testMulDiv(self): "rational muldiv is the same as multiply followed by divide" self.assertEqual(R.muldiv(self.f13, self.f15, self.f17), self.f13*self.f15/self.f17)
def testBadP2(self): "precision must be >= 0" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(precision=-1)))
def testValueInitRational(self): "class Rational if arithmetic=rational" self.assertEqual(V.ArithmeticClass(Options(dict(arithmetic='rational'))), R)
def testBadFixedA(self): "fixed called directly must be fixed or integer" self.assertRaises(UsageError, F.initialize, Options(dict(arithmetic='bill')))
def testValueInitFixed(self): "class Fixed if arithmetic=fixed" self.assertEqual(V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=8))), F)
def testValueInitRationalDefault(self): "default class Guarded" self.assertEqual(V.ArithmeticClass(Options(dict(precision=8))), G)
def testDivInt(self): "test guarded/int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) y = A(3) self.assertEqual(x/y, x/3)
def testFixedIntegerP0(self): "fixed=integer yields precision 0" V.ArithmeticClass(Options(dict(arithmetic='integer'))) self.assertEqual(F.precision, 0)
def testNe(self): "not equal" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) self.assertFalse(A(1)!=A(1))
def testGeps(self): "geps is a function of guard" p = 9 g = p A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p))) self.assertEqual(A._Guarded__geps, 10**g/2)
def testBadFP1(self): "fixed precision must be an int" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(arithmetic='fixed', precision=5.5)))
def testDisplay(self): "display defaults to precision" p = 5 A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p))) self.assertEqual(A.display, p) self.assertEqual(str(A(20)/A(3)), '6.66667')
def testNoHash(self): "test guarded/int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) self.assertRaises(NotImplementedError, hash, x)
def testMulInt(self): "test guarded*int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) y = A(3) self.assertEqual(x*y, x*3)
def testBadG1(self): "guard must be an int" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(precision=5, guard=5.5)))
def testBadG2(self): "guard must be >= 0" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(precision=5, guard=-1)))
def testBadArithmetic(self): "try unknown arithmetic" self.assertRaises(V.ArithmeticValuesError, V.ArithmeticClass, Options(dict(arithmetic='saywhat')))
def testBadFixedDx(self): "fixed display must be numeric" self.assertRaises(UsageError, F.initialize, Options(dict(arithmetic='fixed', precision=9, display='abc')))
def testBadArithmetic(self): "test illegal arithmetic" self.assertRaises(UsageError, G.initialize, Options(dict(arithmetic='fixed', precision=4)))
def testBadFP2(self): "fixed precision must be >= 0" self.assertRaises(UsageError, V.ArithmeticClass, Options(dict(arithmetic='fixed', precision=-1)))
def testRepr(self): "repr is the underlying _value" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) self.assertEqual(repr(A(1)), 'Guarded(1000000000000000000,True)')