feat: provide and use a common calling interface to all chapter tests
This commit is contained in:
parent
dfaee89d56
commit
89d1469088
13 changed files with 52 additions and 26 deletions
|
@ -1 +1,2 @@
|
||||||
|
add_subdirectory(interface)
|
||||||
add_subdirectory(chapter_02)
|
add_subdirectory(chapter_02)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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) << " ";
|
||||||
|
|
|
@ -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()) {
|
||||||
|
|
|
@ -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};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
3
chapters/interface/CMakeLists.txt
Normal file
3
chapters/interface/CMakeLists.txt
Normal 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)
|
7
chapters/interface/include/chapter_interface.h
Normal file
7
chapters/interface/include/chapter_interface.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class ChapterTest {
|
||||||
|
public:
|
||||||
|
virtual ~ChapterTest() = default;
|
||||||
|
virtual void test() = 0;
|
||||||
|
};
|
27
src/main.cpp
27
src/main.cpp
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue