]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/selftest.cc
dwarf2 sub-section test
[thirdparty/binutils-gdb.git] / gdbsupport / selftest.cc
CommitLineData
dcd1f979 1/* GDB self-testing.
3666a048 2 Copyright (C) 2016-2021 Free Software Foundation, Inc.
dcd1f979
TT
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
6d580b63
YQ
19#include "common-defs.h"
20#include "common-exceptions.h"
21#include "common-debug.h"
dcd1f979 22#include "selftest.h"
1526853e 23#include <map>
c45a683f 24#include <functional>
dcd1f979 25
7649770c
YQ
26namespace selftests
27{
1526853e
SM
28/* All the tests that have been registered. Using an std::map allows keeping
29 the order of tests stable and easily looking up whether a test name
30 exists. */
7649770c 31
1526853e 32static std::map<std::string, std::unique_ptr<selftest>> tests;
dcd1f979 33
dcd1f979
TT
34/* See selftest.h. */
35
36void
1526853e 37register_test (const std::string &name, selftest *test)
dcd1f979 38{
1526853e
SM
39 /* Check that no test with this name already exist. */
40 gdb_assert (tests.find (name) == tests.end ());
41
42 tests[name] = std::unique_ptr<selftest> (test);
dcd1f979
TT
43}
44
c45a683f
TV
45/* A selftest that calls the test function without arguments. */
46
47struct lambda_selftest : public selftest
48{
49 lambda_selftest (std::function<void(void)> function_)
50 {
51 function = function_;
52 }
53
54 void operator() () const override
55 {
56 function ();
57 }
58
59 std::function<void(void)> function;
60};
61
dcd1f979
TT
62/* See selftest.h. */
63
64void
c45a683f
TV
65register_test (const std::string &name,
66 std::function<void(void)> function)
dcd1f979 67{
c45a683f 68 register_test (name, new lambda_selftest (function));
1526853e
SM
69}
70
71/* See selftest.h. */
dcd1f979 72
1526853e 73void
ece5bc8a 74run_tests (gdb::array_view<const char *const> filters)
1526853e
SM
75{
76 int ran = 0, failed = 0;
77
78 for (const auto &pair : tests)
dcd1f979 79 {
1526853e
SM
80 const std::string &name = pair.first;
81 const std::unique_ptr<selftest> &test = pair.second;
ece5bc8a 82 bool run = false;
1526853e 83
ece5bc8a
SM
84 if (filters.empty ())
85 run = true;
86 else
87 {
88 for (const char *filter : filters)
89 {
90 if (name.find (filter) != std::string::npos)
91 run = true;
92 }
93 }
94
95 if (!run)
1526853e
SM
96 continue;
97
a70b8144 98 try
dcd1f979 99 {
1526853e
SM
100 debug_printf (_("Running selftest %s.\n"), name.c_str ());
101 ++ran;
102 (*test) ();
dcd1f979 103 }
230d2906 104 catch (const gdb_exception_error &ex)
dcd1f979
TT
105 {
106 ++failed;
3d6e9d23 107 debug_printf ("Self test failed: %s\n", ex.what ());
dcd1f979 108 }
7a3929c4 109
6d580b63 110 reset ();
dcd1f979
TT
111 }
112
1526853e
SM
113 debug_printf (_("Ran %d unit tests, %d failed\n"),
114 ran, failed);
dcd1f979 115}
1526853e
SM
116
117/* See selftest.h. */
118
119void for_each_selftest (for_each_selftest_ftype func)
120{
121 for (const auto &pair : tests)
122 func (pair.first);
123}
124
7649770c 125} // namespace selftests