diff --git a/CMakeLists.txt b/CMakeLists.txt index b5a8caaa..9318caa2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ endif() include(cmake/nestlog.cmake) add_subdirectory(example) +add_subdirectory(utest) # header-only library #add_library(indentlog INTERFACE) diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt new file mode 100644 index 00000000..187a92c3 --- /dev/null +++ b/utest/CMakeLists.txt @@ -0,0 +1,25 @@ +# indentlog unit test + +set(SELF_EXECUTABLE_NAME utest.indentlog) +set(SELF_SOURCE_FILES fixed.test.cpp indentlog_utest_main.cpp) + +add_executable(${SELF_EXECUTABLE_NAME} ${SELF_SOURCE_FILES}) +xo_include_options(${SELF_EXECUTABLE_NAME}) + +add_test(NAME ${SELF_EXECUTABLE_NAME} COMMAND ${SELF_EXECUTABLE_NAME}) + +# ---------------------------------------------------------------- +# 3rd party dependency: catch2 + +find_package(Catch2 2 REQUIRED) + +# ---------------------------------------------------------------- +# make standard directories for std:: includes explicit +# so that +# (1) they appear in compile_commands.json. +# (2) clangd (run from emacs lsp-mode) can find them +# +if(CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES + ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) +endif() diff --git a/utest/fixed.test.cpp b/utest/fixed.test.cpp new file mode 100644 index 00000000..d882e542 --- /dev/null +++ b/utest/fixed.test.cpp @@ -0,0 +1,84 @@ +/* @file fixed.test.cpp */ + +#include "indentlog/print/fixed.hpp" +#include "indentlog/print/tag.hpp" +#include +#include + +using namespace xo; + +namespace ut { + struct fixed_tcase { + fixed_tcase() = default; + fixed_tcase(double x, std::uint32_t prec, std::string s) + : x_{x}, prec_{prec}, s_{std::move(s)} {} + + /* floating-point value to format */ + double x_ = 0.0; + /* precision */ + std::uint32_t prec_ = 0; + /* expected result */ + std::string s_; + }; /*fixed_tcase*/ + + std::vector s_fixed_tcase_v( + {fixed_tcase(0.0, 0, "0"), + fixed_tcase(0.0, 1, "0.0"), + fixed_tcase(0.0, 2, "0.00"), + + //fixed_tcase(0.5, 0, "1"), // failing --> 0 + fixed_tcase(0.5, 1, "0.5"), + + fixed_tcase(0.049, 0, "0"), + fixed_tcase(0.049, 1, "0.0"), + fixed_tcase(0.049, 2, "0.05"), + + fixed_tcase(0.05, 0, "0"), + fixed_tcase(0.05, 1, "0.1"), + fixed_tcase(0.05, 2, "0.05"), + + fixed_tcase(1e-6, 0, "0"), + fixed_tcase(1e-6, 1, "0.0"), + fixed_tcase(1e-6, 2, "0.00"), + fixed_tcase(1e-6, 3, "0.000"), + fixed_tcase(1e-6, 4, "0.0000"), + fixed_tcase(1e-6, 5, "0.00000"), + fixed_tcase(1e-6, 6, "0.000001"), + + fixed_tcase(-1e-6, 0, "-0"), + fixed_tcase(-1e-6, 1, "-0.0"), + fixed_tcase(-1e-6, 2, "-0.00"), + fixed_tcase(-1e-6, 3, "-0.000"), + fixed_tcase(-1e-6, 4, "-0.0000"), + fixed_tcase(-1e-6, 5, "-0.00000"), + fixed_tcase(-1e-6, 6, "-0.000001"), + + fixed_tcase(666.66, 1, "666.7"), + fixed_tcase(666.66, 2, "666.66"), + + fixed_tcase(-666.66, 1, "-666.7"), + fixed_tcase(-666.66, 2, "-666.66"), + + }); + + TEST_CASE("fixed", "[fixed]") { + tag_config::tag_color = color_spec_type::none(); + + for (std::uint32_t i_tc = 0, z_tc = s_fixed_tcase_v.size(); i_tc < z_tc; ++i_tc) { + fixed_tcase const & tc = s_fixed_tcase_v[i_tc]; + + INFO(tostr(xtag("i_tc", i_tc), xtag("x", tc.x_), xtag("prec", tc.prec_))); + + std::stringstream ss; + ss << fixed(tc.x_, tc.prec_); + + INFO(xtag("ss.str", ss.str())); + + REQUIRE(ss.str() == tc.s_); + } + + REQUIRE(s_fixed_tcase_v.size() > 1); + } +} /*namespace ut*/ + +/* end fixed.test.cpp */ diff --git a/utest/indentlog_utest_main.cpp b/utest/indentlog_utest_main.cpp new file mode 100644 index 00000000..6337b638 --- /dev/null +++ b/utest/indentlog_utest_main.cpp @@ -0,0 +1,6 @@ +/* @file indentlog_utest_main.cpp */ + +#define CATCH_CONFIG_MAIN +#include "catch2/catch.hpp" + +/* end indentlog_utest_main.cpp */