Skip to content
Snippets Groups Projects
Commit f96c3f2c authored by Stauder, Lucas's avatar Stauder, Lucas
Browse files

feat: WiSe 2024/25

parent c5264763
No related branches found
No related tags found
No related merge requests found
...@@ -3,46 +3,49 @@ project(Uebung6) ...@@ -3,46 +3,49 @@ project(Uebung6)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
add_executable(Uebung6 src/main.cpp add_executable(Uebung6
src/outliers.h src/main.cpp
src/outliers.hpp
src/outliers.cpp src/outliers.cpp
src/statistics.cpp src/statistics.cpp
src/statistics.h) src/statistics.hpp
src/histogram.cpp
src/histogram.hpp
)
# Fetch Catch2 # Fetch GoogleTest
Include(FetchContent) Include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
Catch2 googletest
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v3.4.0 GIT_TAG v1.15.2
) )
FetchContent_MakeAvailable(Catch2) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing() enable_testing()
add_executable(test_outliers add_executable(test_outliers
src/outliers.h src/outliers.hpp
src/outliers.cpp src/outliers.cpp
src/outliers.test.cpp src/outliers.test.cpp
) )
add_executable(test_statistics add_executable(test_statistics
src/statistics.cpp src/statistics.cpp
src/statistics.h src/statistics.hpp
src/statistics.test.cpp src/statistics.test.cpp
) )
add_executable(test_histogram add_executable(test_histogram
src/histogram.cpp src/histogram.cpp
src/histogram.h src/histogram.hpp
src/histogram.test.cpp src/histogram.test.cpp
) )
target_link_libraries(test_outliers PRIVATE Catch2::Catch2WithMain) target_link_libraries(test_outliers PRIVATE GTest::gtest_main)
target_link_libraries(test_statistics PRIVATE Catch2::Catch2WithMain) target_link_libraries(test_statistics PRIVATE GTest::gtest_main)
target_link_libraries(test_histogram PRIVATE Catch2::Catch2WithMain) target_link_libraries(test_histogram PRIVATE GTest::gtest_main)
target_include_directories(test_outliers PRIVATE src/) target_include_directories(test_outliers PRIVATE src/)
target_include_directories(test_statistics PRIVATE src/) target_include_directories(test_statistics PRIVATE src/)
target_include_directories(test_histogram PRIVATE src/) target_include_directories(test_histogram PRIVATE src/)
add_test(test_outliers ./test_outliers) include(GoogleTest)
add_test(test_statistics ./test_statistics) gtest_discover_tests(test_outliers)
add_test(test_histogram ./test_histogram) gtest_discover_tests(test_statistics)
set_property(TEST test_outliers PROPERTY ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1") gtest_discover_tests(test_histogram)
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 "histogram.h" #include "histogram.hpp"
#include <algorithm> #include <algorithm>
#include <unordered_map> #include <unordered_map>
......
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include <vector>
int getMostFrequentValue(const std::vector<int>& data);
#endif // HISTOGRAM_H
#include "histogram.h" #include "histogram.hpp"
#include <gtest/gtest.h>
#include <catch2/catch_test_macros.hpp> TEST(HistogramTest, GetMostFrequentValue) {
EXPECT_EQ(getMostFrequentValue({1, 2, 2, 3}), 2);
TEST_CASE("getMostFrequentValue") { EXPECT_EQ(getMostFrequentValue({10}), 10);
REQUIRE(getMostFrequentValue({1, 2, 2, 3}) == 2); EXPECT_EQ(getMostFrequentValue(
REQUIRE(getMostFrequentValue({10}) == 10); {1, 2, 2, 2, 3, 3, 1, 3, 2, 8, 32, 1, 3, 1, 2, 4, 3, 2, 3, 3, 3}), 3);
REQUIRE(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 int mfv = getMostFrequentValue({1, 2, 2, 3, 3});
const bool correct = (mfv == 2) || (mfv == 3); const bool correct = (mfv == 2) || (mfv == 3);
REQUIRE(correct); EXPECT_TRUE(correct);
} }
\ No newline at end of file
#include "outliers.h" #include "outliers.hpp"
double findNMin(const std::vector<double>& data, size_t n) { double findNMin(const std::vector<double>& data, size_t n) {
return 0; return 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
#include "outliers.h" #include "outliers.hpp"
#include <gtest/gtest.h>
#include <algorithm>
#include <catch2/catch_test_macros.hpp> void allElementsInVector(const std::vector<double>& test, const std::vector<double>& correct) {
#include <catch2/matchers/catch_matchers_vector.hpp> ASSERT_EQ(test.size(), correct.size());
std::vector<double> sorted_test = test;
void allElementsInVector(std::vector<double> test, std::vector<double> correct) { std::sort(sorted_test.begin(), sorted_test.end());
REQUIRE(test.size() == correct.size()); for (size_t i = 0; i < correct.size(); ++i) {
std::sort(test.begin(), test.end()); EXPECT_EQ(sorted_test[i], correct[i]);
for (int i = 0; i < correct.size(); ++i) {
CHECK(test[i] == correct[i]);
} }
} }
TEST_CASE("findNMin") { TEST(OutliersTest, FindNMin) {
const std::vector<double> data = { std::vector<double> data = {1, 2, 3, 4, 5};
1, 2, 3, 4, 5, EXPECT_EQ(findNMin(data, 1), 1);
}; EXPECT_EQ(findNMin(data, 2), 2);
REQUIRE(findNMin(data, 1) == 1); EXPECT_EQ(findNMin(data, 5), 5);
REQUIRE(findNMin(data, 2) == 2);
REQUIRE(findNMin(data, 5) == 5);
const std::vector<double> data2 = { std::vector<double> data2 = {4, 2, 1, 2, 3};
4, 2, 1, 2, 3, EXPECT_EQ(findNMin(data2, 1), 1);
}; EXPECT_EQ(findNMin(data2, 2), 2);
REQUIRE(findNMin(data2, 1) == 1); EXPECT_EQ(findNMin(data2, 5), 4);
REQUIRE(findNMin(data2, 2) == 2);
REQUIRE(findNMin(data2, 5) == 4);
} }
TEST_CASE("findNMax") { TEST(OutliersTest, FindNMax) {
const std::vector<double> data = { std::vector<double> data = {1, 2, 3, 4, 5};
1, 2, 3, 4, 5, EXPECT_EQ(findNMax(data, 1), 5);
}; EXPECT_EQ(findNMax(data, 2), 4);
REQUIRE(findNMax(data, 1) == 5); EXPECT_EQ(findNMax(data, 5), 1);
REQUIRE(findNMax(data, 2) == 4);
REQUIRE(findNMax(data, 5) == 1);
const std::vector<double> data2 = { std::vector<double> data2 = {4, 2, 1, 2, 3};
4, 2, 1, 2, 3, EXPECT_EQ(findNMax(data2, 1), 4);
}; EXPECT_EQ(findNMax(data2, 2), 3);
REQUIRE(findNMax(data2, 1) == 4); EXPECT_EQ(findNMax(data2, 5), 1);
REQUIRE(findNMax(data2, 2) == 3);
REQUIRE(findNMax(data2, 5) == 1);
} }
TEST_CASE("removeOutliers") { TEST(OutliersTest, RemoveOutliers) {
std::vector<double> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, std::vector<double> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
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> data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 30, std::vector<double> correct = {0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 18};
11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; std::vector<double> correct2 = {0, 0, 3, 4, 5, 6, 7, 8, 9, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19};
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(data, 2), correct);
allElementsInVector(removeOutliers(data2, 2), correct2); allElementsInVector(removeOutliers(data2, 2), correct2);
} }
\ No newline at end of file
#include "statistics.h" #include "statistics.hpp"
#include <algorithm> #include <algorithm>
#include <numeric> #include <numeric>
......
#ifndef STATISTICS_H
#define STATISTICS_H
#include <cstddef>
#include <vector>
size_t countValuesNearMean(const std::vector<double>& data);
#endif // STATISTICS_H
#include "statistics.h" #include "statistics.hpp"
#include <gtest/gtest.h>
#include <catch2/catch_test_macros.hpp> TEST(StatisticsTest, CountValuesNearMeanTest) {
#include <catch2/matchers/catch_matchers_vector.hpp> EXPECT_EQ(countValuesNearMean({1, 2, 3}), 1);
EXPECT_EQ(countValuesNearMean({2, 3, 2, 1}), 2);
TEST_CASE("countValuesNearMean") { EXPECT_EQ(countValuesNearMean({1, 4, 2, 3}), 0);
REQUIRE(countValuesNearMean({1, 2, 3}) == 1); 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);
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);
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment