test_parameter.py 3.8 KB

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