Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr_unittest.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 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 2, 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 sys
25
26 class TestCase(unittest.TestCase):
27     """A subclass of unittest.TestCase that adds additional assertions
28
29     Adds new methods assertComplexAlmostEqual,
30     assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
31     """
32
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.
37
38            Note that decimal places (from zero) is usually not the same
39            as significant digits (measured from the most signficant digit).
40         """
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` ))
47         
48     def assertComplexAlmostEqual2 (self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
49         """
50         Fail if the two complex objects are unequal as determined by...
51         """
52         if abs(ref - x) < abs_eps:
53             return
54
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` ))
60         else:
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` ))
64
65
66         
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)
71
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)):
76             try:
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,))
81                 raise
82
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)
87
88
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)):
93             try:
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,))
98                 raise
99
100
101 TestResult = unittest.TestResult
102 TestSuite = unittest.TestSuite
103 FunctionTestCase = unittest.FunctionTestCase
104 TestLoader = unittest.TestLoader
105 TextTestRunner = unittest.TextTestRunner
106 TestProgram = unittest.TestProgram
107 main = TestProgram
108
109 ##############################################################################
110 # Executing this module from the command line
111 ##############################################################################
112
113 if __name__ == "__main__":
114     main(module=None)