def testCreateFromString(self):
        """Create a FixedPoint from a string"""

        # try an empty string
        self.failUnlessRaises(ValueError, FixedPoint, "");

        # try a fixed point zero
        n = FixedPoint("0");
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 0L)

        # try a floating point zero
        n = FixedPoint("0.0");
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 0L)

        # try a floating point number with a positive exponent
        n = FixedPoint("42.3e5");
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 423000000L)

        # try a floating point number with a negative exponent
        n = FixedPoint("42.3e-1");
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 423L)

        # try truncating the precision
        n = FixedPoint("42.123");
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 4212)
 def testFrac(self):
     """test return of the fractional portion"""
     self.assertEquals(
         FixedPoint(), FixedPoint(4).frac())
     self.assertEquals(
         FixedPoint(0.1416, 4),
         FixedPoint(3.14159, 4).frac())
    def test__nonzero__(self):
        """test the truth value"""

        # test the default
        self.failIf(FixedPoint())

        # test one that should be true
        self.failUnless(FixedPoint(1.0e-15, 15))
    def test__neg__(self):
        """test negative"""

        # test the default
        self.failIf(-FixedPoint())

        # test one that should be true
        self.failUnless(-FixedPoint(-1.0e-15, 15))
    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 test__abs__(self):
        """test absolute value"""

        # test the default
        d = FixedPoint()
        self.assertEquals(abs(d), d)

        # test a negative
        n = FixedPoint(-1.0e-15, 15)
        self.assertEquals(abs(n), -n)
 def test__mod__(self):
     """test modulo"""
     a = FixedPoint(3.33)
     b = 2
     c = a % b
     self.assertEquals(c, FixedPoint(1.33))
     
     a = FixedPoint(3.33)
     b = FixedPoint(1.111)
     c = a % b
     self.assertEquals(c, FixedPoint(0))
    def test__rmod__(self):
        """test right modulo"""
        a = FixedPoint(3.33)
        b = 4
        c = b % a
        self.assertEquals(c, FixedPoint(0.67))

        a = FixedPoint(3.33)
        b = SonOfFixedPoint(6.666)
        c = b % a
        self.assertEquals(c, SonOfFixedPoint(0.01))
 def testBankersRounding(self):
     """test that bankers rounding works as expected"""
     prevrounding = FixedPoint.round
     FixedPoint.round = bankersRounding
     # we expect to round 1 up because it's odd
     self.assertEquals(
         FixedPoint(1.5,0), FixedPoint(2.0,0))
     # we expect to leave 2 alone because it's even
     self.assertEquals(
         FixedPoint(2.5,0), FixedPoint(2.0,0))
     FixedPoint.round = prevrounding
Esempio n. 10
0
 def testAddHalfAndChop(self):
     """test that 'add half and chop' rounding works as expected"""
     prevrounding = FixedPoint.round
     FixedPoint.round = addHalfAndChop
     # we expect to round 1 up
     self.assertEquals(
         FixedPoint(1.5,0), FixedPoint(2.0,0))
     # we expect to round 2 up as well
     self.assertEquals(
         FixedPoint(2.5,0), FixedPoint(3.0,0))
     FixedPoint.round = prevrounding
Esempio n. 11
0
    def testCreateFromFixedPoint(self):
        """Create a FixedPoint from another FixedPoint"""

        # try a negative
        n = FixedPoint(-333);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, -33300L)

        # try a negative
        x = FixedPoint(n);
        self.assertEquals(x.precision, DEFAULT_PRECISION)
        self.assertEquals(x.n, -33300L)

        x = SonOfFixedPoint(n);
        self.assertEquals(x.precision, DEFAULT_PRECISION)
        self.assertEquals(x.n, -33300L)
Esempio n. 12
0
    def testCreateFromIntOrLong(self):
        """Create a FixedPoint from an int or a  long"""

        # try a negative
        n = FixedPoint(-333);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, -33300)

        # try a zero
        n = FixedPoint(0);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 0L)

        # try a positive
        n = FixedPoint(333);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 33300L)
