Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_intro.cpp
- #include "test_runner.h"
- #include "rational.h"
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- void TestDefaultConstructor()
- {
- /*const string file = __FILE__;
- const int line = __LINE__;*/
- const Rational defaultConstructed;
- //AssertEqual(defaultConstructed.Numerator(), 0, "Default constructor numerator");
- //AssertEqual(defaultConstructed.Denominator(), 1, "Default constructor denominator");
- //ASSERT_EQUAL(defaultConstructed.Numerator(), 0, "Default constructor numerator");
- //ASSERT_EQUAL(defaultConstructed.Denominator(), 1, "Default constructor denominator");
- ASSERT_EQUAL(defaultConstructed.Numerator(), 0);
- ASSERT_EQUAL(defaultConstructed.Denominator(), 1);
- }
- void TestConstruction()
- {
- const Rational r(3, 12);
- //AssertEqual(r.Numerator(), 1, "3/12 numerator");
- //AssertEqual(r.Denominator(), 4, "3/12 denominator");
- //ASSERT_EQUAL(r.Numerator(), 1, "3/12 numerator");
- //ASSERT_EQUAL(r.Denominator(), 4, "3/12 denominator");
- ASSERT_EQUAL(r.Numerator(), 1);
- ASSERT_EQUAL(r.Denominator(), 4);
- }
- int main()
- {
- TestRunner tr;
- //tr.RunTest(TestDefaultConstructor, "TestDefaultConstructor");
- //tr.RunTest(TestConstruction, "TestConstruction");
- RUN_TEST(tr, TestDefaultConstructor);
- RUN_TEST(tr, TestConstruction);
- return 0;
- }
- ###################################################################################################################
- rational.h
- #pragma once
- class Rational
- {
- public:
- Rational() = default;
- Rational(int nn, int dd);
- int Numerator() const;
- int Denominator() const;
- private:
- int n = 0;
- int d = 1;
- };
- ####################################################################################################################
- rational.cpp
- #include "rational.h"
- int gcd(int a, int b)
- {
- return b == 0 ? a : gcd(b, a % b);
- }
- Rational::Rational(int nn, int dd)
- {
- int g = gcd(nn, dd);
- n = nn / g;
- d = dd / g;
- if (d < 0)
- {
- d = -d;
- n = -n;
- }
- }
- int Rational::Numerator() const
- {
- return n;
- }
- int Rational::Denominator() const
- {
- return d;
- }
- ####################################################################################################################
- test_runner.h
- #pragma once
- #include <sstream>
- #include <stdexcept>
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <vector>
- using namespace std;
- template<class T>
- std::ostream& operator<<(std::ostream& os, const std::set<T>& s)
- {
- os << "{";
- bool first = true;
- for (const auto& x : s)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << x;
- }
- return os << "}";
- }
- template <class K, class V>
- ostream& operator<<(ostream& os, const map<K, V>& m)
- {
- os << "{";
- bool first = true;
- for (const auto& kv : m)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << kv.first << ": " << kv.second;
- }
- return os << "}";
- }
- template<class T, class U>
- void AssertEqual(const T& t, const U& u, const string& hint)
- {
- if (t != u)
- {
- ostringstream os;
- os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
- throw runtime_error(os.str());
- }
- }
- void Assert(bool b, const string& hint)
- {
- AssertEqual(b, true, hint);
- }
- 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;
- };
- #define ASSERT_EQUAL(x,y) { \
- ostringstream os; \
- os << #x << " != " << #y << "," \
- << __FILE__ << ":" << __LINE__; \
- AssertEqual(x, y, os.str()); \
- }
- #define ASSERT(x) { \
- ostringstream os; \
- os << #x << " is false, " \
- << __FILE__ << ":" << __LINE__; \
- AssertEqual(x, y, os.str()); \
- }
- #define RUN_TEST(tr,func) \
- tr.RunTest(func, #func)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement