Advertisement
RobertDeMilo

YB2.6-7 Улучшаем assert и Внедряем шаблон AssertEqual во все юнит тесты

Oct 28th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.78 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. template <typename T, typename U>
  86. void AssertEqual(const T& t, const U& u, const string& hint)
  87. {
  88.     if (t != u) {
  89.         // Включаем режим вывода значений типа bool в виде true и false
  90.         cout << boolalpha;
  91.         cout << "Assertion failed: "s << t << " != "s << u << "."s;
  92.         if (!hint.empty()) {
  93.             cout << " Hint: "s << hint;
  94.         }
  95.         cout << endl;
  96.         abort();
  97.     }
  98. }
  99.  
  100. void Assert(bool b, const string& hint)
  101. {
  102.     AssertEqual(b, true, hint);
  103. }
  104.  
  105. //##########################################################################################################
  106. //**********************************************************************************************************
  107.  
  108. void TESTADDSYNONYMS()
  109. {
  110.     {
  111.         map<string, set<string>> empty;
  112.         ADDSYNONYMS(empty, "a", "b");
  113.         const map<string, set<string>> expected = { {"a",{"b"}},
  114.                                                     {"b",{"a"}} };
  115.         /*assert(empty == expected);*/
  116.         AssertEqual(empty, expected, "Add to empty");
  117.     }
  118.  
  119.     {
  120.         map<string, set<string>> synonyms = { {"a",{"b"}},
  121.                                               {"b",{"a","c"}},
  122.                                               {"c",{"b"}} };
  123.         ADDSYNONYMS(synonyms, "a", "c");
  124.         const map<string, set<string>> expected = { {"a",{"b","c"}},
  125.                                                     {"b",{"a","c"}},
  126.                                                     {"c",{"b","a"}} };
  127.         /*assert(synonyms == expected);*/
  128.         AssertEqual(synonyms, expected, "Add to non-empty");
  129.  
  130.     }
  131.     cout << "TESTADDSYNONYMS OK" << endl;
  132. }
  133. //**********************************************************************************************************
  134.  
  135. void TESTCOUNT()
  136. {
  137.     {
  138.         map<string, set<string>> empty;
  139.         /*assert(GETSYNONYMCOUNT(empty, "a") == 0);*/
  140.         AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
  141.     }
  142.  
  143.     {
  144.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  145.                                               {"b",{"a"}},
  146.                                               {"c",{"a"}} };
  147.  
  148.         /*assert(GETSYNONYMCOUNT(synonyms, "a") == 2);
  149.          assert(GETSYNONYMCOUNT(synonyms, "b") == 1);
  150.          assert(GETSYNONYMCOUNT(synonyms, "z") == 0);*/
  151.         AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
  152.         AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
  153.         AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
  154.     }
  155.     cout << "TESTCOUNT OK" << endl;
  156. }
  157. //**********************************************************************************************************
  158.  
  159. void TESTARESYNONYMS()
  160. {
  161.     {
  162.         map<string, set<string>> empty;
  163.         /*assert(!ARESYNONYMS(empty, "a", "b"));
  164.          assert(!ARESYNONYMS(empty, "b", "a"));*/
  165.  
  166.          //AssertEqual(ARESYNONYMS(empty, "a", "b"), false, "empty a b");
  167.  
  168.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
  169.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
  170.     }
  171.  
  172.     {
  173.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  174.                                               {"b",{"a"}},
  175.                                               {"c",{"a"}} };
  176.  
  177.         /*assert(ARESYNONYMS(synonyms, "a", "b"));
  178.         assert(ARESYNONYMS(synonyms, "b", "a"));
  179.         assert(ARESYNONYMS(synonyms, "a", "c"));
  180.         assert(ARESYNONYMS(synonyms, "c", "a"));
  181.         assert(!ARESYNONYMS(synonyms, "b", "c"));
  182.         assert(!ARESYNONYMS(synonyms, "c", "b"));*/
  183.         Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
  184.         Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
  185.         Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
  186.         Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
  187.         Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
  188.         Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
  189.     }
  190.     cout << "TESTARESYNONYMS OK" << endl;
  191. }
  192. //**********************************************************************************************************
  193.  
  194. void TESTALL()
  195. {
  196.     TESTCOUNT();
  197.     TESTADDSYNONYMS();
  198.     TESTARESYNONYMS();
  199. }
  200. //**********************************************************************************************************
  201.  
  202. int main()
  203. {
  204.     TESTALL();
  205.     return 0;
  206.  
  207.     //int q;
  208.     //cin >> q;
  209.     //map<string, set<string>>synonyms;
  210.     //for (int i = 0; i < q; i++)
  211.     //{
  212.     //    string operation_code;
  213.     //    cin >> operation_code;
  214.  
  215.     //    if (operation_code == "ADD")
  216.     //    {
  217.     //        string first_word, second_word;
  218.     //        cin >> first_word >> second_word;
  219.  
  220.     //        ADDSYNONYMS(synonyms, first_word, second_word);
  221.     //        /*synonyms[second_word].insert(first_word);
  222.     //        synonyms[first_word].insert(second_word);*/
  223.  
  224.     //    }
  225.     //    else if (operation_code == "COUNT")
  226.     //    {
  227.     //        string word;
  228.     //        cin >> word;
  229.  
  230.     //        cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  231.     //        /*cout << synonyms[word].size()<<endl;*/
  232.     //    }
  233.     //    else if (operation_code == "CHECK")
  234.     //    {
  235.     //        string first_word, second_word;
  236.     //        cin >> first_word >> second_word;
  237.  
  238.     //        if (ARESYNONYMS(synonyms, first_word, second_word))
  239.     //        {
  240.     //            cout << "YES" << endl;
  241.     //        }
  242.     //        else
  243.     //        {
  244.     //            cout << "NO" << endl;
  245.     //        }
  246.     //    }
  247.     //}
  248.     //return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement