feat: provide and use a common calling interface to all chapter tests

This commit is contained in:
Michael Mandl 2023-10-21 12:31:23 +02:00
parent dfaee89d56
commit 89d1469088
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7
13 changed files with 52 additions and 26 deletions

View file

@ -1 +1,2 @@
add_subdirectory(interface)
add_subdirectory(chapter_02) add_subdirectory(chapter_02)

View file

@ -1,4 +1,8 @@
add_library(chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp add_library(chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp
src/ifswitch.cpp) src/ifswitch.cpp)
target_compile_features(chapter_02 PUBLIC cxx_std_20)
target_include_directories(chapter_02 PUBLIC include) target_include_directories(chapter_02 PUBLIC include)
target_link_libraries(chapter_02 PUBLIC chapter_interface)

View file

@ -1,5 +1,8 @@
#pragma once #pragma once
namespace constexp { #include "chapter_interface.h"
void test();
class ConstExpTest : public ChapterTest {
public:
virtual void test() override;
}; };

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
namespace ifswitch { #include "chapter_interface.h"
void test();
class IfswitchTest : public ChapterTest {
public:
virtual void test() override;
}; };

View file

@ -1,5 +1,8 @@
#pragma once #pragma once
namespace initlist { #include "chapter_interface.h"
void test();
class InitListTest : public ChapterTest {
public:
virtual void test() override;
}; };

View file

@ -1,5 +1,8 @@
#pragma once #pragma once
namespace null { #include "chapter_interface.h"
void test();
class NullTest : public ChapterTest {
public:
virtual void test() override;
}; };

View file

@ -9,7 +9,7 @@ constexpr unsigned int fibonacci(const unsigned int n) {
return fibonacci(n - 2) + fibonacci(n - 1); return fibonacci(n - 2) + fibonacci(n - 1);
} }
void constexp::test() { void ConstExpTest::test() {
std::cout << "Fibonacci(10): "; std::cout << "Fibonacci(10): ";
for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
std::cout << fibonacci(n) << " "; std::cout << fibonacci(n) << " ";

View file

@ -5,7 +5,7 @@
#include "ifswitch.h" #include "ifswitch.h"
void ifswitch::test() { void IfswitchTest::test() {
std::vector<uint32_t> vec = {1, 2, 3, 4}; std::vector<uint32_t> vec = {1, 2, 3, 4};
if (auto it = std::find(vec.begin(), vec.end(), 23); it != vec.end()) { if (auto it = std::find(vec.begin(), vec.end(), 23); it != vec.end()) {

View file

@ -10,7 +10,7 @@ public:
InitList(std::initializer_list<unsigned int> list) : _list(list) {} InitList(std::initializer_list<unsigned int> list) : _list(list) {}
}; };
void initlist::test() { void InitListTest::test() {
InitList initList{1, 2, 3, 4, 5}; InitList initList{1, 2, 3, 4, 5};

View file

@ -7,7 +7,7 @@ void call_test(char *) { std::cout << "call_test(char *) called" << std::endl; }
void call_test(int) { std::cout << "call_test(int) called" << std::endl; } void call_test(int) { std::cout << "call_test(int) called" << std::endl; }
void null::test() { void NullTest::test() {
if (std::is_same<decltype(NULL), decltype(0)>::value) { if (std::is_same<decltype(NULL), decltype(0)>::value) {
std::cout << "NULL == 0" << std::endl; std::cout << "NULL == 0" << std::endl;
} }

View file

@ -0,0 +1,3 @@
add_library(chapter_interface INTERFACE)
target_compile_features(chapter_interface INTERFACE cxx_std_20)
target_include_directories(chapter_interface INTERFACE include)

View file

@ -0,0 +1,7 @@
#pragma once
class ChapterTest {
public:
virtual ~ChapterTest() = default;
virtual void test() = 0;
};

View file

@ -1,5 +1,8 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "constexp.h" #include "constexp.h"
#include "ifswitch.h" #include "ifswitch.h"
@ -12,20 +15,18 @@ int main(int argc, char **argv) {
std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n" std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n"
<< std::endl; << std::endl;
std::cout << "Null test:" << std::endl; std::vector<std::tuple<std::string, std::unique_ptr<ChapterTest>>>
null::test(); chapterTests;
chapterTests.emplace_back("Null test", std::make_unique<NullTest>());
chapterTests.emplace_back("If/Switch test", std::make_unique<IfswitchTest>());
chapterTests.emplace_back("Constexpr test", std::make_unique<ConstExpTest>());
chapterTests.emplace_back("Initializer list test",
std::make_unique<InitListTest>());
std::cout << "\n" for (auto &[name, test] : chapterTests) {
<< "Constexpr test:" << std::endl; std::cout << "\n" << name << std::endl;
constexp::test(); test->test();
}
std::cout << "\n"
<< "Ifswitch test:" << std::endl;
ifswitch::test();
std::cout << "\n"
<< "Initializer list test:" << std::endl;
initlist::test();
std::cout << "\nExiting" << std::endl; std::cout << "\nExiting" << std::endl;