Skip to content

Commit db328c3

Browse files
committed
fix tests
1 parent 0399e12 commit db328c3

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

-8 KB
Binary file not shown.

src/6_Classes/shape_inheritance.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ class Rectangle : public Shape {
8787
double width, height;
8888
};
8989

90-
void testRuntimePolymorphism(TestRunner &runner) {
91-
std::unique_ptr<Shape> circle = std::make_unique<Circle>(5.0);
92-
std::unique_ptr<Shape> rectangle = std::make_unique<Rectangle>(4.0, 6.0);
93-
94-
double circleArea = circle->area();
95-
double rectangleArea = rectangle->area();
96-
97-
// Allow for small floating point error.
98-
runner.expectNear(circleArea, M_PI * 25, 1e-6, "runtime circle area");
99-
runner.expectNear(rectangleArea, 24.0, 1e-6, "runtime rectangle area");
100-
}
101-
10290
// ---------------- Optimal (Compile-time Polymorphism using CRTP) Solution
10391
// ----------------
10492
template <typename Derived> class ShapeCRTP {
@@ -124,17 +112,6 @@ class RectangleCRTP : public ShapeCRTP<RectangleCRTP> {
124112
double width, height;
125113
};
126114

127-
void testCompileTimePolymorphism(TestRunner &runner) {
128-
CircleCRTP circle(5.0);
129-
RectangleCRTP rectangle(4.0, 6.0);
130-
131-
double circleArea = circle.area();
132-
double rectangleArea = rectangle.area();
133-
134-
runner.expectNear(circleArea, M_PI * 25, 1e-6, "crtp circle area");
135-
runner.expectNear(rectangleArea, 24.0, 1e-6, "crtp rectangle area");
136-
}
137-
138115
// ---------------- Alternative (Using Composition) Solution ----------------
139116
class AreaStrategy {
140117
public:
@@ -195,6 +172,29 @@ struct TestRunner {
195172
};
196173
} // namespace
197174

175+
void testRuntimePolymorphism(TestRunner &runner) {
176+
std::unique_ptr<Shape> circle = std::make_unique<Circle>(5.0);
177+
std::unique_ptr<Shape> rectangle = std::make_unique<Rectangle>(4.0, 6.0);
178+
179+
double circleArea = circle->area();
180+
double rectangleArea = rectangle->area();
181+
182+
// Allow for small floating point error.
183+
runner.expectNear(circleArea, M_PI * 25, 1e-6, "runtime circle area");
184+
runner.expectNear(rectangleArea, 24.0, 1e-6, "runtime rectangle area");
185+
}
186+
187+
void testCompileTimePolymorphism(TestRunner &runner) {
188+
CircleCRTP circle(5.0);
189+
RectangleCRTP rectangle(4.0, 6.0);
190+
191+
double circleArea = circle.area();
192+
double rectangleArea = rectangle.area();
193+
194+
runner.expectNear(circleArea, M_PI * 25, 1e-6, "crtp circle area");
195+
runner.expectNear(rectangleArea, 24.0, 1e-6, "crtp rectangle area");
196+
}
197+
198198
void testComposition(TestRunner &runner) {
199199
ShapeComposition circleShape(std::make_unique<CircleAreaStrategy>(5.0));
200200
ShapeComposition rectangleShape(

0 commit comments

Comments
 (0)