test_callbacks.cpp 2.86 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/test_callbacks.cpp -- callbacks
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
11
#include "pybind11_tests.h"
#include "constructor_stats.h"
12
#include <pybind11/functional.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
13
14


15
16
py::object test_callback1(py::object func) {
    return func();
Wenzel Jakob's avatar
Wenzel Jakob committed
17
18
}

19
20
py::tuple test_callback2(py::object func) {
    return func("Hello", 'x', true, 5);
Wenzel Jakob's avatar
Wenzel Jakob committed
21
22
}

23
24
std::string test_callback3(const std::function<int(int)> &func) {
    return "func(43) = " + std::to_string(func(43));
25
26
}

27
std::function<int(int)> test_callback4() {
28
29
30
    return [](int i) { return i+1; };
}

31
32
33
34
35
py::cpp_function test_callback5() {
    return py::cpp_function([](int i) { return i+1; },
       py::arg("number"));
}

36
37
int dummy_function(int i) { return i + 1; }
int dummy_function2(int i, int j) { return i + j; }
38
39
40
41
std::function<int(int)> roundtrip(std::function<int(int)> f, bool expect_none = false) {
    if (expect_none && f) {
        throw std::runtime_error("Expected None to be converted to empty std::function");
    }
42
43
44
    return f;
}

45
std::string test_dummy_function(const std::function<int(int)> &f) {
46
47
48
49
    using fn_type = int (*)(int);
    auto result = f.target<fn_type>();
    if (!result) {
        auto r = f(1);
50
        return "can't convert to function pointer: eval(1) = " + std::to_string(r);
51
52
    } else if (*result == dummy_function) {
        auto r = (*result)(1);
53
        return "matches dummy_function: eval(1) = " + std::to_string(r);
54
    } else {
55
        return "argument does NOT match dummy_function. This should never happen!";
56
57
58
    }
}

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
struct Payload {
    Payload() {
        print_default_created(this);
    }
    ~Payload() {
        print_destroyed(this);
    }
    Payload(const Payload &) {
        print_copy_created(this);
    }
    Payload(Payload &&) {
        print_move_created(this);
    }
};

74
test_initializer callbacks([](py::module &m) {
Wenzel Jakob's avatar
Wenzel Jakob committed
75
76
77
    m.def("test_callback1", &test_callback1);
    m.def("test_callback2", &test_callback2);
    m.def("test_callback3", &test_callback3);
78
    m.def("test_callback4", &test_callback4);
79
    m.def("test_callback5", &test_callback5);
80
81
82
83
84
85
86
87
88
89

    /* Test cleanup of lambda closure */

    m.def("test_cleanup", []() -> std::function<void(void)> { 
        Payload p;

        return [p]() {
            /* p should be cleaned up when the returned function is garbage collected */
        };
    });
90
91
92
93

    /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
    m.def("dummy_function", &dummy_function);
    m.def("dummy_function2", &dummy_function2);
94
    m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false);
95
    m.def("test_dummy_function", &test_dummy_function);
96
97
    // Export the payload constructor statistics for testing purposes:
    m.def("payload_cstats", &ConstructorStats::get<Payload>);
98
});