$search
00001 00009 /***************************************************************************** 00010 ** Includes 00011 *****************************************************************************/ 00012 00013 00014 #include <iostream> 00015 #include <gtest/gtest.h> 00016 #include <ecl/exceptions/standard_exception.hpp> 00017 #include "../../include/ecl/sigslots/signal.hpp" 00018 #include "../../include/ecl/sigslots/slot.hpp" 00019 00020 /***************************************************************************** 00021 ** Using 00022 *****************************************************************************/ 00023 00024 using ecl::StandardException; 00025 using ecl::Signal; 00026 using ecl::Slot; 00027 00031 /***************************************************************************** 00032 ** Namespaces 00033 *****************************************************************************/ 00034 00035 namespace ecl { 00036 namespace sigslots { 00037 namespace tests { 00038 00039 /***************************************************************************** 00040 ** Classes 00041 *****************************************************************************/ 00042 00043 class A { 00044 public: 00045 A() : f_count(0), g_count(0) {} 00046 00047 void f(const int &i) { 00048 // std::cout << " Member function with argument: " << i << std::endl;; 00049 f_count++; 00050 } 00051 void g() { 00052 g_count++; 00053 // std::cout << " Member function" << std::endl;; 00054 } 00055 int f_count, g_count; 00056 }; 00057 00058 /***************************************************************************** 00059 ** Functions 00060 *****************************************************************************/ 00061 00062 static int f_count = 0; 00063 static int g_count = 0; 00064 00065 void f(const int &i) 00066 { 00067 f_count++; 00068 // std::cout << " Global function with argument: " << i << std::endl;; 00069 } 00070 00071 void g() 00072 { 00073 g_count++; 00074 // std::cout << " Global function" << std::endl;; 00075 } 00076 00077 } // namespace tests 00078 } // namespace signals 00079 } // namespace ecl 00080 00081 /***************************************************************************** 00082 ** Using 00083 *****************************************************************************/ 00084 00085 using namespace ecl::sigslots::tests; 00086 00090 /***************************************************************************** 00091 ** Tests 00092 *****************************************************************************/ 00093 00094 TEST(SigSlotsTests, voidSigSlots) { 00095 00096 A a; 00097 Signal<> sig_void; 00098 00099 Slot<> slot_void0(g,"void_test"); 00100 Slot<> slot_void1(&A::g,a); 00101 00102 sig_void.connect("void_test"); 00103 // slot_void0.connect("void_test"); 00104 slot_void1.connect("void_test"); 00105 00106 sig_void.emit(); 00107 00108 // ecl::SigSlotsManager<>::printStatistics(); 00109 00110 // std::cout << "a.g_count: " << a.g_count << std::endl; 00111 // std::cout << "g_count: " << g_count << std::endl; 00112 EXPECT_EQ(1,a.g_count); 00113 EXPECT_EQ(1,g_count); 00114 } 00115 00116 TEST(SigSlotsTests, relay) { 00117 00118 A a; 00119 Signal<> sig_starter; 00120 Signal<> sig_relay; 00121 00122 Slot<> slot_relayed_goal(g); 00123 00124 sig_starter.connect("relay"); 00125 sig_relay.connectAsSlot("relay"); 00126 sig_relay.connect("final_goal"); 00127 slot_relayed_goal.connect("final_goal"); 00128 00129 sig_starter.emit(); 00130 00131 // std::cout << "g_count: " << g_count << std::endl; 00132 EXPECT_EQ(2,g_count); 00133 } 00134 00135 TEST(SigSlotsTests,dataSigSlots) { 00136 00137 A a; 00138 Signal<const int&> sig_int; 00139 Slot<const int&> slot_int0(f); 00140 Slot<const int&> slot_int1(&A::f,a); 00141 00142 sig_int.connect("int_test"); 00143 slot_int0.connect("int_test"); 00144 slot_int1.connect("int_test"); 00145 00146 sig_int.emit(4); 00147 // std::cout << "a.f_count: " << a.f_count << std::endl; 00148 // std::cout << "f_count: " << f_count << std::endl; 00149 EXPECT_EQ(1,a.f_count); 00150 EXPECT_EQ(1,f_count); 00151 } 00152 00153 TEST(SigSlotsTests, disconnect) { 00154 00155 Signal<> signal; 00156 Slot<> slot(g); 00157 signal.connect("disconnect"); 00158 slot.connect("disconnect"); 00159 signal.emit(); 00160 slot.disconnect(); 00161 Signal<const int&> data_signal; 00162 Slot<const int&> data_slot(f); 00163 data_signal.connect("disconnect"); // Each data type has its own 'namespace' per se, so this doesn't clash with the above. 00164 data_slot.connect("disconnect"); 00165 data_signal.emit(1); 00166 data_slot.disconnect(); 00167 data_signal.emit(1); // Shouldn't increment 00168 00169 EXPECT_EQ(3,g_count); 00170 EXPECT_EQ(2,f_count); 00171 } 00172 00173 /***************************************************************************** 00174 ** Main program 00175 *****************************************************************************/ 00176 00177 int main(int argc, char **argv) { 00178 00179 testing::InitGoogleTest(&argc,argv); 00180 return RUN_ALL_TESTS(); 00181 }