Exercise (FH): class cuboid

Building class cuboid Upon class point3d

In the project you have set up in Exercise (FH): class point, you’ll find an a test suite for a class cuboid, together with an empty cuboid.{h,cpp} combo.

Continuing the tradition of test driven development, continually extend class cuboid 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 <cuboid.h>

TEST(cuboid_suite, default_ctor)
{
    const cuboid c;

    ASSERT_EQ(c.front_bottom_left(), point3d(0,0,0));
    ASSERT_EQ(c.width(), 0);
    ASSERT_EQ(c.height(), 0);
    ASSERT_EQ(c.length(), 0);
}

Custom Constructor

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, point_whl_ctor)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};

    ASSERT_EQ(c.front_bottom_left(), point3d(1,2,3));
    ASSERT_EQ(c.width(), 4);
    ASSERT_EQ(c.height(), 5);
    ASSERT_EQ(c.length(), 6);
}

(In-)Equality

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_eq_ne)
{
    const cuboid c1{point3d{1,2,3}, 4, 5, 6};
    const cuboid c2{point3d{1,2,3}, 4, 5, 7};
    
    bool b;

    b = (c1 == c2);
    ASSERT_FALSE(b);

    b = (c1 != c2);
    ASSERT_TRUE(b);

    b = (c1 == c1);
    ASSERT_TRUE(b);

    b = (c1 != c1);
    ASSERT_FALSE(b);

    ASSERT_NE(c1, cuboid(point3d{2,3,4}, 4, 5, 6));
}

+=

  • Implement that one in the cuboid.cpp file, please.

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_pluseq)
{
    cuboid c{point3d{1,2,3}, 4, 5, 6};
    const point3d vec{3,4,5};

    const cuboid& c1 = c += vec;

    ASSERT_EQ(c.front_bottom_left(), point3d(4,6,8));
    ASSERT_EQ(c.width(), 4);
    ASSERT_EQ(c.height(), 5);
    ASSERT_EQ(c.length(), 6);

    bool b = (c == c1);
    ASSERT_TRUE(b);

    ASSERT_EQ(&c1, &c);  // operator+=() returns cuboid& !!
}

-=

  • Implement that one in the cuboid.cpp file, please.

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_minuseq)
{
    cuboid c{point3d{1,2,3}, 4, 5, 6};
    const point3d vec{3,4,5};

    const cuboid& c1 = c -= vec;

    ASSERT_EQ(c.front_bottom_left(), point3d(-2,-2,-2));
    ASSERT_EQ(c.width(), 4);
    ASSERT_EQ(c.height(), 5);
    ASSERT_EQ(c.length(), 6);

    bool b = (c == c1);
    ASSERT_TRUE(b);

    ASSERT_EQ(&c1, &c);  // operator-=() returns cuboid& !!
}

+

  • Implement that one in the cuboid.cpp file, please.

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_plus)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};
    const point3d vec{3,4,5};

    const cuboid c1 = c + vec;

    ASSERT_EQ(c1.front_bottom_left(), point3d(4,6,8));
    ASSERT_EQ(c1.width(), 4);
    ASSERT_EQ(c1.height(), 5);
    ASSERT_EQ(c1.length(), 6);
}

-

  • Implement that one in the cuboid.cpp file, please.

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_minus)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};
    const point3d vec{3,4,5};

    const cuboid c1 = c - vec;

    ASSERT_EQ(c1.front_bottom_left(), point3d(-2,-2,-2));
    ASSERT_EQ(c1.width(), 4);
    ASSERT_EQ(c1.height(), 5);
    ASSERT_EQ(c1.length(), 6);
}

<< (std::ostream)

  • Implement that one in the cuboid.cpp file, please.

  • Do not pass the right hand side operand by copy [1].

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, operator_ostream)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};

    std::ostringstream buf;
    buf << c;

    ASSERT_EQ(buf.str(), "cuboid((1,2,3),w=4,h=5,l=6)");
}

Surface

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, surface)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};
    ASSERT_EQ(c.surface(), 148);
}

Volume

#include <gtest/gtest.h>
#include <cuboid.h>

TEST(cuboid_suite, volume)
{
    const cuboid c{point3d{1,2,3}, 4, 5, 6};
    ASSERT_EQ(c.volume(), 120);
}

Footnotes