codekingpro/portable-devtools
115k
1import pytest
2
3import numpy as np
4
5from . import util
6
7
8class TestParameters(util.F2PyTest):
9 # Check that intent(in out) translates as intent(inout)
10 sources = [
11 util.getpath("tests", "src", "parameter", "constant_real.f90"),
12 util.getpath("tests", "src", "parameter", "constant_integer.f90"),
13 util.getpath("tests", "src", "parameter", "constant_both.f90"),
14 util.getpath("tests", "src", "parameter", "constant_compound.f90"),
15 util.getpath("tests", "src", "parameter", "constant_non_compound.f90"),
16 util.getpath("tests", "src", "parameter", "constant_array.f90"),
17 ]
18
19 @pytest.mark.slow
20 def test_constant_real_single(self):
21 # non-contiguous should raise error
22 x = np.arange(6, dtype=np.float32)[::2]
23 pytest.raises(ValueError, self.module.foo_single, x)
24
25 # check values with contiguous array
26 x = np.arange(3, dtype=np.float32)
27 self.module.foo_single(x)
28 assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
29
30 @pytest.mark.slow
31 def test_constant_real_double(self):
32 # non-contiguous should raise error
33 x = np.arange(6, dtype=np.float64)[::2]
34 pytest.raises(ValueError, self.module.foo_double, x)
35
36 # check values with contiguous array
37 x = np.arange(3, dtype=np.float64)
38 self.module.foo_double(x)
39 assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
40
41 @pytest.mark.slow
42 def test_constant_compound_int(self):
43 # non-contiguous should raise error
44 x = np.arange(6, dtype=np.int32)[::2]
45 pytest.raises(ValueError, self.module.foo_compound_int, x)
46
47 # check values with contiguous array
48 x = np.arange(3, dtype=np.int32)
49 self.module.foo_compound_int(x)
50 assert np.allclose(x, [0 + 1 + 2 * 6, 1, 2])
51
52 @pytest.mark.slow
53 def test_constant_non_compound_int(self):
54 # check values
55 x = np.arange(4, dtype=np.int32)
56 self.module.foo_non_compound_int(x)
57 assert np.allclose(x, [0 + 1 + 2 + 3 * 4, 1, 2, 3])
58
59 @pytest.mark.slow
60 def test_constant_integer_int(self):
61 # non-contiguous should raise error
62 x = np.arange(6, dtype=np.int32)[::2]
63 pytest.raises(ValueError, self.module.foo_int, x)
64
65 # check values with contiguous array
66 x = np.arange(3, dtype=np.int32)
67 self.module.foo_int(x)
68 assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
69
70 @pytest.mark.slow
71 def test_constant_integer_long(self):
72 # non-contiguous should raise error
73 x = np.arange(6, dtype=np.int64)[::2]
74 pytest.raises(ValueError, self.module.foo_long, x)
75
76 # check values with contiguous array
77 x = np.arange(3, dtype=np.int64)
78 self.module.foo_long(x)
79 assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
80
81 @pytest.mark.slow
82 def test_constant_both(self):
83 # non-contiguous should raise error
84 x = np.arange(6, dtype=np.float64)[::2]
85 pytest.raises(ValueError, self.module.foo, x)
86
87 # check values with contiguous array
88 x = np.arange(3, dtype=np.float64)
89 self.module.foo(x)
90 assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
91
92 @pytest.mark.slow
93 def test_constant_no(self):
94 # non-contiguous should raise error
95 x = np.arange(6, dtype=np.float64)[::2]
96 pytest.raises(ValueError, self.module.foo_no, x)
97
98 # check values with contiguous array
99 x = np.arange(3, dtype=np.float64)
100 self.module.foo_no(x)
101 assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
102
103 @pytest.mark.slow
104 def test_constant_sum(self):
105 # non-contiguous should raise error
106 x = np.arange(6, dtype=np.float64)[::2]
107 pytest.raises(ValueError, self.module.foo_sum, x)
108
109 # check values with contiguous array
110 x = np.arange(3, dtype=np.float64)
111 self.module.foo_sum(x)
112 assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
113
114 def test_constant_array(self):
115 x = np.arange(3, dtype=np.float64)
116 y = np.arange(5, dtype=np.float64)
117 z = self.module.foo_array(x, y)
118 assert np.allclose(x, [0.0, 1. / 10, 2. / 10])
119 assert np.allclose(y, [0.0, 1. * 10, 2. * 10, 3. * 10, 4. * 10])
120 assert np.allclose(z, 19.0)
121
122 def test_constant_array_any_index(self):
123 x = np.arange(6, dtype=np.float64)
124 y = self.module.foo_array_any_index(x)
125 assert np.allclose(y, x.reshape((2, 3), order='F'))
126
127 def test_constant_array_delims(self):
128 x = self.module.foo_array_delims()
129 assert x == 9
130 