3 # Copyright 2004,2010 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.
27 class TestCase(unittest.TestCase):
28 """A subclass of unittest.TestCase that adds additional assertions
30 Adds new methods assertComplexAlmostEqual,
31 assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
34 def assertComplexAlmostEqual (self, first, second, places=7, msg=None):
35 """Fail if the two complex objects are unequal as determined by their
36 difference rounded to the given number of decimal places
37 (default 7) and comparing to zero.
39 Note that decimal places (from zero) is usually not the same
40 as significant digits (measured from the most signficant digit).
42 if round(second.real-first.real, places) != 0:
43 raise self.failureException, \
44 (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
45 if round(second.imag-first.imag, places) != 0:
46 raise self.failureException, \
47 (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
49 def assertComplexAlmostEqual2 (self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
51 Fail if the two complex objects are unequal as determined by...
53 if abs(ref - x) < abs_eps:
56 if abs(ref) > abs_eps:
57 if abs(ref-x)/abs(ref) > rel_eps:
58 raise self.failureException, \
59 (msg or '%s != %s rel_error = %s rel_limit = %s' % (
60 `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
62 raise self.failureException, \
63 (msg or '%s != %s rel_error = %s rel_limit = %s' % (
64 `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
68 def assertComplexTuplesAlmostEqual (self, a, b, places=7, msg=None):
69 self.assertEqual (len(a), len(b))
70 for i in xrange (len(a)):
71 self.assertComplexAlmostEqual (a[i], b[i], places, msg)
73 def assertComplexTuplesAlmostEqual2 (self, ref, x,
74 abs_eps=1e-12, rel_eps=1e-6, msg=None):
75 self.assertEqual (len(ref), len(x))
76 for i in xrange (len(ref)):
78 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
79 except self.failureException, e:
80 #sys.stderr.write("index = %d " % (i,))
81 #sys.stderr.write("%s\n" % (e,))
84 def assertFloatTuplesAlmostEqual (self, a, b, places=7, msg=None):
85 self.assertEqual (len(a), len(b))
86 for i in xrange (len(a)):
87 self.assertAlmostEqual (a[i], b[i], places, msg)
90 def assertFloatTuplesAlmostEqual2 (self, ref, x,
91 abs_eps=1e-12, rel_eps=1e-6, msg=None):
92 self.assertEqual (len(ref), len(x))
93 for i in xrange (len(ref)):
95 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
96 except self.failureException, e:
97 #sys.stderr.write("index = %d " % (i,))
98 #sys.stderr.write("%s\n" % (e,))
102 TestResult = unittest.TestResult
103 TestSuite = unittest.TestSuite
104 FunctionTestCase = unittest.FunctionTestCase
105 TestLoader = unittest.TestLoader
106 TextTestRunner = unittest.TextTestRunner
107 TestProgram = unittest.TestProgram
110 def run(PUT, filename=None):
112 Runs the unittest on a TestCase and produces an optional XML report
113 PUT: the program under test and should be a gr_unittest.TestCase
114 filename: an optional filename to save the XML report of the tests
115 this will live in ./.unittests/python
118 # Run this is given a file name
119 if(filename is not None):
120 basepath = "./.unittests"
121 path = basepath + "/python"
123 if not os.path.exists(basepath):
124 os.makedirs(basepath, 0750)
127 # only proceed if .unittests is writable
128 st = os.stat(basepath)[stat.ST_MODE]
129 if(st & stat.S_IWUSR > 0):
130 # Test if path exists; if not, build it
131 if not os.path.exists(path):
132 os.makedirs(path, 0750)
134 # Just for safety: make sure we can write here, too
135 st = os.stat(path)[stat.ST_MODE]
136 if(st & stat.S_IWUSR > 0):
137 # Create an XML runner to filename
138 fout = file(path+"/"+filename, "w")
139 xmlrunner = gr_xmlrunner.XMLTestRunner(fout)
141 txtrunner = TextTestRunner(verbosity=1)
143 # Run the test; runner also creates XML output file
144 # FIXME: make xmlrunner output to screen so we don't have to do run and main
145 suite = TestLoader().loadTestsFromTestCase(PUT)
147 # use the xmlrunner if we can write the the directory
148 if(xmlrunner is not None):
153 # This will run and fail make check if problem
154 # but does not output to screen.
155 #main(testRunner = xmlrunner)
158 # If no filename is given, just run the test
162 ##############################################################################
163 # Executing this module from the command line
164 ##############################################################################
166 if __name__ == "__main__":