TestSuite.cpp 4.3 KB

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