Advertisement
RobertDeMilo

Юнит - тесты и профайлер

Nov 17th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. test_runner.h
  2.  
  3. #pragma once
  4.  
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cstdlib>
  8. #include <map>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12.  
  13. using namespace std;
  14.  
  15. //*********************************************************************************************************
  16. template <typename First, typename Second>
  17. ostream& operator<<(ostream& out, const pair<First, Second>& p)
  18. {
  19.     return out << p.first << ": "s << p.second;
  20. }
  21. //*********************************************************************************************************
  22. template <typename Container>
  23. void Print(ostream& out, const Container& container)
  24. {
  25.     bool is_first = true;
  26.     for (const auto& element : container)
  27.     {
  28.         if (!is_first)
  29.         {
  30.             out << ", "s;
  31.         }
  32.         is_first = false;
  33.         out << element;
  34.     }
  35. }
  36. //*********************************************************************************************************
  37. template <typename Element>
  38. ostream& operator<<(ostream& out, const vector<Element>& container)
  39. {
  40.     out << '[';
  41.     Print(out, container);
  42.     out << ']';
  43.     return out;
  44. }
  45. //*********************************************************************************************************
  46. template <typename Element>
  47. ostream& operator<<(ostream& out, const set<Element>& container)
  48. {
  49.     out << '{';
  50.     Print(out, container);
  51.     out << '}';
  52.     return out;
  53. }
  54. //*********************************************************************************************************
  55. template <typename Key, typename Value>
  56. ostream& operator<<(ostream& out, const map<Key, Value>& container)
  57. {
  58.     out << '{';
  59.     Print(out, container);
  60.     out << '}';
  61.     return out;
  62. }
  63. //###################################################################################################################
  64. template <typename T, typename U>
  65. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  66.     const string& func, unsigned line, const string& hint)
  67. {
  68.     if (t != u)
  69.     {
  70.         cout << boolalpha;
  71.         cout << file << "("s << line << "): "s << func << ": "s;
  72.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  73.         cout << t << " != "s << u << "."s;
  74.         if (!hint.empty())
  75.         {
  76.             cout << " Hint: "s << hint;
  77.         }
  78.         cout << endl;
  79.         abort();
  80.     }
  81. }
  82.  
  83. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  84.     const string& hint)
  85. {
  86.     if (!value)
  87.     {
  88.         cout << file << "("s << line << "): "s << func << ": "s;
  89.         cout << "ASSERT("s << expr_str << ") failed."s;
  90.         if (!hint.empty())
  91.         {
  92.             cout << " Hint: "s << hint;
  93.         }
  94.         cout << endl;
  95.         abort();
  96.     }
  97. }
  98. //#####################################################################################################################
  99. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  100.  
  101. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  102.  
  103. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  104.  
  105. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  106.  
  107. #define RUN_TEST(tr,func) tr.RunTest(func, #func)
  108. //####################################################################################################################
  109.  
  110. class TestRunner
  111. {
  112. public:
  113.  
  114.     template <class TestFunc>
  115.     void RunTest(TestFunc func, const string& test_name)
  116.     {
  117.         try
  118.         {
  119.             func();
  120.             cerr << test_name << " OK" << endl;
  121.         }
  122.         catch (runtime_error& e)
  123.         {
  124.             ++fail_count;
  125.             cerr << test_name << " fail: " << e.what() << endl;
  126.         }
  127.     }
  128.     ~TestRunner()
  129.     {
  130.         if (fail_count > 0)
  131.         {
  132.             cerr << fail_count << " tests failed. Terminate";
  133.             exit(1);
  134.         }
  135.     }
  136. private:
  137.  
  138.     int fail_count = 0;
  139. };
  140. #################################################################################################################
  141. profile.h
  142.  
  143. #pragma once
  144.  
  145. #include <iostream>
  146. #include <chrono>
  147.  
  148. using namespace std;
  149. using namespace std::chrono;
  150.  
  151. class LogDuration
  152. {
  153. public:
  154.     explicit LogDuration(const string& msg = "") : message(msg + ": "), start(steady_clock::now())
  155.     {
  156.  
  157.     }
  158.     ~LogDuration()
  159.     {
  160.         auto finish = steady_clock::now();
  161.         auto dur = finish - start;
  162.         cerr << message << duration_cast<milliseconds>(dur).count() << " ms" << endl;
  163.     }
  164. private:
  165.     string message;
  166.     steady_clock::time_point start;
  167. };
  168.  
  169. #define UNIQ_ID_IMPL(lineno) _a_local_var_##lineno
  170. #define UNIQ_ID(lineno) UNIQ_ID_IMPL(lineno)
  171. #define LOG_DURATION(message) LogDuration UNIQ_ID(__LINE__) { message };
  172.  
  173. #################################################################################################################
  174. main
  175.  
  176. #include <iostream>
  177. #include <fstream>
  178. #include <vector>
  179. #include <set>
  180.  
  181. #include "profile.h"
  182. #include "test_runner.h"
  183.  
  184. using namespace std;
  185.  
  186. void Test1()
  187. {
  188.     ///  
  189.     ASSERT(1,1);
  190.     ASSERT_EQUAL(1, 1);
  191. }
  192. void Test2()
  193. {
  194.     ///
  195.     ASSERT(1, 1);
  196.     ASSERT_EQUAL(1, 1);
  197. }
  198.  
  199. // Функция TestAll() является точкой входа для запуска тестов
  200.  
  201. void TestAll()
  202. {
  203.     TestRunner tr;
  204.     RUN_TEST(tr,Test1);
  205.     RUN_TEST(tr,Test2);
  206. }
  207.  
  208. int main()
  209. {
  210.     TestAll();
  211.  
  212.     LOG_DURATION("Total");
  213.  
  214.     ifstream in("input.txt");
  215.     int element_count;
  216.     in >> element_count;
  217.  
  218.     set<int> elements;
  219.    
  220.     {
  221.         LOG_DURATION("Input");
  222.  
  223.         for (int i = 0; i < element_count; ++i)
  224.         {
  225.             int x;
  226.             in >> x;
  227.             elements.insert(x);
  228.         }
  229.     }
  230.    
  231.     int query_count;
  232.     in >> query_count;
  233.  
  234.     int total_found = 0;
  235.  
  236.     {
  237.         LOG_DURATION("Queries processing");
  238.  
  239.         for (int i = 0; i < query_count; ++i)
  240.         {
  241.             int x;
  242.             in >> x;
  243.  
  244.             if (elements.find(x) != elements.end())
  245.             {
  246.                 ++total_found;
  247.             }
  248.         }
  249.     }
  250.  
  251.     cout << total_found << endl;
  252.  
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement