Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- test_runner.h
- #pragma once
- #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))
- #define RUN_TEST(tr,func) tr.RunTest(func, #func)
- //####################################################################################################################
- 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;
- };
- #################################################################################################################
- profile.h
- #pragma once
- #include <iostream>
- #include <chrono>
- using namespace std;
- using namespace std::chrono;
- class LogDuration
- {
- public:
- explicit LogDuration(const string& msg = "") : message(msg + ": "), start(steady_clock::now())
- {
- }
- ~LogDuration()
- {
- auto finish = steady_clock::now();
- auto dur = finish - start;
- cerr << message << duration_cast<milliseconds>(dur).count() << " ms" << endl;
- }
- private:
- string message;
- steady_clock::time_point start;
- };
- #define UNIQ_ID_IMPL(lineno) _a_local_var_##lineno
- #define UNIQ_ID(lineno) UNIQ_ID_IMPL(lineno)
- #define LOG_DURATION(message) LogDuration UNIQ_ID(__LINE__) { message };
- #################################################################################################################
- main
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <set>
- #include "profile.h"
- #include "test_runner.h"
- using namespace std;
- void Test1()
- {
- ///
- ASSERT(1,1);
- ASSERT_EQUAL(1, 1);
- }
- void Test2()
- {
- ///
- ASSERT(1, 1);
- ASSERT_EQUAL(1, 1);
- }
- // Функция TestAll() является точкой входа для запуска тестов
- void TestAll()
- {
- TestRunner tr;
- RUN_TEST(tr,Test1);
- RUN_TEST(tr,Test2);
- }
- int main()
- {
- TestAll();
- LOG_DURATION("Total");
- ifstream in("input.txt");
- int element_count;
- in >> element_count;
- set<int> elements;
- {
- LOG_DURATION("Input");
- for (int i = 0; i < element_count; ++i)
- {
- int x;
- in >> x;
- elements.insert(x);
- }
- }
- int query_count;
- in >> query_count;
- int total_found = 0;
- {
- LOG_DURATION("Queries processing");
- for (int i = 0; i < query_count; ++i)
- {
- int x;
- in >> x;
- if (elements.find(x) != elements.end())
- {
- ++total_found;
- }
- }
- }
- cout << total_found << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement