test_docs.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import pytest
  3. import numpy as np
  4. from numpy.testing import assert_array_equal, assert_equal
  5. from . import util
  6. def get_docdir():
  7. # assuming that documentation tests are run from a source
  8. # directory
  9. return os.path.abspath(os.path.join(
  10. os.path.dirname(__file__),
  11. '..', '..', '..',
  12. 'doc', 'source', 'f2py', 'code'))
  13. pytestmark = pytest.mark.skipif(
  14. not os.path.isdir(get_docdir()),
  15. reason=('Could not find f2py documentation sources'
  16. f' ({get_docdir()} does not exists)'))
  17. def _path(*a):
  18. return os.path.join(*((get_docdir(),) + a))
  19. class TestDocAdvanced(util.F2PyTest):
  20. # options = ['--debug-capi', '--build-dir', '/tmp/build-f2py']
  21. sources = [_path('asterisk1.f90'), _path('asterisk2.f90'),
  22. _path('ftype.f')]
  23. def test_asterisk1(self):
  24. foo = getattr(self.module, 'foo1')
  25. assert_equal(foo(), b'123456789A12')
  26. def test_asterisk2(self):
  27. foo = getattr(self.module, 'foo2')
  28. assert_equal(foo(2), b'12')
  29. assert_equal(foo(12), b'123456789A12')
  30. assert_equal(foo(24), b'123456789A123456789B')
  31. def test_ftype(self):
  32. ftype = self.module
  33. ftype.foo()
  34. assert_equal(ftype.data.a, 0)
  35. ftype.data.a = 3
  36. ftype.data.x = [1, 2, 3]
  37. assert_equal(ftype.data.a, 3)
  38. assert_array_equal(ftype.data.x,
  39. np.array([1, 2, 3], dtype=np.float32))
  40. ftype.data.x[1] = 45
  41. assert_array_equal(ftype.data.x,
  42. np.array([1, 45, 3], dtype=np.float32))
  43. # TODO: implement test methods for other example Fortran codes