Pico GPS Teseo I2C
Loading...
Searching...
No Matches
callbackmanager_test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3import callbackmanager;
4
5// scenario: call object method
6TEST(callback, objectMethod) {
7 class MyClass {
8 public:
9 inline int handler(const int& num1, const int& num2) const {
10 return num1 + num2;
11 }
12 };
13
14 MyClass myClass;
15
17 // Use a lambda to capture myClass and call the object method
18 cb.set([&myClass](const int& num1, const int& num2) -> int {
19 return myClass.handler(num1, num2);
20 });
21 ASSERT_EQ(cb(4, 5), 9);
22 ASSERT_NE(cb(4, 5), 8);
23}
24
25// scenario: call static method
26TEST(callback, classStaticMethod) {
27 class MyClass {
28 public:
29 static inline int staticHandler(const int& num1, const int& num2) {
30 return num1 + num2;
31 }
32 };
33
35 // Use a lambda to call the static method
36 cb.set([](const int& num1, const int& num2) -> int {
37 return MyClass::staticHandler(num1, num2);
38 });
39 ASSERT_EQ(cb(4, 5), 9);
40 ASSERT_NE(cb(4, 5), 8);
41}
42
43// scenario: call C function
44int functionHandler(const int& num1, const int& num2) {
45 return num1 + num2;
46}
47
48TEST(callback, cFunction) {
50 // Use a lambda to call the classic C function
51 cb.set([](const int& num1, const int& num2) -> int {
52 return functionHandler(num1, num2);
53 });
54 ASSERT_EQ(cb(4, 5), 9);
55 ASSERT_NE(cb(4, 5), 8);
56}
57
58// scenario: call pure lambda
59TEST(callback, lambda) {
61 // Use a lambda to execute anonymous C code
62 cb.set([](const int& num1, const int& num2) -> int {
63 return num1 + num2;
64 });
65 ASSERT_EQ(cb(4, 5), 9);
66 ASSERT_NE(cb(4, 5), 8);
67}
68
69// scenario: return a bool
70TEST(callback, bool) {
72 // Use a lambda to execute anonymous C code
73 cb.set([](const int& num1, const int& num2) -> bool {
74 return num1 == num2;
75 });
76 ASSERT_TRUE(cb(1, 1));
77 ASSERT_FALSE(cb(1, 2));
78}
79
80// scenario: use void, and no attributes
81TEST(callback, voidWithoutParameters) {
83 // Use a lambda to execute anonymous C code
84 bool called = false;
85 cb.set([&called]() {
86 called = true;
87 return;
88 });
89 cb();
90 ASSERT_TRUE(called);
91}
92
93// scenario: use void, and a const std::string reference
94TEST(callback, voidWithConstParameter) {
96 // Use a lambda to execute anonymous C code
97 cb.set([](const std::string& s) {
98 ASSERT_STREQ(s.c_str(), "test");
99 return;
100 });
101 cb("test");
102}
103
104// scenario: no handler
105TEST(callback, noHandler) {
107 ASSERT_FALSE(cb.is_set());
108 cb();
109 ASSERT_TRUE(true); // call without callback should succeed
110}
111
112// scenario: set and unset
113TEST(callback, setUnset) {
115 ASSERT_FALSE(cb.is_set());
116 // Use a lambda to execute anonymous C code
117 cb.set([]() {
118 return;
119 });
120 ASSERT_TRUE(cb.is_set());
121 cb.unset();
122 ASSERT_FALSE(cb.is_set());
123}
124
125TEST(callback, object) {
126 class return_class {
127 public:
128 return_class() : value_(0) {};
129 int value_;
130 };
132 // Use a lambda to execute anonymous C code
133 // validate if it works when no function set:
134 return_class ret = cb();
135 ASSERT_EQ(ret.value_, 0);
136 cb.set([]() -> return_class {
137 return_class rc;
138 rc.value_ = 1;
139 return rc;
140 });
141 ret = cb();
142 ASSERT_EQ(ret.value_, 1);
143}
144
145TEST(callback, string) {
147 // Use a lambda to execute anonymous C code
148 cb.set([]() -> std::string {
149 return "test";
150 });
151 ASSERT_STREQ(cb().c_str(), "test");
152}
153
154TEST(callback, constructor) { // scenario: constructor with lambda
156 [](const int& num1, const int& num2) -> bool {
157 return num1 == num2;
158 });
159 ASSERT_FALSE(cb(1, 2));
160}
int functionHandler(const int &num1, const int &num2)
TEST(callback, objectMethod)
int handler(const int &num1, const int &num2) const
static int staticHandler(const int &num1, const int &num2)
void set(callbackfunction_t &&callback)