diff --git a/eigen/CMakeLists.txt b/eigen/CMakeLists.txt index 2b468275a33b1986febdc2a41c2196e8faf51b56..d9890d964fe331c5bdb7cf963bdf8dd1da154cc3 100644 --- a/eigen/CMakeLists.txt +++ b/eigen/CMakeLists.txt @@ -6,44 +6,56 @@ set(CMAKE_CXX_STANDARD 14) add_executable(Uebung7_eigen src/main.cpp src/polyfit.cpp - src/polyfit.h + src/polyfit.hpp ) target_include_directories(Uebung7_eigen 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_polyfit3 src/polyfit.cpp - src/polyfit.h + src/polyfit.hpp src/polyfit3.test.cpp + src/test_helper.cpp + src/test_helper.hpp ) add_executable(test_polyfit src/polyfit.cpp - src/polyfit.h +src/polyfit.hpp src/polyfit.test.cpp + src/test_helper.cpp + src/test_helper.hpp ) add_executable(test_polyrank src/polyfit.cpp - src/polyfit.h + src/polyfit.hpp src/polyrank.test.cpp + src/test_helper.cpp + src/test_helper.hpp ) -target_link_libraries(test_polyfit3 PRIVATE Catch2::Catch2WithMain) -target_link_libraries(test_polyfit PRIVATE Catch2::Catch2WithMain) -target_link_libraries(test_polyrank PRIVATE Catch2::Catch2WithMain) +target_link_libraries(test_polyfit3 PRIVATE GTest::gtest_main) +target_link_libraries(test_polyfit PRIVATE GTest::gtest_main) +target_link_libraries(test_polyrank PRIVATE GTest::gtest_main) target_include_directories(test_polyfit3 PRIVATE src 3rdparty/eigen-3.4.0) target_include_directories(test_polyfit PRIVATE src 3rdparty/eigen-3.4.0) target_include_directories(test_polyrank PRIVATE src 3rdparty/eigen-3.4.0) add_test(test_polyfit3 ./test_polyfit3) add_test(test_polyfit ./test_polyfit) add_test(test_polyrank ./test_polyrank) -set_property(TEST test_polyfit3 PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") -set_property(TEST test_polyfit PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") -set_property(TEST test_polyrank PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") + +include(GoogleTest) + +gtest_discover_tests(test_polyfit3) +gtest_discover_tests(test_polyfit) +gtest_discover_tests(test_polyrank) diff --git a/eigen/src/main.cpp b/eigen/src/main.cpp index 55f01042f7d12e26754f896822652e6e394fb573..0683365b011d75ad5d427d242bd996da3272c8f0 100644 --- a/eigen/src/main.cpp +++ b/eigen/src/main.cpp @@ -1,7 +1,7 @@ #include <iostream> #include "Eigen/Dense" -#include "polyfit.h" +#include "polyfit.hpp" int main() { Eigen::MatrixXd m = Eigen::MatrixXd::Zero(3, 2); diff --git a/eigen/src/polyfit.cpp b/eigen/src/polyfit.cpp index ca2b0020924408664198d7a73e8a715495f76d80..99314c93fbfd88c392862dbdd66007def9f3b3a8 100644 --- a/eigen/src/polyfit.cpp +++ b/eigen/src/polyfit.cpp @@ -1,4 +1,4 @@ -#include "polyfit.h" +#include "polyfit.hpp" #include <algorithm> #include <iostream> diff --git a/eigen/src/polyfit.h b/eigen/src/polyfit.hpp similarity index 100% rename from eigen/src/polyfit.h rename to eigen/src/polyfit.hpp diff --git a/eigen/src/polyfit.test.cpp b/eigen/src/polyfit.test.cpp index 6676d7e0c595b2bbdb6bb399a2575607cc2216f4..c74d5133006fc11542697e615fd7ca14efffed6b 100644 --- a/eigen/src/polyfit.test.cpp +++ b/eigen/src/polyfit.test.cpp @@ -1,49 +1,54 @@ -#include "polyfit.h" +#include "polyfit.hpp" +#include <cmath> +#include <gtest/gtest.h> +#include <test_helper.hpp> -#include <catch2/catch_test_macros.hpp> -#include <catch2/matchers/catch_matchers_vector.hpp> - -TEST_CASE("polyfit exact solution, order 3 (constant)") { +TEST(PolyfitTest, ExactSolutionOrder3Constant) { std::vector<double> x{0.0, 1.0, 2.0, 3.0}; std::vector<double> y1{-1.0, -1.0, -1.0, -1.0}; std::vector<double> p = polyfit(x, y1, 3); + std::vector<double> p_soll{0.0, 0.0, 0.0, -1.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{0.0, 0.0, 0.0, -1.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit exact solution, order 0, overdetermined (constant)") { +TEST(PolyfitTest, ExactSolutionOrder0OverdeterminedConstant) { std::vector<double> x{0.0, 1.0, 2.0, 3.0}; std::vector<double> y1{-1.0, -1.0, -1.0, -1.0}; std::vector<double> p = polyfit(x, y1, 0); + std::vector<double> p_soll{-1.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{-1.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit exact solution, order 0 (constant)") { +TEST(PolyfitTest, ExactSolutionOrder0Constant) { std::vector<double> x{10.0}; std::vector<double> y1{42.0}; std::vector<double> p = polyfit(x, y1, 0); + std::vector<double> p_soll{42.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{42.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit with noise, order 0 (constant)") { +TEST(PolyfitTest, WithNoiseOrder0Constant) { std::vector<double> x{10.0, 20.0, -2.0, 33.0}; std::vector<double> y1{5.5, 6.7, 4.5, 3.3}; std::vector<double> p = polyfit(x, y1, 0); + std::vector<double> p_soll{5.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{5.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit approx, order 1 (constant)") { +TEST(PolyfitTest, ApproxOrder1Constant) { std::vector<double> x{10.0, 25.0, 38.0, 40.0}; std::vector<double> y1{22, 24, 24, 22}; std::vector<double> p = polyfit(x, y1, 0); + std::vector<double> p_soll{23.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{23.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } diff --git a/eigen/src/polyfit3.test.cpp b/eigen/src/polyfit3.test.cpp index 82fd1aab8b93c95531f50dce54ef53690d47c165..76ccf79549577e107b0bccc2c870cb3268c529e8 100644 --- a/eigen/src/polyfit3.test.cpp +++ b/eigen/src/polyfit3.test.cpp @@ -1,50 +1,55 @@ -#include <catch2/catch_test_macros.hpp> -#include <catch2/matchers/catch_matchers_vector.hpp> +#include <gtest/gtest.h> +#include "polyfit.hpp" +#include <cmath> -#include "polyfit.h" +#include "test_helper.hpp" -TEST_CASE("polyfit3 exact solution (constant)") { +TEST(PolyfitTest, ExactSolutionConstant) { std::vector<double> x{0.0, 1.0, 2.0, 3.0}; std::vector<double> y1{-1.0, -1.0, -1.0, -1.0}; std::vector<double> p = polyfit3(x, y1); + std::vector<double> p_soll{0.0, 0.0, 0.0, -1.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{0.0, 0.0, 0.0, -1.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit3 exact solution overdetermined (constant)") { +TEST(PolyfitTest, ExactSolutionOverdeterminedConstant) { std::vector<double> x{0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; std::vector<double> y1{-1.0, -1.0, -1.0, -1.0, -1.0, -1.0}; std::vector<double> p = polyfit3(x, y1); + std::vector<double> p_soll{0.0, 0.0, 0.0, -1.0}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{0.0, 0.0, 0.0, -1.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit3 exact solution (linear)") { +TEST(PolyfitTest, ExactSolutionLinear) { std::vector<double> x{0.0, 1.0, 2.0, 3.0}; std::vector<double> y1{-10.0, -5.0, 0.0, 5.0}; std::vector<double> p = polyfit3(x, y1); + std::vector<double> p_soll{0.0, 0.0, 5.0, -10.0}; - REQUIRE_THAT(p, - Catch::Matchers::Approx(std::vector<double>{0.0, 0.0, 5.0, -10.0}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit3 exact solution (cubic)") { +TEST(PolyfitTest, ExactSolutionCubic) { std::vector<double> x{-1.0, 1.0, 2.0, 3.0}; std::vector<double> y1{-0.8, 1.2, 8.2, 27.2}; std::vector<double> p = polyfit3(x, y1); + std::vector<double> p_soll{1.0, 0.0, 0.0, 0.2}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{1.0, 0.0, 0.0, 0.2}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } -TEST_CASE("polyfit3 with noise (cubic)") { +TEST(PolyfitTest, WithNoiseCubic) { std::vector<double> x{-1.0, 1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0}; std::vector<double> y1{-0.8, 1.2, 8.2, 27.2, 0.3, 0.1, 0.3, 0.1}; std::vector<double> p = polyfit3(x, y1); + std::vector<double> p_soll{1.0, 0.0, 0.0, 0.2}; - REQUIRE_THAT(p, Catch::Matchers::Approx(std::vector<double>{1.0, 0.0, 0.0, 0.2}).margin(1e-6)); + EXPECT_TRUE(are_double_vectors_almost_equal(p, p_soll, 1e-6)); } diff --git a/eigen/src/polyrank.test.cpp b/eigen/src/polyrank.test.cpp index 5571d0b0befe47effbfe656bf27c28ec0c12afd0..03c3d7e7da93befba8dbcd00a848d0c70acf373b 100644 --- a/eigen/src/polyrank.test.cpp +++ b/eigen/src/polyrank.test.cpp @@ -1,27 +1,25 @@ -#include <catch2/catch_test_macros.hpp> -#include <catch2/matchers/catch_matchers_vector.hpp> +#include <gtest/gtest.h> +#include "polyfit.hpp" -#include "polyfit.h" - -TEST_CASE("polyrank only unique input values") { - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0}, 3) == true); - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0}, 4) == false); - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 2.0}, 3) == false); - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0, 4.0}, 3) == true); - REQUIRE(polyfit_check_input_rank({}, 0) == false); - REQUIRE(polyfit_check_input_rank({0.0}, 0) == true); +TEST(PolyfitTest, OnlyUniqueInputValues) { + EXPECT_TRUE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0}, 3)); + EXPECT_FALSE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0}, 4)); + EXPECT_FALSE(polyfit_check_input_rank({0.0, 1.0, 2.0}, 3)); + EXPECT_TRUE(polyfit_check_input_rank({0.0, 1.0, 2.0, 3.0, 4.0}, 3)); + EXPECT_FALSE(polyfit_check_input_rank({}, 0)); + EXPECT_TRUE(polyfit_check_input_rank({0.0}, 0)); } -TEST_CASE("polyrank non unique input values sorted") { - REQUIRE(polyfit_check_input_rank({1.0, 1.0, 2.0, 3.0}, 3) == false); - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 1.0, 2.0, 3.0, 3.0}, 3) == true); - REQUIRE(polyfit_check_input_rank({0.0, 1.0, 1.0, 2.0, 3.0, 3.0}, 4) == false); - REQUIRE(polyfit_check_input_rank({0.0, 0.0, 0.0, 0.0}, 0) == true); - REQUIRE(polyfit_check_input_rank({0.0, 0.0, 0.0, 0.0}, 1) == false); +TEST(PolyfitTest, NonUniqueInputValuesSorted) { + EXPECT_FALSE(polyfit_check_input_rank({1.0, 1.0, 2.0, 3.0}, 3)); + EXPECT_TRUE(polyfit_check_input_rank({0.0, 1.0, 1.0, 2.0, 3.0, 3.0}, 3)); + EXPECT_FALSE(polyfit_check_input_rank({0.0, 1.0, 1.0, 2.0, 3.0, 3.0}, 4)); + EXPECT_TRUE(polyfit_check_input_rank({0.0, 0.0, 0.0, 0.0}, 0)); + EXPECT_FALSE(polyfit_check_input_rank({0.0, 0.0, 0.0, 0.0}, 1)); } -TEST_CASE("polyrank non unique input values unsorted") { - REQUIRE(polyfit_check_input_rank({3.0, 1.0, 2.0, 1.0}, 3) == false); - REQUIRE(polyfit_check_input_rank({2.0, 1.0, 3.0, 0.0, 1.0, 3.0}, 3) == true); - REQUIRE(polyfit_check_input_rank({2.0, 1.0, 3.0, 0.0, 1.0, 3.0}, 4) == false); +TEST(PolyfitTest, NonUniqueInputValuesUnsorted) { + EXPECT_FALSE(polyfit_check_input_rank({3.0, 1.0, 2.0, 1.0}, 3)); + EXPECT_TRUE(polyfit_check_input_rank({2.0, 1.0, 3.0, 0.0, 1.0, 3.0}, 3)); + EXPECT_FALSE(polyfit_check_input_rank({2.0, 1.0, 3.0, 0.0, 1.0, 3.0}, 4)); } diff --git a/eigen/src/test_helper.cpp b/eigen/src/test_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b918b9101f2409d2a3f9471842bb66a2ea80e94 --- /dev/null +++ b/eigen/src/test_helper.cpp @@ -0,0 +1,14 @@ +#include "test_helper.hpp" + +#include "Eigen/Dense" +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/eigen/src/test_helper.hpp b/eigen/src/test_helper.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6136dc0dbc489f97d83eac1986d47d7334236f50 --- /dev/null +++ b/eigen/src/test_helper.hpp @@ -0,0 +1,5 @@ +#ifndef TEST_HELPER_HPP +#define TEST_HELPER_HPP +#include <vector> +bool are_double_vectors_almost_equal(const std::vector<double>& v1, const std::vector<double>& v2, double tolerance); +#endif //TEST_HELPER_HPP