test_regression.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import os
  2. import platform
  3. import pytest
  4. import numpy as np
  5. import numpy.testing as npt
  6. from . import util
  7. class TestIntentInOut(util.F2PyTest):
  8. # Check that intent(in out) translates as intent(inout)
  9. sources = [util.getpath("tests", "src", "regression", "inout.f90")]
  10. @pytest.mark.slow
  11. def test_inout(self):
  12. # non-contiguous should raise error
  13. x = np.arange(6, dtype=np.float32)[::2]
  14. pytest.raises(ValueError, self.module.foo, x)
  15. # check values with contiguous array
  16. x = np.arange(3, dtype=np.float32)
  17. self.module.foo(x)
  18. assert np.allclose(x, [3, 1, 2])
  19. class TestDataOnlyMultiModule(util.F2PyTest):
  20. # Check that modules without subroutines work
  21. sources = [util.getpath("tests", "src", "regression", "datonly.f90")]
  22. @pytest.mark.slow
  23. def test_mdat(self):
  24. assert self.module.datonly.max_value == 100
  25. assert self.module.dat.max_ == 1009
  26. int_in = 5
  27. assert self.module.simple_subroutine(5) == 1014
  28. class TestModuleWithDerivedType(util.F2PyTest):
  29. # Check that modules with derived types work
  30. sources = [util.getpath("tests", "src", "regression", "mod_derived_types.f90")]
  31. @pytest.mark.slow
  32. def test_mtypes(self):
  33. assert self.module.no_type_subroutine(10) == 110
  34. assert self.module.type_subroutine(10) == 210
  35. class TestNegativeBounds(util.F2PyTest):
  36. # Check that negative bounds work correctly
  37. sources = [util.getpath("tests", "src", "negative_bounds", "issue_20853.f90")]
  38. @pytest.mark.slow
  39. def test_negbound(self):
  40. xvec = np.arange(12)
  41. xlow = -6
  42. xhigh = 4
  43. # Calculate the upper bound,
  44. # Keeping the 1 index in mind
  45. def ubound(xl, xh):
  46. return xh - xl + 1
  47. rval = self.module.foo(is_=xlow, ie_=xhigh,
  48. arr=xvec[:ubound(xlow, xhigh)])
  49. expval = np.arange(11, dtype=np.float32)
  50. assert np.allclose(rval, expval)
  51. class TestNumpyVersionAttribute(util.F2PyTest):
  52. # Check that th attribute __f2py_numpy_version__ is present
  53. # in the compiled module and that has the value np.__version__.
  54. sources = [util.getpath("tests", "src", "regression", "inout.f90")]
  55. @pytest.mark.slow
  56. def test_numpy_version_attribute(self):
  57. # Check that self.module has an attribute named "__f2py_numpy_version__"
  58. assert hasattr(self.module, "__f2py_numpy_version__")
  59. # Check that the attribute __f2py_numpy_version__ is a string
  60. assert isinstance(self.module.__f2py_numpy_version__, str)
  61. # Check that __f2py_numpy_version__ has the value numpy.__version__
  62. assert np.__version__ == self.module.__f2py_numpy_version__
  63. def test_include_path():
  64. incdir = np.f2py.get_include()
  65. fnames_in_dir = os.listdir(incdir)
  66. for fname in ("fortranobject.c", "fortranobject.h"):
  67. assert fname in fnames_in_dir
  68. class TestIncludeFiles(util.F2PyTest):
  69. sources = [util.getpath("tests", "src", "regression", "incfile.f90")]
  70. options = [f"-I{util.getpath('tests', 'src', 'regression')}",
  71. f"--include-paths {util.getpath('tests', 'src', 'regression')}"]
  72. @pytest.mark.slow
  73. def test_gh25344(self):
  74. exp = 7.0
  75. res = self.module.add(3.0, 4.0)
  76. assert exp == res
  77. class TestF77Comments(util.F2PyTest):
  78. # Check that comments are stripped from F77 continuation lines
  79. sources = [util.getpath("tests", "src", "regression", "f77comments.f")]
  80. @pytest.mark.slow
  81. def test_gh26148(self):
  82. x1 = np.array(3, dtype=np.int32)
  83. x2 = np.array(5, dtype=np.int32)
  84. res = self.module.testsub(x1, x2)
  85. assert res[0] == 8
  86. assert res[1] == 15
  87. @pytest.mark.slow
  88. def test_gh26466(self):
  89. # Check that comments after PARAMETER directions are stripped
  90. expected = np.arange(1, 11, dtype=np.float32) * 2
  91. res = self.module.testsub2()
  92. npt.assert_allclose(expected, res)
  93. class TestF90Contiuation(util.F2PyTest):
  94. # Check that comments are stripped from F90 continuation lines
  95. sources = [util.getpath("tests", "src", "regression", "f90continuation.f90")]
  96. @pytest.mark.slow
  97. def test_gh26148b(self):
  98. x1 = np.array(3, dtype=np.int32)
  99. x2 = np.array(5, dtype=np.int32)
  100. res = self.module.testsub(x1, x2)
  101. assert res[0] == 8
  102. assert res[1] == 15
  103. class TestLowerF2PYDirectives(util.F2PyTest):
  104. # Check variables are cased correctly
  105. sources = [util.getpath("tests", "src", "regression", "lower_f2py_fortran.f90")]
  106. @pytest.mark.slow
  107. def test_gh28014(self):
  108. self.module.inquire_next(3)
  109. assert True
  110. @pytest.mark.slow
  111. def test_gh26623():
  112. # Including libraries with . should not generate an incorrect meson.build
  113. try:
  114. aa = util.build_module(
  115. [util.getpath("tests", "src", "regression", "f90continuation.f90")],
  116. ["-lfoo.bar"],
  117. module_name="Blah",
  118. )
  119. except RuntimeError as rerr:
  120. assert "lparen got assign" not in str(rerr)
  121. @pytest.mark.slow
  122. @pytest.mark.skipif(platform.system() == "Windows", reason='Unsupported on this platform for now')
  123. def test_gh25784():
  124. # Compile dubious file using passed flags
  125. try:
  126. aa = util.build_module(
  127. [util.getpath("tests", "src", "regression", "f77fixedform.f95")],
  128. options=[
  129. # Meson will collect and dedup these to pass to fortran_args:
  130. "--f77flags='-ffixed-form -O2'",
  131. "--f90flags=\"-ffixed-form -g\"",
  132. ],
  133. module_name="Blah",
  134. )
  135. except ImportError as rerr:
  136. assert "unknown_subroutine_" in str(rerr)
  137. @pytest.mark.slow
  138. class TestAssignmentOnlyModules(util.F2PyTest):
  139. # Ensure that variables are exposed without functions or subroutines in a module
  140. sources = [util.getpath("tests", "src", "regression", "assignOnlyModule.f90")]
  141. @pytest.mark.slow
  142. def test_gh27167(self):
  143. assert (self.module.f_globals.n_max == 16)
  144. assert (self.module.f_globals.i_max == 18)
  145. assert (self.module.f_globals.j_max == 72)