summaryrefslogtreecommitdiff
path: root/tools/swig/test/testSuperTensor.py
blob: cdd88530b2be18b7dea2938c68fc6588e6adca6c (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
385
386
387
388
#! /usr/bin/env python
from __future__ import division, print_function

# System imports
from   distutils.util import get_platform
from   math           import sqrt
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

import SuperTensor

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

class SuperTensorTestCase(unittest.TestCase):

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

    # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap
    def testNorm(self):
        "Test norm function"
        print(self.typeStr, "... ", file=sys.stderr)
        norm = SuperTensor.__dict__[self.typeStr + "Norm"]
        supertensor = np.arange(2*2*2*2, dtype=self.typeCode).reshape((2, 2, 2, 2))
        #Note: cludge to get an answer of the same type as supertensor.
        #Answer is simply sqrt(sum(supertensor*supertensor)/16)
        answer = np.array([np.sqrt(np.sum(supertensor.astype('d')*supertensor)/16.)], dtype=self.typeCode)[0]
        self.assertAlmostEqual(norm(supertensor), answer, 6)

    # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap
    def testNormBadList(self):
        "Test norm function with bad list"
        print(self.typeStr, "... ", file=sys.stderr)
        norm = SuperTensor.__dict__[self.typeStr + "Norm"]
        supertensor = [[[[0, "one"], [2, 3]], [[3, "two"], [1, 0]]], [[[0, "one"], [2, 3]], [[3, "two"], [1, 0]]]]
        self.assertRaises(BadListError, norm, supertensor)

    # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap
    def testNormWrongDim(self):
        "Test norm function with wrong dimensions"
        print(self.typeStr, "... ", file=sys.stderr)
        norm = SuperTensor.__dict__[self.typeStr + "Norm"]
        supertensor = np.arange(2*2*2, dtype=self.typeCode).reshape((2, 2, 2))
        self.assertRaises(TypeError, norm, supertensor)

    # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap
    def testNormWrongSize(self):
        "Test norm function with wrong size"
        print(self.typeStr, "... ", file=sys.stderr)
        norm = SuperTensor.__dict__[self.typeStr + "Norm"]
        supertensor = np.arange(3*2*2, dtype=self.typeCode).reshape((3, 2, 2))
        self.assertRaises(TypeError, norm, supertensor)

    # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap
    def testNormNonContainer(self):
        "Test norm function with non-container"
        print(self.typeStr, "... ", file=sys.stderr)
        norm = SuperTensor.__dict__[self.typeStr + "Norm"]
        self.assertRaises(TypeError, norm, None)

    # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testMax(self):
        "Test max function"
        print(self.typeStr, "... ", file=sys.stderr)
        max = SuperTensor.__dict__[self.typeStr + "Max"]
        supertensor = [[[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]]
        self.assertEquals(max(supertensor), 8)

    # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testMaxBadList(self):
        "Test max function with bad list"
        print(self.typeStr, "... ", file=sys.stderr)
        max = SuperTensor.__dict__[self.typeStr + "Max"]
        supertensor = [[[[1, "two"], [3, 4]], [[5, "six"], [7, 8]]], [[[1, "two"], [3, 4]], [[5, "six"], [7, 8]]]]
        self.assertRaises(BadListError, max, supertensor)

    # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testMaxNonContainer(self):
        "Test max function with non-container"
        print(self.typeStr, "... ", file=sys.stderr)
        max = SuperTensor.__dict__[self.typeStr + "Max"]
        self.assertRaises(TypeError, max, None)

    # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testMaxWrongDim(self):
        "Test max function with wrong dimensions"
        print(self.typeStr, "... ", file=sys.stderr)
        max = SuperTensor.__dict__[self.typeStr + "Max"]
        self.assertRaises(TypeError, max, [0, -1, 2, -3])

    # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap
    def testMin(self):
        "Test min function"
        print(self.typeStr, "... ", file=sys.stderr)
        min = SuperTensor.__dict__[self.typeStr + "Min"]
        supertensor = [[[[9, 8], [7, 6]], [[5, 4], [3, 2]]], [[[9, 8], [7, 6]], [[5, 4], [3, 2]]]]
        self.assertEquals(min(supertensor), 2)

    # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap
    def testMinBadList(self):
        "Test min function with bad list"
        print(self.typeStr, "... ", file=sys.stderr)
        min = SuperTensor.__dict__[self.typeStr + "Min"]
        supertensor = [[[["nine", 8], [7, 6]], [["five", 4], [3, 2]]], [[["nine", 8], [7, 6]], [["five", 4], [3, 2]]]]
        self.assertRaises(BadListError, min, supertensor)

    # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap
    def testMinNonContainer(self):
        "Test min function with non-container"
        print(self.typeStr, "... ", file=sys.stderr)
        min = SuperTensor.__dict__[self.typeStr + "Min"]
        self.assertRaises(TypeError, min, True)

    # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap
    def testMinWrongDim(self):
        "Test min function with wrong dimensions"
        print(self.typeStr, "... ", file=sys.stderr)
        min = SuperTensor.__dict__[self.typeStr + "Min"]
        self.assertRaises(TypeError, min, [[1, 3], [5, 7]])

    # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap
    def testScale(self):
        "Test scale function"
        print(self.typeStr, "... ", file=sys.stderr)
        scale = SuperTensor.__dict__[self.typeStr + "Scale"]
        supertensor = np.arange(3*3*3*3, dtype=self.typeCode).reshape((3, 3, 3, 3))
        answer = supertensor.copy()*4
        scale(supertensor, 4)
        self.assertEquals((supertensor == answer).all(), True)

    # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap
    def testScaleWrongType(self):
        "Test scale function with wrong type"
        print(self.typeStr, "... ", file=sys.stderr)
        scale = SuperTensor.__dict__[self.typeStr + "Scale"]
        supertensor = np.array([[[1, 0, 1], [0, 1, 0], [1, 0, 1]],
                          [[0, 1, 0], [1, 0, 1], [0, 1, 0]],
                          [[1, 0, 1], [0, 1, 0], [1, 0, 1]]], 'c')
        self.assertRaises(TypeError, scale, supertensor)

    # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap
    def testScaleWrongDim(self):
        "Test scale function with wrong dimensions"
        print(self.typeStr, "... ", file=sys.stderr)
        scale = SuperTensor.__dict__[self.typeStr + "Scale"]
        supertensor = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1],
                          [0, 1, 0], [1, 0, 1], [0, 1, 0]], self.typeCode)
        self.assertRaises(TypeError, scale, supertensor)

    # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap
    def testScaleWrongSize(self):
        "Test scale function with wrong size"
        print(self.typeStr, "... ", file=sys.stderr)
        scale = SuperTensor.__dict__[self.typeStr + "Scale"]
        supertensor = np.array([[[1, 0], [0, 1], [1, 0]],
                          [[0, 1], [1, 0], [0, 1]],
                          [[1, 0], [0, 1], [1, 0]]], self.typeCode)
        self.assertRaises(TypeError, scale, supertensor)

    # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap
    def testScaleNonArray(self):
        "Test scale function with non-array"
        print(self.typeStr, "... ", file=sys.stderr)
        scale = SuperTensor.__dict__[self.typeStr + "Scale"]
        self.assertRaises(TypeError, scale, True)

    # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testFloor(self):
        "Test floor function"
        print(self.typeStr, "... ", file=sys.stderr)
        supertensor = np.arange(2*2*2*2, dtype=self.typeCode).reshape((2, 2, 2, 2))
        answer = supertensor.copy()
        answer[answer < 4] = 4

        floor = SuperTensor.__dict__[self.typeStr + "Floor"]
        floor(supertensor, 4)
        np.testing.assert_array_equal(supertensor, answer)

    # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testFloorWrongType(self):
        "Test floor function with wrong type"
        print(self.typeStr, "... ", file=sys.stderr)
        floor = SuperTensor.__dict__[self.typeStr + "Floor"]
        supertensor = np.ones(2*2*2*2, dtype='c').reshape((2, 2, 2, 2))
        self.assertRaises(TypeError, floor, supertensor)

    # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testFloorWrongDim(self):
        "Test floor function with wrong type"
        print(self.typeStr, "... ", file=sys.stderr)
        floor = SuperTensor.__dict__[self.typeStr + "Floor"]
        supertensor = np.arange(2*2*2, dtype=self.typeCode).reshape((2, 2, 2))
        self.assertRaises(TypeError, floor, supertensor)

    # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap
    def testFloorNonArray(self):
        "Test floor function with non-array"
        print(self.typeStr, "... ", file=sys.stderr)
        floor = SuperTensor.__dict__[self.typeStr + "Floor"]
        self.assertRaises(TypeError, floor, object)

    # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap
    def testCeil(self):
        "Test ceil function"
        print(self.typeStr, "... ", file=sys.stderr)
        supertensor = np.arange(2*2*2*2, dtype=self.typeCode).reshape((2, 2, 2, 2))
        answer = supertensor.copy()
        answer[answer > 5] = 5
        ceil = SuperTensor.__dict__[self.typeStr + "Ceil"]
        ceil(supertensor, 5)
        np.testing.assert_array_equal(supertensor, answer)

    # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap
    def testCeilWrongType(self):
        "Test ceil function with wrong type"
        print(self.typeStr, "... ", file=sys.stderr)
        ceil = SuperTensor.__dict__[self.typeStr + "Ceil"]
        supertensor = np.ones(2*2*2*2, 'c').reshape((2, 2, 2, 2))
        self.assertRaises(TypeError, ceil, supertensor)

    # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap
    def testCeilWrongDim(self):
        "Test ceil function with wrong dimensions"
        print(self.typeStr, "... ", file=sys.stderr)
        ceil = SuperTensor.__dict__[self.typeStr + "Ceil"]
        supertensor = np.arange(2*2*2, dtype=self.typeCode).reshape((2, 2, 2))
        self.assertRaises(TypeError, ceil, supertensor)

    # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap
    def testCeilNonArray(self):
        "Test ceil function with non-array"
        print(self.typeStr, "... ", file=sys.stderr)
        ceil = SuperTensor.__dict__[self.typeStr + "Ceil"]
        supertensor = np.arange(2*2*2*2, dtype=self.typeCode).reshape((2, 2, 2, 2)).tolist()
        self.assertRaises(TypeError, ceil, supertensor)

    # Test (type ARGOUT_ARRAY3[ANY][ANY][ANY]) typemap
    def testLUSplit(self):
        "Test luSplit function"
        print(self.typeStr, "... ", file=sys.stderr)
        luSplit = SuperTensor.__dict__[self.typeStr + "LUSplit"]
        supertensor = np.ones(2*2*2*2, dtype=self.typeCode).reshape((2, 2, 2, 2))
        answer_upper = [[[[0, 0], [0, 1]], [[0, 1], [1, 1]]], [[[0, 1], [1, 1]], [[1, 1], [1, 1]]]]
        answer_lower = [[[[1, 1], [1, 0]], [[1, 0], [0, 0]]], [[[1, 0], [0, 0]], [[0, 0], [0, 0]]]]
        lower, upper = luSplit(supertensor)
        self.assertEquals((lower == answer_lower).all(), True)
        self.assertEquals((upper == answer_upper).all(), True)

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

class scharTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "schar"
        self.typeCode = "b"
        #self.result   = int(self.result)

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

class ucharTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "uchar"
        self.typeCode = "B"
        #self.result   = int(self.result)

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

class shortTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "short"
        self.typeCode = "h"
        #self.result   = int(self.result)

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

class ushortTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "ushort"
        self.typeCode = "H"
        #self.result   = int(self.result)

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

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

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

class uintTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "uint"
        self.typeCode = "I"
        #self.result   = int(self.result)

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

class longTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "long"
        self.typeCode = "l"
        #self.result   = int(self.result)

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

class ulongTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "ulong"
        self.typeCode = "L"
        #self.result   = int(self.result)

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

class longLongTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "longLong"
        self.typeCode = "q"
        #self.result   = int(self.result)

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

class ulongLongTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__init__(self, methodName)
        self.typeStr  = "ulongLong"
        self.typeCode = "Q"
        #self.result   = int(self.result)

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

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

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

class doubleTestCase(SuperTensorTestCase):
    def __init__(self, methodName="runTest"):
        SuperTensorTestCase.__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 4D Functions of Module SuperTensor")
    print("NumPy version", np.__version__)
    print()
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(bool(result.errors + result.failures))