Skip to content
Snippets Groups Projects
Verified Commit b66f21d5 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(Uebung03)
set(CMAKE_CXX_STANDARD 14)
add_library(common OBJECT
src/strequal.c
src/strequal.h
src/array_operations.c
src/array_operations.h
src/parse_numbers.c
src/parse_numbers.h
src/sieve_of_eratosthenes.c
src/sieve_of_eratosthenes.h
)
add_executable(Uebung03 src/main.c
src/strequal.c
src/strequal.h
src/array_operations.c
src/array_operations.h
src/parse_numbers.c
src/parse_numbers.h
src/sieve_of_eratosthenes.c
src/sieve_of_eratosthenes.h)
# Fetch Catch2
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(Catch2)
# Build Tests
enable_testing()
add_executable(test_array_ops
$<TARGET_OBJECTS:common>
src/array_operations.test.cpp
)
add_executable(test_strequal
$<TARGET_OBJECTS:common>
src/strequal.test.cpp
)
add_executable(test_parse_numbers
$<TARGET_OBJECTS:common>
src/parse_numbers.test.cpp
)
add_executable(test_sieve
$<TARGET_OBJECTS:common>
src/sieve_of_eratosthenes.test.cpp
)
target_link_libraries(test_array_ops PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_strequal PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_parse_numbers PRIVATE Catch2::Catch2WithMain)
target_link_libraries(test_sieve PRIVATE Catch2::Catch2WithMain)
target_include_directories(test_array_ops PRIVATE src/)
target_include_directories(test_strequal PRIVATE src/)
target_include_directories(test_parse_numbers PRIVATE src/)
target_include_directories(test_sieve PRIVATE src/)
add_test(test_array_ops env CTEST_OUTPUT_ON_FAILURE=1 ./test_array_ops)
add_test(test_strequal env CTEST_OUTPUT_ON_FAILURE=1 ./test_strequal)
add_test(test_parse_numbers env CTEST_OUTPUT_ON_FAILURE=1 ./test_parse_numbers)
add_test(test_sieve env CTEST_OUTPUT_ON_FAILURE=1 ./test_sieve)
#include "array_operations.h"
#include <stdlib.h>
#include "limits.h"
void findMinMax(int* minidx, int* maxidx, const int* array, int size) {
//TODO
}
void limit(int* array, int size, int limit) {
//TODO
}
#ifndef UEBUNG03_ARRAY_OPERATIONS_H
#define UEBUNG03_ARRAY_OPERATIONS_H
void findMinMax(int* minidx, int* maxidx, const int* array, int size);
void limit(int* array, int size, int limit);
#endif // UEBUNG03_ARRAY_OPERATIONS_H
extern "C" {
#include "array_operations.h"
}
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
TEST_CASE("find_min_max") {
int minidx = -1;
int maxidx = -1;
const int testArray1[] = {9, 0, 2, 3};
findMinMax(&minidx, &maxidx, testArray1, 4);
REQUIRE(minidx == 1);
REQUIRE(maxidx == 0);
minidx = -1;
maxidx = -1;
const int testArray2[] = {9, 12, 2, 3};
findMinMax(&minidx, &maxidx, testArray2, 4);
REQUIRE(minidx == 2);
REQUIRE(maxidx == 1);
minidx = -1;
maxidx = -1;
const int testArray3[] = {0, 12, 2, 3};
findMinMax(&minidx, &maxidx, testArray3, 4);
REQUIRE(minidx == 0);
REQUIRE(maxidx == 1);
minidx = -1;
maxidx = -1;
const int testArray4[] = {0, 12, 2, -2};
findMinMax(&minidx, &maxidx, testArray4, 4);
REQUIRE(minidx == 3);
REQUIRE(maxidx == 1);
minidx = -1;
maxidx = -1;
const int testArray5[] = {9, -2, 2, -2};
findMinMax(&minidx, &maxidx, testArray5, 4);
REQUIRE(minidx == 1);
REQUIRE(maxidx == 0);
minidx = -1;
maxidx = -1;
const int testArray6[] = {0, 12, 2, 22};
findMinMax(&minidx, &maxidx, testArray6, 4);
REQUIRE(minidx == 0);
REQUIRE(maxidx == 3);
}
TEST_CASE("limit") {
int testArray[] = {9, 0, -12, 18, -18};
const int result[] = {9, 0, -10, 10, -10};
limit(testArray, 5, 10);
for (int i = 0; i < 5; ++i) {
CHECK(testArray[i] == result[i]);
}
}
\ No newline at end of file
#include <stdio.h>
#include "strequal.h"
int main() {
printf("%i", strequali("Anna!", "aNNa!"));
return 0;
}
#include "parse_numbers.h"
bool parse_uint(uint32_t* value, const char* s) {
//TODO
return false;
}
#ifndef UEBUNG03_STREQUAL_H
#define UEBUNG03_STREQUAL_H
#include <stdbool.h>
#include <stdint.h>
bool parse_uint(uint32_t* value, const char* s);
#endif // UEBUNG03_STREQUAL_H
extern "C" {
#include "parse_numbers.h"
}
#include <catch2/catch_test_macros.hpp>
TEST_CASE("valid_numbers") {
uint32_t parsed = 42;
REQUIRE(parse_uint(&parsed, "0"));
CHECK(parsed == 0);
parsed = 42;
REQUIRE(parse_uint(&parsed, "0000"));
CHECK(parsed == 0);
REQUIRE(parse_uint(&parsed, "1"));
CHECK(parsed == 1);
REQUIRE(parse_uint(&parsed, "10"));
CHECK(parsed == 10);
REQUIRE(parse_uint(&parsed, "19"));
CHECK(parsed == 19);
REQUIRE(parse_uint(&parsed, "0342520"));
CHECK(parsed == 342520);
parsed = 0;
REQUIRE(parse_uint(&parsed, "342520"));
CHECK(parsed == 342520);
REQUIRE(parse_uint(&parsed, "4294967295"));
CHECK(parsed == 4294967295);
parsed = 0;
REQUIRE(parse_uint(&parsed, "0004294967295"));
CHECK(parsed == 4294967295);
}
TEST_CASE("invalid_chars_in_number") {
uint32_t parsed = 42;
REQUIRE_FALSE(parse_uint(&parsed, ""));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, " 0"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "-12"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, " 12"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "12 "));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "23 212"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "235$"));
CHECK(parsed == 42);
}
TEST_CASE("too_large_numbers") {
uint32_t parsed = 42;
REQUIRE_FALSE(parse_uint(&parsed, "10000000000000"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "10000000000"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "4294967296"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "04294967296"));
CHECK(parsed == 42);
REQUIRE_FALSE(parse_uint(&parsed, "18446744073709551617"));
CHECK(parsed == 42);
}
#include "sieve_of_eratosthenes.h"
void sieve_of_eratosthenes(bool *data, int size) {
//TODO
}
#ifndef UEBUNG03_SIEVE_OF_ERATOSTHENES_H
#define UEBUNG03_SIEVE_OF_ERATOSTHENES_H
#include "stdbool.h"
void sieve_of_eratosthenes(bool* data, int size);
#endif // UEBUNG03_SIEVE_OF_ERATOSTHENES_H
extern "C" {
#include "sieve_of_eratosthenes.h"
}
#include <catch2/catch_test_macros.hpp>
TEST_CASE("Test_Sieve") {
bool primes[1000];
sieve_of_eratosthenes(primes, 1000);
REQUIRE(primes[2]);
REQUIRE(primes[3]);
REQUIRE(primes[5]);
REQUIRE(primes[11]);
REQUIRE(primes[19]);
REQUIRE(primes[43]);
REQUIRE(primes[53]);
REQUIRE(primes[67]);
REQUIRE(primes[79]);
REQUIRE(primes[97]);
REQUIRE(primes[997]);
REQUIRE(primes[947]);
REQUIRE_FALSE(primes[1]);
REQUIRE_FALSE(primes[4]);
REQUIRE_FALSE(primes[15]);
REQUIRE_FALSE(primes[91]);
REQUIRE_FALSE(primes[42]);
REQUIRE_FALSE(primes[33]);
REQUIRE_FALSE(primes[998]);
REQUIRE_FALSE(primes[999]);
}
\ No newline at end of file
#include "strequal.h"
#include "ctype.h"
bool strequal(const char *a, const char *b) {
//TODO
return false;
}
bool strequali(const char *a, const char *b) {
//TODO
return false;
}
#ifndef UEBUNG03_STREQUAL_H
#define UEBUNG03_STREQUAL_H
#include "stdbool.h"
bool strequal(const char* a, const char* b);
bool strequali(const char* a, const char* b);
#endif // UEBUNG03_STREQUAL_H
extern "C" {
#include "strequal.h"
}
#include <catch2/catch_test_macros.hpp>
TEST_CASE("Strings_equal") {
REQUIRE(strequal("Darmstadt", "Darmstadt"));
REQUIRE(strequal("C ist TOLL", "C ist TOLL"));
REQUIRE(strequal(" C ist TOLL!", " C ist TOLL!"));
REQUIRE(strequal("!\"§$%&", "!\"§$%&"));
REQUIRE(strequal("", ""));
}
TEST_CASE("Strings_not_equal") {
REQUIRE_FALSE(strequal("Darmstadt", "DaRmStAdT"));
REQUIRE_FALSE(strequal("C ist TOLL", "c iST toll"));
REQUIRE_FALSE(strequal(" C ist TOLL!", " c iST toll!"));
REQUIRE_FALSE(strequal("Darmstadt", "Berlin"));
REQUIRE_FALSE(strequal("Darmstadt", "Darmstadt "));
REQUIRE_FALSE(strequal(" ", ""));
}
TEST_CASE("Strings_equal_i") {
REQUIRE(strequal("Darmstadt", "Darmstadt"));
REQUIRE(strequal("C ist TOLL", "C ist TOLL"));
REQUIRE(strequal(" C ist TOLL!", " C ist TOLL!"));
REQUIRE(strequal("!\"§$%&", "!\"§$%&"));
REQUIRE(strequali("Darmstadt", "DaRmStAdT"));
REQUIRE(strequali("C ist TOLL", "c iST toll"));
REQUIRE(strequali(" C ist TOLL!", " c iST toll!"));
REQUIRE(strequali("", ""));
}
TEST_CASE("Strings_not_equal_i") {
REQUIRE_FALSE(strequali("Darmstadt", "Berlin"));
REQUIRE_FALSE(strequali("Darmstadt", "Darmstadt "));
REQUIRE_FALSE(strequali(" ", ""));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment