29a73194313412f5a8500a8871e144da0bd03dc2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr_unittest.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2010 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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)
10 # any later version.
11
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.
16
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.
21
22
23 import unittest
24 import gr_xmlrunner
25 import sys, os, stat
26
27 class TestCase(unittest.TestCase):
28     """A subclass of unittest.TestCase that adds additional assertions
29
30     Adds new methods assertComplexAlmostEqual,
31     assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
32     """
33
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.
38
39            Note that decimal places (from zero) is usually not the same
40            as significant digits (measured from the most signficant digit).
41         """
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` ))
48         
49     def assertComplexAlmostEqual2 (self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
50         """
51         Fail if the two complex objects are unequal as determined by...
52         """
53         if abs(ref - x) < abs_eps:
54             return
55
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` ))
61         else:
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` ))
65
66
67         
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)
72
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)):
77             try:
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,))
82                 raise
83
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)
88
89
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)):
94             try:
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,))
99                 raise
100
101
102 TestResult = unittest.TestResult
103 TestSuite = unittest.TestSuite
104 FunctionTestCase = unittest.FunctionTestCase
105 TestLoader = unittest.TestLoader
106 TextTestRunner = unittest.TextTestRunner
107 TestProgram = unittest.TestProgram
108 main = TestProgram
109
110 def run(PUT, filename=None):
111     ''' 
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
116     '''
117
118     # Run this is given a file name
119     if(filename is not None):
120         path = os.getenv("HOME") + "/.gnuradio/unittests/python"
121
122         # Test if path exists; if not, build it
123         try:
124             st = os.stat(path)
125         except OSError:
126             os.makedirs(path, 0750)
127
128         # Create an XML runner to filename
129         fout = file(path+"/"+filename, "w")
130         runner = gr_xmlrunner.XMLTestRunner(fout)
131
132         # Run the test; runner also creates XML output file
133         suite = TestLoader().loadTestsFromTestCase(PUT)
134         runner.run(suite)
135     else:
136         # If no filename is given, just run the test
137         main()
138
139
140 ##############################################################################
141 # Executing this module from the command line
142 ##############################################################################
143
144 if __name__ == "__main__":
145     main(module=None)