From 5a4b9e329844cf39febf70a48cf9d8cd053e27e4 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Sun, 21 Nov 2021 22:46:19 +0100 Subject: [PATCH] Started adding unit tests --- .gitmodules | 3 +++ CMakeLists.txt | 10 +++++++++- inc/parse_types.h | 17 +++++++++++++++++ inc/utility.h | 3 +++ test/CMakeLists.txt | 21 +++++++++++++++++++++ test/googletest | 1 + test/src/format.cpp | 2 ++ test/src/parse.cpp | 18 ++++++++++++++++++ test/src/utility.cpp | 9 +++++++++ 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 test/CMakeLists.txt create mode 160000 test/googletest create mode 100644 test/src/format.cpp create mode 100644 test/src/parse.cpp create mode 100644 test/src/utility.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6889aed --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/googletest"] + path = test/googletest + url = https://github.com/google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index b215b99..cda1bcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,14 @@ project(logger) set(CMAKE_CXX_STANDARD 20) + if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() +include_directories(inc) + add_executable(logger src/main.cpp) @@ -18,4 +21,9 @@ else() endif() -include_directories(inc) \ No newline at end of file +option(PACKAGE_TESTS "Build the tests" ON) +if(PACKAGE_TESTS) + enable_testing() + include(GoogleTest) + add_subdirectory(test) +endif() diff --git a/inc/parse_types.h b/inc/parse_types.h index c4e69dd..d2a613b 100644 --- a/inc/parse_types.h +++ b/inc/parse_types.h @@ -18,6 +18,12 @@ struct fmt_node_t { int length = 6; int precision = 2; FormatType type = FormatType::s; + + constexpr bool operator==(const fmt_node_t& rhs) const { + return (rhs.has_zero_padding == has_zero_padding) && + (rhs.length == length) && (rhs.precision == precision) && + (rhs.type == type); + } }; @@ -26,6 +32,17 @@ public: constexpr ast_node_t() { } + constexpr ast_node_t(char c) : m_is_char(true), m_c(c) { + } + + constexpr ast_node_t(fmt_node_t node) : m_is_char(false), m_node(node) { + } + + constexpr bool operator==(const ast_node_t& rhs) const { + return ((rhs.m_is_char == m_is_char) && (rhs.m_c == m_c)) || + ((rhs.m_is_char != m_is_char) && (rhs.m_node == m_node)); + } + constexpr void set_char(char c) { m_c = c; m_is_char = true; diff --git a/inc/utility.h b/inc/utility.h index d11ecd8..168e3d0 100644 --- a/inc/utility.h +++ b/inc/utility.h @@ -2,6 +2,9 @@ #define LOGGER_UTILITY_H +#include + + namespace detail { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..892a542 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,21 @@ +add_subdirectory("googletest") + +mark_as_advanced( + BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS + gmock_build_tests gtest_build_samples gtest_build_tests + gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols +) + +macro(package_add_test TESTNAME) + add_executable(${TESTNAME} ${ARGN}) + target_link_libraries(${TESTNAME} gtest gmock gtest_main) + gtest_discover_tests(${TESTNAME} + WORKING_DIRECTORY ${PROJECT_DIR} + PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}" + ) + set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) +endmacro() + +package_add_test(utility_test src/utility.cpp) +package_add_test(parse_test src/parse.cpp) + diff --git a/test/googletest b/test/googletest new file mode 160000 index 0000000..3e0e32b --- /dev/null +++ b/test/googletest @@ -0,0 +1 @@ +Subproject commit 3e0e32ba300ce8afe695ad3ba7e81b21b7cf237a diff --git a/test/src/format.cpp b/test/src/format.cpp new file mode 100644 index 0000000..edae684 --- /dev/null +++ b/test/src/format.cpp @@ -0,0 +1,2 @@ +#include +#include \ No newline at end of file diff --git a/test/src/parse.cpp b/test/src/parse.cpp new file mode 100644 index 0000000..2102a14 --- /dev/null +++ b/test/src/parse.cpp @@ -0,0 +1,18 @@ +#include +#include + +TEST(Parse, ast_node) { + ast_node_t ast_node1{'a'}; + ast_node_t ast_node2{'a'}; + ast_node_t ast_node3{'b'}; + + EXPECT_EQ(ast_node1 == ast_node2, true); + EXPECT_EQ(ast_node1 == ast_node3, false); +} + +TEST(Parse, chars_only) { + constexpr std::array control = {'a', 'b', 'c', 'd', + 'e', 'f', 'g', 'h'}; + + EXPECT_EQ(detail::parse_string<"abcdefgh">().value, control); +} diff --git a/test/src/utility.cpp b/test/src/utility.cpp new file mode 100644 index 0000000..dc8798a --- /dev/null +++ b/test/src/utility.cpp @@ -0,0 +1,9 @@ +#include +#include + +TEST(Utility, const_pow) { + EXPECT_EQ(detail::const_pow(10, 0), 1); + EXPECT_EQ(detail::const_pow(0, 1), 0); + EXPECT_EQ(detail::const_pow(2, 8), 0b1'0000'0000); +} +