shushus

Спринт 3. Применяем фреймворк на практике.

Dec 9th, 2022
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. // Скопируйте сюда решения задания из урока «Юнит-тестирование класса».
  2. #include <cassert>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <sstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. class Synonyms {
  12. public:
  13.     void Add(const string& first_word, const string& second_word) {
  14.         synonyms_[first_word].insert(second_word);
  15.         synonyms_[second_word].insert(first_word);
  16.     }
  17.  
  18.     size_t GetSynonymCount(const string& word) const {
  19.         if (synonyms_.count(word) != 0) {
  20.             return synonyms_.at(word).size();
  21.         }
  22.         return 0;
  23.     }
  24.  
  25.     bool AreSynonyms(const string& first_word, const string& second_word) const {
  26.         // Напишите недостающий код
  27.         if (synonyms_.count(first_word)){
  28.             if (synonyms_.at(first_word).count(second_word)){
  29.                 return true;
  30.             }else{
  31.                 return false;
  32.             }
  33.         }else{
  34.             return false;
  35.         }
  36.     }
  37.  
  38. private:
  39.     map<string, set<string>> synonyms_;
  40. };
  41.  
  42. void Assert(bool value, const string& string_value, const string& file_name, const string& func_name, unsigned line_number, const string& hint) {
  43.     if (!value) {
  44.         cout << file_name << "("s << line_number << "): "s << func_name << ": "s;
  45.         cout << "ASSERT("s << string_value << ") failed."s;
  46.         if (!hint.empty()) {
  47.             cout << " Hint: "s << hint;
  48.         }
  49.         cout << endl;
  50.         abort();
  51.     }
  52. }
  53.  
  54. #define ASSERT(expr) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  55.  
  56. #define ASSERT_HINT(expr, hint) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  57.  
  58. /*template <typename T, typename U>
  59. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  60.                      const string& func, unsigned line, const string& hint) {
  61.     if (t != u) {
  62.         cout << boolalpha;
  63.         cout << file << "("s << line << "): "s << func << ": "s;
  64.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  65.         cout << t << " != "s << u << "."s;
  66.         if (!hint.empty()) {
  67.             cout << " Hint: "s << hint;
  68.         }
  69.         cout << endl;
  70.         abort();
  71.     }
  72. }
  73.  
  74. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  75.  
  76. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))*/
  77.  
  78. void TestAddingSynonymsIncreasesTheirCount() {
  79.     Synonyms synonyms;
  80.     ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 0, "Synonyms should not be added");
  81.     ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 0, "Synonyms should not be added");
  82.  
  83.     synonyms.Add("music"s, "melody"s);
  84.     ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 1, "This word must have a synonym after adding");
  85.     ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 1, "This word must have a synonym after adding");
  86.  
  87.     synonyms.Add("music"s, "tune"s);
  88.     ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 2, "This word must have more synonyms after adding");
  89.     ASSERT_HINT(synonyms.GetSynonymCount("tune"s) == 1, "This word must have a synonym after adding");
  90.     ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 1, "This word must have a synonym after adding");
  91. }
  92.  
  93. void TestAreSynonyms() {
  94.     // Напишите недостающий код
  95.     Synonyms synonyms;
  96.     synonyms.Add("music"s, "melody"s);
  97.     ASSERT_HINT(synonyms.AreSynonyms("music"s, "melody"s) == 1, "These words should be synonyms");
  98.     ASSERT_HINT(synonyms.AreSynonyms("melody"s, "music"s) == 1, "These words should be synonyms");
  99.    
  100.     synonyms.Add("music"s, "tune"s);
  101.     ASSERT_HINT(synonyms.AreSynonyms("tune"s, "music"s) == 1, "These words should be synonyms");
  102.     ASSERT_HINT(synonyms.AreSynonyms("music"s, "melody"s) == 1, "These words should be synonyms");
  103.    
  104.     ASSERT_HINT(synonyms.AreSynonyms("tune"s, "melody"s) == 0, "These words should not be synonyms");
  105.     ASSERT_HINT(synonyms.AreSynonyms("melody"s, "tune"s) == 0, "These words should not be synonyms");
  106.    
  107.     ASSERT_HINT(synonyms.AreSynonyms("music"s, ""s) == 0, "These words should not be synonyms, full and empty");
  108.     ASSERT_HINT(synonyms.AreSynonyms(""s, ""s) == 0, "These words should not be synonyms, emptyes");
  109.     ASSERT_HINT(synonyms.AreSynonyms(" "s, ""s) == 0, "These words should not be synonyms, space and empty");
  110.     ASSERT_HINT(synonyms.AreSynonyms(" "s, " "s) == 0, "These words should not be synonyms, spaces");
  111. }
  112.  
  113. void TestSynonyms() {
  114.     TestAddingSynonymsIncreasesTheirCount();
  115.     TestAreSynonyms();
  116.     //cout<<"hui"<<endl;
  117. }
  118.  
  119. int main() {
  120.     TestSynonyms();
  121.  
  122.     Synonyms synonyms;
  123.  
  124.     string line;
  125.     while (getline(cin, line)) {
  126.         istringstream command(line);
  127.         string action;
  128.         command >> action;
  129.  
  130.         if (action == "ADD"s) {
  131.             string first_word, second_word;
  132.             command >> first_word >> second_word;
  133.             synonyms.Add(first_word, second_word);
  134.         } else if (action == "COUNT"s) {
  135.             string word;
  136.             command >> word;
  137.             cout << synonyms.GetSynonymCount(word) << endl;
  138.         } else if (action == "CHECK"s) {
  139.             string first_word, second_word;
  140.             command >> first_word >> second_word;
  141.             if (synonyms.AreSynonyms(first_word, second_word)) {
  142.                 cout << "YES"s << endl;
  143.             } else {
  144.                 cout << "NO"s << endl;
  145.             }
  146.         } else if (action == "EXIT"s) {
  147.             break;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment