summaryrefslogtreecommitdiff
path: root/numpy/doc/swig/test/testVector.py
blob: ed085aa94b032d8696a5872526c28d5bcfc62bcb (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#! /usr/bin/env python

# System imports
from   distutils.util import get_platform
import os
import sys
import unittest

# Import NumPy
import numpy as np
major, minor = [ int(d) for d in np.__version__.split(".")[:2] ]
if major == 0: BadListError = TypeError
else:          BadListError = ValueError

# Add the distutils-generated build directory to the python search path and then
# import the extension module
libDir = "lib.%s-%s" % (get_platform(), sys.version[:3])
sys.path.insert(0,os.path.join("build", libDir))
import Vector

######################################################################

class VectorTestCase(unittest.TestCase):

    def __init__(self, methodName="runTest"):
        unittest.TestCase.__init__(self, methodName)
        self.typeStr  = "double"
        self.typeCode = "d"

    # Test the (type IN_ARRAY1[ANY]) typemap
    def testLength(self):
        "Test length function"
        print >>sys.stderr, self.typeStr, "... ",
        length = Vector.__dict__[self.typeStr + "Length"]
        self.assertEquals(length([5, 12, 0]), 13)

    # Test the (type IN_ARRAY1[ANY]) typemap
    def testLengthBadList(self):
        "Test length function with bad list"
        print >>sys.stderr, self.typeStr, "... ",
        length = Vector.__dict__[self.typeStr + "Length"]
        self.assertRaises(BadListError, length, [5, "twelve", 0])

    # Test the (type IN_ARRAY1[ANY]) typemap
    def testLengthWrongSize(self):
        "Test length function with wrong size"
        print >>sys.stderr, self.typeStr, "... ",
        length = Vector.__dict__[self.typeStr + "Length"]
        self.assertRaises(TypeError, length, [5, 12])

    # Test the (type IN_ARRAY1[ANY]) typemap
    def testLengthWrongDim(self):
        "Test length function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        length = Vector.__dict__[self.typeStr + "Length"]
        self.assertRaises(TypeError, length, [[1,2], [3,4]])

    # Test the (type IN_ARRAY1[ANY]) typemap
    def testLengthNonContainer(self):
        "Test length function with non-container"
        print >>sys.stderr, self.typeStr, "... ",
        length = Vector.__dict__[self.typeStr + "Length"]
        self.assertRaises(TypeError, length, None)

    # Test the (type* IN_ARRAY1, int DIM1) typemap
    def testProd(self):
        "Test prod function"
        print >>sys.stderr, self.typeStr, "... ",
        prod = Vector.__dict__[self.typeStr + "Prod"]
        self.assertEquals(prod([1,2,3,4]), 24)

    # Test the (type* IN_ARRAY1, int DIM1) typemap
    def testProdBadList(self):
        "Test prod function with bad list"
        print >>sys.stderr, self.typeStr, "... ",
        prod = Vector.__dict__[self.typeStr + "Prod"]
        self.assertRaises(BadListError, prod, [[1,"two"], ["e","pi"]])

    # Test the (type* IN_ARRAY1, int DIM1) typemap
    def testProdWrongDim(self):
        "Test prod function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        prod = Vector.__dict__[self.typeStr + "Prod"]
        self.assertRaises(TypeError, prod, [[1,2], [8,9]])

    # Test the (type* IN_ARRAY1, int DIM1) typemap
    def testProdNonContainer(self):
        "Test prod function with non-container"
        print >>sys.stderr, self.typeStr, "... ",
        prod = Vector.__dict__[self.typeStr + "Prod"]
        self.assertRaises(TypeError, prod, None)

    # Test the (int DIM1, type* IN_ARRAY1) typemap
    def testSum(self):
        "Test sum function"
        print >>sys.stderr, self.typeStr, "... ",
        sum = Vector.__dict__[self.typeStr + "Sum"]
        self.assertEquals(sum([5,6,7,8]), 26)

    # Test the (int DIM1, type* IN_ARRAY1) typemap
    def testSumBadList(self):
        "Test sum function with bad list"
        print >>sys.stderr, self.typeStr, "... ",
        sum = Vector.__dict__[self.typeStr + "Sum"]
        self.assertRaises(BadListError, sum, [3,4, 5, "pi"])

    # Test the (int DIM1, type* IN_ARRAY1) typemap
    def testSumWrongDim(self):
        "Test sum function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        sum = Vector.__dict__[self.typeStr + "Sum"]
        self.assertRaises(TypeError, sum, [[3,4], [5,6]])

    # Test the (int DIM1, type* IN_ARRAY1) typemap
    def testSumNonContainer(self):
        "Test sum function with non-container"
        print >>sys.stderr, self.typeStr, "... ",
        sum = Vector.__dict__[self.typeStr + "Sum"]
        self.assertRaises(TypeError, sum, True)

    # Test the (type INPLACE_ARRAY1[ANY]) typemap
    def testReverse(self):
        "Test reverse function"
        print >>sys.stderr, self.typeStr, "... ",
        reverse = Vector.__dict__[self.typeStr + "Reverse"]
        vector = np.array([1,2,4],self.typeCode)
        reverse(vector)
        self.assertEquals((vector == [4,2,1]).all(), True)

    # Test the (type INPLACE_ARRAY1[ANY]) typemap
    def testReverseWrongDim(self):
        "Test reverse function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        reverse = Vector.__dict__[self.typeStr + "Reverse"]
        vector = np.array([[1,2], [3,4]],self.typeCode)
        self.assertRaises(TypeError, reverse, vector)

    # Test the (type INPLACE_ARRAY1[ANY]) typemap
    def testReverseWrongSize(self):
        "Test reverse function with wrong size"
        print >>sys.stderr, self.typeStr, "... ",
        reverse = Vector.__dict__[self.typeStr + "Reverse"]
        vector = np.array([9,8,7,6,5,4],self.typeCode)
        self.assertRaises(TypeError, reverse, vector)

    # Test the (type INPLACE_ARRAY1[ANY]) typemap
    def testReverseWrongType(self):
        "Test reverse function with wrong type"
        print >>sys.stderr, self.typeStr, "... ",
        reverse = Vector.__dict__[self.typeStr + "Reverse"]
        vector = np.array([1,2,4],'c')
        self.assertRaises(TypeError, reverse, vector)

    # Test the (type INPLACE_ARRAY1[ANY]) typemap
    def testReverseNonArray(self):
        "Test reverse function with non-array"
        print >>sys.stderr, self.typeStr, "... ",
        reverse = Vector.__dict__[self.typeStr + "Reverse"]
        self.assertRaises(TypeError, reverse, [2,4,6])

    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
    def testOnes(self):
        "Test ones function"
        print >>sys.stderr, self.typeStr, "... ",
        ones = Vector.__dict__[self.typeStr + "Ones"]
        vector = np.zeros(5,self.typeCode)
        ones(vector)
        np.testing.assert_array_equal(vector, np.array([1,1,1,1,1]))

    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
    def testOnesWrongDim(self):
        "Test ones function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        ones = Vector.__dict__[self.typeStr + "Ones"]
        vector = np.zeros((5,5),self.typeCode)
        self.assertRaises(TypeError, ones, vector)

    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
    def testOnesWrongType(self):
        "Test ones function with wrong type"
        print >>sys.stderr, self.typeStr, "... ",
        ones = Vector.__dict__[self.typeStr + "Ones"]
        vector = np.zeros((5,5),'c')
        self.assertRaises(TypeError, ones, vector)

    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
    def testOnesNonArray(self):
        "Test ones function with non-array"
        print >>sys.stderr, self.typeStr, "... ",
        ones = Vector.__dict__[self.typeStr + "Ones"]
        self.assertRaises(TypeError, ones, [2,4,6,8])

    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
    def testZeros(self):
        "Test zeros function"
        print >>sys.stderr, self.typeStr, "... ",
        zeros = Vector.__dict__[self.typeStr + "Zeros"]
        vector = np.ones(5,self.typeCode)
        zeros(vector)
        np.testing.assert_array_equal(vector, np.array([0,0,0,0,0]))

    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
    def testZerosWrongDim(self):
        "Test zeros function with wrong dimensions"
        print >>sys.stderr, self.typeStr, "... ",
        zeros = Vector.__dict__[self.typeStr + "Zeros"]
        vector = np.ones((5,5),self.typeCode)
        self.assertRaises(TypeError, zeros, vector)

    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
    def testZerosWrongType(self):
        "Test zeros function with wrong type"
        print >>sys.stderr, self.typeStr, "... ",
        zeros = Vector.__dict__[self.typeStr + "Zeros"]
        vector = np.ones(6,'c')
        self.assertRaises(TypeError, zeros, vector)

    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
    def testZerosNonArray(self):
        "Test zeros function with non-array"
        print >>sys.stderr, self.typeStr, "... ",
        zeros = Vector.__dict__[self.typeStr + "Zeros"]
        self.assertRaises(TypeError, zeros, [1,3,5,7,9])

    # Test the (type ARGOUT_ARRAY1[ANY]) typemap
    def testEOSplit(self):
        "Test eoSplit function"
        print >>sys.stderr, self.typeStr, "... ",
        eoSplit = Vector.__dict__[self.typeStr + "EOSplit"]
        even, odd = eoSplit([1,2,3])
        self.assertEquals((even == [1,0,3]).all(), True)
        self.assertEquals((odd  == [0,2,0]).all(), True)

    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
    def testTwos(self):
        "Test twos function"
        print >>sys.stderr, self.typeStr, "... ",
        twos = Vector.__dict__[self.typeStr + "Twos"]
        vector = twos(5)
        self.assertEquals((vector == [2,2,2,2,2]).all(), True)

    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
    def testTwosNonInt(self):
        "Test twos function with non-integer dimension"
        print >>sys.stderr, self.typeStr, "... ",
        twos = Vector.__dict__[self.typeStr + "Twos"]
        self.assertRaises(TypeError, twos, 5.0)

    # Test the (int DIM1, type* ARGOUT_ARRAY1) typemap
    def testThrees(self):
        "Test threes function"
        print >>sys.stderr, self.typeStr, "... ",
        threes = Vector.__dict__[self.typeStr + "Threes"]
        vector = threes(6)
        self.assertEquals((vector == [3,3,3,3,3,3]).all(), True)

    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
    def testThreesNonInt(self):
        "Test threes function with non-integer dimension"
        print >>sys.stderr, self.typeStr, "... ",
        threes = Vector.__dict__[self.typeStr + "Threes"]
        self.assertRaises(TypeError, threes, "threes")

######################################################################

class scharTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "schar"
        self.typeCode = "b"

######################################################################

class ucharTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "uchar"
        self.typeCode = "B"

######################################################################

class shortTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "short"
        self.typeCode = "h"

######################################################################

class ushortTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "ushort"
        self.typeCode = "H"

######################################################################

class intTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "int"
        self.typeCode = "i"

######################################################################

class uintTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "uint"
        self.typeCode = "I"

######################################################################

class longTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "long"
        self.typeCode = "l"

######################################################################

class ulongTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "ulong"
        self.typeCode = "L"

######################################################################

class longLongTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "longLong"
        self.typeCode = "q"

######################################################################

class ulongLongTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "ulongLong"
        self.typeCode = "Q"

######################################################################

class floatTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "float"
        self.typeCode = "f"

######################################################################

class doubleTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "double"
        self.typeCode = "d"

######################################################################

if __name__ == "__main__":

    # Build the test suite
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(    scharTestCase))
    suite.addTest(unittest.makeSuite(    ucharTestCase))
    suite.addTest(unittest.makeSuite(    shortTestCase))
    suite.addTest(unittest.makeSuite(   ushortTestCase))
    suite.addTest(unittest.makeSuite(      intTestCase))
    suite.addTest(unittest.makeSuite(     uintTestCase))
    suite.addTest(unittest.makeSuite(     longTestCase))
    suite.addTest(unittest.makeSuite(    ulongTestCase))
    suite.addTest(unittest.makeSuite( longLongTestCase))
    suite.addTest(unittest.makeSuite(ulongLongTestCase))
    suite.addTest(unittest.makeSuite(    floatTestCase))
    suite.addTest(unittest.makeSuite(   doubleTestCase))

    # Execute the test suite
    print "Testing 1D Functions of Module Vector"
    print "NumPy version", np.__version__
    print
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(len(result.errors) + len(result.failures))