Advertisement
RobertDeMilo

RB1.1 Введение в макросы

Apr 15th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. macro_intro.cpp
  2.  
  3. #include "test_runner.h"
  4. #include "rational.h"
  5.  
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. #define ASSERT_EQUAL(x,y,m) \
  13.     AssertEqual(x,y,m)
  14.  
  15.  
  16. void TestDefaultConstructor()
  17. {
  18.     const Rational defaultConstructed;
  19.     //AssertEqual(defaultConstructed.Numerator(), 0, "Default constructor numerator");
  20.     //AssertEqual(defaultConstructed.Denominator(), 1, "Default constructor denominator");
  21.     ASSERT_EQUAL(defaultConstructed.Numerator(), 0, "Default constructor numerator");
  22.     ASSERT_EQUAL(defaultConstructed.Denominator(), 1, "Default constructor denominator");
  23. }
  24.  
  25. void TestConstruction()
  26. {
  27.     const Rational r(3, 12);
  28.     //AssertEqual(r.Numerator(), 1, "3/12 numerator");
  29.     //AssertEqual(r.Denominator(), 4, "3/12 denominator");
  30.     ASSERT_EQUAL(r.Numerator(), 1, "3/12 numerator");
  31.     ASSERT_EQUAL(r.Denominator(), 4, "3/12 denominator");
  32. }
  33.  
  34. int main()
  35. {
  36.     TestRunner tr;
  37.     tr.RunTest(TestDefaultConstructor, "TestDefaultConstructor");
  38.     tr.RunTest(TestConstruction, "TestConstruction");
  39.  
  40.     return 0;
  41. }
  42. ###################################################################################################################
  43. rational.h
  44.  
  45. #pragma once
  46.  
  47. class Rational
  48. {
  49. public:
  50.     Rational() = default;
  51.     Rational(int nn, int dd);
  52.  
  53.     int Numerator() const;
  54.     int Denominator() const;
  55.  
  56. private:
  57.     int n = 0;
  58.     int d = 1;
  59. };
  60. ####################################################################################################################
  61. rational.cpp
  62.  
  63. #include "rational.h"
  64.  
  65. int gcd(int a, int b)
  66. {
  67.     return b == 0 ? a : gcd(b, a % b);
  68. }
  69.  
  70. Rational::Rational(int nn, int dd)
  71. {
  72.     int g = gcd(nn, dd);
  73.     n = nn / g;
  74.     d = dd / g;
  75.  
  76.     if (d < 0)
  77.     {
  78.         d = -d;
  79.         n = -n;
  80.     }
  81. }
  82. int Rational::Numerator() const
  83. {
  84.     return n;
  85. }
  86. int Rational::Denominator() const
  87. {
  88.     return d;
  89. }
  90. ####################################################################################################################
  91. test_runner.h
  92.  
  93. #pragma once
  94.  
  95. #include <sstream>
  96. #include <stdexcept>
  97. #include <iostream>
  98. #include <map>
  99. #include <set>
  100. #include <string>
  101. #include <vector>
  102.  
  103. using namespace std;
  104.  
  105. template<class T>
  106. std::ostream& operator<<(std::ostream& os, const std::set<T>& s)
  107. {
  108.     os << "{";
  109.     bool first = true;
  110.  
  111.     for (const auto& x : s)
  112.     {
  113.         if (!first)
  114.         {
  115.             os << ", ";
  116.         }
  117.         first = false;
  118.         os << x;
  119.     }
  120.     return os << "}";
  121. }
  122.  
  123. template <class K, class V>
  124. ostream& operator<<(ostream& os, const map<K, V>& m)
  125. {
  126.     os << "{";
  127.     bool first = true;
  128.     for (const auto& kv : m)
  129.     {
  130.         if (!first)
  131.         {
  132.             os << ", ";
  133.         }
  134.         first = false;
  135.         os << kv.first << ": " << kv.second;
  136.     }
  137.     return os << "}";
  138. }
  139.  
  140. template<class T, class U>
  141. void AssertEqual(const T& t, const U& u, const string& hint)
  142. {
  143.     if (t != u)
  144.     {
  145.         ostringstream os;
  146.         os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
  147.         throw runtime_error(os.str());
  148.     }
  149. }
  150.  
  151. void Assert(bool b, const string& hint)
  152. {
  153.     AssertEqual(b, true, hint);
  154. }
  155.  
  156. class TestRunner
  157. {
  158. public:
  159.  
  160.     template <class TestFunc>
  161.     void RunTest(TestFunc func, const string& test_name)
  162.     {
  163.         try
  164.         {
  165.             func();
  166.             cerr << test_name << " OK" << endl;
  167.         }
  168.         catch (runtime_error& e)
  169.         {
  170.             ++fail_count;
  171.             cerr << test_name << " fail: " << e.what() << endl;
  172.         }
  173.  
  174.     }
  175.    
  176.     ~TestRunner()
  177.     {
  178.         if (fail_count > 0)
  179.         {
  180.             cerr << fail_count << " tests failed. Terminate";
  181.             exit(1);
  182.         }
  183.     }
  184. private:
  185.  
  186.     int fail_count = 0;
  187. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement