xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
beast
unit_test
reporter.h
1
// Distributed under the Boost Software License, Version 1.0. (See accompanying
2
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3
//
4
5
#pragma once
6
7
#include <xrpl/beast/unit_test/amount.h>
8
#include <xrpl/beast/unit_test/runner.h>
9
#include <xrpl/beast/unit_test/suite_info.h>
10
11
#include <boost/optional.hpp>
12
13
#include <
algorithm
>
14
#include <
chrono
>
15
#include <
cstddef
>
16
#include <
iomanip
>
17
#include <
iostream
>
18
#include <
sstream
>
19
#include <
string
>
20
#include <
utility
>
21
#include <
vector
>
22
23
namespace
beast::unit_test
{
24
25
namespace
detail
{
26
31
template
<
class
=
void
>
32
class
Reporter
:
public
Runner
33
{
34
private
:
35
using
ClockType
=
std::chrono::steady_clock
;
36
37
struct
CaseResults
38
{
39
std::string
name
;
40
std::size_t
total
= 0;
41
std::size_t
failed
= 0;
42
43
explicit
CaseResults
(
std::string
name
=
""
) :
name
(
std
::move(
name
))
44
{
45
}
46
};
47
48
struct
SuiteResults
49
{
50
std::string
name
;
51
std::size_t
cases
= 0;
52
std::size_t
total
= 0;
53
std::size_t
failed
= 0;
54
ClockType::time_point
start
=
ClockType::now
();
55
56
explicit
SuiteResults
(
std::string
name
=
""
) :
name
(
std
::move(
name
))
57
{
58
}
59
60
void
61
add
(
CaseResults
const
& r);
62
};
63
64
struct
Results
65
{
66
using
RunTime
=
std::pair<std::string, ClockType::duration>
;
67
68
static
constexpr
auto
kMaxTop
= 10;
69
70
std::size_t
suites
= 0;
71
std::size_t
cases
= 0;
72
std::size_t
total
= 0;
73
std::size_t
failed
= 0;
74
std::vector<RunTime>
top
;
75
ClockType::time_point
start
=
ClockType::now
();
76
77
void
78
add
(
SuiteResults
const
& r);
79
};
80
81
std::ostream
&
os_
;
82
Results
results_
;
83
SuiteResults
suiteResults_
;
84
CaseResults
caseResults_
;
85
86
public
:
87
Reporter
(
Reporter
const
&) =
delete
;
88
Reporter
&
89
operator=
(
Reporter
const
&) =
delete
;
90
91
~Reporter
()
override
;
92
93
explicit
Reporter
(
std::ostream
& os =
std::cout
);
94
95
private
:
96
static
std::string
97
fmtdur
(ClockType::duration
const
& d);
98
99
void
100
onSuiteBegin
(
SuiteInfo
const
& info)
override
;
101
102
void
103
onSuiteEnd
()
override
;
104
105
void
106
onCaseBegin
(
std::string
const
& name)
override
;
107
108
void
109
onCaseEnd
()
override
;
110
111
void
112
onPass
()
override
;
113
114
void
115
onFail
(
std::string
const
& reason)
override
;
116
117
void
118
onLog
(
std::string
const
& s)
override
;
119
};
120
121
//------------------------------------------------------------------------------
122
123
template
<
class
Unused>
124
void
125
Reporter<Unused>::SuiteResults::add
(
CaseResults
const
& r)
126
{
127
++
cases
;
128
total
+= r.
total
;
129
failed
+= r.
failed
;
130
}
131
132
template
<
class
Unused>
133
void
134
Reporter<Unused>::Results::add
(
SuiteResults
const
& r)
135
{
136
++
suites
;
137
total
+= r.
total
;
138
cases
+= r.
cases
;
139
failed
+= r.
failed
;
140
auto
const
elapsed =
ClockType::now
() - r.
start
;
141
if
(elapsed >=
std::chrono::seconds
{1})
142
{
143
auto
const
iter =
std::lower_bound
(
144
top
.begin(),
top
.end(), elapsed, [](
RunTime
const
& t1, ClockType::duration
const
& t2) {
145
return t1.second > t2;
146
});
147
if
(iter !=
top
.end())
148
{
149
if
(
top
.size() ==
kMaxTop
)
150
top
.resize(
top
.size() - 1);
151
top
.emplace(iter, r.
name
, elapsed);
152
}
153
else
if
(
top
.size() <
kMaxTop
)
154
{
155
top
.emplace_back(r.
name
, elapsed);
156
}
157
}
158
}
159
160
//------------------------------------------------------------------------------
161
162
template
<
class
Unused>
163
Reporter<Unused>::Reporter
(
std::ostream
& os) :
os_
(os)
164
{
165
}
166
167
template
<
class
Unused>
168
Reporter<Unused>::~Reporter
()
169
{
170
if
(
results_
.top.size() > 0)
171
{
172
os_
<<
"Longest suite times:\n"
;
173
for
(
auto
const
& i :
results_
.top)
174
os_
<<
std::setw
(8) <<
fmtdur
(i.second) <<
" "
<< i.first <<
'\n'
;
175
}
176
auto
const
elapsed =
ClockType::now
() -
results_
.start;
177
os_
<<
fmtdur
(elapsed) <<
", "
<<
Amount
{
results_
.suites,
"suite"
} <<
", "
178
<<
Amount
{
results_
.cases,
"case"
} <<
", "
<<
Amount
{
results_
.total,
"test"
} <<
" total, "
179
<<
Amount
{
results_
.failed,
"failure"
} <<
std::endl
;
180
}
181
182
template
<
class
Unused>
183
std::string
184
Reporter<Unused>::fmtdur
(ClockType::duration
const
& d)
185
{
186
using namespace
std::chrono
;
187
auto
const
ms =
duration_cast<milliseconds>
(d);
188
if
(ms <
seconds
{1})
189
return
std::to_string
(ms.count()) +
"ms"
;
190
std::stringstream
ss;
191
ss <<
std::fixed
<<
std::setprecision
(1) << (ms.count() / 1000.) <<
"s"
;
192
return
ss.
str
();
193
}
194
195
template
<
class
Unused>
196
void
197
Reporter<Unused>::onSuiteBegin
(
SuiteInfo
const
& info)
198
{
199
suiteResults_
=
SuiteResults
{info.
fullName
()};
200
}
201
202
template
<
class
Unused>
203
void
204
Reporter<Unused>::onSuiteEnd
()
205
{
206
results_
.add(
suiteResults_
);
207
}
208
209
template
<
class
Unused>
210
void
211
Reporter<Unused>::onCaseBegin
(
std::string
const
& name)
212
{
213
caseResults_
=
CaseResults
(name);
214
os_
<<
suiteResults_
.name << (
caseResults_
.name.empty() ?
""
: (
" "
+
caseResults_
.name))
215
<<
std::endl
;
216
}
217
218
template
<
class
Unused>
219
void
220
Reporter<Unused>::onCaseEnd
()
221
{
222
suiteResults_
.add(
caseResults_
);
223
}
224
225
template
<
class
Unused>
226
void
227
Reporter<Unused>::onPass
()
228
{
229
++
caseResults_
.total;
230
}
231
232
template
<
class
Unused>
233
void
234
Reporter<Unused>::onFail
(
std::string
const
& reason)
235
{
236
++
caseResults_
.failed;
237
++
caseResults_
.total;
238
os_
<<
"#"
<<
caseResults_
.total <<
" failed"
<< (reason.
empty
() ?
""
:
": "
) << reason
239
<<
std::endl
;
240
}
241
242
template
<
class
Unused>
243
void
244
Reporter<Unused>::onLog
(
std::string
const
& s)
245
{
246
os_
<< s;
247
}
248
249
}
// namespace detail
250
251
using
Reporter
=
detail::Reporter<>
;
252
253
// TODO [#8340]: Legacy name still used by external consumers of libxrpl (e.g. validator-keys-tool).
254
// Do not use in new code; remove once those consumers have migrated.
255
using
reporter
=
Reporter
;
// NOLINT(readability-identifier-naming)
256
257
}
// namespace beast::unit_test
algorithm
std::ostream
std::string
std::stringstream
chrono
beast::unit_test::Amount
Utility for producing nicely composed output of amounts with units.
Definition
include/xrpl/beast/unit_test/amount.h:17
beast::unit_test::Runner::Runner
Runner()=default
beast::unit_test::SuiteInfo
Associates a unit test type with metadata.
Definition
suite_info.h:20
beast::unit_test::SuiteInfo::fullName
std::string fullName() const
Return the canonical suite name as a string.
Definition
suite_info.h:78
beast::unit_test::detail::Reporter
A simple test runner that writes everything to a stream in real time.
Definition
reporter.h:33
beast::unit_test::detail::Reporter::fmtdur
static std::string fmtdur(ClockType::duration const &d)
Definition
reporter.h:184
beast::unit_test::detail::Reporter::onSuiteBegin
void onSuiteBegin(SuiteInfo const &info) override
Called when a new suite starts.
Definition
reporter.h:197
beast::unit_test::detail::Reporter::onFail
void onFail(std::string const &reason) override
Called for each failing condition.
Definition
reporter.h:234
beast::unit_test::detail::Reporter::Reporter
Reporter(Reporter const &)=delete
beast::unit_test::detail::Reporter::ClockType
std::chrono::steady_clock ClockType
Definition
reporter.h:35
beast::unit_test::detail::Reporter::results_
Results results_
Definition
reporter.h:82
beast::unit_test::detail::Reporter::onCaseEnd
void onCaseEnd() override
Called when a new case ends.
Definition
reporter.h:220
beast::unit_test::detail::Reporter::suiteResults_
SuiteResults suiteResults_
Definition
reporter.h:83
beast::unit_test::detail::Reporter::caseResults_
CaseResults caseResults_
Definition
reporter.h:84
beast::unit_test::detail::Reporter::onSuiteEnd
void onSuiteEnd() override
Called when a suite ends.
Definition
reporter.h:204
beast::unit_test::detail::Reporter::~Reporter
~Reporter() override
Definition
reporter.h:168
beast::unit_test::detail::Reporter::onPass
void onPass() override
Called for each passing condition.
Definition
reporter.h:227
beast::unit_test::detail::Reporter::os_
std::ostream & os_
Definition
reporter.h:81
beast::unit_test::detail::Reporter::operator=
Reporter & operator=(Reporter const &)=delete
beast::unit_test::detail::Reporter::onCaseBegin
void onCaseBegin(std::string const &name) override
Called when a new case starts.
Definition
reporter.h:211
beast::unit_test::detail::Reporter::onLog
void onLog(std::string const &s) override
Called when a test logs output.
Definition
reporter.h:244
std::cout
cstddef
std::chrono::duration_cast
T duration_cast(T... args)
std::chrono::seconds
std::string::empty
T empty(T... args)
std::endl
T endl(T... args)
std::fixed
T fixed(T... args)
iomanip
iostream
std::lower_bound
T lower_bound(T... args)
beast::unit_test::detail
Definition
const_container.h:7
beast::unit_test
Definition
include/xrpl/beast/unit_test/amount.h:11
beast::unit_test::reporter
Reporter reporter
Definition
reporter.h:255
beast::unit_test::Reporter
detail::Reporter<> Reporter
Definition
reporter.h:251
std::chrono
std
STL namespace.
std::chrono::steady_clock::now
T now(T... args)
std::pair
std::setprecision
T setprecision(T... args)
std::setw
T setw(T... args)
std::size_t
sstream
std::chrono::steady_clock
std::stringstream::str
T str(T... args)
string
beast::unit_test::detail::Reporter::CaseResults
Definition
reporter.h:38
beast::unit_test::detail::Reporter::CaseResults::failed
std::size_t failed
Definition
reporter.h:41
beast::unit_test::detail::Reporter::CaseResults::total
std::size_t total
Definition
reporter.h:40
beast::unit_test::detail::Reporter::CaseResults::CaseResults
CaseResults(std::string name="")
Definition
reporter.h:43
beast::unit_test::detail::Reporter::CaseResults::name
std::string name
Definition
reporter.h:39
beast::unit_test::detail::Reporter::Results
Definition
reporter.h:65
beast::unit_test::detail::Reporter::Results::RunTime
std::pair< std::string, ClockType::duration > RunTime
Definition
reporter.h:66
beast::unit_test::detail::Reporter::Results::total
std::size_t total
Definition
reporter.h:72
beast::unit_test::detail::Reporter::Results::start
ClockType::time_point start
Definition
reporter.h:75
beast::unit_test::detail::Reporter::Results::suites
std::size_t suites
Definition
reporter.h:70
beast::unit_test::detail::Reporter::Results::failed
std::size_t failed
Definition
reporter.h:73
beast::unit_test::detail::Reporter::Results::add
void add(SuiteResults const &r)
Definition
reporter.h:134
beast::unit_test::detail::Reporter::Results::cases
std::size_t cases
Definition
reporter.h:71
beast::unit_test::detail::Reporter::Results::top
std::vector< RunTime > top
Definition
reporter.h:74
beast::unit_test::detail::Reporter::Results::kMaxTop
static constexpr auto kMaxTop
Definition
reporter.h:68
beast::unit_test::detail::Reporter::SuiteResults
Definition
reporter.h:49
beast::unit_test::detail::Reporter::SuiteResults::name
std::string name
Definition
reporter.h:50
beast::unit_test::detail::Reporter::SuiteResults::start
ClockType::time_point start
Definition
reporter.h:54
beast::unit_test::detail::Reporter::SuiteResults::total
std::size_t total
Definition
reporter.h:52
beast::unit_test::detail::Reporter::SuiteResults::cases
std::size_t cases
Definition
reporter.h:51
beast::unit_test::detail::Reporter::SuiteResults::SuiteResults
SuiteResults(std::string name="")
Definition
reporter.h:56
beast::unit_test::detail::Reporter::SuiteResults::add
void add(CaseResults const &r)
Definition
reporter.h:125
beast::unit_test::detail::Reporter::SuiteResults::failed
std::size_t failed
Definition
reporter.h:53
std::to_string
T to_string(T... args)
utility
vector
Generated by
1.17.0