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