From 543b4b832aa2bc88d0561366eb5b5c4df8ea440d Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 21 Oct 2023 17:11:05 +0200 Subject: [PATCH] feat: add structured binding test --- chapters/chapter_02/CMakeLists.txt | 2 +- .../chapter_02/include/structured_binding.h | 8 ++++++ .../chapter_02/src/structured_binding.cpp | 12 +++++++++ src/main.cpp | 25 +++++++++++-------- 4 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 chapters/chapter_02/include/structured_binding.h create mode 100644 chapters/chapter_02/src/structured_binding.cpp diff --git a/chapters/chapter_02/CMakeLists.txt b/chapters/chapter_02/CMakeLists.txt index 7be36fd..ae0ff82 100644 --- a/chapters/chapter_02/CMakeLists.txt +++ b/chapters/chapter_02/CMakeLists.txt @@ -1,5 +1,5 @@ add_library(chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp - src/ifswitch.cpp) + src/ifswitch.cpp src/structured_binding.cpp) target_compile_features(chapter_02 PUBLIC cxx_std_20) diff --git a/chapters/chapter_02/include/structured_binding.h b/chapters/chapter_02/include/structured_binding.h new file mode 100644 index 0000000..a63392f --- /dev/null +++ b/chapters/chapter_02/include/structured_binding.h @@ -0,0 +1,8 @@ +#pragma once + +#include "chapter_interface.h" + +class StructuredBindingTest : public ChapterTest { +public: + virtual void run() override; +}; diff --git a/chapters/chapter_02/src/structured_binding.cpp b/chapters/chapter_02/src/structured_binding.cpp new file mode 100644 index 0000000..6727c61 --- /dev/null +++ b/chapters/chapter_02/src/structured_binding.cpp @@ -0,0 +1,12 @@ +#include "structured_binding.h" +#include +#include +#include + +std::tuple func() { return {"32", 32}; } + +void StructuredBindingTest::run() { + const auto &[str, val] = func(); + + std::cout << "Bound values: " << str << " and " << val << std::endl; +} diff --git a/src/main.cpp b/src/main.cpp index c8da422..ccd3a5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,13 @@ #include #include #include -#include #include #include "constexp.h" #include "ifswitch.h" #include "initlist.h" #include "null.h" +#include "structured_binding.h" #include "version.h" int main(int argc, char **argv) { @@ -15,16 +15,21 @@ int main(int argc, char **argv) { std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n" << std::endl; - std::vector>> - chapterTests; - chapterTests.emplace_back("Null test", std::make_unique()); - chapterTests.emplace_back("If/Switch test", std::make_unique()); - chapterTests.emplace_back("Constexpr test", std::make_unique()); - chapterTests.emplace_back("Initializer list test", - std::make_unique()); + std::vector>> + chapter_2_tests; - for (auto &[name, test] : chapterTests) { - std::cout << "\n" << name << std::endl; + chapter_2_tests.emplace_back("Nullptr test", std::make_unique()); + chapter_2_tests.emplace_back("If/Switch test", + std::make_unique()); + chapter_2_tests.emplace_back("Constexpr test", + std::make_unique()); + chapter_2_tests.emplace_back("Initializer list test", + std::make_unique()); + chapter_2_tests.emplace_back("Structured binding test", + std::make_unique()); + + for (auto &[test_description, test] : chapter_2_tests) { + std::cout << "\n" << test_description << ":" << std::endl; test->run(); }