TestSuite.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibTest/Macros.h> // intentionally first -- we redefine VERIFY and friends in here
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibTest/TestSuite.h>
  10. #include <stdlib.h>
  11. #include <sys/time.h>
  12. namespace Test {
  13. TestSuite* TestSuite::s_global = nullptr;
  14. class TestElapsedTimer {
  15. public:
  16. TestElapsedTimer() { restart(); }
  17. void restart() { gettimeofday(&m_started, nullptr); }
  18. u64 elapsed_milliseconds()
  19. {
  20. struct timeval now = {};
  21. gettimeofday(&now, nullptr);
  22. struct timeval delta = {};
  23. timersub(&now, &m_started, &delta);
  24. return delta.tv_sec * 1000 + delta.tv_usec / 1000;
  25. }
  26. private:
  27. struct timeval m_started = {};
  28. };
  29. // Declared in Macros.h
  30. void current_test_case_did_fail()
  31. {
  32. TestSuite::the().current_test_case_did_fail();
  33. }
  34. // Declared in TestCase.h
  35. void add_test_case_to_suite(const NonnullRefPtr<TestCase>& test_case)
  36. {
  37. TestSuite::the().add_case(test_case);
  38. }
  39. int TestSuite::main(const String& suite_name, int argc, char** argv)
  40. {
  41. m_suite_name = suite_name;
  42. Core::ArgsParser args_parser;
  43. bool do_tests_only = getenv("TESTS_ONLY") != nullptr;
  44. bool do_benchmarks_only = false;
  45. bool do_list_cases = false;
  46. const char* search_string = "*";
  47. args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0);
  48. args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0);
  49. args_parser.add_option(do_list_cases, "List available test cases.", "list", 0);
  50. args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
  51. args_parser.parse(argc, argv);
  52. const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
  53. if (do_list_cases) {
  54. outln("Available cases for {}:", suite_name);
  55. for (const auto& test : matching_tests) {
  56. outln(" {}", test.name());
  57. }
  58. return 0;
  59. }
  60. outln("Running {} cases out of {}.", matching_tests.size(), m_cases.size());
  61. return run(matching_tests);
  62. }
  63. NonnullRefPtrVector<TestCase> TestSuite::find_cases(const String& search, bool find_tests, bool find_benchmarks)
  64. {
  65. NonnullRefPtrVector<TestCase> matches;
  66. for (const auto& t : m_cases) {
  67. if (!search.is_empty() && !t.name().matches(search, CaseSensitivity::CaseInsensitive)) {
  68. continue;
  69. }
  70. if (!find_tests && !t.is_benchmark()) {
  71. continue;
  72. }
  73. if (!find_benchmarks && t.is_benchmark()) {
  74. continue;
  75. }
  76. matches.append(t);
  77. }
  78. return matches;
  79. }
  80. int TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
  81. {
  82. size_t test_count = 0;
  83. size_t test_failed_count = 0;
  84. size_t benchmark_count = 0;
  85. TestElapsedTimer global_timer;
  86. for (const auto& t : tests) {
  87. const auto test_type = t.is_benchmark() ? "benchmark" : "test";
  88. warnln("Running {} '{}'.", test_type, t.name());
  89. m_current_test_case_passed = true;
  90. TestElapsedTimer timer;
  91. t.func()();
  92. const auto time = timer.elapsed_milliseconds();
  93. dbgln("{} {} '{}' in {}ms", m_current_test_case_passed ? "Completed" : "Failed", test_type, t.name(), time);
  94. if (t.is_benchmark()) {
  95. m_benchtime += time;
  96. benchmark_count++;
  97. } else {
  98. m_testtime += time;
  99. test_count++;
  100. }
  101. if (!m_current_test_case_passed) {
  102. test_failed_count++;
  103. }
  104. }
  105. dbgln("Finished {} tests and {} benchmarks in {}ms ({}ms tests, {}ms benchmarks, {}ms other).",
  106. test_count,
  107. benchmark_count,
  108. global_timer.elapsed_milliseconds(),
  109. m_testtime,
  110. m_benchtime,
  111. global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime));
  112. dbgln("Out of {} tests, {} passed and {} failed.", test_count, test_count - test_failed_count, test_failed_count);
  113. return (int)test_failed_count;
  114. }
  115. }