]>
Commit | Line | Data |
---|---|---|
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 | ||
6d580b63 YQ |
19 | #include "common-defs.h" |
20 | #include "common-exceptions.h" | |
21 | #include "common-debug.h" | |
dcd1f979 | 22 | #include "selftest.h" |
1526853e | 23 | #include <map> |
c45a683f | 24 | #include <functional> |
dcd1f979 | 25 | |
7649770c YQ |
26 | namespace selftests |
27 | { | |
1526853e SM |
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. */ | |
7649770c | 31 | |
d7c68312 | 32 | static std::map<std::string, std::function<void(void)>> tests; |
dcd1f979 | 33 | |
dcd1f979 TT |
34 | /* See selftest.h. */ |
35 | ||
36 | void | |
d7c68312 TT |
37 | register_test (const std::string &name, |
38 | std::function<void(void)> function) | |
dcd1f979 | 39 | { |
1526853e SM |
40 | /* Check that no test with this name already exist. */ |
41 | gdb_assert (tests.find (name) == tests.end ()); | |
42 | ||
d7c68312 | 43 | tests[name] = function; |
1526853e SM |
44 | } |
45 | ||
46 | /* See selftest.h. */ | |
dcd1f979 | 47 | |
479209dd TV |
48 | static bool run_verbose_ = false; |
49 | ||
50 | /* See selftest.h. */ | |
51 | ||
52 | bool | |
53 | run_verbose () | |
54 | { | |
55 | return run_verbose_; | |
56 | } | |
57 | ||
58 | /* See selftest.h. */ | |
59 | ||
1526853e | 60 | void |
479209dd | 61 | run_tests (gdb::array_view<const char *const> filters, bool verbose) |
1526853e SM |
62 | { |
63 | int ran = 0, failed = 0; | |
479209dd | 64 | run_verbose_ = verbose; |
1526853e SM |
65 | |
66 | for (const auto &pair : tests) | |
dcd1f979 | 67 | { |
1526853e | 68 | const std::string &name = pair.first; |
d7c68312 | 69 | const auto &test = pair.second; |
ece5bc8a | 70 | bool run = false; |
1526853e | 71 | |
ece5bc8a SM |
72 | if (filters.empty ()) |
73 | run = true; | |
74 | else | |
75 | { | |
76 | for (const char *filter : filters) | |
77 | { | |
78 | if (name.find (filter) != std::string::npos) | |
79 | run = true; | |
80 | } | |
81 | } | |
82 | ||
83 | if (!run) | |
1526853e SM |
84 | continue; |
85 | ||
a70b8144 | 86 | try |
dcd1f979 | 87 | { |
1526853e SM |
88 | debug_printf (_("Running selftest %s.\n"), name.c_str ()); |
89 | ++ran; | |
d7c68312 | 90 | test (); |
dcd1f979 | 91 | } |
230d2906 | 92 | catch (const gdb_exception_error &ex) |
dcd1f979 TT |
93 | { |
94 | ++failed; | |
3d6e9d23 | 95 | debug_printf ("Self test failed: %s\n", ex.what ()); |
dcd1f979 | 96 | } |
7a3929c4 | 97 | |
6d580b63 | 98 | reset (); |
dcd1f979 TT |
99 | } |
100 | ||
1526853e SM |
101 | debug_printf (_("Ran %d unit tests, %d failed\n"), |
102 | ran, failed); | |
dcd1f979 | 103 | } |
1526853e SM |
104 | |
105 | /* See selftest.h. */ | |
106 | ||
107 | void for_each_selftest (for_each_selftest_ftype func) | |
108 | { | |
109 | for (const auto &pair : tests) | |
110 | func (pair.first); | |
111 | } | |
112 | ||
7649770c | 113 | } // namespace selftests |