test_parameter.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import pytest
  3. import numpy as np
  4. from numpy.testing import assert_raises, assert_equal
  5. from . import util
  6. def _path(*a):
  7. return os.path.join(*((os.path.dirname(__file__),) + a))
  8. class TestParameters(util.F2PyTest):
  9. # Check that intent(in out) translates as intent(inout)
  10. sources = [_path('src', 'parameter', 'constant_real.f90'),
  11. _path('src', 'parameter', 'constant_integer.f90'),
  12. _path('src', 'parameter', 'constant_both.f90'),
  13. _path('src', 'parameter', 'constant_compound.f90'),
  14. _path('src', 'parameter', 'constant_non_compound.f90'),
  15. ]
  16. @pytest.mark.slow
  17. def test_constant_real_single(self):
  18. # non-contiguous should raise error
  19. x = np.arange(6, dtype=np.float32)[::2]
  20. assert_raises(ValueError, self.module.foo_single, x)
  21. # check values with contiguous array
  22. x = np.arange(3, dtype=np.float32)
  23. self.module.foo_single(x)
  24. assert_equal(x, [0 + 1 + 2*3, 1, 2])
  25. @pytest.mark.slow
  26. def test_constant_real_double(self):
  27. # non-contiguous should raise error
  28. x = np.arange(6, dtype=np.float64)[::2]
  29. assert_raises(ValueError, self.module.foo_double, x)
  30. # check values with contiguous array
  31. x = np.arange(3, dtype=np.float64)
  32. self.module.foo_double(x)
  33. assert_equal(x, [0 + 1 + 2*3, 1, 2])
  34. @pytest.mark.slow
  35. def test_constant_compound_int(self):
  36. # non-contiguous should raise error
  37. x = np.arange(6, dtype=np.int32)[::2]
  38. assert_raises(ValueError, self.module.foo_compound_int, x)
  39. # check values with contiguous array
  40. x = np.arange(3, dtype=np.int32)
  41. self.module.foo_compound_int(x)
  42. assert_equal(x, [0 + 1 + 2*6, 1, 2])
  43. @pytest.mark.slow
  44. def test_constant_non_compound_int(self):
  45. # check values
  46. x = np.arange(4, dtype=np.int32)
  47. self.module.foo_non_compound_int(x)
  48. assert_equal(x, [0 + 1 + 2 + 3*4, 1, 2, 3])
  49. @pytest.mark.slow
  50. def test_constant_integer_int(self):
  51. # non-contiguous should raise error
  52. x = np.arange(6, dtype=np.int32)[::2]
  53. assert_raises(ValueError, self.module.foo_int, x)
  54. # check values with contiguous array
  55. x = np.arange(3, dtype=np.int32)
  56. self.module.foo_int(x)
  57. assert_equal(x, [0 + 1 + 2*3, 1, 2])
  58. @pytest.mark.slow
  59. def test_constant_integer_long(self):
  60. # non-contiguous should raise error
  61. x = np.arange(6, dtype=np.int64)[::2]
  62. assert_raises(ValueError, self.module.foo_long, x)
  63. # check values with contiguous array
  64. x = np.arange(3, dtype=np.int64)
  65. self.module.foo_long(x)
  66. assert_equal(x, [0 + 1 + 2*3, 1, 2])
  67. @pytest.mark.slow
  68. def test_constant_both(self):
  69. # non-contiguous should raise error
  70. x = np.arange(6, dtype=np.float64)[::2]
  71. assert_raises(ValueError, self.module.foo, x)
  72. # check values with contiguous array
  73. x = np.arange(3, dtype=np.float64)
  74. self.module.foo(x)
  75. assert_equal(x, [0 + 1*3*3 + 2*3*3, 1*3, 2*3])
  76. @pytest.mark.slow
  77. def test_constant_no(self):
  78. # non-contiguous should raise error
  79. x = np.arange(6, dtype=np.float64)[::2]
  80. assert_raises(ValueError, self.module.foo_no, x)
  81. # check values with contiguous array
  82. x = np.arange(3, dtype=np.float64)
  83. self.module.foo_no(x)
  84. assert_equal(x, [0 + 1*3*3 + 2*3*3, 1*3, 2*3])
  85. @pytest.mark.slow
  86. def test_constant_sum(self):
  87. # non-contiguous should raise error
  88. x = np.arange(6, dtype=np.float64)[::2]
  89. assert_raises(ValueError, self.module.foo_sum, x)
  90. # check values with contiguous array
  91. x = np.arange(3, dtype=np.float64)
  92. self.module.foo_sum(x)
  93. assert_equal(x, [0 + 1*3*3 + 2*3*3, 1*3, 2*3])