Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
foo.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 http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef MP_TEST_FOO_H
6#define MP_TEST_FOO_H
7
8#include <cassert>
9#include <cstddef>
10#include <functional>
11#include <map>
12#include <memory>
13#include <memory_resource>
14#include <optional>
15#include <string>
16#include <set>
17#include <unordered_set>
18#include <vector>
19
20namespace mp {
21namespace test {
22
24struct FooHash
25{
26 size_t operator()(const std::string& str) const { return str.size(); }
27};
28
30{
31 std::string name;
32 std::set<int> set_int;
33 std::vector<bool> vector_bool;
34 std::optional<int> optional_int;
35 std::unordered_set<int> unordered_set_int;
36 std::map<std::string, int> map_string_int;
37 // Containers with non-default comparator, allocator, and hash types.
38 std::set<int, std::greater<>> set_int_desc;
39 std::pmr::vector<int> pmr_vector_int;
40 std::unordered_set<std::string, FooHash> unordered_set_string_hash;
41 std::map<std::string, int, std::greater<>> map_string_int_desc;
42};
43
44enum class FooEnum : uint8_t { ONE = 1, TWO = 2, };
45
47{
48 std::string v1;
49 int v2;
50 std::vector<int> v3;
51};
52
54{
55};
56
58{
59 std::string message;
60};
61
63{
64 std::string message;
65};
66
67using FooData = std::vector<char>;
68using FooDataRef = std::shared_ptr<const FooData>;
69
71{
72public:
73 virtual ~FooCallback() = default;
74 virtual int call(int arg) = 0;
75};
76
78{
79public:
80 virtual int callExtended(int arg) = 0;
81};
82
84{
85};
86
89class Bar
90{
91public:
92 virtual ~Bar() = default;
93 virtual int value() = 0;
94};
95
97class SimpleBar : public Bar
98{
99public:
100 explicit SimpleBar(int value) : m_value(value) {}
101 int value() override { return m_value; }
103};
105{
106public:
107 int add(int a, int b) { return a + b; }
108 void addOut(int a, int b, int& out) { out = a + b; }
109 void addInOut(int x, int& sum) { sum += x; }
110 int mapSize(const std::map<std::string, std::string>& map) { return map.size(); }
112 [[noreturn]] void raise(FooStruct foo) { throw foo; }
114 int callback(FooCallback& callback, int arg) { return callback.call(arg); }
115 int callbackUnique(std::unique_ptr<FooCallback> callback, int arg) { return callback->call(arg); }
116 int callbackShared(std::shared_ptr<FooCallback> callback, int arg) { return callback->call(arg); } // NOLINT(performance-unnecessary-value-param)
117 void saveCallback(std::shared_ptr<FooCallback> callback) { m_callback = std::move(callback); }
118 int callbackSaved(int arg) { return m_callback->call(arg); }
119 int callbackExtended(ExtendedCallback& callback, int arg) { return callback.callExtended(arg); }
124 void passMutable(FooMutable& foo) { foo.message += " call"; }
126 double passDouble(double value) { return value; }
127 int passFn(std::function<int()> fn) { return fn(); }
128 std::vector<FooDataRef> passDataPointers(std::vector<FooDataRef> values) { return values; }
129 std::vector<std::unique_ptr<Bar>> listBars(int n)
130 {
131 std::vector<std::unique_ptr<Bar>> result;
132 result.reserve(n);
133 for (int i = 0; i < n; ++i) result.push_back(std::make_unique<SimpleBar>(i));
134 return result;
135 }
136 std::shared_ptr<FooCallback> m_callback;
137 void callFn() { assert(m_fn); m_fn(); }
138 void callFnAsync() { assert(m_fn); m_fn(); }
139 int callIntFnAsync(int arg) { assert(m_int_fn); return m_int_fn(arg); }
141 std::function<void()> m_fn;
142 std::function<int(int)> m_int_fn;
143};
144
145} // namespace test
146} // namespace mp
147
148#endif // MP_TEST_FOO_H
A second, arbitrary interface used to test returning a list of interface objects.
Definition foo.h:90
virtual int value()=0
virtual ~Bar()=default
virtual int callExtended(int arg)=0
virtual int call(int arg)=0
virtual ~FooCallback()=default
void addOut(int a, int b, int &out)
Definition foo.h:108
std::function< int(int)> m_int_fn
Definition foo.h:142
int callbackSaved(int arg)
Definition foo.h:118
void passMutable(FooMutable &foo)
Definition foo.h:124
FooEmpty passEmpty(FooEmpty foo)
Definition foo.h:121
int passFn(std::function< int()> fn)
Definition foo.h:127
std::shared_ptr< FooCallback > m_callback
Definition foo.h:136
std::function< void()> m_fn
Definition foo.h:141
int callbackShared(std::shared_ptr< FooCallback > callback, int arg)
Definition foo.h:116
void addInOut(int x, int &sum)
Definition foo.h:109
FooMessage callMessageAsync()
Definition foo.h:140
void raise(FooStruct foo)
Definition foo.h:112
int callbackUnique(std::unique_ptr< FooCallback > callback, int arg)
Definition foo.h:115
FooEnum passEnum(FooEnum foo)
Definition foo.h:125
int add(int a, int b)
Definition foo.h:107
double passDouble(double value)
Definition foo.h:126
int callIntFnAsync(int arg)
Definition foo.h:139
FooMessage passMessage(FooMessage foo)
Definition foo.h:123
int callback(FooCallback &callback, int arg)
Definition foo.h:114
std::vector< std::unique_ptr< Bar > > listBars(int n)
Definition foo.h:129
FooStruct pass(FooStruct foo)
Definition foo.h:111
std::vector< FooDataRef > passDataPointers(std::vector< FooDataRef > values)
Definition foo.h:128
FooCustom passCustom(FooCustom foo)
Definition foo.h:120
void saveCallback(std::shared_ptr< FooCallback > callback)
Definition foo.h:117
FooData passData(FooData foo)
Definition foo.h:122
int callbackExtended(ExtendedCallback &callback, int arg)
Definition foo.h:119
int mapSize(const std::map< std::string, std::string > &map)
Definition foo.h:110
Concrete Bar that returns a fixed value, used by listBars tests.
Definition foo.h:98
SimpleBar(int value)
Definition foo.h:100
int value() override
Definition foo.h:101
volatile double sum
Definition examples.cpp:10
std::shared_ptr< const FooData > FooDataRef
Definition foo.h:68
FooEnum
Definition foo.h:44
std::vector< char > FooData
Definition foo.h:67
Functions to serialize / deserialize common bitcoin types.
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
std::vector< int > v3
Definition foo.h:50
std::string v1
Definition foo.h:48
Hash function used to test std::unordered_set with a custom hash.
Definition foo.h:25
size_t operator()(const std::string &str) const
Definition foo.h:26
std::string message
Definition foo.h:59
std::string message
Definition foo.h:64
std::set< int, std::greater<> > set_int_desc
Definition foo.h:38
std::unordered_set< std::string, FooHash > unordered_set_string_hash
Definition foo.h:40
std::string name
Definition foo.h:31
std::pmr::vector< int > pmr_vector_int
Definition foo.h:39
std::map< std::string, int > map_string_int
Definition foo.h:36
std::unordered_set< int > unordered_set_int
Definition foo.h:35
std::map< std::string, int, std::greater<> > map_string_int_desc
Definition foo.h:41
std::optional< int > optional_int
Definition foo.h:34
std::vector< bool > vector_bool
Definition foo.h:33
std::set< int > set_int
Definition foo.h:32
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())