1 #define BOOST_TEST_MODULE STGSubscriptions
3 #include "stg/subscriptions.h"
5 #pragma GCC diagnostic push
6 #pragma GCC diagnostic ignored "-Wold-style-cast"
7 #pragma GCC diagnostic ignored "-Wunused-parameter"
8 #pragma GCC diagnostic ignored "-Wsign-compare"
9 #pragma GCC diagnostic ignored "-Wparentheses"
10 #include <boost/test/unit_test.hpp>
11 #pragma GCC diagnostic pop
13 using STG::Subscriptions;
14 using STG::ScopedConnection;
29 void r0() { ++countR0; }
30 void r1(uint8_t v) { ++countR1; valueR1 = v; }
31 void r2(uint8_t v1, const std::string& v2) { ++countR2; value1R2 = v1; value2R2 = v2; }
43 BOOST_AUTO_TEST_SUITE(TestSubscriptions)
45 BOOST_AUTO_TEST_CASE(Construction)
47 Subscriptions<> nullary;
48 BOOST_CHECK(nullary.empty());
49 BOOST_CHECK_EQUAL(nullary.size(), 0);
51 Subscriptions<uint8_t> unary;
52 BOOST_CHECK(unary.empty());
53 BOOST_CHECK_EQUAL(unary.size(), 0);
55 Subscriptions<uint8_t, std::string> binary;
56 BOOST_CHECK(binary.empty());
57 BOOST_CHECK_EQUAL(binary.size(), 0);
60 BOOST_AUTO_TEST_CASE(AddingAndRemoving)
63 Subscriptions<> nullary;
65 ScopedConnection c1 = nullary.add(r, &Receiver::r0);
66 ScopedConnection c2 = nullary.add([&r](){ r.r0(); });
68 BOOST_CHECK(!nullary.empty());
69 BOOST_CHECK_EQUAL(nullary.size(), 2);
73 BOOST_CHECK(!nullary.empty());
74 BOOST_CHECK_EQUAL(nullary.size(), 1);
76 BOOST_CHECK(nullary.empty());
77 BOOST_CHECK_EQUAL(nullary.size(), 0);
79 Subscriptions<uint8_t> unary;
81 ScopedConnection c1 = unary.add(r, &Receiver::r1);
82 ScopedConnection c2 = unary.add([&r](const auto& v){ r.r1(v); });
84 BOOST_CHECK(!unary.empty());
85 BOOST_CHECK_EQUAL(unary.size(), 2);
89 BOOST_CHECK(!unary.empty());
90 BOOST_CHECK_EQUAL(unary.size(), 1);
92 BOOST_CHECK(unary.empty());
93 BOOST_CHECK_EQUAL(unary.size(), 0);
95 Subscriptions<uint8_t, std::string> binary;
97 ScopedConnection c1 = binary.add(r, &Receiver::r2);
98 ScopedConnection c2 = binary.add([&r](const auto& v1, const auto& v2){ r.r2(v1, v2); });
100 BOOST_CHECK(!binary.empty());
101 BOOST_CHECK_EQUAL(binary.size(), 2);
105 BOOST_CHECK(!binary.empty());
106 BOOST_CHECK_EQUAL(binary.size(), 1);
108 BOOST_CHECK(binary.empty());
109 BOOST_CHECK_EQUAL(binary.size(), 0);
112 BOOST_AUTO_TEST_CASE(Notification)
116 BOOST_CHECK_EQUAL(r.countR0, 0);
117 BOOST_CHECK_EQUAL(r.countR1, 0);
118 BOOST_CHECK_EQUAL(r.valueR1, 0);
119 BOOST_CHECK_EQUAL(r.countR2, 0);
120 BOOST_CHECK_EQUAL(r.value1R2, 0);
121 BOOST_CHECK_EQUAL(r.value2R2, "");
123 Subscriptions<> nullary;
125 ScopedConnection c1 = nullary.add(r, &Receiver::r0);
126 ScopedConnection c2 = nullary.add([&r](){ r.r0(); });
130 BOOST_CHECK_EQUAL(r.countR0, 2);
131 BOOST_CHECK_EQUAL(r.countR1, 0);
132 BOOST_CHECK_EQUAL(r.valueR1, 0);
133 BOOST_CHECK_EQUAL(r.countR2, 0);
134 BOOST_CHECK_EQUAL(r.value1R2, 0);
135 BOOST_CHECK_EQUAL(r.value2R2, "");
140 BOOST_CHECK_EQUAL(r.countR0, 3);
141 BOOST_CHECK_EQUAL(r.countR1, 0);
142 BOOST_CHECK_EQUAL(r.valueR1, 0);
143 BOOST_CHECK_EQUAL(r.countR2, 0);
144 BOOST_CHECK_EQUAL(r.value1R2, 0);
145 BOOST_CHECK_EQUAL(r.value2R2, "");
150 BOOST_CHECK_EQUAL(r.countR0, 3);
151 BOOST_CHECK_EQUAL(r.countR1, 0);
152 BOOST_CHECK_EQUAL(r.valueR1, 0);
153 BOOST_CHECK_EQUAL(r.countR2, 0);
154 BOOST_CHECK_EQUAL(r.value1R2, 0);
155 BOOST_CHECK_EQUAL(r.value2R2, "");
157 Subscriptions<uint8_t> unary;
159 ScopedConnection c1 = unary.add(r, &Receiver::r1);
160 ScopedConnection c2 = unary.add([&r](const auto& v){ r.r1(v); });
164 BOOST_CHECK_EQUAL(r.countR0, 3);
165 BOOST_CHECK_EQUAL(r.countR1, 2);
166 BOOST_CHECK_EQUAL(r.valueR1, 42);
167 BOOST_CHECK_EQUAL(r.countR2, 0);
168 BOOST_CHECK_EQUAL(r.value1R2, 0);
169 BOOST_CHECK_EQUAL(r.value2R2, "");
174 BOOST_CHECK_EQUAL(r.countR0, 3);
175 BOOST_CHECK_EQUAL(r.countR1, 3);
176 BOOST_CHECK_EQUAL(r.valueR1, 13);
177 BOOST_CHECK_EQUAL(r.countR2, 0);
178 BOOST_CHECK_EQUAL(r.value1R2, 0);
179 BOOST_CHECK_EQUAL(r.value2R2, "");
184 BOOST_CHECK_EQUAL(r.countR0, 3);
185 BOOST_CHECK_EQUAL(r.countR1, 3);
186 BOOST_CHECK_EQUAL(r.valueR1, 13);
187 BOOST_CHECK_EQUAL(r.countR2, 0);
188 BOOST_CHECK_EQUAL(r.value1R2, 0);
189 BOOST_CHECK_EQUAL(r.value2R2, "");
191 Subscriptions<uint8_t, std::string> binary;
193 ScopedConnection c1 = binary.add(r, &Receiver::r2);
194 ScopedConnection c2 = binary.add([&r](const auto& v1, const auto& v2){ r.r2(v1, v2); });
196 binary.notify(42, "Douglas");
198 BOOST_CHECK_EQUAL(r.countR0, 3);
199 BOOST_CHECK_EQUAL(r.countR1, 3);
200 BOOST_CHECK_EQUAL(r.valueR1, 13);
201 BOOST_CHECK_EQUAL(r.countR2, 2);
202 BOOST_CHECK_EQUAL(r.value1R2, 42);
203 BOOST_CHECK_EQUAL(r.value2R2, "Douglas");
206 binary.notify(21, "Adams");
208 BOOST_CHECK_EQUAL(r.countR0, 3);
209 BOOST_CHECK_EQUAL(r.countR1, 3);
210 BOOST_CHECK_EQUAL(r.valueR1, 13);
211 BOOST_CHECK_EQUAL(r.countR2, 3);
212 BOOST_CHECK_EQUAL(r.value1R2, 21);
213 BOOST_CHECK_EQUAL(r.value2R2, "Adams");
216 binary.notify(13, "Devil's Dozen");
218 BOOST_CHECK_EQUAL(r.countR0, 3);
219 BOOST_CHECK_EQUAL(r.countR1, 3);
220 BOOST_CHECK_EQUAL(r.valueR1, 13);
221 BOOST_CHECK_EQUAL(r.countR2, 3);
222 BOOST_CHECK_EQUAL(r.value1R2, 21);
223 BOOST_CHECK_EQUAL(r.value2R2, "Adams");
226 BOOST_AUTO_TEST_SUITE_END()