test_constants_and_functions.cpp revision 12391
1/* 2 tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings 3 4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 6 All rights reserved. Use of this source code is governed by a 7 BSD-style license that can be found in the LICENSE file. 8*/ 9 10#include "pybind11_tests.h" 11 12enum MyEnum { EFirstEntry = 1, ESecondEntry }; 13 14std::string test_function1() { 15 return "test_function()"; 16} 17 18std::string test_function2(MyEnum k) { 19 return "test_function(enum=" + std::to_string(k) + ")"; 20} 21 22std::string test_function3(int i) { 23 return "test_function(" + std::to_string(i) + ")"; 24} 25 26py::str test_function4() { return "test_function()"; } 27py::str test_function4(char *) { return "test_function(char *)"; } 28py::str test_function4(int, float) { return "test_function(int, float)"; } 29py::str test_function4(float, int) { return "test_function(float, int)"; } 30 31py::bytes return_bytes() { 32 const char *data = "\x01\x00\x02\x00"; 33 return std::string(data, 4); 34} 35 36std::string print_bytes(py::bytes bytes) { 37 std::string ret = "bytes["; 38 const auto value = static_cast<std::string>(bytes); 39 for (size_t i = 0; i < value.length(); ++i) { 40 ret += std::to_string(static_cast<int>(value[i])) + " "; 41 } 42 ret.back() = ']'; 43 return ret; 44} 45 46// Test that we properly handle C++17 exception specifiers (which are part of the function signature 47// in C++17). These should all still work before C++17, but don't affect the function signature. 48namespace test_exc_sp { 49int f1(int x) noexcept { return x+1; } 50int f2(int x) noexcept(true) { return x+2; } 51int f3(int x) noexcept(false) { return x+3; } 52int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true) 53struct C { 54 int m1(int x) noexcept { return x-1; } 55 int m2(int x) const noexcept { return x-2; } 56 int m3(int x) noexcept(true) { return x-3; } 57 int m4(int x) const noexcept(true) { return x-4; } 58 int m5(int x) noexcept(false) { return x-5; } 59 int m6(int x) const noexcept(false) { return x-6; } 60 int m7(int x) throw() { return x-7; } 61 int m8(int x) const throw() { return x-8; } 62}; 63} 64 65 66TEST_SUBMODULE(constants_and_functions, m) { 67 // test_constants 68 m.attr("some_constant") = py::int_(14); 69 70 // test_function_overloading 71 m.def("test_function", &test_function1); 72 m.def("test_function", &test_function2); 73 m.def("test_function", &test_function3); 74 75#if defined(PYBIND11_OVERLOAD_CAST) 76 m.def("test_function", py::overload_cast<>(&test_function4)); 77 m.def("test_function", py::overload_cast<char *>(&test_function4)); 78 m.def("test_function", py::overload_cast<int, float>(&test_function4)); 79 m.def("test_function", py::overload_cast<float, int>(&test_function4)); 80#else 81 m.def("test_function", static_cast<py::str (*)()>(&test_function4)); 82 m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4)); 83 m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4)); 84 m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4)); 85#endif 86 87 py::enum_<MyEnum>(m, "MyEnum") 88 .value("EFirstEntry", EFirstEntry) 89 .value("ESecondEntry", ESecondEntry) 90 .export_values(); 91 92 // test_bytes 93 m.def("return_bytes", &return_bytes); 94 m.def("print_bytes", &print_bytes); 95 96 // test_exception_specifiers 97 using namespace test_exc_sp; 98 py::class_<C>(m, "C") 99 .def(py::init<>()) 100 .def("m1", &C::m1) 101 .def("m2", &C::m2) 102 .def("m3", &C::m3) 103 .def("m4", &C::m4) 104 .def("m5", &C::m5) 105 .def("m6", &C::m6) 106 .def("m7", &C::m7) 107 .def("m8", &C::m8) 108 ; 109 m.def("f1", f1); 110 m.def("f2", f2); 111 m.def("f3", f3); 112 m.def("f4", f4); 113} 114