Advertisement
RobertDeMilo

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

Nov 3rd, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. #include <map>
  8. #include <cmath>
  9. #include <cassert>
  10.  
  11. using namespace std;
  12.  
  13. /*
  14.    Подставьте сюда вашу реализацию макросов
  15.    ASSERT, ASSERT_EQUAL, ASSERT_EQUAL_HINT, ASSERT_HINT и RUN_TEST
  16. */
  17.  
  18. template <typename T, typename U>
  19. ostream& operator<<(ostream& out, const pair<T, U>& container)
  20. {
  21.     return out << container.first << ": " << container.second;
  22. }
  23.  
  24. template <typename T>
  25. void Print(ostream& out, const T& container)
  26. {
  27.     bool is_first = true;
  28.     for (const auto& element : container)
  29.     {
  30.         if (is_first)
  31.         {
  32.             out << element;
  33.             is_first = false;
  34.         }
  35.         else
  36.         {
  37.             out << ", "s << element;
  38.         }
  39.     }
  40. }
  41.  
  42. template <typename T>
  43. ostream& operator<<(ostream& out, const vector<T>& container)
  44. {
  45.     out << "["s;
  46.     Print(out, container);
  47.     out << "]"s;
  48.     return out;
  49. }
  50.  
  51. template <typename T>
  52. ostream& operator<<(ostream& out, const set<T>& container)
  53. {
  54.     out << "{"s;
  55.     Print(out, container);
  56.     out << "}"s;
  57.     return out;
  58. }
  59.  
  60. template <typename T, typename U>
  61. ostream& operator<<(ostream& out, const map<T, U>& container)
  62. {
  63.     out << "{"s;
  64.     Print(out, container);
  65.     out << "}"s;
  66.     return out;
  67. }
  68.  
  69. template <typename T, typename U>
  70. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  71.     const string& func, unsigned line, const string& hint)
  72. {
  73.     if (t != u)
  74.     {
  75.         cerr << boolalpha;
  76.         cerr << file << "("s << line << "): "s << func << ": "s;
  77.         cerr << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  78.         cerr << t << " != "s << u << "."s;
  79.         if (!hint.empty())
  80.         {
  81.             cerr << " Hint: "s << hint;
  82.         }
  83.         cerr << endl;
  84.         abort();
  85.     }
  86. }
  87.  
  88. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  89.     const string& hint)
  90. {
  91.     if (!value)
  92.     {
  93.         cerr << file << "("s << line << "): "s << func << ": "s;
  94.         cerr << "ASSERT("s << expr_str << ") failed."s;
  95.         if (!hint.empty())
  96.         {
  97.             cerr << " Hint: "s << hint;
  98.         }
  99.         cerr << endl;
  100.         abort();
  101.     }
  102. }
  103.  
  104. template <typename T>
  105. void RunTestImpl(T func, const string& func_str)
  106. {
  107.     func();
  108.     cerr << func_str << " OK" << endl;
  109. }
  110.  
  111. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  112.  
  113. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  114.  
  115. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  116.  
  117. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  118.  
  119. #define RUN_TEST(func) RunTestImpl((func), #func)
  120.  
  121. // -------- Начало модульных тестов ----------
  122.  
  123. void Test1()
  124. {
  125.     ///  
  126. }
  127. void Test2()
  128. {
  129.     ///
  130. }
  131.  
  132.  // Функция TestSearchServer является точкой входа для запуска тестов
  133.  
  134. void TestSearchServer()
  135. {
  136.     RUN_TEST(Test1);
  137.     RUN_TEST(Test2);
  138.    
  139. }
  140.  //--------- Окончание модульных тестов -----------
  141.  
  142. int main()
  143. {
  144.     TestSearchServer();
  145.  
  146.     ///
  147.  
  148.     return 0;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement