$search
00001 00008 /***************************************************************************** 00009 ** Includes 00010 *****************************************************************************/ 00011 00012 #include <gtest/gtest.h> 00013 #include "../../include/ecl/utilities/function_objects.hpp" 00014 00019 /***************************************************************************** 00020 ** Classes 00021 *****************************************************************************/ 00022 00023 namespace ecl { 00024 namespace utilities { 00025 namespace tests { 00026 00027 class FunctionObject { 00028 public: 00029 FunctionObject() : result(-1) {} 00030 typedef void result_type; 00031 void operator()() { 00032 result = 0; 00033 // std::cout << "Void function object." << std::endl; 00034 } 00035 int result; 00036 }; 00037 00038 class MemberFunctions { 00039 public: 00040 void e() { 00041 result = 0; 00042 // std::cout << "Member function with no arguments." << std::endl;; 00043 } 00044 void f(const int &i) { 00045 result = 1; 00046 // std::cout << "Member function with argument: " << i << std::endl;; 00047 } 00048 void g() { 00049 result = 2; 00050 // std::cout << "Member function" << std::endl;; 00051 } 00052 void h(const int& i) { 00053 result = 3; 00054 // std::cout << "Member function with ref argument: " << i << std::endl;; 00055 } 00056 int result; 00057 }; 00058 00059 /***************************************************************************** 00060 ** Functions 00061 *****************************************************************************/ 00062 00063 static int free_result = -1; 00064 00065 void rnf1() { 00066 free_result = 0; 00067 // std::cout << "Free nullary function" << std::endl;; 00068 } 00069 00070 int rnf2() { 00071 free_result = 1; 00072 // std::cout << "Free nullary function returning: 3" << std::endl;; 00073 return 3; 00074 } 00075 00076 void ruf1(const int &i) { 00077 free_result = 2; 00078 // std::cout << "Free unary function with argument: " << i << std::endl;; 00079 } 00080 00081 int ruf2(const int &i) { 00082 free_result = 3; 00083 // std::cout << "Free unary function with argument: " << i << std::endl;; 00084 return i; 00085 } 00086 00087 }}} 00088 00089 /***************************************************************************** 00090 ** Using 00091 *****************************************************************************/ 00092 00093 using namespace ecl; 00094 using namespace ecl::utilities::tests; 00095 00096 /***************************************************************************** 00097 ** Tests 00098 *****************************************************************************/ 00099 00100 TEST(FunctionObjects,freeFunctions) { 00101 NullaryFreeFunction<void> rnfo1(rnf1); 00102 rnfo1(); 00103 EXPECT_EQ(0,free_result); 00104 00105 NullaryFreeFunction<int> rnfo2(rnf2); 00106 NullaryFreeFunction<int>::result_type i1 = rnfo2(); 00107 EXPECT_EQ(3,i1); 00108 00109 UnaryFreeFunction<const int&,void> rufo1(ruf1); 00110 rufo1(2); 00111 EXPECT_EQ(2,free_result); 00112 00113 UnaryFreeFunction<const int&,int> rufo2(ruf2); 00114 UnaryFreeFunction<int,int>::result_type i2 = rufo2(4); 00115 EXPECT_EQ(4,i2); 00116 } 00117 00118 TEST(FunctionObjects,boundFreeFunctions) { 00119 00120 BoundUnaryFreeFunction<const int&,void> brufo1(ruf1,5); 00121 brufo1(); 00122 EXPECT_EQ(2,free_result); 00123 } 00124 00125 TEST(FunctionObjects,memberFunctions) { 00126 00127 MemberFunctions a; 00128 NullaryMemberFunction<MemberFunctions,void> mufo1(&MemberFunctions::e); 00129 mufo1(a); 00130 EXPECT_EQ(0,a.result); 00131 00132 UnaryMemberFunction<MemberFunctions,const int&,void> mbfo1(&MemberFunctions::f); 00133 mbfo1(a,2); 00134 EXPECT_EQ(1,a.result); 00135 } 00136 00137 TEST(FunctionObjects,boundMemberFunctions) { 00138 00139 MemberFunctions a; 00140 BoundNullaryMemberFunction<MemberFunctions,void> bmufo1(&MemberFunctions::e, a); 00141 bmufo1(); 00142 EXPECT_EQ(0,a.result); 00143 00144 BoundUnaryMemberFunction<MemberFunctions,const int&,void> bmbfo1(&MemberFunctions::f, a, 3); 00145 bmbfo1(); 00146 EXPECT_EQ(1,a.result); 00147 } 00148 00149 TEST(FunctionObjects,wrappers) { 00150 00151 FunctionObject function_object; 00152 NullaryFunctionCopy<FunctionObject> nfcopy(function_object); 00153 nfcopy(); 00154 // Can't really validate this baby. 00155 00156 // std::cout << "Nullary Function References." << std::endl; 00157 NullaryFunctionReference<FunctionObject> nfref(ref(function_object)); 00158 nfref(); 00159 // Can't really validate this baby. 00160 } 00161 00162 TEST(FunctionObjects,generators) { 00163 00164 MemberFunctions a; 00165 generateFunctionObject(rnf1)(); 00166 EXPECT_EQ(0,free_result); 00167 generateFunctionObject(ruf1)(3); 00168 EXPECT_EQ(2,free_result); 00169 generateFunctionObject(ruf1,3)(); 00170 EXPECT_EQ(2,free_result); 00171 generateFunctionObject(&MemberFunctions::e)(a); 00172 EXPECT_EQ(0,a.result); 00173 generateFunctionObject(&MemberFunctions::e, a)(); 00174 EXPECT_EQ(0,a.result); 00175 generateFunctionObject(&MemberFunctions::f)(a,1); 00176 EXPECT_EQ(1,a.result); 00177 generateFunctionObject(&MemberFunctions::h)(a,1); 00178 EXPECT_EQ(3,a.result); 00179 generateFunctionObject(&MemberFunctions::h,a,1)(); // Calls the non const version 00180 EXPECT_EQ(3,a.result); 00181 int j = 3; 00182 generateFunctionObject(&MemberFunctions::h,a,j)(); // Calls the const version 00183 EXPECT_EQ(3,a.result); 00184 } 00185 00186 /***************************************************************************** 00187 ** Main program 00188 *****************************************************************************/ 00189 00190 int main(int argc, char **argv) { 00191 testing::InitGoogleTest(&argc,argv); 00192 return RUN_ALL_TESTS(); 00193 }