test_constants_and_functions.cpp revision 14299
11689SN/A/*
29444SAndreas.Sandberg@ARM.com    tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
39444SAndreas.Sandberg@ARM.com
49444SAndreas.Sandberg@ARM.com    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
59444SAndreas.Sandberg@ARM.com
69444SAndreas.Sandberg@ARM.com    All rights reserved. Use of this source code is governed by a
79444SAndreas.Sandberg@ARM.com    BSD-style license that can be found in the LICENSE file.
89444SAndreas.Sandberg@ARM.com*/
99444SAndreas.Sandberg@ARM.com
109444SAndreas.Sandberg@ARM.com#include "pybind11_tests.h"
119444SAndreas.Sandberg@ARM.com
129444SAndreas.Sandberg@ARM.comenum MyEnum { EFirstEntry = 1, ESecondEntry };
139444SAndreas.Sandberg@ARM.com
142329SN/Astd::string test_function1() {
151689SN/A    return "test_function()";
161689SN/A}
171689SN/A
181689SN/Astd::string test_function2(MyEnum k) {
191689SN/A    return "test_function(enum=" + std::to_string(k) + ")";
201689SN/A}
211689SN/A
221689SN/Astd::string test_function3(int i) {
231689SN/A    return "test_function(" + std::to_string(i) + ")";
241689SN/A}
251689SN/A
261689SN/Apy::str test_function4()           { return "test_function()"; }
271689SN/Apy::str test_function4(char *)     { return "test_function(char *)"; }
281689SN/Apy::str test_function4(int, float) { return "test_function(int, float)"; }
291689SN/Apy::str test_function4(float, int) { return "test_function(float, int)"; }
301689SN/A
311689SN/Apy::bytes return_bytes() {
321689SN/A    const char *data = "\x01\x00\x02\x00";
331689SN/A    return std::string(data, 4);
341689SN/A}
351689SN/A
361689SN/Astd::string print_bytes(py::bytes bytes) {
371689SN/A    std::string ret = "bytes[";
381689SN/A    const auto value = static_cast<std::string>(bytes);
392665Ssaidi@eecs.umich.edu    for (size_t i = 0; i < value.length(); ++i) {
402665Ssaidi@eecs.umich.edu        ret += std::to_string(static_cast<int>(value[i])) + " ";
412831Sksewell@umich.edu    }
421689SN/A    ret.back() = ']';
431689SN/A    return ret;
449944Smatt.horsnell@ARM.com}
459944Smatt.horsnell@ARM.com
469944Smatt.horsnell@ARM.com// Test that we properly handle C++17 exception specifiers (which are part of the function signature
476221Snate@binkert.org// in C++17).  These should all still work before C++17, but don't affect the function signature.
486221Snate@binkert.orgnamespace test_exc_sp {
4913449Sgabeblack@google.comint f1(int x) noexcept { return x+1; }
501717SN/Aint f2(int x) noexcept(true) { return x+2; }
518232Snate@binkert.orgint f3(int x) noexcept(false) { return x+3; }
528232Snate@binkert.org#if defined(__GNUG__)
539954SFaissal.Sleiman@arm.com#  pragma GCC diagnostic push
541060SN/A#  pragma GCC diagnostic ignored "-Wdeprecated"
556221Snate@binkert.org#endif
562292SN/Aint f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
571061SN/A#if defined(__GNUG__)
589954SFaissal.Sleiman@arm.com#  pragma GCC diagnostic pop
594329Sktlim@umich.edu#endif
609954SFaissal.Sleiman@arm.comstruct C {
619954SFaissal.Sleiman@arm.com    int m1(int x) noexcept { return x-1; }
621060SN/A    int m2(int x) const noexcept { return x-2; }
639954SFaissal.Sleiman@arm.com    int m3(int x) noexcept(true) { return x-3; }
641060SN/A    int m4(int x) const noexcept(true) { return x-4; }
659954SFaissal.Sleiman@arm.com    int m5(int x) noexcept(false) { return x-5; }
662292SN/A    int m6(int x) const noexcept(false) { return x-6; }
672292SN/A#if defined(__GNUG__)
682292SN/A#  pragma GCC diagnostic push
692292SN/A#  pragma GCC diagnostic ignored "-Wdeprecated"
702292SN/A#endif
712292SN/A    int m7(int x) throw() { return x-7; }
722292SN/A    int m8(int x) const throw() { return x-8; }
732292SN/A#if defined(__GNUG__)
742292SN/A#  pragma GCC diagnostic pop
752292SN/A#endif
766221Snate@binkert.org};
776221Snate@binkert.org}
782292SN/A
792292SN/A
802292SN/ATEST_SUBMODULE(constants_and_functions, m) {
812292SN/A    // test_constants
824329Sktlim@umich.edu    m.attr("some_constant") = py::int_(14);
832292SN/A
842292SN/A    // test_function_overloading
852292SN/A    m.def("test_function", &test_function1);
862292SN/A    m.def("test_function", &test_function2);
872292SN/A    m.def("test_function", &test_function3);
886221Snate@binkert.org
896221Snate@binkert.org#if defined(PYBIND11_OVERLOAD_CAST)
902292SN/A    m.def("test_function", py::overload_cast<>(&test_function4));
912292SN/A    m.def("test_function", py::overload_cast<char *>(&test_function4));
922292SN/A    m.def("test_function", py::overload_cast<int, float>(&test_function4));
932292SN/A    m.def("test_function", py::overload_cast<float, int>(&test_function4));
944329Sktlim@umich.edu#else
952292SN/A    m.def("test_function", static_cast<py::str (*)()>(&test_function4));
969954SFaissal.Sleiman@arm.com    m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
972292SN/A    m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
982292SN/A    m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
996221Snate@binkert.org#endif
1006221Snate@binkert.org
1012292SN/A    py::enum_<MyEnum>(m, "MyEnum")
1022292SN/A        .value("EFirstEntry", EFirstEntry)
10313449Sgabeblack@google.com        .value("ESecondEntry", ESecondEntry)
10413449Sgabeblack@google.com        .export_values();
1052292SN/A
1061060SN/A    // test_bytes
1079444SAndreas.Sandberg@ARM.com    m.def("return_bytes", &return_bytes);
1089444SAndreas.Sandberg@ARM.com    m.def("print_bytes", &print_bytes);
1099444SAndreas.Sandberg@ARM.com
1109444SAndreas.Sandberg@ARM.com    // test_exception_specifiers
1119444SAndreas.Sandberg@ARM.com    using namespace test_exc_sp;
1129444SAndreas.Sandberg@ARM.com    py::class_<C>(m, "C")
1139444SAndreas.Sandberg@ARM.com        .def(py::init<>())
1149444SAndreas.Sandberg@ARM.com        .def("m1", &C::m1)
1159444SAndreas.Sandberg@ARM.com        .def("m2", &C::m2)
1169444SAndreas.Sandberg@ARM.com        .def("m3", &C::m3)
1176221Snate@binkert.org        .def("m4", &C::m4)
1189444SAndreas.Sandberg@ARM.com        .def("m5", &C::m5)
1192292SN/A        .def("m6", &C::m6)
1209444SAndreas.Sandberg@ARM.com        .def("m7", &C::m7)
1211060SN/A        .def("m8", &C::m8)
1222292SN/A        ;
1232292SN/A    m.def("f1", f1);
1242292SN/A    m.def("f2", f2);
1252292SN/A    m.def("f3", f3);
1262292SN/A    m.def("f4", f4);
1272292SN/A}
1282292SN/A