Advertisement
chevengur

СПРИНТ № 3 | Фреймворк для юнит-тестов | Урок 5: Применяем фреймворк на практике

Nov 14th, 2023
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. https://t.me/hitmyfeet канал с музыкой от начинающего программиста.
  2.  
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. template <typename First, typename Second>
  14. ostream& operator<<(ostream& out, const pair<First, Second>& p) {
  15.     return out << p.first << ": "s << p.second;
  16. }
  17.  
  18. template <typename Container>
  19. void Print(ostream& out, const Container& container) {
  20.     bool is_first = true;
  21.     for (const auto& element : container) {
  22.         if (!is_first) {
  23.             out << ", "s;
  24.         }
  25.         is_first = false;
  26.         out << element;
  27.     }
  28. }
  29.  
  30. template <typename Element>
  31. ostream& operator<<(ostream& out, const vector<Element>& container) {
  32.     out << '[';
  33.     Print(out, container);
  34.     out << ']';
  35.     return out;
  36. }
  37.  
  38. template <typename Element>
  39. ostream& operator<<(ostream& out, const set<Element>& container) {
  40.     out << '{';
  41.     Print(out, container);
  42.     out << '}';
  43.     return out;
  44. }
  45.  
  46. template <typename Key, typename Value>
  47. ostream& operator<<(ostream& out, const map<Key, Value>& container) {
  48.     out << '{';
  49.     Print(out, container);
  50.     out << '}';
  51.     return out;
  52. }
  53.  
  54.  
  55. template <typename T, typename U>
  56. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  57.                      const string& func, unsigned line, const string& hint) {
  58.     if (t != u) {
  59.         cout << boolalpha;
  60.         cout << file << "("s << line << "): "s << func << ": "s;
  61.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  62.         cout << t << " != "s << u << "."s;
  63.         if (!hint.empty()) {
  64.             cout << " Hint: "s << hint;
  65.         }
  66.         cout << endl;
  67.         abort();
  68.     }
  69. }
  70.  
  71. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  72.  
  73. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  74.  
  75. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  76.                 const string& hint) {
  77.     if (!value) {
  78.         cout << file << "("s << line << "): "s << func << ": "s;
  79.         cout << "ASSERT("s << expr_str << ") failed."s;
  80.         if (!hint.empty()) {
  81.             cout << " Hint: "s << hint;
  82.         }
  83.         cout << endl;
  84.         abort();
  85.     }
  86. }
  87.  
  88. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  89.  
  90. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  91.  
  92. class Synonyms {
  93. public:
  94.     void Add(const string& first_word, const string& second_word) {
  95.         synonyms_[first_word].insert(second_word);
  96.         synonyms_[second_word].insert(first_word);
  97.     }
  98.  
  99.     size_t GetSynonymCount(const string& word) const {
  100.         if (synonyms_.count(word) != 0) {
  101.             return synonyms_.at(word).size();
  102.         }
  103.         return 0;
  104.     }
  105.  
  106.     bool AreSynonyms(const string& first_word, const string& second_word) const {
  107.         if(synonyms_.count(first_word) > 0){
  108.             auto word1 = synonyms_.at(first_word);
  109.             for(const auto& w: word1){
  110.                 if(w == second_word)
  111.                     return true;
  112.             }
  113.         }
  114.  
  115.         return false;
  116.     }
  117.  
  118. private:
  119.     map<string, set<string>> synonyms_;
  120. };
  121.  
  122. void TestAddingSynonymsIncreasesTheirCount() {
  123.     Synonyms synonyms;
  124.     ASSERT(synonyms.GetSynonymCount("music"s) == 0);
  125.     ASSERT(synonyms.GetSynonymCount("melody"s) == 0);
  126.  
  127.     synonyms.Add("music"s, "melody"s);
  128.     ASSERT(synonyms.GetSynonymCount("music"s) == 1);
  129.     ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
  130.  
  131.     synonyms.Add("music"s, "tune"s);
  132.     ASSERT(synonyms.GetSynonymCount("music"s) == 2);
  133.     ASSERT(synonyms.GetSynonymCount("tune"s) == 1);
  134.     ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
  135. }
  136.  
  137. void TestAreSynonyms() {
  138.     Synonyms synonyms;
  139.  
  140.     synonyms.Add("music", "tune");
  141.     ASSERT(synonyms.AreSynonyms("music", "tune") == true);
  142.  
  143.     synonyms.Add("melody", "tune");
  144.     ASSERT(synonyms.AreSynonyms("music", "melody") == false);
  145.  
  146.     synonyms.Add("melody", "tune");
  147.     ASSERT(synonyms.AreSynonyms("melody", "tune") == true);
  148. }
  149.  
  150. void TestSynonyms() {
  151.     TestAddingSynonymsIncreasesTheirCount();
  152.     TestAreSynonyms();
  153. }
  154.  
  155. int main() {
  156.     TestSynonyms();
  157.  
  158.     Synonyms synonyms;
  159.  
  160.     string line;
  161.     while (getline(cin, line)) {
  162.         istringstream command(line);
  163.         string action;
  164.         command >> action;
  165.  
  166.         if (action == "ADD"s) {
  167.             string first_word, second_word;
  168.             command >> first_word >> second_word;
  169.             synonyms.Add(first_word, second_word);
  170.         } else if (action == "COUNT"s) {
  171.             string word;
  172.             command >> word;
  173.             cout << synonyms.GetSynonymCount(word) << endl;
  174.         } else if (action == "CHECK"s) {
  175.             string first_word, second_word;
  176.             command >> first_word >> second_word;
  177.             if (synonyms.AreSynonyms(first_word, second_word)) {
  178.                 cout << "YES"s << endl;
  179.             } else {
  180.                 cout << "NO"s << endl;
  181.             }
  182.         } else if (action == "EXIT"s) {
  183.             break;
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement