From d29109e5161e97cd7622d3e839aa095b4bf124ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 19 Apr 2020 00:40:17 +0200 Subject: [PATCH] Add unittests for calculation functions for vectors --- test/geometric/vector-operations.cpp | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/test/geometric/vector-operations.cpp b/test/geometric/vector-operations.cpp index 259df90..58178ea 100644 --- a/test/geometric/vector-operations.cpp +++ b/test/geometric/vector-operations.cpp @@ -41,3 +41,120 @@ TEST_CASE("geometric/vector-operations/vector_2d_calculate_angle_between", "[GEO angle = vector_2d_calculate_angle_between(&a, &b); REQUIRE(angle == Approx(90.0 / 180.0 * M_PI)); } + +TEST_CASE("geometric/vector-operations/vector_2d_subtract", "[GEOMETRIC]") +{ + struct vector_2d res; + struct vector_2d a; + struct vector_2d b; + + a.x = 1; + a.y = 2; + + b.x = 2; + b.y = 6; + + vector_2d_subtract(&res, &a, &b); + + REQUIRE(res.x == Approx(a.x - b.x)); + REQUIRE(res.y == Approx(a.y - b.y)); +} + +TEST_CASE("geometric/vector-operations/vector_2d_abs", "[GEOMETRIC]") +{ + struct vector_2d c; + struct vector_2d a; + struct vector_2d b; + double a_len, b_len, c_len; + + a.x = 1; + a.y = 0; + + b.x = 0; + b.y = 2; + + c.x = 3; + c.y = 4; + + a_len = vector_2d_abs(&a); + b_len = vector_2d_abs(&b); + c_len = vector_2d_abs(&c); + + REQUIRE(a_len == Approx(1.0)); + REQUIRE(b_len == Approx(2.0)); + REQUIRE(c_len == Approx(5.0)); +} + +TEST_CASE("geometric/vector-operations/vector_2d_scalar_multipy", "[GEOMETRIC]") +{ + struct vector_2d c; + struct vector_2d a; + struct vector_2d b; + double mult; + + a.x = 1; + a.y = 0; + b.x = 0; + b.y = 2; + mult = vector_2d_scalar_multipy(&a, &b); + REQUIRE(mult == Approx(0.0)); + + a.x = 1; + a.y = 1; + b.x = 1; + b.y = 1; + mult = vector_2d_scalar_multipy(&a, &b); + REQUIRE(mult == Approx(2.0)); +} + +TEST_CASE("geometric/vector-operations/vector_2d_normalize", "[GEOMETRIC]") +{ + struct vector_2d a; + + a.x = 1; + a.y = 0; + vector_2d_normalize(&a); + REQUIRE(a.x == Approx(1.0)); + REQUIRE(a.y == Approx(0.0)); + + a.x = 1; + a.y = -1; + vector_2d_normalize(&a); + REQUIRE(a.x == Approx(1.0/sqrt(2))); + REQUIRE(a.y == Approx(-1.0/sqrt(2))); +} + +TEST_CASE("geometric/vector-operations/vector_2d_rotate", "[GEOMETRIC]") +{ + struct vector_2d a; + + a.x = 1; + a.y = 0; + vector_2d_rotate(&a, M_PI/2); + REQUIRE(a.x == Approx(0.0).scale(0.001)); + REQUIRE(a.y == Approx(1.0)); + + a.x = 0; + a.y = 1; + vector_2d_rotate(&a, -M_PI/2); + vector_2d_rotate(&a, M_PI); + REQUIRE(a.x == Approx(-1.0)); + REQUIRE(a.y == Approx(0.0).scale(0.001)); +} + +TEST_CASE("geometric/vector-operations/vector_2d_scale", "[GEOMETRIC]") +{ + struct vector_2d a; + + a.x = 1; + a.y = 0; + vector_2d_scale(&a, 2.0); + REQUIRE(a.x == Approx(2.0)); + REQUIRE(a.y == Approx(0.0)); + + a.x = 1; + a.y = -3; + vector_2d_scale(&a, 0.5); + REQUIRE(a.x == Approx(0.5)); + REQUIRE(a.y == Approx(-1.5)); +}