#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_numeric.py $
$Id: test_numeric.py 24664 2004-07-12 16:20:31Z dbinger $
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.numeric import near_cmp

tested_modules = [ "dulcinea.numeric" ]


class CompareTest (TestScenario):

    def check_nonfloat (self):
        self.test_val("near_cmp(1, 2)", -1)
        self.test_val("near_cmp(2, 1)", +1)
        self.test_val("near_cmp(2, 2)", 0)

        self.test_true("near_cmp('foo', 'bar') > 0")
        self.test_true("near_cmp('bar', 'foo') < 0")
        self.test_false("near_cmp('foo', 'foo')")

    def check_float (self):
        self.test_val("near_cmp(1.0, 1.0)", 0)
        self.test_val("near_cmp(1.0, 1.00001)", -1)
        self.test_val("near_cmp(1.000001, 1.0)", +1)

        self.test_val("near_cmp(1.0e-9, 1.0e-9)", 0)
        self.test_val("near_cmp(1.0e-9, 1.00001e-9)", -1)
        self.test_val("near_cmp(1.000001e-9, 1.0e-9)", +1)

        self.test_val("near_cmp(10.0, -10.0)", +1)
        self.test_val("near_cmp(-10.0, 10.0)", -1)
        self.test_val("near_cmp(-10.0, -10.0)", 0)
        self.test_val("near_cmp(-10.0, -20.0)", +1)
        self.test_val("near_cmp(-20.0, -10.0)", -1)

        # provoke some round-off error
        v1 = 1000.0
        v2 = 1.0e-3 / 1.0e-6
        self.test_true("abs(v1 - v2) > 0") # be sure there is round-off
        self.test_true("v1 != v2")      # another way of doing that
        self.test_val("near_cmp(v1, v2)", 0)
        self.test_val("near_cmp(v2, v1)", 0)

    def check_zero (self):
        small = 1e-6
        very_small = 1e-18

        self.test_val("near_cmp(0.0, 0.0)", 0)
        self.test_val("near_cmp(0.0, small)", -1)
        self.test_val("near_cmp(0.0,-small)", +1)
        self.test_val("near_cmp(0.0, very_small)", 0)
        self.test_val("near_cmp(0.0, -very_small)", 0)

    def check_mixed (self):
        self.test_val("near_cmp(1, 1.0)", 0)
        self.test_val("near_cmp(1.0, 1)", 0)
        self.test_val("near_cmp(1, 1.00001)", -1)
        self.test_val("near_cmp(1.000001, 1)", +1)

        v1 = 1000
        v2 = 1.0e-3 / 1.0e-6
        self.test_val("near_cmp(v1, v2)", 0)
        self.test_val("near_cmp(v2, v1)", 0)


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
