2018-03-04 20:16:51 +03:00
|
|
|
// Formatting library for C++ - printf tests
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
2014-06-06 17:38:37 +04:00
|
|
|
|
2014-08-21 18:29:23 +04:00
|
|
|
#include <cctype>
|
2014-06-06 17:38:37 +04:00
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
|
|
|
|
2017-12-06 18:42:42 +03:00
|
|
|
#include "fmt/core.h"
|
2016-06-08 02:23:32 +03:00
|
|
|
#include "fmt/printf.h"
|
2014-06-06 17:38:37 +04:00
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-06-29 02:58:02 +04:00
|
|
|
using fmt::format;
|
2016-08-25 18:38:07 +03:00
|
|
|
using fmt::format_error;
|
2019-09-08 19:04:09 +03:00
|
|
|
using fmt::internal::max_value;
|
2014-06-06 17:38:37 +04:00
|
|
|
|
|
|
|
const unsigned BIG_NUM = INT_MAX + 1u;
|
|
|
|
|
2014-06-06 19:01:25 +04:00
|
|
|
// Makes format string argument positional.
|
2018-06-06 16:57:59 +03:00
|
|
|
static std::string make_positional(fmt::string_view format) {
|
2018-02-07 18:37:05 +03:00
|
|
|
std::string s(format.data(), format.size());
|
2014-06-06 19:01:25 +04:00
|
|
|
s.replace(s.find('%'), 1, "%1$");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:57:59 +03:00
|
|
|
static std::wstring make_positional(fmt::wstring_view format) {
|
2018-04-26 21:32:14 +03:00
|
|
|
std::wstring s(format.data(), format.size());
|
|
|
|
s.replace(s.find(L'%'), 1, L"%1$");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:04:45 +03:00
|
|
|
// A wrapper around fmt::sprintf to workaround bogus warnings about invalid
|
|
|
|
// format strings in MSVC.
|
2018-12-06 21:12:42 +03:00
|
|
|
template <typename... Args>
|
2019-01-13 05:27:38 +03:00
|
|
|
std::string test_sprintf(fmt::string_view format, const Args&... args) {
|
2018-12-06 17:04:45 +03:00
|
|
|
return fmt::sprintf(format, args...);
|
|
|
|
}
|
2018-12-06 21:12:42 +03:00
|
|
|
template <typename... Args>
|
2019-01-13 05:27:38 +03:00
|
|
|
std::wstring test_sprintf(fmt::wstring_view format, const Args&... args) {
|
2018-12-06 21:12:42 +03:00
|
|
|
return fmt::sprintf(format, args...);
|
|
|
|
}
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
#define EXPECT_PRINTF(expected_output, format, arg) \
|
2018-12-06 21:12:42 +03:00
|
|
|
EXPECT_EQ(expected_output, test_sprintf(format, arg)) \
|
2019-01-13 05:27:38 +03:00
|
|
|
<< "format: " << format; \
|
2018-12-06 21:12:42 +03:00
|
|
|
EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))
|
2018-12-06 17:04:45 +03:00
|
|
|
|
2014-06-06 17:38:37 +04:00
|
|
|
TEST(PrintfTest, NoArgs) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("test", test_sprintf("test"));
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_EQ(L"test", fmt::sprintf(L"test"));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, Escape) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("%", test_sprintf("%%"));
|
|
|
|
EXPECT_EQ("before %", test_sprintf("before %%"));
|
|
|
|
EXPECT_EQ("% after", test_sprintf("%% after"));
|
|
|
|
EXPECT_EQ("before % after", test_sprintf("before %% after"));
|
|
|
|
EXPECT_EQ("%s", test_sprintf("%%s"));
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_EQ(L"%", fmt::sprintf(L"%%"));
|
|
|
|
EXPECT_EQ(L"before %", fmt::sprintf(L"before %%"));
|
|
|
|
EXPECT_EQ(L"% after", fmt::sprintf(L"%% after"));
|
|
|
|
EXPECT_EQ(L"before % after", fmt::sprintf(L"before %% after"));
|
|
|
|
EXPECT_EQ(L"%s", fmt::sprintf(L"%%s"));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, PositionalArgs) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("42", test_sprintf("%1$d", 42));
|
|
|
|
EXPECT_EQ("before 42", test_sprintf("before %1$d", 42));
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ("42 after", test_sprintf("%1$d after", 42));
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("before 42 after", test_sprintf("before %1$d after", 42));
|
|
|
|
EXPECT_EQ("answer = 42", test_sprintf("%1$s = %2$d", "answer", 42));
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ("42 is the answer", test_sprintf("%2$d is the %1$s", "answer", 42));
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("abracadabra", test_sprintf("%1$s%2$s%1$s", "abra", "cad"));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, AutomaticArgIndexing) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("abc", test_sprintf("%c%c%c", 'a', 'b', 'c'));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, NumberIsTooBigInArgIndex) {
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%{}$", BIG_NUM)), format_error,
|
|
|
|
"number is too big");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", BIG_NUM)), format_error,
|
|
|
|
"number is too big");
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, SwitchArgIndexing) {
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%1$d%", 1, 2), format_error,
|
|
|
|
"cannot switch from manual to automatic argument indexing");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
2019-01-13 05:27:38 +03:00
|
|
|
format_error, "number is too big");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf("%1$d%d", 1, 2), format_error,
|
|
|
|
"cannot switch from manual to automatic argument indexing");
|
2014-06-06 17:38:37 +04:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%d%1$", 1, 2), format_error,
|
|
|
|
"cannot switch from automatic to manual argument indexing");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%d%{}$d", BIG_NUM), 1, 2), format_error,
|
|
|
|
"number is too big");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf("%d%1$d", 1, 2), format_error,
|
|
|
|
"cannot switch from automatic to manual argument indexing");
|
2014-06-06 17:38:37 +04:00
|
|
|
|
|
|
|
// Indexing errors override width errors.
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", BIG_NUM), 1, 2),
|
2019-01-13 05:27:38 +03:00
|
|
|
format_error, "number is too big");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
2019-01-13 05:27:38 +03:00
|
|
|
format_error, "number is too big");
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, InvalidArgIndex) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%0$d", 42), format_error,
|
2019-10-09 20:31:17 +03:00
|
|
|
"argument index out of range");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%2$d", 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"argument index out of range");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", INT_MAX), 42), format_error,
|
|
|
|
"argument index out of range");
|
2014-06-06 17:38:37 +04:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%2$", 42), format_error,
|
|
|
|
"argument index out of range");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", BIG_NUM), 42), format_error,
|
|
|
|
"number is too big");
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 18:40:51 +04:00
|
|
|
TEST(PrintfTest, DefaultAlignRight) {
|
|
|
|
EXPECT_PRINTF(" 42", "%5d", 42);
|
|
|
|
EXPECT_PRINTF(" abc", "%5s", "abc");
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:07:57 +04:00
|
|
|
TEST(PrintfTest, ZeroFlag) {
|
2014-06-06 19:01:25 +04:00
|
|
|
EXPECT_PRINTF("00042", "%05d", 42);
|
|
|
|
EXPECT_PRINTF("-0042", "%05d", -42);
|
|
|
|
|
|
|
|
EXPECT_PRINTF("00042", "%05d", 42);
|
|
|
|
EXPECT_PRINTF("-0042", "%05d", -42);
|
|
|
|
EXPECT_PRINTF("-004.2", "%06g", -4.2);
|
|
|
|
|
|
|
|
EXPECT_PRINTF("+00042", "%00+6d", 42);
|
2014-06-06 18:07:57 +04:00
|
|
|
|
2014-06-09 18:40:51 +04:00
|
|
|
// '0' flag is ignored for non-numeric types.
|
2017-09-04 22:28:53 +03:00
|
|
|
EXPECT_PRINTF("0000x", "%05c", 'x');
|
2014-06-06 18:07:57 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 18:40:51 +04:00
|
|
|
TEST(PrintfTest, PlusFlag) {
|
|
|
|
EXPECT_PRINTF("+42", "%+d", 42);
|
|
|
|
EXPECT_PRINTF("-42", "%+d", -42);
|
|
|
|
EXPECT_PRINTF("+0042", "%+05d", 42);
|
|
|
|
EXPECT_PRINTF("+0042", "%0++5d", 42);
|
2014-06-10 17:21:41 +04:00
|
|
|
|
|
|
|
// '+' flag is ignored for non-numeric types.
|
|
|
|
EXPECT_PRINTF("x", "%+c", 'x');
|
2014-06-09 18:40:51 +04:00
|
|
|
}
|
2014-06-06 17:38:37 +04:00
|
|
|
|
2014-06-09 18:40:51 +04:00
|
|
|
TEST(PrintfTest, MinusFlag) {
|
|
|
|
EXPECT_PRINTF("abc ", "%-5s", "abc");
|
|
|
|
EXPECT_PRINTF("abc ", "%0--5s", "abc");
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 18:40:51 +04:00
|
|
|
TEST(PrintfTest, SpaceFlag) {
|
|
|
|
EXPECT_PRINTF(" 42", "% d", 42);
|
|
|
|
EXPECT_PRINTF("-42", "% d", -42);
|
|
|
|
EXPECT_PRINTF(" 0042", "% 05d", 42);
|
|
|
|
EXPECT_PRINTF(" 0042", "%0 5d", 42);
|
2014-06-10 17:21:41 +04:00
|
|
|
|
|
|
|
// ' ' flag is ignored for non-numeric types.
|
|
|
|
EXPECT_PRINTF("x", "% c", 'x');
|
2014-06-09 18:40:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, HashFlag) {
|
|
|
|
EXPECT_PRINTF("042", "%#o", 042);
|
2014-08-19 19:49:10 +04:00
|
|
|
EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
|
2014-06-09 18:40:51 +04:00
|
|
|
EXPECT_PRINTF("0", "%#o", 0);
|
|
|
|
|
|
|
|
EXPECT_PRINTF("0x42", "%#x", 0x42);
|
|
|
|
EXPECT_PRINTF("0X42", "%#X", 0x42);
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_PRINTF(fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x",
|
|
|
|
-0x42);
|
2014-06-09 18:40:51 +04:00
|
|
|
EXPECT_PRINTF("0", "%#x", 0);
|
|
|
|
|
2014-06-10 17:21:41 +04:00
|
|
|
EXPECT_PRINTF("0x0042", "%#06x", 0x42);
|
2014-06-09 18:40:51 +04:00
|
|
|
EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
|
|
|
|
|
2014-06-10 17:21:41 +04:00
|
|
|
EXPECT_PRINTF("-42.000000", "%#f", -42.0);
|
|
|
|
EXPECT_PRINTF("-42.000000", "%#F", -42.0);
|
|
|
|
|
|
|
|
char buffer[BUFFER_SIZE];
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%#e", -42.0);
|
2014-06-10 17:21:41 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%#e", -42.0);
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%#E", -42.0);
|
2014-06-10 17:21:41 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%#E", -42.0);
|
|
|
|
|
2018-10-14 08:14:36 +03:00
|
|
|
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
|
|
|
|
EXPECT_PRINTF("-42.0000", "%#G", -42.0);
|
2014-06-10 17:21:41 +04:00
|
|
|
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%#a", 16.0);
|
2014-06-10 18:36:23 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%#a", 16.0);
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%#A", 16.0);
|
2014-06-10 18:36:23 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%#A", 16.0);
|
2014-06-09 18:40:51 +04:00
|
|
|
|
2014-06-10 17:21:41 +04:00
|
|
|
// '#' flag is ignored for non-numeric types.
|
|
|
|
EXPECT_PRINTF("x", "%#c", 'x');
|
2014-06-09 18:40:51 +04:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:34:02 +04:00
|
|
|
TEST(PrintfTest, Width) {
|
|
|
|
EXPECT_PRINTF(" abc", "%5s", "abc");
|
|
|
|
|
|
|
|
// Width cannot be specified twice.
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"invalid type specifier");
|
2014-06-20 18:34:02 +04:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%{}d", BIG_NUM), 42), format_error,
|
|
|
|
"number is too big");
|
|
|
|
EXPECT_THROW_MSG(test_sprintf(format("%1${}d", BIG_NUM), 42), format_error,
|
|
|
|
"number is too big");
|
2014-06-20 18:34:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, DynamicWidth) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ(" 42", test_sprintf("%*d", 5, 42));
|
|
|
|
EXPECT_EQ("42 ", test_sprintf("%*d", -5, 42));
|
|
|
|
EXPECT_THROW_MSG(test_sprintf("%*d", 5.0, 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"width is not integer");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%*d"), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"argument index out of range");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%*d", BIG_NUM, 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"number is too big");
|
2014-06-20 18:34:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:07:10 +04:00
|
|
|
TEST(PrintfTest, IntPrecision) {
|
2014-06-20 18:34:02 +04:00
|
|
|
EXPECT_PRINTF("00042", "%.5d", 42);
|
2014-06-20 19:04:44 +04:00
|
|
|
EXPECT_PRINTF("-00042", "%.5d", -42);
|
2014-06-20 18:59:23 +04:00
|
|
|
EXPECT_PRINTF("00042", "%.5x", 0x42);
|
|
|
|
EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
|
|
|
|
EXPECT_PRINTF("00042", "%.5o", 042);
|
|
|
|
EXPECT_PRINTF("00042", "%#.5o", 042);
|
|
|
|
|
|
|
|
EXPECT_PRINTF(" 00042", "%7.5d", 42);
|
|
|
|
EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
|
|
|
|
EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
|
|
|
|
EXPECT_PRINTF(" 00042", "%7.5o", 042);
|
|
|
|
EXPECT_PRINTF(" 00042", "%#10.5o", 042);
|
|
|
|
|
2014-06-21 19:18:05 +04:00
|
|
|
EXPECT_PRINTF("00042 ", "%-7.5d", 42);
|
|
|
|
EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
|
|
|
|
EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
|
|
|
|
EXPECT_PRINTF("00042 ", "%-7.5o", 042);
|
|
|
|
EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
|
2014-06-20 18:34:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:07:10 +04:00
|
|
|
TEST(PrintfTest, FloatPrecision) {
|
|
|
|
char buffer[BUFFER_SIZE];
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%.3e", 1234.5678);
|
2014-06-23 18:07:10 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
|
|
|
|
EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
|
2019-03-16 22:17:35 +03:00
|
|
|
EXPECT_PRINTF("1.23e+03", "%.3g", 1234.5678);
|
2014-07-29 18:50:05 +04:00
|
|
|
safe_sprintf(buffer, "%.3a", 1234.5678);
|
2014-06-23 18:07:10 +04:00
|
|
|
EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, IgnorePrecisionForNonNumericArg) {
|
|
|
|
EXPECT_PRINTF("abc", "%.5s", "abc");
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:16:46 +04:00
|
|
|
TEST(PrintfTest, DynamicPrecision) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("00042", test_sprintf("%.*d", 5, 42));
|
|
|
|
EXPECT_EQ("42", test_sprintf("%.*d", -5, 42));
|
|
|
|
EXPECT_THROW_MSG(test_sprintf("%.*d", 5.0, 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"precision is not integer");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"argument index out of range");
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%.*d", BIG_NUM, 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"number is too big");
|
2017-08-26 19:09:43 +03:00
|
|
|
if (sizeof(long long) != sizeof(int)) {
|
|
|
|
long long prec = static_cast<long long>(INT_MIN) - 1;
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_THROW_MSG(test_sprintf("%.*d", prec, 42), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"number is too big");
|
|
|
|
}
|
2014-06-23 18:16:46 +04:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
template <typename T> struct make_signed { typedef T type; };
|
2014-08-06 19:21:12 +04:00
|
|
|
|
|
|
|
#define SPECIALIZE_MAKE_SIGNED(T, S) \
|
2019-01-13 05:27:38 +03:00
|
|
|
template <> struct make_signed<T> { typedef S type; }
|
2014-08-06 19:21:12 +04:00
|
|
|
|
|
|
|
SPECIALIZE_MAKE_SIGNED(char, signed char);
|
|
|
|
SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
|
|
|
|
SPECIALIZE_MAKE_SIGNED(unsigned short, short);
|
|
|
|
SPECIALIZE_MAKE_SIGNED(unsigned, int);
|
|
|
|
SPECIALIZE_MAKE_SIGNED(unsigned long, long);
|
2017-08-26 19:09:43 +03:00
|
|
|
SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
|
2014-08-06 19:21:12 +04:00
|
|
|
|
2016-01-24 02:43:42 +03:00
|
|
|
// Test length format specifier ``length_spec``.
|
2014-08-05 18:52:59 +04:00
|
|
|
template <typename T, typename U>
|
2019-01-13 05:27:38 +03:00
|
|
|
void TestLength(const char* length_spec, U value) {
|
2017-08-26 19:09:43 +03:00
|
|
|
long long signed_value = 0;
|
|
|
|
unsigned long long unsigned_value = 0;
|
2014-08-12 19:15:39 +04:00
|
|
|
// Apply integer promotion to the argument.
|
2019-09-08 19:04:09 +03:00
|
|
|
unsigned long long max = max_value<U>();
|
2016-07-12 16:40:23 +03:00
|
|
|
using fmt::internal::const_check;
|
2019-09-08 19:04:09 +03:00
|
|
|
if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
|
2014-08-12 19:15:39 +04:00
|
|
|
signed_value = static_cast<int>(value);
|
2016-03-02 18:53:14 +03:00
|
|
|
unsigned_value = static_cast<unsigned>(value);
|
2019-09-08 19:04:09 +03:00
|
|
|
} else if (const_check(max <= max_value<unsigned>())) {
|
2014-08-12 19:15:39 +04:00
|
|
|
signed_value = static_cast<unsigned>(value);
|
|
|
|
unsigned_value = static_cast<unsigned>(value);
|
|
|
|
}
|
|
|
|
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
|
2017-08-26 19:09:43 +03:00
|
|
|
signed_value = static_cast<long long>(value);
|
2017-09-04 21:41:15 +03:00
|
|
|
unsigned_value =
|
|
|
|
static_cast<typename std::make_unsigned<unsigned>::type>(value);
|
2014-08-12 19:15:39 +04:00
|
|
|
} else {
|
2017-02-19 17:46:51 +03:00
|
|
|
signed_value = static_cast<typename make_signed<T>::type>(value);
|
2017-09-04 21:41:15 +03:00
|
|
|
unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
|
2014-08-12 19:15:39 +04:00
|
|
|
}
|
2014-08-11 19:34:17 +04:00
|
|
|
std::ostringstream os;
|
2014-08-12 19:15:39 +04:00
|
|
|
os << signed_value;
|
2014-08-11 19:34:17 +04:00
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
|
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
|
|
|
|
os.str("");
|
|
|
|
os << unsigned_value;
|
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
|
|
|
|
os.str("");
|
|
|
|
os << std::oct << unsigned_value;
|
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
|
|
|
|
os.str("");
|
|
|
|
os << std::hex << unsigned_value;
|
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
|
|
|
|
os.str("");
|
|
|
|
os << std::hex << std::uppercase << unsigned_value;
|
|
|
|
EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
|
2014-07-30 19:08:08 +04:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
template <typename T> void TestLength(const char* length_spec) {
|
2019-09-08 19:04:09 +03:00
|
|
|
T min = std::numeric_limits<T>::min(), max = max_value<T>();
|
2014-08-11 19:34:17 +04:00
|
|
|
TestLength<T>(length_spec, 42);
|
|
|
|
TestLength<T>(length_spec, -42);
|
|
|
|
TestLength<T>(length_spec, min);
|
|
|
|
TestLength<T>(length_spec, max);
|
2019-10-08 16:28:39 +03:00
|
|
|
long long long_long_min = std::numeric_limits<long long>::min();
|
|
|
|
if (static_cast<long long>(min) > long_long_min)
|
|
|
|
TestLength<T>(length_spec, static_cast<long long>(min) - 1);
|
2019-09-08 19:04:09 +03:00
|
|
|
unsigned long long long_long_max = max_value<long long>();
|
2017-08-26 19:09:43 +03:00
|
|
|
if (static_cast<unsigned long long>(max) < long_long_max)
|
|
|
|
TestLength<T>(length_spec, static_cast<long long>(max) + 1);
|
2014-08-11 19:34:17 +04:00
|
|
|
TestLength<T>(length_spec, std::numeric_limits<short>::min());
|
2019-09-08 19:04:09 +03:00
|
|
|
TestLength<T>(length_spec, max_value<unsigned short>());
|
2014-08-11 19:34:17 +04:00
|
|
|
TestLength<T>(length_spec, std::numeric_limits<int>::min());
|
2019-09-08 19:04:09 +03:00
|
|
|
TestLength<T>(length_spec, max_value<int>());
|
2014-08-11 19:34:17 +04:00
|
|
|
TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
|
2019-09-08 19:04:09 +03:00
|
|
|
TestLength<T>(length_spec, max_value<unsigned>());
|
2017-08-26 19:09:43 +03:00
|
|
|
TestLength<T>(length_spec, std::numeric_limits<long long>::min());
|
2019-09-08 19:04:09 +03:00
|
|
|
TestLength<T>(length_spec, max_value<long long>());
|
2017-08-26 19:09:43 +03:00
|
|
|
TestLength<T>(length_spec, std::numeric_limits<unsigned long long>::min());
|
2019-09-08 19:04:09 +03:00
|
|
|
TestLength<T>(length_spec, max_value<unsigned long long>());
|
2014-07-30 19:08:08 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 19:39:07 +04:00
|
|
|
TEST(PrintfTest, Length) {
|
2014-08-11 19:34:17 +04:00
|
|
|
TestLength<char>("hh");
|
2014-08-05 19:21:47 +04:00
|
|
|
TestLength<signed char>("hh");
|
2014-08-08 17:59:17 +04:00
|
|
|
TestLength<unsigned char>("hh");
|
2014-08-12 17:49:27 +04:00
|
|
|
TestLength<short>("h");
|
2014-08-09 19:53:14 +04:00
|
|
|
TestLength<unsigned short>("h");
|
2014-08-12 19:15:39 +04:00
|
|
|
TestLength<long>("l");
|
|
|
|
TestLength<unsigned long>("l");
|
2017-08-26 19:09:43 +03:00
|
|
|
TestLength<long long>("ll");
|
|
|
|
TestLength<unsigned long long>("ll");
|
2014-08-13 17:42:15 +04:00
|
|
|
TestLength<intmax_t>("j");
|
|
|
|
TestLength<std::size_t>("z");
|
|
|
|
TestLength<std::ptrdiff_t>("t");
|
2019-09-08 19:04:09 +03:00
|
|
|
long double max = max_value<long double>();
|
2019-02-03 18:44:42 +03:00
|
|
|
EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
|
|
|
|
EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
|
2014-08-19 19:14:21 +04:00
|
|
|
}
|
|
|
|
|
2015-11-13 18:18:44 +03:00
|
|
|
TEST(PrintfTest, Bool) {
|
|
|
|
EXPECT_PRINTF("1", "%d", true);
|
|
|
|
EXPECT_PRINTF("true", "%s", true);
|
|
|
|
}
|
|
|
|
|
2014-08-19 19:49:10 +04:00
|
|
|
TEST(PrintfTest, Int) {
|
|
|
|
EXPECT_PRINTF("-42", "%d", -42);
|
|
|
|
EXPECT_PRINTF("-42", "%i", -42);
|
2016-03-19 16:39:33 +03:00
|
|
|
unsigned u = 0 - 42u;
|
2014-08-19 19:49:10 +04:00
|
|
|
EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
|
|
|
|
EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
|
|
|
|
EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
|
|
|
|
EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:52:52 +03:00
|
|
|
TEST(PrintfTest, long_long) {
|
2016-01-24 02:43:42 +03:00
|
|
|
// fmt::printf allows passing long long arguments to %d without length
|
|
|
|
// specifiers.
|
2019-09-08 19:04:09 +03:00
|
|
|
long long max = max_value<long long>();
|
2016-01-24 02:43:42 +03:00
|
|
|
EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:21:06 +04:00
|
|
|
TEST(PrintfTest, Float) {
|
|
|
|
EXPECT_PRINTF("392.650000", "%f", 392.65);
|
2018-01-28 04:56:19 +03:00
|
|
|
EXPECT_PRINTF("392.65", "%.2f", 392.65);
|
|
|
|
EXPECT_PRINTF("392.6", "%.1f", 392.65);
|
|
|
|
EXPECT_PRINTF("393", "%.f", 392.65);
|
2014-08-20 19:21:06 +04:00
|
|
|
EXPECT_PRINTF("392.650000", "%F", 392.65);
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
safe_sprintf(buffer, "%e", 392.65);
|
|
|
|
EXPECT_PRINTF(buffer, "%e", 392.65);
|
|
|
|
safe_sprintf(buffer, "%E", 392.65);
|
|
|
|
EXPECT_PRINTF(buffer, "%E", 392.65);
|
|
|
|
EXPECT_PRINTF("392.65", "%g", 392.65);
|
|
|
|
EXPECT_PRINTF("392.65", "%G", 392.65);
|
2019-05-10 23:37:19 +03:00
|
|
|
EXPECT_PRINTF("392", "%g", 392.0);
|
|
|
|
EXPECT_PRINTF("392", "%G", 392.0);
|
2014-08-21 18:14:02 +04:00
|
|
|
safe_sprintf(buffer, "%a", -392.65);
|
|
|
|
EXPECT_EQ(buffer, format("{:a}", -392.65));
|
|
|
|
safe_sprintf(buffer, "%A", -392.65);
|
|
|
|
EXPECT_EQ(buffer, format("{:A}", -392.65));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, Inf) {
|
|
|
|
double inf = std::numeric_limits<double>::infinity();
|
|
|
|
for (const char* type = "fega"; *type; ++type) {
|
|
|
|
EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
|
2018-06-06 16:57:59 +03:00
|
|
|
char upper = static_cast<char>(std::toupper(*type));
|
2014-08-21 18:14:02 +04:00
|
|
|
EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
|
|
|
|
}
|
2014-08-20 19:21:06 +04:00
|
|
|
}
|
|
|
|
|
2014-08-19 19:14:21 +04:00
|
|
|
TEST(PrintfTest, Char) {
|
|
|
|
EXPECT_PRINTF("x", "%c", 'x');
|
2019-09-08 19:04:09 +03:00
|
|
|
int max = max_value<int>();
|
2014-08-19 19:14:21 +04:00
|
|
|
EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
|
2019-01-13 05:27:38 +03:00
|
|
|
// EXPECT_PRINTF("x", "%lc", L'x');
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(L"x", L"%c", L'x');
|
|
|
|
EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
|
2014-06-29 02:58:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:21:06 +04:00
|
|
|
TEST(PrintfTest, String) {
|
2014-08-21 18:14:02 +04:00
|
|
|
EXPECT_PRINTF("abc", "%s", "abc");
|
2019-05-30 17:01:31 +03:00
|
|
|
const char* null_str = nullptr;
|
2015-11-11 18:57:19 +03:00
|
|
|
EXPECT_PRINTF("(null)", "%s", null_str);
|
2015-11-24 08:01:28 +03:00
|
|
|
EXPECT_PRINTF(" (null)", "%10s", null_str);
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(L"abc", L"%s", L"abc");
|
2019-05-30 17:01:31 +03:00
|
|
|
const wchar_t* null_wstr = nullptr;
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(L"(null)", L"%s", null_wstr);
|
|
|
|
EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
|
2014-08-20 19:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, Pointer) {
|
2014-08-21 18:14:02 +04:00
|
|
|
int n;
|
2019-01-13 05:27:38 +03:00
|
|
|
void* p = &n;
|
2014-08-21 18:14:02 +04:00
|
|
|
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
2019-05-30 17:01:31 +03:00
|
|
|
p = nullptr;
|
2015-11-11 18:57:19 +03:00
|
|
|
EXPECT_PRINTF("(nil)", "%p", p);
|
2015-11-24 08:01:28 +03:00
|
|
|
EXPECT_PRINTF(" (nil)", "%10p", p);
|
2019-01-13 05:27:38 +03:00
|
|
|
const char* s = "test";
|
2015-11-09 18:17:36 +03:00
|
|
|
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
|
2019-05-30 17:01:31 +03:00
|
|
|
const char* null_str = nullptr;
|
2015-11-12 17:09:08 +03:00
|
|
|
EXPECT_PRINTF("(nil)", "%p", null_str);
|
2018-04-26 21:32:14 +03:00
|
|
|
|
|
|
|
p = &n;
|
|
|
|
EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p);
|
2019-05-30 17:01:31 +03:00
|
|
|
p = nullptr;
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(L"(nil)", L"%p", p);
|
|
|
|
EXPECT_PRINTF(L" (nil)", L"%10p", p);
|
2019-01-13 05:27:38 +03:00
|
|
|
const wchar_t* w = L"test";
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w);
|
2019-05-30 17:01:31 +03:00
|
|
|
const wchar_t* null_wstr = nullptr;
|
2018-04-26 21:32:14 +03:00
|
|
|
EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
|
2014-08-20 19:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, Location) {
|
2014-08-21 18:14:02 +04:00
|
|
|
// TODO: test %n
|
2014-08-20 19:21:06 +04:00
|
|
|
}
|
2014-08-28 17:42:59 +04:00
|
|
|
|
2015-03-10 17:53:46 +03:00
|
|
|
enum E { A = 42 };
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
TEST(PrintfTest, Enum) { EXPECT_PRINTF("42", "%d", A); }
|
2015-03-10 17:53:46 +03:00
|
|
|
|
2014-08-28 17:42:59 +04:00
|
|
|
#if FMT_USE_FILE_DESCRIPTORS
|
|
|
|
TEST(PrintfTest, Examples) {
|
2019-01-13 05:27:38 +03:00
|
|
|
const char* weekday = "Thursday";
|
|
|
|
const char* month = "August";
|
2014-08-28 17:42:59 +04:00
|
|
|
int day = 21;
|
|
|
|
EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day),
|
|
|
|
"Thursday, 21 August");
|
|
|
|
}
|
2014-11-14 20:40:01 +03:00
|
|
|
|
|
|
|
TEST(PrintfTest, PrintfError) {
|
2018-05-19 20:32:53 +03:00
|
|
|
fmt::file read_end, write_end;
|
|
|
|
fmt::file::pipe(read_end, write_end);
|
2014-11-14 20:40:01 +03:00
|
|
|
int result = fmt::fprintf(read_end.fdopen("r").get(), "test");
|
|
|
|
EXPECT_LT(result, 0);
|
|
|
|
}
|
2014-08-28 17:42:59 +04:00
|
|
|
#endif
|
2015-09-19 02:26:41 +03:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
TEST(PrintfTest, WideString) { EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc")); }
|
2016-08-03 18:52:05 +03:00
|
|
|
|
|
|
|
TEST(PrintfTest, PrintfCustom) {
|
2018-12-06 17:04:45 +03:00
|
|
|
EXPECT_EQ("abc", test_sprintf("%s", TestString("abc")));
|
2016-08-03 18:52:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, OStream) {
|
|
|
|
std::ostringstream os;
|
|
|
|
int ret = fmt::fprintf(os, "Don't %s!", "panic");
|
|
|
|
EXPECT_EQ("Don't panic!", os.str());
|
|
|
|
EXPECT_EQ(12, ret);
|
|
|
|
}
|
2018-10-25 17:20:02 +03:00
|
|
|
|
|
|
|
TEST(PrintfTest, VPrintf) {
|
2018-11-23 00:57:07 +03:00
|
|
|
fmt::format_arg_store<fmt::printf_context, int> as{42};
|
|
|
|
fmt::basic_format_args<fmt::printf_context> args(as);
|
2018-10-25 17:20:02 +03:00
|
|
|
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
|
|
|
|
EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
|
|
|
|
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
|
|
|
|
EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42");
|
|
|
|
}
|
2018-11-04 02:20:45 +03:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
template <typename... Args>
|
2018-11-04 02:20:45 +03:00
|
|
|
void check_format_string_regression(fmt::string_view s, const Args&... args) {
|
|
|
|
fmt::sprintf(s, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, CheckFormatStringRegression) {
|
|
|
|
check_format_string_regression("%c%s", 'x', "");
|
|
|
|
}
|
2018-11-23 00:57:07 +03:00
|
|
|
|
2019-04-21 17:39:41 +03:00
|
|
|
TEST(PrintfTest, FixedLargeExponent) {
|
|
|
|
EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
|
|
|
|
}
|
|
|
|
|
2018-11-23 00:57:07 +03:00
|
|
|
TEST(PrintfTest, VSPrintfMakeArgsExample) {
|
2019-01-13 05:27:38 +03:00
|
|
|
fmt::format_arg_store<fmt::printf_context, int, const char*> as{42,
|
|
|
|
"something"};
|
2018-11-23 00:57:07 +03:00
|
|
|
fmt::basic_format_args<fmt::printf_context> args(as);
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ("[42] something happened", fmt::vsprintf("[%d] %s happened", args));
|
2018-11-23 00:57:07 +03:00
|
|
|
auto as2 = fmt::make_printf_args(42, "something");
|
|
|
|
fmt::basic_format_args<fmt::printf_context> args2(as2);
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ("[42] something happened",
|
|
|
|
fmt::vsprintf("[%d] %s happened", args2));
|
2019-07-08 02:43:38 +03:00
|
|
|
// The older gcc versions can't cast the return value.
|
2019-01-13 05:27:38 +03:00
|
|
|
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
|
|
|
EXPECT_EQ("[42] something happened",
|
|
|
|
fmt::vsprintf("[%d] %s happened",
|
2019-07-08 02:43:38 +03:00
|
|
|
{fmt::make_printf_args(42, "something")}));
|
2018-11-23 00:57:07 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PrintfTest, VSPrintfMakeWArgsExample) {
|
2019-01-13 05:27:38 +03:00
|
|
|
fmt::format_arg_store<fmt::wprintf_context, int, const wchar_t*> as{
|
|
|
|
42, L"something"};
|
2018-11-23 00:57:07 +03:00
|
|
|
fmt::basic_format_args<fmt::wprintf_context> args(as);
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ(L"[42] something happened",
|
|
|
|
fmt::vsprintf(L"[%d] %s happened", args));
|
|
|
|
auto as2 = fmt::make_wprintf_args(42, L"something");
|
2018-11-23 00:57:07 +03:00
|
|
|
fmt::basic_format_args<fmt::wprintf_context> args2(as2);
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ(L"[42] something happened",
|
|
|
|
fmt::vsprintf(L"[%d] %s happened", args2));
|
2018-11-23 00:57:07 +03:00
|
|
|
// the older gcc versions can't cast the return value
|
|
|
|
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
2019-01-13 05:27:38 +03:00
|
|
|
EXPECT_EQ(L"[42] something happened",
|
|
|
|
fmt::vsprintf(L"[%d] %s happened",
|
2019-07-08 02:43:38 +03:00
|
|
|
{fmt::make_wprintf_args(42, L"something")}));
|
2018-11-23 00:57:07 +03:00
|
|
|
#endif
|
|
|
|
}
|
2019-04-11 12:05:55 +03:00
|
|
|
|
2019-08-28 16:18:34 +03:00
|
|
|
typedef fmt::printf_arg_formatter<fmt::buffer_range<char>> formatter_t;
|
2019-04-11 12:05:55 +03:00
|
|
|
typedef fmt::basic_printf_context<formatter_t::iterator, char> context_t;
|
|
|
|
|
|
|
|
// A custom printf argument formatter that doesn't print `-` for floating-point
|
|
|
|
// values rounded to 0.
|
|
|
|
class custom_printf_arg_formatter : public formatter_t {
|
|
|
|
public:
|
2019-04-15 21:39:19 +03:00
|
|
|
using formatter_t::iterator;
|
2019-04-11 12:05:55 +03:00
|
|
|
|
2019-04-15 21:39:19 +03:00
|
|
|
custom_printf_arg_formatter(formatter_t::iterator iter,
|
2019-07-07 16:39:20 +03:00
|
|
|
formatter_t::format_specs& specs, context_t& ctx)
|
|
|
|
: formatter_t(iter, specs, ctx) {}
|
2019-04-11 12:05:55 +03:00
|
|
|
|
|
|
|
using formatter_t::operator();
|
|
|
|
|
|
|
|
#if FMT_MSC_VER > 0 && FMT_MSC_VER <= 1804
|
2019-04-15 21:39:19 +03:00
|
|
|
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
|
|
|
|
iterator operator()(T value){
|
2019-04-11 12:05:55 +03:00
|
|
|
#else
|
2019-04-15 21:39:19 +03:00
|
|
|
iterator operator()(double value) {
|
2019-04-11 12:05:55 +03:00
|
|
|
#endif
|
2019-04-15 21:39:19 +03:00
|
|
|
// Comparing a float to 0.0 is safe.
|
2019-07-07 16:39:20 +03:00
|
|
|
if (round(value * pow(10, specs()->precision)) == 0.0) value = 0;
|
2019-04-15 21:39:19 +03:00
|
|
|
return formatter_t::operator()(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2019-04-11 12:05:55 +03:00
|
|
|
|
|
|
|
typedef fmt::basic_format_args<context_t> format_args_t;
|
|
|
|
|
|
|
|
std::string custom_vformat(fmt::string_view format_str, format_args_t args) {
|
|
|
|
fmt::memory_buffer buffer;
|
|
|
|
fmt::vprintf<custom_printf_arg_formatter>(buffer, format_str, args);
|
|
|
|
return std::string(buffer.data(), buffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
std::string custom_format(const char* format_str, const Args&... args) {
|
2019-04-15 21:39:19 +03:00
|
|
|
auto va = fmt::make_printf_args(args...);
|
2019-07-08 02:43:38 +03:00
|
|
|
return custom_vformat(format_str, {va});
|
2019-04-11 12:05:55 +03:00
|
|
|
}
|
|
|
|
|
2019-04-21 17:39:41 +03:00
|
|
|
TEST(PrintfTest, CustomFormat) {
|
2019-04-11 12:05:55 +03:00
|
|
|
EXPECT_EQ("0.00", custom_format("%.2f", -.00001));
|
|
|
|
EXPECT_EQ("0.00", custom_format("%.2f", .00001));
|
|
|
|
EXPECT_EQ("1.00", custom_format("%.2f", 1.00001));
|
|
|
|
EXPECT_EQ("-1.00", custom_format("%.2f", -1.00001));
|
|
|
|
}
|