1 // Signals2 library 2 // tests for connection class 3 4 // Copyright Frank Mori Hess 2008 5 // Use, modification and 6 // distribution is subject to the Boost Software License, Version 7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 // http://www.boost.org/LICENSE_1_0.txt) 9 10 // For more information, see http://www.boost.org 11 #include <boost/signals2.hpp> 12 #define BOOST_TEST_MODULE connection_test 13 #include <boost/test/included/unit_test.hpp> 14 15 namespace bs2 = boost::signals2; 16 17 typedef bs2::signal<void ()> sig_type; 18 myslot()19void myslot() 20 {} 21 swap_test()22void swap_test() 23 { 24 sig_type sig; 25 26 { 27 bs2::connection conn1 = sig.connect(&myslot); 28 BOOST_CHECK(conn1.connected()); 29 bs2::connection conn2; 30 BOOST_CHECK(conn2.connected() == false); 31 32 conn1.swap(conn2); 33 BOOST_CHECK(conn2.connected()); 34 BOOST_CHECK(conn1.connected() == false); 35 36 swap(conn1, conn2); 37 BOOST_CHECK(conn1.connected()); 38 BOOST_CHECK(conn2.connected() == false); 39 } 40 41 { 42 bs2::scoped_connection conn1; 43 conn1 = sig.connect(&myslot); 44 BOOST_CHECK(conn1.connected()); 45 bs2::scoped_connection conn2; 46 BOOST_CHECK(conn2.connected() == false); 47 48 conn1.swap(conn2); 49 BOOST_CHECK(conn2.connected()); 50 BOOST_CHECK(conn1.connected() == false); 51 52 swap(conn1, conn2); 53 BOOST_CHECK(conn1.connected()); 54 BOOST_CHECK(conn2.connected() == false); 55 } 56 } 57 release_test()58void release_test() 59 { 60 sig_type sig; 61 bs2::connection conn; 62 { 63 bs2::scoped_connection scoped(sig.connect(&myslot)); 64 BOOST_CHECK(scoped.connected()); 65 conn = scoped.release(); 66 } 67 BOOST_CHECK(conn.connected()); 68 69 bs2::connection conn2; 70 { 71 bs2::scoped_connection scoped(conn); 72 BOOST_CHECK(scoped.connected()); 73 conn = scoped.release(); 74 BOOST_CHECK(conn.connected()); 75 BOOST_CHECK(scoped.connected() == false); 76 conn.disconnect(); 77 78 // earlier release shouldn't affect new connection 79 conn2 = sig.connect(&myslot); 80 scoped = conn2; 81 } 82 BOOST_CHECK(conn2.connected() == false); 83 } 84 move_test()85void move_test() 86 { 87 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) 88 sig_type sig; 89 bs2::connection conn; 90 // test move assignment from scoped_connection to connection 91 { 92 bs2::scoped_connection scoped(sig.connect(&myslot)); 93 BOOST_CHECK(scoped.connected()); 94 conn = std::move(scoped); 95 BOOST_CHECK(scoped.connected() == false); 96 } 97 BOOST_CHECK(conn.connected()); 98 99 // test move construction from scoped to scoped 100 { 101 bs2::scoped_connection scoped2(conn); 102 BOOST_CHECK(scoped2.connected()); 103 bs2::scoped_connection scoped3(std::move(scoped2)); 104 BOOST_CHECK(scoped2.connected() == false); 105 BOOST_CHECK(scoped3.connected() == true); 106 BOOST_CHECK(conn.connected() == true); 107 } 108 BOOST_CHECK(conn.connected() == false); 109 110 // test move assignment from scoped to scoped 111 conn = sig.connect(&myslot); 112 { 113 bs2::scoped_connection scoped3; 114 bs2::scoped_connection scoped2(conn); 115 BOOST_CHECK(scoped2.connected()); 116 scoped3 = std::move(scoped2); 117 BOOST_CHECK(scoped2.connected() == false); 118 BOOST_CHECK(scoped3.connected() == true); 119 BOOST_CHECK(conn.connected() == true); 120 } 121 BOOST_CHECK(conn.connected() == false); 122 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) 123 } 124 BOOST_AUTO_TEST_CASE(test_main)125BOOST_AUTO_TEST_CASE(test_main) 126 { 127 release_test(); 128 swap_test(); 129 move_test(); 130 } 131