def testCreateDefault(self): """Simply create a default object.""" n = FixedPoint(); self.assertEquals(n.get_precision(), DEFAULT_PRECISION) self.assertEquals(long(n), 0) n = SonOfFixedPoint(); self.assertEquals(n.get_precision(), DEFAULT_PRECISION) self.assertEquals(long(n), 0)
def testSetAndGetPrecision(self): """Change and retrieve the precision of an existin object""" # try a floating point number with a negative exponent n = FixedPoint(42.3e-1); self.assertEquals(n.get_precision(), DEFAULT_PRECISION) self.assertEquals(n.precision, DEFAULT_PRECISION) self.assertEquals(n.n, 423L) n = SonOfFixedPoint(42.3e-1); self.assertEquals(n.get_precision(), DEFAULT_PRECISION) self.assertEquals(n.precision, DEFAULT_PRECISION) self.assertEquals(n.n, 423L) # try something that's not a number self.failUnlessRaises(TypeError, n.set_precision, object); self.failUnlessRaises(TypeError, n.precision, object); # try a negative number self.failUnlessRaises(ValueError, n.set_precision, -3); # try a precision greater than we started with newprecision = DEFAULT_PRECISION + 1 n.set_precision(newprecision) self.assertEquals(n.get_precision(), newprecision) self.assertEquals(n.n, 4230L) precision = n.precision + 1 n.precision += 1 self.assertEquals(n.precision, precision) # try a precision less than we started with newprecision = DEFAULT_PRECISION - 1 n.set_precision(newprecision) self.assertEquals(n.get_precision(), newprecision) self.assertEquals(n.n, 42)
def test__repr__(self): """test representation""" REPR_FORMAT = "FixedPoint('%s', %d)" # try the default n = FixedPoint() self.assertEquals(repr(n), REPR_FORMAT % (str(n), n.get_precision())) # try a floating point number with a negative exponent n = FixedPoint(42.3e-1); self.assertEquals(repr(n), REPR_FORMAT % (str(n), n.get_precision())) # try a negative floating point number n = FixedPoint(-4.23); self.assertEquals(repr(n), REPR_FORMAT % (str(n), n.get_precision())) # try an int n = FixedPoint(1, 0); self.assertEquals(repr(n), REPR_FORMAT % (str(n), n.get_precision())) SON_OF_FORMAT = "SonOfFixedPoint('%s', %d)" # try the default n = SonOfFixedPoint() self.assertEquals(repr(n), SON_OF_FORMAT % (str(n), n.get_precision())) # try a floating point number with a negative exponent n = SonOfFixedPoint(42.3e-1); self.assertEquals(repr(n), SON_OF_FORMAT % (str(n), n.get_precision())) # try a negative floating point number n = SonOfFixedPoint(-4.23); self.assertEquals(repr(n), SON_OF_FORMAT % (str(n), n.get_precision())) # try an int n = SonOfFixedPoint(1, 0); self.assertEquals(repr(n), SON_OF_FORMAT % (str(n), n.get_precision()))