Advertisement
RobertDeMilo

RB1.3 Макросы FILE и LINE

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