From 4153e50f32d13a37da65460c287619bf248f16c4 Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Wed, 24 Nov 2021 00:46:00 +0100 Subject: [PATCH] rework Foo --- Foo/CMakeLists.txt | 20 ++-- Foo/src/Foo.cpp | 18 ++-- Foo/test/CMakeLists.txt | 20 ---- Foo/tests/CMakeLists.txt | 8 ++ Foo/tests/foo_test.cpp | 155 +++++++++++++++++++++++++++++ Foo/{test => tests}/src/Foo_UT.cpp | 0 Foo/{test => tests}/src/main.cpp | 0 7 files changed, 179 insertions(+), 42 deletions(-) delete mode 100644 Foo/test/CMakeLists.txt create mode 100644 Foo/tests/CMakeLists.txt create mode 100644 Foo/tests/foo_test.cpp rename Foo/{test => tests}/src/Foo_UT.cpp (100%) rename Foo/{test => tests}/src/main.cpp (100%) diff --git a/Foo/CMakeLists.txt b/Foo/CMakeLists.txt index 5fc9104..3eb9934 100644 --- a/Foo/CMakeLists.txt +++ b/Foo/CMakeLists.txt @@ -1,26 +1,20 @@ -add_library(Foo "") +add_library(Foo) target_sources(Foo - PUBLIC - $ PRIVATE - src/Foo.cpp - ) + include/foo/Foo.hpp + src/Foo.cpp) target_include_directories(Foo PUBLIC $ - $ - ) -target_compile_features(Foo PUBLIC cxx_std_11) + $) +target_compile_features(Foo PUBLIC cxx_std_17) set_target_properties(Foo PROPERTIES VERSION ${PROJECT_VERSION} - PUBLIC_HEADER $ - ) + PUBLIC_HEADER include/foo/Foo.hpp) #target_link_libraries(Foo PUBLIC ...) add_library(${PROJECT_NAMESPACE}::Foo ALIAS Foo) -if(BUILD_TESTING) - add_subdirectory(test) -endif() +add_subdirectory(tests) # Install install(TARGETS Foo diff --git a/Foo/src/Foo.cpp b/Foo/src/Foo.cpp index fc9e440..24f77e2 100644 --- a/Foo/src/Foo.cpp +++ b/Foo/src/Foo.cpp @@ -156,20 +156,15 @@ void freeFunction(int64_t level) { } void Foo::staticFunction(int level) { - std::cout << "[" << level << "] Enter Foo::staticFunction(int)" << std::endl; + std::cout << "[" << level << "] Enter " << __func__ << "(int)" << std::endl; freeFunction(level + 1); - std::cout << "[" << level << "] Exit Foo::staticFunction(int)" << std::endl; + std::cout << "[" << level << "] Exit " << __func__ << "(int)" << std::endl; } void Foo::staticFunction(int64_t level) { - std::cout << "[" << level << "] Enter Foo::staticFunction(int64_t)" << std::endl; + std::cout << "[" << level << "] Enter " << __func__ << "(int64_t)" << std::endl; freeFunction(level + 1); - std::cout << "[" << level << "] Exit Foo::staticFunction(int64_t)" << std::endl; -} - -std::string Foo::operator()() const { - return std::string{"\"Foo\":{\"int\":"} + std::to_string(_intValue) + - ",\"int64\":" + std::to_string(_int64Value) + "}"; + std::cout << "[" << level << "] Exit " << __func__ << "(int64_t)" << std::endl; } int Foo::getInt() const { @@ -188,4 +183,9 @@ void Foo::setInt64(int64_t input) { _int64Value = input; } +std::string Foo::operator()() const { + return std::string{"\"Foo\":{\"int\":"} + std::to_string(_intValue) + + ",\"int64\":" + std::to_string(_int64Value) + "}"; +} + } // namespace foo diff --git a/Foo/test/CMakeLists.txt b/Foo/test/CMakeLists.txt deleted file mode 100644 index 8eb3b18..0000000 --- a/Foo/test/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -set(NAME Foo_UT) - -file(GLOB _SRCS "src/*.[hc]pp") - -add_executable(${NAME} - src/main.cpp - src/Foo_UT.cpp - ) -# note: macOS is APPLE and also UNIX ! -if(APPLE) - set_target_properties(${NAME} PROPERTIES - INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR}") -elseif(UNIX AND NOT APPLE) - set_target_properties(${NAME} PROPERTIES - INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}") -endif() -target_link_libraries(${NAME} - PRIVATE - Catch2 ${PROJECT_NAMESPACE}::Foo) -add_test(NAME cxx_${NAME} COMMAND ${NAME}) diff --git a/Foo/tests/CMakeLists.txt b/Foo/tests/CMakeLists.txt new file mode 100644 index 0000000..79d1916 --- /dev/null +++ b/Foo/tests/CMakeLists.txt @@ -0,0 +1,8 @@ +if(NOT BUILD_TESTING) + return() +endif() + +file(GLOB CPP_SRCS "*.cpp") +foreach(TEST IN LISTS CPP_SRCS) + add_cpp_test(${TEST}) +endforeach() diff --git a/Foo/tests/foo_test.cpp b/Foo/tests/foo_test.cpp new file mode 100644 index 0000000..68c8126 --- /dev/null +++ b/Foo/tests/foo_test.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include + +#include + +namespace foo { + +TEST_CASE("Foo free function", "[Foo]") { + SECTION("Int Function") { REQUIRE_NOTHROW(freeFunction(42)); } + SECTION("Int64_t Function") { REQUIRE_NOTHROW(freeFunction(int64_t{42})); } +} + +TEST_CASE("String Vector usage", "[Foo]") { + SECTION("Vector of String Output") { + std::vector result; + REQUIRE_NOTHROW(result = stringVectorOutput(8)); + REQUIRE(result.size() == 8); + for (const auto& it : result) { + REQUIRE(it == std::to_string(8)); + } + } + SECTION("Vector of String Input by value") { + std::vector data{"1", "2", "3", "4", "5"}; + int size = 0; + REQUIRE_NOTHROW(size = stringVectorInput(data)); + REQUIRE(size == 5); + } + SECTION("Vector of String Input by const ref") { + std::vector data{"1", "2", "3", "4", "5"}; + int size = 0; + REQUIRE_NOTHROW(size = stringVectorRefInput(data)); + REQUIRE(size == 5); + } +} + +TEST_CASE("String Jagged Array usage", "[Foo]") { + SECTION("Jagged Array of String Output") { + std::vector> result; + REQUIRE_NOTHROW(result = stringJaggedArrayOutput(8)); + REQUIRE(result.size() == 8); + for (int i = 0; i < result.size(); ++i) { + REQUIRE(i + 1 == result[i].size()); + } + for (int i = 1; i <= result.size(); ++i) { + const auto& inner = result[i - 1]; + for (const auto& it : inner) { + REQUIRE(it == std::to_string(i)); + } + } + } + SECTION("Jagged Array of String Input by value") { + std::vector> data{{"1", "2", "3"}, {"4", "5"}}; + int size = 0; + REQUIRE_NOTHROW(size = stringJaggedArrayInput(data)); + REQUIRE(size == 2); + } + SECTION("Jagged Array of String Input by const ref") { + std::vector> data{{"1", "2", "3"}, {"4", "5"}}; + int size = 0; + REQUIRE_NOTHROW(size = stringJaggedArrayRefInput(data)); + REQUIRE(size == 2); + } +} + +TEST_CASE("Pair Vector usage", "[Foo]") { + SECTION("Vector of Pair Output") { + std::vector result; + REQUIRE_NOTHROW(result = stringVectorOutput(8)); + REQUIRE(result.size() == 8); + for (const auto& it : result) { + REQUIRE(it == std::to_string(8)); + } + } + SECTION("Vector of Pair Input by value") { + std::vector data{"1", "2", "3", "4", "5"}; + int size = 0; + REQUIRE_NOTHROW(size = stringVectorInput(data)); + REQUIRE(size == 5); + } + SECTION("Vector of Pair Input by const ref") { + std::vector data{"1", "2", "3", "4", "5"}; + int size = 0; + REQUIRE_NOTHROW(size = stringVectorRefInput(data)); + REQUIRE(size == 5); + } +} + +TEST_CASE("Pair Jagged Array usage", "[Foo]") { + SECTION("Jagged Array of Pair Output") { + std::vector>> result; + REQUIRE_NOTHROW(result = pairJaggedArrayOutput(8)); + REQUIRE(result.size() == 8); + for (int i = 0; i < result.size(); ++i) { + REQUIRE(i + 1 == result[i].size()); + } + for (int i = 1; i <= result.size(); ++i) { + const auto& inner = result[i - 1]; + for (const auto& it : inner) { + REQUIRE(it == std::make_pair(i, i)); + } + } + } + SECTION("Jagged Array of Pair Input by value") { + std::vector>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}}; + int size = 0; + REQUIRE_NOTHROW(size = pairJaggedArrayInput(data)); + REQUIRE(size == 2); + } + SECTION("Jagged Array of Pair Input by const ref") { + std::vector>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}}; + int size = 0; + REQUIRE_NOTHROW(size = pairJaggedArrayRefInput(data)); + REQUIRE(size == 2); + } +} + +TEST_CASE("Foo static method", "[Foo]") { + SECTION("Int Method") { REQUIRE_NOTHROW(Foo::staticFunction(42)); } + SECTION("Int64_t Method") { REQUIRE_NOTHROW(Foo::staticFunction(int64_t{42})); } +} + +TEST_CASE("Foo::Ctor", "[Foo]") { + SECTION("Default constructor") { + Foo* b = new Foo(); + REQUIRE(b != nullptr); + } +} + +SCENARIO("Foo Int", "[Foo]") { + GIVEN("A Foo instance") { + Foo foo; + WHEN("Setting a value") { + REQUIRE_NOTHROW(foo.setInt(42)); + THEN("The value is updated") { REQUIRE(foo.getInt() == 42); } + } + } +} + +SCENARIO("Foo Int64", "[Foo]") { + GIVEN("A Foo instance") { + Foo foo; + WHEN("Setting a value") { + REQUIRE_NOTHROW(foo.setInt64(31)); + THEN("The value is updated") { REQUIRE(foo.getInt64() == 31); } + } + } +} + +TEST_CASE("Foo::operator()", "[Foo]") { + SECTION("Debug print") { INFO("Foo: " << Foo()()); } +} + +} // namespace foo diff --git a/Foo/test/src/Foo_UT.cpp b/Foo/tests/src/Foo_UT.cpp similarity index 100% rename from Foo/test/src/Foo_UT.cpp rename to Foo/tests/src/Foo_UT.cpp diff --git a/Foo/test/src/main.cpp b/Foo/tests/src/main.cpp similarity index 100% rename from Foo/test/src/main.cpp rename to Foo/tests/src/main.cpp