test_constants_and_functions.cpp 1.47 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
Wenzel Jakob's avatar
Wenzel Jakob committed
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
5
6
7
8
9

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

Dean Moldovan's avatar
Dean Moldovan committed
10
#include "pybind11_tests.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
11

12
enum MyEnum { EFirstEntry = 1, ESecondEntry };
Wenzel Jakob's avatar
Wenzel Jakob committed
13

14
15
std::string test_function1() {
    return "test_function()";
Wenzel Jakob's avatar
Wenzel Jakob committed
16
17
}

18
std::string test_function2(MyEnum k) {
19
    return "test_function(enum=" + std::to_string(k) + ")";
Wenzel Jakob's avatar
Wenzel Jakob committed
20
21
}

22
23
std::string test_function3(int i) {
    return "test_function(" + std::to_string(i) + ")";
Wenzel Jakob's avatar
Wenzel Jakob committed
24
25
}

26
27
py::bytes return_bytes() {
    const char *data = "\x01\x00\x02\x00";
28
    return std::string(data, 4);
29
30
}

31
32
33
34
35
36
37
38
std::string print_bytes(py::bytes bytes) {
    std::string ret = "bytes[";
    const auto value = static_cast<std::string>(bytes);
    for (size_t i = 0; i < value.length(); ++i) {
        ret += std::to_string(static_cast<int>(value[i])) + " ";
    }
    ret.back() = ']';
    return ret;
39
40
}

41
void init_ex_constants_and_functions(py::module &m) {
42
43
    m.attr("some_constant") = py::int_(14);

Wenzel Jakob's avatar
Wenzel Jakob committed
44
45
46
47
    m.def("test_function", &test_function1);
    m.def("test_function", &test_function2);
    m.def("test_function", &test_function3);

48
    py::enum_<MyEnum>(m, "MyEnum")
Wenzel Jakob's avatar
Wenzel Jakob committed
49
50
51
52
        .value("EFirstEntry", EFirstEntry)
        .value("ESecondEntry", ESecondEntry)
        .export_values();

53
54
    m.def("return_bytes", &return_bytes);
    m.def("print_bytes", &print_bytes);
Wenzel Jakob's avatar
Wenzel Jakob committed
55
}