Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <map>
- #include <set>
- #include <string>
- #include <vector>
- using namespace std;
- //*********************************************************************************************************
- template <typename First, typename Second>
- ostream& operator<<(ostream& out, const pair<First, Second>& p)
- {
- return out << p.first << ": "s << p.second;
- }
- //*********************************************************************************************************
- template <typename Container>
- void Print(ostream& out, const Container& container)
- {
- bool is_first = true;
- for (const auto& element : container)
- {
- if (!is_first)
- {
- out << ", "s;
- }
- is_first = false;
- out << element;
- }
- }
- //*********************************************************************************************************
- template <typename Element>
- ostream& operator<<(ostream& out, const vector<Element>& container)
- {
- out << '[';
- Print(out, container);
- out << ']';
- return out;
- }
- //*********************************************************************************************************
- template <typename Element>
- ostream& operator<<(ostream& out, const set<Element>& container)
- {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- //*********************************************************************************************************
- template <typename Key, typename Value>
- ostream& operator<<(ostream& out, const map<Key, Value>& container)
- {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- //####################################################################################################################
- template <typename T, typename U>
- void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
- const string& func, unsigned line, const string& hint)
- {
- if (t != u)
- {
- cout << boolalpha;
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
- cout << t << " != "s << u << "."s;
- if (!hint.empty())
- {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
- const string& hint)
- {
- if (!value)
- {
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT("s << expr_str << ") failed."s;
- if (!hint.empty())
- {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- //#####################################################################################################################
- #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
- #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
- //#####################################################################################################################
- class TestRunner
- {
- public:
- template <class TestFunc>
- void 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;
- }
- }
- ~TestRunner()
- {
- if (fail_count > 0)
- {
- cerr << fail_count << " tests failed. Terminate";
- exit(1);
- }
- }
- private:
- int fail_count = 0;
- };
- void Test1()
- {
- ///
- }
- void Test2()
- {
- ///
- }
- void Test3()
- {
- ///
- }
- void TestAll()
- {
- TestRunner tr;
- tr.RunTest(Test1, "Test1");
- tr.RunTest(Test2, "Test2");
- tr.RunTest(Test3, "Test3");
- }
- int main()
- {
- TestAll();
- ///
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement