Skip to content

Commit 6f81932

Browse files
committed
Fixed TestResultPrinter
1 parent d233b1f commit 6f81932

File tree

2 files changed

+73
-80
lines changed

2 files changed

+73
-80
lines changed

exercises/operators/operators.cpp

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ class Fraction {
4343
};
4444

4545
class TestResultPrinter {
46-
4746
public:
48-
49-
TestResultPrinter( unsigned int a_width ) : m_width(a_width) {}
50-
51-
void process(std::string const & what, bool passed) {
52-
std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n';
53-
}
54-
47+
static TestResultPrinter& instance() {
48+
static TestResultPrinter printer(64);
49+
return printer;
50+
}
51+
52+
void process(std::string const & what, bool passed) {
53+
std::cout << std::left << std::setw(m_width) << what << ": "
54+
<< (passed ? "PASS" : "** FAIL **") << '\n';
55+
}
5556
private:
56-
57-
unsigned int m_width;
58-
57+
TestResultPrinter(unsigned int a_width) : m_width(a_width) {}
58+
unsigned int m_width;
5959
};
6060

6161
// This is using the cpp, the C preprocessor to expand a bit of code
6262
// (the what argument) to a pair containing a string representation
6363
// of it and the code itself. That way, print is given a string and a
6464
// value where the string is the code that lead to the value
65-
#define CHECK(printer, ...) printer.process(#__VA_ARGS__, (__VA_ARGS__))
65+
#define CHECK(...) TestResultPrinter::instance().process(#__VA_ARGS__, (__VA_ARGS__))
6666

