forked from python-control/python-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxferfcn_input_test.py
More file actions
263 lines (211 loc) · 11.7 KB
/
Copy pathxferfcn_input_test.py
File metadata and controls
263 lines (211 loc) · 11.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
#
# xferfcn_input_test.py - test inputs to TransferFunction class
# jed-frey, 18 Feb 2017 (based on xferfcn_test.py)
import unittest
import numpy as np
from numpy import int, int8, int16, int32, int64
from numpy import float, float16, float32, float64, float128
from numpy import all, ndarray, array
from control.xferfcn import _clean_part
class TestXferFcnInput(unittest.TestCase):
"""These are tests for functionality of cleaning and validating XferFcnInput."""
# Tests for raising exceptions.
def test_clean_part_bad_input_type(self):
"""Give the part cleaner invalid input type."""
self.assertRaises(TypeError, _clean_part, [[0., 1.], [2., 3.]])
def test_clean_part_bad_input_type2(self):
"""Give the part cleaner another invalid input type."""
self.assertRaises(TypeError, _clean_part, [1, "a"])
def test_clean_part_scalar(self):
"""Test single scalar value."""
num = 1
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_list_scalar(self):
"""Test single scalar value in list."""
num = [1]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_tuple_scalar(self):
"""Test single scalar value in tuple."""
num = (1)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_list(self):
"""Test multiple values in a list."""
num = [1, 2]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
def test_clean_part_tuple(self):
"""Test multiple values in tuple."""
num = (1, 2)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
def test_clean_part_all_scalar_types(self):
"""Test single scalar value for all valid data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = dtype(1)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_np_array(self):
"""Test multiple values in numpy array."""
num = np.array([1, 2])
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
def test_clean_part_all_np_array_types(self):
"""Test scalar value in numpy array of ndim=0 for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = np.array(1, dtype=dtype)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_all_np_array_types2(self):
"""Test numpy array for all types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = np.array([1, 2], dtype=dtype)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
def test_clean_part_list_all_types(self):
"""Test list of a single value for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = [dtype(1)]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_list_all_types2(self):
"""List of list of numbers of all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = [dtype(1), dtype(2)]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
def test_clean_part_tuple_all_types(self):
"""Test tuple of a single value for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = (dtype(1),)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_tuple_all_types2(self):
"""Test tuple of a single value for all data types."""
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]:
num = (dtype(1), dtype(2))
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1, 2], dtype=float))
def test_clean_part_list_list_list_int(self):
""" Test an int in a list of a list of a list."""
num = [[[1]]]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_list_list_list_float(self):
""" Test a float in a list of a list of a list."""
num = [[[1.0]]]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
def test_clean_part_list_list_list_ints(self):
"""Test 2 lists of ints in a list in a list."""
num = [[[1, 1], [2, 2]]]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_list_list_list_floats(self):
"""Test 2 lists of ints in a list in a list."""
num = [[[1.0, 1.0], [2.0, 2.0]]]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_list_list_array(self):
"""List of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = [[array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)]]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_tuple_list_array(self):
"""Tuple of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = ([array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)],)
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_list_tuple_array(self):
"""List of tuple of numpy array for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = [(array([1, 1], dtype=dtype), array([2, 2], dtype=dtype))]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_tuple_tuples_arrays(self):
"""Tuple of tuples of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = ((array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)),
(array([3, 4], dtype=dtype), array([4, 4], dtype=dtype)))
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_list_tuples_arrays(self):
"""List of tuples of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = [(array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)),
(array([3, 4], dtype=dtype), array([4, 4], dtype=dtype))]
num_ = _clean_part(num)
assert isinstance(num_, list)
assert np.all([isinstance(part, list) for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
def test_clean_part_list_list_arrays(self):
"""List of list of numpy arrays for all valid types."""
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128:
num = [[array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)],
[array([3, 3], dtype=dtype), array([4, 4], dtype=dtype)]]
num_ = _clean_part(num)
assert len(num_) == 2
assert np.all([isinstance(part, list) for part in num_])
assert np.all([len(part) == 2 for part in num_])
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float))
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
np.testing.assert_array_equal(num_[1][0], array([3.0, 3.0], dtype=float))
np.testing.assert_array_equal(num_[1][1], array([4.0, 4.0], dtype=float))
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestXferFcnInput)
if __name__ == "__main__":
unittest.main()