selftest.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* GDB self-testing.
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "common-exceptions.h"
  16. #include "common-debug.h"
  17. #include "selftest.h"
  18. #include <map>
  19. #include <functional>
  20. namespace selftests
  21. {
  22. /* All the tests that have been registered. Using an std::map allows keeping
  23. the order of tests stable and easily looking up whether a test name
  24. exists. */
  25. static std::map<std::string, std::function<void(void)>> tests;
  26. /* See selftest.h. */
  27. void
  28. register_test (const std::string &name,
  29. std::function<void(void)> function)
  30. {
  31. /* Check that no test with this name already exist. */
  32. gdb_assert (tests.find (name) == tests.end ());
  33. tests[name] = function;
  34. }
  35. /* See selftest.h. */
  36. static bool run_verbose_ = false;
  37. /* See selftest.h. */
  38. bool
  39. run_verbose ()
  40. {
  41. return run_verbose_;
  42. }
  43. /* See selftest.h. */
  44. void
  45. run_tests (gdb::array_view<const char *const> filters, bool verbose)
  46. {
  47. int ran = 0, failed = 0;
  48. run_verbose_ = verbose;
  49. for (const auto &pair : tests)
  50. {
  51. const std::string &name = pair.first;
  52. const auto &test = pair.second;
  53. bool run = false;
  54. if (filters.empty ())
  55. run = true;
  56. else
  57. {
  58. for (const char *filter : filters)
  59. {
  60. if (name.find (filter) != std::string::npos)
  61. run = true;
  62. }
  63. }
  64. if (!run)
  65. continue;
  66. try
  67. {
  68. debug_printf (_("Running selftest %s.\n"), name.c_str ());
  69. ++ran;
  70. test ();
  71. }
  72. catch (const gdb_exception_error &ex)
  73. {
  74. ++failed;
  75. debug_printf ("Self test failed: %s\n", ex.what ());
  76. }
  77. reset ();
  78. }
  79. debug_printf (_("Ran %d unit tests, %d failed\n"),
  80. ran, failed);
  81. }
  82. /* See selftest.h. */
  83. void for_each_selftest (for_each_selftest_ftype func)
  84. {
  85. for (const auto &pair : tests)
  86. func (pair.first);
  87. }
  88. } // namespace selftests