160 |
file.close() |
file.close() |
161 |
|
|
162 |
|
|
163 |
class FloatComparisonMixin: |
class FloatComparisonMixin(unittest.TestCase): |
164 |
|
|
165 |
""" |
""" |
166 |
Mixin class for tests comparing floating point numbers. |
Mixin class for tests comparing floating point numbers. |
170 |
""" |
""" |
171 |
|
|
172 |
fp_epsilon = 1e-6 |
fp_epsilon = 1e-6 |
173 |
|
fp_inf = float('1e1000') # FIXME: hack for infinite |
174 |
|
|
175 |
def assertFloatEqual(self, test, value): |
def assertFloatEqual(self, test, value): |
176 |
"""Assert equality of test and value with some tolerance. |
"""Assert equality of test and value with some tolerance. |
178 |
Assert that the absolute difference between test and value is |
Assert that the absolute difference between test and value is |
179 |
less than self.fp_epsilon. |
less than self.fp_epsilon. |
180 |
""" |
""" |
181 |
self.assert_(self.fp_epsilon > abs(test - value), |
if abs(test) == self.fp_inf: |
182 |
|
self.assertEqual(test, value) |
183 |
|
else: |
184 |
|
self.assert_(self.fp_epsilon > abs(test - value), |
185 |
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
186 |
|
|
187 |
def assertFloatSeqEqual(self, test, value, epsilon = None): |
def assertFloatSeqEqual(self, test, value, epsilon = None): |
238 |
"""Check whether the messages received match the list messages""" |
"""Check whether the messages received match the list messages""" |
239 |
self.assertEquals(messages, self.received_messages) |
self.assertEquals(messages, self.received_messages) |
240 |
|
|
|
class FloatTestCase(unittest.TestCase): |
|
|
"""TestCase with methods for testing floating point values""" |
|
|
|
|
|
fp_epsilon = 1e-6 |
|
|
fp_inf = float('1e1000') # FIXME: hack for infinite |
|
|
|
|
|
def assertFloatEqual(self, first, second, msg=None): |
|
|
"""Fail if one float is greater than the other + fp_epsilon""" |
|
|
if abs(first) == self.fp_inf: |
|
|
self.assertEqual(first, second, msg) |
|
|
else: |
|
|
self.assert_(self.fp_epsilon > abs(first - second), msg) |
|
|
|
|