+ unit test for xo::fixed

This commit is contained in:
Roland Conybeare 2023-09-19 17:12:13 -04:00
commit 7676b7a676
4 changed files with 116 additions and 0 deletions

View file

@ -20,6 +20,7 @@ endif()
include(cmake/nestlog.cmake)
add_subdirectory(example)
add_subdirectory(utest)
# header-only library
#add_library(indentlog INTERFACE)

25
utest/CMakeLists.txt Normal file
View file

@ -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()

84
utest/fixed.test.cpp Normal file
View file

@ -0,0 +1,84 @@
/* @file fixed.test.cpp */
#include "indentlog/print/fixed.hpp"
#include "indentlog/print/tag.hpp"
#include <catch2/catch.hpp>
#include <sstream>
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<fixed_tcase> 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 */

View file

@ -0,0 +1,6 @@
/* @file indentlog_utest_main.cpp */
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
/* end indentlog_utest_main.cpp */