Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <sstream>
  5. #include <stdexcept>
  6. #include <string>
  7. #include <vector>
  8. #include <exception>
  9.  
  10. using namespace std;
  11.  
  12. template <class T>
  13. ostream& operator << (ostream& os, const vector<T>& s) {
  14. os << "{";
  15. bool first = true;
  16. for (const auto& x : s) {
  17. if (!first) {
  18. os << ", ";
  19. }
  20. first = false;
  21. os << x;
  22. }
  23. return os << "}";
  24. }
  25.  
  26. template <class T>
  27. ostream& operator << (ostream& os, const set<T>& s) {
  28. os << "{";
  29. bool first = true;
  30. for (const auto& x : s) {
  31. if (!first) {
  32. os << ", ";
  33. }
  34. first = false;
  35. os << x;
  36. }
  37. return os << "}";
  38. }
  39.  
  40. template <class K, class V>
  41. ostream& operator << (ostream& os, const map<K, V>& m) {
  42. os << "{";
  43. bool first = true;
  44. for (const auto& kv : m) {
  45. if (!first) {
  46. os << ", ";
  47. }
  48. first = false;
  49. os << kv.first << ": " << kv.second;
  50. }
  51. return os << "}";
  52. }
  53.  
  54. template<class T, class U>
  55. void AssertEqual(const T& t, const U& u, const string& hint = {}) {
  56. if (t != u) {
  57. ostringstream os;
  58. os << "Assertion failed: " << t << " != " << u;
  59. if (!hint.empty()) {
  60. os << " hint: " << hint;
  61. }
  62. throw runtime_error(os.str());
  63. }
  64. }
  65.  
  66. void Assert(bool b, const string& hint) {
  67. AssertEqual(b, true, hint);
  68. }
  69.  
  70. class TestRunner {
  71. public:
  72. template <class TestFunc>
  73. void RunTest(TestFunc func, const string& test_name) {
  74. try {
  75. func();
  76. cerr << test_name << " OK" << endl;
  77. } catch (exception& e) {
  78. ++fail_count;
  79. cerr << test_name << " fail: " << e.what() << endl;
  80. } catch (...) {
  81. ++fail_count;
  82. cerr << "Unknown exception caught" << endl;
  83. }
  84. }
  85.  
  86. ~TestRunner() {
  87. if (fail_count > 0) {
  88. cerr << fail_count << " unit tests failed. Terminate" << endl;
  89. exit(1);
  90. }
  91. }
  92.  
  93. private:
  94. int fail_count = 0;
  95. };
  96. void TestPalindrom(){
  97. Assert(IsPalindrom("xux"),"hey");
  98. Assert(IsPalindrom("xux"), "kek");
  99. Assert(IsPalindrom(""), "kek");
  100. Assert(IsPalindrom("a"), "kek");
  101. Assert(IsPalindrom("Z"), "kek");
  102. Assert(IsPalindrom(" "), "kek");
  103. Assert(IsPalindrom("fxuxf"), "kek");
  104. Assert(IsPalindrom("abccba"), "kek");
  105. Assert(IsPalindrom("abchcba"), "kek");
  106. Assert(IsPalindrom("wasitacaroracatisaw"), "kek");
  107. Assert(IsPalindrom(" a "), "kek");
  108. }
  109.  
  110. void TestNoPalindrom(){
  111. Assert(!IsPalindrom("xuxi"), "nekek");
  112. Assert(!IsPalindrom("abcbd"), "nekek");
  113. Assert(!IsPalindrom("xuxix"), "nekek");
  114. Assert(!IsPalindrom("ablcba"), "nekek");
  115. Assert(!IsPalindrom("ablcba "), "nekek");
  116. }
  117.  
  118. void TestAll(){
  119. TestRunner r;
  120. r.RunTest(TestPalindrom, "testPal");
  121. r.RunTest(TestNoPalindrom, "testNoPal");
  122. }
  123.  
  124. int main() {
  125. TestAll();
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement