2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #include "common-defs.h"
20 #include "common-exceptions.h"
21 #include "common-debug.h"
27 /* All the tests that have been registered. Using an std::set allows keeping
28 the order of tests stable and easily looking up whether a test name
31 static selftests_registry tests
;
33 /* Set of callback functions used to register selftests after GDB is fully
36 static std::vector
<selftests_generator
> lazy_generators
;
41 register_test (const std::string
&name
,
42 std::function
<void(void)> function
)
44 /* Check that no test with this name already exist. */
45 auto status
= tests
.emplace (name
, std::move (function
));
47 gdb_assert_not_reached ("Test already registered");
53 add_lazy_generator (selftests_generator generator
)
55 lazy_generators
.push_back (std::move (generator
));
60 static bool run_verbose_
= false;
73 run_tests (gdb::array_view
<const char *const> filters
, bool verbose
)
76 run_verbose_
= verbose
;
77 std::vector
<const char *> failed
;
79 for (const auto &test
: all_selftests ())
87 for (const char *filter
: filters
)
89 if (test
.name
.find (filter
) != std::string::npos
)
99 debug_printf (_("Running selftest %s.\n"), test
.name
.c_str ());
103 catch (const gdb_exception_error
&ex
)
105 debug_printf ("Self test failed: %s\n", ex
.what ());
106 failed
.push_back (test
.name
.c_str ());
112 if (!failed
.empty ())
114 debug_printf ("\nFailures:\n");
116 for (const char *name
: failed
)
117 debug_printf (" %s\n", name
);
122 debug_printf (_("Ran %d unit tests, %zu failed\n"),
123 ran
, failed
.size ());
126 /* See selftest.h. */
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 ();
139 return selftests_range (tests
.cbegin (), tests
.cend ());
142 } // namespace selftests