]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdbsupport/selftest.h
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdbsupport / selftest.h
... / ...
CommitLineData
1/* GDB self-testing.
2 Copyright (C) 2016-2025 Free Software Foundation, Inc.
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
19#ifndef GDBSUPPORT_SELFTEST_H
20#define GDBSUPPORT_SELFTEST_H
21
22#include "gdbsupport/array-view.h"
23#include "gdbsupport/function-view.h"
24#include "gdbsupport/iterator-range.h"
25#include <set>
26#include <vector>
27
28/* A test is just a function that does some checks and throws an
29 exception if something has gone wrong. */
30
31namespace selftests
32{
33
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
56/* True if selftest should run verbosely. */
57
58extern bool run_verbose ();
59
60/* Register a new self-test. */
61
62extern void register_test (const std::string &name,
63 std::function<void(void)> function);
64
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
75/* Run all the self tests. This print a message describing the number
76 of test and the number of failures.
77
78 If FILTERS is not empty, only run tests with names containing one of the
79 element of FILTERS. */
80
81extern void run_tests (gdb::array_view<const char *const> filters,
82 bool verbose = false);
83
84/* Reset GDB or GDBserver's internal state. */
85extern void reset ();
86}
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
96#endif /* GDBSUPPORT_SELFTEST_H */