Exercise (FH): class rectangle
¶
Building class rectangle
Upon class point
¶
In the project you have set up in Exercise (FH): class point, you’ll find an a
test suite for a class rectangle
, again together with a sparse
implementation thereof.
Continuing the tradition of test driven development, continually
extend class rectangle
by adding the following tests one by one.
Important
Whenever an object of class rectangle
is passed as a parameter,
do not pass that object by copy! Use references instead!
Default Constructor¶
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, default_ctor)
{
const rectangle r;
}
Coordinates¶
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, coordinates)
{
{
const rectangle r;
ASSERT_EQ(r.bottom_left(), point(0,0));
ASSERT_EQ(r.top_right(), point(0,0));
ASSERT_EQ(r.top_left(), point(0,0));
ASSERT_EQ(r.bottom_right(), point(0,0));
}
{
const point bottom_left{1,2};
const point top_right{8,6};
const rectangle r{bottom_left, top_right};
ASSERT_EQ(r.bottom_left(), point(1,2));
ASSERT_EQ(r.top_right(), point(8,6));
ASSERT_EQ(r.top_left(), point(1,6));
ASSERT_EQ(r.bottom_right(), point(8,2));
}
}
Width, Height, Area¶
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, area)
{
{
const rectangle r;
ASSERT_EQ(r.width(), 0);
ASSERT_EQ(r.height(), 0);
ASSERT_EQ(r.area(), 0);
}
{
const rectangle r{point(3,4), point(8,6)};
ASSERT_EQ(r.width(), 5);
ASSERT_EQ(r.height(), 2);
ASSERT_EQ(r.area(), 10);
}
}
(In-)Equality¶
In addition to make the test run, please
implement that one in the
rectangle.cpp
filedo not pass the right hand side operand by copy (rationale: a rectangle is 16 bytes in size, so passing objects by reference is cheaper [1]).
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, operator_eq_ne)
{
const rectangle r1(point(1,2), point(3,4));
const rectangle r2(point(1,2), point(3,5));
bool b;
b = (r1 == r2);
ASSERT_FALSE(b);
b = (r1 == r1);
ASSERT_TRUE(b);
b = (r1 != r1);
ASSERT_FALSE(b);
b = (r1 != r2);
ASSERT_TRUE(b);
}
+=
¶
Implement that one in the rectangle.cpp
file, please.
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, operator_plus_equal_vec)
{
rectangle r(point(3,4), point(8,6));
const point vec(1,2);
const rectangle r1 = r += vec;
ASSERT_EQ(r.bottom_left(), point(4,6));
ASSERT_EQ(r.top_right(), point(9,8));
ASSERT_EQ(r, r1);
}
+
¶
Implement that one in the rectangle.cpp
file, please.
#include <gtest/gtest.h>
#include <rectangle.h>
TEST(rectangle_suite, plus_vec)
{
const rectangle r(point(3,4), point(8,6));
const point vec(1,2);
const rectangle r1 = r + vec;
ASSERT_EQ(r1.bottom_left(), point(4,6));
ASSERT_EQ(r1.top_right(), point(9,8));
ASSERT_EQ(r, rectangle(point(3,4), point(8,6)));
}
<<
(std::ostream
)¶
Implement that one in the rectangle.cpp
file, please.
#include <rectangle.h>
#include <sstream>
#include <gtest/gtest.h>
TEST(rectangle_suite, operator_ostream)
{
const rectangle r(point(3,4), point(8,6));
std::ostringstream buf;
buf << r;
ASSERT_EQ(buf.str(), "((3,4),(8,6))");
}
Footnotes