Debian Codegraph @

tools/swig/test/testFortran.py

raw
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 Fortran
14
15 ######################################################################
16
17 class FortranTestCase(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_FARRAY2, int DIM1, int DIM2) typemap
25 def testSecondElementFortran(self):
26 "Test Fortran matrix initialized from reshaped NumPy fortranarray"
27 print(self.typeStr, "... ", end=' ', file=sys.stderr)
28 second = Fortran.__dict__[self.typeStr + "SecondElement"]
29 matrix = np.asfortranarray(np.arange(9).reshape(3, 3),
30 self.typeCode)
31 self.assertEqual(second(matrix), 3)
32
33 def testSecondElementObject(self):
34 "Test Fortran matrix initialized from nested list fortranarray"
35 print(self.typeStr, "... ", end=' ', file=sys.stderr)
36 second = Fortran.__dict__[self.typeStr + "SecondElement"]
37 matrix = np.asfortranarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]], self.typeCode)
38 self.assertEqual(second(matrix), 3)
39
40 ######################################################################
41
42 class scharTestCase(FortranTestCase):
43 def __init__(self, methodName="runTest"):
44 FortranTestCase.__init__(self, methodName)
45 self.typeStr = "schar"
46 self.typeCode = "b"
47
48 ######################################################################
49
50 class ucharTestCase(FortranTestCase):
51 def __init__(self, methodName="runTest"):
52 FortranTestCase.__init__(self, methodName)
53 self.typeStr = "uchar"
54 self.typeCode = "B"
55
56 ######################################################################
57
58 class shortTestCase(FortranTestCase):
59 def __init__(self, methodName="runTest"):
60 FortranTestCase.__init__(self, methodName)
61 self.typeStr = "short"
62 self.typeCode = "h"
63
64 ######################################################################
65
66 class ushortTestCase(FortranTestCase):
67 def __init__(self, methodName="runTest"):
68 FortranTestCase.__init__(self, methodName)
69 self.typeStr = "ushort"
70 self.typeCode = "H"
71
72 ######################################################################
73
74 class intTestCase(FortranTestCase):
75 def __init__(self, methodName="runTest"):
76 FortranTestCase.__init__(self, methodName)
77 self.typeStr = "int"
78 self.typeCode = "i"
79
80 ######################################################################
81
82 class uintTestCase(FortranTestCase):
83 def __init__(self, methodName="runTest"):
84 FortranTestCase.__init__(self, methodName)
85 self.typeStr = "uint"
86 self.typeCode = "I"
87
88 ######################################################################
89
90 class longTestCase(FortranTestCase):
91 def __init__(self, methodName="runTest"):
92 FortranTestCase.__init__(self, methodName)
93 self.typeStr = "long"
94 self.typeCode = "l"
95
96 ######################################################################
97
98 class ulongTestCase(FortranTestCase):
99 def __init__(self, methodName="runTest"):
100 FortranTestCase.__init__(self, methodName)
101 self.typeStr = "ulong"
102 self.typeCode = "L"
103
104 ######################################################################
105
106 class longLongTestCase(FortranTestCase):
107 def __init__(self, methodName="runTest"):
108 FortranTestCase.__init__(self, methodName)
109 self.typeStr = "longLong"
110 self.typeCode = "q"
111
112 ######################################################################
113
114 class ulongLongTestCase(FortranTestCase):
115 def __init__(self, methodName="runTest"):
116 FortranTestCase.__init__(self, methodName)
117 self.typeStr = "ulongLong"
118 self.typeCode = "Q"
119
120 ######################################################################
121
122 class floatTestCase(FortranTestCase):
123 def __init__(self, methodName="runTest"):
124 FortranTestCase.__init__(self, methodName)
125 self.typeStr = "float"
126 self.typeCode = "f"
127
128 ######################################################################
129
130 class doubleTestCase(FortranTestCase):
131 def __init__(self, methodName="runTest"):
132 FortranTestCase.__init__(self, methodName)
133 self.typeStr = "double"
134 self.typeCode = "d"
135
136 ######################################################################
137
138
139 if __name__ == "__main__":
140
141 # Build the test suite
142 suite = unittest.TestSuite()
143 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( scharTestCase))
144 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ucharTestCase))
145 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( shortTestCase))
146 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ushortTestCase))
147 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( intTestCase))
148 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( uintTestCase))
149 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longTestCase))
150 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( ulongTestCase))
151 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( longLongTestCase))
152 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ulongLongTestCase))
153 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( floatTestCase))
154 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( doubleTestCase))
155
156 # Execute the test suite
157 print("Testing 2D Functions of Module Matrix")
158 print("NumPy version", np.__version__)
159 print()
160 result = unittest.TextTestRunner(verbosity=2).run(suite)
161 sys.exit(bool(result.errors + result.failures))