2020-07-20 17:35:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-12 11:50:00 +00:00
|
|
|
import pytest
|
2021-08-13 16:37:05 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
from pybind11_tests import numpy_vectorize as m
|
2016-08-12 11:50:00 +00:00
|
|
|
|
2020-08-16 20:02:12 +00:00
|
|
|
np = pytest.importorskip("numpy")
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_vectorize(capture):
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j])
|
2016-08-12 11:50:00 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
for f in [m.vectorized_func, m.vectorized_func2]:
|
2016-08-12 11:50:00 +00:00
|
|
|
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])
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2016-08-12 11:50:00 +00:00
|
|
|
my_func(x:int=1, y:float=2, z:float=3)
|
|
|
|
my_func(x:int=3, y:float=4, z:float=3)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-03-19 00:11:59 +00:00
|
|
|
with capture:
|
2020-10-16 20:38:13 +00:00
|
|
|
a = np.array([[1, 2], [3, 4]], order="F")
|
|
|
|
b = np.array([[10, 20], [30, 40]], order="F")
|
2017-03-19 00:11:59 +00:00
|
|
|
c = 3
|
|
|
|
result = f(a, b, c)
|
|
|
|
assert np.allclose(result, a * b * c)
|
|
|
|
assert result.flags.f_contiguous
|
|
|
|
# All inputs are F order and full or singletons, so we the result is in col-major order:
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2017-03-19 00:11:59 +00:00
|
|
|
my_func(x:int=1, y:float=10, z:float=3)
|
|
|
|
my_func(x:int=3, y:float=30, z:float=3)
|
|
|
|
my_func(x:int=2, y:float=20, z:float=3)
|
|
|
|
my_func(x:int=4, y:float=40, z:float=3)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
with capture:
|
2020-10-16 20:38:13 +00:00
|
|
|
a, b, c = (
|
|
|
|
np.array([[1, 3, 5], [7, 9, 11]]),
|
|
|
|
np.array([[2, 4, 6], [8, 10, 12]]),
|
|
|
|
3,
|
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
assert np.allclose(f(a, b, c), a * b * c)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2016-08-12 11:50:00 +00:00
|
|
|
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)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
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)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2016-08-12 11:50:00 +00:00
|
|
|
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)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
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)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2016-08-12 11:50:00 +00:00
|
|
|
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)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
with capture:
|
2020-10-16 20:38:13 +00:00
|
|
|
a, b, c = (
|
|
|
|
np.array([[1, 2, 3], [4, 5, 6]], order="F"),
|
|
|
|
np.array([[2], [3]]),
|
|
|
|
2,
|
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
assert np.allclose(f(a, b, c), a * b * c)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
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)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
with capture:
|
|
|
|
a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
|
|
|
|
assert np.allclose(f(a, b, c), a * b * c)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
my_func(x:int=1, 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=6, y:float=3, z:float=2)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
with capture:
|
2020-10-16 20:38:13 +00:00
|
|
|
a, b, c = (
|
|
|
|
np.array([[1, 2, 3], [4, 5, 6]], order="F")[::, ::2],
|
|
|
|
np.array([[2], [3]]),
|
|
|
|
2,
|
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
assert np.allclose(f(a, b, c), a * b * c)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
my_func(x:int=1, 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=6, y:float=3, z:float=2)
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_type_selection():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
|
|
|
|
assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
m.selective_func(np.array([1.0j], dtype=np.complex64))
|
|
|
|
== "Complex float branch taken."
|
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_docs(doc):
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.vectorized_func)
|
|
|
|
== """
|
2020-05-14 12:34:48 +00:00
|
|
|
vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object
|
2016-12-12 23:59:28 +00:00
|
|
|
""" # noqa: E501 line too long
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_trivial_broadcasting():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
|
2017-03-19 00:11:59 +00:00
|
|
|
assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3)
|
|
|
|
== trivial.c_trivial
|
|
|
|
)
|
2017-03-19 00:11:59 +00:00
|
|
|
assert trivial.c_trivial == vectorized_is_trivial(
|
2020-10-16 20:38:13 +00:00
|
|
|
np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2)
|
|
|
|
== trivial.non_trivial
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2)
|
|
|
|
== trivial.non_trivial
|
|
|
|
)
|
|
|
|
z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype="int32")
|
|
|
|
z2 = np.array(z1, dtype="float32")
|
|
|
|
z3 = np.array(z1, dtype="float64")
|
2017-03-19 00:11:59 +00:00
|
|
|
assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
|
|
|
|
assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
|
|
|
|
assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
|
|
|
|
assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
y1 = np.array(z1, order="F")
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
y2 = np.array(y1)
|
|
|
|
y3 = np.array(y1)
|
2017-03-19 00:11:59 +00:00
|
|
|
assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
|
|
|
|
assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
|
|
|
|
assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
|
|
|
|
assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
|
|
|
|
assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
|
|
|
|
assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
|
|
|
|
assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
|
|
|
|
assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
|
|
|
|
assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
|
|
|
|
assert m.vectorized_func(1, y2, 1).flags.f_contiguous
|
|
|
|
assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
|
|
|
|
assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
|
2017-03-26 03:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_passthrough_arguments(doc):
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert doc(m.vec_passthrough) == (
|
2020-10-16 20:38:13 +00:00
|
|
|
"vec_passthrough("
|
|
|
|
+ ", ".join(
|
|
|
|
[
|
|
|
|
"arg0: float",
|
|
|
|
"arg1: numpy.ndarray[numpy.float64]",
|
|
|
|
"arg2: numpy.ndarray[numpy.float64]",
|
|
|
|
"arg3: numpy.ndarray[numpy.int32]",
|
|
|
|
"arg4: int",
|
|
|
|
"arg5: m.numpy_vectorize.NonPODClass",
|
|
|
|
"arg6: numpy.ndarray[numpy.float64]",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ ") -> object"
|
|
|
|
)
|
|
|
|
|
|
|
|
b = np.array([[10, 20, 30]], dtype="float64")
|
2017-03-26 03:51:40 +00:00
|
|
|
c = np.array([100, 200]) # NOT a vectorized argument
|
2020-10-16 20:38:13 +00:00
|
|
|
d = np.array([[1000], [2000], [3000]], dtype="int")
|
|
|
|
g = np.array([[1000000, 2000000, 3000000]], dtype="int") # requires casting
|
2017-03-26 03:51:40 +00:00
|
|
|
assert np.all(
|
2020-10-16 20:38:13 +00:00
|
|
|
m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g)
|
|
|
|
== np.array(
|
|
|
|
[
|
|
|
|
[1111111, 2111121, 3111131],
|
|
|
|
[1112111, 2112121, 3112131],
|
|
|
|
[1113111, 2113121, 3113131],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2017-03-26 03:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_method_vectorization():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
o = m.VectorizeTestClass(3)
|
2020-10-16 20:38:13 +00:00
|
|
|
x = np.array([1, 2], dtype="int")
|
|
|
|
y = np.array([[10], [20]], dtype="float32")
|
2017-03-26 03:51:40 +00:00
|
|
|
assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
|
|
|
|
|
|
|
|
|
|
|
|
def test_array_collapse():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
|
|
|
|
assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
|
|
|
|
z = m.vectorized_func([1], 2, 3)
|
2017-03-26 03:51:40 +00:00
|
|
|
assert isinstance(z, np.ndarray)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert z.shape == (1,)
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
z = m.vectorized_func(1, [[[2]]], 3)
|
2017-03-26 03:51:40 +00:00
|
|
|
assert isinstance(z, np.ndarray)
|
|
|
|
assert z.shape == (1, 1, 1)
|
2020-10-02 19:30:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_vectorized_noreturn():
|
|
|
|
x = m.NonPODClass(0)
|
|
|
|
assert x.value == 0
|
|
|
|
m.add_to(x, [1, 2, 3, 4])
|
|
|
|
assert x.value == 10
|
|
|
|
m.add_to(x, 1)
|
|
|
|
assert x.value == 11
|
|
|
|
m.add_to(x, [[1, 1], [2, 3]])
|
|
|
|
assert x.value == 18
|