]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbsupport/selftest.h
gdb: allow specifying multiple filters when running selftests
[thirdparty/binutils-gdb.git] / gdbsupport / selftest.h
1 /* GDB self-testing.
2 Copyright (C) 2016-2020 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 COMMON_SELFTEST_H
20 #define COMMON_SELFTEST_H
21
22 #include "gdbsupport/array-view.h"
23
24 /* A test is just a function that does some checks and throws an
25 exception if something has gone wrong. */
26
27 typedef void self_test_function (void);
28
29 namespace selftests
30 {
31
32 /* Interface for the various kinds of selftests. */
33
34 struct selftest
35 {
36 virtual ~selftest () = default;
37 virtual void operator() () const = 0;
38 };
39
40 /* Register a new self-test. */
41
42 extern void register_test (const std::string &name, selftest *test);
43
44 /* Register a new self-test. */
45
46 extern void register_test (const std::string &name,
47 self_test_function *function);
48
49 /* Run all the self tests. This print a message describing the number
50 of test and the number of failures.
51
52 If FILTERS is not empty, only run tests with names containing one of the
53 element of FILTERS. */
54
55 extern void run_tests (gdb::array_view<const char *const> filters);
56
57 /* Reset GDB or GDBserver's internal state. */
58 extern void reset ();
59
60 typedef void for_each_selftest_ftype (const std::string &name);
61
62 /* Call FUNC for each registered selftest. */
63
64 extern void for_each_selftest (for_each_selftest_ftype func);
65 }
66
67 /* Check that VALUE is true, and, if not, throw an exception. */
68
69 #define SELF_CHECK(VALUE) \
70 do { \
71 if (!(VALUE)) \
72 error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \
73 } while (0)
74
75 #endif /* COMMON_SELFTEST_H */