2018-11-27 13:52:00 +03:00
|
|
|
// Formatting library for C++ - formatting library tests
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
2019-07-25 19:01:21 +03:00
|
|
|
// For the license information refer to format.h.
|
2018-11-27 13:52:00 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
#include "fmt/compile.h"
|
2018-11-27 13:52:00 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
#include <type_traits>
|
2018-11-27 13:52:00 +03:00
|
|
|
|
2021-02-16 08:07:36 +03:00
|
|
|
#include "fmt/chrono.h"
|
2024-01-02 03:21:21 +03:00
|
|
|
#include "fmt/ranges.h"
|
2021-04-29 01:59:43 +03:00
|
|
|
#include "gmock/gmock.h"
|
2021-05-26 00:54:56 +03:00
|
|
|
#include "gtest-extra.h"
|
2018-11-27 13:52:00 +03:00
|
|
|
|
2021-05-01 03:02:14 +03:00
|
|
|
TEST(iterator_test, counting_iterator) {
|
|
|
|
auto it = fmt::detail::counting_iterator();
|
|
|
|
auto prev = it++;
|
|
|
|
EXPECT_EQ(prev.count(), 0);
|
|
|
|
EXPECT_EQ(it.count(), 1);
|
|
|
|
EXPECT_EQ((it + 41).count(), 42);
|
|
|
|
}
|
|
|
|
|
2021-05-30 03:46:12 +03:00
|
|
|
TEST(compile_test, compile_fallback) {
|
|
|
|
// FMT_COMPILE should fallback on runtime formatting when `if constexpr` is
|
|
|
|
// not available.
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
|
|
|
|
}
|
|
|
|
|
2021-10-09 16:43:48 +03:00
|
|
|
struct type_with_get {
|
|
|
|
template <int> friend void get(type_with_get);
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<type_with_get> : formatter<int> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(type_with_get, FormatContext& ctx) -> decltype(ctx.out()) {
|
|
|
|
return formatter<int>::format(42, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
|
|
|
TEST(compile_test, compile_type_with_get) {
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), type_with_get()));
|
|
|
|
}
|
|
|
|
|
2021-10-14 01:43:05 +03:00
|
|
|
#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
|
2020-07-14 21:13:21 +03:00
|
|
|
struct test_formattable {};
|
2019-09-01 21:48:01 +03:00
|
|
|
|
2019-09-01 22:12:19 +03:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2020-07-14 21:13:21 +03:00
|
|
|
template <> struct formatter<test_formattable> : formatter<const char*> {
|
2021-05-30 03:46:12 +03:00
|
|
|
char word_spec = 'f';
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
auto it = ctx.begin(), end = ctx.end();
|
|
|
|
if (it == end || *it == '}') return it;
|
|
|
|
if (it != end && (*it == 'f' || *it == 'b')) word_spec = *it++;
|
|
|
|
if (it != end && *it != '}') throw format_error("invalid format");
|
|
|
|
return it;
|
|
|
|
}
|
2020-06-23 22:05:37 +03:00
|
|
|
template <typename FormatContext>
|
2021-05-30 03:46:12 +03:00
|
|
|
constexpr auto format(test_formattable, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return formatter<const char*>::format(word_spec == 'f' ? "foo" : "bar",
|
|
|
|
ctx);
|
2019-09-01 21:48:01 +03:00
|
|
|
}
|
|
|
|
};
|
2019-09-01 22:12:19 +03:00
|
|
|
FMT_END_NAMESPACE
|
2019-09-01 21:48:01 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, format_default) {
|
2020-06-12 23:24:49 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
|
2020-06-14 17:16:50 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42u));
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ll));
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ull));
|
|
|
|
EXPECT_EQ("true", fmt::format(FMT_COMPILE("{}"), true));
|
|
|
|
EXPECT_EQ("x", fmt::format(FMT_COMPILE("{}"), 'x'));
|
|
|
|
EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2));
|
2020-06-12 23:24:49 +03:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
|
2020-06-14 17:16:50 +03:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
2020-07-14 21:13:21 +03:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
|
2021-06-11 20:52:39 +03:00
|
|
|
auto t = std::chrono::system_clock::now();
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), fmt::format(FMT_COMPILE("{}"), t));
|
2020-12-27 18:23:28 +03:00
|
|
|
# ifdef __cpp_lib_byte
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42}));
|
|
|
|
# endif
|
2020-06-23 22:05:37 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, format_wide_string) {
|
2020-09-17 16:35:33 +03:00
|
|
|
EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{}"), 42));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, format_specs) {
|
2020-06-23 22:05:37 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{:x}"), 0x42));
|
2021-02-16 08:07:36 +03:00
|
|
|
EXPECT_EQ("1.2 ms ",
|
|
|
|
fmt::format(FMT_COMPILE("{:7.1%Q %q}"),
|
|
|
|
std::chrono::duration<double, std::milli>(1.234)));
|
2020-06-12 23:24:49 +03:00
|
|
|
}
|
2020-06-14 21:04:41 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, dynamic_format_specs) {
|
2021-02-16 08:07:36 +03:00
|
|
|
EXPECT_EQ("foo ", fmt::format(FMT_COMPILE("{:{}}"), "foo", 5));
|
|
|
|
EXPECT_EQ(" 3.14", fmt::format(FMT_COMPILE("{:{}.{}f}"), 3.141592, 6, 2));
|
|
|
|
EXPECT_EQ(
|
|
|
|
"=1.234ms=",
|
|
|
|
fmt::format(FMT_COMPILE("{:=^{}.{}}"),
|
|
|
|
std::chrono::duration<double, std::milli>(1.234), 9, 3));
|
2021-02-07 23:43:52 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, manual_ordering) {
|
2021-02-07 23:43:52 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{0}"), 42));
|
|
|
|
EXPECT_EQ(" -42", fmt::format(FMT_COMPILE("{0:4}"), -42));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {1}"), 41, 43));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{1} {0}"), 43, 41));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {2}"), 41, 42, 43));
|
|
|
|
EXPECT_EQ(" 41 43", fmt::format(FMT_COMPILE("{1:{2}} {0:4}"), 43, 41, 4));
|
2021-02-16 08:07:36 +03:00
|
|
|
EXPECT_EQ("42 1.2 ms ",
|
|
|
|
fmt::format(FMT_COMPILE("{0} {1:7.1%Q %q}"), 42,
|
|
|
|
std::chrono::duration<double, std::milli>(1.234)));
|
2021-02-07 23:43:52 +03:00
|
|
|
EXPECT_EQ(
|
|
|
|
"true 42 42 foo 0x1234 foo",
|
|
|
|
fmt::format(FMT_COMPILE("{0} {1} {2} {3} {4} {5}"), true, 42, 42.0f,
|
|
|
|
"foo", reinterpret_cast<void*>(0x1234), test_formattable()));
|
|
|
|
EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{0}"), 42));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, named) {
|
2021-04-18 13:59:24 +03:00
|
|
|
auto runtime_named_field_compiled =
|
|
|
|
fmt::detail::compile<decltype(fmt::arg("arg", 42))>(FMT_COMPILE("{arg}"));
|
|
|
|
static_assert(std::is_same_v<decltype(runtime_named_field_compiled),
|
|
|
|
fmt::detail::runtime_named_field<char>>);
|
|
|
|
|
2021-02-16 07:04:52 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), fmt::arg("arg", 42)));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{} {}"), fmt::arg("arg", 41),
|
|
|
|
fmt::arg("arg", 43)));
|
|
|
|
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
2021-02-16 07:16:57 +03:00
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foofoo", fmt::format(FMT_COMPILE("{a0}{}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{0}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{a0}{1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
2021-02-16 07:04:52 +03:00
|
|
|
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{}{a1}"), "foo", fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a1", "bar"),
|
|
|
|
fmt::arg("a2", "baz"), fmt::arg("a0", "foo")));
|
|
|
|
EXPECT_EQ(" bar foo ",
|
|
|
|
fmt::format(FMT_COMPILE(" {foo} {bar} "), fmt::arg("foo", "bar"),
|
|
|
|
fmt::arg("bar", "foo")));
|
2021-02-07 23:43:52 +03:00
|
|
|
|
|
|
|
EXPECT_THROW(fmt::format(FMT_COMPILE("{invalid}"), fmt::arg("valid", 42)),
|
|
|
|
fmt::format_error);
|
2021-04-18 13:59:24 +03:00
|
|
|
|
2022-05-30 03:00:09 +03:00
|
|
|
# if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
2021-04-18 13:59:24 +03:00
|
|
|
using namespace fmt::literals;
|
|
|
|
auto statically_named_field_compiled =
|
|
|
|
fmt::detail::compile<decltype("arg"_a = 42)>(FMT_COMPILE("{arg}"));
|
|
|
|
static_assert(std::is_same_v<decltype(statically_named_field_compiled),
|
|
|
|
fmt::detail::field<char, int, 0>>);
|
|
|
|
|
|
|
|
EXPECT_EQ("41 43",
|
|
|
|
fmt::format(FMT_COMPILE("{a0} {a1}"), "a0"_a = 41, "a1"_a = 43));
|
|
|
|
EXPECT_EQ("41 43",
|
|
|
|
fmt::format(FMT_COMPILE("{a1} {a0}"), "a0"_a = 43, "a1"_a = 41));
|
|
|
|
# endif
|
2020-08-10 19:13:13 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 20:58:49 +03:00
|
|
|
TEST(compile_test, join) {
|
|
|
|
unsigned char data[] = {0x1, 0x2, 0xaf};
|
|
|
|
EXPECT_EQ("0102af", fmt::format(FMT_COMPILE("{:02x}"), fmt::join(data, "")));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, format_to) {
|
2020-09-20 16:59:01 +03:00
|
|
|
char buf[8];
|
|
|
|
auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42);
|
|
|
|
*end = '\0';
|
|
|
|
EXPECT_STREQ("42", buf);
|
|
|
|
end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42);
|
|
|
|
*end = '\0';
|
|
|
|
EXPECT_STREQ("2a", buf);
|
2020-06-20 18:50:02 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, format_to_n) {
|
2020-09-20 16:59:01 +03:00
|
|
|
constexpr auto buffer_size = 8;
|
|
|
|
char buffer[buffer_size];
|
|
|
|
auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42);
|
|
|
|
*res.out = '\0';
|
|
|
|
EXPECT_STREQ("42", buffer);
|
|
|
|
res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42);
|
|
|
|
*res.out = '\0';
|
|
|
|
EXPECT_STREQ("2a", buffer);
|
2020-09-15 17:28:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-23 03:06:37 +03:00
|
|
|
# ifdef __cpp_lib_bit_cast
|
2022-08-10 19:35:30 +03:00
|
|
|
TEST(compile_test, constexpr_formatted_size) {
|
2023-05-23 03:06:37 +03:00
|
|
|
FMT_CONSTEXPR20 size_t size = fmt::formatted_size(FMT_COMPILE("{}"), 42);
|
|
|
|
EXPECT_EQ(size, 2);
|
2023-08-15 16:45:48 +03:00
|
|
|
FMT_CONSTEXPR20 size_t hex_size =
|
|
|
|
fmt::formatted_size(FMT_COMPILE("{:x}"), 15);
|
|
|
|
EXPECT_EQ(hex_size, 1);
|
|
|
|
FMT_CONSTEXPR20 size_t binary_size =
|
|
|
|
fmt::formatted_size(FMT_COMPILE("{:b}"), 15);
|
|
|
|
EXPECT_EQ(binary_size, 4);
|
|
|
|
FMT_CONSTEXPR20 size_t padded_size =
|
|
|
|
fmt::formatted_size(FMT_COMPILE("{:*^6}"), 42);
|
|
|
|
EXPECT_EQ(padded_size, 6);
|
|
|
|
FMT_CONSTEXPR20 size_t float_size =
|
|
|
|
fmt::formatted_size(FMT_COMPILE("{:.3}"), 12.345);
|
|
|
|
EXPECT_EQ(float_size, 4);
|
|
|
|
FMT_CONSTEXPR20 size_t str_size =
|
|
|
|
fmt::formatted_size(FMT_COMPILE("{:s}"), "abc");
|
|
|
|
EXPECT_EQ(str_size, 3);
|
2021-03-07 17:44:36 +03:00
|
|
|
}
|
2023-05-23 03:06:37 +03:00
|
|
|
# endif
|
2021-03-07 17:44:36 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, text_and_arg) {
|
2020-06-14 21:04:41 +03:00
|
|
|
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
2020-07-11 18:35:26 +03:00
|
|
|
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
|
2020-06-14 21:04:41 +03:00
|
|
|
}
|
2020-12-02 18:14:57 +03:00
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, unknown_format_fallback) {
|
2021-03-04 18:20:57 +03:00
|
|
|
EXPECT_EQ(" 42 ",
|
|
|
|
fmt::format(FMT_COMPILE("{name:^4}"), fmt::arg("name", 42)));
|
|
|
|
|
|
|
|
std::vector<char> v;
|
|
|
|
fmt::format_to(std::back_inserter(v), FMT_COMPILE("{name:^4}"),
|
|
|
|
fmt::arg("name", 42));
|
|
|
|
EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size()));
|
|
|
|
|
|
|
|
char buffer[4];
|
|
|
|
auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"),
|
|
|
|
fmt::arg("name", 42));
|
|
|
|
EXPECT_EQ(5u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 4, result.out);
|
|
|
|
EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); }
|
2021-03-19 16:43:38 +03:00
|
|
|
|
|
|
|
struct to_stringable {
|
|
|
|
friend fmt::string_view to_string_view(to_stringable) { return {}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<to_stringable> {
|
|
|
|
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) {
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, to_string_and_formatter) {
|
2021-03-19 16:43:38 +03:00
|
|
|
fmt::format(FMT_COMPILE("{}"), to_stringable());
|
|
|
|
}
|
2021-05-26 00:54:56 +03:00
|
|
|
|
|
|
|
TEST(compile_test, print) {
|
|
|
|
EXPECT_WRITE(stdout, fmt::print(FMT_COMPILE("Don't {}!"), "panic"),
|
|
|
|
"Don't panic!");
|
|
|
|
EXPECT_WRITE(stderr, fmt::print(stderr, FMT_COMPILE("Don't {}!"), "panic"),
|
|
|
|
"Don't panic!");
|
|
|
|
}
|
2020-06-12 23:24:49 +03:00
|
|
|
#endif
|
2020-11-29 19:59:11 +03:00
|
|
|
|
2022-05-30 03:00:09 +03:00
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_test, compile_format_string_literal) {
|
2020-12-08 02:53:11 +03:00
|
|
|
using namespace fmt::literals;
|
|
|
|
EXPECT_EQ("", fmt::format(""_cf));
|
|
|
|
EXPECT_EQ("42", fmt::format("{}"_cf, 42));
|
2021-04-19 18:29:35 +03:00
|
|
|
EXPECT_EQ(L"42", fmt::format(L"{}"_cf, 42));
|
2020-12-08 02:53:11 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-03 13:56:56 +03:00
|
|
|
// MSVS 2019 19.29.30145.0 - Support C++20 and OK.
|
2023-12-17 19:33:17 +03:00
|
|
|
// MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error
|
|
|
|
// C1001: Internal compiler error.
|
2022-07-03 13:56:56 +03:00
|
|
|
// (compiler file
|
|
|
|
// 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp',
|
|
|
|
// line 8635)
|
2023-12-17 19:33:17 +03:00
|
|
|
#if (FMT_CPLUSPLUS >= 202002L || \
|
|
|
|
(FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)) && \
|
|
|
|
((!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 10) && \
|
|
|
|
(!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 10000) && \
|
|
|
|
(!FMT_MSC_VERSION || \
|
|
|
|
(FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930))) && \
|
|
|
|
defined(__cpp_lib_is_constant_evaluated)
|
2020-12-25 17:40:03 +03:00
|
|
|
template <size_t max_string_length, typename Char = char> struct test_string {
|
2020-11-29 19:59:11 +03:00
|
|
|
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
|
2021-04-08 03:25:40 +03:00
|
|
|
return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
|
2020-11-29 19:59:11 +03:00
|
|
|
}
|
2021-04-08 03:25:40 +03:00
|
|
|
Char buffer[max_string_length]{};
|
2020-11-29 19:59:11 +03:00
|
|
|
};
|
|
|
|
|
2020-12-25 17:40:03 +03:00
|
|
|
template <size_t max_string_length, typename Char = char, typename... Args>
|
2020-11-29 19:59:11 +03:00
|
|
|
consteval auto test_format(auto format, const Args&... args) {
|
2020-12-25 17:40:03 +03:00
|
|
|
test_string<max_string_length, Char> string{};
|
2021-04-08 03:25:40 +03:00
|
|
|
fmt::format_to(string.buffer, format, args...);
|
2020-11-29 19:59:11 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, bool) {
|
2020-12-04 01:21:23 +03:00
|
|
|
EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true));
|
|
|
|
EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false));
|
2020-12-25 17:40:03 +03:00
|
|
|
EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true));
|
|
|
|
EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true));
|
2020-11-29 19:59:11 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, integer) {
|
2020-12-04 01:21:23 +03:00
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), 42));
|
|
|
|
EXPECT_EQ("420", test_format<4>(FMT_COMPILE("{}"), 420));
|
|
|
|
EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42));
|
|
|
|
EXPECT_EQ("42 42",
|
|
|
|
test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42}));
|
2020-12-25 17:40:03 +03:00
|
|
|
|
|
|
|
EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42));
|
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42));
|
|
|
|
|
|
|
|
EXPECT_EQ("-0042", test_format<6>(FMT_COMPILE("{:05}"), -42));
|
|
|
|
|
|
|
|
EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42));
|
|
|
|
EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42));
|
|
|
|
EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42));
|
|
|
|
EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042));
|
|
|
|
EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a));
|
|
|
|
EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a));
|
|
|
|
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll));
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull));
|
|
|
|
|
|
|
|
EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42));
|
|
|
|
EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42));
|
|
|
|
EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, char) {
|
2020-12-25 17:40:03 +03:00
|
|
|
EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c'));
|
|
|
|
|
|
|
|
EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c'));
|
|
|
|
EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c'));
|
2020-11-29 19:59:11 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, string) {
|
2020-12-04 01:21:23 +03:00
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42"));
|
|
|
|
EXPECT_EQ("The answer is 42",
|
|
|
|
test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42"));
|
2020-12-25 17:40:03 +03:00
|
|
|
|
|
|
|
EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc"));
|
2021-02-13 20:24:39 +03:00
|
|
|
EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡"));
|
2020-11-29 19:59:11 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, combination) {
|
2020-12-04 01:21:23 +03:00
|
|
|
EXPECT_EQ("420, true, answer",
|
|
|
|
test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer"));
|
2020-12-25 17:40:03 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4));
|
|
|
|
}
|
|
|
|
|
2021-05-30 03:46:12 +03:00
|
|
|
TEST(compile_time_formatting_test, custom_type) {
|
|
|
|
EXPECT_EQ("foo", test_format<4>(FMT_COMPILE("{}"), test_formattable()));
|
|
|
|
EXPECT_EQ("bar", test_format<4>(FMT_COMPILE("{:b}"), test_formattable()));
|
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:59 +03:00
|
|
|
TEST(compile_time_formatting_test, multibyte_fill) {
|
2020-12-25 17:40:03 +03:00
|
|
|
EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42));
|
2020-11-29 19:59:11 +03:00
|
|
|
}
|
|
|
|
#endif
|