Advertisement
RobertDeMilo

YB2.9 Избавляемся от смешения вывода тестов и основной программы

Oct 28th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 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. template <class TestFunc>
  181. void RunTest(TestFunc func, const string& test_name)
  182. {
  183.     try
  184.     {
  185.         func();
  186.         cerr << test_name << " OK" << endl;
  187.     }
  188.     catch (runtime_error& e)
  189.     {
  190.         cerr << test_name << " fail: " << e.what() << endl;
  191.     }
  192.  
  193. }
  194.  
  195. //**********************************************************************************************************
  196.  
  197. int main()
  198. {
  199.     RunTest(TESTARESYNONYMS, "TESTARESYNONYMS");
  200.     RunTest(TESTADDSYNONYMS, "TESTADDSYNONYMS");
  201.     RunTest(TESTCOUNT, "TESTCOUNT");
  202.  
  203.     int q;
  204.     cin >> q;
  205.     map<string, set<string>>synonyms;
  206.     for (int i = 0; i < q; i++)
  207.     {
  208.         string operation_code;
  209.         cin >> operation_code;
  210.  
  211.         if (operation_code == "ADD")
  212.         {
  213.             string first_word, second_word;
  214.             cin >> first_word >> second_word;
  215.  
  216.             ADDSYNONYMS(synonyms, first_word, second_word);
  217.             /*synonyms[second_word].insert(first_word);
  218.             synonyms[first_word].insert(second_word);*/
  219.  
  220.         }
  221.         else if (operation_code == "COUNT")
  222.         {
  223.             string word;
  224.             cin >> word;
  225.  
  226.             cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  227.             /*cout << synonyms[word].size()<<endl;*/
  228.         }
  229.         else if (operation_code == "CHECK")
  230.         {
  231.             string first_word, second_word;
  232.             cin >> first_word >> second_word;
  233.  
  234.             if (ARESYNONYMS(synonyms, first_word, second_word))
  235.             {
  236.                 cout << "YES" << endl;
  237.             }
  238.             else
  239.             {
  240.                 cout << "NO" << endl;
  241.             }
  242.         }
  243.     }
  244.  
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement