]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/selftest.h
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdbsupport / selftest.h
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
1a5c2598
TT
19#ifndef COMMON_SELFTEST_H
20#define COMMON_SELFTEST_H
dcd1f979 21
ece5bc8a 22#include "gdbsupport/array-view.h"
c0492bea 23#include "gdbsupport/function-view.h"
c57207c1
LS
24#include "gdbsupport/iterator-range.h"
25#include <set>
9a0f7f63 26#include <vector>
ece5bc8a 27
dcd1f979
TT
28/* A test is just a function that does some checks and throws an
29 exception if something has gone wrong. */
30
7649770c
YQ
31namespace selftests
32{
33
c57207c1
LS
34/* Selftests are registered under a unique name. */
35
36struct selftest
37{
38 selftest (std::string name, std::function<void (void)> test)
39 : name { std::move (name) }, test { std::move (test) }
40 { }
41 bool operator< (const selftest &rhs) const
42 { return name < rhs.name; }
43
44 std::string name;
45 std::function<void (void)> test;
46};
47
48/* Type of the container of all the registered selftests. */
49using selftests_registry = std::set<selftest>;
50using selftests_range = iterator_range<selftests_registry::const_iterator>;
51
52/* Create a range to iterate over all registered tests. */
53
54selftests_range all_selftests ();
55
479209dd
TV
56/* True if selftest should run verbosely. */
57
58extern bool run_verbose ();
59
dcd1f979
TT
60/* Register a new self-test. */
61
1526853e 62extern void register_test (const std::string &name,
c45a683f 63 std::function<void(void)> function);
dcd1f979 64
9a0f7f63
LS
65/* A selftest generator is a callback function used to delay the generation
66 of selftests. */
67
68using selftests_generator = std::function<std::vector<selftest> (void)>;
69
70/* Register a function which can lazily register selftests once GDB is fully
71 initialized. */
72
73extern void add_lazy_generator (selftests_generator generator);
74
dcd1f979 75/* Run all the self tests. This print a message describing the number
1526853e
SM
76 of test and the number of failures.
77
ece5bc8a
SM
78 If FILTERS is not empty, only run tests with names containing one of the
79 element of FILTERS. */
dcd1f979 80
479209dd
TV
81extern void run_tests (gdb::array_view<const char *const> filters,
82 bool verbose = false);
7649770c 83
6d580b63
YQ
84/* Reset GDB or GDBserver's internal state. */
85extern void reset ();
7649770c 86}
dcd1f979
TT
87
88/* Check that VALUE is true, and, if not, throw an exception. */
89
90#define SELF_CHECK(VALUE) \
91 do { \
92 if (!(VALUE)) \
93 error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \
94 } while (0)
95
1a5c2598 96#endif /* COMMON_SELFTEST_H */