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 $HOME/.gnuradio/unittests/python
118 # Run this is given a file name
119 if(filename is not None):
120 homepath = os.getenv("HOME")
121 basepath = homepath + "/.gnuradio"
122 path = homepath + "/.gnuradio/unittests/python"
125 if os.path.exists(basepath):
126 # only proceed if $HOME/.gnuradio is writable
127 st = os.stat(basepath)[stat.ST_MODE]
128 if(st & stat.S_IWUSR > 0):
129 # Test if path exists; if not, build it
130 if not os.path.exists(path):
131 os.makedirs(path, 0750)
133 # Just for safety: make sure we can write here, too
134 st = os.stat(path)[stat.ST_MODE]
135 if(st & stat.S_IWUSR > 0):
136 # Create an XML runner to filename
137 fout = file(path+"/"+filename, "w")
138 xmlrunner = gr_xmlrunner.XMLTestRunner(fout)
140 txtrunner = TextTestRunner(verbosity=1)
142 # Run the test; runner also creates XML output file
143 # FIXME: make xmlrunner output to screen so we don't have to do run and main
144 suite = TestLoader().loadTestsFromTestCase(PUT)
146 # use the xmlrunner if we can write the the directory
147 if(xmlrunner is not None):
152 # This will run and fail make check if problem
153 # but does not output to screen.
154 #main(testRunner = xmlrunner)
157 # If no filename is given, just run the test
161 ##############################################################################
162 # Executing this module from the command line
163 ##############################################################################
165 if __name__ == "__main__":