]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/selftest.cc
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdbsupport / selftest.cc
CommitLineData
dcd1f979 1/* GDB self-testing.
1d506c26 2 Copyright (C) 2016-2024 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"
c45a683f 23#include <functional>
dcd1f979 24
7649770c
YQ
25namespace selftests
26{
c57207c1 27/* All the tests that have been registered. Using an std::set allows keeping
1526853e
SM
28 the order of tests stable and easily looking up whether a test name
29 exists. */
7649770c 30
c57207c1 31static selftests_registry tests;
dcd1f979 32
9a0f7f63
LS
33/* Set of callback functions used to register selftests after GDB is fully
34 initialized. */
35
36static std::vector<selftests_generator> lazy_generators;
37
dcd1f979
TT
38/* See selftest.h. */
39
40void
d7c68312
TT
41register_test (const std::string &name,
42 std::function<void(void)> function)
dcd1f979 43{
1526853e 44 /* Check that no test with this name already exist. */
c57207c1
LS
45 auto status = tests.emplace (name, std::move (function));
46 if (!status.second)
47 gdb_assert_not_reached ("Test already registered");
1526853e
SM
48}
49
50/* See selftest.h. */
dcd1f979 51
9a0f7f63
LS
52void
53add_lazy_generator (selftests_generator generator)
54{
55 lazy_generators.push_back (std::move (generator));
56}
57
58/* See selftest.h. */
59
479209dd
TV
60static bool run_verbose_ = false;
61
62/* See selftest.h. */
63
64bool
65run_verbose ()
66{
67 return run_verbose_;
68}
69
70/* See selftest.h. */
71
1526853e 72void
479209dd 73run_tests (gdb::array_view<const char *const> filters, bool verbose)
1526853e 74{
dbbfabb4 75 int ran = 0;
479209dd 76 run_verbose_ = verbose;
dbbfabb4 77 std::vector<const char *> failed;
1526853e 78
c57207c1 79 for (const auto &test : all_selftests ())
dcd1f979 80 {
ece5bc8a 81 bool run = false;
1526853e 82
ece5bc8a
SM
83 if (filters.empty ())
84 run = true;
85 else
86 {
87 for (const char *filter : filters)
88 {
c57207c1 89 if (test.name.find (filter) != std::string::npos)
ece5bc8a
SM
90 run = true;
91 }
92 }
93
94 if (!run)
1526853e
SM
95 continue;
96
a70b8144 97 try
dcd1f979 98 {
c57207c1 99 debug_printf (_("Running selftest %s.\n"), test.name.c_str ());
1526853e 100 ++ran;
c57207c1 101 test.test ();
dcd1f979 102 }
230d2906 103 catch (const gdb_exception_error &ex)
dcd1f979 104 {
3d6e9d23 105 debug_printf ("Self test failed: %s\n", ex.what ());
dbbfabb4 106 failed.push_back (test.name.c_str ());
dcd1f979 107 }
7a3929c4 108
6d580b63 109 reset ();
dcd1f979
TT
110 }
111
dbbfabb4
SM
112 if (!failed.empty ())
113 {
114 debug_printf ("\nFailures:\n");
115
116 for (const char *name : failed)
117 debug_printf (" %s\n", name);
118
119 debug_printf ("\n");
120 }
121
122 debug_printf (_("Ran %d unit tests, %zu failed\n"),
123 ran, failed.size ());
dcd1f979 124}
1526853e
SM
125
126/* See selftest.h. */
127
c57207c1
LS
128selftests_range
129all_selftests ()
1526853e 130{
9a0f7f63
LS
131 /* Execute any function which might still want to register tests. Once each
132 function has been executed, clear lazy_generators to ensure that
133 callback functions are only executed once. */
134 for (const auto &generator : lazy_generators)
135 for (selftest &test : generator ())
136 register_test (std::move (test.name), std::move (test.test));
137 lazy_generators.clear ();
138
c57207c1 139 return selftests_range (tests.cbegin (), tests.cend ());
1526853e
SM
140}
141
7649770c 142} // namespace selftests