1+ #!/usr/bin/env python
2+ #
3+ # xferfcn_test.py - test TransferFunction class
4+ # RMM, 30 Mar 2011 (based on TestXferFcn from v0.4a)
5+
6+ import unittest
7+ import numpy as np
8+
9+ from numpy import int , int8 , int16 , int32 , int64
10+ from numpy import float , float16 , float32 , float64 , float128
11+ from numpy import all , ndarray , array
12+
13+ from control .xferfcn import cleanPart
14+
15+ class TestXferFcnInput (unittest .TestCase ):
16+ """These are tests for functionality of cleaning and validating
17+ XferFucnInput."""
18+
19+ # Tests for raising exceptions.
20+ def testBadInputType (self ):
21+ """Give the part cleaner invalid input type."""
22+
23+ self .assertRaises (TypeError , cleanPart , [[0. , 1. ], [2. , 3. ]])
24+
25+ def testBadInputType2 (self ):
26+ """Give the part cleaner another invalid input type."""
27+ self .assertRaises (TypeError , cleanPart , [1 ,"a" ])
28+
29+ def testScalar (self ):
30+ """Test single scalar value."""
31+ num = 1
32+ num_ = cleanPart (num )
33+
34+ assert isinstance (num_ , list )
35+ assert np .all ([isinstance (part , list ) for part in num_ ])
36+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
37+
38+ def testListScalar (self ):
39+ """Test single scalar value in list."""
40+ num = [1 ]
41+ num_ = cleanPart (num )
42+
43+ assert isinstance (num_ , list )
44+ assert np .all ([isinstance (part , list ) for part in num_ ])
45+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
46+
47+ def testTupleScalar (self ):
48+ """Test single scalar value in tuple."""
49+ num = (1 )
50+ num_ = cleanPart (num )
51+
52+ assert isinstance (num_ , list )
53+ assert np .all ([isinstance (part , list ) for part in num_ ])
54+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
55+
56+ def testList (self ):
57+ """Test multiple values in a list."""
58+ num = [1 , 2 ]
59+ num_ = cleanPart (num )
60+
61+ assert isinstance (num_ , list )
62+ assert np .all ([isinstance (part , list ) for part in num_ ])
63+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 2.0 ], dtype = float ))
64+
65+ def testTuple (self ):
66+ """Test multiple values in tuple."""
67+ num = (1 , 2 )
68+ num_ = cleanPart (num )
69+
70+ assert isinstance (num_ , list )
71+ assert np .all ([isinstance (part , list ) for part in num_ ])
72+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 2.0 ], dtype = float ))
73+
74+ def testAllScalarTypes (self ):
75+ """Test single scalar value for all valid data types."""
76+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
77+ num = dtype (1 )
78+ num_ = cleanPart (num )
79+
80+ assert isinstance (num_ , list )
81+ assert np .all ([isinstance (part , list ) for part in num_ ])
82+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
83+
84+ def testNpArray (self ):
85+ """Test multiple values in numpy array."""
86+ num = np .array ([1 , 2 ])
87+ num_ = cleanPart (num )
88+
89+ assert isinstance (num_ , list )
90+ assert np .all ([isinstance (part , list ) for part in num_ ])
91+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 2.0 ], dtype = float ))
92+
93+ def testAllNumpyArrayTypes (self ):
94+ """Test scalar value in numpy array of ndim=0 for all data types."""
95+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
96+ num = np .array (1 , dtype = dtype )
97+ num_ = cleanPart (num )
98+
99+ assert isinstance (num_ , list )
100+ assert np .all ([isinstance (part , list ) for part in num_ ])
101+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
102+
103+ def testAllNumpyArrayTypes2 (self ):
104+ """Test numpy array for all types."""
105+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
106+ num = np .array ([1 , 2 ], dtype = dtype )
107+ num_ = cleanPart (num )
108+
109+ assert isinstance (num_ , list )
110+ assert np .all ([isinstance (part , list ) for part in num_ ])
111+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 2.0 ], dtype = float ))
112+
113+ def testListAllTypes (self ):
114+ """Test list of a single value for all data types."""
115+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
116+ num = [dtype (1 )]
117+ num_ = cleanPart (num )
118+ assert isinstance (num_ , list )
119+ assert np .all ([isinstance (part , list ) for part in num_ ])
120+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
121+
122+ def testListAllTypes2 (self ):
123+ """List of list of numbers of all data types."""
124+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
125+ num = [dtype (1 ), dtype (2 )]
126+ num_ = cleanPart (num )
127+ assert isinstance (num_ , list )
128+ assert np .all ([isinstance (part , list ) for part in num_ ])
129+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 2.0 ], dtype = float ))
130+
131+ def testTupleAllTypes (self ):
132+ """Test tuple of a single value for all data types."""
133+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
134+ num = (dtype (1 ),)
135+ num_ = cleanPart (num )
136+ assert isinstance (num_ , list )
137+ assert np .all ([isinstance (part , list ) for part in num_ ])
138+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
139+
140+ def testTupleAllTypes2 (self ):
141+ """Test tuple of a single value for all data types."""
142+ for dtype in [int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 ]:
143+ num = (dtype (1 ), dtype (2 ))
144+ num_ = cleanPart (num )
145+ assert isinstance (num_ , list )
146+ assert np .all ([isinstance (part , list ) for part in num_ ])
147+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1 , 2 ], dtype = float ))
148+
149+ def testListListListInt (self ):
150+ """ Test an int in a list of a list of a list."""
151+ num = [[[1 ]]]
152+ num_ = cleanPart (num )
153+ assert isinstance (num_ , list )
154+ assert np .all ([isinstance (part , list ) for part in num_ ])
155+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
156+
157+ def testListListListFloat (self ):
158+ """ Test a float in a list of a list of a list."""
159+ num = [[[1.0 ]]]
160+ num_ = cleanPart (num )
161+ assert isinstance (num_ , list )
162+ assert np .all ([isinstance (part , list ) for part in num_ ])
163+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 ], dtype = float ))
164+
165+ def testListListListInts (self ):
166+ """Test 2 lists of ints in a list in a list."""
167+ num = [[[1 ,1 ],[2 ,2 ]]]
168+ num_ = cleanPart (num )
169+
170+ assert isinstance (num_ , list )
171+ assert np .all ([isinstance (part , list ) for part in num_ ])
172+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
173+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
174+
175+ def testListListListFloats (self ):
176+ """Test 2 lists of ints in a list in a list."""
177+ num = [[[1.0 ,1.0 ],[2.0 ,2.0 ]]]
178+ num_ = cleanPart (num )
179+
180+ assert isinstance (num_ , list )
181+ assert np .all ([isinstance (part , list ) for part in num_ ])
182+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
183+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
184+
185+ def testListListArray (self ):
186+ """List of list of numpy arrays for all valid types."""
187+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
188+ num = [[array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype )]]
189+ num_ = cleanPart (num )
190+
191+ assert isinstance (num_ , list )
192+ assert np .all ([isinstance (part , list ) for part in num_ ])
193+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
194+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
195+
196+ def testTupleListArray (self ):
197+ """Tuple of list of numpy arrays for all valid types."""
198+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
199+ num = ([array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype )],)
200+ num_ = cleanPart (num )
201+
202+ assert isinstance (num_ , list )
203+ assert np .all ([isinstance (part , list ) for part in num_ ])
204+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
205+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
206+
207+ def testListTupleArray (self ):
208+ """List of tuple of numpy array for all valid types."""
209+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
210+ num = [(array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype ))]
211+ num_ = cleanPart (num )
212+
213+ assert isinstance (num_ , list )
214+ assert np .all ([isinstance (part , list ) for part in num_ ])
215+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
216+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
217+
218+ def testTupleTuplesArrays (self ):
219+ """Tuple of tuples of numpy arrays for all valid types."""
220+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
221+ num = ((array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype )),
222+ (array ([3 ,4 ], dtype = dtype ),array ([4 ,4 ], dtype = dtype )))
223+ num_ = cleanPart (num )
224+
225+ assert isinstance (num_ , list )
226+ assert np .all ([isinstance (part , list ) for part in num_ ])
227+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
228+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
229+
230+ def testListTuplesArrays (self ):
231+ """List of tuples of numpy arrays for all valid types."""
232+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
233+ num = [(array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype )),
234+ (array ([3 ,4 ], dtype = dtype ),array ([4 ,4 ], dtype = dtype ))]
235+ num_ = cleanPart (num )
236+
237+ assert isinstance (num_ , list )
238+ assert np .all ([isinstance (part , list ) for part in num_ ])
239+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
240+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
241+
242+ def testListListArrays (self ):
243+ """List of list of numpy arrays for all valid types."""
244+ for dtype in int , int8 , int16 , int32 , int64 , float , float16 , float32 , float64 , float128 :
245+ num = [[array ([1 ,1 ], dtype = dtype ),array ([2 ,2 ], dtype = dtype )],
246+ [array ([3 ,3 ], dtype = dtype ),array ([4 ,4 ], dtype = dtype )]]
247+ num_ = cleanPart (num )
248+
249+ assert len (num_ ) == 2
250+ assert np .all ([isinstance (part , list ) for part in num_ ])
251+ assert np .all ([len (part ) == 2 for part in num_ ])
252+ np .testing .assert_array_equal (num_ [0 ][0 ], array ([1.0 , 1.0 ], dtype = float ))
253+ np .testing .assert_array_equal (num_ [0 ][1 ], array ([2.0 , 2.0 ], dtype = float ))
254+ np .testing .assert_array_equal (num_ [1 ][0 ], array ([3.0 , 3.0 ], dtype = float ))
255+ np .testing .assert_array_equal (num_ [1 ][1 ], array ([4.0 , 4.0 ], dtype = float ))
256+
257+ def suite ():
258+ return unittest .TestLoader ().loadTestsFromTestCase (TestXferFcnInput )
259+
260+ if __name__ == "__main__" :
261+ unittest .main ()
0 commit comments