SignalSlot_test.cc revision 2d6ff445
12d6ff445SShuo Chen#include "../SignalSlot.h"
24670cafeSShuo Chen#include "../SignalSlotTrivial.h"
34670cafeSShuo Chen
44670cafeSShuo Chen#include <boost/bind.hpp>
54670cafeSShuo Chen
64670cafeSShuo Chen#define BOOST_TEST_MAIN
74670cafeSShuo Chen#ifdef BOOST_TEST_DYN_LINK
84670cafeSShuo Chen#include <boost/test/unit_test.hpp>
94670cafeSShuo Chen#else
104670cafeSShuo Chen#include <boost/test/included/unit_test.hpp>
114670cafeSShuo Chen#endif
124670cafeSShuo Chen
134670cafeSShuo Chenclass String
144670cafeSShuo Chen{
154670cafeSShuo Chen public:
164670cafeSShuo Chen  String(const char* str)
174670cafeSShuo Chen  {
184670cafeSShuo Chen    printf("String ctor this %p\n", this);
194670cafeSShuo Chen  }
204670cafeSShuo Chen
214670cafeSShuo Chen  String(const String& rhs)
224670cafeSShuo Chen  {
234670cafeSShuo Chen    printf("String copy ctor this %p, rhs %p\n", this, &rhs);
244670cafeSShuo Chen  }
254670cafeSShuo Chen
264670cafeSShuo Chen  String(String&& rhs)
274670cafeSShuo Chen  {
284670cafeSShuo Chen    printf("String move ctor this %p, rhs %p\n", this, &rhs);
294670cafeSShuo Chen  }
304670cafeSShuo Chen};
314670cafeSShuo Chen
324670cafeSShuo Chenclass Foo : boost::noncopyable
334670cafeSShuo Chen{
344670cafeSShuo Chen public:
354670cafeSShuo Chen  void zero();
364670cafeSShuo Chen  void zeroc() const;
374670cafeSShuo Chen  void one(int);
384670cafeSShuo Chen  void oner(int&);
394670cafeSShuo Chen  void onec(int) const;
404670cafeSShuo Chen  void oneString(const String& str);
414670cafeSShuo Chen  // void oneStringRR(String&& str);
424670cafeSShuo Chen  static void szero();
434670cafeSShuo Chen  static void sone(int);
444670cafeSShuo Chen  static void soneString(const String& str);
454670cafeSShuo Chen};
464670cafeSShuo Chen
474670cafeSShuo Chenvoid Foo::zero()
484670cafeSShuo Chen{
494670cafeSShuo Chen  printf("Foo::zero()\n");
504670cafeSShuo Chen}
514670cafeSShuo Chen
524670cafeSShuo Chenvoid Foo::zeroc() const
534670cafeSShuo Chen{
544670cafeSShuo Chen  printf("Foo::zeroc()\n");
554670cafeSShuo Chen}
564670cafeSShuo Chen
574670cafeSShuo Chenvoid Foo::szero()
584670cafeSShuo Chen{
594670cafeSShuo Chen  printf("Foo::szero()\n");
604670cafeSShuo Chen}
614670cafeSShuo Chen
624670cafeSShuo Chenvoid Foo::one(int x)
634670cafeSShuo Chen{
644670cafeSShuo Chen  printf("Foo::one() x=%d\n", x);
654670cafeSShuo Chen}
664670cafeSShuo Chen
674670cafeSShuo Chenvoid Foo::onec(int x) const
684670cafeSShuo Chen{
694670cafeSShuo Chen  printf("Foo::onec() x=%d\n", x);
704670cafeSShuo Chen}
714670cafeSShuo Chen
724670cafeSShuo Chenvoid Foo::sone(int x)
734670cafeSShuo Chen{
744670cafeSShuo Chen  printf("Foo::sone() x=%d\n", x);
754670cafeSShuo Chen}
764670cafeSShuo Chen
774670cafeSShuo Chenvoid Foo::oneString(const String& str)
784670cafeSShuo Chen{
794670cafeSShuo Chen  printf("Foo::oneString\n");
804670cafeSShuo Chen}
814670cafeSShuo Chen
824670cafeSShuo Chenvoid Foo::soneString(const String& str)
834670cafeSShuo Chen{
844670cafeSShuo Chen  printf("Foo::soneString\n");
854670cafeSShuo Chen}
864670cafeSShuo Chen
874670cafeSShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotTrivialZero)
884670cafeSShuo Chen{
894670cafeSShuo Chen  SignalTrivial<void()> signal;
904670cafeSShuo Chen
914670cafeSShuo Chen  printf("========\n");
924670cafeSShuo Chen  signal.call();
934670cafeSShuo Chen
944670cafeSShuo Chen  signal.connect(&Foo::szero);
954670cafeSShuo Chen
964670cafeSShuo Chen  printf("========\n");
974670cafeSShuo Chen  signal.call();
984670cafeSShuo Chen
994670cafeSShuo Chen  Foo f;
1004670cafeSShuo Chen  signal.connect(boost::bind(&Foo::zero, &f));
1014670cafeSShuo Chen
1024670cafeSShuo Chen  printf("========\n");
1034670cafeSShuo Chen  signal.call();
1044670cafeSShuo Chen
1054670cafeSShuo Chen  signal.connect(boost::bind(&Foo::one, &f, 42));
1064670cafeSShuo Chen
1074670cafeSShuo Chen  printf("========\n");
1084670cafeSShuo Chen  signal.call();
1094670cafeSShuo Chen
1104670cafeSShuo Chen  const Foo cf;
1114670cafeSShuo Chen  signal.connect(boost::bind(&Foo::zeroc, &cf));
1124670cafeSShuo Chen
1134670cafeSShuo Chen  printf("========\n");
1144670cafeSShuo Chen  signal.call();
1154670cafeSShuo Chen
1164670cafeSShuo Chen  signal.connect(boost::bind(&Foo::onec, &cf, 128));
1174670cafeSShuo Chen
1184670cafeSShuo Chen  printf("========\n");
1194670cafeSShuo Chen  signal.call();
1204670cafeSShuo Chen
1214670cafeSShuo Chen  printf("========\n");
1224670cafeSShuo Chen  signal.call();
1234670cafeSShuo Chen}
1244670cafeSShuo Chen
1254670cafeSShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotTrivialOne)
1264670cafeSShuo Chen{
1274670cafeSShuo Chen  SignalTrivial<void(int)> signal;
1284670cafeSShuo Chen
1294670cafeSShuo Chen  printf("========\n");
1304670cafeSShuo Chen  signal.call(50);
1314670cafeSShuo Chen
1324670cafeSShuo Chen  signal.connect(&Foo::sone);
1334670cafeSShuo Chen
1344670cafeSShuo Chen  printf("========\n");
1354670cafeSShuo Chen  signal.call(51);
1364670cafeSShuo Chen
1374670cafeSShuo Chen  Foo f;
1384670cafeSShuo Chen  signal.connect(boost::bind(&Foo::one, &f, _1));
1394670cafeSShuo Chen
1404670cafeSShuo Chen  printf("========\n");
1414670cafeSShuo Chen  signal.call(52);
1424670cafeSShuo Chen
1434670cafeSShuo Chen  const Foo cf;
1444670cafeSShuo Chen  signal.connect(boost::bind(&Foo::onec, &cf, _1));
1454670cafeSShuo Chen
1464670cafeSShuo Chen  printf("========\n");
1474670cafeSShuo Chen  signal.call(53);
1484670cafeSShuo Chen}
1494670cafeSShuo Chen
1504670cafeSShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotTrivialString)
1514670cafeSShuo Chen{
1524670cafeSShuo Chen  SignalTrivial<void(const String&)> signal;
1534670cafeSShuo Chen  signal.call("hello");
1544670cafeSShuo Chen
1554670cafeSShuo Chen  signal.connect(&Foo::soneString);
1564670cafeSShuo Chen
1574670cafeSShuo Chen  printf("========\n");
1584670cafeSShuo Chen  signal.call("hello");
1594670cafeSShuo Chen
1604670cafeSShuo Chen  Foo f;
1614670cafeSShuo Chen  signal.connect(boost::bind(&Foo::oneString, &f, _1));
1624670cafeSShuo Chen
1634670cafeSShuo Chen  printf("========\n");
1644670cafeSShuo Chen  signal.call("hello");
1654670cafeSShuo Chen}
1664670cafeSShuo Chen
1672d6ff445SShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotZero)
1682d6ff445SShuo Chen{
1692d6ff445SShuo Chen  muduo::Signal<void()> signal;
1702d6ff445SShuo Chen
1712d6ff445SShuo Chen  printf("==== testSignalSlotZero ====\n");
1722d6ff445SShuo Chen  signal.call();
1732d6ff445SShuo Chen
1742d6ff445SShuo Chen  muduo::Slot s1 = signal.connect(&Foo::szero);
1752d6ff445SShuo Chen
1762d6ff445SShuo Chen  printf("========\n");
1772d6ff445SShuo Chen  signal.call();
1782d6ff445SShuo Chen
1792d6ff445SShuo Chen  Foo f;
1802d6ff445SShuo Chen  muduo::Slot s2 = signal.connect(boost::bind(&Foo::zero, &f));
1812d6ff445SShuo Chen
1822d6ff445SShuo Chen  printf("========\n");
1832d6ff445SShuo Chen  signal.call();
1842d6ff445SShuo Chen
1852d6ff445SShuo Chen  muduo::Slot s3 = signal.connect(boost::bind(&Foo::one, &f, 42));
1862d6ff445SShuo Chen
1872d6ff445SShuo Chen  printf("========\n");
1882d6ff445SShuo Chen  signal.call();
1892d6ff445SShuo Chen
1902d6ff445SShuo Chen  const Foo cf;
1912d6ff445SShuo Chen  muduo::Slot s4 = signal.connect(boost::bind(&Foo::zeroc, &cf));
1922d6ff445SShuo Chen
1932d6ff445SShuo Chen  printf("========\n");
1942d6ff445SShuo Chen  signal.call();
1952d6ff445SShuo Chen
1962d6ff445SShuo Chen  muduo::Slot s5 = signal.connect(boost::bind(&Foo::onec, &cf, 128));
1972d6ff445SShuo Chen
1982d6ff445SShuo Chen  printf("========\n");
1992d6ff445SShuo Chen  signal.call();
2002d6ff445SShuo Chen
2012d6ff445SShuo Chen  s1 = muduo::Slot();
2022d6ff445SShuo Chen  printf("========\n");
2032d6ff445SShuo Chen  signal.call();
2042d6ff445SShuo Chen
2052d6ff445SShuo Chen
2062d6ff445SShuo Chen  s4 = s3 = s2 = muduo::Slot();
2072d6ff445SShuo Chen  printf("========\n");
2082d6ff445SShuo Chen  signal.call();
2092d6ff445SShuo Chen
2102d6ff445SShuo Chen}
2112d6ff445SShuo Chen
2122d6ff445SShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotOne)
2132d6ff445SShuo Chen{
2142d6ff445SShuo Chen  muduo::Signal<void(int)> signal;
2152d6ff445SShuo Chen
2162d6ff445SShuo Chen  printf("========\n");
2172d6ff445SShuo Chen  signal.call(50);
2182d6ff445SShuo Chen
2192d6ff445SShuo Chen  muduo::Slot s4;
2202d6ff445SShuo Chen  {
2212d6ff445SShuo Chen  muduo::Slot s1 = signal.connect(&Foo::sone);
2222d6ff445SShuo Chen
2232d6ff445SShuo Chen  printf("========\n");
2242d6ff445SShuo Chen  signal.call(51);
2252d6ff445SShuo Chen
2262d6ff445SShuo Chen  Foo f;
2272d6ff445SShuo Chen  muduo::Slot s2 = signal.connect(boost::bind(&Foo::one, &f, _1));
2282d6ff445SShuo Chen
2292d6ff445SShuo Chen  printf("========\n");
2302d6ff445SShuo Chen  signal.call(52);
2312d6ff445SShuo Chen
2322d6ff445SShuo Chen  const Foo cf;
2332d6ff445SShuo Chen  muduo::Slot s3 = signal.connect(boost::bind(&Foo::onec, &cf, _1));
2342d6ff445SShuo Chen
2352d6ff445SShuo Chen  printf("========\n");
2362d6ff445SShuo Chen  signal.call(53);
2372d6ff445SShuo Chen
2382d6ff445SShuo Chen  s4 = s3;
2392d6ff445SShuo Chen  }
2402d6ff445SShuo Chen
2412d6ff445SShuo Chen  printf("========\n");
2422d6ff445SShuo Chen  signal.call(54);
2432d6ff445SShuo Chen}
2442d6ff445SShuo Chen
2452d6ff445SShuo ChenBOOST_AUTO_TEST_CASE(testSignalSlotLife)
2462d6ff445SShuo Chen{
2472d6ff445SShuo Chen  muduo::Slot s1;
2482d6ff445SShuo Chen
2492d6ff445SShuo Chen  {
2502d6ff445SShuo Chen  muduo::Signal<void()> signal;
2512d6ff445SShuo Chen  s1 = signal.connect(&Foo::szero);
2522d6ff445SShuo Chen
2532d6ff445SShuo Chen  printf("========\n");
2542d6ff445SShuo Chen  signal.call();
2552d6ff445SShuo Chen  }
2562d6ff445SShuo Chen
2572d6ff445SShuo Chen}
2582d6ff445SShuo Chen
259