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 #include "common-defs.h" 207d62b00eSchristos #include "common-exceptions.h" 217d62b00eSchristos #include "common-debug.h" 227d62b00eSchristos #include "selftest.h" 23*6881a400Schristos #include <functional> 247d62b00eSchristos 257d62b00eSchristos namespace selftests 267d62b00eSchristos { 27*6881a400Schristos /* All the tests that have been registered. Using an std::set allows keeping 287d62b00eSchristos the order of tests stable and easily looking up whether a test name 297d62b00eSchristos exists. */ 307d62b00eSchristos 31*6881a400Schristos static selftests_registry tests; 327d62b00eSchristos 33*6881a400Schristos /* Set of callback functions used to register selftests after GDB is fully 34*6881a400Schristos initialized. */ 357d62b00eSchristos 36*6881a400Schristos static std::vector<selftests_generator> lazy_generators; 377d62b00eSchristos 387d62b00eSchristos /* See selftest.h. */ 397d62b00eSchristos 407d62b00eSchristos void 41*6881a400Schristos register_test (const std::string &name, 42*6881a400Schristos std::function<void(void)> function) 437d62b00eSchristos { 447d62b00eSchristos /* Check that no test with this name already exist. */ 45*6881a400Schristos auto status = tests.emplace (name, std::move (function)); 46*6881a400Schristos if (!status.second) 47*6881a400Schristos gdb_assert_not_reached ("Test already registered"); 487d62b00eSchristos } 497d62b00eSchristos 507d62b00eSchristos /* See selftest.h. */ 517d62b00eSchristos 527d62b00eSchristos void 53*6881a400Schristos add_lazy_generator (selftests_generator generator) 547d62b00eSchristos { 55*6881a400Schristos lazy_generators.push_back (std::move (generator)); 56*6881a400Schristos } 57*6881a400Schristos 58*6881a400Schristos /* See selftest.h. */ 59*6881a400Schristos 60*6881a400Schristos static bool run_verbose_ = false; 61*6881a400Schristos 62*6881a400Schristos /* See selftest.h. */ 63*6881a400Schristos 64*6881a400Schristos bool 65*6881a400Schristos run_verbose () 66*6881a400Schristos { 67*6881a400Schristos return run_verbose_; 687d62b00eSchristos } 697d62b00eSchristos 707d62b00eSchristos /* See selftest.h. */ 717d62b00eSchristos 727d62b00eSchristos void 73*6881a400Schristos run_tests (gdb::array_view<const char *const> filters, bool verbose) 747d62b00eSchristos { 757d62b00eSchristos int ran = 0, failed = 0; 76*6881a400Schristos run_verbose_ = verbose; 777d62b00eSchristos 78*6881a400Schristos for (const auto &test : all_selftests ()) 797d62b00eSchristos { 807d62b00eSchristos bool run = false; 817d62b00eSchristos 827d62b00eSchristos if (filters.empty ()) 837d62b00eSchristos run = true; 847d62b00eSchristos else 857d62b00eSchristos { 867d62b00eSchristos for (const char *filter : filters) 877d62b00eSchristos { 88*6881a400Schristos if (test.name.find (filter) != std::string::npos) 897d62b00eSchristos run = true; 907d62b00eSchristos } 917d62b00eSchristos } 927d62b00eSchristos 937d62b00eSchristos if (!run) 947d62b00eSchristos continue; 957d62b00eSchristos 967d62b00eSchristos try 977d62b00eSchristos { 98*6881a400Schristos debug_printf (_("Running selftest %s.\n"), test.name.c_str ()); 997d62b00eSchristos ++ran; 100*6881a400Schristos test.test (); 1017d62b00eSchristos } 1027d62b00eSchristos catch (const gdb_exception_error &ex) 1037d62b00eSchristos { 1047d62b00eSchristos ++failed; 1057d62b00eSchristos debug_printf ("Self test failed: %s\n", ex.what ()); 1067d62b00eSchristos } 1077d62b00eSchristos 1087d62b00eSchristos reset (); 1097d62b00eSchristos } 1107d62b00eSchristos 1117d62b00eSchristos debug_printf (_("Ran %d unit tests, %d failed\n"), 1127d62b00eSchristos ran, failed); 1137d62b00eSchristos } 1147d62b00eSchristos 1157d62b00eSchristos /* See selftest.h. */ 1167d62b00eSchristos 117*6881a400Schristos selftests_range 118*6881a400Schristos all_selftests () 1197d62b00eSchristos { 120*6881a400Schristos /* Execute any function which might still want to register tests. Once each 121*6881a400Schristos function has been executed, clear lazy_generators to ensure that 122*6881a400Schristos callback functions are only executed once. */ 123*6881a400Schristos for (const auto &generator : lazy_generators) 124*6881a400Schristos for (selftest &test : generator ()) 125*6881a400Schristos register_test (std::move (test.name), std::move (test.test)); 126*6881a400Schristos lazy_generators.clear (); 127*6881a400Schristos 128*6881a400Schristos return selftests_range (tests.cbegin (), tests.cend ()); 1297d62b00eSchristos } 1307d62b00eSchristos 1317d62b00eSchristos } // namespace selftests 132