| 1 |
#!/usr/bin/env python3 |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from math import sqrt |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
|
| 8 |
major, minor = [int(d) for d in np.__version__.split(".")[:2]] |
| 9 |
if major == 0: |
| 10 |
BadListError = TypeError |
| 11 |
else: |
| 12 |
BadListError = ValueError |
| 13 |
|
| 14 |
import Tensor |
| 15 |
|
| 16 |
###################################################################### |
| 17 |
|
| 18 |
class TensorTestCase(unittest.TestCase): |
| 19 |
|
| 20 |
def __init__(self, methodName="runTests"): |
| 21 |
unittest.TestCase.__init__(self, methodName) |
| 22 |
self.typeStr = "double" |
| 23 |
self.typeCode = "d" |
| 24 |
self.result = sqrt(28.0 / 8) |
| 25 |
|
| 26 |
# Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap |
| 27 |
def testNorm(self): |
| 28 |
"Test norm function" |
| 29 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 30 |
norm = Tensor.__dict__[self.typeStr + "Norm"] |
| 31 |
tensor = [[[0, 1], [2, 3]], |
| 32 |
[[3, 2], [1, 0]]] |
| 33 |
if isinstance(self.result, int): |
| 34 |
self.assertEqual(norm(tensor), self.result) |
| 35 |
else: |
| 36 |
self.assertAlmostEqual(norm(tensor), self.result, 6) |
| 37 |
|
| 38 |
# Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap |
| 39 |
def testNormBadList(self): |
| 40 |
"Test norm function with bad list" |
| 41 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 42 |
norm = Tensor.__dict__[self.typeStr + "Norm"] |
| 43 |
tensor = [[[0, "one"], [2, 3]], |
| 44 |
[[3, "two"], [1, 0]]] |
| 45 |
self.assertRaises(BadListError, norm, tensor) |
| 46 |
|
| 47 |
# Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap |
| 48 |
def testNormWrongDim(self): |
| 49 |
"Test norm function with wrong dimensions" |
| 50 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 51 |
norm = Tensor.__dict__[self.typeStr + "Norm"] |
| 52 |
tensor = [[0, 1, 2, 3], |
| 53 |
[3, 2, 1, 0]] |
| 54 |
self.assertRaises(TypeError, norm, tensor) |
| 55 |
|
| 56 |
# Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap |
| 57 |
def testNormWrongSize(self): |
| 58 |
"Test norm function with wrong size" |
| 59 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 60 |
norm = Tensor.__dict__[self.typeStr + "Norm"] |
| 61 |
tensor = [[[0, 1, 0], [2, 3, 2]], |
| 62 |
[[3, 2, 3], [1, 0, 1]]] |
| 63 |
self.assertRaises(TypeError, norm, tensor) |
| 64 |
|
| 65 |
# Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap |
| 66 |
def testNormNonContainer(self): |
| 67 |
"Test norm function with non-container" |
| 68 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 69 |
norm = Tensor.__dict__[self.typeStr + "Norm"] |
| 70 |
self.assertRaises(TypeError, norm, None) |
| 71 |
|
| 72 |
# Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 73 |
def testMax(self): |
| 74 |
"Test max function" |
| 75 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 76 |
max = Tensor.__dict__[self.typeStr + "Max"] |
| 77 |
tensor = [[[1, 2], [3, 4]], |
| 78 |
[[5, 6], [7, 8]]] |
| 79 |
self.assertEqual(max(tensor), 8) |
| 80 |
|
| 81 |
# Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 82 |
def testMaxBadList(self): |
| 83 |
"Test max function with bad list" |
| 84 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 85 |
max = Tensor.__dict__[self.typeStr + "Max"] |
| 86 |
tensor = [[[1, "two"], [3, 4]], |
| 87 |
[[5, "six"], [7, 8]]] |
| 88 |
self.assertRaises(BadListError, max, tensor) |
| 89 |
|
| 90 |
# Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 91 |
def testMaxNonContainer(self): |
| 92 |
"Test max function with non-container" |
| 93 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 94 |
max = Tensor.__dict__[self.typeStr + "Max"] |
| 95 |
self.assertRaises(TypeError, max, None) |
| 96 |
|
| 97 |
# Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 98 |
def testMaxWrongDim(self): |
| 99 |
"Test max function with wrong dimensions" |
| 100 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 101 |
max = Tensor.__dict__[self.typeStr + "Max"] |
| 102 |
self.assertRaises(TypeError, max, [0, 1, 2, 3]) |
| 103 |
|
| 104 |
# Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap |
| 105 |
def testMin(self): |
| 106 |
"Test min function" |
| 107 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 108 |
min = Tensor.__dict__[self.typeStr + "Min"] |
| 109 |
tensor = [[[9, 8], [7, 6]], |
| 110 |
[[5, 4], [3, 2]]] |
| 111 |
self.assertEqual(min(tensor), 2) |
| 112 |
|
| 113 |
# Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap |
| 114 |
def testMinBadList(self): |
| 115 |
"Test min function with bad list" |
| 116 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 117 |
min = Tensor.__dict__[self.typeStr + "Min"] |
| 118 |
tensor = [[["nine", 8], [7, 6]], |
| 119 |
[["five", 4], [3, 2]]] |
| 120 |
self.assertRaises(BadListError, min, tensor) |
| 121 |
|
| 122 |
# Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap |
| 123 |
def testMinNonContainer(self): |
| 124 |
"Test min function with non-container" |
| 125 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 126 |
min = Tensor.__dict__[self.typeStr + "Min"] |
| 127 |
self.assertRaises(TypeError, min, True) |
| 128 |
|
| 129 |
# Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap |
| 130 |
def testMinWrongDim(self): |
| 131 |
"Test min function with wrong dimensions" |
| 132 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 133 |
min = Tensor.__dict__[self.typeStr + "Min"] |
| 134 |
self.assertRaises(TypeError, min, [[1, 3], [5, 7]]) |
| 135 |
|
| 136 |
# Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap |
| 137 |
def testScale(self): |
| 138 |
"Test scale function" |
| 139 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 140 |
scale = Tensor.__dict__[self.typeStr + "Scale"] |
| 141 |
tensor = np.array([[[1, 0, 1], [0, 1, 0], [1, 0, 1]], |
| 142 |
[[0, 1, 0], [1, 0, 1], [0, 1, 0]], |
| 143 |
[[1, 0, 1], [0, 1, 0], [1, 0, 1]]], self.typeCode) |
| 144 |
scale(tensor, 4) |
| 145 |
self.assertEqual((tensor == [[[4, 0, 4], [0, 4, 0], [4, 0, 4]], |
| 146 |
[[0, 4, 0], [4, 0, 4], [0, 4, 0]], |
| 147 |
[[4, 0, 4], [0, 4, 0], [4, 0, 4]]]).all(), True) |
| 148 |
|
| 149 |
# Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap |
| 150 |
def testScaleWrongType(self): |
| 151 |
"Test scale function with wrong type" |
| 152 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 153 |
scale = Tensor.__dict__[self.typeStr + "Scale"] |
| 154 |
tensor = np.array([[[1, 0, 1], [0, 1, 0], [1, 0, 1]], |
| 155 |
[[0, 1, 0], [1, 0, 1], [0, 1, 0]], |
| 156 |
[[1, 0, 1], [0, 1, 0], [1, 0, 1]]], 'c') |
| 157 |
self.assertRaises(TypeError, scale, tensor) |
| 158 |
|
| 159 |
# Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap |
| 160 |
def testScaleWrongDim(self): |
| 161 |
"Test scale function with wrong dimensions" |
| 162 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 163 |
scale = Tensor.__dict__[self.typeStr + "Scale"] |
| 164 |
tensor = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1], |
| 165 |
[0, 1, 0], [1, 0, 1], [0, 1, 0]], self.typeCode) |
| 166 |
self.assertRaises(TypeError, scale, tensor) |
| 167 |
|
| 168 |
# Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap |
| 169 |
def testScaleWrongSize(self): |
| 170 |
"Test scale function with wrong size" |
| 171 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 172 |
scale = Tensor.__dict__[self.typeStr + "Scale"] |
| 173 |
tensor = np.array([[[1, 0], [0, 1], [1, 0]], |
| 174 |
[[0, 1], [1, 0], [0, 1]], |
| 175 |
[[1, 0], [0, 1], [1, 0]]], self.typeCode) |
| 176 |
self.assertRaises(TypeError, scale, tensor) |
| 177 |
|
| 178 |
# Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap |
| 179 |
def testScaleNonArray(self): |
| 180 |
"Test scale function with non-array" |
| 181 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 182 |
scale = Tensor.__dict__[self.typeStr + "Scale"] |
| 183 |
self.assertRaises(TypeError, scale, True) |
| 184 |
|
| 185 |
# Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 186 |
def testFloor(self): |
| 187 |
"Test floor function" |
| 188 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 189 |
floor = Tensor.__dict__[self.typeStr + "Floor"] |
| 190 |
tensor = np.array([[[1, 2], [3, 4]], |
| 191 |
[[5, 6], [7, 8]]], self.typeCode) |
| 192 |
floor(tensor, 4) |
| 193 |
np.testing.assert_array_equal(tensor, np.array([[[4, 4], [4, 4]], |
| 194 |
[[5, 6], [7, 8]]])) |
| 195 |
|
| 196 |
# Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 197 |
def testFloorWrongType(self): |
| 198 |
"Test floor function with wrong type" |
| 199 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 200 |
floor = Tensor.__dict__[self.typeStr + "Floor"] |
| 201 |
tensor = np.array([[[1, 2], [3, 4]], |
| 202 |
[[5, 6], [7, 8]]], 'c') |
| 203 |
self.assertRaises(TypeError, floor, tensor) |
| 204 |
|
| 205 |
# Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 206 |
def testFloorWrongDim(self): |
| 207 |
"Test floor function with wrong type" |
| 208 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 209 |
floor = Tensor.__dict__[self.typeStr + "Floor"] |
| 210 |
tensor = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], self.typeCode) |
| 211 |
self.assertRaises(TypeError, floor, tensor) |
| 212 |
|
| 213 |
# Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap |
| 214 |
def testFloorNonArray(self): |
| 215 |
"Test floor function with non-array" |
| 216 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 217 |
floor = Tensor.__dict__[self.typeStr + "Floor"] |
| 218 |
self.assertRaises(TypeError, floor, object) |
| 219 |
|
| 220 |
# Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap |
| 221 |
def testCeil(self): |
| 222 |
"Test ceil function" |
| 223 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 224 |
ceil = Tensor.__dict__[self.typeStr + "Ceil"] |
| 225 |
tensor = np.array([[[9, 8], [7, 6]], |
| 226 |
[[5, 4], [3, 2]]], self.typeCode) |
| 227 |
ceil(tensor, 5) |
| 228 |
np.testing.assert_array_equal(tensor, np.array([[[5, 5], [5, 5]], |
| 229 |
[[5, 4], [3, 2]]])) |
| 230 |
|
| 231 |
# Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap |
| 232 |
def testCeilWrongType(self): |
| 233 |
"Test ceil function with wrong type" |
| 234 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 235 |
ceil = Tensor.__dict__[self.typeStr + "Ceil"] |
| 236 |
tensor = np.array([[[9, 8], [7, 6]], |
| 237 |
[[5, 4], [3, 2]]], 'c') |
| 238 |
self.assertRaises(TypeError, ceil, tensor) |
| 239 |
|
| 240 |
# Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap |
| 241 |
def testCeilWrongDim(self): |
| 242 |
"Test ceil function with wrong dimensions" |
| 243 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 244 |
ceil = Tensor.__dict__[self.typeStr + "Ceil"] |
| 245 |
tensor = np.array([[9, 8], [7, 6], [5, 4], [3, 2]], self.typeCode) |
| 246 |
self.assertRaises(TypeError, ceil, tensor) |
| 247 |
|
| 248 |
# Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap |
| 249 |
def testCeilNonArray(self): |
| 250 |
"Test ceil function with non-array" |
| 251 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 252 |
ceil = Tensor.__dict__[self.typeStr + "Ceil"] |
| 253 |
tensor = [[[9, 8], [7, 6]], |
| 254 |
[[5, 4], [3, 2]]] |
| 255 |
self.assertRaises(TypeError, ceil, tensor) |
| 256 |
|
| 257 |
# Test (type ARGOUT_ARRAY3[ANY][ANY][ANY]) typemap |
| 258 |
def testLUSplit(self): |
| 259 |
"Test luSplit function" |
| 260 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 261 |
luSplit = Tensor.__dict__[self.typeStr + "LUSplit"] |
| 262 |
lower, upper = luSplit([[[1, 1], [1, 1]], |
| 263 |
[[1, 1], [1, 1]]]) |
| 264 |
self.assertEqual((lower == [[[1, 1], [1, 0]], |
| 265 |
[[1, 0], [0, 0]]]).all(), True) |
| 266 |
self.assertEqual((upper == [[[0, 0], [0, 1]], |
| 267 |
[[0, 1], [1, 1]]]).all(), True) |
| 268 |
|
| 269 |
###################################################################### |
| 270 |
|
| 271 |
class scharTestCase(TensorTestCase): |
| 272 |
def __init__(self, methodName="runTest"): |
| 273 |
TensorTestCase.__init__(self, methodName) |
| 274 |
self.typeStr = "schar" |
| 275 |
self.typeCode = "b" |
| 276 |
self.result = int(self.result) |
| 277 |
|
| 278 |
###################################################################### |
| 279 |
|
| 280 |
class ucharTestCase(TensorTestCase): |
| 281 |
def __init__(self, methodName="runTest"): |
| 282 |
TensorTestCase.__init__(self, methodName) |
| 283 |
self.typeStr = "uchar" |
| 284 |
self.typeCode = "B" |
| 285 |
self.result = int(self.result) |
| 286 |
|
| 287 |
###################################################################### |
| 288 |
|
| 289 |
class shortTestCase(TensorTestCase): |
| 290 |
def __init__(self, methodName="runTest"): |
| 291 |
TensorTestCase.__init__(self, methodName) |
| 292 |
self.typeStr = "short" |
| 293 |
self.typeCode = "h" |
| 294 |
self.result = int(self.result) |
| 295 |
|
| 296 |
###################################################################### |
| 297 |
|
| 298 |
class ushortTestCase(TensorTestCase): |
| 299 |
def __init__(self, methodName="runTest"): |
| 300 |
TensorTestCase.__init__(self, methodName) |
| 301 |
self.typeStr = "ushort" |
| 302 |
self.typeCode = "H" |
| 303 |
self.result = int(self.result) |
| 304 |
|
| 305 |
###################################################################### |
| 306 |
|
| 307 |
class intTestCase(TensorTestCase): |
| 308 |
def __init__(self, methodName="runTest"): |
| 309 |
TensorTestCase.__init__(self, methodName) |
| 310 |
self.typeStr = "int" |
| 311 |
self.typeCode = "i" |
| 312 |
self.result = int(self.result) |
| 313 |
|
| 314 |
###################################################################### |
| 315 |
|
| 316 |
class uintTestCase(TensorTestCase): |
| 317 |
def __init__(self, methodName="runTest"): |
| 318 |
TensorTestCase.__init__(self, methodName) |
| 319 |
self.typeStr = "uint" |
| 320 |
self.typeCode = "I" |
| 321 |
self.result = int(self.result) |
| 322 |
|
| 323 |
###################################################################### |
| 324 |
|
| 325 |
class longTestCase(TensorTestCase): |
| 326 |
def __init__(self, methodName="runTest"): |
| 327 |
TensorTestCase.__init__(self, methodName) |
| 328 |
self.typeStr = "long" |
| 329 |
self.typeCode = "l" |
| 330 |
self.result = int(self.result) |
| 331 |
|
| 332 |
###################################################################### |
| 333 |
|
| 334 |
class ulongTestCase(TensorTestCase): |
| 335 |
def __init__(self, methodName="runTest"): |
| 336 |
TensorTestCase.__init__(self, methodName) |
| 337 |
self.typeStr = "ulong" |
| 338 |
self.typeCode = "L" |
| 339 |
self.result = int(self.result) |
| 340 |
|
| 341 |
###################################################################### |
| 342 |
|
| 343 |
class longLongTestCase(TensorTestCase): |
| 344 |
def __init__(self, methodName="runTest"): |
| 345 |
TensorTestCase.__init__(self, methodName) |
| 346 |
self.typeStr = "longLong" |
| 347 |
self.typeCode = "q" |
| 348 |
self.result = int(self.result) |
| 349 |
|
| 350 |
###################################################################### |
| 351 |
|
| 352 |
class ulongLongTestCase(TensorTestCase): |
| 353 |
def __init__(self, methodName="runTest"): |
| 354 |
TensorTestCase.__init__(self, methodName) |
| 355 |
self.typeStr = "ulongLong" |
| 356 |
self.typeCode = "Q" |
| 357 |
self.result = int(self.result) |
| 358 |
|
| 359 |
###################################################################### |
| 360 |
|
| 361 |
class floatTestCase(TensorTestCase): |
| 362 |
def __init__(self, methodName="runTest"): |
| 363 |
TensorTestCase.__init__(self, methodName) |
| 364 |
self.typeStr = "float" |
| 365 |
self.typeCode = "f" |
| 366 |
|
| 367 |
###################################################################### |
| 368 |
|
| 369 |
class doubleTestCase(TensorTestCase): |
| 370 |
def __init__(self, methodName="runTest"): |
| 371 |
TensorTestCase.__init__(self, methodName) |
| 372 |
self.typeStr = "double" |
| 373 |
self.typeCode = "d" |
| 374 |
|
| 375 |
###################################################################### |
| 376 |
|
| 377 |
|
| 378 |
if __name__ == "__main__": |
| 379 |
|
| 380 |
# Build the test suite |
| 381 |
suite = unittest.TestSuite() |
| 382 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( scharTestCase)) |
| 383 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ucharTestCase)) |
| 384 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( shortTestCase)) |
| 385 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ushortTestCase)) |
| 386 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( intTestCase)) |
| 387 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( uintTestCase)) |
| 388 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longTestCase)) |
| 389 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ulongTestCase)) |
| 390 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longLongTestCase)) |
| 391 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ulongLongTestCase)) |
| 392 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( floatTestCase)) |
| 393 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( doubleTestCase)) |
| 394 |
|
| 395 |
# Execute the test suite |
| 396 |
print("Testing 3D Functions of Module Tensor") |
| 397 |
print("NumPy version", np.__version__) |
| 398 |
print() |
| 399 |
result = unittest.TextTestRunner(verbosity=2).run(suite) |
| 400 |
sys.exit(bool(result.errors + result.failures)) |