Advertisement
RobertDeMilo

YB2.4 Отладка решения задачи «Синонимы» с помощью юнит тестов

Oct 28th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include<iostream>
  2. #include<cassert>
  3. #include<string>
  4. #include<map>
  5. #include<set>
  6.  
  7. using namespace std;
  8.  
  9. using SYNONYMS = map<string, set<string>>;
  10.  
  11. **********************************************************************************************************
  12.  
  13. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  14. {
  15.     synonyms[second_word].insert(first_word);
  16.     synonyms[first_word].insert(second_word);
  17.     //synonyms[first_word].insert(first_word);
  18. }
  19. **********************************************************************************************************
  20.  
  21. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
  22. {
  23.     return synonyms[first_word].size();
  24. }
  25. **********************************************************************************************************
  26.  
  27. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  28. {
  29.     return synonyms[first_word].count(second_word) == 1;
  30. }
  31. **********************************************************************************************************
  32.  
  33. void TESTADDSYNONYMS()
  34. {
  35.     {
  36.         map<string, set<string>> empty;
  37.         ADDSYNONYMS(empty, "a", "b");
  38.         const map<string, set<string>> expected = { {"a",{"b"}},
  39.                                                     {"b",{"a"}} };
  40.         assert(empty == expected);
  41.     }
  42.  
  43.     {
  44.         map<string, set<string>> synonyms = { {"a",{"b"}},
  45.                                               {"b",{"a","c"}},
  46.                                               {"c",{"b"}} };
  47.         ADDSYNONYMS(synonyms, "a", "c");
  48.         const map<string, set<string>> expected = { {"a",{"b","c"}},
  49.                                                     {"b",{"a","c"}},
  50.                                                     {"c",{"b","a"}} };
  51.         assert(synonyms == expected);
  52.  
  53.     }
  54.     cout << "TESTADDSYNONYMS OK" << endl;
  55. }
  56. **********************************************************************************************************
  57.  
  58. void TESTCOUNT()
  59. {
  60.     {
  61.         map<string, set<string>> empty;
  62.         assert(GETSYNONYMCOUNT(empty, "a") == 0);
  63.     }
  64.  
  65.     {
  66.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  67.                                               {"b",{"a"}},
  68.                                               {"c",{"a"}} };
  69.  
  70.         assert(GETSYNONYMCOUNT(synonyms, "a") == 2);
  71.         assert(GETSYNONYMCOUNT(synonyms, "b") == 1);
  72.         assert(GETSYNONYMCOUNT(synonyms, "z") == 0);
  73.     }
  74.     cout << "TESTCOUNT OK" << endl;
  75. }
  76. **********************************************************************************************************
  77.  
  78. void TESTARESYNONYMS()
  79. {
  80.     {
  81.         map<string, set<string>> empty;
  82.         assert(!ARESYNONYMS(empty, "a", "b"));
  83.         assert(!ARESYNONYMS(empty, "b", "a"));
  84.     }
  85.  
  86.     {
  87.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  88.                                               {"b",{"a"}},
  89.                                               {"c",{"a"}} };
  90.  
  91.         assert(ARESYNONYMS(synonyms, "a", "b"));
  92.         assert(ARESYNONYMS(synonyms, "b", "a"));
  93.         assert(ARESYNONYMS(synonyms, "a", "c"));
  94.         assert(ARESYNONYMS(synonyms, "c", "a"));
  95.         assert(!ARESYNONYMS(synonyms, "b", "c"));
  96.         assert(!ARESYNONYMS(synonyms, "c", "b"));
  97.     }
  98.     cout << "TESTARESYNONYMS OK" << endl;
  99. }
  100. **********************************************************************************************************
  101.  
  102. void TESTALL()
  103. {
  104.     TESTCOUNT();
  105.     TESTARESYNONYMS();
  106.     TESTADDSYNONYMS();
  107. }
  108. **********************************************************************************************************
  109.  
  110. int main()
  111. {
  112.     TESTALL();
  113.     return 0;
  114.  
  115.     //int q;
  116.     //cin >> q;
  117.     //map<string, set<string>>synonyms;
  118.     //for (int i = 0; i < q; i++)
  119.     //{
  120.     //    string operation_code;
  121.     //    cin >> operation_code;
  122.  
  123.     //    if (operation_code == "ADD")
  124.     //    {
  125.     //        string first_word, second_word;
  126.     //        cin >> first_word >> second_word;
  127.  
  128.     //        ADDSYNONYMS(synonyms, first_word, second_word);
  129.     //        /*synonyms[second_word].insert(first_word);
  130.     //        synonyms[first_word].insert(second_word);*/
  131.  
  132.     //    }
  133.     //    else if (operation_code == "COUNT")
  134.     //    {
  135.     //        string word;
  136.     //        cin >> word;
  137.  
  138.     //        cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  139.     //        /*cout << synonyms[word].size()<<endl;*/
  140.     //    }
  141.     //    else if (operation_code == "CHECK")
  142.     //    {
  143.     //        string first_word, second_word;
  144.     //        cin >> first_word >> second_word;
  145.  
  146.     //        if (ARESYNONYMS(synonyms, first_word, second_word))
  147.     //        {
  148.     //            cout << "YES" << endl;
  149.     //        }
  150.     //        else
  151.     //        {
  152.     //            cout << "NO" << endl;
  153.     //        }
  154.     //    }
  155.     //}
  156.     //return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement