Chapter 7 Exercise Set 2: Doctest Exercises¶
We introduced doctest in Chapter 5 Extention: Introducing doctest. Review that section if you are not yet familiar with it.
Create a file named test_struct_functions.cpp with the following
scaffolding and then modify it according to the given problem:
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <string>
#include <doctest.h>
using namespace std;
// Your function goes here
TEST_CASE("...") {
CHECK(...);
}
Using the
Pointstructure introduced in Point objects, Add the following test case for the functiondistance_from_origin(Point)that takes aPoint,p, as an argument, and returns its distance from the origin,(0, 0)(Note: You’ll have to add:#include <cmath>to be able to use the exponential functions you’ll need for this function).TEST_CASE("distance_from_origin(p) returns p's distance from (0, 0) ") { p = Point{3, 4}; CHECK(distance_from_origin(p) == 5.0); p2 = Point{5, 12}; CHECK(distance_from_origin(p2) == 13.0); p3 = Point{65, 72}; CHECK(distance_from_origin(p3) == 97.0); }
Now add your own test case for the more general
distance(Point, Point)function discussed in Structues as parameters.