2 Copyright (C) 2016-2024 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 #ifndef COMMON_SELFTEST_H
20 #define COMMON_SELFTEST_H
22 #include "gdbsupport/array-view.h"
23 #include "gdbsupport/function-view.h"
24 #include "gdbsupport/iterator-range.h"
28 /* A test is just a function that does some checks and throws an
29 exception if something has gone wrong. */
34 /* Selftests are registered under a unique name. */
38 selftest (std::string name
, std::function
<void (void)> test
)
39 : name
{ std::move (name
) }, test
{ std::move (test
) }
41 bool operator< (const selftest
&rhs
) const
42 { return name
< rhs
.name
; }
45 std::function
<void (void)> test
;
48 /* Type of the container of all the registered selftests. */
49 using selftests_registry
= std::set
<selftest
>;
50 using selftests_range
= iterator_range
<selftests_registry::const_iterator
>;
52 /* Create a range to iterate over all registered tests. */
54 selftests_range
all_selftests ();
56 /* True if selftest should run verbosely. */
58 extern bool run_verbose ();
60 /* Register a new self-test. */
62 extern void register_test (const std::string
&name
,
63 std::function
<void(void)> function
);
65 /* A selftest generator is a callback function used to delay the generation
68 using selftests_generator
= std::function
<std::vector
<selftest
> (void)>;
70 /* Register a function which can lazily register selftests once GDB is fully
73 extern void add_lazy_generator (selftests_generator generator
);
75 /* Run all the self tests. This print a message describing the number
76 of test and the number of failures.
78 If FILTERS is not empty, only run tests with names containing one of the
79 element of FILTERS. */
81 extern void run_tests (gdb::array_view
<const char *const> filters
,
82 bool verbose
= false);
84 /* Reset GDB or GDBserver's internal state. */
88 /* Check that VALUE is true, and, if not, throw an exception. */
90 #define SELF_CHECK(VALUE) \
93 error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \
96 #endif /* COMMON_SELFTEST_H */