function_objects.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <gtest/gtest.h>
13 #include "../../include/ecl/utilities/function_objects.hpp"
14 
19 /*****************************************************************************
20 ** Classes
21 *****************************************************************************/
22 
23 namespace ecl {
24 namespace utilities {
25 namespace tests {
26 
27 class FunctionObject {
28 public:
29  FunctionObject() : result(-1) {}
30  typedef void result_type;
31  void operator()() {
32  result = 0;
33 // std::cout << "Void function object." << std::endl;
34  }
35  int result;
36 };
37 
38 class MemberFunctions {
39 public:
40  void e() {
41  result = 0;
42 // std::cout << "Member function with no arguments." << std::endl;;
43  }
44  void f(const int &i) {
45  result = 1;
46 // std::cout << "Member function with argument: " << i << std::endl;;
47  }
48  void g() {
49  result = 2;
50 // std::cout << "Member function" << std::endl;;
51  }
52  void h(const int& i) {
53  result = 3;
54 // std::cout << "Member function with ref argument: " << i << std::endl;;
55  }
56  int result;
57 };
58 
59 /*****************************************************************************
60 ** Functions
61 *****************************************************************************/
62 
63 static int free_result = -1;
64 
65 void rnf1() {
66  free_result = 0;
67 // std::cout << "Free nullary function" << std::endl;;
68 }
69 
70 int rnf2() {
71  free_result = 1;
72 // std::cout << "Free nullary function returning: 3" << std::endl;;
73  return 3;
74 }
75 
76 void ruf1(const int &i) {
77  free_result = 2;
78 // std::cout << "Free unary function with argument: " << i << std::endl;;
79 }
80 
81 int ruf2(const int &i) {
82  free_result = 3;
83 // std::cout << "Free unary function with argument: " << i << std::endl;;
84  return i;
85 }
86 
87 }}}
88 
89 /*****************************************************************************
90 ** Using
91 *****************************************************************************/
92 
93 using namespace ecl;
94 using namespace ecl::utilities::tests;
95 
96 /*****************************************************************************
97 ** Tests
98 *****************************************************************************/
99 
100 TEST(FunctionObjects,freeFunctions) {
101  NullaryFreeFunction<void> rnfo1(rnf1);
102  rnfo1();
103  EXPECT_EQ(0,free_result);
104 
105  NullaryFreeFunction<int> rnfo2(rnf2);
107  EXPECT_EQ(3,i1);
108 
110  rufo1(2);
111  EXPECT_EQ(2,free_result);
112 
115  EXPECT_EQ(4,i2);
116 }
117 
118 TEST(FunctionObjects,boundFreeFunctions) {
119 
121  brufo1();
122  EXPECT_EQ(2,free_result);
123 }
124 
125 TEST(FunctionObjects,memberFunctions) {
126 
127  MemberFunctions a;
128  NullaryMemberFunction<MemberFunctions,void> mufo1(&MemberFunctions::e);
129  mufo1(a);
130  EXPECT_EQ(0,a.result);
131 
132  UnaryMemberFunction<MemberFunctions,const int&,void> mbfo1(&MemberFunctions::f);
133  mbfo1(a,2);
134  EXPECT_EQ(1,a.result);
135 }
136 
137 TEST(FunctionObjects,boundMemberFunctions) {
138 
139  MemberFunctions a;
140  BoundNullaryMemberFunction<MemberFunctions,void> bmufo1(&MemberFunctions::e, a);
141  bmufo1();
142  EXPECT_EQ(0,a.result);
143 
144  BoundUnaryMemberFunction<MemberFunctions,const int&,void> bmbfo1(&MemberFunctions::f, a, 3);
145  bmbfo1();
146  EXPECT_EQ(1,a.result);
147 }
148 
149 TEST(FunctionObjects,wrappers) {
150 
151  FunctionObject function_object;
152  NullaryFunctionCopy<FunctionObject> nfcopy(function_object);
153  nfcopy();
154  // Can't really validate this baby.
155 
156 // std::cout << "Nullary Function References." << std::endl;
157  NullaryFunctionReference<FunctionObject> nfref(ref(function_object));
158  nfref();
159  // Can't really validate this baby.
160 }
161 
162 TEST(FunctionObjects,generators) {
163 
164  MemberFunctions a;
165  generateFunctionObject(rnf1)();
166  EXPECT_EQ(0,free_result);
167  generateFunctionObject(ruf1)(3);
168  EXPECT_EQ(2,free_result);
169  generateFunctionObject(ruf1,3)();
170  EXPECT_EQ(2,free_result);
171  generateFunctionObject(&MemberFunctions::e)(a);
172  EXPECT_EQ(0,a.result);
173  generateFunctionObject(&MemberFunctions::e, a)();
174  EXPECT_EQ(0,a.result);
175  generateFunctionObject(&MemberFunctions::f)(a,1);
176  EXPECT_EQ(1,a.result);
177  generateFunctionObject(&MemberFunctions::h)(a,1);
178  EXPECT_EQ(3,a.result);
179  generateFunctionObject(&MemberFunctions::h,a,1)(); // Calls the non const version
180  EXPECT_EQ(3,a.result);
181  int j = 3;
182  generateFunctionObject(&MemberFunctions::h,a,j)(); // Calls the const version
183  EXPECT_EQ(3,a.result);
184 }
185 
186 /*****************************************************************************
187 ** Main program
188 *****************************************************************************/
189 
190 int main(int argc, char **argv) {
191  testing::InitGoogleTest(&argc,argv);
192  return RUN_ALL_TESTS();
193 }
Embedded control libraries.
Specialisation for free nullary functions that return void.
Nullary function object for bound unary global/static functions.
Create a NullaryFunction object composited from an existing function object.
Binary function object for unary member functions.
Unary function object for member functions without arguments.
Creates a nullary function from a reference wrapper.
Nullary function object for bound nullary member functions.
TEST(TypeTests, fundamentals)
Nullary function object for bound unary member functions.
Unary function object for global/static functions.
int main(int argc, char **argv)
Nullary function object for void global/static functions.
ReferenceWrapper< T > ref(T &wrapped_object)
Definition: references.hpp:145
NullaryFreeFunction< R > generateFunctionObject(R(*function)())
Generate a nullary function object from a void global/static function.


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:41