Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <map>
- #include <vector>
- #include <set>
- #include <exception>
- #include <iostream>
- #include <sstream>
- using namespace std;
- template <typename T>
- ostream& operator<<(ostream& os, const vector <T>& v);
- template <typename K, typename V>
- ostream& operator<<(ostream& os, const map <K, V>& mp);
- template <typename T>
- ostream& operator<<(ostream& os, set <T> st);
- template <typename T, typename U>
- void AssertEqual(const T& t, const U& u, const string& hint = {});
- void Assert(bool b, const string& hint);
- class TestRunner
- {
- public:
- template <typename FuncName>
- void RunTest(const FuncName& func, const string& testName);
- ~TestRunner();
- private:
- int failedTests = 0;
- };
- //================================ Implementations ======================================
- template <typename FuncName>
- void TestRunner::RunTest(const FuncName& func, const string& testName)
- {
- try
- {
- func();
- cerr << testName << ": OK\n";
- }
- catch (const runtime_error& re)
- {
- ++failedTests;
- cerr << testName << ": FAILED!!! " << re.what() << '\n';
- }
- catch (const exception& ex)
- {
- ++failedTests;
- cerr << "Unknown exception!!! : " << ex.what() << "\n";
- }
- }
- template <typename T, typename U>
- void AssertEqual(const T& t, const U& u, const string& hint)
- {
- if (t != u)
- {
- ostringstream os;
- os << t << " != " << u;
- if (!hint.empty())
- {
- os << " hint:" << hint;
- }
- throw runtime_error(os.str());
- }
- }
- template <typename K, typename V>
- ostream& operator<<(ostream& os, const map <K, V>& mp)
- {
- os << "{";
- bool first = 1;
- for (const auto& [key, value] : mp)
- {
- if (!first)
- {
- os << ", ";
- }
- first = 0;
- os << key << ": " << value;
- }
- return os << '}';
- }
- template <typename T>
- ostream& operator<<(ostream& os, set <T> st)
- {
- os << "{";
- bool first = 1;
- for (const auto& i : st)
- {
- if (!first)
- os << ", ";
- os << i;
- }
- os << '}';
- }
- template <typename T>
- ostream& operator<<(ostream& os, const vector <T>& v)
- {
- os << "[";
- bool first = 1;
- for (const auto& i : v)
- {
- if (!first)
- {
- os << ", ";
- }
- first = 0;
- os << i;
- }
- return os << "]";
- }
Advertisement
Add Comment
Please, Sign In to add comment