Esempio n. 13
0
    def test__cmp__(self):
        """test compare"""

        # test two defaults
        a = FixedPoint()
        b = FixedPoint()
        self.failIf(a < b)
        self.failUnless(a == b)
        self.failIf(a > b)
        
        # test equal precision
        a = FixedPoint(1.11)
        b = FixedPoint(1.12)
        self.failUnless(a < b)
        self.failIf(a == b)
        self.failIf(a > b)
        
        # test unequal precision
        a = FixedPoint(1.125, 3)
        b = FixedPoint(1.12)
        self.failIf(a < b)
        self.failIf(a == b)
        self.failUnless(a > b)

        # test equal precision, with subclass
        a = FixedPoint(1.11)
        b = SonOfFixedPoint(1.12)
        self.failUnless(a < b)
        self.failIf(a == b)
        self.failIf(a > b)
Esempio n. 14
0
    def testCreateFromFloat(self):
        """Create a FixedPoint from a floating point number"""

        # try a floating point zero
        n = FixedPoint(0.0);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 0L)

        # try a floating point number with a positive exponent
        n = FixedPoint(42.3e5);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 423000000L)

        # try a floating point number with a negative exponent
        n = FixedPoint(42.3e-1);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 423L)

        # try truncating the precision
        n = FixedPoint(42.123);
        self.assertEquals(n.precision, DEFAULT_PRECISION)
        self.assertEquals(n.n, 4212L)
Esempio n. 15
0
 def __init__(self, amount, currency):
     __traceback_info__ = (amount, currency)
     assert not isinstance(amount, FloatType), str(amount)
     if isinstance(amount, StringTypes) and len(amount) > 0:
         amount = self._decode(amount)
     if not isinstance(amount, FixedPoint):
         amount = FixedPoint(amount)
     self.amt = amount
     if isinstance(currency, StringTypes):
         currency = CURRENCIES[currency]
     assert isinstance(currency, Currency), (
         str(currency), type(currency))
     self.ccy = currency
Esempio n. 16
0
 def test__copy__(self):
     """test shallow copy"""
     import copy
     
     # try a negative floating point number
     n = FixedPoint(-4.23);
     self.assertEquals(n, copy.copy(n))
     self.failIf(n is copy.copy(n))
     
     # try a negative floating point number
     n = SonOfFixedPoint(-4.23);
     self.assertEquals(n, copy.copy(n))
     self.failIf(n is copy.copy(n))
Esempio n. 17
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)
Esempio n. 18
0
    def test__str__(self):
        """test conversion to string"""

        # try the default
        n = FixedPoint()
        self.assertEquals(str(n), "0.00")
        
        n = SonOfFixedPoint()
        self.assertEquals(str(n), "0.00")
        
        # try a floating point number with a negative exponent
        n = FixedPoint(42.3e-1);
        self.assertEquals(str(n), "4.23")
        
        n = SonOfFixedPoint(42.3e-1);
        self.assertEquals(str(n), "4.23")
        
        # try a negative floating point number
        n = FixedPoint(-4.23);
        self.assertEquals(str(n), "-4.23")

        # try an int
        n = FixedPoint(1, 0);
        self.assertEquals(str(n), "1.")
Esempio n. 19
0
 def test__rmul__(self):
     """test multiplication"""
     
     a = FixedPoint(3.33)
     b = 3
     c = b * a
     self.assertEquals(type(c), type(a))
     self.assertEquals(c.precision, DEFAULT_PRECISION)
     self.assertEquals(c.n, 999L)
     
     a = SonOfFixedPoint(3.33)
     b = 3
     c = b * a
     self.assertEquals(type(c), type(a))
     self.assertEquals(c.precision, DEFAULT_PRECISION)
     self.assertEquals(c.n, 999L)
Esempio n. 20
0
    def test__rdiv__(self):
        """test right division"""
        
        a = FixedPoint(3)
        b = 1
        c = b / a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, DEFAULT_PRECISION)
        self.assertEquals(c.n, 33L)

        a = SonOfFixedPoint(3.33, 6)
        b = 1
        c = b / a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, 6)
        self.assertEquals(c.n, 300300)
Esempio n. 21
0
    def set(self, instance, value, **kwargs):
        """
        Check if value is an actual FixedPoint value. If not, attempt to
        convert it to one; Raise an error if value is a float. Assign
        all properties passed as kwargs to object.

        field.set( FixedPoint(10))
        field.set( FixedPointInstance)

        """
        assert type(value) != type(0.00)

        if not value is None and not isinstance(value, FixedPoint):
            value = FixedPoint(value)

        ObjectField.set(self, instance, value, **kwargs)
