summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_numpy_vectorize.py
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commitc79706ff4ce591df2151db5504d3c224f3c9965f (patch)
treeb56cd2bfe704a40575a71075e78194a4c516c98d /ext/pybind11/tests/test_numpy_vectorize.py
parent359cb08623324b62d7c34973ae54d5bc7f23f9fd (diff)
downloadgem5-c79706ff4ce591df2151db5504d3c224f3c9965f.tar.xz
ext: Add pybind rev f4b81b3
Change-Id: I52e4fc9ebf2f59da57d8cf8f3e37cc79598c2f5f Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Curtis Dunham <curtis.dunham@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2229 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr>
Diffstat (limited to 'ext/pybind11/tests/test_numpy_vectorize.py')
-rw-r--r--ext/pybind11/tests/test_numpy_vectorize.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_numpy_vectorize.py b/ext/pybind11/tests/test_numpy_vectorize.py
new file mode 100644
index 000000000..718646efa
--- /dev/null
+++ b/ext/pybind11/tests/test_numpy_vectorize.py
@@ -0,0 +1,76 @@
+import pytest
+
+with pytest.suppress(ImportError):
+ import numpy as np
+
+
+@pytest.requires_numpy
+def test_vectorize(capture):
+ from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
+
+ assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j])
+
+ for f in [vectorized_func, vectorized_func2]:
+ with capture:
+ assert np.isclose(f(1, 2, 3), 6)
+ assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
+ with capture:
+ assert np.isclose(f(np.array(1), np.array(2), 3), 6)
+ assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
+ with capture:
+ assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
+ assert capture == """
+ my_func(x:int=1, y:float=2, z:float=3)
+ my_func(x:int=3, y:float=4, z:float=3)
+ """
+ with capture:
+ a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
+ assert np.allclose(f(a, b, c), a * b * c)
+ assert capture == """
+ my_func(x:int=1, y:float=2, z:float=3)
+ my_func(x:int=3, y:float=4, z:float=3)
+ my_func(x:int=5, y:float=6, z:float=3)
+ my_func(x:int=7, y:float=8, z:float=3)
+ my_func(x:int=9, y:float=10, z:float=3)
+ my_func(x:int=11, y:float=12, z:float=3)
+ """
+ with capture:
+ a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
+ assert np.allclose(f(a, b, c), a * b * c)
+ assert capture == """
+ my_func(x:int=1, y:float=2, z:float=2)
+ my_func(x:int=2, y:float=3, z:float=2)
+ my_func(x:int=3, y:float=4, z:float=2)
+ my_func(x:int=4, y:float=2, z:float=2)
+ my_func(x:int=5, y:float=3, z:float=2)
+ my_func(x:int=6, y:float=4, z:float=2)
+ """
+ with capture:
+ a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
+ assert np.allclose(f(a, b, c), a * b * c)
+ assert capture == """
+ my_func(x:int=1, y:float=2, z:float=2)
+ my_func(x:int=2, y:float=2, z:float=2)
+ my_func(x:int=3, y:float=2, z:float=2)
+ my_func(x:int=4, y:float=3, z:float=2)
+ my_func(x:int=5, y:float=3, z:float=2)
+ my_func(x:int=6, y:float=3, z:float=2)
+ """
+
+
+@pytest.requires_numpy
+def test_type_selection():
+ from pybind11_tests import selective_func
+
+ assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
+ assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
+ assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
+
+
+@pytest.requires_numpy
+def test_docs(doc):
+ from pybind11_tests import vectorized_func
+
+ assert doc(vectorized_func) == """
+ vectorized_func(arg0: numpy.ndarray[int], arg1: numpy.ndarray[float], arg2: numpy.ndarray[float]) -> object
+ """ # noqa: E501 line too long