#include "catch.hpp" #include "functions/functions.h" TEST_CASE("Functions Generator", "[Functions]") { SECTION("D1ToD1") { SECTION("PiecewiseLinear") { GIVEN("A piecewise linear function") { std::vector positions = {0, 1, 2, 3}; std::vector values = {-1, 1, 1, 2}; std::function pw = D1ToD1::PiecewiseLinear(positions, values); THEN("Positions below give last value") { REQUIRE(pw(-1) == -1); } THEN("Positions above gile last value") { REQUIRE(pw(5) == 2); } THEN("Positions at given values give given values") { for (int i = 0; i < positions.size(); i++) { REQUIRE(pw(positions[i]) == values[i]); } } THEN("The points in between are calculated correctly") { REQUIRE(pw(0.5) == 0); REQUIRE(pw(1.5) == 1); REQUIRE(pw(2.5) == 1.5); } } } SECTION("Constant") { GIVEN("A constant function") { const double constant = 1.; std::function co = D1ToD1::Constant(constant); THEN("The constant is given back for all values") { REQUIRE(co(1.) == constant); REQUIRE(co(0.4) == constant); REQUIRE(co(-11.) == constant); REQUIRE(co(1E4) == constant); } } } SECTION("Product") { GIVEN("A function product") { std::vector positions = {0, 1, 2, 3}; std::vector values = {-1, 1, 1, 2}; std::function pw = D1ToD1::PiecewiseLinear(positions, values); std::function prod = D1ToD1::Product({pw, pw}); THEN("The product of the functions is returned") { for (int i = 0; i < positions.size(); i++) { REQUIRE(prod(positions[i]) == pw(positions[i]) * pw(positions[i])); } } } } } }