Advertisement
chzchz

Untitled

Apr 2nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <map>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8. #include <exception>
  9.  
  10. using namespace std;
  11.  
  12. template<class T>
  13. ostream &operator<<(ostream &os, const set<T> &s);
  14.  
  15. template<class K, class V>
  16. ostream &operator<<(ostream &os, const map<K, V> &m);
  17.  
  18. template<class T, class U>
  19. void AssertEqual(const T &t, const U &u, const string &hint);
  20.  
  21. void Assert(bool b, const string &hint);
  22.  
  23. class TestRunner {
  24.  public:
  25.   template<class TestFunc>
  26.   void RunTest(TestFunc func, const string &test_name);
  27.  
  28.   ~TestRunner();
  29.  
  30.  private:
  31.   int fail_count = 0;
  32. };
  33.  
  34. template<class T>
  35. ostream &operator<<(ostream &os, const set<T> &s) {
  36.   os << "{";
  37.   bool first = true;
  38.   for (const auto &x : s) {
  39.     if (!first) {
  40.       os << ", ";
  41.     }
  42.     first = false;
  43.     os << x;
  44.   }
  45.   return os << "}";
  46. }
  47.  
  48. template<class K, class V>
  49. ostream &operator<<(ostream &os, const map<K, V> &m) {
  50.   os << "{";
  51.   bool first = true;
  52.   for (const auto &kv : m) {
  53.     if (!first) {
  54.       os << ", ";
  55.     }
  56.     first = false;
  57.     os << kv.first << ": " << kv.second;
  58.   }
  59.   return os << "}";
  60. }
  61.  
  62. template<class T, class U>
  63. void AssertEqual(const T &t, const U &u, const string &hint) {
  64.   if (t!=u) {
  65.     ostringstream os;
  66.     os << "Assertion failed: " << t << " != " << u
  67.        << " hint: " << hint;
  68.     throw runtime_error(os.str());
  69.   }
  70. }
  71.  
  72. template<class TestFunc>
  73. void TestRunner::RunTest(TestFunc func, const string &test_name) {
  74.   try {
  75.     func();
  76.     cerr << test_name << " OK" << endl;
  77.   }
  78.   catch (runtime_error &e) {
  79.     ++fail_count;
  80.     cerr << test_name << " fail: " << e.what() << endl;
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement