mirror of
https://github.com/fmtlib/fmt.git
synced 2026-05-05 12:04:11 +02:00
FMT_EXPLICIT -> explicit, FMT_NULL -> nullptr
This commit is contained in:
+45
-40
@@ -3,36 +3,36 @@
|
||||
|
||||
TEST(StdFormatTest, Escaping) {
|
||||
using namespace std;
|
||||
string s = format("{0}-{{", 8); // s == "8-{"
|
||||
string s = format("{0}-{{", 8); // s == "8-{"
|
||||
EXPECT_EQ(s, "8-{");
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Indexing) {
|
||||
using namespace std;
|
||||
string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing
|
||||
string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing
|
||||
string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing
|
||||
string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing
|
||||
EXPECT_EQ(s0, "a to b");
|
||||
EXPECT_EQ(s1, "b to a");
|
||||
// Error: mixing automatic and manual indexing
|
||||
EXPECT_THROW(string s2 = format("{0} to {}", "a", "b"), std::format_error);
|
||||
// Error: mixing automatic and manual indexing
|
||||
EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error);
|
||||
// Error: mixing automatic and manual indexing
|
||||
EXPECT_THROW(string s2 = format("{0} to {}", "a", "b"), std::format_error);
|
||||
// Error: mixing automatic and manual indexing
|
||||
EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error);
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Alignment) {
|
||||
using namespace std;
|
||||
char c = 120;
|
||||
string s0 = format("{:6}", 42); // s0 == " 42"
|
||||
string s1 = format("{:6}", 'x'); // s1 == "x "
|
||||
string s2 = format("{:*<6}", 'x'); // s2 == "x*****"
|
||||
string s3 = format("{:*>6}", 'x'); // s3 == "*****x"
|
||||
string s4 = format("{:*^6}", 'x'); // s4 == "**x***"
|
||||
string s0 = format("{:6}", 42); // s0 == " 42"
|
||||
string s1 = format("{:6}", 'x'); // s1 == "x "
|
||||
string s2 = format("{:*<6}", 'x'); // s2 == "x*****"
|
||||
string s3 = format("{:*>6}", 'x'); // s3 == "*****x"
|
||||
string s4 = format("{:*^6}", 'x'); // s4 == "**x***"
|
||||
// Error: '=' with charT and no integer presentation type
|
||||
EXPECT_THROW(string s5 = format("{:=6}", 'x'), std::format_error);
|
||||
string s6 = format("{:6d}", c); // s6 == " 120"
|
||||
string s7 = format("{:=+06d}", c); // s7 == "+00120"
|
||||
string s8 = format("{:0=#6x}", 0xa); // s8 == "0x000a"
|
||||
string s9 = format("{:6}", true); // s9 == "true "
|
||||
string s6 = format("{:6d}", c); // s6 == " 120"
|
||||
string s7 = format("{:=+06d}", c); // s7 == "+00120"
|
||||
string s8 = format("{:0=#6x}", 0xa); // s8 == "0x000a"
|
||||
string s9 = format("{:6}", true); // s9 == "true "
|
||||
EXPECT_EQ(s0, " 42");
|
||||
EXPECT_EQ(s1, "x ");
|
||||
EXPECT_EQ(s2, "x*****");
|
||||
@@ -50,8 +50,10 @@ TEST(StdFormatTest, Float) {
|
||||
double nan = numeric_limits<double>::quiet_NaN();
|
||||
string s0 = format("{0:} {0:+} {0:-} {0: }", 1); // s0 == "1 +1 1 1"
|
||||
string s1 = format("{0:} {0:+} {0:-} {0: }", -1); // s1 == "-1 -1 -1 -1"
|
||||
string s2 = format("{0:} {0:+} {0:-} {0: }", inf); // s2 == "inf +inf inf inf"
|
||||
string s3 = format("{0:} {0:+} {0:-} {0: }", nan); // s3 == "nan +nan nan nan"
|
||||
string s2 =
|
||||
format("{0:} {0:+} {0:-} {0: }", inf); // s2 == "inf +inf inf inf"
|
||||
string s3 =
|
||||
format("{0:} {0:+} {0:-} {0: }", nan); // s3 == "nan +nan nan nan"
|
||||
EXPECT_EQ(s0, "1 +1 1 1");
|
||||
EXPECT_EQ(s1, "-1 -1 -1 -1");
|
||||
EXPECT_EQ(s2, "inf +inf inf inf");
|
||||
@@ -60,10 +62,10 @@ TEST(StdFormatTest, Float) {
|
||||
|
||||
TEST(StdFormatTest, Int) {
|
||||
using namespace std;
|
||||
string s0 = format("{}", 42); // s0 == "42"
|
||||
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a"
|
||||
string s2 = format("{0:#x} {0:#X}", 42); // s2 == "0x2a 0X2A"
|
||||
string s3 = format("{:n}", 1234); // s3 == "1,234" (depends on the locale)
|
||||
string s0 = format("{}", 42); // s0 == "42"
|
||||
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a"
|
||||
string s2 = format("{0:#x} {0:#X}", 42); // s2 == "0x2a 0X2A"
|
||||
string s3 = format("{:n}", 1234); // s3 == "1,234" (depends on the locale)
|
||||
EXPECT_EQ(s0, "42");
|
||||
EXPECT_EQ(s1, "101010 42 52 2a");
|
||||
EXPECT_EQ(s2, "0x2a 0X2A");
|
||||
@@ -74,9 +76,9 @@ TEST(StdFormatTest, Int) {
|
||||
|
||||
enum color { red, green, blue };
|
||||
|
||||
const char* color_names[] = { "red", "green", "blue" };
|
||||
const char* color_names[] = {"red", "green", "blue"};
|
||||
|
||||
template<> struct std::formatter<color> : std::formatter<const char*> {
|
||||
template <> struct std::formatter<color> : std::formatter<const char*> {
|
||||
auto format(color c, format_context& ctx) {
|
||||
return formatter<const char*>::format(color_names[c], ctx);
|
||||
}
|
||||
@@ -85,10 +87,12 @@ template<> struct std::formatter<color> : std::formatter<const char*> {
|
||||
struct err {};
|
||||
|
||||
TEST(StdFormatTest, Formatter) {
|
||||
std::string s0 = std::format("{}", 42); // OK: library-provided formatter
|
||||
//std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled formatter
|
||||
std::string s2 = std::format("{}", red); // OK: user-provided formatter
|
||||
//std::string s3 = std::format("{}", err{}); // Ill-formed: disabled formatter
|
||||
std::string s0 = std::format("{}", 42); // OK: library-provided formatter
|
||||
// std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled
|
||||
// formatter
|
||||
std::string s2 = std::format("{}", red); // OK: user-provided formatter
|
||||
// std::string s3 = std::format("{}", err{}); // Ill-formed: disabled
|
||||
// formatter
|
||||
EXPECT_EQ(s0, "42");
|
||||
EXPECT_EQ(s2, "red");
|
||||
}
|
||||
@@ -97,15 +101,14 @@ struct S {
|
||||
int value;
|
||||
};
|
||||
|
||||
template<> struct std::formatter<S> {
|
||||
template <> struct std::formatter<S> {
|
||||
size_t width_arg_id = 0;
|
||||
|
||||
// Parses a width argument id in the format { <digit> }.
|
||||
constexpr auto parse(format_parse_context& ctx) {
|
||||
auto iter = ctx.begin();
|
||||
auto get_char = [&]() { return iter != ctx.end() ? *iter : 0; };
|
||||
if (get_char() != '{')
|
||||
return iter;
|
||||
if (get_char() != '{') return iter;
|
||||
++iter;
|
||||
char c = get_char();
|
||||
if (!isdigit(c) || (++iter, get_char()) != '}')
|
||||
@@ -117,20 +120,22 @@ template<> struct std::formatter<S> {
|
||||
|
||||
// Formats S with width given by the argument width_arg_id.
|
||||
auto format(S s, format_context& ctx) {
|
||||
int width = visit_format_arg([](auto value) -> int {
|
||||
if constexpr (!is_integral_v<decltype(value)>)
|
||||
throw format_error("width is not integral");
|
||||
else if (value < 0 || value > numeric_limits<int>::max())
|
||||
throw format_error("invalid width");
|
||||
else
|
||||
return value;
|
||||
}, ctx.arg(width_arg_id));
|
||||
int width = visit_format_arg(
|
||||
[](auto value) -> int {
|
||||
if constexpr (!is_integral_v<decltype(value)>)
|
||||
throw format_error("width is not integral");
|
||||
else if (value < 0 || value > numeric_limits<int>::max())
|
||||
throw format_error("invalid width");
|
||||
else
|
||||
return value;
|
||||
},
|
||||
ctx.arg(width_arg_id));
|
||||
return format_to(ctx.out(), "{0:{1}}", s.value, width);
|
||||
}
|
||||
};
|
||||
|
||||
TEST(StdFormatTest, Parsing) {
|
||||
std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42"
|
||||
std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42"
|
||||
EXPECT_EQ(s, " 42");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user