1129198Scognet/* GDB self-testing.
2129198Scognet   Copyright (C) 2016-2020 Free Software Foundation, Inc.
3139735Simp
4129198Scognet   This file is part of GDB.
5129198Scognet
6129198Scognet   This program is free software; you can redistribute it and/or modify
7129198Scognet   it under the terms of the GNU General Public License as published by
8129198Scognet   the Free Software Foundation; either version 3 of the License, or
9129198Scognet   (at your option) any later version.
10129198Scognet
11129198Scognet   This program is distributed in the hope that it will be useful,
12129198Scognet   but WITHOUT ANY WARRANTY; without even the implied warranty of
13129198Scognet   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14129198Scognet   GNU General Public License for more details.
15129198Scognet
16129198Scognet   You should have received a copy of the GNU General Public License
17129198Scognet   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18129198Scognet
19129198Scognet#include "common-defs.h"
20129198Scognet#include "common-exceptions.h"
21129198Scognet#include "common-debug.h"
22129198Scognet#include "selftest.h"
23129198Scognet#include <map>
24129198Scognet
25129198Scognetnamespace selftests
26129198Scognet{
27129198Scognet/* All the tests that have been registered.  Using an std::map allows keeping
28129198Scognet   the order of tests stable and easily looking up whether a test name
29129198Scognet   exists.  */
30129198Scognet
31129198Scognetstatic std::map<std::string, std::unique_ptr<selftest>> tests;
32129198Scognet
33129198Scognet/* A selftest that calls the test function without arguments.  */
34129198Scognet
35129198Scognetstruct simple_selftest : public selftest
36129198Scognet{
37129198Scognet  simple_selftest (self_test_function *function_)
38129198Scognet  : function (function_)
39129198Scognet  {}
40129198Scognet
41129198Scognet  void operator() () const override
42129198Scognet  {
43129198Scognet    function ();
44129198Scognet  }
45129198Scognet
46129198Scognet  self_test_function *function;
47129198Scognet};
48129198Scognet
49129198Scognet/* See selftest.h.  */
50290661Smmel
51129198Scognetvoid
52132055Scognetregister_test (const std::string &name, selftest *test)
53129198Scognet{
54132055Scognet  /* Check that no test with this name already exist.  */
55132055Scognet  gdb_assert (tests.find (name) == tests.end ());
56132055Scognet
57137940Scognet  tests[name] = std::unique_ptr<selftest> (test);
58132055Scognet}
59132471Scognet
60129198Scognet/* See selftest.h.  */
61129198Scognet
62129198Scognetvoid
63290648Smmelregister_test (const std::string &name, self_test_function *function)
64129198Scognet{
65129198Scognet  register_test (name, new simple_selftest (function));
66129198Scognet}
67129198Scognet
68129198Scognet/* See selftest.h.  */
69129198Scognet
70129198Scognetvoid
71129198Scognetrun_tests (gdb::array_view<const char *const> filters)
72129198Scognet{
73129198Scognet  int ran = 0, failed = 0;
74129198Scognet
75129198Scognet  for (const auto &pair : tests)
76129198Scognet    {
77290648Smmel      const std::string &name = pair.first;
78290648Smmel      const std::unique_ptr<selftest> &test = pair.second;
79129198Scognet      bool run = false;
80290648Smmel
81129198Scognet      if (filters.empty ())
82290648Smmel	run = true;
83129198Scognet      else
84129198Scognet	{
85129198Scognet	  for (const char *filter : filters)
86129198Scognet	    {
87129198Scognet	      if (name.find (filter) != std::string::npos)
88129198Scognet		run = true;
89129198Scognet	    }
90129198Scognet	}
91129198Scognet
92129198Scognet      if (!run)
93129198Scognet	continue;
94129198Scognet
95129198Scognet      try
96129198Scognet	{
97129198Scognet	  debug_printf (_("Running selftest %s.\n"), name.c_str ());
98129198Scognet	  ++ran;
99129198Scognet	  (*test) ();
100129198Scognet	}
101129198Scognet      catch (const gdb_exception_error &ex)
102129198Scognet	{
103129198Scognet	  ++failed;
104129198Scognet	  debug_printf ("Self test failed: %s\n", ex.what ());
105129198Scognet	}
106129198Scognet
107129198Scognet      reset ();
108262420Sian    }
109262420Sian
110262420Sian  debug_printf (_("Ran %d unit tests, %d failed\n"),
111262420Sian		ran, failed);
112262420Sian}
113290648Smmel
114129198Scognet/* See selftest.h.  */
115129198Scognet
116129198Scognetvoid for_each_selftest (for_each_selftest_ftype func)
117129198Scognet{
118129198Scognet  for (const auto &pair : tests)
119129198Scognet    func (pair.first);
120129198Scognet}
121129198Scognet
122129198Scognet} // namespace selftests
123129198Scognet