Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
Jason Rhinelander | 2a75784 | 2017-01-24 11:26:51 -0500 | [diff] [blame] | 3 | pytestmark = pytest.requires_numpy |
| 4 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 5 | with pytest.suppress(ImportError): |
| 6 | import numpy as np |
| 7 | |
| 8 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 9 | def test_vectorize(capture): |
| 10 | from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3 |
| 11 | |
| 12 | assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j]) |
| 13 | |
| 14 | for f in [vectorized_func, vectorized_func2]: |
| 15 | with capture: |
| 16 | assert np.isclose(f(1, 2, 3), 6) |
| 17 | assert capture == "my_func(x:int=1, y:float=2, z:float=3)" |
| 18 | with capture: |
| 19 | assert np.isclose(f(np.array(1), np.array(2), 3), 6) |
| 20 | assert capture == "my_func(x:int=1, y:float=2, z:float=3)" |
| 21 | with capture: |
| 22 | assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36]) |
| 23 | assert capture == """ |
| 24 | my_func(x:int=1, y:float=2, z:float=3) |
| 25 | my_func(x:int=3, y:float=4, z:float=3) |
| 26 | """ |
| 27 | with capture: |
| 28 | a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3 |
| 29 | assert np.allclose(f(a, b, c), a * b * c) |
| 30 | assert capture == """ |
| 31 | my_func(x:int=1, y:float=2, z:float=3) |
| 32 | my_func(x:int=3, y:float=4, z:float=3) |
| 33 | my_func(x:int=5, y:float=6, z:float=3) |
| 34 | my_func(x:int=7, y:float=8, z:float=3) |
| 35 | my_func(x:int=9, y:float=10, z:float=3) |
| 36 | my_func(x:int=11, y:float=12, z:float=3) |
| 37 | """ |
| 38 | with capture: |
| 39 | a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2 |
| 40 | assert np.allclose(f(a, b, c), a * b * c) |
| 41 | assert capture == """ |
| 42 | my_func(x:int=1, y:float=2, z:float=2) |
| 43 | my_func(x:int=2, y:float=3, z:float=2) |
| 44 | my_func(x:int=3, y:float=4, z:float=2) |
| 45 | my_func(x:int=4, y:float=2, z:float=2) |
| 46 | my_func(x:int=5, y:float=3, z:float=2) |
| 47 | my_func(x:int=6, y:float=4, z:float=2) |
| 48 | """ |
| 49 | with capture: |
| 50 | a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2 |
| 51 | assert np.allclose(f(a, b, c), a * b * c) |
| 52 | assert capture == """ |
| 53 | my_func(x:int=1, y:float=2, z:float=2) |
| 54 | my_func(x:int=2, y:float=2, z:float=2) |
| 55 | my_func(x:int=3, y:float=2, z:float=2) |
| 56 | my_func(x:int=4, y:float=3, z:float=2) |
| 57 | my_func(x:int=5, y:float=3, z:float=2) |
| 58 | my_func(x:int=6, y:float=3, z:float=2) |
| 59 | """ |
Jason Rhinelander | ae5a8f7 | 2017-03-15 00:57:56 -0300 | [diff] [blame^] | 60 | with capture: |
| 61 | a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F'), np.array([[2], [3]]), 2 |
| 62 | assert np.allclose(f(a, b, c), a * b * c) |
| 63 | assert capture == """ |
| 64 | my_func(x:int=1, y:float=2, z:float=2) |
| 65 | my_func(x:int=2, y:float=2, z:float=2) |
| 66 | my_func(x:int=3, y:float=2, z:float=2) |
| 67 | my_func(x:int=4, y:float=3, z:float=2) |
| 68 | my_func(x:int=5, y:float=3, z:float=2) |
| 69 | my_func(x:int=6, y:float=3, z:float=2) |
| 70 | """ |
| 71 | with capture: |
| 72 | a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2 |
| 73 | assert np.allclose(f(a, b, c), a * b * c) |
| 74 | assert capture == """ |
| 75 | my_func(x:int=1, y:float=2, z:float=2) |
| 76 | my_func(x:int=3, y:float=2, z:float=2) |
| 77 | my_func(x:int=4, y:float=3, z:float=2) |
| 78 | my_func(x:int=6, y:float=3, z:float=2) |
| 79 | """ |
| 80 | with capture: |
| 81 | a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F')[::, ::2], np.array([[2], [3]]), 2 |
| 82 | assert np.allclose(f(a, b, c), a * b * c) |
| 83 | assert capture == """ |
| 84 | my_func(x:int=1, y:float=2, z:float=2) |
| 85 | my_func(x:int=3, y:float=2, z:float=2) |
| 86 | my_func(x:int=4, y:float=3, z:float=2) |
| 87 | my_func(x:int=6, y:float=3, z:float=2) |
| 88 | """ |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 89 | |
| 90 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 91 | def test_type_selection(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 92 | from pybind11_tests import selective_func |
| 93 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 94 | assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken." |
| 95 | assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken." |
| 96 | assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken." |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 97 | |
| 98 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 99 | def test_docs(doc): |
| 100 | from pybind11_tests import vectorized_func |
| 101 | |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 102 | assert doc(vectorized_func) == """ |
Dean Moldovan | 16afbce | 2017-03-13 19:17:18 +0100 | [diff] [blame] | 103 | vectorized_func(arg0: numpy.ndarray[int32], arg1: numpy.ndarray[float32], arg2: numpy.ndarray[float64]) -> object |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 104 | """ # noqa: E501 line too long |
Jason Rhinelander | ae5a8f7 | 2017-03-15 00:57:56 -0300 | [diff] [blame^] | 105 | |
| 106 | |
| 107 | def test_trivial_broadcasting(): |
| 108 | from pybind11_tests import vectorized_is_trivial |
| 109 | |
| 110 | assert vectorized_is_trivial(1, 2, 3) |
| 111 | assert vectorized_is_trivial(np.array(1), np.array(2), 3) |
| 112 | assert vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3) |
| 113 | assert vectorized_is_trivial( |
| 114 | np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3) |
| 115 | assert not vectorized_is_trivial( |
| 116 | np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2) |
| 117 | assert not vectorized_is_trivial( |
| 118 | np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2) |
| 119 | z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype='int32') |
| 120 | z2 = np.array(z1, dtype='float32') |
| 121 | z3 = np.array(z1, dtype='float64') |
| 122 | assert vectorized_is_trivial(z1, z2, z3) |
| 123 | assert not vectorized_is_trivial(z1[::2, ::2], 1, 1) |
| 124 | assert vectorized_is_trivial(1, 1, z1[::2, ::2]) |
| 125 | assert not vectorized_is_trivial(1, 1, z3[::2, ::2]) |
| 126 | assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) |
| 127 | |
| 128 | y1 = np.array(z1, order='F') |
| 129 | y2 = np.array(y1) |
| 130 | y3 = np.array(y1) |
| 131 | assert not vectorized_is_trivial(y1, y2, y3) |
| 132 | assert not vectorized_is_trivial(y1, z2, z3) |
| 133 | assert not vectorized_is_trivial(y1, 1, 1) |