From f96c3f2c990dc2bf3af7a9c22bea4c71d2338f2b Mon Sep 17 00:00:00 2001
From: Lucas Stauder <git@lucas-stauder.de>
Date: Wed, 22 Jan 2025 17:06:03 +0000
Subject: [PATCH] feat: WiSe 2024/25

---
 CMakeLists.txt          | 43 ++++++++++++-----------
 src/histogram.cpp       |  2 +-
 src/histogram.h         |  8 -----
 src/histogram.test.cpp  | 19 +++++-----
 src/outliers.cpp        |  2 +-
 src/outliers.h          | 11 ------
 src/outliers.test.cpp   | 78 +++++++++++++++++------------------------
 src/statistics.cpp      |  2 +-
 src/statistics.h        |  9 -----
 src/statistics.test.cpp | 18 +++++-----
 10 files changed, 76 insertions(+), 116 deletions(-)
 delete mode 100644 src/histogram.h
 delete mode 100644 src/outliers.h
 delete mode 100644 src/statistics.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9d86cdf..d729549 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,46 +3,49 @@ project(Uebung6)
 
 set(CMAKE_CXX_STANDARD 14)
 
-add_executable(Uebung6 src/main.cpp
-        src/outliers.h
+add_executable(Uebung6
+        src/main.cpp
+        src/outliers.hpp
         src/outliers.cpp
         src/statistics.cpp
-        src/statistics.h)
+        src/statistics.hpp
+        src/histogram.cpp
+        src/histogram.hpp
+)
 
-# Fetch Catch2
+# Fetch GoogleTest
 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_outliers
-        src/outliers.h
+        src/outliers.hpp
         src/outliers.cpp
         src/outliers.test.cpp
 )
 add_executable(test_statistics
         src/statistics.cpp
-        src/statistics.h
+        src/statistics.hpp
         src/statistics.test.cpp
 )
 add_executable(test_histogram
         src/histogram.cpp
-        src/histogram.h
+        src/histogram.hpp
         src/histogram.test.cpp
 )
-target_link_libraries(test_outliers PRIVATE Catch2::Catch2WithMain)
-target_link_libraries(test_statistics PRIVATE Catch2::Catch2WithMain)
-target_link_libraries(test_histogram PRIVATE Catch2::Catch2WithMain)
+target_link_libraries(test_outliers PRIVATE GTest::gtest_main)
+target_link_libraries(test_statistics PRIVATE GTest::gtest_main)
+target_link_libraries(test_histogram PRIVATE GTest::gtest_main)
 target_include_directories(test_outliers PRIVATE src/)
 target_include_directories(test_statistics PRIVATE src/)
 target_include_directories(test_histogram PRIVATE src/)
-add_test(test_outliers ./test_outliers)
-add_test(test_statistics ./test_statistics)
-add_test(test_histogram ./test_histogram)
-set_property(TEST test_outliers PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1")
-set_property(TEST test_statistics PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1")
-set_property(TEST test_histogram PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1")
+include(GoogleTest)
+gtest_discover_tests(test_outliers)
+gtest_discover_tests(test_statistics)
+gtest_discover_tests(test_histogram)
diff --git a/src/histogram.cpp b/src/histogram.cpp
index b3e48db..cf17dd8 100644
--- a/src/histogram.cpp
+++ b/src/histogram.cpp
@@ -1,4 +1,4 @@
-#include "histogram.h"
+#include "histogram.hpp"
 
 #include <algorithm>
 #include <unordered_map>
diff --git a/src/histogram.h b/src/histogram.h
deleted file mode 100644
index 2484e4a..0000000
--- a/src/histogram.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef HISTOGRAM_H
-#define HISTOGRAM_H
-
-#include <vector>
-
-int getMostFrequentValue(const std::vector<int>& data);
-
-#endif  // HISTOGRAM_H
diff --git a/src/histogram.test.cpp b/src/histogram.test.cpp
index b79236b..ac45478 100644
--- a/src/histogram.test.cpp
+++ b/src/histogram.test.cpp
@@ -1,14 +1,13 @@
-#include "histogram.h"
+#include "histogram.hpp"
+#include <gtest/gtest.h>
 
-#include <catch2/catch_test_macros.hpp>
-
-TEST_CASE("getMostFrequentValue") {
-    REQUIRE(getMostFrequentValue({1, 2, 2, 3}) == 2);
-    REQUIRE(getMostFrequentValue({10}) == 10);
-    REQUIRE(getMostFrequentValue(
-                {1, 2, 2, 2, 3, 3, 1, 3, 2, 8, 32, 1, 3, 1, 2, 4, 3, 2, 3, 3, 3}) == 3);
+TEST(HistogramTest, GetMostFrequentValue) {
+    EXPECT_EQ(getMostFrequentValue({1, 2, 2, 3}), 2);
+    EXPECT_EQ(getMostFrequentValue({10}), 10);
+    EXPECT_EQ(getMostFrequentValue(
+                {1, 2, 2, 2, 3, 3, 1, 3, 2, 8, 32, 1, 3, 1, 2, 4, 3, 2, 3, 3, 3}), 3);
 
     const int mfv = getMostFrequentValue({1, 2, 2, 3, 3});
     const bool correct = (mfv == 2) || (mfv == 3);
-    REQUIRE(correct);
-}
+    EXPECT_TRUE(correct);
+}
\ No newline at end of file
diff --git a/src/outliers.cpp b/src/outliers.cpp
index b607e2d..574d32c 100644
--- a/src/outliers.cpp
+++ b/src/outliers.cpp
@@ -1,4 +1,4 @@
-#include "outliers.h"
+#include "outliers.hpp"
 
 double findNMin(const std::vector<double>& data, size_t n) {
     return 0;
diff --git a/src/outliers.h b/src/outliers.h
deleted file mode 100644
index 29ae0ab..0000000
--- a/src/outliers.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#ifndef UEBUNG5_OUTLIERS_H
-#define UEBUNG5_OUTLIERS_H
-
-#include <cstddef>
-#include <vector>
-
-double findNMin(const std::vector<double>& data, size_t n);
-double findNMax(const std::vector<double>& data, size_t n);
-std::vector<double> removeOutliers(std::vector<double> data, size_t n);
-
-#endif  // UEBUNG5_OUTLIERS_H
diff --git a/src/outliers.test.cpp b/src/outliers.test.cpp
index 793ea5c..802d63a 100644
--- a/src/outliers.test.cpp
+++ b/src/outliers.test.cpp
@@ -1,57 +1,45 @@
-#include "outliers.h"
+#include "outliers.hpp"
+#include <gtest/gtest.h>
+#include <algorithm>
 
-#include <catch2/catch_test_macros.hpp>
-#include <catch2/matchers/catch_matchers_vector.hpp>
-
-void allElementsInVector(std::vector<double> test, std::vector<double> correct) {
-    REQUIRE(test.size() == correct.size());
-    std::sort(test.begin(), test.end());
-    for (int i = 0; i < correct.size(); ++i) {
-        CHECK(test[i] == correct[i]);
+void allElementsInVector(const std::vector<double>& test, const std::vector<double>& correct) {
+    ASSERT_EQ(test.size(), correct.size());
+    std::vector<double> sorted_test = test;
+    std::sort(sorted_test.begin(), sorted_test.end());
+    for (size_t i = 0; i < correct.size(); ++i) {
+        EXPECT_EQ(sorted_test[i], correct[i]);
     }
 }
 
-TEST_CASE("findNMin") {
-    const std::vector<double> data = {
-        1, 2, 3, 4, 5,
-    };
-    REQUIRE(findNMin(data, 1) == 1);
-    REQUIRE(findNMin(data, 2) == 2);
-    REQUIRE(findNMin(data, 5) == 5);
+TEST(OutliersTest, FindNMin) {
+    std::vector<double> data = {1, 2, 3, 4, 5};
+    EXPECT_EQ(findNMin(data, 1), 1);
+    EXPECT_EQ(findNMin(data, 2), 2);
+    EXPECT_EQ(findNMin(data, 5), 5);
 
-    const std::vector<double> data2 = {
-        4, 2, 1, 2, 3,
-    };
-    REQUIRE(findNMin(data2, 1) == 1);
-    REQUIRE(findNMin(data2, 2) == 2);
-    REQUIRE(findNMin(data2, 5) == 4);
+    std::vector<double> data2 = {4, 2, 1, 2, 3};
+    EXPECT_EQ(findNMin(data2, 1), 1);
+    EXPECT_EQ(findNMin(data2, 2), 2);
+    EXPECT_EQ(findNMin(data2, 5), 4);
 }
 
-TEST_CASE("findNMax") {
-    const std::vector<double> data = {
-        1, 2, 3, 4, 5,
-    };
-    REQUIRE(findNMax(data, 1) == 5);
-    REQUIRE(findNMax(data, 2) == 4);
-    REQUIRE(findNMax(data, 5) == 1);
+TEST(OutliersTest, FindNMax) {
+    std::vector<double> data = {1, 2, 3, 4, 5};
+    EXPECT_EQ(findNMax(data, 1), 5);
+    EXPECT_EQ(findNMax(data, 2), 4);
+    EXPECT_EQ(findNMax(data, 5), 1);
 
-    const std::vector<double> data2 = {
-        4, 2, 1, 2, 3,
-    };
-    REQUIRE(findNMax(data2, 1) == 4);
-    REQUIRE(findNMax(data2, 2) == 3);
-    REQUIRE(findNMax(data2, 5) == 1);
+    std::vector<double> data2 = {4, 2, 1, 2, 3};
+    EXPECT_EQ(findNMax(data2, 1), 4);
+    EXPECT_EQ(findNMax(data2, 2), 3);
+    EXPECT_EQ(findNMax(data2, 5), 1);
 }
 
-TEST_CASE("removeOutliers") {
-    std::vector<double> data = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
-                                11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
-    std::vector<double> data2 = {1,  2,  3,  4,  5,  6,  7,  8,  9,  30,
-                                 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
-    std::vector<double> correct = {0,  0,  3,  4,  5,  6,  7,  8,  9,  10,
-                                   11, 12, 13, 14, 15, 16, 17, 18, 18, 18};
-    std::vector<double> correct2 = {0,  0,  3,  4,  5,  6,  7,  8,  9,  9,
-                                    11, 12, 13, 14, 15, 16, 17, 18, 19, 19};
+TEST(OutliersTest, RemoveOutliers) {
+    std::vector<double> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
+    std::vector<double> data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
+    std::vector<double> correct = {0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 18};
+    std::vector<double> correct2 = {0, 0, 3, 4, 5, 6, 7, 8, 9, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19};
     allElementsInVector(removeOutliers(data, 2), correct);
     allElementsInVector(removeOutliers(data2, 2), correct2);
-}
+}
\ No newline at end of file
diff --git a/src/statistics.cpp b/src/statistics.cpp
index 2bcb5a0..3c24994 100644
--- a/src/statistics.cpp
+++ b/src/statistics.cpp
@@ -1,4 +1,4 @@
-#include "statistics.h"
+#include "statistics.hpp"
 
 #include <algorithm>
 #include <numeric>
diff --git a/src/statistics.h b/src/statistics.h
deleted file mode 100644
index a236a32..0000000
--- a/src/statistics.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef STATISTICS_H
-#define STATISTICS_H
-
-#include <cstddef>
-#include <vector>
-
-size_t countValuesNearMean(const std::vector<double>& data);
-
-#endif  // STATISTICS_H
diff --git a/src/statistics.test.cpp b/src/statistics.test.cpp
index 922608f..c689a2d 100644
--- a/src/statistics.test.cpp
+++ b/src/statistics.test.cpp
@@ -1,11 +1,9 @@
-#include "statistics.h"
+#include "statistics.hpp"
+#include <gtest/gtest.h>
 
-#include <catch2/catch_test_macros.hpp>
-#include <catch2/matchers/catch_matchers_vector.hpp>
-
-TEST_CASE("countValuesNearMean") {
-    REQUIRE(countValuesNearMean({1, 2, 3}) == 1);
-    REQUIRE(countValuesNearMean({2, 3, 2, 1}) == 2);
-    REQUIRE(countValuesNearMean({1, 4, 2, 3}) == 0);
-    REQUIRE(countValuesNearMean({2.5, 2.5, 1, 4, 2, 3, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5}) == 9);
-}
+TEST(StatisticsTest, CountValuesNearMeanTest) {
+    EXPECT_EQ(countValuesNearMean({1, 2, 3}), 1);
+    EXPECT_EQ(countValuesNearMean({2, 3, 2, 1}), 2);
+    EXPECT_EQ(countValuesNearMean({1, 4, 2, 3}), 0);
+    EXPECT_EQ(countValuesNearMean({2.5, 2.5, 1, 4, 2, 3, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5}), 9);
+}
\ No newline at end of file
-- 
GitLab