Exercise (FH): class point

Skeleton Project

The project contains an initial sparse test suite for - and an initial (likewise sparse) implementation of - class point. Take the following steps to get it, and get yourself an overview.

Preparation

$ mkdir ~/FH-work
$ cd ~/FH-work/
$ git clone https://github.com/jfasch/FH-ECE20-shapes.git
$ cd FH-ECE20-shapes/
$ git submodule init
$ git submodule update

Initial Build

$ mkdir ~/FH-work/FH-ECE20-shapes-x86_64
$ cd  ~/FH-work/FH-ECE20-shapes-x86_64
$ cmake ~/FH-work/FH-ECE20-shapes
$ make

Initial Test Run

$ pwd
/home/jfasch/FH-work/FH-ECE20-shapes-x86_64
$ ./tests/run-tests
Running main() from /home/jfasch/FH-work/FH-ECE20-shapes/googletest/googletest/src/gtest_main.cc
[==========] Running 3 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from point_suite
[ RUN      ] point_suite.starting_point
[       OK ] point_suite.starting_point (0 ms)
[ RUN      ] point_suite.implementation_in_cpp_file
[       OK ] point_suite.implementation_in_cpp_file (0 ms)
[----------] 2 tests from point_suite (0 ms total)

[----------] 1 test from rectangle_suite
[ RUN      ] rectangle_suite.starting_point
[       OK ] rectangle_suite.starting_point (0 ms)
[----------] 1 test from rectangle_suite (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 3 tests.

Extending class point: More Unit Tests

Important

One by one, add the following tests to tests/point-suite.cpp. Build, fix/hack, run, repeat, until everything is green.

Default Constructor

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, default_ctor)
{
    const point p;
}

Access Methods

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, xy)
{
    {
        const point p;
        ASSERT_EQ(p.x(), 0);
        ASSERT_EQ(p.y(), 0);
    }
    {
        const point p{1,2};
        ASSERT_EQ(p.x(), 1);
        ASSERT_EQ(p.y(), 2);
    }
}

(In-)Equality

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, operator_eq_ne)
{
    const point p{1,2};
    const point q{3,4};

    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 point.cpp file, please.

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, operator_pluseq)
{
    point p{1,2};
    const point vec{3,4};
    const point& p1 = p += vec;

    ASSERT_EQ(p.x(), 4);
    ASSERT_EQ(p.y(), 6);

    bool b = (p == p1);
    ASSERT_TRUE(b);

    ASSERT_EQ(&p1, &p);  // operator+=() returns point& !!
}

+

Implement that one in the point.cpp file, please.

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, operator_plus)
{
    const point p{1,2};
    const point vec{3,4};

    const point p1 = p + vec;

    ASSERT_EQ(p1.x(), 4);
    ASSERT_EQ(p1.y(), 6);
}

Unary -

#include <gtest/gtest.h>

#include <point.h>

TEST(point_suite, operator_unary_minus)
{
    const point p{1,2};
    const point minus_p = -p;

    ASSERT_EQ(minus_p.x(), -1);
    ASSERT_EQ(minus_p.y(), -2);
}

<< (std::ostream)

Implement that one in the point.cpp file, please.

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

#include <point.h>

TEST(point_suite, operator_ostream)
{
    const point p{1,2};

    std::ostringstream buf;
    buf << p;

    ASSERT_EQ(buf.str(), "(1,2)");
}