1 1.1 christos /* GDB self-testing. 2 1.1.1.3 christos Copyright (C) 2016-2024 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of GDB. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3 of the License, or 9 1.1 christos (at your option) any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 1.1 christos 19 1.1 christos #ifndef COMMON_SELFTEST_H 20 1.1 christos #define COMMON_SELFTEST_H 21 1.1 christos 22 1.1 christos #include "gdbsupport/array-view.h" 23 1.1.1.2 christos #include "gdbsupport/function-view.h" 24 1.1.1.2 christos #include "gdbsupport/iterator-range.h" 25 1.1.1.2 christos #include <set> 26 1.1.1.2 christos #include <vector> 27 1.1 christos 28 1.1 christos /* A test is just a function that does some checks and throws an 29 1.1 christos exception if something has gone wrong. */ 30 1.1 christos 31 1.1 christos namespace selftests 32 1.1 christos { 33 1.1 christos 34 1.1.1.2 christos /* Selftests are registered under a unique name. */ 35 1.1 christos 36 1.1 christos struct selftest 37 1.1 christos { 38 1.1.1.2 christos selftest (std::string name, std::function<void (void)> test) 39 1.1.1.2 christos : name { std::move (name) }, test { std::move (test) } 40 1.1.1.2 christos { } 41 1.1.1.2 christos bool operator< (const selftest &rhs) const 42 1.1.1.2 christos { return name < rhs.name; } 43 1.1.1.2 christos 44 1.1.1.2 christos std::string name; 45 1.1.1.2 christos std::function<void (void)> test; 46 1.1 christos }; 47 1.1 christos 48 1.1.1.2 christos /* Type of the container of all the registered selftests. */ 49 1.1.1.2 christos using selftests_registry = std::set<selftest>; 50 1.1.1.2 christos using selftests_range = iterator_range<selftests_registry::const_iterator>; 51 1.1.1.2 christos 52 1.1.1.2 christos /* Create a range to iterate over all registered tests. */ 53 1.1 christos 54 1.1.1.2 christos selftests_range all_selftests (); 55 1.1.1.2 christos 56 1.1.1.2 christos /* True if selftest should run verbosely. */ 57 1.1.1.2 christos 58 1.1.1.2 christos extern bool run_verbose (); 59 1.1 christos 60 1.1 christos /* Register a new self-test. */ 61 1.1 christos 62 1.1 christos extern void register_test (const std::string &name, 63 1.1.1.2 christos std::function<void(void)> function); 64 1.1.1.2 christos 65 1.1.1.2 christos /* A selftest generator is a callback function used to delay the generation 66 1.1.1.2 christos of selftests. */ 67 1.1.1.2 christos 68 1.1.1.2 christos using selftests_generator = std::function<std::vector<selftest> (void)>; 69 1.1.1.2 christos 70 1.1.1.2 christos /* Register a function which can lazily register selftests once GDB is fully 71 1.1.1.2 christos initialized. */ 72 1.1.1.2 christos 73 1.1.1.2 christos extern void add_lazy_generator (selftests_generator generator); 74 1.1 christos 75 1.1 christos /* Run all the self tests. This print a message describing the number 76 1.1 christos of test and the number of failures. 77 1.1 christos 78 1.1 christos If FILTERS is not empty, only run tests with names containing one of the 79 1.1 christos element of FILTERS. */ 80 1.1 christos 81 1.1.1.2 christos extern void run_tests (gdb::array_view<const char *const> filters, 82 1.1.1.2 christos bool verbose = false); 83 1.1 christos 84 1.1 christos /* Reset GDB or GDBserver's internal state. */ 85 1.1 christos extern void reset (); 86 1.1 christos } 87 1.1 christos 88 1.1 christos /* Check that VALUE is true, and, if not, throw an exception. */ 89 1.1 christos 90 1.1 christos #define SELF_CHECK(VALUE) \ 91 1.1 christos do { \ 92 1.1 christos if (!(VALUE)) \ 93 1.1 christos error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \ 94 1.1 christos } while (0) 95 1.1 christos 96 1.1 christos #endif /* COMMON_SELFTEST_H */ 97