forked from gnuradio/newsched
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgr_unittest.py
175 lines (147 loc) · 5.85 KB
/
gr_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
#
# Copyright 2004,2010,2018 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#
"""
GNU radio specific extension of unittest.
"""
import time
import unittest
# We allow snakeCase here for consistency with unittest
# pylint: disable=invalid-name
class TestCase(unittest.TestCase):
"""A subclass of unittest.TestCase that adds additional assertions
Adds new methods assertComplexAlmostEqual,
assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
"""
def assertComplexAlmostEqual(self, first, second, places=7, msg=None):
"""Fail if the two complex objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
Note that decimal places (from zero) is usually not the same
as significant digits (measured from the most significant digit).
"""
if round(second.real-first.real, places) != 0:
raise self.failureException(
msg or '%r != %r within %r places' % (first, second, places))
if round(second.imag-first.imag, places) != 0:
raise self.failureException(
msg or '%r != %r within %r places' % (first, second, places)
)
def assertComplexAlmostEqual2(self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
"""
Fail if the two complex objects are unequal as determined by both
absolute delta (abs_eps) and relative delta (rel_eps).
"""
if abs(ref - x) < abs_eps:
return
if abs(ref) > abs_eps:
if abs(ref-x) / abs(ref) > rel_eps:
raise self.failureException(
msg or '%r != %r rel_error = %r rel_limit = %r' % (
ref, x, abs(ref-x) / abs(ref), rel_eps
)
)
else:
raise self.failureException(
msg or '%r != %r rel_error = %r rel_limit = %r' % (
ref, x, abs(ref-x) / abs(ref), rel_eps
)
)
def assertComplexTuplesAlmostEqual(self, a, b, places=7, msg=None):
"""
Fail if the two complex tuples are not approximately equal.
Approximate equality is determined by specifying the number of decimal
places.0
"""
self.assertEqual(len(a), len(b))
return all([
self.assertComplexAlmostEqual(x, y, places, msg)
for (x, y) in zip(a, b)
])
def assertComplexTuplesAlmostEqual2(self, a, b,
abs_eps=1e-12, rel_eps=1e-6, msg=None):
"""
Fail if the two complex tuples are not approximately equal.
Approximate equality is determined by calling assertComplexAlmostEqual().
"""
self.assertEqual(len(a), len(b))
return all([
self.assertComplexAlmostEqual2(x, y, abs_eps, rel_eps, msg)
for (x, y) in zip(a, b)
])
def assertFloatTuplesAlmostEqual(self, a, b, places=7, msg=None):
"""
Fail if the two real-valued tuples are not approximately equal.
Approximate equality is determined by specifying the number of decimal
places.
"""
self.assertEqual(len(a), len(b))
return all([
self.assertAlmostEqual(x, y, places, msg)
for (x, y) in zip(a, b)
])
def assertFloatTuplesAlmostEqual2(self, a, b,
abs_eps=1e-12, rel_eps=1e-6, msg=None):
self.assertEqual(len(a), len(b))
return all([
self.assertComplexAlmostEqual2(x, y, abs_eps, rel_eps, msg)
for (x, y) in zip(a, b)
])
def waitFor(
self,
condition,
timeout=5.0,
poll_interval=0.2,
fail_on_timeout=True,
fail_msg=None):
"""
Helper function: Wait for a callable to return True within a given
timeout.
This is useful for running tests where an exact wait time is not known.
Arguments:
- condition: A callable. Must return True when a 'good' condition is met.
- timeout: Timeout in seconds. `condition` must return True within this
timeout.
- poll_interval: Time between calls to condition() in seconds
- fail_on_timeout: If True, the test case will fail when the timeout
occurs. If False, this function will return False in
that case.
- fail_msg: The message that is printed when a timeout occurs and
fail_on_timeout is true.
"""
if not callable(condition):
self.fail("Invalid condition provided to waitFor()!")
stop_time = time.monotonic() + timeout
while time.monotonic() <= stop_time:
if condition():
return True
time.sleep(poll_interval)
if fail_on_timeout:
fail_msg = fail_msg or "Timeout exceeded during call to waitFor()!"
self.fail(fail_msg)
return False
TestResult = unittest.TestResult
TestSuite = unittest.TestSuite
FunctionTestCase = unittest.FunctionTestCase
TestLoader = unittest.TestLoader
TextTestRunner = unittest.TextTestRunner
TestProgram = unittest.TestProgram
main = TestProgram
def run(PUT, verbosity=1):
'''
Runs the unittest on a TestCase
PUT: the program under test and should be a gr_unittest.TestCase
filename: This argument is here for historical reasons.
'''
main(verbosity=verbosity)
##############################################################################
# Executing this module from the command line
##############################################################################
if __name__ == "__main__":
main(module=None)