gdbsupport: make gdb_open_cloexec return scoped_fd
[binutils-gdb.git] / gdbsupport / selftest.cc
1 /* GDB self-testing.
2 Copyright (C) 2016-2021 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 #include "common-defs.h"
20 #include "common-exceptions.h"
21 #include "common-debug.h"
22 #include "selftest.h"
23 #include <map>
24 #include <functional>
25
26 namespace selftests
27 {
28 /* All the tests that have been registered. Using an std::map allows keeping
29 the order of tests stable and easily looking up whether a test name
30 exists. */
31
32 static std::map<std::string, std::unique_ptr<selftest>> tests;
33
34 /* See selftest.h. */
35
36 void
37 register_test (const std::string &name, selftest *test)
38 {
39 /* Check that no test with this name already exist. */
40 gdb_assert (tests.find (name) == tests.end ());
41
42 tests[name] = std::unique_ptr<selftest> (test);
43 }
44
45 /* A selftest that calls the test function without arguments. */
46
47 struct lambda_selftest : public selftest
48 {
49 lambda_selftest (std::function<void(void)> function_)
50 {
51 function = function_;
52 }
53
54 void operator() () const override
55 {
56 function ();
57 }
58
59 std::function<void(void)> function;
60 };
61
62 /* See selftest.h. */
63
64 void
65 register_test (const std::string &name,
66 std::function<void(void)> function)
67 {
68 register_test (name, new lambda_selftest (function));
69 }
70
71 /* See selftest.h. */
72
73 static bool run_verbose_ = false;
74
75 /* See selftest.h. */
76
77 bool
78 run_verbose ()
79 {
80 return run_verbose_;
81 }
82
83 /* See selftest.h. */
84
85 void
86 run_tests (gdb::array_view<const char *const> filters, bool verbose)
87 {
88 int ran = 0, failed = 0;
89 run_verbose_ = verbose;
90
91 for (const auto &pair : tests)
92 {
93 const std::string &name = pair.first;
94 const std::unique_ptr<selftest> &test = pair.second;
95 bool run = false;
96
97 if (filters.empty ())
98 run = true;
99 else
100 {
101 for (const char *filter : filters)
102 {
103 if (name.find (filter) != std::string::npos)
104 run = true;
105 }
106 }
107
108 if (!run)
109 continue;
110
111 try
112 {
113 debug_printf (_("Running selftest %s.\n"), name.c_str ());
114 ++ran;
115 (*test) ();
116 }
117 catch (const gdb_exception_error &ex)
118 {
119 ++failed;
120 debug_printf ("Self test failed: %s\n", ex.what ());
121 }
122
123 reset ();
124 }
125
126 debug_printf (_("Ran %d unit tests, %d failed\n"),
127 ran, failed);
128 }
129
130 /* See selftest.h. */
131
132 void for_each_selftest (for_each_selftest_ftype func)
133 {
134 for (const auto &pair : tests)
135 func (pair.first);
136 }
137
138 } // namespace selftests