Question unit testing numerical algorithms

Brian O. Bush bushbo at attbi.com
Sun Dec 8 15:58:28 EST 2002


All. I have a basic question on testing numerical algorithms. In my
unit test code, I have two intervals, A = Interval(3.52, 5.83) and B =
Interval(-2.24, 6.04), and their SUM = Interval(1.28, 11.87). However,
when run there is a significantly small enough error to allow failure
in the assertion A+B == SUM.

How can I in general unit test algorithms and allow for a small enough
error? Thanks, Brian


import unittest

class Interval:
    def __init__(self, lower=0, upper=0):
        self.lower = lower
        self.upper = upper
    def __add__(self, other):
        return Interval(self.lower + other.lower, \
                        self.upper + other.upper)
    def __eq__(self, other):
        return self.lower == other.lower and \
               self.upper == other.upper
    def __str__(self):
        return "(%g,%g)" % (self.lower, self.upper)

class Tests(unittest.TestCase):
    def testAddition(self):
        A = Interval(3.52, 5.83)
        B = Interval(-2.24, 6.04)
        SUM = Interval(1.28, 11.87)
        print A + B
        print SUM
        assert A + B == SUM

def main():
   unittest.TextTestRunner().run(unittest.makeSuite(Tests, 'test'))

if __name__ == '__main__':
    main()



More information about the Python-list mailing list