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