Commit | Line | Data |
---|---|---|
dcd1f979 | 1 | /* GDB self-testing. |
b811d2c2 | 2 | Copyright (C) 2016-2020 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> |
dcd1f979 | 24 | |
7649770c YQ |
25 | namespace selftests |
26 | { | |
1526853e SM |
27 | /* All the tests that have been registered. Using an std::map allows keeping |
28 | the order of tests stable and easily looking up whether a test name | |
29 | exists. */ | |
7649770c | 30 | |
1526853e | 31 | static std::map<std::string, std::unique_ptr<selftest>> tests; |
dcd1f979 | 32 | |
1526853e SM |
33 | /* A selftest that calls the test function without arguments. */ |
34 | ||
35 | struct simple_selftest : public selftest | |
36 | { | |
37 | simple_selftest (self_test_function *function_) | |
38 | : function (function_) | |
39 | {} | |
40 | ||
41 | void operator() () const override | |
42 | { | |
43 | function (); | |
44 | } | |
45 | ||
46 | self_test_function *function; | |
47 | }; | |
dcd1f979 TT |
48 | |
49 | /* See selftest.h. */ | |
50 | ||
51 | void | |
1526853e | 52 | register_test (const std::string &name, selftest *test) |
dcd1f979 | 53 | { |
1526853e SM |
54 | /* Check that no test with this name already exist. */ |
55 | gdb_assert (tests.find (name) == tests.end ()); | |
56 | ||
57 | tests[name] = std::unique_ptr<selftest> (test); | |
dcd1f979 TT |
58 | } |
59 | ||
60 | /* See selftest.h. */ | |
61 | ||
62 | void | |
1526853e | 63 | register_test (const std::string &name, self_test_function *function) |
dcd1f979 | 64 | { |
1526853e SM |
65 | register_test (name, new simple_selftest (function)); |
66 | } | |
67 | ||
68 | /* See selftest.h. */ | |
dcd1f979 | 69 | |
1526853e | 70 | void |
ece5bc8a | 71 | run_tests (gdb::array_view<const char *const> filters) |
1526853e SM |
72 | { |
73 | int ran = 0, failed = 0; | |
74 | ||
75 | for (const auto &pair : tests) | |
dcd1f979 | 76 | { |
1526853e SM |
77 | const std::string &name = pair.first; |
78 | const std::unique_ptr<selftest> &test = pair.second; | |
ece5bc8a | 79 | bool run = false; |
1526853e | 80 | |
ece5bc8a SM |
81 | if (filters.empty ()) |
82 | run = true; | |
83 | else | |
84 | { | |
85 | for (const char *filter : filters) | |
86 | { | |
87 | if (name.find (filter) != std::string::npos) | |
88 | run = true; | |
89 | } | |
90 | } | |
91 | ||
92 | if (!run) | |
1526853e SM |
93 | continue; |
94 | ||
a70b8144 | 95 | try |
dcd1f979 | 96 | { |
1526853e SM |
97 | debug_printf (_("Running selftest %s.\n"), name.c_str ()); |
98 | ++ran; | |
99 | (*test) (); | |
dcd1f979 | 100 | } |
230d2906 | 101 | catch (const gdb_exception_error &ex) |
dcd1f979 TT |
102 | { |
103 | ++failed; | |
3d6e9d23 | 104 | debug_printf ("Self test failed: %s\n", ex.what ()); |
dcd1f979 | 105 | } |
7a3929c4 | 106 | |
6d580b63 | 107 | reset (); |
dcd1f979 TT |
108 | } |
109 | ||
1526853e SM |
110 | debug_printf (_("Ran %d unit tests, %d failed\n"), |
111 | ran, failed); | |
dcd1f979 | 112 | } |
1526853e SM |
113 | |
114 | /* See selftest.h. */ | |
115 | ||
116 | void for_each_selftest (for_each_selftest_ftype func) | |
117 | { | |
118 | for (const auto &pair : tests) | |
119 | func (pair.first); | |
120 | } | |
121 | ||
7649770c | 122 | } // namespace selftests |