From 6f8193244e87c194ae705afc096225cc4727156e Mon Sep 17 00:00:00 2001 From: Octavian-Mihai Matei Date: Wed, 29 Oct 2025 16:02:29 +0100 Subject: [PATCH 1/2] Fixed TestResultPrinter --- exercises/operators/operators.cpp | 61 ++++++------ .../operators/solution/operators_sol.cpp | 92 +++++++++---------- 2 files changed, 73 insertions(+), 80 deletions(-) diff --git a/exercises/operators/operators.cpp b/exercises/operators/operators.cpp index fa69514f..7dfb9f52 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< Date: Wed, 29 Oct 2025 15:15:37 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- exercises/operators/operators.cpp | 4 ++-- exercises/operators/solution/operators_sol.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/operators/operators.cpp b/exercises/operators/operators.cpp index 7dfb9f52..238fb355 100644 --- a/exercises/operators/operators.cpp +++ b/exercises/operators/operators.cpp @@ -48,9 +48,9 @@ class TestResultPrinter { static TestResultPrinter printer(64); return printer; } - + void process(std::string const & what, bool passed) { - std::cout << std::left << std::setw(m_width) << what << ": " + std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n'; } private: diff --git a/exercises/operators/solution/operators_sol.cpp b/exercises/operators/solution/operators_sol.cpp index aadc2dda..83e8bfb2 100644 --- a/exercises/operators/solution/operators_sol.cpp +++ b/exercises/operators/solution/operators_sol.cpp @@ -59,9 +59,9 @@ class TestResultPrinter { static TestResultPrinter printer(64); return printer; } - + void operator()(std::string const & what, bool passed) { - std::cout << std::left << std::setw(m_width) << what << ": " + std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n'; } private: