Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main
- #include "tests.h"
- #include "synonyms.h"
- #include <iostream>
- #include <exception>
- #include <vector>
- using namespace std;
- int main()
- {
- TestAll();
- int q;
- cin >> q;
- map<string, set<string>>synonyms;
- for (int i = 0; i < q; i++)
- {
- string operation_code;
- cin >> operation_code;
- if (operation_code == "ADD")
- {
- string first_word, second_word;
- cin >> first_word >> second_word;
- ADDSYNONYMS(synonyms, first_word, second_word);
- }
- else if (operation_code == "COUNT")
- {
- string word;
- cin >> word;
- cout << GETSYNONYMCOUNT(synonyms, word) << endl;
- }
- else if (operation_code == "CHECK")
- {
- string first_word, second_word;
- cin >> first_word >> second_word;
- if (ARESYNONYMS(synonyms, first_word, second_word))
- {
- cout << "YES" << endl;
- }
- else
- {
- cout << "NO" << endl;
- }
- }
- }
- return 0;
- }
- #####################################################################################################################
- synonyms.h
- #pragma once
- #include <string>
- #include <set>
- #include <map>
- using namespace std;
- using SYNONYMS = map<string, set<string>>;
- void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word);
- bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word);
- size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word);
- ####################################################################################################################
- synonyms.cpp
- #include "synonyms.h"
- void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
- {
- synonyms[second_word].insert(first_word);
- synonyms[first_word].insert(second_word);
- }
- //**********************************************************************************************************
- size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
- {
- return synonyms[first_word].size();
- }
- //**********************************************************************************************************
- bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
- {
- return synonyms[first_word].count(second_word) == 1;
- }
- ####################################################################################################################
- tests.h
- #pragma once
- #include "test_runner.h"
- #include "synonyms.h"
- // Interface
- void TESTADDSYNONYMS();
- void TESTARESYNONYMS();
- void TESTCOUNT();
- void TestAll();
- ####################################################################################################################
- tests.cpp
- #include "tests.h"
- // Implementation
- void TESTADDSYNONYMS()
- {
- {
- map<string, set<string>> empty;
- ADDSYNONYMS(empty, "a", "b");
- const map<string, set<string>> expected = { {"a",{"b"}},
- {"b",{"a"}} };
- AssertEqual(empty, expected, "Add to empty");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b"}},
- {"b",{"a","c"}},
- {"c",{"b"}} };
- ADDSYNONYMS(synonyms, "a", "c");
- const map<string, set<string>> expected = { {"a",{"b","c"}},
- {"b",{"a","c"}},
- {"c",{"b","a"}} };
- AssertEqual(synonyms, expected, "Add to non-empty");
- }
- }
- //**********************************************************************************************************
- void TESTCOUNT()
- {
- {
- map<string, set<string>> empty;
- AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b","c"}},
- {"b",{"a"}},
- {"c",{"a"}} };
- AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
- AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
- AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
- }
- }
- //**********************************************************************************************************
- void TESTARESYNONYMS()
- {
- {
- map<string, set<string>> empty;
- Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
- Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b","c"}},
- {"b",{"a"}},
- {"c",{"a"}} };
- Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
- Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
- Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
- Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
- Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
- Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
- }
- }
- //**********************************************************************************************************
- void TestAll()
- {
- TestRunner tr;
- tr.RunTest(TESTARESYNONYMS, "TESTARESYNONYMS");
- tr.RunTest(TESTADDSYNONYMS, "TESTADDSYNONYMS");
- tr.RunTest(TESTCOUNT, "TESTCOUNT");
- }
- ####################################################################################################################
- test_runner.h
- #pragma once
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <set>
- #include <map>
- using namespace std;
- template<class T>
- ostream& operator<<(ostream& os, const set<T>& s);
- template <class K, class V>
- ostream& operator<<(ostream& os, const map<K, V>& m);
- template<class T, class U>
- void AssertEqual(const T& t, const U& u, const string& hint);
- void Assert(bool b, const string& hint);
- template<class T>
- ostream& operator<<(ostream& os, const set<T>& s)
- {
- os << "{";
- bool first = true;
- for (const auto& x : s)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << x;
- }
- return os << "}";
- }
- //**********************************************************************************************************
- template <class K, class V>
- ostream& operator<<(ostream& os, const map<K, V>& m)
- {
- os << "{";
- bool first = true;
- for (const auto& kv : m)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << kv.first << ": " << kv.second;
- }
- return os << "}";
- }
- //**********************************************************************************************************
- template<class T, class U>
- void AssertEqual(const T& t, const U& u, const string& hint)
- {
- if (t != u)
- {
- ostringstream os;
- os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
- throw runtime_error(os.str());
- }
- }
- //**********************************************************************************************************
- class TestRunner
- {
- public:
- template <class TestFunc>
- void RunTest(TestFunc func, const string& test_name);
- ~TestRunner();
- private:
- int fail_count = 0;
- };
- template <class TestFunc>
- void TestRunner::RunTest(TestFunc func, const string& test_name)
- {
- try
- {
- func();
- cerr << test_name << " OK" << endl;
- }
- catch (runtime_error& e)
- {
- ++fail_count;
- cerr << test_name << " fail: " << e.what() << endl;
- }
- }
- ####################################################################################################################
- test_runner.cpp
- #include "test_runner.h"
- void Assert(bool b, const string& hint)
- {
- AssertEqual(b, true, hint);
- }
- TestRunner::~TestRunner()
- {
- if (fail_count > 0)
- {
- cerr << fail_count << " tests failed. Terminate";
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement