# -*- coding: utf-8 -*- import pytest from pybind11_tests import ConstructorStats np = pytest.importorskip("numpy") m = pytest.importorskip("pybind11_tests.eigen") ref = np.array( [ [0.0, 3, 0, 0, 0, 11], [22, 0, 0, 0, 17, 11], [7, 5, 0, 1, 0, 11], [0, 0, 0, 0, 0, 11], [0, 0, 14, 0, 8, 11], ] ) def assert_equal_ref(mat): np.testing.assert_array_equal(mat, ref) def assert_sparse_equal_ref(sparse_mat): assert_equal_ref(sparse_mat.toarray()) def test_fixed(): assert_equal_ref(m.fixed_c()) assert_equal_ref(m.fixed_r()) assert_equal_ref(m.fixed_copy_r(m.fixed_r())) assert_equal_ref(m.fixed_copy_c(m.fixed_c())) assert_equal_ref(m.fixed_copy_r(m.fixed_c())) assert_equal_ref(m.fixed_copy_c(m.fixed_r())) def test_dense(): assert_equal_ref(m.dense_r()) assert_equal_ref(m.dense_c()) assert_equal_ref(m.dense_copy_r(m.dense_r())) assert_equal_ref(m.dense_copy_c(m.dense_c())) assert_equal_ref(m.dense_copy_r(m.dense_c())) assert_equal_ref(m.dense_copy_c(m.dense_r())) def test_partially_fixed(): ref2 = np.array([[0.0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]]) np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :]) np.testing.assert_array_equal( m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] ) np.testing.assert_array_equal( m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] ) np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]]) np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :]) np.testing.assert_array_equal( m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] ) np.testing.assert_array_equal( m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] ) # TypeError should be raise for a shape mismatch functions = [ m.partial_copy_four_rm_r, m.partial_copy_four_rm_c, m.partial_copy_four_cm_r, m.partial_copy_four_cm_c, ] matrix_with_wrong_shape = [[1, 2], [3, 4]] for f in functions: with pytest.raises(TypeError) as excinfo: f(matrix_with_wrong_shape) assert "incompatible function arguments" in str(excinfo.value) def test_mutator_descriptors(): zr = np.arange(30, dtype="float32").reshape(5, 6) # row-major zc = zr.reshape(6, 5).transpose() # column-major m.fixed_mutator_r(zr) m.fixed_mutator_c(zc) m.fixed_mutator_a(zr) m.fixed_mutator_a(zc) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_r(zc) assert ( "(arg0: numpy.ndarray[numpy.float32[5, 6]," " flags.writeable, flags.c_contiguous]) -> None" in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_c(zr) assert ( "(arg0: numpy.ndarray[numpy.float32[5, 6]," " flags.writeable, flags.f_contiguous]) -> None" in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32")) assert "(arg0: numpy.ndarray[numpy.float32[5, 6], flags.writeable]) -> None" in str( excinfo.value ) zr.flags.writeable = False with pytest.raises(TypeError): m.fixed_mutator_r(zr) with pytest.raises(TypeError): m.fixed_mutator_a(zr) def test_cpp_casting(): assert m.cpp_copy(m.fixed_r()) == 22.0 assert m.cpp_copy(m.fixed_c()) == 22.0 z = np.array([[5.0, 6], [7, 8]]) assert m.cpp_copy(z) == 7.0 assert m.cpp_copy(m.get_cm_ref()) == 21.0 assert m.cpp_copy(m.get_rm_ref()) == 21.0 assert m.cpp_ref_c(m.get_cm_ref()) == 21.0 assert m.cpp_ref_r(m.get_rm_ref()) == 21.0 with pytest.raises(RuntimeError) as excinfo: # Can't reference m.fixed_c: it contains floats, m.cpp_ref_any wants doubles m.cpp_ref_any(m.fixed_c()) assert "Unable to cast Python instance" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: # Can't reference m.fixed_r: it contains floats, m.cpp_ref_any wants doubles m.cpp_ref_any(m.fixed_r()) assert "Unable to cast Python instance" in str(excinfo.value) assert m.cpp_ref_any(m.ReturnTester.create()) == 1.0 assert m.cpp_ref_a
# -*- coding: utf-8 -*-
from pybind11_tests import docstring_options as m
def test_docstring_options():
# options.disable_function_signatures()
assert not m.test_function1.__doc__
assert m.test_function2.__doc__ == "A custom docstring"
# docstring specified on just the first overload definition:
assert m.test_overloaded1.__doc__ == "Overload docstring"
# docstring on both overloads:
assert m.test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2"
# docstring on only second overload:
assert m.test_overloaded3.__doc__ == "Overload docstr"
# options.enable_function_signatures()
assert m.test_function3.__doc__.startswith("test_function3(a: int, b: int) -> None")
assert m.test_function4.__doc__.startswith("test_function4(a: int, b: int) -> None")
assert m.test_function4.__doc__.endswith("A custom docstring\n")
# options.disable_function_signatures()
# options.disable_user_defined_docstrings()
assert not m.test_function5.__doc__
# nested options.enable_user_defined_docstrings()
assert m.test_function6.__doc__ == "A custom docstring"
# RAII destructor
assert m.test_function7.__doc__.startswith("test_function7(a: int, b: int) -> None")
assert m.test_function7.__doc__.endswith("A custom docstring\n")
# Suppression of user-defined docstrings for non-function objects
assert not m.DocstringTestFoo.__doc__
assert not m.DocstringTestFoo.value_prop.__doc__