Esempio n. 22
0
    def test__rsub__(self):
        """test subtraction as the right hand argument"""

        # test with a float
        a = FixedPoint(3.33)
        b = 1.11
        c = b - a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, DEFAULT_PRECISION)
        self.assertEquals(c.n, -222)

        a = SonOfFixedPoint(3.33)
        b = 1.11
        c = b - a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, DEFAULT_PRECISION)
        self.assertEquals(c.n, -222)
Esempio n. 23
0
    def test__radd__(self):
        """test addition as the right argument"""

        # test with a float
        a = FixedPoint(3.33)
        b = 3.3333
        c = b + a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, DEFAULT_PRECISION)
        self.assertEquals(c.n, 666L)


        # test subclass
        a = SonOfFixedPoint(3.33)
        b = 3.3333
        c = b + a
        self.assertEquals(type(c), type(a))
        self.assertEquals(c.precision, DEFAULT_PRECISION)
        self.assertEquals(c.n, 666L)
Esempio n. 24
0
def parseString(s):
    # remove commas
    s = s.replace(',', '')
    if not (money_re.match(s) or float_re.match(s)):
        raise InvalidMoneyString(s)
    if money_neg_re.match(s):
        # strip the brackets
        s = s[1:-1]
        cur, value = s.split()
        # add minus symbol to fixedpoint value
        s = '%s -%s' % (cur, value)
    if money_re.match(s):
        cur, value = s.split()
        if CURRENCIES.has_key(cur):
            cur = CURRENCIES[cur]
        elif SYMBOLS_MAP.has_key(cur):
            cur = SYMBOLS_MAP[cur]
    else:
        cur = None
        value = s
    return cur, FixedPoint(value)
Esempio n. 25
0
 def test__divmod__(self):
     """test integer division with modulo"""
     a = FixedPoint(3.33)
     q, m = divmod(a, 2)
     self.assertEquals(type(q), type(1L))
     self.assertEquals(type(m), type(a))
     self.assertEquals(q, 1)
     self.assertEquals(m, FixedPoint(1.33))
     
     a = SonOfFixedPoint(3.33)
     q, m = divmod(a, 2)
     self.assertEquals(type(q), type(1L))
     self.assertEquals(type(m), type(a))
     self.assertEquals(q, 1L)
     self.assertEquals(m, FixedPoint(1.33))
     
     a = FixedPoint(3.33)
     b = FixedPoint(1.11)
     q, m = divmod(a, b)
     self.assertEquals(type(q), type(1L))
     self.assertEquals(type(m), type(a))
     self.assertEquals(q, 3L)
     self.assertEquals(m, FixedPoint(0))
Esempio n. 26
0
 def test_ceil_negative_fraction_nearest_lower(self):
     a = FixedPoint(-496.6)
     b = ceil(a)
     self.assertIsInstance(b, int)
     self.assertEqual(b, -496)
Esempio n. 27
0
 def test_ceil_positive_fraction_nearest_upper(self):
     a = FixedPoint(496.6)
     b = ceil(a)
     self.assertIsInstance(b, int)
     self.assertEqual(b, 497)
Esempio n. 28
0
 def test_floor_positive_fraction_nearest_lower(self):
     a = FixedPoint(496.2)
     b = ceil(a)
     self.assertIsInstance(b, int)
     self.assertEqual(b, 497)
Esempio n. 29
0
 def test_ceil_integer(self):
     a = FixedPoint(496)
     b = ceil(a)
     self.assertIsInstance(b, int)
     self.assertEqual(b, 496)
Esempio n. 30
0
 def test_floor_negative_fraction_nearest_upper(self):
     a = FixedPoint(-496.2)
     b = floor(a)
     self.assertIsInstance(b, int)
     self.assertEqual(b, -497)
Esempio n. 31
0
 def test_equal_fraction_expecting_false(self):
     a = FixedPoint(0.1875)
     b = Fraction(5, 16)
     self.assertFalse(a == b)
Esempio n. 32
0
    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()))
Esempio n. 33
0
 def __init__(self, value=0, precision=DEFAULT_PRECISION):
     FixedPoint.__init__(self, value, precision)