Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions exercises/operators/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -74,32 +74,29 @@ int main() {

// equality
std::cout<<std::endl;
TestResultPrinter p1{40};
CHECK(p1,equal(three,three));
CHECK(p1,equal(third,third));
CHECK(p1,equal(three,Fraction{3}));
CHECK(p1,equal(three,Fraction{3,1}));
CHECK(p1,equal(third,Fraction{1,3}));
CHECK(p1,equal(Fraction{3},three));
CHECK(p1,equal(Fraction{1,3},third));
CHECK(p1,!equal(third,Fraction{2,6}));
CHECK(p1,equal(third,Fraction{2,6}.normalized()));
CHECK(equal(three,three));
CHECK(equal(third,third));
CHECK(equal(three,Fraction{3}));
CHECK(equal(three,Fraction{3,1}));
CHECK(equal(third,Fraction{1,3}));
CHECK(equal(Fraction{3},three));
CHECK(equal(Fraction{1,3},third));
CHECK(!equal(third,Fraction{2,6}));
CHECK(equal(third,Fraction{2,6}.normalized()));

// equivalence
std::cout<<std::endl;
TestResultPrinter p2{32};
CHECK(p2,compare(third,Fraction{2,6})==0);
CHECK(p2,compare(third,Fraction{1,4})>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<<std::endl;
TestResultPrinter p3{48};
CHECK(p3,equal(multiply(third,2),Fraction{2,3}));
CHECK(p3,equal(multiply(2,third),Fraction{2,3}));
CHECK(p3,compare(multiply(three,third),Fraction{1,1})==0);
CHECK(p3,compare(multiply(3,third),Fraction{1,1})==0);
CHECK(p3,equal(multiply(3,third).normalized(),1));
CHECK(equal(multiply(third,2),Fraction{2,3}));
CHECK(equal(multiply(2,third),Fraction{2,3}));
CHECK(compare(multiply(three,third),Fraction{1,1})==0);
CHECK(compare(multiply(3,third),Fraction{1,1})==0);
CHECK(equal(multiply(3,third).normalized(),1));

// end
std::cout<<std::endl;
Expand Down
92 changes: 44 additions & 48 deletions exercises/operators/solution/operators_sol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ std::ostream & operator<<(std::ostream & os, Fraction const & f) {
}

class TestResultPrinter {

public:

TestResultPrinter( unsigned int a_width ) : m_width(a_width) {}

void operator()(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 operator()(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,what) printer(#what, what)
#define CHECK(...) TestResultPrinter::instance()(#__VA_ARGS__, (__VA_ARGS__))

int main() {

Expand All @@ -85,53 +85,49 @@ int main() {

// equality
std::cout<<std::endl;
TestResultPrinter p1{36};
CHECK(p1,three==three);
CHECK(p1,third==third);
CHECK(p1,three==Fraction{3});
CHECK(p1,(three==Fraction{3,1}));
CHECK(p1,(third==Fraction{1,3}));
CHECK(p1,(Fraction{3}==three));
CHECK(p1,(Fraction{1,3}==third));
CHECK(p1,(third!=Fraction{2,6}));
CHECK(p1,third==(Fraction{2,6}.normalized()));
CHECK(three==three);
CHECK(third==third);
CHECK(three==Fraction{3});
CHECK(three==Fraction{3,1});
CHECK(third==Fraction{1,3});
CHECK(Fraction{3}==three);
CHECK(Fraction{1,3}==third);
CHECK(third!=Fraction{2,6});
CHECK(third==(Fraction{2,6}.normalized()));

// equivalence & comparison
std::cout<<std::endl;
TestResultPrinter p2{34};
CHECK(p2,std::is_eq(third<=>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,4}));
CHECK(p2,(third<=Fraction{2,4}));
CHECK(p2,(third>=Fraction{1,3}));
CHECK(p2,(third<=Fraction{2,3}));
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(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,4});
CHECK(third<=Fraction{2,4});
CHECK(third>=Fraction{1,3});
CHECK(third<=Fraction{2,3});
CHECK(!(third<Fraction{1,4}));
CHECK(!(third>Fraction{2,4}));
CHECK(!(third<Fraction{1,3}));
CHECK(!(third>Fraction{2,3}));

// multiply
std::cout<<std::endl;
TestResultPrinter p3{42};
CHECK(p3,((third*2)==Fraction{2,3}));
CHECK(p3,((2*third)==Fraction{2,3}));
CHECK(p3,std::is_eq((three*third)<=>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<<std::endl;
TestResultPrinter p4{20};
Fraction one {third};
((one *= 2) *= 3) *= Fraction{1,2};
CHECK(p4,std::is_eq(one<=>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<<std::endl;
Expand Down