1 | // Boost.Signals2 library |
---|---|
2 | |
3 | // Copyright Douglas Gregor 2001-2003. |
4 | // Use, modification and |
5 | // distribution is subject to the Boost Software License, Version |
6 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // For more information, see http://www.boost.org |
10 | |
11 | #include <boost/array.hpp> |
12 | #include <boost/signals2/shared_connection_block.hpp> |
13 | #include <boost/signals2/signal.hpp> |
14 | #define BOOST_TEST_MODULE shared_connection_block_test |
15 | #include <boost/test/included/unit_test.hpp> |
16 | #include <iostream> |
17 | #include <sstream> |
18 | #include <string> |
19 | |
20 | static boost::array<boost::signals2::connection, 4> connections; |
21 | |
22 | static std::ostringstream test_output; |
23 | |
24 | struct test_slot { |
25 | explicit test_slot(int v = 0) : value(v) |
26 | {} |
27 | |
28 | void operator()() const { |
29 | test_output << value; |
30 | } |
31 | |
32 | int value; |
33 | }; |
34 | |
35 | BOOST_AUTO_TEST_CASE(test_main) |
36 | { |
37 | boost::signals2::signal<void ()> s0; |
38 | |
39 | for(unsigned i = 0; i < connections.size(); ++i) |
40 | { |
41 | connections.at(i) = s0.connect(slot: test_slot(i)); |
42 | } |
43 | |
44 | |
45 | { |
46 | // Blocking 2 |
47 | boost::signals2::shared_connection_block block(connections.at(i: 2)); |
48 | BOOST_CHECK(block.blocking()); |
49 | test_output.str(s: ""); |
50 | s0(); |
51 | BOOST_CHECK(test_output.str() == "013"); |
52 | } |
53 | |
54 | // Unblocking 2 |
55 | test_output.str(s: ""); |
56 | s0(); |
57 | BOOST_CHECK(test_output.str() == "0123"); |
58 | |
59 | { |
60 | // Blocking 1 through const connection |
61 | const boost::signals2::connection conn = connections.at(i: 1); |
62 | boost::signals2::shared_connection_block block(conn); |
63 | test_output.str(s: ""); |
64 | s0(); |
65 | std::cout << test_output.str() << std::endl; |
66 | BOOST_CHECK(test_output.str() == "023"); |
67 | // Unblocking 1 |
68 | block.unblock(); |
69 | BOOST_CHECK(block.blocking() == false); |
70 | test_output.str(s: ""); |
71 | s0(); |
72 | BOOST_CHECK(test_output.str() == "0123"); |
73 | } |
74 | |
75 | { |
76 | // initially unblocked |
77 | boost::signals2::shared_connection_block block(connections.at(i: 3), false); |
78 | BOOST_CHECK(block.blocking() == false); |
79 | test_output.str(s: ""); |
80 | s0(); |
81 | BOOST_CHECK(test_output.str() == "0123"); |
82 | // block |
83 | block.block(); |
84 | test_output.str(s: ""); |
85 | s0(); |
86 | BOOST_CHECK(test_output.str() == "012"); |
87 | } |
88 | |
89 | { |
90 | // test default constructed block |
91 | boost::signals2::shared_connection_block block; |
92 | BOOST_CHECK(block.blocking() == true); |
93 | block.unblock(); |
94 | BOOST_CHECK(block.blocking() == false); |
95 | block.block(); |
96 | BOOST_CHECK(block.blocking() == true); |
97 | |
98 | // test assignment |
99 | { |
100 | block.unblock(); |
101 | boost::signals2::shared_connection_block block2(connections.at(i: 0)); |
102 | BOOST_CHECK(block.connection() != block2.connection()); |
103 | BOOST_CHECK(block.blocking() != block2.blocking()); |
104 | block = block2; |
105 | BOOST_CHECK(block.connection() == block2.connection()); |
106 | BOOST_CHECK(block.blocking() == block2.blocking()); |
107 | } |
108 | test_output.str(s: ""); |
109 | s0(); |
110 | BOOST_CHECK(test_output.str() == "123"); |
111 | } |
112 | } |
113 |