xref: /aosp_15_r20/external/pigweed/pw_function/pointer_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_function/pointer.h"
16 
17 #include "pw_function/function.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace pw::function {
21 namespace {
22 
pw_function_test_InvokeFromCApi(void (* function)(void * context),void * context)23 extern "C" void pw_function_test_InvokeFromCApi(void (*function)(void* context),
24                                                 void* context) {
25   function(context);
26 }
27 
pw_function_test_Sum(int (* summer)(int a,int b,void * context),void * context)28 extern "C" int pw_function_test_Sum(int (*summer)(int a, int b, void* context),
29                                     void* context) {
30   return summer(60, 40, context);
31 }
32 
TEST(StaticInvoker,Function_NoArguments)33 TEST(StaticInvoker, Function_NoArguments) {
34   int value = 0;
35   Function<void()> set_value_to_42([&value] { value += 42; });
36 
37   pw_function_test_InvokeFromCApi(GetFunctionPointer(set_value_to_42),
38                                   &set_value_to_42);
39 
40   EXPECT_EQ(value, 42);
41 
42   pw_function_test_InvokeFromCApi(
43       GetFunctionPointer<decltype(set_value_to_42)>(), &set_value_to_42);
44 
45   EXPECT_EQ(value, 84);
46 }
47 
TEST(StaticInvoker,Function_WithArguments)48 TEST(StaticInvoker, Function_WithArguments) {
49   int sum = 0;
50   Function<int(int, int)> sum_stuff([&sum](int a, int b) {
51     sum += a + b;
52     return a + b;
53   });
54 
55   EXPECT_EQ(100,
56             pw_function_test_Sum(GetFunctionPointer(sum_stuff), &sum_stuff));
57 
58   EXPECT_EQ(sum, 100);
59 
60   EXPECT_EQ(100,
61             pw_function_test_Sum(GetFunctionPointer<decltype(sum_stuff)>(),
62                                  &sum_stuff));
63 
64   EXPECT_EQ(sum, 200);
65 }
66 
TEST(StaticInvoker,Callback_NoArguments)67 TEST(StaticInvoker, Callback_NoArguments) {
68   int value = 0;
69   Callback<void()> set_value_to_42([&value] { value += 42; });
70 
71   pw_function_test_InvokeFromCApi(GetFunctionPointer(set_value_to_42),
72                                   &set_value_to_42);
73 
74   EXPECT_EQ(value, 42);
75 }
76 
TEST(StaticInvoker,Callback_WithArguments)77 TEST(StaticInvoker, Callback_WithArguments) {
78   int sum = 0;
79   Callback<int(int, int)> sum_stuff([&sum](int a, int b) {
80     sum += a + b;
81     return a + b;
82   });
83 
84   EXPECT_EQ(100,
85             pw_function_test_Sum(GetFunctionPointer<decltype(sum_stuff)>(),
86                                  &sum_stuff));
87 
88   EXPECT_EQ(sum, 100);
89 }
90 
TEST(StaticInvoker,Lambda_NoArguments)91 TEST(StaticInvoker, Lambda_NoArguments) {
92   int value = 0;
93   auto set_value_to_42([&value] { value += 42; });
94 
95   pw_function_test_InvokeFromCApi(GetFunctionPointer(set_value_to_42),
96                                   &set_value_to_42);
97 
98   EXPECT_EQ(value, 42);
99 
100   pw_function_test_InvokeFromCApi(
101       GetFunctionPointer<decltype(set_value_to_42)>(), &set_value_to_42);
102 
103   EXPECT_EQ(value, 84);
104 }
105 
TEST(StaticInvoker,Lambda_WithArguments)106 TEST(StaticInvoker, Lambda_WithArguments) {
107   int sum = 0;
108   auto sum_stuff = [&sum](int a, int b) {
109     sum += a + b;
110     return a + b;
111   };
112 
113   EXPECT_EQ(100,
114             pw_function_test_Sum(GetFunctionPointer(sum_stuff), &sum_stuff));
115 
116   EXPECT_EQ(sum, 100);
117 
118   EXPECT_EQ(100,
119             pw_function_test_Sum(GetFunctionPointer<decltype(sum_stuff)>(),
120                                  &sum_stuff));
121 
122   EXPECT_EQ(sum, 200);
123 }
124 
125 }  // namespace
126 }  // namespace pw::function
127