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)
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)
#include "histogram.h"
#include "histogram.hpp"
#include <algorithm>
#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_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
#include "outliers.h"
#include "outliers.hpp"
double findNMin(const std::vector<double>& data, size_t n) {
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>
#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
#include "statistics.h"
#include "statistics.hpp"
#include <algorithm>
#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>
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment