Skip to content

Commit d5536d3

Browse files
committed
Simplify the CHECK() macro of the operator exercise.
The first argument of the check macro is unnecessary if all checks are printed using the same width. This makes the code easier to read. Using the variadic macro approach from the previous commit, the solution could also be simplified.
1 parent a2d4e2e commit d5536d3

File tree

2 files changed

+55
-62
lines changed

2 files changed

+55
-62
lines changed

exercises/operators/operators.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class TestResultPrinter {
5959
};
6060

6161
// This is using the cpp, the C preprocessor to expand a bit of code
62-
// (the what argument) to a pair containing a string representation
62+
// (the arguments in '...') 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{50}.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()));
87-
88-
// equivalence
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()));
86+
87+
// equivalence & comparison
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: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ class TestResultPrinter {
7070
};
7171

7272
// This is using the cpp, the C preprocessor to expand a bit of code
73-
// (the what argument) to a pair containing a string representation
73+
// (the arguments in '...') 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{50}(#__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)