Skip to content

Commit

Permalink
rework Foo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Nov 23, 2021
1 parent cd1e699 commit 4153e50
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 42 deletions.
20 changes: 7 additions & 13 deletions Foo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
add_library(Foo "")
add_library(Foo)
target_sources(Foo
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/foo/Foo.hpp>
PRIVATE
src/Foo.cpp
)
include/foo/Foo.hpp
src/Foo.cpp)
target_include_directories(Foo
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_compile_features(Foo PUBLIC cxx_std_11)
$<INSTALL_INTERFACE:include>)
target_compile_features(Foo PUBLIC cxx_std_17)
set_target_properties(Foo PROPERTIES
VERSION ${PROJECT_VERSION}
PUBLIC_HEADER $<TARGET_PROPERTY:Foo,INTERFACE_SOURCES>
)
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
Expand Down
18 changes: 9 additions & 9 deletions Foo/src/Foo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
20 changes: 0 additions & 20 deletions Foo/test/CMakeLists.txt

This file was deleted.

8 changes: 8 additions & 0 deletions Foo/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
155 changes: 155 additions & 0 deletions Foo/tests/foo_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <catch2/catch_test_macros.hpp>
#include <iostream>
#include <numeric>
#include <string>

#include <foo/Foo.hpp>

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<std::string> 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<std::string> 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<std::string> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::string> 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<std::string> 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<std::string> 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<std::vector<std::pair<int, int>>> 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<std::vector<std::pair<int, int>>> 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<std::vector<std::pair<int, int>>> 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
File renamed without changes.
File renamed without changes.

0 comments on commit 4153e50

Please sign in to comment.