Skip to content

Commit 423c6e3

Browse files
committed
Add and test printing of smart pointers
1 parent de15de1 commit 423c6e3

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

libs/common/include/s25util/boostTestHelpers.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66
#pragma once
77

88
#include <boost/version.hpp>
9+
#include <iosfwd>
10+
#include <memory>
911

1012
#if BOOST_VERSION < 107000
1113
# define BOOST_TEST_INFO_SCOPE(context_descr) \
1214
::boost::test_tools::tt_detail::context_frame BOOST_JOIN(context_frame_, __LINE__) = \
1315
::boost::test_tools::tt_detail::context_frame(BOOST_TEST_LAZY_MSG(context_descr))
1416
#endif
17+
18+
namespace std {
19+
template<class T, class D>
20+
std::ostream& boost_test_print_type(std::ostream& os, const std::unique_ptr<T, D>& p)
21+
{
22+
return os << p.get();
23+
}
24+
template<class T>
25+
std::ostream& boost_test_print_type(std::ostream& os, const std::shared_ptr<T>& p)
26+
{
27+
return os << p.get();
28+
}
29+
} // namespace std

tests/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#define BOOST_TEST_MODULE s25util_Test
6+
#include "s25util/boostTestHelpers.h"
67
#include <boost/test/unit_test.hpp>
8+
#include <memory>
9+
#include <sstream>
710

811
//#include <vld.h>
12+
13+
using TestedTypes = std::tuple<std::unique_ptr<int>, std::shared_ptr<int>>;
14+
BOOST_AUTO_TEST_CASE_TEMPLATE(PrintSmartPtr, T, TestedTypes)
15+
{
16+
std::stringstream sOrig, sBoost;
17+
T p;
18+
sOrig << p.get();
19+
boost_test_print_type(sBoost, p);
20+
21+
p.reset(new int(42));
22+
sOrig << p.get();
23+
boost_test_print_type(sBoost, p);
24+
25+
BOOST_TEST(sBoost.str() == sOrig.str());
26+
}

0 commit comments

Comments
 (0)