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