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