Exercise (FH): class point3d
¶
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 point3d
, together with an empty
point3d.{h,cpp}
combo.
Continuing the tradition of test driven development, continually
extend class point3d
by adding the following tests one by one.
Default Constructor¶
Use the C++11 = default
and initialize the members at their
definition. (See here)
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, default_ctor)
{
const point3d p;
ASSERT_EQ(p.x(), 0);
ASSERT_EQ(p.y(), 0);
ASSERT_EQ(p.z(), 0);
}
Coordinates¶
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, coordinates_ctor)
{
const point3d p{1,2,3};
ASSERT_EQ(p.x(), 1);
ASSERT_EQ(p.y(), 2);
ASSERT_EQ(p.z(), 3);
}
(In-)Equality¶
Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_eq_ne)
{
const point3d p{1,2,3};
const point3d q{3,4,5};
bool b;
b = (p == q);
ASSERT_FALSE(b);
b = (p != q);
ASSERT_TRUE(b);
b = (p == p);
ASSERT_TRUE(b);
b = (p != p);
ASSERT_FALSE(b);
}
+=
¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_pluseq)
{
point3d p{1,2,3};
const point3d vec{3,4,5};
const point3d& p1 = p += vec;
ASSERT_EQ(p.x(), 4);
ASSERT_EQ(p.y(), 6);
ASSERT_EQ(p.z(), 8);
bool b = (p == p1);
ASSERT_TRUE(b);
ASSERT_EQ(&p1, &p); // operator+=() returns point3d& !!
}
-=
¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_minuseq)
{
point3d p{1,2,3};
const point3d vec{3,4,5};
const point3d& p1 = p -= vec;
ASSERT_EQ(p.x(), -2);
ASSERT_EQ(p.y(), -2);
ASSERT_EQ(p.z(), -2);
bool b = (p == p1);
ASSERT_TRUE(b);
ASSERT_EQ(&p1, &p); // operator-=() returns point3d& !!
}
+
¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_plus)
{
const point3d p{1,2,3};
const point3d vec{3,4,5};
const point3d p1 = p + vec;
ASSERT_EQ(p1.x(), 4);
ASSERT_EQ(p1.y(), 6);
ASSERT_EQ(p1.z(), 8);
}
-
¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_minus)
{
const point3d p{1,2,3};
const point3d vec{3,4,5};
const point3d p1 = p - vec;
ASSERT_EQ(p1.x(), -2);
ASSERT_EQ(p1.y(), -2);
ASSERT_EQ(p1.z(), -2);
}
Unary -
¶
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_unary_minus)
{
const point3d p{1,2,3};
const point3d minus_p = -p;
ASSERT_EQ(minus_p.x(), -1);
ASSERT_EQ(minus_p.y(), -2);
ASSERT_EQ(minus_p.z(), -3);
}
<<
(std::ostream
)¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, operator_ostream)
{
const point3d p{1,2,3};
std::ostringstream buf;
buf << p;
ASSERT_EQ(buf.str(), "(1,2,3)");
}
Absolute Value¶
Implement that one in the
point3d.cpp
file, please.Do not pass the right hand side operand by copy [1].
Use the
std::sqrt()
function from<cmath>
to compute a square root.
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, abs)
{
const point3d p{3,4,5};
double abs = p.abs();
ASSERT_FLOAT_EQ(abs, 7.071067812);
}
Distance Between Two Points¶
#include <gtest/gtest.h>
#include <point3d.h>
TEST(point3d_suite, distance)
{
const point3d p1{3,4,5};
const point3d p2{5,7,8};
double dst = p1.distance(p2);
ASSERT_FLOAT_EQ(dst, 4.69041576);
}
Footnotes