WeakCallback_test.cc revision 112f583d
1a0e66be7SShuo Chen#include "../WeakCallback.h"
2a0e66be7SShuo Chen
3a0e66be7SShuo Chen#define BOOST_TEST_MAIN
4a0e66be7SShuo Chen#ifdef BOOST_TEST_DYN_LINK
5a0e66be7SShuo Chen#include <boost/test/unit_test.hpp>
6a0e66be7SShuo Chen#else
7a0e66be7SShuo Chen#include <boost/test/included/unit_test.hpp>
8a0e66be7SShuo Chen#endif
9a0e66be7SShuo Chen
10a0e66be7SShuo Chen#include <stdio.h>
11a0e66be7SShuo Chen
12a0e66be7SShuo Chenclass String
13a0e66be7SShuo Chen{
14a0e66be7SShuo Chen public:
15a0e66be7SShuo Chen  String(const char* str)
16a0e66be7SShuo Chen  {
17a0e66be7SShuo Chen    printf("String ctor this %p\n", this);
18a0e66be7SShuo Chen  }
19a0e66be7SShuo Chen
20a0e66be7SShuo Chen  String(const String& rhs)
21a0e66be7SShuo Chen  {
22a0e66be7SShuo Chen    printf("String copy ctor this %p, rhs %p\n", this, &rhs);
23a0e66be7SShuo Chen  }
24a0e66be7SShuo Chen
25a0e66be7SShuo Chen  String(String&& rhs)
26a0e66be7SShuo Chen  {
27a0e66be7SShuo Chen    printf("String move ctor this %p, rhs %p\n", this, &rhs);
28a0e66be7SShuo Chen  }
29a0e66be7SShuo Chen};
30a0e66be7SShuo Chen
31a0e66be7SShuo Chenclass Foo : boost::noncopyable
32a0e66be7SShuo Chen{
33a0e66be7SShuo Chen public:
34a0e66be7SShuo Chen  void zero();
35a0e66be7SShuo Chen  void zeroc() const;
36a0e66be7SShuo Chen  void one(int);
37a0e66be7SShuo Chen  void oner(int&);
38a0e66be7SShuo Chen  void onec(int) const;
39a0e66be7SShuo Chen  void oneString(const String& str);
40a0e66be7SShuo Chen  void oneStringRR(String&& str);
41a0e66be7SShuo Chen};
42a0e66be7SShuo Chen
43a0e66be7SShuo Chenvoid Foo::zero()
44a0e66be7SShuo Chen{
45a0e66be7SShuo Chen  printf("Foo::zero()\n");
46a0e66be7SShuo Chen}
47a0e66be7SShuo Chen
48a0e66be7SShuo Chenvoid Foo::zeroc() const
49a0e66be7SShuo Chen{
50a0e66be7SShuo Chen  printf("Foo::zeroc()\n");
51a0e66be7SShuo Chen}
52a0e66be7SShuo Chen
53a0e66be7SShuo Chenvoid Foo::one(int x)
54a0e66be7SShuo Chen{
55a0e66be7SShuo Chen  printf("Foo::one() x=%d\n", x);
56a0e66be7SShuo Chen}
57a0e66be7SShuo Chen
58a0e66be7SShuo Chenvoid Foo::oner(int& x)
59a0e66be7SShuo Chen{
60a0e66be7SShuo Chen  printf("Foo::oner() x=%d\n", x);
61a0e66be7SShuo Chen  x = 1000;
62a0e66be7SShuo Chen}
63a0e66be7SShuo Chen
64a0e66be7SShuo Chenvoid Foo::onec(int x) const
65a0e66be7SShuo Chen{
66a0e66be7SShuo Chen  printf("Foo::onec() x=%d\n", x);
67a0e66be7SShuo Chen}
68a0e66be7SShuo Chen
69a0e66be7SShuo Chenvoid Foo::oneString(const String& str)
70a0e66be7SShuo Chen{
71a0e66be7SShuo Chen  printf("Foo::oneString\n");
72a0e66be7SShuo Chen}
73a0e66be7SShuo Chen
74a0e66be7SShuo Chenvoid Foo::oneStringRR(String&& str)
75a0e66be7SShuo Chen{
76a0e66be7SShuo Chen  printf("Foo::oneStringRR\n");
77a0e66be7SShuo Chen}
78a0e66be7SShuo Chen
79a0e66be7SShuo ChenString getString()
80a0e66be7SShuo Chen{
81a0e66be7SShuo Chen  return String("zz");
82a0e66be7SShuo Chen}
83a0e66be7SShuo Chen
84a0e66be7SShuo ChenBOOST_AUTO_TEST_CASE(testMove)
85a0e66be7SShuo Chen{
86a0e66be7SShuo Chen  String s("xx");
87a0e66be7SShuo Chen  Foo f;
88a0e66be7SShuo Chen  f.oneString(s);
89a0e66be7SShuo Chen  f.oneString(String("yy"));
90112f583dSShuo Chen  // f.oneStringRR(s);
91a0e66be7SShuo Chen  f.oneStringRR(String("yy"));
92a0e66be7SShuo Chen  f.oneString(getString());
93a0e66be7SShuo Chen  f.oneStringRR(getString());
94a0e66be7SShuo Chen}
95a0e66be7SShuo Chen
96a0e66be7SShuo ChenBOOST_AUTO_TEST_CASE(testWeakCallback)
97a0e66be7SShuo Chen{
98a0e66be7SShuo Chen  printf("======== testWeakCallback \n");
99a0e66be7SShuo Chen  std::shared_ptr<Foo> foo(new Foo);
100a0e66be7SShuo Chen  muduo::WeakCallback<Foo> cb0 = muduo::makeWeakCallback(foo, &Foo::zero);
101a0e66be7SShuo Chen  muduo::WeakCallback<Foo> cb0c = muduo::makeWeakCallback(foo, &Foo::zeroc);
102a0e66be7SShuo Chen  cb0();
103a0e66be7SShuo Chen  cb0c();
104a0e66be7SShuo Chen
105a0e66be7SShuo Chen  muduo::WeakCallback<Foo, int> cb1 = muduo::makeWeakCallback(foo, &Foo::one);
106a0e66be7SShuo Chen  auto cb1c = muduo::makeWeakCallback(foo, &Foo::onec);
107a0e66be7SShuo Chen  auto cb1r = muduo::makeWeakCallback(foo, &Foo::oner);
108a0e66be7SShuo Chen  cb1(123);
109a0e66be7SShuo Chen  cb1c(234);
110a0e66be7SShuo Chen  int i = 345;
111a0e66be7SShuo Chen  cb1r(i);
112a0e66be7SShuo Chen  BOOST_CHECK_EQUAL(i, 1000);
113a0e66be7SShuo Chen
114a0e66be7SShuo Chen  auto cb2 = muduo::makeWeakCallback(foo, &Foo::oneString);
115a0e66be7SShuo Chen  auto cb2r = muduo::makeWeakCallback(foo, &Foo::oneStringRR);
116a0e66be7SShuo Chen  printf("_Z%s\n", typeid(cb2).name());
117a0e66be7SShuo Chen  printf("_Z%s\n", typeid(cb2r).name());
118a0e66be7SShuo Chen  cb2(String("xx"));
119a0e66be7SShuo Chen  cb2r(String("yy"));
120a0e66be7SShuo Chen
121a0e66be7SShuo Chen  muduo::WeakCallback<Foo> cb3(foo, std::bind(&Foo::oneString, std::placeholders::_1, "zz"));
122a0e66be7SShuo Chen
123a0e66be7SShuo Chen  cb3();
124a0e66be7SShuo Chen
125a0e66be7SShuo Chen  printf("======== reset \n");
126a0e66be7SShuo Chen  foo.reset();
127a0e66be7SShuo Chen  cb0();
128a0e66be7SShuo Chen  cb0c();
129a0e66be7SShuo Chen  cb1(123);
130a0e66be7SShuo Chen  cb1c(234);
131a0e66be7SShuo Chen  cb2(String("xx"));
132a0e66be7SShuo Chen  cb2r(String("yy"));
133a0e66be7SShuo Chen  cb3();
134a0e66be7SShuo Chen}
135