1
0

testing.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #pragma once
  2. #include "common.h"
  3. #include <chrono>
  4. #include <exception>
  5. #include <iostream>
  6. #include <string>
  7. #include <regex>
  8. #include <vector>
  9. struct testing {
  10. std::ostream &out;
  11. std::vector<std::string> stack;
  12. std::regex filter;
  13. bool filter_tests = false;
  14. bool throw_exception = false;
  15. bool verbose = false;
  16. int tests = 0;
  17. int assertions = 0;
  18. int failures = 0;
  19. int unnamed = 0;
  20. int exceptions = 0;
  21. static constexpr std::size_t status_column = 80;
  22. explicit testing(std::ostream &os = std::cout) : out(os) {}
  23. std::string indent() const {
  24. if (stack.empty()) {
  25. return "";
  26. }
  27. return std::string((stack.size() - 1) * 2, ' ');
  28. }
  29. std::string full_name() const {
  30. return string_join(stack, ".");
  31. }
  32. void log(const std::string & msg) {
  33. if (verbose) {
  34. out << indent() << " " << msg << "\n";
  35. }
  36. }
  37. void set_filter(const std::string & re) {
  38. filter = std::regex(re);
  39. filter_tests = true;
  40. }
  41. bool should_run() const {
  42. if (filter_tests) {
  43. if (!std::regex_match(full_name(), filter)) {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. template <typename F>
  50. void run_with_exceptions(F &&f, const char *ctx) {
  51. try {
  52. f();
  53. } catch (const std::exception &e) {
  54. ++failures;
  55. ++exceptions;
  56. out << indent() << "UNHANDLED EXCEPTION (" << ctx << "): " << e.what() << "\n";
  57. if (throw_exception) {
  58. throw;
  59. }
  60. } catch (...) {
  61. ++failures;
  62. ++exceptions;
  63. out << indent() << "UNHANDLED EXCEPTION (" << ctx << "): unknown\n";
  64. if (throw_exception) {
  65. throw;
  66. }
  67. }
  68. }
  69. void print_result(const std::string &label, int new_failures, int new_assertions, const std::string &extra = "") const {
  70. std::string line = indent() + label;
  71. std::string details;
  72. if (new_assertions > 0) {
  73. if (new_failures == 0) {
  74. details = std::to_string(new_assertions) + " assertion(s)";
  75. } else {
  76. details = std::to_string(new_failures) + " of " +
  77. std::to_string(new_assertions) + " assertion(s) failed";
  78. }
  79. }
  80. if (!extra.empty()) {
  81. if (!details.empty()) {
  82. details += ", ";
  83. }
  84. details += extra;
  85. }
  86. if (!details.empty()) {
  87. line += " (" + details + ")";
  88. }
  89. std::string status = (new_failures == 0) ? "[PASS]" : "[FAIL]";
  90. if (line.size() + 1 < status_column) {
  91. line.append(status_column - line.size(), ' ');
  92. } else {
  93. line.push_back(' ');
  94. }
  95. out << line << status << "\n";
  96. }
  97. template <typename F>
  98. void test(const std::string &name, F f) {
  99. stack.push_back(name);
  100. if (!should_run()) {
  101. stack.pop_back();
  102. return;
  103. }
  104. ++tests;
  105. out << indent() << name << "\n";
  106. int before_failures = failures;
  107. int before_assertions = assertions;
  108. run_with_exceptions([&] { f(*this); }, "test");
  109. int new_failures = failures - before_failures;
  110. int new_assertions = assertions - before_assertions;
  111. print_result(name, new_failures, new_assertions);
  112. stack.pop_back();
  113. }
  114. template <typename F>
  115. void test(F f) {
  116. test("test #" + std::to_string(++unnamed), f);
  117. }
  118. template <typename F>
  119. void bench(const std::string &name, F f, int iterations = 100) {
  120. stack.push_back(name);
  121. if (!should_run()) {
  122. stack.pop_back();
  123. return;
  124. }
  125. ++tests;
  126. out << indent() << "[bench] " << name << "\n";
  127. int before_failures = failures;
  128. int before_assertions = assertions;
  129. using clock = std::chrono::high_resolution_clock;
  130. std::chrono::microseconds duration(0);
  131. run_with_exceptions([&] {
  132. for (auto i = 0; i < iterations; i++) {
  133. auto start = clock::now();
  134. f();
  135. duration += std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - start);
  136. }
  137. }, "bench");
  138. auto avg_elapsed = duration.count() / iterations;
  139. auto avg_elapsed_s = std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() / iterations;
  140. auto rate = (avg_elapsed_s > 0.0) ? (1.0 / avg_elapsed_s) : 0.0;
  141. int new_failures = failures - before_failures;
  142. int new_assertions = assertions - before_assertions;
  143. std::string extra =
  144. "n=" + std::to_string(iterations) +
  145. " avg=" + std::to_string(avg_elapsed) + "us" +
  146. " rate=" + std::to_string(int(rate)) + "/s";
  147. print_result("[bench] " + name, new_failures, new_assertions, extra);
  148. stack.pop_back();
  149. }
  150. template <typename F>
  151. void bench(F f, int iterations = 100) {
  152. bench("bench #" + std::to_string(++unnamed), f, iterations);
  153. }
  154. // Assertions
  155. bool assert_true(bool cond) {
  156. return assert_true("", cond);
  157. }
  158. bool assert_true(const std::string &msg, bool cond) {
  159. ++assertions;
  160. if (!cond) {
  161. ++failures;
  162. out << indent() << "ASSERT TRUE FAILED";
  163. if (!msg.empty()) {
  164. out << " : " << msg;
  165. }
  166. out << "\n";
  167. return false;
  168. }
  169. return true;
  170. }
  171. template <typename A, typename B>
  172. bool assert_equal(const A &expected, const B &actual) {
  173. return assert_equal("", expected, actual);
  174. }
  175. template <typename A, typename B>
  176. bool assert_equal(const std::string &msg, const A &expected, const B &actual) {
  177. ++assertions;
  178. if (!(actual == expected)) {
  179. ++failures;
  180. out << indent() << "ASSERT EQUAL FAILED";
  181. if (!msg.empty()) {
  182. out << " : " << msg;
  183. }
  184. out << "\n";
  185. out << indent() << " expected: " << expected << "\n";
  186. out << indent() << " actual : " << actual << "\n";
  187. return false;
  188. }
  189. return true;
  190. }
  191. // Print summary and return an exit code
  192. int summary() const {
  193. out << "\n";
  194. out << "tests : " << tests << "\n";
  195. out << "assertions : " << assertions << "\n";
  196. out << "failures : " << failures << "\n";
  197. out << "exceptions : " << exceptions << "\n";
  198. return failures == 0 ? 0 : 1;
  199. }
  200. };