Advertisement
RobertDeMilo

YB2.10 Обеспечиваем регулярный запуск юнит тестов

Oct 28th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<exception>
  3. #include<sstream>
  4. #include<string>
  5. #include<map>
  6. #include<set>
  7.  
  8. using namespace std;
  9.  
  10. using SYNONYMS = map<string, set<string>>;
  11.  
  12. //**********************************************************************************************************
  13.  
  14. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  15. {
  16.     synonyms[second_word].insert(first_word);
  17.     synonyms[first_word].insert(second_word);
  18.     //synonyms[first_word].insert(first_word);
  19. }
  20.  
  21. //**********************************************************************************************************
  22.  
  23. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
  24. {
  25.     return synonyms[first_word].size();
  26. }
  27.  
  28. //**********************************************************************************************************
  29.  
  30. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  31. {
  32.     return synonyms[first_word].count(second_word) == 1;
  33. }
  34.  
  35. //**********************************************************************************************************
  36. //##########################################################################################################
  37.  
  38. template<class T>
  39. ostream& operator<<(ostream& os, const set<T>& s)
  40. {
  41.     os << "{";
  42.     bool first = true;
  43.  
  44.     for (const auto& x : s)
  45.     {
  46.         if (!first)
  47.         {
  48.             os << ", ";
  49.         }
  50.         first = false;
  51.         os << x;
  52.     }
  53.     return os << "}";
  54. }
  55.  
  56. template <class K, class V>
  57. ostream& operator<<(ostream& os, const map<K, V>& m)
  58. {
  59.     os << "{";
  60.     bool first = true;
  61.     for (const auto& kv : m)
  62.     {
  63.         if (!first)
  64.         {
  65.             os << ", ";
  66.         }
  67.         first = false;
  68.         os << kv.first << ": " << kv.second;
  69.     }
  70.     return os << "}";
  71. }
  72.  
  73. //##########################################################################################################
  74. template<class T, class U>
  75. void AssertEqual(const T& t, const U& u, const string& hint)
  76. {
  77.     if (t != u)
  78.     {
  79.         ostringstream os;
  80.         os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
  81.         throw runtime_error(os.str());
  82.     }
  83. }
  84.  
  85.  
  86. void Assert(bool b, const string& hint)
  87. {
  88.     AssertEqual(b, true, hint);
  89. }
  90.  
  91. //##########################################################################################################
  92. //**********************************************************************************************************
  93.  
  94. void TESTADDSYNONYMS()
  95. {
  96.     {
  97.         map<string, set<string>> empty;
  98.         ADDSYNONYMS(empty, "a", "b");
  99.         const map<string, set<string>> expected = { {"a",{"b"}},
  100.                                                     {"b",{"a"}} };
  101.         /*assert(empty == expected);*/
  102.         AssertEqual(empty, expected, "Add to empty");
  103.     }
  104.  
  105.     {
  106.         map<string, set<string>> synonyms = { {"a",{"b"}},
  107.                                               {"b",{"a","c"}},
  108.                                               {"c",{"b"}} };
  109.         ADDSYNONYMS(synonyms, "a", "c");
  110.         const map<string, set<string>> expected = { {"a",{"b","c"}},
  111.                                                     {"b",{"a","c"}},
  112.                                                     {"c",{"b","a"}} };
  113.         /*assert(synonyms == expected);*/
  114.         AssertEqual(synonyms, expected, "Add to non-empty");
  115.  
  116.     }
  117.     /*cout << "TESTADDSYNONYMS OK" << endl;*/
  118. }
  119. //**********************************************************************************************************
  120.  
  121. void TESTCOUNT()
  122. {
  123.     {
  124.         map<string, set<string>> empty;
  125.         /*assert(GETSYNONYMCOUNT(empty, "a") == 0);*/
  126.         AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
  127.     }
  128.  
  129.     {
  130.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  131.                                               {"b",{"a"}},
  132.                                               {"c",{"a"}} };
  133.  
  134.         /*assert(GETSYNONYMCOUNT(synonyms, "a") == 2);
  135.          assert(GETSYNONYMCOUNT(synonyms, "b") == 1);
  136.          assert(GETSYNONYMCOUNT(synonyms, "z") == 0);*/
  137.         AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
  138.         AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
  139.         AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
  140.     }
  141.     /*cout << "TESTCOUNT OK" << endl;*/
  142. }
  143. //**********************************************************************************************************
  144.  
  145. void TESTARESYNONYMS()
  146. {
  147.     {
  148.         map<string, set<string>> empty;
  149.         /*assert(!ARESYNONYMS(empty, "a", "b"));
  150.          assert(!ARESYNONYMS(empty, "b", "a"));*/
  151.  
  152.          //AssertEqual(ARESYNONYMS(empty, "a", "b"), false, "empty a b");
  153.  
  154.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
  155.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
  156.     }
  157.  
  158.     {
  159.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  160.                                               {"b",{"a"}},
  161.                                               {"c",{"a"}} };
  162.  
  163.         /*assert(ARESYNONYMS(synonyms, "a", "b"));
  164.         assert(ARESYNONYMS(synonyms, "b", "a"));
  165.         assert(ARESYNONYMS(synonyms, "a", "c"));
  166.         assert(ARESYNONYMS(synonyms, "c", "a"));
  167.         assert(!ARESYNONYMS(synonyms, "b", "c"));
  168.         assert(!ARESYNONYMS(synonyms, "c", "b"));*/
  169.         Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
  170.         Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
  171.         Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
  172.         Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
  173.         Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
  174.         Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
  175.     }
  176.     /*cout << "TESTARESYNONYMS OK" << endl;*/
  177. }
  178. //**********************************************************************************************************
  179.  
  180. class TestRunner
  181. {
  182. public:
  183.  
  184.     template <class TestFunc>
  185.     void RunTest(TestFunc func, const string& test_name)
  186.     {
  187.         try
  188.         {
  189.             func();
  190.             cerr << test_name << " OK" << endl;
  191.         }
  192.         catch (runtime_error& e)
  193.         {
  194.             ++fail_count;
  195.             cerr << test_name << " fail: " << e.what() << endl;
  196.         }
  197.  
  198.     }
  199.     ~TestRunner()
  200.     {
  201.         if (fail_count > 0)
  202.         {
  203.             cerr << fail_count << " tests failed. Terminate";
  204.             exit(1);
  205.         }
  206.     }
  207. private:
  208.  
  209.     int fail_count = 0;
  210. };
  211.  
  212. void TestAll()
  213. {
  214.     TestRunner tr;
  215.  
  216.     tr.RunTest(TESTARESYNONYMS, "TESTARESYNONYMS");
  217.     tr.RunTest(TESTADDSYNONYMS, "TESTADDSYNONYMS");
  218.     tr.RunTest(TESTCOUNT, "TESTCOUNT");
  219. }
  220.  
  221. //**********************************************************************************************************
  222. int main()
  223. {
  224.     TestAll();
  225.  
  226.     int q;
  227.     cin >> q;
  228.     map<string, set<string>>synonyms;
  229.     for (int i = 0; i < q; i++)
  230.     {
  231.         string operation_code;
  232.         cin >> operation_code;
  233.  
  234.         if (operation_code == "ADD")
  235.         {
  236.             string first_word, second_word;
  237.             cin >> first_word >> second_word;
  238.  
  239.             ADDSYNONYMS(synonyms, first_word, second_word);
  240.             /*synonyms[second_word].insert(first_word);
  241.             synonyms[first_word].insert(second_word);*/
  242.  
  243.         }
  244.         else if (operation_code == "COUNT")
  245.         {
  246.             string word;
  247.             cin >> word;
  248.  
  249.             cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  250.             /*cout << synonyms[word].size()<<endl;*/
  251.         }
  252.         else if (operation_code == "CHECK")
  253.         {
  254.             string first_word, second_word;
  255.             cin >> first_word >> second_word;
  256.  
  257.             if (ARESYNONYMS(synonyms, first_word, second_word))
  258.             {
  259.                 cout << "YES" << endl;
  260.             }
  261.             else
  262.             {
  263.                 cout << "NO" << endl;
  264.             }
  265.         }
  266.     }
  267.  
  268.     return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement