diff --git a/CMakeLists.txt b/CMakeLists.txt index 50ea1ec2c8ce77a1aba60401d564752251fe0a2b..e8a6cdd575788ded19142335fd19dcad6b4f8f53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,36 +6,49 @@ set(CMAKE_CXX_STANDARD 14) add_executable(Hausuebung3 src/main.cpp src/drvpath.cpp - src/drvpath.h + src/drvpath.hpp ) target_include_directories(Hausuebung3 PRIVATE 3rdparty/eigen-3.4.0) -# Fetch Catch2 +# Fetch GTest Include(FetchContent) FetchContent_Declare( - Catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v3.4.0 + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.15.2 ) -FetchContent_MakeAvailable(Catch2) + +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) enable_testing() + + add_executable(test_drvpath_public src/drvpath.cpp - src/drvpath.h + src/drvpath.hpp + src/testhelper.cpp + src/testhelper.hpp src/drvpath.test-public.cpp ) -add_executable(test_drvpath_all + +add_executable(test_drvpath_private src/drvpath.cpp - src/drvpath.h - src/drvpath.test-public.cpp + src/drvpath.hpp + src/testhelper.cpp + src/testhelper.hpp src/drvpath.test-private.cpp ) -target_link_libraries(test_drvpath_public PRIVATE Catch2::Catch2WithMain) -target_link_libraries(test_drvpath_all PRIVATE Catch2::Catch2WithMain) + +target_link_libraries(test_drvpath_public PRIVATE GTest::gtest_main) +target_link_libraries(test_drvpath_private PRIVATE GTest::gtest_main) target_include_directories(test_drvpath_public PRIVATE src 3rdparty/eigen-3.4.0) -target_include_directories(test_drvpath_all PRIVATE src 3rdparty/eigen-3.4.0) +target_include_directories(test_drvpath_private PRIVATE src 3rdparty/eigen-3.4.0) add_test(test_drvpath_public ./test_drvpath_public) -add_test(test_drvpath_all ./test_drvpath_all) -set_property(TEST test_drvpath_public PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") -set_property(TEST test_drvpath_all PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") +add_test(test_drvpath_private ./test_drvpath_private) + +include(GoogleTest) + +gtest_discover_tests(test_drvpath_public) +gtest_discover_tests(test_drvpath_private) + diff --git a/src/drvpath.cpp b/src/drvpath.cpp index cde5a2093575bbc5f08c01f9c9b99f5317be35dc..13a6261b00cf8587e942941ea84a2fc88234deff 100644 --- a/src/drvpath.cpp +++ b/src/drvpath.cpp @@ -1,4 +1,4 @@ -#include "drvpath.h" +#include "drvpath.hpp" #include <cmath> diff --git a/src/drvpath.h b/src/drvpath.hpp similarity index 100% rename from src/drvpath.h rename to src/drvpath.hpp diff --git a/src/drvpath.test-private.cpp b/src/drvpath.test-private.cpp index 1bd27289ab9ddc130670f38914e114f5e966914d..cb04f88e7d6acc7fd07993ac4bf9286c8dda95e4 100644 --- a/src/drvpath.test-private.cpp +++ b/src/drvpath.test-private.cpp @@ -1,6 +1,7 @@ -#include "drvpath.h" +#include "drvpath.hpp" -#include <catch2/catch_test_macros.hpp> -#include <catch2/matchers/catch_matchers_vector.hpp> +#include <gtest/gtest.h> -TEST_CASE("testcase 1 private") { REQUIRE(false); } +TEST(DrvPathPrivatTest,Example) { + FAIL(); +} diff --git a/src/drvpath.test-public.cpp b/src/drvpath.test-public.cpp index f76575d7b38e484d27389b365c044c0a333e391f..b4d0b2226d9245afb9cdc47751eefca281b2352f 100644 --- a/src/drvpath.test-public.cpp +++ b/src/drvpath.test-public.cpp @@ -1,48 +1,12 @@ -#include "drvpath.h" +#include "drvpath.hpp" -#include <catch2/catch_test_macros.hpp> -#include <catch2/matchers/catch_matchers_vector.hpp> +#include <gtest/gtest.h> -bool are_point_vectors_almost_equal(const std::vector<Point>& v1, - const std::vector<Point>& v2, - double tolerance) { - if (v1.size() != v2.size()) { - return false; - } +#include "testhelper.hpp" - for (size_t i = 0; i < v1.size(); ++i) { - const double dx = v1[i].x - v2[i].x; - const double dy = v1[i].y - v2[i].y; - if ((std::abs(dx) > tolerance) || (std::abs(dy) > tolerance)) { - return false; - } - } - return true; -} - -std::vector<double> flat_matrix(const Eigen::MatrixXd& m) { - const int rows = m.rows(); - const int cols = m.cols(); - - std::vector<double> res{}; - res.reserve(rows * cols); - - for (int r = 0; r < rows; ++r) { - for (int c = 0; c < cols; ++c) { - res.push_back(m(r, c)); - } - } - - return res; -} - -std::vector<double> flat_vector(const Eigen::VectorXd& v) { - return {v.begin(), v.end()}; -} - -TEST_CASE("get_car_points") { +TEST(DrvPathPublicTest,get_car_points) { const std::vector<Point> lane{{2.0, -2.0}, {3.0, -2.0}, {4.5, -1.75}, {5.5, -1.5}, {6.5, -1.0}, {8, -0.25}, {10.0, 0.75}, {11.0, 1.5}}; @@ -58,10 +22,10 @@ TEST_CASE("get_car_points") { const std::vector<Point> path = get_car_path_points(lane, 1.5); - REQUIRE(are_point_vectors_almost_equal(path, path_sol, 1e-6)); + EXPECT_TRUE(are_point_vectors_almost_equal(path, path_sol, 1e-6)); } -TEST_CASE("get_direct_path_length") { +TEST(DrvPathPublicTest,get_direct_path_length) { const std::vector<Point> path{{0.0, 0.0}, {2.5, -0.5}, {3.5034015190419643, -0.3954091142517844}, @@ -81,11 +45,10 @@ TEST_CASE("get_direct_path_length") { 10.275392402085236}; const std::vector<double> path_length = get_direct_path_length(path); - REQUIRE_THAT(path_length, - Catch::Matchers::Approx(path_length_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(path_length, path_length_sol, 1e-6)); } -TEST_CASE("normalize_path_length") { +TEST(DrvPathPublicTest,normalize_path_length) { const std::vector<double> path_length{0.0, 2.5495097567963922, 3.5583476336613113, @@ -106,23 +69,24 @@ TEST_CASE("normalize_path_length") { const std::vector<double> p = normalize_path_length(path_length, 2.0); - REQUIRE_THAT(p, Catch::Matchers::Approx(p_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_sol, 1e-6)); } -TEST_CASE("get_coordinate") { +TEST(DrvPathPublicTest,get_coordinate) { const std::vector<Point> lane{{2.0, -2.0}, {3.0, -2.0}, {4.5, -1.75}, {5.5, -1.5}, {6.5, -1.0}, {8, -0.25}, {10.0, 0.75}, {11.0, 1.5}}; const std::vector<double> x = get_coordinate(lane, 0); const std::vector<double> y = get_coordinate(lane, 1); + const std::vector<double> x_sol{2, 3, 4.5, 5.5, 6.5, 8, 10, 11}; + const std::vector<double> y_sol{-2, -2, -1.75, -1.5, -1, -0.25, 0.75, 1.5}; - REQUIRE(x == std::vector<double>{2, 3, 4.5, 5.5, 6.5, 8, 10, 11}); - REQUIRE(y == - std::vector<double>{-2, -2, -1.75, -1.5, -1, -0.25, 0.75, 1.5}); + EXPECT_TRUE(are_double_vectors_almost_equal(x, x_sol, 1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(y, y_sol, 1e-6)); } -TEST_CASE("get_objective_Psi_eta") { +TEST(DrvPathPublicTest,get_objective_Psi_eta) { const std::vector<double> p{0.0, 0.49623598925117596, 0.6925959602164095, @@ -217,13 +181,11 @@ TEST_CASE("get_objective_Psi_eta") { const auto psi_eta = get_objective_Psi_eta(p, x); - REQUIRE_THAT(flat_matrix(psi_eta.first), - Catch::Matchers::Approx(psi_sol).margin(1e-6)); - REQUIRE_THAT(flat_vector(psi_eta.second), - Catch::Matchers::Approx(eta_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_matrix(psi_eta.first), psi_sol, 1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_vector(psi_eta.second), eta_sol, 1e-6)); } -TEST_CASE("get_constraints_A_b") { +TEST(DrvPathPublicTest,get_constraints_A_b) { const std::vector<double> a_sol{1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, -1.0, 3.0, 2.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 6.0, 2.0, 0.0, 0.0, 0.0, -2.0, 0.0, 0.0, @@ -232,13 +194,11 @@ TEST_CASE("get_constraints_A_b") { const auto a_b = get_constraints_A_b(); - REQUIRE_THAT(flat_matrix(a_b.first), - Catch::Matchers::Approx(a_sol).margin(1e-6)); - REQUIRE_THAT(flat_vector(a_b.second), - Catch::Matchers::Approx(b_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_matrix(a_b.first), a_sol, 1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_vector(a_b.second), b_sol, 1e-6)); } -TEST_CASE("get_quadprog_L_r") { +TEST(DrvPathPublicTest,get_quadprog_L_r) { const Eigen::MatrixXd psi{{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, {0.12219819027609326, 0.24625015702809322, 0.49623598925117596, 1.0, 0.0, 0.0, 0.0, 0.0}, @@ -422,13 +382,11 @@ TEST_CASE("get_quadprog_L_r") { const auto l_r = get_quadprog_L_r(psi, eta, a, b); - REQUIRE_THAT(flat_matrix(l_r.first), - Catch::Matchers::Approx(l_sol).margin(1e-6)); - REQUIRE_THAT(flat_vector(l_r.second), - Catch::Matchers::Approx(r_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_matrix(l_r.first), l_sol, 1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(flat_vector(l_r.second), r_sol, 1e-6)); } -TEST_CASE("get_path_spline") { +TEST(DrvPathPublicTest,get_path_spline) { const std::vector<double> p{0.0, 0.49623598925117596, 0.6925959602164095, @@ -455,5 +413,5 @@ TEST_CASE("get_path_spline") { const std::vector<double> theta = get_path_spline(p, x); - REQUIRE_THAT(theta, Catch::Matchers::Approx(theta_sol).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(theta, theta_sol, 1e-6)); } diff --git a/src/main.cpp b/src/main.cpp index 9b2832b0071267c621023c3f4bf4a5165b54f96c..b0c798d0cd1cabe3dac443165c1d98b96a3bc69a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -#include "drvpath.h" +#include "drvpath.hpp" int main() { std::vector<Point> lane{{2.0, -2.0}, {3.0, -2.0}, {4.5, -1.75}, diff --git a/src/testhelper.cpp b/src/testhelper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e96a0db97ca6260fb5e6f797b101aa7e848bdcd --- /dev/null +++ b/src/testhelper.cpp @@ -0,0 +1,51 @@ +#include "testhelper.hpp" + +bool are_point_vectors_almost_equal(const std::vector<Point>& v1, + const std::vector<Point>& v2, + double tolerance) { + if (v1.size() != v2.size()) { + return false; + } + + for (size_t i = 0; i < v1.size(); ++i) { + const double dx = v1[i].x - v2[i].x; + const double dy = v1[i].y - v2[i].y; + + if ((std::abs(dx) > tolerance) || (std::abs(dy) > tolerance)) { + return false; + } + } + + return true; +} + +std::vector<double> flat_matrix(const Eigen::MatrixXd& m) { + const int rows = m.rows(); + const int cols = m.cols(); + + std::vector<double> res{}; + res.reserve(rows * cols); + + for (int r = 0; r < rows; ++r) { + for (int c = 0; c < cols; ++c) { + res.push_back(m(r, c)); + } + } + + return res; +} + +std::vector<double> flat_vector(const Eigen::VectorXd& v) { + return {v.begin(), v.end()}; +} +bool are_double_vectors_almost_equal(const std::vector<double>& v1, const std::vector<double>& v2, double tolerance) { + if (v1.size() != v2.size()) { + return false; + } + for (size_t i = 0; i < v1.size(); ++i) { + if (std::abs(v1[i] - v2[i]) > tolerance) { + return false; + } + } + return true; +} \ No newline at end of file diff --git a/src/testhelper.hpp b/src/testhelper.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f68f4cd4890679a92de58bd2a1504f34b7356333 --- /dev/null +++ b/src/testhelper.hpp @@ -0,0 +1,19 @@ +#ifndef PROGAUT_HAUSUEBUNG3_TESTHELPER_HPP +#define PROGAUT_HAUSUEBUNG3_TESTHELPER_HPP + +#include <vector> + +#include "Eigen/Dense" +#include "drvpath.hpp" + +bool are_point_vectors_almost_equal(const std::vector<Point>& v1, + const std::vector<Point>& v2, + double tolerance); + +std::vector<double> flat_matrix(const Eigen::MatrixXd& m); + +std::vector<double> flat_vector(const Eigen::VectorXd& v); + +bool are_double_vectors_almost_equal(const std::vector<double>& v1, const std::vector<double>& v2, double tolerance); + +#endif // PROGAUT_HAUSUEBUNG3_TESTHELPER_HPP