mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
391c75447d
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.
114 lines
3.7 KiB
C++
114 lines
3.7 KiB
C++
/*
|
|
tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
enum MyEnum { EFirstEntry = 1, ESecondEntry };
|
|
|
|
std::string test_function1() {
|
|
return "test_function()";
|
|
}
|
|
|
|
std::string test_function2(MyEnum k) {
|
|
return "test_function(enum=" + std::to_string(k) + ")";
|
|
}
|
|
|
|
std::string test_function3(int i) {
|
|
return "test_function(" + std::to_string(i) + ")";
|
|
}
|
|
|
|
py::str test_function4() { return "test_function()"; }
|
|
py::str test_function4(char *) { return "test_function(char *)"; }
|
|
py::str test_function4(int, float) { return "test_function(int, float)"; }
|
|
py::str test_function4(float, int) { return "test_function(float, int)"; }
|
|
|
|
py::bytes return_bytes() {
|
|
const char *data = "\x01\x00\x02\x00";
|
|
return std::string(data, 4);
|
|
}
|
|
|
|
std::string print_bytes(py::bytes bytes) {
|
|
std::string ret = "bytes[";
|
|
const auto value = static_cast<std::string>(bytes);
|
|
for (size_t i = 0; i < value.length(); ++i) {
|
|
ret += std::to_string(static_cast<int>(value[i])) + " ";
|
|
}
|
|
ret.back() = ']';
|
|
return ret;
|
|
}
|
|
|
|
// Test that we properly handle C++17 exception specifiers (which are part of the function signature
|
|
// in C++17). These should all still work before C++17, but don't affect the function signature.
|
|
namespace test_exc_sp {
|
|
int f1(int x) noexcept { return x+1; }
|
|
int f2(int x) noexcept(true) { return x+2; }
|
|
int f3(int x) noexcept(false) { return x+3; }
|
|
int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
|
|
struct C {
|
|
int m1(int x) noexcept { return x-1; }
|
|
int m2(int x) const noexcept { return x-2; }
|
|
int m3(int x) noexcept(true) { return x-3; }
|
|
int m4(int x) const noexcept(true) { return x-4; }
|
|
int m5(int x) noexcept(false) { return x-5; }
|
|
int m6(int x) const noexcept(false) { return x-6; }
|
|
int m7(int x) throw() { return x-7; }
|
|
int m8(int x) const throw() { return x-8; }
|
|
};
|
|
}
|
|
|
|
|
|
TEST_SUBMODULE(constants_and_functions, m) {
|
|
// test_constants
|
|
m.attr("some_constant") = py::int_(14);
|
|
|
|
// test_function_overloading
|
|
m.def("test_function", &test_function1);
|
|
m.def("test_function", &test_function2);
|
|
m.def("test_function", &test_function3);
|
|
|
|
#if defined(PYBIND11_OVERLOAD_CAST)
|
|
m.def("test_function", py::overload_cast<>(&test_function4));
|
|
m.def("test_function", py::overload_cast<char *>(&test_function4));
|
|
m.def("test_function", py::overload_cast<int, float>(&test_function4));
|
|
m.def("test_function", py::overload_cast<float, int>(&test_function4));
|
|
#else
|
|
m.def("test_function", static_cast<py::str (*)()>(&test_function4));
|
|
m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
|
|
m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
|
|
m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
|
|
#endif
|
|
|
|
py::enum_<MyEnum>(m, "MyEnum")
|
|
.value("EFirstEntry", EFirstEntry)
|
|
.value("ESecondEntry", ESecondEntry)
|
|
.export_values();
|
|
|
|
// test_bytes
|
|
m.def("return_bytes", &return_bytes);
|
|
m.def("print_bytes", &print_bytes);
|
|
|
|
// test_exception_specifiers
|
|
using namespace test_exc_sp;
|
|
py::class_<C>(m, "C")
|
|
.def(py::init<>())
|
|
.def("m1", &C::m1)
|
|
.def("m2", &C::m2)
|
|
.def("m3", &C::m3)
|
|
.def("m4", &C::m4)
|
|
.def("m5", &C::m5)
|
|
.def("m6", &C::m6)
|
|
.def("m7", &C::m7)
|
|
.def("m8", &C::m8)
|
|
;
|
|
m.def("f1", f1);
|
|
m.def("f2", f2);
|
|
m.def("f3", f3);
|
|
m.def("f4", f4);
|
|
}
|