6767
int main() {
6868

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

7575
// equality
7676
std::cout<<std::endl;
77-
TestResultPrinter p1{40};
78-
CHECK(p1,equal(three,three));
79-
CHECK(p1,equal(third,third));
80-
CHECK(p1,equal(three,Fraction{3}));
81-
CHECK(p1,equal(three,Fraction{3,1}));
82-
CHECK(p1,equal(third,Fraction{1,3}));
83-
CHECK(p1,equal(Fraction{3},three));
84-
CHECK(p1,equal(Fraction{1,3},third));
85-
CHECK(p1,!equal(third,Fraction{2,6}));
86-
CHECK(p1,equal(third,Fraction{2,6}.normalized()));
77+
CHECK(equal(three,three));
78+
CHECK(equal(third,third));
79+
CHECK(equal(three,Fraction{3}));
80+
CHECK(equal(three,Fraction{3,1}));
81+
CHECK(equal(third,Fraction{1,3}));
82+
CHECK(equal(Fraction{3},three));
83+
CHECK(equal(Fraction{1,3},third));
84+
CHECK(!equal(third,Fraction{2,6}));
85+
CHECK(equal(third,Fraction{2,6}.normalized()));
8786

8887
// equivalence
8988
std::cout<<std::endl;
90-
TestResultPrinter p2{32};
91-
CHECK(p2,compare(third,Fraction{2,6})==0);
92-
CHECK(p2,compare(third,Fraction{1,4})>0);
93-
CHECK(p2,compare(third,Fraction{2,4})<0);
89+
CHECK(compare(third,Fraction{2,6})==0);
90+
CHECK(compare(third,Fraction{1,4})>0);
91+
CHECK(compare(third,Fraction{2,4})<0);
9492

9593
// multiply
9694
std::cout<<std::endl;
97-
TestResultPrinter p3{48};
98-
CHECK(p3,equal(multiply(third,2),Fraction{2,3}));
99-
CHECK(p3,equal(multiply(2,third),Fraction{2,3}));
100-
CHECK(p3,compare(multiply(three,third),Fraction{1,1})==0);
101-
CHECK(p3,compare(multiply(3,third),Fraction{1,1})==0);
102-
CHECK(p3,equal(multiply(3,third).normalized(),1));
95+
CHECK(equal(multiply(third,2),Fraction{2,3}));
96+
CHECK(equal(multiply(2,third),Fraction{2,3}));
97+
CHECK(compare(multiply(three,third),Fraction{1,1})==0);
98+
CHECK(compare(multiply(3,third),Fraction{1,1})==0);
99+
CHECK(equal(multiply(3,third).normalized(),1));
103100

104101
// end
105102
std::cout<<std::endl;

exercises/operators/solution/operators_sol.cpp

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,26 @@ std::ostream & operator<<(std::ostream & os, Fraction const & f) {
5454
}
5555

5656
class TestResultPrinter {
57-
5857
public:
59-
60-
TestResultPrinter( unsigned int a_width ) : m_width(a_width) {}
61-
62-
void operator()(std::string const & what, bool passed) {
63-
std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n';
64-
}
65-
58+
static TestResultPrinter& instance() {
59+
static TestResultPrinter printer(64);
60+
return printer;
61+
}
62+
63+
void operator()(std::string const & what, bool passed) {
64+
std::cout << std::left << std::setw(m_width) << what << ": "
65+
<< (passed ? "PASS" : "** FAIL **") << '\n';
66+
}
6667
private:
67-
68-
unsigned int m_width;
69-
68+
TestResultPrinter(unsigned int a_width) : m_width(a_width) {}
69+
unsigned int m_width;
7070
};
7171

7272
// This is using the cpp, the C preprocessor to expand a bit of code
7373
// (the what argument) to a pair containing a string representation
7474
// of it and the code itself. That way, print is given a string and a
7575
// value where the string is the code that lead to the value
76-
#define CHECK(printer,what) printer(#what, what)
76+
#define CHECK(...) TestResultPrinter::instance()(#__VA_ARGS__, (__VA_ARGS__))
7777

7878
int main() {
7979

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

8686
// equality
8787
std::cout<<std::endl;
88-
TestResultPrinter p1{36};
89-
CHECK(p1,three==three);
90-
CHECK(p1,third==third);
91-
CHECK(p1,three==Fraction{3});
92-
CHECK(p1,(three==Fraction{3,1}));
93-
CHECK(p1,(third==Fraction{1,3}));
94-
CHECK(p1,(Fraction{3}==three));
95-
CHECK(p1,(Fraction{1,3}==third));
96-
CHECK(p1,(third!=Fraction{2,6}));
97-
CHECK(p1,third==(Fraction{2,6}.normalized()));
88+
CHECK(three==three);
89+
CHECK(third==third);
90+
CHECK(three==Fraction{3});
91+
CHECK(three==Fraction{3,1});
92+
CHECK(third==Fraction{1,3});
93+
CHECK(Fraction{3}==three);
94+
CHECK(Fraction{1,3}==third);
95+
CHECK(third!=Fraction{2,6});
96+
CHECK(third==(Fraction{2,6}.normalized()));
9897

9998
// equivalence & comparison
10099
std::cout<<std::endl;
101-
TestResultPrinter p2{34};
102-
CHECK(p2,std::is_eq(third<=>Fraction{2,6}));
103-
CHECK(p2,std::is_gt(third<=>Fraction{1,4}));
104-
CHECK(p2,std::is_lt(third<=>Fraction{2,4}));
105-
CHECK(p2,(third>Fraction{1,4}));
106-
CHECK(p2,(third<Fraction{2,4}));
107-
CHECK(p2,!(third<=Fraction{1,4}));
108-
CHECK(p2,!(third>=Fraction{2,4}));
109-
CHECK(p2,(third>=Fraction{1,4}));
110-
CHECK(p2,(third<=Fraction{2,4}));
111-
CHECK(p2,(third>=Fraction{1,3}));
112-
CHECK(p2,(third<=Fraction{2,3}));
113-
CHECK(p2,!(third<Fraction{1,4}));
114-
CHECK(p2,!(third>Fraction{2,4}));
115-
CHECK(p2,!(third<Fraction{1,3}));
116-
CHECK(p2,!(third>Fraction{2,3}));
100+
CHECK(std::is_eq(third<=>Fraction{2,6}));
101+
CHECK(std::is_gt(third<=>Fraction{1,4}));
102+
CHECK(std::is_lt(third<=>Fraction{2,4}));
103+
CHECK(third>Fraction{1,4});
104+
CHECK(third<Fraction{2,4});
105+
CHECK(!(third<=Fraction{1,4}));
106+
CHECK(!(third>=Fraction{2,4}));
107+
CHECK(third>=Fraction{1,4});
108+
CHECK(third<=Fraction{2,4});
109+
CHECK(third>=Fraction{1,3});
110+
CHECK(third<=Fraction{2,3});
111+
CHECK(!(third<Fraction{1,4}));
112+
CHECK(!(third>Fraction{2,4}));
113+
CHECK(!(third<Fraction{1,3}));
114+
CHECK(!(third>Fraction{2,3}));
117115

118116
// multiply
119117
std::cout<<std::endl;
120-
TestResultPrinter p3{42};
121-
CHECK(p3,((third*2)==Fraction{2,3}));
122-
CHECK(p3,((2*third)==Fraction{2,3}));
123-
CHECK(p3,std::is_eq((three*third)<=>Fraction{1,1}));
124-
CHECK(p3,std::is_eq((3*third)<=>Fraction{1,1}));
125-
CHECK(p3,((3*third).normalized()==1));
118+
CHECK((third*2)==Fraction{2,3});
119+
CHECK((2*third)==Fraction{2,3});
120+
CHECK(std::is_eq((three*third)<=>Fraction{1,1}));
121+
CHECK(std::is_eq((3*third)<=>Fraction{1,1}));
122+
CHECK((3*third).normalized()==1);
126123

127124
// multiply in place
128125
std::cout<<std::endl;
129-
TestResultPrinter p4{20};
130126
Fraction one {third};
131127
((one *= 2) *= 3) *= Fraction{1,2};
132-
CHECK(p4,std::is_eq(one<=>1));
133-
CHECK(p4,one.normalized()==1);
134-
CHECK(p4,one!=1);
128+
CHECK(std::is_eq(one<=>1));
129+
CHECK(one.normalized()==1);
130+
CHECK(one!=1);
135131

136132
// end
137133
std::cout<<std::endl;

0 commit comments

Comments
 (0)