test_constants_and_functions.cpp revision 12391
112027Sjungma@eit.uni-kl.de/*
212027Sjungma@eit.uni-kl.de    tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
312027Sjungma@eit.uni-kl.de
412027Sjungma@eit.uni-kl.de    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
512027Sjungma@eit.uni-kl.de
612027Sjungma@eit.uni-kl.de    All rights reserved. Use of this source code is governed by a
712027Sjungma@eit.uni-kl.de    BSD-style license that can be found in the LICENSE file.
812027Sjungma@eit.uni-kl.de*/
912027Sjungma@eit.uni-kl.de
1012027Sjungma@eit.uni-kl.de#include "pybind11_tests.h"
1112027Sjungma@eit.uni-kl.de
1212027Sjungma@eit.uni-kl.deenum MyEnum { EFirstEntry = 1, ESecondEntry };
1312027Sjungma@eit.uni-kl.de
1412027Sjungma@eit.uni-kl.destd::string test_function1() {
1512027Sjungma@eit.uni-kl.de    return "test_function()";
1612027Sjungma@eit.uni-kl.de}
1712027Sjungma@eit.uni-kl.de
1812027Sjungma@eit.uni-kl.destd::string test_function2(MyEnum k) {
1912027Sjungma@eit.uni-kl.de    return "test_function(enum=" + std::to_string(k) + ")";
2012027Sjungma@eit.uni-kl.de}
2112027Sjungma@eit.uni-kl.de
2212027Sjungma@eit.uni-kl.destd::string test_function3(int i) {
2312027Sjungma@eit.uni-kl.de    return "test_function(" + std::to_string(i) + ")";
2412027Sjungma@eit.uni-kl.de}
2512027Sjungma@eit.uni-kl.de
2612563Sgabeblack@google.compy::str test_function4()           { return "test_function()"; }
2712563Sgabeblack@google.compy::str test_function4(char *)     { return "test_function(char *)"; }
2812027Sjungma@eit.uni-kl.depy::str test_function4(int, float) { return "test_function(int, float)"; }
2912046Sgabeblack@google.compy::str test_function4(float, int) { return "test_function(float, int)"; }
3012027Sjungma@eit.uni-kl.de
3112027Sjungma@eit.uni-kl.depy::bytes return_bytes() {
3212046Sgabeblack@google.com    const char *data = "\x01\x00\x02\x00";
3312027Sjungma@eit.uni-kl.de    return std::string(data, 4);
3412046Sgabeblack@google.com}
3512046Sgabeblack@google.com
3612027Sjungma@eit.uni-kl.destd::string print_bytes(py::bytes bytes) {
3712046Sgabeblack@google.com    std::string ret = "bytes[";
3812046Sgabeblack@google.com    const auto value = static_cast<std::string>(bytes);
3912027Sjungma@eit.uni-kl.de    for (size_t i = 0; i < value.length(); ++i) {
4012046Sgabeblack@google.com        ret += std::to_string(static_cast<int>(value[i])) + " ";
4112046Sgabeblack@google.com    }
4212027Sjungma@eit.uni-kl.de    ret.back() = ']';
4312046Sgabeblack@google.com    return ret;
4412046Sgabeblack@google.com}
4512046Sgabeblack@google.com
4612027Sjungma@eit.uni-kl.de// Test that we properly handle C++17 exception specifiers (which are part of the function signature
4712046Sgabeblack@google.com// in C++17).  These should all still work before C++17, but don't affect the function signature.
4812046Sgabeblack@google.comnamespace test_exc_sp {
4912046Sgabeblack@google.comint f1(int x) noexcept { return x+1; }
5012046Sgabeblack@google.comint f2(int x) noexcept(true) { return x+2; }
5112046Sgabeblack@google.comint f3(int x) noexcept(false) { return x+3; }
5212046Sgabeblack@google.comint f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
5312046Sgabeblack@google.comstruct C {
5412046Sgabeblack@google.com    int m1(int x) noexcept { return x-1; }
5512046Sgabeblack@google.com    int m2(int x) const noexcept { return x-2; }
5612046Sgabeblack@google.com    int m3(int x) noexcept(true) { return x-3; }
5712046Sgabeblack@google.com    int m4(int x) const noexcept(true) { return x-4; }
5812046Sgabeblack@google.com    int m5(int x) noexcept(false) { return x-5; }
5912046Sgabeblack@google.com    int m6(int x) const noexcept(false) { return x-6; }
6012046Sgabeblack@google.com    int m7(int x) throw() { return x-7; }
6112046Sgabeblack@google.com    int m8(int x) const throw() { return x-8; }
6212563Sgabeblack@google.com};
6312563Sgabeblack@google.com}
6412046Sgabeblack@google.com
6512027Sjungma@eit.uni-kl.de
6612027Sjungma@eit.uni-kl.deTEST_SUBMODULE(constants_and_functions, m) {
6712046Sgabeblack@google.com    // test_constants
6812046Sgabeblack@google.com    m.attr("some_constant") = py::int_(14);
6912027Sjungma@eit.uni-kl.de
7012046Sgabeblack@google.com    // test_function_overloading
7112046Sgabeblack@google.com    m.def("test_function", &test_function1);
7212046Sgabeblack@google.com    m.def("test_function", &test_function2);
7312046Sgabeblack@google.com    m.def("test_function", &test_function3);
7412027Sjungma@eit.uni-kl.de
7512046Sgabeblack@google.com#if defined(PYBIND11_OVERLOAD_CAST)
7612046Sgabeblack@google.com    m.def("test_function", py::overload_cast<>(&test_function4));
7712046Sgabeblack@google.com    m.def("test_function", py::overload_cast<char *>(&test_function4));
7812046Sgabeblack@google.com    m.def("test_function", py::overload_cast<int, float>(&test_function4));
7912046Sgabeblack@google.com    m.def("test_function", py::overload_cast<float, int>(&test_function4));
8012046Sgabeblack@google.com#else
8112046Sgabeblack@google.com    m.def("test_function", static_cast<py::str (*)()>(&test_function4));
8212027Sjungma@eit.uni-kl.de    m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
8312046Sgabeblack@google.com    m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
8412046Sgabeblack@google.com    m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
8512046Sgabeblack@google.com#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