Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 3 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 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 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 11 | |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 12 | enum MyEnum { EFirstEntry = 1, ESecondEntry }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 13 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 14 | std::string test_function1() { |
| 15 | return "test_function()"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 16 | } |
| 17 | |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 18 | std::string test_function2(MyEnum k) { |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 19 | return "test_function(enum=" + std::to_string(k) + ")"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 20 | } |
| 21 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 22 | std::string test_function3(int i) { |
| 23 | return "test_function(" + std::to_string(i) + ")"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 26 | py::str test_function4(int, float) { return "test_function(int, float)"; } |
| 27 | py::str test_function4(float, int) { return "test_function(float, int)"; } |
| 28 | |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 29 | py::bytes return_bytes() { |
| 30 | const char *data = "\x01\x00\x02\x00"; |
Wenzel Jakob | 15f6a00 | 2016-01-24 14:05:12 +0100 | [diff] [blame] | 31 | return std::string(data, 4); |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 34 | std::string print_bytes(py::bytes bytes) { |
| 35 | std::string ret = "bytes["; |
| 36 | const auto value = static_cast<std::string>(bytes); |
| 37 | for (size_t i = 0; i < value.length(); ++i) { |
| 38 | ret += std::to_string(static_cast<int>(value[i])) + " "; |
| 39 | } |
| 40 | ret.back() = ']'; |
| 41 | return ret; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Jason Rhinelander | 6e036e7 | 2016-12-13 20:06:41 -0500 | [diff] [blame] | 44 | // Test that we properly handle C++17 exception specifiers (which are part of the function signature |
| 45 | // in C++17). These should all still work before C++17, but don't affect the function signature. |
| 46 | namespace test_exc_sp { |
| 47 | int f1(int x) noexcept { return x+1; } |
| 48 | int f2(int x) noexcept(true) { return x+2; } |
| 49 | int f3(int x) noexcept(false) { return x+3; } |
| 50 | int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true) |
| 51 | struct C { |
| 52 | int m1(int x) noexcept { return x-1; } |
| 53 | int m2(int x) const noexcept { return x-2; } |
| 54 | int m3(int x) noexcept(true) { return x-3; } |
| 55 | int m4(int x) const noexcept(true) { return x-4; } |
| 56 | int m5(int x) noexcept(false) { return x-5; } |
| 57 | int m6(int x) const noexcept(false) { return x-6; } |
| 58 | int m7(int x) throw() { return x-7; } |
| 59 | int m8(int x) const throw() { return x-8; } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 64 | test_initializer constants_and_functions([](py::module &m) { |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 65 | m.attr("some_constant") = py::int_(14); |
| 66 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 67 | m.def("test_function", &test_function1); |
| 68 | m.def("test_function", &test_function2); |
| 69 | m.def("test_function", &test_function3); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 70 | |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 71 | #if defined(PYBIND11_OVERLOAD_CAST) |
| 72 | m.def("test_function", py::overload_cast<int, float>(&test_function4)); |
| 73 | m.def("test_function", py::overload_cast<float, int>(&test_function4)); |
| 74 | #else |
| 75 | m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4)); |
| 76 | m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4)); |
| 77 | #endif |
| 78 | |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 79 | py::enum_<MyEnum>(m, "MyEnum") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 80 | .value("EFirstEntry", EFirstEntry) |
| 81 | .value("ESecondEntry", ESecondEntry) |
| 82 | .export_values(); |
| 83 | |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 84 | m.def("return_bytes", &return_bytes); |
| 85 | m.def("print_bytes", &print_bytes); |
Jason Rhinelander | 6e036e7 | 2016-12-13 20:06:41 -0500 | [diff] [blame] | 86 | |
| 87 | using namespace test_exc_sp; |
| 88 | py::module m2 = m.def_submodule("exc_sp"); |
| 89 | py::class_<C>(m2, "C") |
| 90 | .def(py::init<>()) |
| 91 | .def("m1", &C::m1) |
| 92 | .def("m2", &C::m2) |
| 93 | .def("m3", &C::m3) |
| 94 | .def("m4", &C::m4) |
| 95 | .def("m5", &C::m5) |
| 96 | .def("m6", &C::m6) |
| 97 | .def("m7", &C::m7) |
| 98 | .def("m8", &C::m8) |
| 99 | ; |
| 100 | m2.def("f1", f1); |
| 101 | m2.def("f2", f2); |
| 102 | m2.def("f3", f3); |
| 103 | m2.def("f4", f4); |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 104 | }); |