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

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# CLion
/.idea
/*-build-*
# Visual Studio
/.vs
/out
# vscode
/.vscode
/build
cmake_minimum_required(VERSION 3.26)
project(Uebung4)
set(CMAKE_CXX_STANDARD 17)
include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS(m sin "" HAVE_LIB_M)
if (HAVE_LIB_M)
set(EXTRA_LIBS ${EXTRA_LIBS} m)
endif (HAVE_LIB_M)
# Fetch Catch2
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.3.2
)
FetchContent_MakeAvailable(Catch2)
# Build Tests
enable_testing()
add_executable(main src/main.cpp
src/string_tools.cpp
src/string_tools.h
src/integrate.cpp
src/integrate.h
src/fillnans.cpp
src/fillnans.h)
add_library(common OBJECT
src/string_tools.cpp
src/string_tools.h
src/integrate.cpp
src/integrate.h
src/fillnans.cpp
src/fillnans.h)
target_link_libraries(main PUBLIC ${EXTRA_LIBS})
target_link_libraries(common PUBLIC ${EXTRA_LIBS})
add_executable(test_string
$<TARGET_OBJECTS:common>
src/string_tools.test.cpp
)
add_executable(test_integrate
$<TARGET_OBJECTS:common>
src/integrate.test.cpp
)
add_executable(test_fillnans
$<TARGET_OBJECTS:common>
src/fillnans.test.cpp
)
target_link_libraries(test_string PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_integrate PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_fillnans PRIVATE Catch2::Catch2WithMain)
target_include_directories(test_string PRIVATE src/)
target_include_directories(test_integrate PRIVATE src/)
target_include_directories(test_fillnans PRIVATE src/)
add_test(test_string env CTEST_OUTPUT_ON_FAILURE=1 ./test_string)
add_test(test_integrate env CTEST_OUTPUT_ON_FAILURE=1 ./test_integrate)
add_test(test_fillnans env CTEST_OUTPUT_ON_FAILURE=1 ./test_fillnans)
#include "fillnans.h"
#include <cmath>
#include <iostream>
static size_t fill_nans_left(double* data, size_t len) {
//TODO
return 0;
}
static size_t fill_nans_interp(double* data, size_t len) {
//TODO
return 0;
}
size_t fill_nans(double* data, size_t len, InterpMethod method) {
//TODO
return 0;
}
\ No newline at end of file
#ifndef UEBUNG4_FILLNANS_H
#define UEBUNG4_FILLNANS_H
#include<cstddef>
enum class InterpMethod {
Left,
Linear,
};
size_t fill_nans(double* data, size_t len, InterpMethod method);
#endif // UEBUNG4_FILLNANS_H
#include <cmath>
#include "fillnans.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
static void check_array(double* data, std::vector<double> ref, double tol = 1e-12) {
for (size_t i = 0; i < ref.size(); ++i) {
CHECK_THAT(data[i], Catch::Matchers::WithinRel(ref[i], tol));
}
}
TEST_CASE("fillnans_left") {
double data1[]{1, 2, 3, 4};
const size_t count1 = fill_nans(data1, sizeof(data1) / sizeof(double), InterpMethod::Left);
REQUIRE(count1 == 0);
check_array(data1, {1.0, 2.0, 3.0, 4.0});
double data2[]{1, NAN, 3, 4, 5};
const size_t count2 = fill_nans(data2, sizeof(data2) / sizeof(double), InterpMethod::Left);
REQUIRE(count2 == 1);
check_array(data2, {1.0, 1.0, 3.0, 4.0, 5.0});
double data3[]{1, NAN, 3, NAN, 5};
const size_t count3 = fill_nans(data3, sizeof(data3) / sizeof(double), InterpMethod::Left);
REQUIRE(count3 == 2);
check_array(data3, {1.0, 1.0, 3.0, 3.0, 5.0});
double data4[]{1, NAN, NAN, 4, 5};
const size_t count4 = fill_nans(data4, sizeof(data4) / sizeof(double), InterpMethod::Left);
REQUIRE(count4 == 2);
check_array(data4, {1.0, 1.0, 1.0, 4.0, 5.0});
double data5[]{1, NAN, NAN, NAN, 5};
const size_t count5 = fill_nans(data5, sizeof(data5) / sizeof(double), InterpMethod::Left);
REQUIRE(count5 == 3);
check_array(data5, {1.0, 1.0, 1.0, 1.0, 5.0});
}
TEST_CASE("fillnans_linear") {
double data1[]{1, 2, 4, 6, 10};
const size_t count1 = fill_nans(data1, sizeof(data1) / sizeof(double), InterpMethod::Linear);
REQUIRE(count1 == 0);
check_array(data1, {1.0, 2.0, 4.0, 6.0});
double data2[]{1, NAN, 4, 6, 10};
const size_t count2 = fill_nans(data2, sizeof(data2) / sizeof(double), InterpMethod::Linear);
REQUIRE(count2 == 1);
check_array(data2, {1.0, 2.5, 4.0, 6.0, 10.0});
double data3[]{1, NAN, 4, NAN, 10};
const size_t count3 = fill_nans(data3, sizeof(data3) / sizeof(double), InterpMethod::Linear);
REQUIRE(count3 == 2);
check_array(data3, {1.0, 2.5, 4.0, 7.0, 10.0});
double data4[]{1, NAN, NAN, 6, 10};
const size_t count4 = fill_nans(data4, sizeof(data4) / sizeof(double), InterpMethod::Linear);
REQUIRE(count4 == 2);
check_array(data4, {1.0, 8.0 / 3.0, 13.0 / 3.0, 6.0, 10.0});
double data5[]{1, NAN, NAN, NAN, 11};
const size_t count5 = fill_nans(data5, sizeof(data5) / sizeof(double), InterpMethod::Linear);
REQUIRE(count5 == 3);
check_array(data5, {1.0, 3.5, 6.0, 8.5, 11.0});
}
#include "integrate.h"
#include <cmath>
double f1(double x) {
//TODO
return 0.0;
}
double f2(double x) {
//TODO
return 0.0;
}
double integrate(double (*fct)(double), double x_start, double x_end, size_t n_steps) {
//TODO
return 0.0;
}
#ifndef UEBUNG4_INTEGRATE_H
#define UEBUNG4_INTEGRATE_H
#include <cstddef>
double integrate(double (*fct)(double), double x_start, double x_end, size_t n_steps);
double f1(double x);
double f2(double x);
#endif // UEBUNG4_INTEGRATE_H
#include "integrate.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
TEST_CASE("f1") { CHECK_THAT(f1(0.4), Catch::Matchers::WithinRel(0.56, 0.01)); }
TEST_CASE("f2") { CHECK_THAT(f2(2), Catch::Matchers::WithinRel(0.693, 0.01)); }
static double one(double) { return 1.0; }
TEST_CASE("integrate_constant") {
CHECK_THAT(integrate(one, 0, 10, 1), Catch::Matchers::WithinRel(10.0, 1e-12));
CHECK_THAT(integrate(one, 1, 10, 2), Catch::Matchers::WithinRel(9.0, 1e-12));
CHECK_THAT(integrate(one, 2, 10, 10000), Catch::Matchers::WithinRel(8.0, 1e-12));
}
TEST_CASE("integrate") {
CHECK_THAT(integrate(f1, 0, 10, 10000), Catch::Matchers::WithinRel(383.33, 0.01));
CHECK_THAT(integrate(f2, 1, 10, 10000), Catch::Matchers::WithinRel(14.0373, 0.01));
}
#include <iostream>
#include "integrate.h"
int main() {
std::cout << integrate(f2, 1, 10, 100000);
return 0;
}
#include "string_tools.h"
#include <cctype>
bool isWordPalindrom(const std::string& word) {
//TODO
return false;
}
void concat_s(char* target, size_t target_size, const char* string1, const char* string2) {
//TODO
return;
}
#ifndef UEBUNG4_STRING_TOOLS_H
#define UEBUNG4_STRING_TOOLS_H
#include <string>
bool isWordPalindrom(const std::string& word);
void concat_s(char* target, size_t target_size, const char* string1, const char* string2);
#endif // UEBUNG4_STRING_TOOLS_H
#include "string_tools.h"
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
TEST_CASE("palindrome") {
REQUIRE(isWordPalindrom(""));
REQUIRE(isWordPalindrom("a"));
REQUIRE(isWordPalindrom("anna"));
REQUIRE(isWordPalindrom("Anna"));
REQUIRE_FALSE(isWordPalindrom("Tomate"));
}
constexpr char UNWRITTEN = (char)0xFF;
static void init_buffer(char* buf, size_t bufsize) {
for (size_t i = 0; i < bufsize; ++i) {
*buf++ = UNWRITTEN;
}
}
TEST_CASE("concat") {
// use one byte before and two bytes after buffer that is given to the function to assert that
// the function doesn't touch the surounding bytes.
constexpr size_t TRUEBUFLEN = 13;
char true_buf[TRUEBUFLEN];
constexpr size_t BUFLEN = 10;
char* buf = true_buf + 1;
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "Test", "that");
REQUIRE(strcmp(buf, "Testthat") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, " 0Test", " 2 ");
REQUIRE(strcmp(buf, " 0Test 2 ") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "1234567890123", "ABC");
REQUIRE(strcmp(buf, "123456789") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "ABC", "1234567890123");
REQUIRE(strcmp(buf, "ABC123456") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "123456789", "ABC");
REQUIRE(strcmp(buf, "123456789") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "xy", "");
REQUIRE(strcmp(buf, "xy") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "", "ab");
REQUIRE(strcmp(buf, "ab") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "", "");
REQUIRE(strcmp(buf, "") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, 1, "a", "b");
REQUIRE(strcmp(buf, "") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[1] == UNWRITTEN);
REQUIRE(buf[2] == UNWRITTEN);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment