Advertisement
RobertDeMilo

YB3.6 Механизм сборки проектов, состоящих из нескольких файлов

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