| 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 Vector |
| 14 |
|
| 15 |
###################################################################### |
| 16 |
|
| 17 |
class VectorTestCase(unittest.TestCase): |
| 18 |
|
| 19 |
def __init__(self, methodName="runTest"): |
| 20 |
unittest.TestCase.__init__(self, methodName) |
| 21 |
self.typeStr = "double" |
| 22 |
self.typeCode = "d" |
| 23 |
|
| 24 |
# Test the (type IN_ARRAY1[ANY]) typemap |
| 25 |
def testLength(self): |
| 26 |
"Test length function" |
| 27 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 28 |
length = Vector.__dict__[self.typeStr + "Length"] |
| 29 |
self.assertEqual(length([5, 12, 0]), 13) |
| 30 |
|
| 31 |
# Test the (type IN_ARRAY1[ANY]) typemap |
| 32 |
def testLengthBadList(self): |
| 33 |
"Test length function with bad list" |
| 34 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 35 |
length = Vector.__dict__[self.typeStr + "Length"] |
| 36 |
self.assertRaises(BadListError, length, [5, "twelve", 0]) |
| 37 |
|
| 38 |
# Test the (type IN_ARRAY1[ANY]) typemap |
| 39 |
def testLengthWrongSize(self): |
| 40 |
"Test length function with wrong size" |
| 41 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 42 |
length = Vector.__dict__[self.typeStr + "Length"] |
| 43 |
self.assertRaises(TypeError, length, [5, 12]) |
| 44 |
|
| 45 |
# Test the (type IN_ARRAY1[ANY]) typemap |
| 46 |
def testLengthWrongDim(self): |
| 47 |
"Test length function with wrong dimensions" |
| 48 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 49 |
length = Vector.__dict__[self.typeStr + "Length"] |
| 50 |
self.assertRaises(TypeError, length, [[1, 2], [3, 4]]) |
| 51 |
|
| 52 |
# Test the (type IN_ARRAY1[ANY]) typemap |
| 53 |
def testLengthNonContainer(self): |
| 54 |
"Test length function with non-container" |
| 55 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 56 |
length = Vector.__dict__[self.typeStr + "Length"] |
| 57 |
self.assertRaises(TypeError, length, None) |
| 58 |
|
| 59 |
# Test the (type* IN_ARRAY1, int DIM1) typemap |
| 60 |
def testProd(self): |
| 61 |
"Test prod function" |
| 62 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 63 |
prod = Vector.__dict__[self.typeStr + "Prod"] |
| 64 |
self.assertEqual(prod([1, 2, 3, 4]), 24) |
| 65 |
|
| 66 |
# Test the (type* IN_ARRAY1, int DIM1) typemap |
| 67 |
def testProdBadList(self): |
| 68 |
"Test prod function with bad list" |
| 69 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 70 |
prod = Vector.__dict__[self.typeStr + "Prod"] |
| 71 |
self.assertRaises(BadListError, prod, [[1, "two"], ["e", "pi"]]) |
| 72 |
|
| 73 |
# Test the (type* IN_ARRAY1, int DIM1) typemap |
| 74 |
def testProdWrongDim(self): |
| 75 |
"Test prod function with wrong dimensions" |
| 76 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 77 |
prod = Vector.__dict__[self.typeStr + "Prod"] |
| 78 |
self.assertRaises(TypeError, prod, [[1, 2], [8, 9]]) |
| 79 |
|
| 80 |
# Test the (type* IN_ARRAY1, int DIM1) typemap |
| 81 |
def testProdNonContainer(self): |
| 82 |
"Test prod function with non-container" |
| 83 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 84 |
prod = Vector.__dict__[self.typeStr + "Prod"] |
| 85 |
self.assertRaises(TypeError, prod, None) |
| 86 |
|
| 87 |
# Test the (int DIM1, type* IN_ARRAY1) typemap |
| 88 |
def testSum(self): |
| 89 |
"Test sum function" |
| 90 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 91 |
sum = Vector.__dict__[self.typeStr + "Sum"] |
| 92 |
self.assertEqual(sum([5, 6, 7, 8]), 26) |
| 93 |
|
| 94 |
# Test the (int DIM1, type* IN_ARRAY1) typemap |
| 95 |
def testSumBadList(self): |
| 96 |
"Test sum function with bad list" |
| 97 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 98 |
sum = Vector.__dict__[self.typeStr + "Sum"] |
| 99 |
self.assertRaises(BadListError, sum, [3, 4, 5, "pi"]) |
| 100 |
|
| 101 |
# Test the (int DIM1, type* IN_ARRAY1) typemap |
| 102 |
def testSumWrongDim(self): |
| 103 |
"Test sum function with wrong dimensions" |
| 104 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 105 |
sum = Vector.__dict__[self.typeStr + "Sum"] |
| 106 |
self.assertRaises(TypeError, sum, [[3, 4], [5, 6]]) |
| 107 |
|
| 108 |
# Test the (int DIM1, type* IN_ARRAY1) typemap |
| 109 |
def testSumNonContainer(self): |
| 110 |
"Test sum function with non-container" |
| 111 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 112 |
sum = Vector.__dict__[self.typeStr + "Sum"] |
| 113 |
self.assertRaises(TypeError, sum, True) |
| 114 |
|
| 115 |
# Test the (type INPLACE_ARRAY1[ANY]) typemap |
| 116 |
def testReverse(self): |
| 117 |
"Test reverse function" |
| 118 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 119 |
reverse = Vector.__dict__[self.typeStr + "Reverse"] |
| 120 |
vector = np.array([1, 2, 4], self.typeCode) |
| 121 |
reverse(vector) |
| 122 |
self.assertEqual((vector == [4, 2, 1]).all(), True) |
| 123 |
|
| 124 |
# Test the (type INPLACE_ARRAY1[ANY]) typemap |
| 125 |
def testReverseWrongDim(self): |
| 126 |
"Test reverse function with wrong dimensions" |
| 127 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 128 |
reverse = Vector.__dict__[self.typeStr + "Reverse"] |
| 129 |
vector = np.array([[1, 2], [3, 4]], self.typeCode) |
| 130 |
self.assertRaises(TypeError, reverse, vector) |
| 131 |
|
| 132 |
# Test the (type INPLACE_ARRAY1[ANY]) typemap |
| 133 |
def testReverseWrongSize(self): |
| 134 |
"Test reverse function with wrong size" |
| 135 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 136 |
reverse = Vector.__dict__[self.typeStr + "Reverse"] |
| 137 |
vector = np.array([9, 8, 7, 6, 5, 4], self.typeCode) |
| 138 |
self.assertRaises(TypeError, reverse, vector) |
| 139 |
|
| 140 |
# Test the (type INPLACE_ARRAY1[ANY]) typemap |
| 141 |
def testReverseWrongType(self): |
| 142 |
"Test reverse function with wrong type" |
| 143 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 144 |
reverse = Vector.__dict__[self.typeStr + "Reverse"] |
| 145 |
vector = np.array([1, 2, 4], 'c') |
| 146 |
self.assertRaises(TypeError, reverse, vector) |
| 147 |
|
| 148 |
# Test the (type INPLACE_ARRAY1[ANY]) typemap |
| 149 |
def testReverseNonArray(self): |
| 150 |
"Test reverse function with non-array" |
| 151 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 152 |
reverse = Vector.__dict__[self.typeStr + "Reverse"] |
| 153 |
self.assertRaises(TypeError, reverse, [2, 4, 6]) |
| 154 |
|
| 155 |
# Test the (type* INPLACE_ARRAY1, int DIM1) typemap |
| 156 |
def testOnes(self): |
| 157 |
"Test ones function" |
| 158 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 159 |
ones = Vector.__dict__[self.typeStr + "Ones"] |
| 160 |
vector = np.zeros(5, self.typeCode) |
| 161 |
ones(vector) |
| 162 |
np.testing.assert_array_equal(vector, np.array([1, 1, 1, 1, 1])) |
| 163 |
|
| 164 |
# Test the (type* INPLACE_ARRAY1, int DIM1) typemap |
| 165 |
def testOnesWrongDim(self): |
| 166 |
"Test ones function with wrong dimensions" |
| 167 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 168 |
ones = Vector.__dict__[self.typeStr + "Ones"] |
| 169 |
vector = np.zeros((5, 5), self.typeCode) |
| 170 |
self.assertRaises(TypeError, ones, vector) |
| 171 |
|
| 172 |
# Test the (type* INPLACE_ARRAY1, int DIM1) typemap |
| 173 |
def testOnesWrongType(self): |
| 174 |
"Test ones function with wrong type" |
| 175 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 176 |
ones = Vector.__dict__[self.typeStr + "Ones"] |
| 177 |
vector = np.zeros((5, 5), 'c') |
| 178 |
self.assertRaises(TypeError, ones, vector) |
| 179 |
|
| 180 |
# Test the (type* INPLACE_ARRAY1, int DIM1) typemap |
| 181 |
def testOnesNonArray(self): |
| 182 |
"Test ones function with non-array" |
| 183 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 184 |
ones = Vector.__dict__[self.typeStr + "Ones"] |
| 185 |
self.assertRaises(TypeError, ones, [2, 4, 6, 8]) |
| 186 |
|
| 187 |
# Test the (int DIM1, type* INPLACE_ARRAY1) typemap |
| 188 |
def testZeros(self): |
| 189 |
"Test zeros function" |
| 190 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 191 |
zeros = Vector.__dict__[self.typeStr + "Zeros"] |
| 192 |
vector = np.ones(5, self.typeCode) |
| 193 |
zeros(vector) |
| 194 |
np.testing.assert_array_equal(vector, np.array([0, 0, 0, 0, 0])) |
| 195 |
|
| 196 |
# Test the (int DIM1, type* INPLACE_ARRAY1) typemap |
| 197 |
def testZerosWrongDim(self): |
| 198 |
"Test zeros function with wrong dimensions" |
| 199 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 200 |
zeros = Vector.__dict__[self.typeStr + "Zeros"] |
| 201 |
vector = np.ones((5, 5), self.typeCode) |
| 202 |
self.assertRaises(TypeError, zeros, vector) |
| 203 |
|
| 204 |
# Test the (int DIM1, type* INPLACE_ARRAY1) typemap |
| 205 |
def testZerosWrongType(self): |
| 206 |
"Test zeros function with wrong type" |
| 207 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 208 |
zeros = Vector.__dict__[self.typeStr + "Zeros"] |
| 209 |
vector = np.ones(6, 'c') |
| 210 |
self.assertRaises(TypeError, zeros, vector) |
| 211 |
|
| 212 |
# Test the (int DIM1, type* INPLACE_ARRAY1) typemap |
| 213 |
def testZerosNonArray(self): |
| 214 |
"Test zeros function with non-array" |
| 215 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 216 |
zeros = Vector.__dict__[self.typeStr + "Zeros"] |
| 217 |
self.assertRaises(TypeError, zeros, [1, 3, 5, 7, 9]) |
| 218 |
|
| 219 |
# Test the (type ARGOUT_ARRAY1[ANY]) typemap |
| 220 |
def testEOSplit(self): |
| 221 |
"Test eoSplit function" |
| 222 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 223 |
eoSplit = Vector.__dict__[self.typeStr + "EOSplit"] |
| 224 |
even, odd = eoSplit([1, 2, 3]) |
| 225 |
self.assertEqual((even == [1, 0, 3]).all(), True) |
| 226 |
self.assertEqual((odd == [0, 2, 0]).all(), True) |
| 227 |
|
| 228 |
# Test the (type* ARGOUT_ARRAY1, int DIM1) typemap |
| 229 |
def testTwos(self): |
| 230 |
"Test twos function" |
| 231 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 232 |
twos = Vector.__dict__[self.typeStr + "Twos"] |
| 233 |
vector = twos(5) |
| 234 |
self.assertEqual((vector == [2, 2, 2, 2, 2]).all(), True) |
| 235 |
|
| 236 |
# Test the (type* ARGOUT_ARRAY1, int DIM1) typemap |
| 237 |
def testTwosNonInt(self): |
| 238 |
"Test twos function with non-integer dimension" |
| 239 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 240 |
twos = Vector.__dict__[self.typeStr + "Twos"] |
| 241 |
self.assertRaises(TypeError, twos, 5.0) |
| 242 |
|
| 243 |
# Test the (int DIM1, type* ARGOUT_ARRAY1) typemap |
| 244 |
def testThrees(self): |
| 245 |
"Test threes function" |
| 246 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 247 |
threes = Vector.__dict__[self.typeStr + "Threes"] |
| 248 |
vector = threes(6) |
| 249 |
self.assertEqual((vector == [3, 3, 3, 3, 3, 3]).all(), True) |
| 250 |
|
| 251 |
# Test the (type* ARGOUT_ARRAY1, int DIM1) typemap |
| 252 |
def testThreesNonInt(self): |
| 253 |
"Test threes function with non-integer dimension" |
| 254 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 255 |
threes = Vector.__dict__[self.typeStr + "Threes"] |
| 256 |
self.assertRaises(TypeError, threes, "threes") |
| 257 |
|
| 258 |
###################################################################### |
| 259 |
|
| 260 |
class scharTestCase(VectorTestCase): |
| 261 |
def __init__(self, methodName="runTest"): |
| 262 |
VectorTestCase.__init__(self, methodName) |
| 263 |
self.typeStr = "schar" |
| 264 |
self.typeCode = "b" |
| 265 |
|
| 266 |
###################################################################### |
| 267 |
|
| 268 |
class ucharTestCase(VectorTestCase): |
| 269 |
def __init__(self, methodName="runTest"): |
| 270 |
VectorTestCase.__init__(self, methodName) |
| 271 |
self.typeStr = "uchar" |
| 272 |
self.typeCode = "B" |
| 273 |
|
| 274 |
###################################################################### |
| 275 |
|
| 276 |
class shortTestCase(VectorTestCase): |
| 277 |
def __init__(self, methodName="runTest"): |
| 278 |
VectorTestCase.__init__(self, methodName) |
| 279 |
self.typeStr = "short" |
| 280 |
self.typeCode = "h" |
| 281 |
|
| 282 |
###################################################################### |
| 283 |
|
| 284 |
class ushortTestCase(VectorTestCase): |
| 285 |
def __init__(self, methodName="runTest"): |
| 286 |
VectorTestCase.__init__(self, methodName) |
| 287 |
self.typeStr = "ushort" |
| 288 |
self.typeCode = "H" |
| 289 |
|
| 290 |
###################################################################### |
| 291 |
|
| 292 |
class intTestCase(VectorTestCase): |
| 293 |
def __init__(self, methodName="runTest"): |
| 294 |
VectorTestCase.__init__(self, methodName) |
| 295 |
self.typeStr = "int" |
| 296 |
self.typeCode = "i" |
| 297 |
|
| 298 |
###################################################################### |
| 299 |
|
| 300 |
class uintTestCase(VectorTestCase): |
| 301 |
def __init__(self, methodName="runTest"): |
| 302 |
VectorTestCase.__init__(self, methodName) |
| 303 |
self.typeStr = "uint" |
| 304 |
self.typeCode = "I" |
| 305 |
|
| 306 |
###################################################################### |
| 307 |
|
| 308 |
class longTestCase(VectorTestCase): |
| 309 |
def __init__(self, methodName="runTest"): |
| 310 |
VectorTestCase.__init__(self, methodName) |
| 311 |
self.typeStr = "long" |
| 312 |
self.typeCode = "l" |
| 313 |
|
| 314 |
###################################################################### |
| 315 |
|
| 316 |
class ulongTestCase(VectorTestCase): |
| 317 |
def __init__(self, methodName="runTest"): |
| 318 |
VectorTestCase.__init__(self, methodName) |
| 319 |
self.typeStr = "ulong" |
| 320 |
self.typeCode = "L" |
| 321 |
|
| 322 |
###################################################################### |
| 323 |
|
| 324 |
class longLongTestCase(VectorTestCase): |
| 325 |
def __init__(self, methodName="runTest"): |
| 326 |
VectorTestCase.__init__(self, methodName) |
| 327 |
self.typeStr = "longLong" |
| 328 |
self.typeCode = "q" |
| 329 |
|
| 330 |
###################################################################### |
| 331 |
|
| 332 |
class ulongLongTestCase(VectorTestCase): |
| 333 |
def __init__(self, methodName="runTest"): |
| 334 |
VectorTestCase.__init__(self, methodName) |
| 335 |
self.typeStr = "ulongLong" |
| 336 |
self.typeCode = "Q" |
| 337 |
|
| 338 |
###################################################################### |
| 339 |
|
| 340 |
class floatTestCase(VectorTestCase): |
| 341 |
def __init__(self, methodName="runTest"): |
| 342 |
VectorTestCase.__init__(self, methodName) |
| 343 |
self.typeStr = "float" |
| 344 |
self.typeCode = "f" |
| 345 |
|
| 346 |
###################################################################### |
| 347 |
|
| 348 |
class doubleTestCase(VectorTestCase): |
| 349 |
def __init__(self, methodName="runTest"): |
| 350 |
VectorTestCase.__init__(self, methodName) |
| 351 |
self.typeStr = "double" |
| 352 |
self.typeCode = "d" |
| 353 |
|
| 354 |
###################################################################### |
| 355 |
|
| 356 |
|
| 357 |
if __name__ == "__main__": |
| 358 |
|
| 359 |
# Build the test suite |
| 360 |
suite = unittest.TestSuite() |
| 361 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( scharTestCase)) |
| 362 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ucharTestCase)) |
| 363 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( shortTestCase)) |
| 364 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ushortTestCase)) |
| 365 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( intTestCase)) |
| 366 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( uintTestCase)) |
| 367 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longTestCase)) |
| 368 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ulongTestCase)) |
| 369 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longLongTestCase)) |
| 370 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ulongLongTestCase)) |
| 371 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( floatTestCase)) |
| 372 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( doubleTestCase)) |
| 373 |
|
| 374 |
# Execute the test suite |
| 375 |
print("Testing 1D Functions of Module Vector") |
| 376 |
print("NumPy version", np.__version__) |
| 377 |
print() |
| 378 |
result = unittest.TextTestRunner(verbosity=2).run(suite) |
| 379 |
sys.exit(bool(result.errors + result.failures)) |