2 Copyright (C) 2016-2022 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"
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
32 static std::map
<std::string
, std::function
<void(void)>> tests
;
37 register_test (const std::string
&name
,
38 std::function
<void(void)> function
)
40 /* Check that no test with this name already exist. */
41 gdb_assert (tests
.find (name
) == tests
.end ());
43 tests
[name
] = function
;
48 static bool run_verbose_
= false;
61 run_tests (gdb::array_view
<const char *const> filters
, bool verbose
)
63 int ran
= 0, failed
= 0;
64 run_verbose_
= verbose
;
66 for (const auto &pair
: tests
)
68 const std::string
&name
= pair
.first
;
69 const auto &test
= pair
.second
;
76 for (const char *filter
: filters
)
78 if (name
.find (filter
) != std::string::npos
)
88 debug_printf (_("Running selftest %s.\n"), name
.c_str ());
92 catch (const gdb_exception_error
&ex
)
95 debug_printf ("Self test failed: %s\n", ex
.what ());
101 debug_printf (_("Ran %d unit tests, %d failed\n"),
105 /* See selftest.h. */
107 void for_each_selftest (for_each_selftest_ftype func
)
109 for (const auto &pair
: tests
)
113 } // namespace selftests