9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
17 #include <type_traits>
22 #if defined(_ACTS_ANY_ENABLE_TRACK_ALLOCATIONS)
23 #define CHECK_ANY_ALLOCATIONS() \
25 _AnyAllocationReporter::checkAllocations(); \
28 #define CHECK_ANY_ALLOCATIONS() \
33 BOOST_AUTO_TEST_SUITE(AnyTests)
45 BOOST_CHECK_EQUAL(a.as<
int>(), v);
46 BOOST_CHECK_NE(a.as<
int>(), v + 1);
48 BOOST_CHECK_THROW(a.as<
float>(), std::bad_any_cast);
49 BOOST_CHECK_THROW(a =
Any{0.5f}, std::bad_any_cast);
58 std::array<int, 2>
v{1, 2};
62 BOOST_CHECK_EQUAL_COLLECTIONS(a.as<decltype(
v)>().
begin(),
63 a.as<decltype(
v)>().
end(),
v.begin(),
65 BOOST_CHECK_THROW(a.as<
float>(), std::bad_any_cast);
66 BOOST_CHECK_THROW(a =
Any{0.5f}, std::bad_any_cast);
75 std::array<unsigned long, 5>
v{1, 2, 3, 4, 5};
79 BOOST_CHECK_EQUAL_COLLECTIONS(a.as<decltype(
v)>().
begin(),
80 a.as<decltype(
v)>().
end(),
v.begin(),
82 BOOST_CHECK_THROW(a.as<
float>(), std::bad_any_cast);
83 BOOST_CHECK_THROW(a =
Any{0.5f}, std::bad_any_cast);
116 BOOST_CHECK_EQUAL(a.as<
A>().value, 76);
127 Any a{std::in_place_type<A>, 42};
129 BOOST_CHECK_EQUAL(
a.as<
A>().value, 42);
146 BOOST_CHECK_EQUAL(b.as<
int>(), 5);
151 BOOST_CHECK_EQUAL(c.as<
int>(), 5);
169 BOOST_CHECK_EQUAL(b.as<
int>(), 5);
174 BOOST_CHECK_EQUAL(c.as<
int>(), 5);
181 D(
bool* d) : destroyed{d} {}
182 ~
D() { *destroyed =
true; }
186 bool* destroyed{
nullptr};
187 std::array<char, 512> blob{};
189 D2(
bool* d) : destroyed{d} {}
191 ~
D2() { *destroyed =
true; }
196 bool destroyed =
false;
198 BOOST_CHECK(!destroyed);
201 BOOST_CHECK(!destroyed);
203 BOOST_CHECK(destroyed);
208 bool destroyed =
false;
210 BOOST_CHECK(!destroyed);
213 BOOST_CHECK(!destroyed);
215 BOOST_CHECK(destroyed);
222 bool destroyed =
false;
227 Any a{std::in_place_type<D>, &destroyed};
228 BOOST_CHECK(!destroyed);
230 BOOST_CHECK(!destroyed);
232 BOOST_CHECK(destroyed);
234 BOOST_CHECK(!destroyed);
236 BOOST_CHECK(destroyed);
241 bool destroyed =
false;
246 Any a{std::in_place_type<D2>, &destroyed};
247 BOOST_CHECK(!destroyed);
249 BOOST_CHECK(!destroyed);
251 BOOST_CHECK(destroyed);
253 BOOST_CHECK(!destroyed);
255 BOOST_CHECK(destroyed);
262 bool destroyed =
false;
263 BOOST_CHECK(!destroyed);
265 Any a{std::in_place_type<D>, &destroyed};
266 BOOST_CHECK(!destroyed);
268 BOOST_CHECK(destroyed);
273 bool destroyed =
false;
274 BOOST_CHECK(!destroyed);
276 Any a{std::in_place_type<D2>, &destroyed};
277 BOOST_CHECK(!destroyed);
279 BOOST_CHECK(destroyed);
285 size_t* destroyed{
nullptr};
286 std::array<char, 512> blob{};
288 D3(
size_t* d) : destroyed{d} {}
290 ~
D3() { (*destroyed)++; }
294 size_t destroyed = 0;
295 for (
size_t i = 0;
i < 10000;
i++) {
297 BOOST_CHECK_EQUAL(destroyed,
i);
299 BOOST_CHECK_EQUAL(destroyed,
i);
300 a =
Any{std::in_place_type<D3>, &destroyed};
301 BOOST_CHECK_EQUAL(destroyed,
i);
303 BOOST_CHECK_EQUAL(destroyed,
i + 1);
310 size_t nCopyConstruct = 0;
312 size_t nMoveConstruct = 0;
316 template <
size_t PADDING>
350 template <
size_t PADDING>
352 std::array<char, PADDING> m_padding{};
357 template <
size_t PADDING>
365 #define checkCounters() \
367 BOOST_REQUIRE_EQUAL(l.counters.nCopy, counters.nCopy); \
368 BOOST_REQUIRE_EQUAL(l.counters.nCopyConstruct, counters.nCopyConstruct); \
369 BOOST_REQUIRE_EQUAL(l.counters.nMove, counters.nMove); \
370 BOOST_REQUIRE_EQUAL(l.counters.nMoveConstruct, counters.nMoveConstruct); \
371 BOOST_REQUIRE_EQUAL(l.counters.nDestroy, counters.nDestroy); \
374 #define makeCounter(counter, n) \
380 #define incCopyConstruct(n) makeCounter(counters.nCopyConstruct, n)
381 #define incCopy(n) makeCounter(counters.nCopy, n)
382 #define incMoveConstruct(n) makeCounter(counters.nMoveConstruct, n)
383 #define incMove(n) makeCounter(counters.nMove, n)
384 #define incDestroy(n) makeCounter(counters.nDestroy, n)
393 const auto& o = l.
inner;
448 const auto& o = l.
inner;
498 BOOST_AUTO_TEST_SUITE_END()