Skip to content

Commit 743ac30

Browse files
l0rinchodlinatorstickies-v
committed
Add std::optional support to Boost's equality check
Also moved the operators to the bottom of the file since they're less important and to group them together. Co-authored-by: Hodlinator <[email protected]> Co-authored-by: stickies-v <[email protected]>
1 parent 712a2b5 commit 743ac30

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

src/test/util/setup_common.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ constexpr inline auto TEST_DIR_PATH_ELEMENT{"test_common bitcoin"}; // Includes
7878
/** Random context to get unique temp data dirs. Separate from m_rng, which can be seeded from a const env var */
7979
static FastRandomContext g_rng_temp_path;
8080

81-
std::ostream& operator<<(std::ostream& os, const arith_uint256& num)
82-
{
83-
os << num.ToString();
84-
return os;
85-
}
86-
87-
std::ostream& operator<<(std::ostream& os, const uint160& num)
88-
{
89-
os << num.ToString();
90-
return os;
91-
}
92-
93-
std::ostream& operator<<(std::ostream& os, const uint256& num)
94-
{
95-
os << num.ToString();
96-
return os;
97-
}
98-
9981
struct NetworkSetup
10082
{
10183
NetworkSetup()
@@ -606,3 +588,18 @@ CBlock getBlock13b8a()
606588
stream >> TX_WITH_WITNESS(block);
607589
return block;
608590
}
591+
592+
std::ostream& operator<<(std::ostream& os, const arith_uint256& num)
593+
{
594+
return os << num.ToString();
595+
}
596+
597+
std::ostream& operator<<(std::ostream& os, const uint160& num)
598+
{
599+
return os << num.ToString();
600+
}
601+
602+
std::ostream& operator<<(std::ostream& os, const uint256& num)
603+
{
604+
return os << num.ToString();
605+
}

src/test/util/setup_common.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <key.h>
1111
#include <node/caches.h>
1212
#include <node/context.h> // IWYU pragma: export
13+
#include <optional>
14+
#include <ostream>
1315
#include <primitives/transaction.h>
1416
#include <pubkey.h>
1517
#include <stdexcept>
@@ -29,6 +31,8 @@ class arith_uint256;
2931
class CFeeRate;
3032
class Chainstate;
3133
class FastRandomContext;
34+
class uint160;
35+
class uint256;
3236

3337
/** This is connected to the logger. Can be used to redirect logs to any other log */
3438
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
@@ -39,15 +43,6 @@ extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUM
3943
/** Retrieve the unit test name. */
4044
extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
4145

42-
// Enable BOOST_CHECK_EQUAL for enum class types
43-
namespace std {
44-
template <typename T>
45-
std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
46-
{
47-
return stream << static_cast<typename std::underlying_type<T>::type>(e);
48-
}
49-
} // namespace std
50-
5146
static constexpr CAmount CENT{1000000};
5247

5348
struct TestOpts {
@@ -250,10 +245,26 @@ std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::
250245

251246
CBlock getBlock13b8a();
252247

253-
// Make types usable in BOOST_CHECK_*
248+
// Make types usable in BOOST_CHECK_* @{
249+
namespace std {
250+
template <typename T> requires std::is_enum_v<T>
251+
inline std::ostream& operator<<(std::ostream& os, const T& e)
252+
{
253+
return os << static_cast<std::underlying_type_t<T>>(e);
254+
}
255+
256+
template <typename T>
257+
inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
258+
{
259+
return v ? os << *v
260+
: os << "std::nullopt";
261+
}
262+
} // namespace std
263+
254264
std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
255265
std::ostream& operator<<(std::ostream& os, const uint160& num);
256266
std::ostream& operator<<(std::ostream& os, const uint256& num);
267+
// @}
257268

258269
/**
259270
* BOOST_CHECK_EXCEPTION predicates to check the specific validation error.

0 commit comments

Comments
 (0)