TestSuite.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/TestResult.h>
  11. #include <LibTest/TestSuite.h>
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <sys/time.h>
  15. namespace Test {
  16. TestSuite* TestSuite::s_global = nullptr;
  17. class TestElapsedTimer {
  18. public:
  19. TestElapsedTimer() { restart(); }
  20. void restart() { gettimeofday(&m_started, nullptr); }
  21. u64 elapsed_milliseconds()
  22. {
  23. struct timeval now = {};
  24. gettimeofday(&now, nullptr);
  25. struct timeval delta = {};
  26. timersub(&now, &m_started, &delta);
  27. return delta.tv_sec * 1000 + delta.tv_usec / 1000;
  28. }
  29. private:
  30. struct timeval m_started = {};
  31. };
  32. // Declared in Macros.h
  33. TestResult current_test_result()
  34. {
  35. return TestSuite::the().current_test_result();
  36. }
  37. // Declared in Macros.h
  38. void set_current_test_result(TestResult result)
  39. {
  40. TestSuite::the().set_current_test_result(result);
  41. }
  42. // Declared in Macros.h
  43. void set_randomness_source(Randomized::RandomnessSource source)
  44. {
  45. TestSuite::the().set_randomness_source(move(source));
  46. }
  47. // Declared in Macros.h
  48. Randomized::RandomnessSource& randomness_source()
  49. {
  50. return TestSuite::the().randomness_source();
  51. }
  52. // Declared in TestCase.h
  53. void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case)
  54. {
  55. TestSuite::the().add_case(test_case);
  56. }
  57. // Declared in TestCase.h
  58. void set_suite_setup_function(Function<void()> setup)
  59. {
  60. TestSuite::the().set_suite_setup(move(setup));
  61. }
  62. // Declared in Macros.h
  63. bool is_reporting_enabled()
  64. {
  65. return TestSuite::the().is_reporting_enabled();
  66. }
  67. // Declared in Macros.h
  68. void enable_reporting()
  69. {
  70. TestSuite::the().enable_reporting();
  71. }
  72. // Declared in Macros.h
  73. void disable_reporting()
  74. {
  75. TestSuite::the().disable_reporting();
  76. }
  77. static DeprecatedString test_result_to_string(TestResult result)
  78. {
  79. switch (result) {
  80. case TestResult::NotRun:
  81. return "Not run";
  82. case TestResult::Passed:
  83. return "Completed";
  84. case TestResult::Failed:
  85. return "Failed";
  86. case TestResult::Rejected:
  87. return "Rejected";
  88. case TestResult::Overrun:
  89. return "Ran out of randomness";
  90. default:
  91. return "Unknown TestResult";
  92. }
  93. }
  94. int TestSuite::main(DeprecatedString const& suite_name, Span<StringView> arguments)
  95. {
  96. m_suite_name = suite_name;
  97. Core::ArgsParser args_parser;
  98. bool do_tests_only = getenv("TESTS_ONLY") != nullptr;
  99. bool do_benchmarks_only = false;
  100. bool do_list_cases = false;
  101. StringView search_string = "*"sv;
  102. args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0);
  103. args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0);
  104. args_parser.add_option(m_benchmark_repetitions, "Number of times to repeat each benchmark (default 1)", "benchmark_repetitions", 0, "N");
  105. args_parser.add_option(do_list_cases, "List available test cases.", "list", 0);
  106. args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
  107. args_parser.parse(arguments);
  108. if (m_setup)
  109. m_setup();
  110. auto const& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
  111. if (do_list_cases) {
  112. outln("Available cases for {}:", suite_name);
  113. for (auto const& test : matching_tests) {
  114. outln(" {}", test->name());
  115. }
  116. return 0;
  117. }
  118. outln("Running {} cases out of {}.", matching_tests.size(), m_cases.size());
  119. return run(matching_tests);
  120. }
  121. Vector<NonnullRefPtr<TestCase>> TestSuite::find_cases(DeprecatedString const& search, bool find_tests, bool find_benchmarks)
  122. {
  123. Vector<NonnullRefPtr<TestCase>> matches;
  124. for (auto& t : m_cases) {
  125. if (!search.is_empty() && !t->name().matches(search, CaseSensitivity::CaseInsensitive)) {
  126. continue;
  127. }
  128. if (!find_tests && !t->is_benchmark()) {
  129. continue;
  130. }
  131. if (!find_benchmarks && t->is_benchmark()) {
  132. continue;
  133. }
  134. matches.append(t);
  135. }
  136. return matches;
  137. }
  138. int TestSuite::run(Vector<NonnullRefPtr<TestCase>> const& tests)
  139. {
  140. size_t test_count = 0;
  141. size_t test_passed_count = 0;
  142. size_t test_failed_count = 0;
  143. size_t benchmark_count = 0;
  144. size_t benchmark_passed_count = 0;
  145. size_t benchmark_failed_count = 0;
  146. TestElapsedTimer global_timer;
  147. for (auto const& t : tests) {
  148. auto const test_type = t->is_benchmark() ? "benchmark" : "test";
  149. auto const repetitions = t->is_benchmark() ? m_benchmark_repetitions : 1;
  150. warnln("Running {} '{}'.", test_type, t->name());
  151. m_current_test_result = TestResult::NotRun;
  152. enable_reporting();
  153. u64 total_time = 0;
  154. u64 sum_of_squared_times = 0;
  155. u64 min_time = NumericLimits<u64>::max();
  156. u64 max_time = 0;
  157. for (u64 i = 0; i < repetitions; ++i) {
  158. TestElapsedTimer timer;
  159. t->func()();
  160. auto const iteration_time = timer.elapsed_milliseconds();
  161. total_time += iteration_time;
  162. sum_of_squared_times += iteration_time * iteration_time;
  163. min_time = min(min_time, iteration_time);
  164. max_time = max(max_time, iteration_time);
  165. // Non-randomized tests don't touch the test result when passing.
  166. if (m_current_test_result == TestResult::NotRun)
  167. m_current_test_result = TestResult::Passed;
  168. }
  169. if (repetitions != 1) {
  170. double average = total_time / double(repetitions);
  171. double average_squared = average * average;
  172. double standard_deviation = sqrt((sum_of_squared_times + repetitions * average_squared - 2 * total_time * average) / (repetitions - 1));
  173. dbgln("{} {} '{}' on average in {:.1f}±{:.1f}ms (min={}ms, max={}ms, total={}ms)",
  174. test_result_to_string(m_current_test_result), test_type, t->name(),
  175. average, standard_deviation, min_time, max_time, total_time);
  176. } else {
  177. dbgln("{} {} '{}' in {}ms", test_result_to_string(m_current_test_result), test_type, t->name(), total_time);
  178. }
  179. if (t->is_benchmark()) {
  180. m_benchtime += total_time;
  181. benchmark_count++;
  182. switch (m_current_test_result) {
  183. case TestResult::Passed:
  184. benchmark_passed_count++;
  185. break;
  186. case TestResult::Failed:
  187. benchmark_failed_count++;
  188. break;
  189. default:
  190. break;
  191. }
  192. } else {
  193. m_testtime += total_time;
  194. test_count++;
  195. switch (m_current_test_result) {
  196. case TestResult::Passed:
  197. test_passed_count++;
  198. break;
  199. case TestResult::Failed:
  200. test_failed_count++;
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. }
  207. dbgln("Finished {} tests and {} benchmarks in {}ms ({}ms tests, {}ms benchmarks, {}ms other).",
  208. test_count,
  209. benchmark_count,
  210. global_timer.elapsed_milliseconds(),
  211. m_testtime,
  212. m_benchtime,
  213. global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime));
  214. if (test_count != 0) {
  215. if (test_passed_count == test_count) {
  216. dbgln("All {} tests passed.", test_count);
  217. } else if (test_passed_count + test_failed_count == test_count) {
  218. dbgln("Out of {} tests, {} passed and {} failed.", test_count, test_passed_count, test_failed_count);
  219. } else {
  220. dbgln("Out of {} tests, {} passed, {} failed and {} didn't finish for other reasons.", test_count, test_passed_count, test_failed_count, test_count - test_passed_count - test_failed_count);
  221. }
  222. }
  223. if (benchmark_count != 0) {
  224. if (benchmark_passed_count == benchmark_count) {
  225. dbgln("All {} benchmarks passed.", benchmark_count);
  226. } else if (benchmark_passed_count + benchmark_failed_count == benchmark_count) {
  227. dbgln("Out of {} benchmarks, {} passed and {} failed.", benchmark_count, benchmark_passed_count, benchmark_failed_count);
  228. } else {
  229. dbgln("Out of {} benchmarks, {} passed, {} failed and {} didn't finish for other reasons.", benchmark_count, benchmark_passed_count, benchmark_failed_count, benchmark_count - benchmark_passed_count - benchmark_failed_count);
  230. }
  231. }
  232. // We have multiple TestResults, all except for Passed being "bad".
  233. // Let's get a count of them:
  234. return (int)(test_count - test_passed_count + benchmark_count - benchmark_passed_count);
  235. }
  236. } // namespace Test