Skip to content
Snippets Groups Projects
Verified Commit da9c83cc 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
/.idea/
/cmake-build-debug/
/cmake-build-debug-coverage/
cmake_minimum_required(VERSION 3.26)
project(uebung01)
include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS(m sin "" HAVE_LIB_M)
if (HAVE_LIB_M)
set(EXTRA_LIBS ${EXTRA_LIBS} m)
endif (HAVE_LIB_M)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(common OBJECT
src/prime.c
src/ggT.c
src/ggT.h
src/kgV.c
src/kgV.h
)
add_executable(main src/main.c
src/prime.c
src/prime.h
src/ggT.c
src/ggT.h
src/kgV.c
src/kgV.h)
target_link_libraries(main PUBLIC ${EXTRA_LIBS})
target_link_libraries(common PUBLIC ${EXTRA_LIBS})
# 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(test_prime
$<TARGET_OBJECTS:common>
src/prime.test.cpp
)
add_executable(test_ggT_kgV
$<TARGET_OBJECTS:common>
src/ggT.test.cpp
src/kgV.test.cpp
)
add_executable(test_ggT_rec
$<TARGET_OBJECTS:common>
src/ggt_rec.test.cpp
)
target_link_libraries(test_prime PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_prime PUBLIC ${EXTRA_LIBS})
target_link_libraries(test_ggT_kgV PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_ggT_kgV PUBLIC ${EXTRA_LIBS})
target_link_libraries(test_ggT_rec PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_ggT_rec PUBLIC ${EXTRA_LIBS})
target_include_directories(test_prime PRIVATE src/)
target_include_directories(test_ggT_kgV PRIVATE src/)
add_test(test_prime env CTEST_OUTPUT_ON_FAILURE=1 ./test_prime)
add_test(test_ggT_kgV env CTEST_OUTPUT_ON_FAILURE=1 ./test_ggT_kgV)
add_test(test_ggT_rec env CTEST_OUTPUT_ON_FAILURE=1 ./test_ggT_rec)
#include "ggT.h"
#include "stdlib.h"
/**
* Calculates the ggT using a while loop.
* @return the ggT of a and b
*/
int ggT(int a, int b) {
// TODO
return 0;
}
/**
* Calculates the ggT using recursion.
* @return the ggT of a and b
*/
int ggT_rec(int a, int b) {
// TODO
return 0;
}
\ No newline at end of file
#ifndef uebung01_GGT_H
#define uebung01_GGT_H
int ggT(int a, int b);
int ggT_rec(int a, int b);
#endif // uebung01_GGT_H
\ No newline at end of file
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
extern "C" {
#include "ggT.h"
}
TEST_CASE("ggT_Zero") {
REQUIRE(ggT(0, 0) == 0);
REQUIRE(ggT(1, 0) == 1);
REQUIRE(ggT(0, 1) == 1);
}
TEST_CASE("ggT_negativ") {
REQUIRE(ggT(-4, -2) == ggT(4, 2));
REQUIRE(ggT(4, -2) == ggT(4, 2));
REQUIRE(ggT(-4, 2) == ggT(4, 2));
}
TEST_CASE("ggT_same") { REQUIRE(ggT(8, 8) == 8); }
TEST_CASE("ggT") {
REQUIRE(ggT(3528, 3780) == 252);
REQUIRE(ggT(70, 42) == 14);
REQUIRE(ggT(768, 912) == 48);
}
\ No newline at end of file
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
extern "C" {
#include "ggT.h"
}
TEST_CASE("ggT_rec_Zero") {
REQUIRE(ggT_rec(0, 0) == 0);
REQUIRE(ggT_rec(1, 0) == 1);
REQUIRE(ggT_rec(0, 1) == 1);
}
TEST_CASE("ggT_rec_negativ") {
REQUIRE(ggT_rec(-4, -2) == ggT_rec(4, 2));
REQUIRE(ggT_rec(4, -2) == ggT_rec(4, 2));
REQUIRE(ggT_rec(-4, 2) == ggT_rec(4, 2));
}
TEST_CASE("ggT_rec_same") { REQUIRE(ggT_rec(8, 8) == 8); }
TEST_CASE("ggT_rec") {
REQUIRE(ggT_rec(3528, 3780) == 252);
REQUIRE(ggT_rec(70, 42) == 14);
REQUIRE(ggT_rec(768, 912) == 48);
}
#include "kgV.h"
#include <stdlib.h>
#include "ggT.h"
int kgV(int a, int b) {
// TODO
return 0;
}
#ifndef UEBUNG01_KGV_H
#define UEBUNG01_KGV_H
int kgV(int a, int b);
#endif // UEBUNG01_KGV_H
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
extern "C" {
#include "kgV.h"
}
TEST_CASE("kgV") {
REQUIRE(kgV(12, 18) == 36);
REQUIRE(kgV(18, 24) == 72);
}
\ No newline at end of file
#include <stdio.h>
#include "prime.h"
int main() { return 0; }
#include "prime.h"
#include <math.h>
/** Checks whether a given number is prime
*
* @param primeCandidate checked if it is prime
*/
bool isPrime(int primeCandidate) {
// TODO
return false;
}
#ifndef PRIME_CATCH2_PRIME_H
#define PRIME_CATCH2_PRIME_H
#include <stdbool.h>
bool isPrime(int primeCandidate);
#endif // PRIME_CATCH2_PRIME_H
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
extern "C" {
#include "prime.h"
}
TEST_CASE("primes_negativ") { REQUIRE_FALSE(isPrime(-2)); }
TEST_CASE("primes_zero") { REQUIRE_FALSE(isPrime(0)); }
TEST_CASE("primes_one") { REQUIRE_FALSE(isPrime(1)); }
TEST_CASE("primes") {
REQUIRE(isPrime(2));
REQUIRE(isPrime(3));
REQUIRE_FALSE(isPrime(4));
REQUIRE(isPrime(5));
REQUIRE_FALSE(isPrime(6));
REQUIRE(isPrime(7));
REQUIRE_FALSE(isPrime(8));
REQUIRE_FALSE(isPrime(9));
}
TEST_CASE("primes_high_numbers") {
REQUIRE(isPrime(241));
REQUIRE(isPrime(9857));
REQUIRE(isPrime(99971));
REQUIRE_FALSE(isPrime(9841));
}
TEST_CASE("Benchmark Prime") {
BENCHMARK("isPrime(99971)") { return isPrime(99971); };
}
\ 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