You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.9 KiB

  1. import pytest
  2. with pytest.suppress(ImportError):
  3. import numpy as np
  4. @pytest.requires_numpy
  5. def test_vectorize(capture):
  6. from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
  7. assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j])
  8. for f in [vectorized_func, vectorized_func2]:
  9. with capture:
  10. assert np.isclose(f(1, 2, 3), 6)
  11. assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
  12. with capture:
  13. assert np.isclose(f(np.array(1), np.array(2), 3), 6)
  14. assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
  15. with capture:
  16. assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
  17. assert capture == """
  18. my_func(x:int=1, y:float=2, z:float=3)
  19. my_func(x:int=3, y:float=4, z:float=3)
  20. """
  21. with capture:
  22. a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
  23. assert np.allclose(f(a, b, c), a * b * c)
  24. assert capture == """
  25. my_func(x:int=1, y:float=2, z:float=3)
  26. my_func(x:int=3, y:float=4, z:float=3)
  27. my_func(x:int=5, y:float=6, z:float=3)
  28. my_func(x:int=7, y:float=8, z:float=3)
  29. my_func(x:int=9, y:float=10, z:float=3)
  30. my_func(x:int=11, y:float=12, z:float=3)
  31. """
  32. with capture:
  33. a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
  34. assert np.allclose(f(a, b, c), a * b * c)
  35. assert capture == """
  36. my_func(x:int=1, y:float=2, z:float=2)
  37. my_func(x:int=2, y:float=3, z:float=2)
  38. my_func(x:int=3, y:float=4, z:float=2)
  39. my_func(x:int=4, y:float=2, z:float=2)
  40. my_func(x:int=5, y:float=3, z:float=2)
  41. my_func(x:int=6, y:float=4, z:float=2)
  42. """
  43. with capture:
  44. a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
  45. assert np.allclose(f(a, b, c), a * b * c)
  46. assert capture == """
  47. my_func(x:int=1, y:float=2, z:float=2)
  48. my_func(x:int=2, y:float=2, z:float=2)
  49. my_func(x:int=3, y:float=2, z:float=2)
  50. my_func(x:int=4, y:float=3, z:float=2)
  51. my_func(x:int=5, y:float=3, z:float=2)
  52. my_func(x:int=6, y:float=3, z:float=2)
  53. """
  54. @pytest.requires_numpy
  55. def test_type_selection():
  56. from pybind11_tests import selective_func
  57. assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
  58. assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
  59. assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
  60. @pytest.requires_numpy
  61. def test_docs(doc):
  62. from pybind11_tests import vectorized_func
  63. assert doc(vectorized_func) == "vectorized_func(arg0: numpy.ndarray[int], arg1: numpy.ndarray[float], arg2: numpy.ndarray[float]) -> object"