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