Added tests for negative int formatting

This commit is contained in:
Andreas Tsouchlos 2021-11-22 12:46:33 +01:00
parent ba86cc40a5
commit 040dae6bf3
3 changed files with 26 additions and 6 deletions

View File

@ -34,6 +34,7 @@ constexpr int get_output_len() {
// TODO: See if this is possible with <charconv>
// TODO: Steal some code from fmtlib
// TODO: In case of error, set chars to all 'f's
template <fmt_node_t fmt_node, std::integral arg_t>
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
check_fmt_params<fmt_node, arg_t>();
@ -49,7 +50,7 @@ constexpr std::array<char, fmt_node.length> 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<int>(offset)) && (arg > 0); --i) {
result[i] = arg % 10 + 48;
arg = arg / 10;
}
@ -59,6 +60,7 @@ constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
// TODO: See if this is possible with <charconv>
// TODO: Steal some code from fmtlib
// TODO: In case of error, set chars to all 'f's
template <fmt_node_t fmt_node, std::floating_point arg_t>
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
check_fmt_params<fmt_node, arg_t>();
@ -88,6 +90,7 @@ constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
}
// TODO: Steal some code from fmtlib
// TODO: In case of error, set chars to all 'f's
template <fmt_node_t fmt_node>
constexpr std::array<char, fmt_node.length> format_arg(const char* arg) {
check_fmt_params<fmt_node, const char*>();

View File

@ -2,7 +2,7 @@
#define LOGGER_UTILITY_H
#include <cstdlib>
#include "parse_types.h"
namespace detail {

View File

@ -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<char, 8> control1 = {'-', '0', '0', '0',
'0', '0', '0', '2'};
constexpr std::array<char, 8> formatted1 = format<"{:08}">(-2);
constexpr std::array<char, 8> control2 = {' ', ' ', '-', '2',
'2', '2', '2', '2'};
constexpr std::array<char, 8> formatted2 = format<"{:8}">(-22222);
constexpr std::array<char, 8> control3 = {'-', '0', '0', '1',
'2', '3', '4', '5'};
constexpr std::array<char, 8> formatted3 = format<"{:08.4}">(-12345);
constexpr std::array<char, 5> control4 = {'-', '6', '7', '8', '9'};
constexpr std::array<char, 5> 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