xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/selftest.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* GDB self-testing.
2*6881a400Schristos    Copyright (C) 2016-2023 Free Software Foundation, Inc.
37d62b00eSchristos 
47d62b00eSchristos    This file is part of GDB.
57d62b00eSchristos 
67d62b00eSchristos    This program is free software; you can redistribute it and/or modify
77d62b00eSchristos    it under the terms of the GNU General Public License as published by
87d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
97d62b00eSchristos    (at your option) any later version.
107d62b00eSchristos 
117d62b00eSchristos    This program is distributed in the hope that it will be useful,
127d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
137d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147d62b00eSchristos    GNU General Public License for more details.
157d62b00eSchristos 
167d62b00eSchristos    You should have received a copy of the GNU General Public License
177d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
187d62b00eSchristos 
197d62b00eSchristos #ifndef COMMON_SELFTEST_H
207d62b00eSchristos #define COMMON_SELFTEST_H
217d62b00eSchristos 
227d62b00eSchristos #include "gdbsupport/array-view.h"
23*6881a400Schristos #include "gdbsupport/function-view.h"
24*6881a400Schristos #include "gdbsupport/iterator-range.h"
25*6881a400Schristos #include <set>
26*6881a400Schristos #include <vector>
277d62b00eSchristos 
287d62b00eSchristos /* A test is just a function that does some checks and throws an
297d62b00eSchristos    exception if something has gone wrong.  */
307d62b00eSchristos 
317d62b00eSchristos namespace selftests
327d62b00eSchristos {
337d62b00eSchristos 
34*6881a400Schristos /* Selftests are registered under a unique name.  */
357d62b00eSchristos 
367d62b00eSchristos struct selftest
377d62b00eSchristos {
38*6881a400Schristos   selftest (std::string name, std::function<void (void)> test)
39*6881a400Schristos     : name { std::move (name) }, test { std::move (test) }
40*6881a400Schristos   { }
41*6881a400Schristos   bool operator< (const selftest &rhs) const
42*6881a400Schristos   { return name < rhs.name; }
43*6881a400Schristos 
44*6881a400Schristos   std::string name;
45*6881a400Schristos   std::function<void (void)> test;
467d62b00eSchristos };
477d62b00eSchristos 
48*6881a400Schristos /* Type of the container of all the registered selftests.  */
49*6881a400Schristos using selftests_registry = std::set<selftest>;
50*6881a400Schristos using selftests_range = iterator_range<selftests_registry::const_iterator>;
517d62b00eSchristos 
52*6881a400Schristos /* Create a range to iterate over all registered tests.  */
53*6881a400Schristos 
54*6881a400Schristos selftests_range all_selftests ();
55*6881a400Schristos 
56*6881a400Schristos /* True if selftest should run verbosely.  */
57*6881a400Schristos 
58*6881a400Schristos extern bool run_verbose ();
597d62b00eSchristos 
607d62b00eSchristos /* Register a new self-test.  */
617d62b00eSchristos 
627d62b00eSchristos extern void register_test (const std::string &name,
63*6881a400Schristos 			   std::function<void(void)> function);
64*6881a400Schristos 
65*6881a400Schristos /* A selftest generator is a callback function used to delay the generation
66*6881a400Schristos    of selftests.  */
67*6881a400Schristos 
68*6881a400Schristos using selftests_generator = std::function<std::vector<selftest> (void)>;
69*6881a400Schristos 
70*6881a400Schristos /* Register a function which can lazily register selftests once GDB is fully
71*6881a400Schristos    initialized. */
72*6881a400Schristos 
73*6881a400Schristos extern void add_lazy_generator (selftests_generator generator);
747d62b00eSchristos 
757d62b00eSchristos /* Run all the self tests.  This print a message describing the number
767d62b00eSchristos    of test and the number of failures.
777d62b00eSchristos 
787d62b00eSchristos    If FILTERS is not empty, only run tests with names containing one of the
797d62b00eSchristos    element of FILTERS.  */
807d62b00eSchristos 
81*6881a400Schristos extern void run_tests (gdb::array_view<const char *const> filters,
82*6881a400Schristos 		       bool verbose = false);
837d62b00eSchristos 
847d62b00eSchristos /* Reset GDB or GDBserver's internal state.  */
857d62b00eSchristos extern void reset ();
867d62b00eSchristos }
877d62b00eSchristos 
887d62b00eSchristos /* Check that VALUE is true, and, if not, throw an exception.  */
897d62b00eSchristos 
907d62b00eSchristos #define SELF_CHECK(VALUE)						\
917d62b00eSchristos   do {									\
927d62b00eSchristos     if (!(VALUE))							\
937d62b00eSchristos       error (_("self-test failed at %s:%d"), __FILE__, __LINE__);	\
947d62b00eSchristos   } while (0)
957d62b00eSchristos 
967d62b00eSchristos #endif /* COMMON_SELFTEST_H */
97