Bitcoin Core 31.99.0
P2P Digital Currency
common.h
Go to the documentation of this file.
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#include <chrono>
9#include <optional>
10#include <ostream>
11#include <string>
12
19{
20public:
21 explicit HasReason(std::string_view reason) : m_reason(reason) {}
22 bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
23 bool operator()(const std::exception& e) const { return (*this)(e.what()); }
24
25private:
26 const std::string m_reason;
27};
28
29// Make types usable in BOOST_CHECK_* @{
30namespace std {
31template <typename Clock, typename Duration>
32inline std::ostream& operator<<(std::ostream& os, const std::chrono::time_point<Clock, Duration>& tp)
33{
34 return os << tp.time_since_epoch().count();
35}
36
37template <typename T> requires std::is_enum_v<T>
38inline std::ostream& operator<<(std::ostream& os, const T& e)
39{
40 return os << static_cast<std::underlying_type_t<T>>(e);
41}
42
43template <typename T>
44inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
45{
46 return v ? os << *v
47 : os << "std::nullopt";
48}
49} // namespace std
50
51template <typename T>
52concept HasToString = requires(const T& t) { t.ToString(); };
53
54template <HasToString T>
55inline std::ostream& operator<<(std::ostream& os, const T& obj)
56{
57 return os << obj.ToString();
58}
59
60// @}
61
62#endif // BITCOIN_TEST_UTIL_COMMON_H
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:19
bool operator()(std::string_view s) const
Definition: common.h:22
const std::string m_reason
Definition: common.h:26
bool operator()(const std::exception &e) const
Definition: common.h:23
HasReason(std::string_view reason)
Definition: common.h:21
#define T(expected, seed, data)
Definition: common.h:30
std::ostream & operator<<(std::ostream &os, const std::chrono::time_point< Clock, Duration > &tp)
Definition: common.h:32
std::ostream & operator<<(std::ostream &os, const T &obj)
Definition: common.h:55