3 # Copyright 2004 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
26 class TestCase(unittest.TestCase):
27 """A subclass of unittest.TestCase that adds additional assertions
29 Adds new methods assertComplexAlmostEqual,
30 assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
33 def assertComplexAlmostEqual (self, first, second, places=7, msg=None):
34 """Fail if the two complex objects are unequal as determined by their
35 difference rounded to the given number of decimal places
36 (default 7) and comparing to zero.
38 Note that decimal places (from zero) is usually not the same
39 as significant digits (measured from the most signficant digit).
41 if round(second.real-first.real, places) != 0:
42 raise self.failureException, \
43 (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
44 if round(second.imag-first.imag, places) != 0:
45 raise self.failureException, \
46 (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
48 def assertComplexAlmostEqual2 (self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
50 Fail if the two complex objects are unequal as determined by...
52 if abs(ref - x) < abs_eps:
55 if abs(ref) > abs_eps:
56 if abs(ref-x)/abs(ref) > rel_eps:
57 raise self.failureException, \
58 (msg or '%s != %s rel_error = %s rel_limit = %s' % (
59 `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
61 raise self.failureException, \
62 (msg or '%s != %s rel_error = %s rel_limit = %s' % (
63 `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
67 def assertComplexTuplesAlmostEqual (self, a, b, places=7, msg=None):
68 self.assertEqual (len(a), len(b))
69 for i in xrange (len(a)):
70 self.assertComplexAlmostEqual (a[i], b[i], places, msg)
72 def assertComplexTuplesAlmostEqual2 (self, ref, x,
73 abs_eps=1e-12, rel_eps=1e-6, msg=None):
74 self.assertEqual (len(ref), len(x))
75 for i in xrange (len(ref)):
77 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
78 except self.failureException, e:
79 #sys.stderr.write("index = %d " % (i,))
80 #sys.stderr.write("%s\n" % (e,))
83 def assertFloatTuplesAlmostEqual (self, a, b, places=7, msg=None):
84 self.assertEqual (len(a), len(b))
85 for i in xrange (len(a)):
86 self.assertAlmostEqual (a[i], b[i], places, msg)
89 def assertFloatTuplesAlmostEqual2 (self, ref, x,
90 abs_eps=1e-12, rel_eps=1e-6, msg=None):
91 self.assertEqual (len(ref), len(x))
92 for i in xrange (len(ref)):
94 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
95 except self.failureException, e:
96 #sys.stderr.write("index = %d " % (i,))
97 #sys.stderr.write("%s\n" % (e,))
101 TestResult = unittest.TestResult
102 TestSuite = unittest.TestSuite
103 FunctionTestCase = unittest.FunctionTestCase
104 TestLoader = unittest.TestLoader
105 TextTestRunner = unittest.TextTestRunner
106 TestProgram = unittest.TestProgram
109 ##############################################################################
110 # Executing this module from the command line
111 ##############################################################################
113 if __name__ == "__main__":