Advertisement
RobertDeMilo

Ассерты и юнит-тесты2

Nov 3rd, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. //*********************************************************************************************************
  12. template <typename First, typename Second>
  13. ostream& operator<<(ostream& out, const pair<First, Second>& p)
  14. {
  15.     return out << p.first << ": "s << p.second;
  16. }
  17. //*********************************************************************************************************
  18. template <typename Container>
  19. void Print(ostream& out, const Container& container)
  20. {
  21.     bool is_first = true;
  22.     for (const auto& element : container)
  23.     {
  24.         if (!is_first)
  25.         {
  26.             out << ", "s;
  27.         }
  28.         is_first = false;
  29.         out << element;
  30.     }
  31. }
  32. //*********************************************************************************************************
  33. template <typename Element>
  34. ostream& operator<<(ostream& out, const vector<Element>& container)
  35. {
  36.     out << '[';
  37.     Print(out, container);
  38.     out << ']';
  39.     return out;
  40. }
  41. //*********************************************************************************************************
  42. template <typename Element>
  43. ostream& operator<<(ostream& out, const set<Element>& container)
  44. {
  45.     out << '{';
  46.     Print(out, container);
  47.     out << '}';
  48.     return out;
  49. }
  50. //*********************************************************************************************************
  51. template <typename Key, typename Value>
  52. ostream& operator<<(ostream& out, const map<Key, Value>& container)
  53. {
  54.     out << '{';
  55.     Print(out, container);
  56.     out << '}';
  57.     return out;
  58. }
  59. //####################################################################################################################
  60. template <typename T, typename U>
  61. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  62.     const string& func, unsigned line, const string& hint)
  63. {
  64.     if (t != u)
  65.     {
  66.         cout << boolalpha;
  67.         cout << file << "("s << line << "): "s << func << ": "s;
  68.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  69.         cout << t << " != "s << u << "."s;
  70.         if (!hint.empty())
  71.         {
  72.             cout << " Hint: "s << hint;
  73.         }
  74.         cout << endl;
  75.         abort();
  76.     }
  77. }
  78.  
  79. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  80.     const string& hint)
  81. {
  82.     if (!value)
  83.     {
  84.         cout << file << "("s << line << "): "s << func << ": "s;
  85.         cout << "ASSERT("s << expr_str << ") failed."s;
  86.         if (!hint.empty())
  87.         {
  88.             cout << " Hint: "s << hint;
  89.         }
  90.         cout << endl;
  91.         abort();
  92.     }
  93. }
  94.  
  95. //#####################################################################################################################
  96. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  97.  
  98. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  99.  
  100. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  101.  
  102. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  103. //#####################################################################################################################
  104.  
  105. class TestRunner
  106. {
  107. public:
  108.  
  109.     template <class TestFunc>
  110.     void RunTest(TestFunc func, const string& test_name)
  111.     {
  112.         try
  113.         {
  114.             func();
  115.             cerr << test_name << " OK" << endl;
  116.         }
  117.         catch (runtime_error& e)
  118.         {
  119.             ++fail_count;
  120.             cerr << test_name << " fail: " << e.what() << endl;
  121.         }
  122.  
  123.     }
  124.     ~TestRunner()
  125.     {
  126.         if (fail_count > 0)
  127.         {
  128.             cerr << fail_count << " tests failed. Terminate";
  129.             exit(1);
  130.         }
  131.     }
  132. private:
  133.  
  134.     int fail_count = 0;
  135. };
  136. void Test1()
  137. {
  138.     ///
  139. }
  140. void Test2()
  141. {
  142.     ///
  143. }
  144. void Test3()
  145. {
  146.     ///
  147. }
  148. void TestAll()
  149. {
  150.     TestRunner tr;
  151.  
  152.     tr.RunTest(Test1, "Test1");
  153.     tr.RunTest(Test2, "Test2");
  154.     tr.RunTest(Test3, "Test3");
  155. }
  156.  
  157. int main()
  158. {
  159.     TestAll();
  160.  
  161.     ///
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement