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
26 void r0() { ++countR0; }
27 void r1(uint8_t v) { ++countR1; valueR1 = v; }
28 void r2(uint8_t v1, const std::string& v2) { ++countR2; value1R2 = v1; value2R2 = v2; }
40 BOOST_AUTO_TEST_SUITE(Subscriptions)
42 BOOST_AUTO_TEST_CASE(Construction)
44 STG::Subscriptions<> nullary;
45 BOOST_CHECK(nullary.empty());
46 BOOST_CHECK_EQUAL(nullary.size(), 0);
48 STG::Subscriptions<uint8_t> unary;
49 BOOST_CHECK(unary.empty());
50 BOOST_CHECK_EQUAL(unary.size(), 0);
52 STG::Subscriptions<uint8_t, std::string> binary;
53 BOOST_CHECK(binary.empty());
54 BOOST_CHECK_EQUAL(binary.size(), 0);
57 BOOST_AUTO_TEST_CASE(AddingAndRemoving)
60 STG::Subscriptions<> nullary;
62 auto c1 = nullary.add(r, &Receiver::r0);
63 auto c2 = nullary.add([&r](){ r.r0(); });
65 BOOST_CHECK(!nullary.empty());
66 BOOST_CHECK_EQUAL(nullary.size(), 2);
70 BOOST_CHECK(!nullary.empty());
71 BOOST_CHECK_EQUAL(nullary.size(), 1);
73 BOOST_CHECK(nullary.empty());
74 BOOST_CHECK_EQUAL(nullary.size(), 0);
76 STG::Subscriptions<uint8_t> unary;
78 auto c1 = unary.add(r, &Receiver::r1);
79 auto c2 = unary.add([&r](const auto& v){ r.r1(v); });
81 BOOST_CHECK(!unary.empty());
82 BOOST_CHECK_EQUAL(unary.size(), 2);
86 BOOST_CHECK(!unary.empty());
87 BOOST_CHECK_EQUAL(unary.size(), 1);
89 BOOST_CHECK(unary.empty());
90 BOOST_CHECK_EQUAL(unary.size(), 0);
92 STG::Subscriptions<uint8_t, std::string> binary;
94 auto c1 = binary.add(r, &Receiver::r2);
95 auto c2 = binary.add([&r](const auto& v1, const auto& v2){ r.r2(v1, v2); });
97 BOOST_CHECK(!binary.empty());
98 BOOST_CHECK_EQUAL(binary.size(), 2);
102 BOOST_CHECK(!binary.empty());
103 BOOST_CHECK_EQUAL(binary.size(), 1);
105 BOOST_CHECK(binary.empty());
106 BOOST_CHECK_EQUAL(binary.size(), 0);
109 BOOST_AUTO_TEST_CASE(Notification)
113 BOOST_CHECK_EQUAL(r.countR0, 0);
114 BOOST_CHECK_EQUAL(r.countR1, 0);
115 BOOST_CHECK_EQUAL(r.valueR1, 0);
116 BOOST_CHECK_EQUAL(r.countR2, 0);
117 BOOST_CHECK_EQUAL(r.value1R2, 0);
118 BOOST_CHECK_EQUAL(r.value2R2, "");
120 STG::Subscriptions<> nullary;
122 auto c1 = nullary.add(r, &Receiver::r0);
123 auto c2 = nullary.add([&r](){ r.r0(); });
127 BOOST_CHECK_EQUAL(r.countR0, 2);
128 BOOST_CHECK_EQUAL(r.countR1, 0);
129 BOOST_CHECK_EQUAL(r.valueR1, 0);
130 BOOST_CHECK_EQUAL(r.countR2, 0);
131 BOOST_CHECK_EQUAL(r.value1R2, 0);
132 BOOST_CHECK_EQUAL(r.value2R2, "");
137 BOOST_CHECK_EQUAL(r.countR0, 3);
138 BOOST_CHECK_EQUAL(r.countR1, 0);
139 BOOST_CHECK_EQUAL(r.valueR1, 0);
140 BOOST_CHECK_EQUAL(r.countR2, 0);
141 BOOST_CHECK_EQUAL(r.value1R2, 0);
142 BOOST_CHECK_EQUAL(r.value2R2, "");
147 BOOST_CHECK_EQUAL(r.countR0, 3);
148 BOOST_CHECK_EQUAL(r.countR1, 0);
149 BOOST_CHECK_EQUAL(r.valueR1, 0);
150 BOOST_CHECK_EQUAL(r.countR2, 0);
151 BOOST_CHECK_EQUAL(r.value1R2, 0);
152 BOOST_CHECK_EQUAL(r.value2R2, "");
154 STG::Subscriptions<uint8_t> unary;
156 auto c1 = unary.add(r, &Receiver::r1);
157 auto c2 = unary.add([&r](const auto& v){ r.r1(v); });
161 BOOST_CHECK_EQUAL(r.countR0, 3);
162 BOOST_CHECK_EQUAL(r.countR1, 2);
163 BOOST_CHECK_EQUAL(r.valueR1, 42);
164 BOOST_CHECK_EQUAL(r.countR2, 0);
165 BOOST_CHECK_EQUAL(r.value1R2, 0);
166 BOOST_CHECK_EQUAL(r.value2R2, "");
171 BOOST_CHECK_EQUAL(r.countR0, 3);
172 BOOST_CHECK_EQUAL(r.countR1, 3);
173 BOOST_CHECK_EQUAL(r.valueR1, 13);
174 BOOST_CHECK_EQUAL(r.countR2, 0);
175 BOOST_CHECK_EQUAL(r.value1R2, 0);
176 BOOST_CHECK_EQUAL(r.value2R2, "");
181 BOOST_CHECK_EQUAL(r.countR0, 3);
182 BOOST_CHECK_EQUAL(r.countR1, 3);
183 BOOST_CHECK_EQUAL(r.valueR1, 13);
184 BOOST_CHECK_EQUAL(r.countR2, 0);
185 BOOST_CHECK_EQUAL(r.value1R2, 0);
186 BOOST_CHECK_EQUAL(r.value2R2, "");
188 STG::Subscriptions<uint8_t, std::string> binary;
190 auto c1 = binary.add(r, &Receiver::r2);
191 auto c2 = binary.add([&r](const auto& v1, const auto& v2){ r.r2(v1, v2); });
193 binary.notify(42, "Douglas");
195 BOOST_CHECK_EQUAL(r.countR0, 3);
196 BOOST_CHECK_EQUAL(r.countR1, 3);
197 BOOST_CHECK_EQUAL(r.valueR1, 13);
198 BOOST_CHECK_EQUAL(r.countR2, 2);
199 BOOST_CHECK_EQUAL(r.value1R2, 42);
200 BOOST_CHECK_EQUAL(r.value2R2, "Douglas");
203 binary.notify(21, "Adams");
205 BOOST_CHECK_EQUAL(r.countR0, 3);
206 BOOST_CHECK_EQUAL(r.countR1, 3);
207 BOOST_CHECK_EQUAL(r.valueR1, 13);
208 BOOST_CHECK_EQUAL(r.countR2, 3);
209 BOOST_CHECK_EQUAL(r.value1R2, 21);
210 BOOST_CHECK_EQUAL(r.value2R2, "Adams");
213 binary.notify(13, "Devil's Dozen");
215 BOOST_CHECK_EQUAL(r.countR0, 3);
216 BOOST_CHECK_EQUAL(r.countR1, 3);
217 BOOST_CHECK_EQUAL(r.valueR1, 13);
218 BOOST_CHECK_EQUAL(r.countR2, 3);
219 BOOST_CHECK_EQUAL(r.value1R2, 21);
220 BOOST_CHECK_EQUAL(r.value2R2, "Adams");
223 BOOST_AUTO_TEST_SUITE_END()