diff --git a/exercises/operators/operators.cpp b/exercises/operators/operators.cpp index fa69514f..238fb355 100644 --- a/exercises/operators/operators.cpp +++ b/exercises/operators/operators.cpp @@ -43,26 +43,26 @@ class Fraction { }; class TestResultPrinter { - public: - - TestResultPrinter( unsigned int a_width ) : m_width(a_width) {} - - void process(std::string const & what, bool passed) { - std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n'; - } - + static TestResultPrinter& instance() { + static TestResultPrinter printer(64); + return printer; + } + + void process(std::string const & what, bool passed) { + std::cout << std::left << std::setw(m_width) << what << ": " + << (passed ? "PASS" : "** FAIL **") << '\n'; + } private: - - unsigned int m_width; - + TestResultPrinter(unsigned int a_width) : m_width(a_width) {} + unsigned int m_width; }; // This is using the cpp, the C preprocessor to expand a bit of code // (the what argument) to a pair containing a string representation // of it and the code itself. That way, print is given a string and a // value where the string is the code that lead to the value -#define CHECK(printer, ...) printer.process(#__VA_ARGS__, (__VA_ARGS__)) +#define CHECK(...) TestResultPrinter::instance().process(#__VA_ARGS__, (__VA_ARGS__)) int main() { @@ -74,32 +74,29 @@ int main() { // equality std::cout<0); - CHECK(p2,compare(third,Fraction{2,4})<0); + CHECK(compare(third,Fraction{2,6})==0); + CHECK(compare(third,Fraction{1,4})>0); + CHECK(compare(third,Fraction{2,4})<0); // multiply std::cout<Fraction{2,6})); - CHECK(p2,std::is_gt(third<=>Fraction{1,4})); - CHECK(p2,std::is_lt(third<=>Fraction{2,4})); - CHECK(p2,(third>Fraction{1,4})); - CHECK(p2,(third=Fraction{2,4})); - CHECK(p2,(third>=Fraction{1,4})); - CHECK(p2,(third<=Fraction{2,4})); - CHECK(p2,(third>=Fraction{1,3})); - CHECK(p2,(third<=Fraction{2,3})); - CHECK(p2,!(thirdFraction{2,4})); - CHECK(p2,!(thirdFraction{2,3})); + CHECK(std::is_eq(third<=>Fraction{2,6})); + CHECK(std::is_gt(third<=>Fraction{1,4})); + CHECK(std::is_lt(third<=>Fraction{2,4})); + CHECK(third>Fraction{1,4}); + CHECK(third=Fraction{2,4})); + CHECK(third>=Fraction{1,4}); + CHECK(third<=Fraction{2,4}); + CHECK(third>=Fraction{1,3}); + CHECK(third<=Fraction{2,3}); + CHECK(!(thirdFraction{2,4})); + CHECK(!(thirdFraction{2,3})); // multiply std::cout<Fraction{1,1})); - CHECK(p3,std::is_eq((3*third)<=>Fraction{1,1})); - CHECK(p3,((3*third).normalized()==1)); + CHECK((third*2)==Fraction{2,3}); + CHECK((2*third)==Fraction{2,3}); + CHECK(std::is_eq((three*third)<=>Fraction{1,1})); + CHECK(std::is_eq((3*third)<=>Fraction{1,1})); + CHECK((3*third).normalized()==1); // multiply in place std::cout<1)); - CHECK(p4,one.normalized()==1); - CHECK(p4,one!=1); + CHECK(std::is_eq(one<=>1)); + CHECK(one.normalized()==1); + CHECK(one!=1); // end std::cout<