test_constants_and_functions.cpp 3.7 KB

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