GDB (xrefs)

selftest.c
Go to the documentation of this file.
1 /* GDB self-testing.
2 Copyright (C) 2016-2018 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
25 namespace selftests
26 {
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. */
30
31 static std::map<std::string, std::unique_ptr<selftest>> tests;
32
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 };
48
49 /* See selftest.h. */
50
51 void
52 register_test (const std::string &name, selftest *test)
53 {
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);
58 }
59
60 /* See selftest.h. */
61
62 void
63 register_test (const std::string &name, self_test_function *function)
64 {
65 register_test (name, new simple_selftest (function));
66 }
67
68 /* See selftest.h. */
69
70 void
71 run_tests (const char *filter)
72 {
73 int ran = 0, failed = 0;
74
75 for (const auto &pair : tests)
76 {
77 const std::string &name = pair.first;
78 const std::unique_ptr<selftest> &test = pair.second;
79
80 if (filter != NULL && *filter != '\0'
81 && name.find (filter) == std::string::npos)
82 continue;
83
84 TRY
85 {
86 debug_printf (_("Running selftest %s.\n"), name.c_str ());
87 ++ran;
88 (*test) ();
89 }
90 CATCH (ex, RETURN_MASK_ERROR)
91 {
92 ++failed;
93 debug_printf ("Self test failed: %s\n", ex.message);
94 }
95 END_CATCH
96
97 reset ();
98 }
99
100 debug_printf (_("Ran %d unit tests, %d failed\n"),
101 ran, failed);
102 }
103
104 /* See selftest.h. */
105
106 void for_each_selftest (for_each_selftest_ftype func)
107 {
108 for (const auto &pair : tests)
109 func (pair.first);
110 }
111
112 } // namespace selftests
string
const char * string
Definition: signals.c:50
selftests::reset
void reset()
func
void(* func)(char *)
Definition: remote-fileio.c:1104
selftests::simple_selftest::function
self_test_function * function
Definition: selftest.c:46
RETURN_MASK_ERROR
Definition: common-exceptions.h:43
_
#define _(String)
Definition: gdb_locale.h:35
assign_1::test
void test()
Definition: 1.cc:95
END_CATCH
#define END_CATCH
Definition: common-exceptions.h:259
selftests
Definition: selftest.c:25
selftests::selftest
Definition: selftest.h:32
selftests::tests
static std::map< std::string, std::unique_ptr< selftest > > tests
Definition: selftest.c:31
TRY
#define TRY
Definition: common-exceptions.h:246
name
const char *const name
Definition: aarch64-tdep.c:76
CATCH
#define CATCH(EXCEPTION, MASK)
Definition: common-exceptions.h:254
selftests::simple_selftest::simple_selftest
simple_selftest(self_test_function *function_)
Definition: selftest.c:37
selftests::for_each_selftest_ftype
void for_each_selftest_ftype(const std::string &name)
Definition: selftest.h:58
common-defs.h
selftests::for_each_selftest
void for_each_selftest(for_each_selftest_ftype func)
Definition: selftest.c:106
selftests::simple_selftest
Definition: selftest.c:35
common-exceptions.h
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
selftests::simple_selftest::operator()
void operator()() const override
Definition: selftest.c:41
selftests::run_tests
void run_tests(const char *filter)
Definition: selftest.c:71
common-debug.h
selftest.h
self_test_function
void self_test_function(void)
Definition: selftest.h:25
debug_printf
void debug_printf(const char *fmt,...)
Definition: common-debug.c:30
selftests::register_test
void register_test(const std::string &name, selftest *test)
Definition: selftest.c:52