Debian Codegraph @

tools/swig/test/testArray.py

raw
1 #!/usr/bin/env python3
2 import sys
3 import unittest
4
5 import numpy as np
6
7 major, minor = [int(d) for d in np.__version__.split(".")[:2]]
8 if major == 0:
9 BadListError = TypeError
10 else:
11 BadListError = ValueError
12
13 import Array
14
15 ######################################################################
16
17 class Array1TestCase(unittest.TestCase):
18
19 def setUp(self):
20 self.length = 5
21 self.array1 = Array.Array1(self.length)
22
23 def testConstructor0(self):
24 "Test Array1 default constructor"
25 a = Array.Array1()
26 self.assertTrue(isinstance(a, Array.Array1))
27 self.assertTrue(len(a) == 0)
28
29 def testConstructor1(self):
30 "Test Array1 length constructor"
31 self.assertTrue(isinstance(self.array1, Array.Array1))
32
33 def testConstructor2(self):
34 "Test Array1 array constructor"
35 na = np.arange(self.length)
36 aa = Array.Array1(na)
37 self.assertTrue(isinstance(aa, Array.Array1))
38
39 def testConstructor3(self):
40 "Test Array1 copy constructor"
41 for i in range(self.array1.length()):
42 self.array1[i] = i
43 arrayCopy = Array.Array1(self.array1)
44 self.assertTrue(arrayCopy == self.array1)
45
46 def testConstructorBad(self):
47 "Test Array1 length constructor, negative"
48 self.assertRaises(ValueError, Array.Array1, -4)
49
50 def testLength(self):
51 "Test Array1 length method"
52 self.assertTrue(self.array1.length() == self.length)
53
54 def testLen(self):
55 "Test Array1 __len__ method"
56 self.assertTrue(len(self.array1) == self.length)
57
58 def testResize0(self):
59 "Test Array1 resize method, length"
60 newLen = 2 * self.length
61 self.array1.resize(newLen)
62 self.assertTrue(len(self.array1) == newLen)
63
64 def testResize1(self):
65 "Test Array1 resize method, array"
66 a = np.zeros((2 * self.length,), dtype='l')
67 self.array1.resize(a)
68 self.assertTrue(len(self.array1) == a.size)
69
70 def testResizeBad(self):
71 "Test Array1 resize method, negative length"
72 self.assertRaises(ValueError, self.array1.resize, -5)
73
74 def testSetGet(self):
75 "Test Array1 __setitem__, __getitem__ methods"
76 n = self.length
77 for i in range(n):
78 self.array1[i] = i * i
79 for i in range(n):
80 self.assertTrue(self.array1[i] == i * i)
81
82 def testSetBad1(self):
83 "Test Array1 __setitem__ method, negative index"
84 self.assertRaises(IndexError, self.array1.__setitem__, -1, 0)
85
86 def testSetBad2(self):
87 "Test Array1 __setitem__ method, out-of-range index"
88 self.assertRaises(IndexError, self.array1.__setitem__, self.length + 1, 0)
89
90 def testGetBad1(self):
91 "Test Array1 __getitem__ method, negative index"
92 self.assertRaises(IndexError, self.array1.__getitem__, -1)
93
94 def testGetBad2(self):
95 "Test Array1 __getitem__ method, out-of-range index"
96 self.assertRaises(IndexError, self.array1.__getitem__, self.length + 1)
97
98 def testAsString(self):
99 "Test Array1 asString method"
100 for i in range(self.array1.length()):
101 self.array1[i] = i + 1
102 self.assertTrue(self.array1.asString() == "[ 1, 2, 3, 4, 5 ]")
103
104 def testStr(self):
105 "Test Array1 __str__ method"
106 for i in range(self.array1.length()):
107 self.array1[i] = i - 2
108 self.assertTrue(str(self.array1) == "[ -2, -1, 0, 1, 2 ]")
109
110 def testView(self):
111 "Test Array1 view method"
112 for i in range(self.array1.length()):
113 self.array1[i] = i + 1
114 a = self.array1.view()
115 self.assertTrue(isinstance(a, np.ndarray))
116 self.assertTrue(len(a) == self.length)
117 self.assertTrue((a == [1, 2, 3, 4, 5]).all())
118
119 ######################################################################
120
121 class Array2TestCase(unittest.TestCase):
122
123 def setUp(self):
124 self.nrows = 5
125 self.ncols = 4
126 self.array2 = Array.Array2(self.nrows, self.ncols)
127
128 def testConstructor0(self):
129 "Test Array2 default constructor"
130 a = Array.Array2()
131 self.assertTrue(isinstance(a, Array.Array2))
132 self.assertTrue(len(a) == 0)
133
134 def testConstructor1(self):
135 "Test Array2 nrows, ncols constructor"
136 self.assertTrue(isinstance(self.array2, Array.Array2))
137
138 def testConstructor2(self):
139 "Test Array2 array constructor"
140 na = np.zeros((3, 4), dtype="l")
141 aa = Array.Array2(na)
142 self.assertTrue(isinstance(aa, Array.Array2))
143
144 def testConstructor3(self):
145 "Test Array2 copy constructor"
146 for i in range(self.nrows):
147 for j in range(self.ncols):
148 self.array2[i][j] = i * j
149 arrayCopy = Array.Array2(self.array2)
150 self.assertTrue(arrayCopy == self.array2)
151
152 def testConstructorBad1(self):
153 "Test Array2 nrows, ncols constructor, negative nrows"
154 self.assertRaises(ValueError, Array.Array2, -4, 4)
155
156 def testConstructorBad2(self):
157 "Test Array2 nrows, ncols constructor, negative ncols"
158 self.assertRaises(ValueError, Array.Array2, 4, -4)
159
160 def testNrows(self):
161 "Test Array2 nrows method"
162 self.assertTrue(self.array2.nrows() == self.nrows)
163
164 def testNcols(self):
165 "Test Array2 ncols method"
166 self.assertTrue(self.array2.ncols() == self.ncols)
167
168 def testLen(self):
169 "Test Array2 __len__ method"
170 self.assertTrue(len(self.array2) == self.nrows * self.ncols)
171
172 def testResize0(self):
173 "Test Array2 resize method, size"
174 newRows = 2 * self.nrows
175 newCols = 2 * self.ncols
176 self.array2.resize(newRows, newCols)
177 self.assertTrue(len(self.array2) == newRows * newCols)
178
179 def testResize1(self):
180 "Test Array2 resize method, array"
181 a = np.zeros((2 * self.nrows, 2 * self.ncols), dtype='l')
182 self.array2.resize(a)
183 self.assertTrue(len(self.array2) == a.size)
184
185 def testResizeBad1(self):
186 "Test Array2 resize method, negative nrows"
187 self.assertRaises(ValueError, self.array2.resize, -5, 5)
188
189 def testResizeBad2(self):
190 "Test Array2 resize method, negative ncols"
191 self.assertRaises(ValueError, self.array2.resize, 5, -5)
192
193 def testSetGet1(self):
194 "Test Array2 __setitem__, __getitem__ methods"
195 m = self.nrows
196 n = self.ncols
197 array1 = []
198 a = np.arange(n, dtype="l")
199 for i in range(m):
200 array1.append(Array.Array1(i * a))
201 for i in range(m):
202 self.array2[i] = array1[i]
203 for i in range(m):
204 self.assertTrue(self.array2[i] == array1[i])
205
206 def testSetGet2(self):
207 "Test Array2 chained __setitem__, __getitem__ methods"
208 m = self.nrows
209 n = self.ncols
210 for i in range(m):
211 for j in range(n):
212 self.array2[i][j] = i * j
213 for i in range(m):
214 for j in range(n):
215 self.assertTrue(self.array2[i][j] == i * j)
216
217 def testSetBad1(self):
218 "Test Array2 __setitem__ method, negative index"
219 a = Array.Array1(self.ncols)
220 self.assertRaises(IndexError, self.array2.__setitem__, -1, a)
221
222 def testSetBad2(self):
223 "Test Array2 __setitem__ method, out-of-range index"
224 a = Array.Array1(self.ncols)
225 self.assertRaises(IndexError, self.array2.__setitem__, self.nrows + 1, a)
226
227 def testGetBad1(self):
228 "Test Array2 __getitem__ method, negative index"
229 self.assertRaises(IndexError, self.array2.__getitem__, -1)
230
231 def testGetBad2(self):
232 "Test Array2 __getitem__ method, out-of-range index"
233 self.assertRaises(IndexError, self.array2.__getitem__, self.nrows + 1)
234
235 def testAsString(self):
236 "Test Array2 asString method"
237 result = """\
238 [ [ 0, 1, 2, 3 ],
239 [ 1, 2, 3, 4 ],
240 [ 2, 3, 4, 5 ],
241 [ 3, 4, 5, 6 ],
242 [ 4, 5, 6, 7 ] ]
243 """
244 for i in range(self.nrows):
245 for j in range(self.ncols):
246 self.array2[i][j] = i + j
247 self.assertTrue(self.array2.asString() == result)
248
249 def testStr(self):
250 "Test Array2 __str__ method"
251 result = """\
252 [ [ 0, -1, -2, -3 ],
253 [ 1, 0, -1, -2 ],
254 [ 2, 1, 0, -1 ],
255 [ 3, 2, 1, 0 ],
256 [ 4, 3, 2, 1 ] ]
257 """
258 for i in range(self.nrows):
259 for j in range(self.ncols):
260 self.array2[i][j] = i - j
261 self.assertTrue(str(self.array2) == result)
262
263 def testView(self):
264 "Test Array2 view method"
265 a = self.array2.view()
266 self.assertTrue(isinstance(a, np.ndarray))
267 self.assertTrue(len(a) == self.nrows)
268
269 ######################################################################
270
271 class ArrayZTestCase(unittest.TestCase):
272
273 def setUp(self):
274 self.length = 5
275 self.array3 = Array.ArrayZ(self.length)
276
277 def testConstructor0(self):
278 "Test ArrayZ default constructor"
279 a = Array.ArrayZ()
280 self.assertTrue(isinstance(a, Array.ArrayZ))
281 self.assertTrue(len(a) == 0)
282
283 def testConstructor1(self):
284 "Test ArrayZ length constructor"
285 self.assertTrue(isinstance(self.array3, Array.ArrayZ))
286
287 def testConstructor2(self):
288 "Test ArrayZ array constructor"
289 na = np.arange(self.length, dtype=np.complex128)
290 aa = Array.ArrayZ(na)
291 self.assertTrue(isinstance(aa, Array.ArrayZ))
292
293 def testConstructor3(self):
294 "Test ArrayZ copy constructor"
295 for i in range(self.array3.length()):
296 self.array3[i] = complex(i, -i)
297 arrayCopy = Array.ArrayZ(self.array3)
298 self.assertTrue(arrayCopy == self.array3)
299
300 def testConstructorBad(self):
301 "Test ArrayZ length constructor, negative"
302 self.assertRaises(ValueError, Array.ArrayZ, -4)
303
304 def testLength(self):
305 "Test ArrayZ length method"
306 self.assertTrue(self.array3.length() == self.length)
307
308 def testLen(self):
309 "Test ArrayZ __len__ method"
310 self.assertTrue(len(self.array3) == self.length)
311
312 def testResize0(self):
313 "Test ArrayZ resize method, length"
314 newLen = 2 * self.length
315 self.array3.resize(newLen)
316 self.assertTrue(len(self.array3) == newLen)
317
318 def testResize1(self):
319 "Test ArrayZ resize method, array"
320 a = np.zeros((2 * self.length,), dtype=np.complex128)
321 self.array3.resize(a)
322 self.assertTrue(len(self.array3) == a.size)
323
324 def testResizeBad(self):
325 "Test ArrayZ resize method, negative length"
326 self.assertRaises(ValueError, self.array3.resize, -5)
327
328 def testSetGet(self):
329 "Test ArrayZ __setitem__, __getitem__ methods"
330 n = self.length
331 for i in range(n):
332 self.array3[i] = i * i
333 for i in range(n):
334 self.assertTrue(self.array3[i] == i * i)
335
336 def testSetBad1(self):
337 "Test ArrayZ __setitem__ method, negative index"
338 self.assertRaises(IndexError, self.array3.__setitem__, -1, 0)
339
340 def testSetBad2(self):
341 "Test ArrayZ __setitem__ method, out-of-range index"
342 self.assertRaises(IndexError, self.array3.__setitem__, self.length + 1, 0)
343
344 def testGetBad1(self):
345 "Test ArrayZ __getitem__ method, negative index"
346 self.assertRaises(IndexError, self.array3.__getitem__, -1)
347
348 def testGetBad2(self):
349 "Test ArrayZ __getitem__ method, out-of-range index"
350 self.assertRaises(IndexError, self.array3.__getitem__, self.length + 1)
351
352 def testAsString(self):
353 "Test ArrayZ asString method"
354 for i in range(self.array3.length()):
355 self.array3[i] = complex(i + 1, -i - 1)
356 self.assertTrue(self.array3.asString() ==
357 "[ (1,-1), (2,-2), (3,-3), (4,-4), (5,-5) ]")
358
359 def testStr(self):
360 "Test ArrayZ __str__ method"
361 for i in range(self.array3.length()):
362 self.array3[i] = complex(i - 2, (i - 2) * 2)
363 self.assertTrue(str(self.array3) == "[ (-2,-4), (-1,-2), (0,0), (1,2), (2,4) ]")
364
365 def testView(self):
366 "Test ArrayZ view method"
367 for i in range(self.array3.length()):
368 self.array3[i] = complex(i + 1, i + 2)
369 a = self.array3.view()
370 self.assertTrue(isinstance(a, np.ndarray))
371 self.assertTrue(len(a) == self.length)
372 self.assertTrue((a == [1 + 2j, 2 + 3j, 3 + 4j, 4 + 5j, 5 + 6j]).all())
373
374 ######################################################################
375
376
377 if __name__ == "__main__":
378
379 # Build the test suite
380 suite = unittest.TestSuite()
381 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Array1TestCase))
382 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Array2TestCase))
383 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ArrayZTestCase))
384
385 # Execute the test suite
386 print("Testing Classes of Module Array")
387 print("NumPy version", np.__version__)
388 print()
389 result = unittest.TextTestRunner(verbosity=2).run(suite)
390 sys.exit(bool(result.errors + result.failures))