test_return_logical.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import pytest
  2. from numpy import array
  3. from numpy.testing import assert_, assert_raises
  4. from . import util
  5. class TestReturnLogical(util.F2PyTest):
  6. def check_function(self, t):
  7. assert_(t(True) == 1, repr(t(True)))
  8. assert_(t(False) == 0, repr(t(False)))
  9. assert_(t(0) == 0)
  10. assert_(t(None) == 0)
  11. assert_(t(0.0) == 0)
  12. assert_(t(0j) == 0)
  13. assert_(t(1j) == 1)
  14. assert_(t(234) == 1)
  15. assert_(t(234.6) == 1)
  16. assert_(t(234.6 + 3j) == 1)
  17. assert_(t('234') == 1)
  18. assert_(t('aaa') == 1)
  19. assert_(t('') == 0)
  20. assert_(t([]) == 0)
  21. assert_(t(()) == 0)
  22. assert_(t({}) == 0)
  23. assert_(t(t) == 1)
  24. assert_(t(-234) == 1)
  25. assert_(t(10 ** 100) == 1)
  26. assert_(t([234]) == 1)
  27. assert_(t((234,)) == 1)
  28. assert_(t(array(234)) == 1)
  29. assert_(t(array([234])) == 1)
  30. assert_(t(array([[234]])) == 1)
  31. assert_(t(array([234], 'b')) == 1)
  32. assert_(t(array([234], 'h')) == 1)
  33. assert_(t(array([234], 'i')) == 1)
  34. assert_(t(array([234], 'l')) == 1)
  35. assert_(t(array([234], 'f')) == 1)
  36. assert_(t(array([234], 'd')) == 1)
  37. assert_(t(array([234 + 3j], 'F')) == 1)
  38. assert_(t(array([234], 'D')) == 1)
  39. assert_(t(array(0)) == 0)
  40. assert_(t(array([0])) == 0)
  41. assert_(t(array([[0]])) == 0)
  42. assert_(t(array([0j])) == 0)
  43. assert_(t(array([1])) == 1)
  44. assert_raises(ValueError, t, array([0, 0]))
  45. class TestF77ReturnLogical(TestReturnLogical):
  46. code = """
  47. function t0(value)
  48. logical value
  49. logical t0
  50. t0 = value
  51. end
  52. function t1(value)
  53. logical*1 value
  54. logical*1 t1
  55. t1 = value
  56. end
  57. function t2(value)
  58. logical*2 value
  59. logical*2 t2
  60. t2 = value
  61. end
  62. function t4(value)
  63. logical*4 value
  64. logical*4 t4
  65. t4 = value
  66. end
  67. c function t8(value)
  68. c logical*8 value
  69. c logical*8 t8
  70. c t8 = value
  71. c end
  72. subroutine s0(t0,value)
  73. logical value
  74. logical t0
  75. cf2py intent(out) t0
  76. t0 = value
  77. end
  78. subroutine s1(t1,value)
  79. logical*1 value
  80. logical*1 t1
  81. cf2py intent(out) t1
  82. t1 = value
  83. end
  84. subroutine s2(t2,value)
  85. logical*2 value
  86. logical*2 t2
  87. cf2py intent(out) t2
  88. t2 = value
  89. end
  90. subroutine s4(t4,value)
  91. logical*4 value
  92. logical*4 t4
  93. cf2py intent(out) t4
  94. t4 = value
  95. end
  96. c subroutine s8(t8,value)
  97. c logical*8 value
  98. c logical*8 t8
  99. cf2py intent(out) t8
  100. c t8 = value
  101. c end
  102. """
  103. @pytest.mark.slow
  104. @pytest.mark.parametrize('name', 't0,t1,t2,t4,s0,s1,s2,s4'.split(','))
  105. def test_all(self, name):
  106. self.check_function(getattr(self.module, name))
  107. class TestF90ReturnLogical(TestReturnLogical):
  108. suffix = ".f90"
  109. code = """
  110. module f90_return_logical
  111. contains
  112. function t0(value)
  113. logical :: value
  114. logical :: t0
  115. t0 = value
  116. end function t0
  117. function t1(value)
  118. logical(kind=1) :: value
  119. logical(kind=1) :: t1
  120. t1 = value
  121. end function t1
  122. function t2(value)
  123. logical(kind=2) :: value
  124. logical(kind=2) :: t2
  125. t2 = value
  126. end function t2
  127. function t4(value)
  128. logical(kind=4) :: value
  129. logical(kind=4) :: t4
  130. t4 = value
  131. end function t4
  132. function t8(value)
  133. logical(kind=8) :: value
  134. logical(kind=8) :: t8
  135. t8 = value
  136. end function t8
  137. subroutine s0(t0,value)
  138. logical :: value
  139. logical :: t0
  140. !f2py intent(out) t0
  141. t0 = value
  142. end subroutine s0
  143. subroutine s1(t1,value)
  144. logical(kind=1) :: value
  145. logical(kind=1) :: t1
  146. !f2py intent(out) t1
  147. t1 = value
  148. end subroutine s1
  149. subroutine s2(t2,value)
  150. logical(kind=2) :: value
  151. logical(kind=2) :: t2
  152. !f2py intent(out) t2
  153. t2 = value
  154. end subroutine s2
  155. subroutine s4(t4,value)
  156. logical(kind=4) :: value
  157. logical(kind=4) :: t4
  158. !f2py intent(out) t4
  159. t4 = value
  160. end subroutine s4
  161. subroutine s8(t8,value)
  162. logical(kind=8) :: value
  163. logical(kind=8) :: t8
  164. !f2py intent(out) t8
  165. t8 = value
  166. end subroutine s8
  167. end module f90_return_logical
  168. """
  169. @pytest.mark.slow
  170. @pytest.mark.parametrize('name',
  171. 't0,t1,t2,t4,t8,s0,s1,s2,s4,s8'.split(','))
  172. def test_all(self, name):
  173. self.check_function(getattr(self.module.f90_return_logical, name))