diff --git a/const_fmt/format_impl.h b/const_fmt/format_impl.h index 3fe0359..c91aa0f 100644 --- a/const_fmt/format_impl.h +++ b/const_fmt/format_impl.h @@ -148,7 +148,7 @@ template constexpr inline void format_int(char* out, int_t value) { const auto [abs_value, negative] = get_abs_value(value); - const int n_digits = count_digits(abs_value); + const std::size_t n_digits = count_digits(abs_value); format_decimal(out + 1 * (negative), abs_value, n_digits, t_fmt_node.length - 1 * (negative)); @@ -156,7 +156,8 @@ constexpr inline void format_int(char* out, int_t value) { if constexpr (t_fmt_node.has_zero_padding) { if (negative) *(out) = '-'; } else { - if (negative) *(out + t_fmt_node.length - n_digits - 1) = '-'; + if (n_digits < t_fmt_node.length) + if (negative) *(out + t_fmt_node.length - n_digits - 1) = '-'; } } @@ -194,10 +195,12 @@ constexpr inline void format_float(char* out, float_t value) { constexpr std::size_t factor = const_pow(10, t_fmt_node.precision); const int fractional = static_cast((value - integral) * factor); - const auto [fractional_abs, fractional_negative] = get_abs_value(fractional); + const auto [fractional_abs, fractional_negative] = + get_abs_value(fractional); format_int(out, integral); - format_int(out + t_fmt_node.length - t_fmt_node.precision, fractional_abs); + format_int( + out + t_fmt_node.length - t_fmt_node.precision, fractional_abs); } diff --git a/examples/src/examples.cpp b/examples/src/examples.cpp index 82ab7ea..08862a5 100644 --- a/examples/src/examples.cpp +++ b/examples/src/examples.cpp @@ -5,7 +5,7 @@ using namespace const_fmt; int main() { - constexpr auto s = "abcdef {:9.3}"_const_fmt(-876.3); + constexpr auto s = "abcdef {:09.2}"_const_fmt(-876.3); // Convert s (with a type of 'std::array') into something // writable to std::cout diff --git a/test/src/format.cpp b/test/src/format.cpp index 00f53d9..33e1c42 100644 --- a/test/src/format.cpp +++ b/test/src/format.cpp @@ -8,59 +8,101 @@ using namespace const_fmt::const_fmt_detail; TEST(Format, positive_int) { constexpr std::array control1 = {'0', '0', '0', '0', - '0', '0', '0', '2'}; + '0', '0', '0', '2'}; constexpr std::array formatted1 = const_format<"{:08}">(2); constexpr std::array control2 = {' ', ' ', ' ', '2', - '2', '2', '2', '2'}; + '2', '2', '2', '2'}; constexpr std::array formatted2 = const_format<"{:8}">(22222); constexpr std::array control3 = {'0', '0', '0', '1', - '2', '3', '4', '5'}; + '2', '3', '4', '5'}; constexpr std::array formatted3 = const_format<"{:08.4}">(12345); constexpr std::array control4 = {'6', '7', '8', '9'}; constexpr std::array formatted4 = const_format<"{:4}">(6789); + constexpr std::array control5 = {'f', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:4}">(67895); + EXPECT_EQ(control1, formatted1); EXPECT_EQ(control2, formatted2); EXPECT_EQ(control3, formatted3); EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); } TEST(Format, negative_int) { constexpr std::array control1 = {'-', '0', '0', '0', - '0', '0', '0', '2'}; + '0', '0', '0', '2'}; constexpr std::array formatted1 = const_format<"{:08}">(-2); constexpr std::array control2 = {' ', ' ', '-', '2', - '2', '2', '2', '2'}; + '2', '2', '2', '2'}; constexpr std::array formatted2 = const_format<"{:8}">(-22222); constexpr std::array control3 = {'-', '0', '0', '1', - '2', '3', '4', '5'}; + '2', '3', '4', '5'}; constexpr std::array formatted3 = const_format<"{:08.4}">(-12345); constexpr std::array control4 = {'-', '6', '7', '8', '9'}; constexpr std::array formatted4 = const_format<"{:5}">(-6789); + constexpr std::array control5 = {'-', 'f', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:05}">(-66789); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); +} + +TEST(Format, positive_float) { + constexpr std::array control1 = {'0', '0', '2', '.', + '3', '4', '5', '6'}; + constexpr std::array formatted1 = const_format<"{:08.4}">(2.3456); + + // Float error: 2323.2 -> 2323.1 + constexpr std::array control2 = {' ', ' ', '2', '3', + '2', '3', '.', '1'}; + constexpr std::array formatted2 = const_format<"{:8.1}">(2323.2); + + constexpr std::array control3 = {'1', '2', '3', '4', + '.', '5', '0', '0'}; + constexpr std::array formatted3 = const_format<"{:08.3}">(1234.5); + + // Float error: .9 -> .8 + constexpr std::array control4 = {'f', 'f', '.', '8'}; + constexpr std::array formatted4 = const_format<"{:4.1}">(678.9); + EXPECT_EQ(control1, formatted1); EXPECT_EQ(control2, formatted2); EXPECT_EQ(control3, formatted3); EXPECT_EQ(control4, formatted4); } + +TEST(Format, negative_float) { + constexpr std::array control1 = {'-', '0', '2', '.', + '3', '4', '5', '6'}; + constexpr std::array formatted1 = const_format<"{:08.4}">(-2.3456); + + // Float error: 2323.2 -> 2323.1 + constexpr std::array control2 = {' ', '-', '2', '3', + '2', '3', '.', '1'}; + constexpr std::array formatted2 = const_format<"{:8.1}">(-2323.2); + + constexpr std::array control3 = {'-', 'f', 'f', 'f', + '.', '5', '0', '0'}; + constexpr std::array formatted3 = const_format<"{:08.3}">(-1234.5); + + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); +} // -//TEST(Format, positive_float) { -// // TODO -// EXPECT_EQ(true, false); -//} -// -//TEST(Format, negative_float) { -// // TODO -// EXPECT_EQ(true, false); -//} -// -//TEST(Format, string) { +// TEST(Format, string) { // // TODO // EXPECT_EQ(true, false); //}