O_Egor

test_runner.h

Oct 15th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include <set>
  5. #include <exception>
  6. #include <iostream>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. template <typename T>
  12. ostream& operator<<(ostream& os, const vector <T>& v);
  13.  
  14. template <typename K, typename V>
  15. ostream& operator<<(ostream& os, const map <K, V>& mp);
  16.  
  17. template <typename T>
  18. ostream& operator<<(ostream& os, set <T> st);
  19.  
  20. template <typename T, typename U>
  21. void AssertEqual(const T& t, const U& u, const string& hint = {});
  22.  
  23. void Assert(bool b, const string& hint);
  24.  
  25. class TestRunner
  26. {
  27. public:
  28.     template <typename FuncName>
  29.     void RunTest(const FuncName& func, const string& testName);
  30.  
  31.     ~TestRunner();
  32. private:
  33.     int failedTests = 0;
  34. };
  35.  
  36. //================================ Implementations ======================================
  37. template <typename FuncName>
  38. void TestRunner::RunTest(const FuncName& func, const string& testName)
  39. {
  40.     try
  41.     {
  42.         func();
  43.         cerr << testName << ": OK\n";
  44.     }
  45.     catch (const runtime_error& re)
  46.     {
  47.         ++failedTests;
  48.         cerr << testName << ": FAILED!!! " << re.what() << '\n';
  49.     }
  50.     catch (const exception& ex)
  51.     {
  52.         ++failedTests;
  53.         cerr << "Unknown exception!!! : " << ex.what() << "\n";
  54.     }
  55. }
  56.  
  57. template <typename T, typename U>
  58. void AssertEqual(const T& t, const U& u, const string& hint)
  59. {
  60.     if (t != u)
  61.     {
  62.         ostringstream os;
  63.         os << t << " != " << u;
  64.         if (!hint.empty())
  65.         {
  66.             os << " hint:" << hint;
  67.         }
  68.         throw runtime_error(os.str());
  69.     }
  70. }
  71.  
  72. template <typename K, typename V>
  73. ostream& operator<<(ostream& os, const map <K, V>& mp)
  74. {
  75.     os << "{";
  76.     bool first = 1;
  77.     for (const auto& [key, value] : mp)
  78.     {
  79.         if (!first)
  80.         {
  81.             os << ", ";
  82.         }
  83.         first = 0;
  84.         os << key << ": " << value;
  85.     }
  86.     return os << '}';
  87. }
  88.  
  89. template <typename T>
  90. ostream& operator<<(ostream& os, set <T> st)
  91. {
  92.     os << "{";
  93.     bool first = 1;
  94.     for (const auto& i : st)
  95.     {
  96.         if (!first)
  97.             os << ", ";
  98.         os << i;
  99.     }
  100.     os << '}';
  101. }
  102.  
  103. template <typename T>
  104. ostream& operator<<(ostream& os, const vector <T>& v)
  105. {
  106.     os << "[";
  107.     bool first = 1;
  108.     for (const auto& i : v)
  109.     {
  110.         if (!first)
  111.         {
  112.             os << ", ";
  113.         }
  114.         first = 0;
  115.         os << i;
  116.     }
  117.     return os << "]";
  118. }
Advertisement
Add Comment
Please, Sign In to add comment