test_regression.py 5.7 KB

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