18dffb485Schristos /* GDB self-testing. 2*5ba1f45fSchristos Copyright (C) 2016-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos This file is part of GDB. 58dffb485Schristos 68dffb485Schristos This program is free software; you can redistribute it and/or modify 78dffb485Schristos it under the terms of the GNU General Public License as published by 88dffb485Schristos the Free Software Foundation; either version 3 of the License, or 98dffb485Schristos (at your option) any later version. 108dffb485Schristos 118dffb485Schristos This program is distributed in the hope that it will be useful, 128dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148dffb485Schristos GNU General Public License for more details. 158dffb485Schristos 168dffb485Schristos You should have received a copy of the GNU General Public License 178dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 188dffb485Schristos 198dffb485Schristos #ifndef COMMON_SELFTEST_H 208dffb485Schristos #define COMMON_SELFTEST_H 218dffb485Schristos 228dffb485Schristos #include "gdbsupport/array-view.h" 234b169a6bSchristos #include "gdbsupport/function-view.h" 244b169a6bSchristos #include "gdbsupport/iterator-range.h" 254b169a6bSchristos #include <set> 264b169a6bSchristos #include <vector> 278dffb485Schristos 288dffb485Schristos /* A test is just a function that does some checks and throws an 298dffb485Schristos exception if something has gone wrong. */ 308dffb485Schristos 318dffb485Schristos namespace selftests 328dffb485Schristos { 338dffb485Schristos 344b169a6bSchristos /* Selftests are registered under a unique name. */ 358dffb485Schristos 368dffb485Schristos struct selftest 378dffb485Schristos { 384b169a6bSchristos selftest (std::string name, std::function<void (void)> test) 394b169a6bSchristos : name { std::move (name) }, test { std::move (test) } 404b169a6bSchristos { } 414b169a6bSchristos bool operator< (const selftest &rhs) const 424b169a6bSchristos { return name < rhs.name; } 434b169a6bSchristos 444b169a6bSchristos std::string name; 454b169a6bSchristos std::function<void (void)> test; 468dffb485Schristos }; 478dffb485Schristos 484b169a6bSchristos /* Type of the container of all the registered selftests. */ 494b169a6bSchristos using selftests_registry = std::set<selftest>; 504b169a6bSchristos using selftests_range = iterator_range<selftests_registry::const_iterator>; 518dffb485Schristos 524b169a6bSchristos /* Create a range to iterate over all registered tests. */ 534b169a6bSchristos 544b169a6bSchristos selftests_range all_selftests (); 554b169a6bSchristos 564b169a6bSchristos /* True if selftest should run verbosely. */ 574b169a6bSchristos 584b169a6bSchristos extern bool run_verbose (); 598dffb485Schristos 608dffb485Schristos /* Register a new self-test. */ 618dffb485Schristos 628dffb485Schristos extern void register_test (const std::string &name, 634b169a6bSchristos std::function<void(void)> function); 644b169a6bSchristos 654b169a6bSchristos /* A selftest generator is a callback function used to delay the generation 664b169a6bSchristos of selftests. */ 674b169a6bSchristos 684b169a6bSchristos using selftests_generator = std::function<std::vector<selftest> (void)>; 694b169a6bSchristos 704b169a6bSchristos /* Register a function which can lazily register selftests once GDB is fully 714b169a6bSchristos initialized. */ 724b169a6bSchristos 734b169a6bSchristos extern void add_lazy_generator (selftests_generator generator); 748dffb485Schristos 758dffb485Schristos /* Run all the self tests. This print a message describing the number 768dffb485Schristos of test and the number of failures. 778dffb485Schristos 788dffb485Schristos If FILTERS is not empty, only run tests with names containing one of the 798dffb485Schristos element of FILTERS. */ 808dffb485Schristos 814b169a6bSchristos extern void run_tests (gdb::array_view<const char *const> filters, 824b169a6bSchristos bool verbose = false); 838dffb485Schristos 848dffb485Schristos /* Reset GDB or GDBserver's internal state. */ 858dffb485Schristos extern void reset (); 868dffb485Schristos } 878dffb485Schristos 888dffb485Schristos /* Check that VALUE is true, and, if not, throw an exception. */ 898dffb485Schristos 908dffb485Schristos #define SELF_CHECK(VALUE) \ 918dffb485Schristos do { \ 928dffb485Schristos if (!(VALUE)) \ 938dffb485Schristos error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \ 948dffb485Schristos } while (0) 958dffb485Schristos 968dffb485Schristos #endif /* COMMON_SELFTEST_H */ 97