Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_TEST_UTIL_COMMON_H
6 : : #define BITCOIN_TEST_UTIL_COMMON_H
7 : :
8 : : // This file defines concepts and operator<< overloads such that
9 : : // Bitcoin Core types are printable by BOOST_CHECK_*. Include this from
10 : : // *_tests.cpp to get value output on failure; browse this file for
11 : : // the list of supported types.
12 : :
13 : : #include <addresstype.h>
14 : : #include <util/feefrac.h>
15 : :
16 : : #include <chrono>
17 : : #include <optional>
18 : : #include <ostream>
19 : : #include <string>
20 : : #include <utility>
21 : :
22 : : struct AddressPosition;
23 : : class CAddress;
24 : : struct CExtKey;
25 : : struct CExtPubKey;
26 : : class CPubKey;
27 : : class CScript;
28 : : class CService;
29 : : struct KeyOriginInfo;
30 : : class XOnlyPubKey;
31 : :
32 : : /**
33 : : * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
34 : : * Use as
35 : : * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
36 : : */
37 [ + - + - ]: 239 : class HasReason
[ + - - - ]
[ + - + -
+ - + - ]
38 : : {
39 : : public:
40 [ + - ]: 239 : explicit HasReason(std::string_view reason) : m_reason(reason) {}
[ + - + - ]
[ + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - -
- - - - -
- - - - ]
[ + - + -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- ][ + - +
- + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
41 [ - + ]: 239 : bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
42 : 207 : bool operator()(const std::exception& e) const { return (*this)(e.what()); }
43 : :
44 : : private:
45 : : const std::string m_reason;
46 : : };
47 : :
48 : : // Make types usable in BOOST_CHECK_* @{
49 : : namespace std {
50 : : template <typename Clock, typename Duration>
51 : 0 : inline std::ostream& operator<<(std::ostream& os, const std::chrono::time_point<Clock, Duration>& tp)
52 : : {
53 : 0 : return os << tp.time_since_epoch().count();
54 : : }
55 : :
56 : : template <typename T> requires std::is_enum_v<T>
57 : 49 : inline std::ostream& operator<<(std::ostream& os, const T& e)
58 : : {
59 [ - - ]: 49 : return os << static_cast<std::underlying_type_t<T>>(e);
60 : : }
61 : :
62 : : template <typename T>
63 [ # # ]: 0 : inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
64 : : {
65 [ # # ]: 0 : return v ? os << *v
66 [ # # ]: 0 : : os << "std::nullopt";
67 : : }
68 : : } // namespace std
69 : :
70 : : template <typename T>
71 : : concept HasToString = requires(const T& t) { t.ToString(); };
72 : :
73 : : template <HasToString T>
74 : 0 : inline std::ostream& operator<<(std::ostream& os, const T& obj)
75 : : {
76 [ # # ]: 0 : return os << obj.ToString();
77 : : }
78 : :
79 : : std::ostream& operator<<(std::ostream& os, const AddressPosition& pos);
80 : : std::ostream& operator<<(std::ostream& os, const CScript& script);
81 : : std::ostream& operator<<(std::ostream& os, const CService& service);
82 : : std::ostream& operator<<(std::ostream& os, const CAddress& addr);
83 : : std::ostream& operator<<(std::ostream& os, const CExtKey& k);
84 : : std::ostream& operator<<(std::ostream& os, const CExtPubKey& k);
85 : : std::ostream& operator<<(std::ostream& os, const FeeFrac& ff);
86 : : std::ostream& operator<<(std::ostream& os, const CNoDestination& dest);
87 : : std::ostream& operator<<(std::ostream& os, const PubKeyDestination& dest);
88 : : std::ostream& operator<<(std::ostream& os, const PKHash& h);
89 : : std::ostream& operator<<(std::ostream& os, const ScriptHash& h);
90 : : std::ostream& operator<<(std::ostream& os, const WitnessV0KeyHash& h);
91 : : std::ostream& operator<<(std::ostream& os, const WitnessV0ScriptHash& h);
92 : : std::ostream& operator<<(std::ostream& os, const WitnessUnknown& w);
93 : : std::ostream& operator<<(std::ostream& os, const PayToAnchor& p);
94 : : std::ostream& operator<<(std::ostream& os, const WitnessV1Taproot& t);
95 : : std::ostream& operator<<(std::ostream& os, const CTxDestination& dest);
96 : : std::ostream& operator<<(std::ostream& os, const CPubKey& pk);
97 : : std::ostream& operator<<(std::ostream& os, const KeyOriginInfo& info);
98 : : std::ostream& operator<<(std::ostream& os, const ByRatioNegSize<FeeFrac>& b);
99 : : std::ostream& operator<<(std::ostream& os, const XOnlyPubKey& x);
100 : :
101 : : template <typename T, typename U>
102 : : requires requires(std::ostream& os, const T& t, const U& u) { os << t; os << u; }
103 : 0 : inline std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p)
104 : : {
105 : 0 : return os << "std::pair(" << p.first << ", " << p.second << ")";
106 : : }
107 : :
108 : : // @}
109 : :
110 : : #endif // BITCOIN_TEST_UTIL_COMMON_H
|