112391Sjason@lowepower.com/*
212391Sjason@lowepower.com    tests/test_builtin_casters.cpp -- Casters available without any additional headers
312391Sjason@lowepower.com
412391Sjason@lowepower.com    Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
512391Sjason@lowepower.com
612391Sjason@lowepower.com    All rights reserved. Use of this source code is governed by a
712391Sjason@lowepower.com    BSD-style license that can be found in the LICENSE file.
812391Sjason@lowepower.com*/
912391Sjason@lowepower.com
1012391Sjason@lowepower.com#include "pybind11_tests.h"
1112391Sjason@lowepower.com#include <pybind11/complex.h>
1212391Sjason@lowepower.com
1312391Sjason@lowepower.com#if defined(_MSC_VER)
1412391Sjason@lowepower.com#  pragma warning(push)
1512391Sjason@lowepower.com#  pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
1612391Sjason@lowepower.com#endif
1712391Sjason@lowepower.com
1812391Sjason@lowepower.comTEST_SUBMODULE(builtin_casters, m) {
1912391Sjason@lowepower.com    // test_simple_string
2012391Sjason@lowepower.com    m.def("string_roundtrip", [](const char *s) { return s; });
2112391Sjason@lowepower.com
2212391Sjason@lowepower.com    // test_unicode_conversion
2312391Sjason@lowepower.com    // Some test characters in utf16 and utf32 encodings.  The last one (the ��) contains a null byte
2412391Sjason@lowepower.com    char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*��*/,              mathbfA32 = 0x1d400 /*��*/;
2512391Sjason@lowepower.com    char16_t b16 = 0x62 /*b*/, z16 = 0x7a,       ib16 = 0x203d,       cake16_1 = 0xd83c, cake16_2 = 0xdf82, mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
2612391Sjason@lowepower.com    std::wstring wstr;
2712391Sjason@lowepower.com    wstr.push_back(0x61); // a
2812391Sjason@lowepower.com    wstr.push_back(0x2e18); // ⸘
2912391Sjason@lowepower.com    if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // ��, utf16
3012391Sjason@lowepower.com    else { wstr.push_back((wchar_t) mathbfA32); } // ��, utf32
3112391Sjason@lowepower.com    wstr.push_back(0x7a); // z
3212391Sjason@lowepower.com
3312391Sjason@lowepower.com    m.def("good_utf8_string", []() { return std::string(u8"Say utf8\u203d \U0001f382 \U0001d400"); }); // Say utf8‽ �� ��
3412391Sjason@lowepower.com    m.def("good_utf16_string", [=]() { return std::u16string({ b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16 }); }); // b‽����z
3512391Sjason@lowepower.com    m.def("good_utf32_string", [=]() { return std::u32string({ a32, mathbfA32, cake32, ib32, z32 }); }); // a����‽z
3612391Sjason@lowepower.com    m.def("good_wchar_string", [=]() { return wstr; }); // a‽��z
3712391Sjason@lowepower.com    m.def("bad_utf8_string", []()  { return std::string("abc\xd0" "def"); });
3812391Sjason@lowepower.com    m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); });
3912391Sjason@lowepower.com    // Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError
4012391Sjason@lowepower.com    if (PY_MAJOR_VERSION >= 3)
4112391Sjason@lowepower.com        m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
4212391Sjason@lowepower.com    if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2)
4312391Sjason@lowepower.com        m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); });
4412391Sjason@lowepower.com    m.def("u8_Z", []() -> char { return 'Z'; });
4512391Sjason@lowepower.com    m.def("u8_eacute", []() -> char { return '\xe9'; });
4612391Sjason@lowepower.com    m.def("u16_ibang", [=]() -> char16_t { return ib16; });
4712391Sjason@lowepower.com    m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
4812391Sjason@lowepower.com    m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
4912391Sjason@lowepower.com
5012391Sjason@lowepower.com    // test_single_char_arguments
5112391Sjason@lowepower.com    m.attr("wchar_size") = py::cast(sizeof(wchar_t));
5212391Sjason@lowepower.com    m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
5314299Sbbruce@ucdavis.edu    m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
5412391Sjason@lowepower.com    m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
5514299Sbbruce@ucdavis.edu    m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
5612391Sjason@lowepower.com    m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
5712391Sjason@lowepower.com    m.def("ord_wchar", [](wchar_t c) -> int { return c; });
5812391Sjason@lowepower.com
5912391Sjason@lowepower.com    // test_bytes_to_string
6012391Sjason@lowepower.com    m.def("strlen", [](char *s) { return strlen(s); });
6112391Sjason@lowepower.com    m.def("string_length", [](std::string s) { return s.length(); });
6212391Sjason@lowepower.com
6312391Sjason@lowepower.com    // test_string_view
6412391Sjason@lowepower.com#ifdef PYBIND11_HAS_STRING_VIEW
6512391Sjason@lowepower.com    m.attr("has_string_view") = true;
6612391Sjason@lowepower.com    m.def("string_view_print",   [](std::string_view s)    { py::print(s, s.size()); });
6712391Sjason@lowepower.com    m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
6812391Sjason@lowepower.com    m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
6912391Sjason@lowepower.com    m.def("string_view_chars",   [](std::string_view s)    { py::list l; for (auto c : s) l.append((std::uint8_t) c); return l; });
7012391Sjason@lowepower.com    m.def("string_view16_chars", [](std::u16string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
7112391Sjason@lowepower.com    m.def("string_view32_chars", [](std::u32string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
7212391Sjason@lowepower.com    m.def("string_view_return",   []() { return std::string_view(u8"utf8 secret \U0001f382"); });
7312391Sjason@lowepower.com    m.def("string_view16_return", []() { return std::u16string_view(u"utf16 secret \U0001f382"); });
7412391Sjason@lowepower.com    m.def("string_view32_return", []() { return std::u32string_view(U"utf32 secret \U0001f382"); });
7512391Sjason@lowepower.com#endif
7612391Sjason@lowepower.com
7712391Sjason@lowepower.com    // test_integer_casting
7812391Sjason@lowepower.com    m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
7912391Sjason@lowepower.com    m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
8012391Sjason@lowepower.com    m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
8112391Sjason@lowepower.com    m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
8212391Sjason@lowepower.com
8312391Sjason@lowepower.com    // test_tuple
8412391Sjason@lowepower.com    m.def("pair_passthrough", [](std::pair<bool, std::string> input) {
8512391Sjason@lowepower.com        return std::make_pair(input.second, input.first);
8612391Sjason@lowepower.com    }, "Return a pair in reversed order");
8712391Sjason@lowepower.com    m.def("tuple_passthrough", [](std::tuple<bool, std::string, int> input) {
8812391Sjason@lowepower.com        return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
8912391Sjason@lowepower.com    }, "Return a triple in reversed order");
9012391Sjason@lowepower.com    m.def("empty_tuple", []() { return std::tuple<>(); });
9112391Sjason@lowepower.com    static std::pair<RValueCaster, RValueCaster> lvpair;
9212391Sjason@lowepower.com    static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
9312391Sjason@lowepower.com    static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>> lvnested;
9412391Sjason@lowepower.com    m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
9512391Sjason@lowepower.com    m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
9612391Sjason@lowepower.com    m.def("rvalue_tuple", []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
9712391Sjason@lowepower.com    m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
9812391Sjason@lowepower.com    m.def("rvalue_nested", []() {
9912391Sjason@lowepower.com        return std::make_pair(RValueCaster{}, std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{}))); });
10012391Sjason@lowepower.com    m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
10112391Sjason@lowepower.com
10212391Sjason@lowepower.com    // test_builtins_cast_return_none
10312391Sjason@lowepower.com    m.def("return_none_string", []() -> std::string * { return nullptr; });
10412391Sjason@lowepower.com    m.def("return_none_char",   []() -> const char *  { return nullptr; });
10512391Sjason@lowepower.com    m.def("return_none_bool",   []() -> bool *        { return nullptr; });
10612391Sjason@lowepower.com    m.def("return_none_int",    []() -> int *         { return nullptr; });
10712391Sjason@lowepower.com    m.def("return_none_float",  []() -> float *       { return nullptr; });
10812391Sjason@lowepower.com
10912391Sjason@lowepower.com    // test_none_deferred
11012391Sjason@lowepower.com    m.def("defer_none_cstring", [](char *) { return false; });
11112391Sjason@lowepower.com    m.def("defer_none_cstring", [](py::none) { return true; });
11212391Sjason@lowepower.com    m.def("defer_none_custom", [](UserType *) { return false; });
11312391Sjason@lowepower.com    m.def("defer_none_custom", [](py::none) { return true; });
11412391Sjason@lowepower.com    m.def("nodefer_none_void", [](void *) { return true; });
11512391Sjason@lowepower.com    m.def("nodefer_none_void", [](py::none) { return false; });
11612391Sjason@lowepower.com
11712391Sjason@lowepower.com    // test_void_caster
11812391Sjason@lowepower.com    m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
11912391Sjason@lowepower.com    m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
12012391Sjason@lowepower.com
12112391Sjason@lowepower.com    // test_bool_caster
12212391Sjason@lowepower.com    m.def("bool_passthrough", [](bool arg) { return arg; });
12312391Sjason@lowepower.com    m.def("bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg().noconvert());
12412391Sjason@lowepower.com
12512391Sjason@lowepower.com    // test_reference_wrapper
12612391Sjason@lowepower.com    m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
12712391Sjason@lowepower.com    m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
12812391Sjason@lowepower.com    // Not currently supported (std::pair caster has return-by-value cast operator);
12912391Sjason@lowepower.com    // triggers static_assert failure.
13012391Sjason@lowepower.com    //m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
13112391Sjason@lowepower.com
13212391Sjason@lowepower.com    m.def("refwrap_list", [](bool copy) {
13312391Sjason@lowepower.com        static IncType x1(1), x2(2);
13412391Sjason@lowepower.com        py::list l;
13512391Sjason@lowepower.com        for (auto &f : {std::ref(x1), std::ref(x2)}) {
13612391Sjason@lowepower.com            l.append(py::cast(f, copy ? py::return_value_policy::copy
13712391Sjason@lowepower.com                                      : py::return_value_policy::reference));
13812391Sjason@lowepower.com        }
13912391Sjason@lowepower.com        return l;
14012391Sjason@lowepower.com    }, "copy"_a);
14112391Sjason@lowepower.com
14212391Sjason@lowepower.com    m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
14312391Sjason@lowepower.com    m.def("refwrap_call_iiw", [](IncType &w, py::function f) {
14412391Sjason@lowepower.com        py::list l;
14512391Sjason@lowepower.com        l.append(f(std::ref(w)));
14612391Sjason@lowepower.com        l.append(f(std::cref(w)));
14712391Sjason@lowepower.com        IncType x(w.value());
14812391Sjason@lowepower.com        l.append(f(std::ref(x)));
14912391Sjason@lowepower.com        IncType y(w.value());
15012391Sjason@lowepower.com        auto r3 = std::ref(y);
15112391Sjason@lowepower.com        l.append(f(r3));
15212391Sjason@lowepower.com        return l;
15312391Sjason@lowepower.com    });
15412391Sjason@lowepower.com
15512391Sjason@lowepower.com    // test_complex
15612391Sjason@lowepower.com    m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
15712391Sjason@lowepower.com    m.def("complex_cast", [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
15814299Sbbruce@ucdavis.edu
15914299Sbbruce@ucdavis.edu    // test int vs. long (Python 2)
16014299Sbbruce@ucdavis.edu    m.def("int_cast", []() {return (int) 42;});
16114299Sbbruce@ucdavis.edu    m.def("long_cast", []() {return (long) 42;});
16214299Sbbruce@ucdavis.edu    m.def("longlong_cast", []() {return  ULLONG_MAX;});
16314299Sbbruce@ucdavis.edu
16414299Sbbruce@ucdavis.edu    /// test void* cast operator
16514299Sbbruce@ucdavis.edu    m.def("test_void_caster", []() -> bool {
16614299Sbbruce@ucdavis.edu        void *v = (void *) 0xabcd;
16714299Sbbruce@ucdavis.edu        py::object o = py::cast(v);
16814299Sbbruce@ucdavis.edu        return py::cast<void *>(o) == v;
16914299Sbbruce@ucdavis.edu    });
17012391Sjason@lowepower.com}
171