1
0
Fork 0

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

main
mandlm 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)

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,8 @@
#pragma once
namespace null {
void test();
#include "chapter_interface.h"
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);
}
void constexp::test() {
void ConstExpTest::test() {
std::cout << "Fibonacci(10): ";
for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
std::cout << fibonacci(n) << " ";

View File

@ -5,7 +5,7 @@
#include "ifswitch.h"
void ifswitch::test() {
void IfswitchTest::test() {
std::vector<uint32_t> vec = {1, 2, 3, 4};
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) {}
};
void initlist::test() {
void InitListTest::test() {
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 null::test() {
void NullTest::test() {
if (std::is_same<decltype(NULL), decltype(0)>::value) {
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 <iostream>
#include <memory>
#include <string>
#include <vector>
#include "constexp.h"
#include "ifswitch.h"
@ -12,20 +15,18 @@ int main(int argc, char **argv) {
std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n"
<< std::endl;
std::cout << "Null test:" << std::endl;
null::test();
std::vector<std::tuple<std::string, std::unique_ptr<ChapterTest>>>
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"
<< "Constexpr test:" << std::endl;
constexp::test();
std::cout << "\n"
<< "Ifswitch test:" << std::endl;
ifswitch::test();
std::cout << "\n"
<< "Initializer list test:" << std::endl;
initlist::test();
for (auto &[name, test] : chapterTests) {
std::cout << "\n" << name << std::endl;
test->test();
}
std::cout << "\nExiting" << std::endl;