From 040dae6bf3cc367ef4b3d18dbcb4f2276f159945 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Mon, 22 Nov 2021 12:46:33 +0100 Subject: [PATCH] Added tests for negative int formatting --- inc/format.h | 5 ++++- inc/utility.h | 2 +- test/src/format.cpp | 25 +++++++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/inc/format.h b/inc/format.h index 4a66415..0143613 100644 --- a/inc/format.h +++ b/inc/format.h @@ -34,6 +34,7 @@ constexpr int get_output_len() { // TODO: See if this is possible with // TODO: Steal some code from fmtlib +// TODO: In case of error, set chars to all 'f's template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); @@ -49,7 +50,7 @@ constexpr std::array format_arg(arg_t arg) { ++offset; } - for (int i = result.size() - 1; (i >= offset) && (arg > 0); --i) { + for (int i = result.size() - 1; (i >= static_cast(offset)) && (arg > 0); --i) { result[i] = arg % 10 + 48; arg = arg / 10; } @@ -59,6 +60,7 @@ constexpr std::array format_arg(arg_t arg) { // TODO: See if this is possible with // TODO: Steal some code from fmtlib +// TODO: In case of error, set chars to all 'f's template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); @@ -88,6 +90,7 @@ constexpr std::array format_arg(arg_t arg) { } // TODO: Steal some code from fmtlib +// TODO: In case of error, set chars to all 'f's template constexpr std::array format_arg(const char* arg) { check_fmt_params(); diff --git a/inc/utility.h b/inc/utility.h index b43eba5..26f41a7 100644 --- a/inc/utility.h +++ b/inc/utility.h @@ -2,7 +2,7 @@ #define LOGGER_UTILITY_H -#include +#include "parse_types.h" namespace detail { diff --git a/test/src/format.cpp b/test/src/format.cpp index 800ec11..1d479c7 100644 --- a/test/src/format.cpp +++ b/test/src/format.cpp @@ -27,10 +27,27 @@ TEST(Format, positive_int) { EXPECT_EQ(control4, formatted4); } -//TEST(Format, negative_int) { -// // TODO -// EXPECT_EQ(true, false); -//} +TEST(Format, negative_int) { + constexpr std::array control1 = {'-', '0', '0', '0', + '0', '0', '0', '2'}; + constexpr std::array formatted1 = format<"{:08}">(-2); + + constexpr std::array control2 = {' ', ' ', '-', '2', + '2', '2', '2', '2'}; + constexpr std::array formatted2 = format<"{:8}">(-22222); + + constexpr std::array control3 = {'-', '0', '0', '1', + '2', '3', '4', '5'}; + constexpr std::array formatted3 = format<"{:08.4}">(-12345); + + constexpr std::array control4 = {'-', '6', '7', '8', '9'}; + constexpr std::array formatted4 = format<"{:5}">(-6789); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); +} // //TEST(Format, positive_float) { // // TODO