| 1 |
#!/usr/bin/env python3 |
| 2 |
import struct |
| 3 |
import sys |
| 4 |
import unittest |
| 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 Flat |
| 15 |
|
| 16 |
###################################################################### |
| 17 |
|
| 18 |
class FlatTestCase(unittest.TestCase): |
| 19 |
|
| 20 |
def __init__(self, methodName="runTest"): |
| 21 |
unittest.TestCase.__init__(self, methodName) |
| 22 |
self.typeStr = "double" |
| 23 |
self.typeCode = "d" |
| 24 |
|
| 25 |
# Test the (type* INPLACE_ARRAY_FLAT, int DIM_FLAT) typemap |
| 26 |
def testProcess1D(self): |
| 27 |
"Test Process function 1D array" |
| 28 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 29 |
process = Flat.__dict__[self.typeStr + "Process"] |
| 30 |
pack_output = b'' |
| 31 |
for i in range(10): |
| 32 |
pack_output += struct.pack(self.typeCode, i) |
| 33 |
x = np.frombuffer(pack_output, dtype=self.typeCode) |
| 34 |
y = x.copy() |
| 35 |
process(y) |
| 36 |
self.assertEqual(np.all((x + 1) == y), True) |
| 37 |
|
| 38 |
def testProcess3D(self): |
| 39 |
"Test Process function 3D array" |
| 40 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 41 |
process = Flat.__dict__[self.typeStr + "Process"] |
| 42 |
pack_output = b'' |
| 43 |
for i in range(24): |
| 44 |
pack_output += struct.pack(self.typeCode, i) |
| 45 |
x = np.frombuffer(pack_output, dtype=self.typeCode) |
| 46 |
x = x.reshape((2, 3, 4)) |
| 47 |
y = x.copy() |
| 48 |
process(y) |
| 49 |
self.assertEqual(np.all((x + 1) == y), True) |
| 50 |
|
| 51 |
def testProcess3DTranspose(self): |
| 52 |
"Test Process function 3D array, FORTRAN order" |
| 53 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 54 |
process = Flat.__dict__[self.typeStr + "Process"] |
| 55 |
pack_output = b'' |
| 56 |
for i in range(24): |
| 57 |
pack_output += struct.pack(self.typeCode, i) |
| 58 |
x = np.frombuffer(pack_output, dtype=self.typeCode) |
| 59 |
x = x.reshape((2, 3, 4)) |
| 60 |
y = x.copy() |
| 61 |
process(y.T) |
| 62 |
self.assertEqual(np.all((x.T + 1) == y.T), True) |
| 63 |
|
| 64 |
def testProcessNoncontiguous(self): |
| 65 |
"Test Process function with non-contiguous array, which should raise an error" |
| 66 |
print(self.typeStr, "... ", end=' ', file=sys.stderr) |
| 67 |
process = Flat.__dict__[self.typeStr + "Process"] |
| 68 |
pack_output = b'' |
| 69 |
for i in range(24): |
| 70 |
pack_output += struct.pack(self.typeCode, i) |
| 71 |
x = np.frombuffer(pack_output, dtype=self.typeCode) |
| 72 |
x = x.reshape((2, 3, 4)) |
| 73 |
self.assertRaises(TypeError, process, x[:, :, 0]) |
| 74 |
|
| 75 |
|
| 76 |
###################################################################### |
| 77 |
|
| 78 |
class scharTestCase(FlatTestCase): |
| 79 |
def __init__(self, methodName="runTest"): |
| 80 |
FlatTestCase.__init__(self, methodName) |
| 81 |
self.typeStr = "schar" |
| 82 |
self.typeCode = "b" |
| 83 |
|
| 84 |
###################################################################### |
| 85 |
|
| 86 |
class ucharTestCase(FlatTestCase): |
| 87 |
def __init__(self, methodName="runTest"): |
| 88 |
FlatTestCase.__init__(self, methodName) |
| 89 |
self.typeStr = "uchar" |
| 90 |
self.typeCode = "B" |
| 91 |
|
| 92 |
###################################################################### |
| 93 |
|
| 94 |
class shortTestCase(FlatTestCase): |
| 95 |
def __init__(self, methodName="runTest"): |
| 96 |
FlatTestCase.__init__(self, methodName) |
| 97 |
self.typeStr = "short" |
| 98 |
self.typeCode = "h" |
| 99 |
|
| 100 |
###################################################################### |
| 101 |
|
| 102 |
class ushortTestCase(FlatTestCase): |
| 103 |
def __init__(self, methodName="runTest"): |
| 104 |
FlatTestCase.__init__(self, methodName) |
| 105 |
self.typeStr = "ushort" |
| 106 |
self.typeCode = "H" |
| 107 |
|
| 108 |
###################################################################### |
| 109 |
|
| 110 |
class intTestCase(FlatTestCase): |
| 111 |
def __init__(self, methodName="runTest"): |
| 112 |
FlatTestCase.__init__(self, methodName) |
| 113 |
self.typeStr = "int" |
| 114 |
self.typeCode = "i" |
| 115 |
|
| 116 |
###################################################################### |
| 117 |
|
| 118 |
class uintTestCase(FlatTestCase): |
| 119 |
def __init__(self, methodName="runTest"): |
| 120 |
FlatTestCase.__init__(self, methodName) |
| 121 |
self.typeStr = "uint" |
| 122 |
self.typeCode = "I" |
| 123 |
|
| 124 |
###################################################################### |
| 125 |
|
| 126 |
class longTestCase(FlatTestCase): |
| 127 |
def __init__(self, methodName="runTest"): |
| 128 |
FlatTestCase.__init__(self, methodName) |
| 129 |
self.typeStr = "long" |
| 130 |
self.typeCode = "l" |
| 131 |
|
| 132 |
###################################################################### |
| 133 |
|
| 134 |
class ulongTestCase(FlatTestCase): |
| 135 |
def __init__(self, methodName="runTest"): |
| 136 |
FlatTestCase.__init__(self, methodName) |
| 137 |
self.typeStr = "ulong" |
| 138 |
self.typeCode = "L" |
| 139 |
|
| 140 |
###################################################################### |
| 141 |
|
| 142 |
class longLongTestCase(FlatTestCase): |
| 143 |
def __init__(self, methodName="runTest"): |
| 144 |
FlatTestCase.__init__(self, methodName) |
| 145 |
self.typeStr = "longLong" |
| 146 |
self.typeCode = "q" |
| 147 |
|
| 148 |
###################################################################### |
| 149 |
|
| 150 |
class ulongLongTestCase(FlatTestCase): |
| 151 |
def __init__(self, methodName="runTest"): |
| 152 |
FlatTestCase.__init__(self, methodName) |
| 153 |
self.typeStr = "ulongLong" |
| 154 |
self.typeCode = "Q" |
| 155 |
|
| 156 |
###################################################################### |
| 157 |
|
| 158 |
class floatTestCase(FlatTestCase): |
| 159 |
def __init__(self, methodName="runTest"): |
| 160 |
FlatTestCase.__init__(self, methodName) |
| 161 |
self.typeStr = "float" |
| 162 |
self.typeCode = "f" |
| 163 |
|
| 164 |
###################################################################### |
| 165 |
|
| 166 |
class doubleTestCase(FlatTestCase): |
| 167 |
def __init__(self, methodName="runTest"): |
| 168 |
FlatTestCase.__init__(self, methodName) |
| 169 |
self.typeStr = "double" |
| 170 |
self.typeCode = "d" |
| 171 |
|
| 172 |
###################################################################### |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
|
| 177 |
# Build the test suite |
| 178 |
suite = unittest.TestSuite() |
| 179 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( scharTestCase)) |
| 180 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ucharTestCase)) |
| 181 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( shortTestCase)) |
| 182 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ushortTestCase)) |
| 183 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( intTestCase)) |
| 184 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( uintTestCase)) |
| 185 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longTestCase)) |
| 186 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ulongTestCase)) |
| 187 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longLongTestCase)) |
| 188 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ulongLongTestCase)) |
| 189 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( floatTestCase)) |
| 190 |
suite.addTest(unittest.TestLoader().loadTestsFromTestCase( doubleTestCase)) |
| 191 |
|
| 192 |
# Execute the test suite |
| 193 |
print("Testing 1D Functions of Module Flat") |
| 194 |
print("NumPy version", np.__version__) |
| 195 |
print() |
| 196 |
result = unittest.TextTestRunner(verbosity=2).run(suite) |
| 197 |
sys.exit(bool(result.errors + result.failures)) |