Writer::format -> Writer::write. Make fmt::format return string.

This commit is contained in:
Victor Zverovich 2014-06-29 11:51:10 -07:00
parent d5b8196749
commit a1264926a0
7 changed files with 443 additions and 464 deletions

View File

@ -1107,13 +1107,13 @@ void fmt::ANSITerminalSink::operator()(
void fmt::print(StringRef format, const ArgList &args) {
Writer w;
w.format(format, args);
w.write(format, args);
std::fwrite(w.data(), 1, w.size(), stdout);
}
void fmt::print(std::FILE *f, StringRef format, const ArgList &args) {
Writer w;
w.format(format, args);
w.write(format, args);
std::fwrite(w.data(), 1, w.size(), f);
}

195
format.h
View File

@ -119,7 +119,9 @@
TypeName(const TypeName&); \
void operator=(const TypeName&)
#ifdef __GNUC__
#ifdef FMT_DEPRECATED
// Do nothing.
#elif defined(__GNUC__)
# define FMT_DEPRECATED(func) func __attribute__((deprecated))
#elif defined(_MSC_VER)
# define FMT_DEPRECATED(func) __declspec(deprecated) func
@ -157,8 +159,20 @@ struct FormatSpec;
/**
\rst
A string reference. It can be constructed from a C string or
``std::string``. It is most useful as a parameter type to allow
passing different types of strings in a function, for example::
``std::string``.
You can use one of the following typedefs for common character types:
+------------+-------------------------+
| Type | Definition |
+============+=========================+
| StringRef | BasicStringRef<char> |
+------------+-------------------------+
| WStringRef | BasicStringRef<wchar_t> |
+------------+-------------------------+
This class is most useful as a parameter type to allow passing
different types of strings to a function, for example::
template<typename... Args>
Writer format(StringRef format, const Args & ... args);
@ -968,7 +982,7 @@ public:
Writer out;
out << "The answer is " << 42 << "\n";
out.format("({:+f}, {:+f})", -3.14, 3.14);
out.write("({:+f}, {:+f})", -3.14, 3.14);
This will write the following output to the ``out`` object:
@ -1229,38 +1243,6 @@ class BasicWriter {
buffer_ = std::move(other.buffer_);
return *this;
}
#else
friend inline BasicWriter &move(BasicWriter &w) { return w; }
private:
struct Proxy { Buffer *buffer; };
public:
operator Proxy() {
Proxy p = {&buffer_};
return p;
}
/**
\rst
A "move" constructor. Constructs a writer transferring the buffer
from other to this object. This constructor is used to return a
writer object from a formatting function since the copy constructor
taking a const reference is disabled to prevent misuse of the API.
It is not implemented as a move constructor for compatibility with
pre-C++11 compilers, but should be treated as such.
**Example**::
fmt::Writer format(fmt::StringRef format, const fmt::ArgList &args) {
fmt::Writer w;
w.format(format, args);
return move(w);
}
\endrst
*/
BasicWriter(BasicWriter &other) { buffer_.move(other.buffer_); }
BasicWriter(Proxy p) { buffer_.move(*p.buffer); }
#endif
/**
@ -1294,14 +1276,15 @@ class BasicWriter {
/**
\rst
Formats a string sending the output to the writer. This function
takes variable number of arguments.
Writes formatted data.
*args* is an argument list representing arbitrary arguments.
**Example**::
Writer out;
out.format("Current point:\n");
out.format("({:+f}, {:+f})", -3.14, 3.14);
out.write("Current point:\n");
out.write("({:+f}, {:+f})", -3.14, 3.14);
This will write the following output to the ``out`` object:
@ -1316,24 +1299,16 @@ class BasicWriter {
See also `Format String Syntax`_.
\endrst
*/
inline void format(BasicStringRef<Char> format, const ArgList &args) {
inline void write(BasicStringRef<Char> format, const ArgList &args) {
FormatParser().Format(*this, format, args);
}
FMT_VARIADIC_VOID(format, fmt::BasicStringRef<Char>)
FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>)
inline void printf(BasicStringRef<Char> format, const ArgList &args) {
PrintfParser().Format(*this, format, args);
}
FMT_VARIADIC_VOID(printf, fmt::BasicStringRef<Char>)
FMT_DEPRECATED(BasicFormatter<Char> Format(StringRef format));
#if FMT_USE_VARIADIC_TEMPLATES
// This function is deprecated, use Writer::format instead.
template<typename... Args>
FMT_DEPRECATED(void Format(BasicStringRef<Char> format, const Args & ... args));
#endif
template <typename T>
static Arg MakeArg(const T &arg) { return BasicArg<>(arg); }
@ -1415,13 +1390,23 @@ class BasicWriter {
FormatString(s.data(), s.size(), spec);
}
// This function is deprecated, use write instead.
FMT_DEPRECATED(void Write(const std::basic_string<Char> &s, const FormatSpec &spec));
void clear() { buffer_.clear(); }
// This function is deprecated, use clear instead.
#if !defined(FMT_NO_DEPRECATED)
FMT_DEPRECATED(BasicFormatter<Char> Format(StringRef format));
#if FMT_USE_VARIADIC_TEMPLATES
// This function is deprecated. Use Writer::write instead.
template<typename... Args>
FMT_DEPRECATED(void Format(BasicStringRef<Char> format, const Args & ... args));
#endif
// This function is deprecated. Use Writer::write instead.
FMT_DEPRECATED(void Write(const std::basic_string<Char> &s, const FormatSpec &spec));
// This function is deprecated. Use Writer::clear instead.
FMT_DEPRECATED(void Clear());
#endif
};
template <typename Char>
@ -1685,7 +1670,7 @@ class BasicFormatter {
if (!format_) return;
const Char *format = format_;
format_ = 0;
writer_->format(format, ArgList(&args_[0], args_.size()));
writer_->write(format, ArgList(&args_[0], args_.size()));
}
public:
@ -1783,14 +1768,15 @@ class Formatter : private Sink, public BasicFormatter<Char> {
}
};
// This function is deprecated, use format instead.
#if !defined(FMT_NO_DEPRECATED)
// This function is deprecated. Use fmt::format instead.
FMT_DEPRECATED(Formatter<> Format(StringRef format));
inline Formatter<> Format(StringRef format) {
Formatter<> f(format);
return f;
}
// This function is deprecated, use format instead.
// This function is deprecated. Use fmt::format instead.
Formatter<NullSink, wchar_t> FMT_DEPRECATED(Format(WStringRef format));
inline Formatter<NullSink, wchar_t> Format(WStringRef format) {
Formatter<NullSink, wchar_t> f(format);
@ -1807,6 +1793,7 @@ class SystemErrorSink {
void operator()(const Writer &w) const;
};
#endif
/**
\rst
@ -1876,29 +1863,11 @@ class FileSink {
}
};
/**
\rst
Formats a string and writes the result to ``stdout``.
**Example**::
Print("Elapsed time: {0:.2f} seconds") << 1.23;
\endrst
*/
inline Formatter<FileSink> Print(StringRef format) {
Formatter<FileSink> f(format, FileSink(stdout));
return f;
}
/**
\rst
Formats a string and writes the result to a file.
**Example**::
Print(stderr, "Don't {}!") << "panic";
\endrst
*/
inline Formatter<FileSink> Print(std::FILE *file, StringRef format) {
Formatter<FileSink> f(format, FileSink(file));
return f;
@ -1933,46 +1902,64 @@ inline Formatter<ANSITerminalSink> PrintColored(Color c, StringRef format) {
\rst
Formats a string similarly to Python's `str.format
<http://docs.python.org/3/library/stdtypes.html#str.format>`__ function
and returns an :cpp:class:`fmt::BasicWriter` object containing the output.
and returns the result as a string.
*format* is a format string that contains literal text and replacement
fields surrounded by braces ``{}``. The formatter object replaces the
fields with formatted arguments and stores the output in a memory buffer.
The content of the buffer can be converted to ``std::string`` with
:cpp:func:`fmt::str()` or accessed as a C string with
:cpp:func:`fmt::c_str()`.
*format_str* is a format string that contains literal text and replacement
fields surrounded by braces ``{}``. The fields are replaced with formatted
arguments in the resulting string.
*args* is an argument list representing arbitrary arguments.
**Example**::
std::string message = str(format("The answer is {}", 42));
std::string message = format("The answer is {}", 42);
See also `Format String Syntax`_.
\endrst
*/
inline Writer format(StringRef format, const ArgList &args) {
inline std::string format(StringRef format_str, const ArgList &args) {
Writer w;
w.format(format, args);
return move(w);
w.write(format_str, args);
return w.str();
}
inline WWriter format(WStringRef format, const ArgList &args) {
inline std::wstring format(WStringRef format, const ArgList &args) {
WWriter w;
w.format(format, args);
return move(w);
w.write(format, args);
return w.str();
}
/**
\rst
Prints formatted data to ``stdout``.
**Example**::
print("Elapsed time: {0:.2f} seconds", 1.23);
\endrst
*/
void print(StringRef format, const ArgList &args);
/**
\rst
Prints formatted data to a file.
**Example**::
print(stderr, "Don't {}!", "panic");
\endrst
*/
void print(std::FILE *f, StringRef format, const ArgList &args);
inline Writer sprintf(StringRef format, const ArgList &args) {
inline std::string sprintf(StringRef format, const ArgList &args) {
Writer w;
w.printf(format, args);
return move(w);
return w.str();
}
void printf(StringRef format, const ArgList &args);
#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
#if !defined(FMT_NO_DEPRECATED) && FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
template <typename Char>
template<typename... Args>
@ -1981,7 +1968,7 @@ void BasicWriter<Char>::Format(
this->format(format, args...);
}
// This function is deprecated, use fmt::format instead.
// This function is deprecated. Use fmt::format instead.
template<typename... Args>
FMT_DEPRECATED(Writer Format(StringRef format, const Args & ... args));
@ -1992,7 +1979,7 @@ inline Writer Format(StringRef format, const Args & ... args) {
return std::move(w);
}
// This function is deprecated, use fmt::format instead.
// This function is deprecated. Use fmt::format instead.
template<typename... Args>
FMT_DEPRECATED(WWriter Format(WStringRef format, const Args & ... args));
@ -2003,18 +1990,18 @@ inline WWriter Format(WStringRef format, const Args & ... args) {
return std::move(w);
}
// This function is deprecated, use fmt::print instead.
// This function is deprecated. Use fmt::print instead.
template<typename... Args>
FMT_DEPRECATED(void Print(StringRef format, const Args & ... args));
template<typename... Args>
void Print(StringRef format, const Args & ... args) {
Writer w;
w.format(format, args...);
w.write(format, args...);
std::fwrite(w.data(), 1, w.size(), stdout);
}
// This function is deprecated, use fmt::print instead.
// This function is deprecated. Use fmt::print instead.
template<typename... Args>
FMT_DEPRECATED(void Print(std::FILE *f, StringRef format, const Args & ... args));
@ -2202,10 +2189,11 @@ inline void FormatDec(char *&buffer, T value) {
#endif // FMT_USE_VARIADIC_TEMPLATES
/**
Defines a variadic function with the specified return type and argument
types passed as variable arguments.
\rst
Defines a variadic function with the specified return type, function name
and argument types passed as variable arguments to this macro.
Example::
**Example**::
std::string FormatMessage(int id, const char *format,
const fmt::ArgList &args) {
@ -2215,6 +2203,7 @@ inline void FormatDec(char *&buffer, T value) {
return w.str();
}
FMT_VARIADIC(std::string, FormatMessage, int, const char *)
\endrst
*/
#define FMT_VARIADIC(ReturnType, func, ...) \
FMT_VARIADIC_(char, ReturnType, func, __VA_ARGS__)
@ -2223,11 +2212,11 @@ inline void FormatDec(char *&buffer, T value) {
FMT_VARIADIC_(wchar_t, ReturnType, func, __VA_ARGS__)
namespace fmt {
FMT_VARIADIC(Writer, format, StringRef)
FMT_VARIADIC_W(WWriter, format, WStringRef)
FMT_VARIADIC(std::string, format, StringRef)
FMT_VARIADIC_W(std::wstring, format, WStringRef)
FMT_VARIADIC(void, print, StringRef)
FMT_VARIADIC(void, print, std::FILE *, StringRef)
FMT_VARIADIC(Writer, sprintf, StringRef)
FMT_VARIADIC(std::string, sprintf, StringRef)
FMT_VARIADIC(void, printf, StringRef)
}

View File

@ -551,12 +551,12 @@ TEST(WriterTest, NoConflictWithIOManip) {
TEST(WriterTest, Format) {
Writer w;
w.format("part{0}", 1);
w.write("part{0}", 1);
EXPECT_EQ(strlen("part1"), w.size());
EXPECT_STREQ("part1", w.c_str());
EXPECT_STREQ("part1", w.data());
EXPECT_EQ("part1", w.str());
w.format("part{0}", 2);
w.write("part{0}", 2);
EXPECT_EQ(strlen("part1part2"), w.size());
EXPECT_STREQ("part1part2", w.c_str());
EXPECT_STREQ("part1part2", w.data());
@ -568,18 +568,18 @@ TEST(WriterTest, WWriter) {
}
TEST(FormatterTest, Escape) {
EXPECT_EQ("{", str(format("{{")));
EXPECT_EQ("before {", str(format("before {{")));
EXPECT_EQ("{ after", str(format("{{ after")));
EXPECT_EQ("before { after", str(format("before {{ after")));
EXPECT_EQ("{", format("{{"));
EXPECT_EQ("before {", format("before {{"));
EXPECT_EQ("{ after", format("{{ after"));
EXPECT_EQ("before { after", format("before {{ after"));
EXPECT_EQ("}", str(format("}}")));
EXPECT_EQ("before }", str(format("before }}")));
EXPECT_EQ("} after", str(format("}} after")));
EXPECT_EQ("before } after", str(format("before }} after")));
EXPECT_EQ("}", format("}}"));
EXPECT_EQ("before }", format("before }}"));
EXPECT_EQ("} after", format("}} after"));
EXPECT_EQ("before } after", format("before }} after"));
EXPECT_EQ("{}", str(format("{{}}")));
EXPECT_EQ("{42}", str(format("{{{0}}}", 42)));
EXPECT_EQ("{}", format("{{}}"));
EXPECT_EQ("{42}", format("{{{0}}}", 42));
}
TEST(FormatterTest, UnmatchedBraces) {
@ -589,18 +589,17 @@ TEST(FormatterTest, UnmatchedBraces) {
}
TEST(FormatterTest, NoArgs) {
EXPECT_EQ("test", str(format("test")));
EXPECT_EQ("test", format("test"));
}
TEST(FormatterTest, ArgsInDifferentPositions) {
EXPECT_EQ("42", str(format("{0}", 42)));
EXPECT_EQ("before 42", str(format("before {0}", 42)));
EXPECT_EQ("42 after", str(format("{0} after", 42)));
EXPECT_EQ("before 42 after", str(format("before {0} after", 42)));
EXPECT_EQ("answer = 42", str(format("{0} = {1}", "answer", 42)));
EXPECT_EQ("42 is the answer",
str(format("{1} is the {0}", "answer", 42)));
EXPECT_EQ("abracadabra", str(format("{0}{1}{0}", "abra", "cad")));
EXPECT_EQ("42", format("{0}", 42));
EXPECT_EQ("before 42", format("before {0}", 42));
EXPECT_EQ("42 after", format("{0} after", 42));
EXPECT_EQ("before 42 after", format("before {0} after", 42));
EXPECT_EQ("answer = 42", format("{0} = {1}", "answer", 42));
EXPECT_EQ("42 is the answer", format("{1} is the {0}", "answer", 42));
EXPECT_EQ("abracadabra", format("{0}{1}{0}", "abra", "cad"));
}
TEST(FormatterTest, ArgErrors) {
@ -625,12 +624,12 @@ TEST(FormatterTest, ArgErrors) {
}
TEST(FormatterTest, AutoArgIndex) {
EXPECT_EQ("abc", str(format("{}{}{}", 'a', 'b', 'c')));
EXPECT_EQ("abc", format("{}{}{}", 'a', 'b', 'c'));
EXPECT_THROW_MSG(format("{0}{}", 'a', 'b'),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(format("{}{0}", 'a', 'b'),
FormatError, "cannot switch from automatic to manual argument indexing");
EXPECT_EQ("1.2", str(format("{:.{}}", 1.2345, 2)));
EXPECT_EQ("1.2", format("{:.{}}", 1.2345, 2));
EXPECT_THROW_MSG(format("{0}:.{}", 1.2345, 2),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(format("{:.{0}}", 1.2345, 2),
@ -640,62 +639,60 @@ TEST(FormatterTest, AutoArgIndex) {
}
TEST(FormatterTest, EmptySpecs) {
EXPECT_EQ("42", str(format("{0:}", 42)));
EXPECT_EQ("42", format("{0:}", 42));
}
TEST(FormatterTest, LeftAlign) {
EXPECT_EQ("42 ", str(format("{0:<4}", 42)));
EXPECT_EQ("42 ", str(format("{0:<4o}", 042)));
EXPECT_EQ("42 ", str(format("{0:<4x}", 0x42)));
EXPECT_EQ("-42 ", str(format("{0:<5}", -42)));
EXPECT_EQ("42 ", str(format("{0:<5}", 42u)));
EXPECT_EQ("-42 ", str(format("{0:<5}", -42l)));
EXPECT_EQ("42 ", str(format("{0:<5}", 42ul)));
EXPECT_EQ("-42 ", str(format("{0:<5}", -42ll)));
EXPECT_EQ("42 ", str(format("{0:<5}", 42ull)));
EXPECT_EQ("-42 ", str(format("{0:<5}", -42.0)));
EXPECT_EQ("-42 ", str(format("{0:<5}", -42.0l)));
EXPECT_EQ("c ", str(format("{0:<5}", 'c')));
EXPECT_EQ("abc ", str(format("{0:<5}", "abc")));
EXPECT_EQ("0xface ",
str(format("{0:<8}", reinterpret_cast<void*>(0xface))));
EXPECT_EQ("def ", str(format("{0:<5}", TestString("def"))));
EXPECT_EQ("42 ", format("{0:<4}", 42));
EXPECT_EQ("42 ", format("{0:<4o}", 042));
EXPECT_EQ("42 ", format("{0:<4x}", 0x42));
EXPECT_EQ("-42 ", format("{0:<5}", -42));
EXPECT_EQ("42 ", format("{0:<5}", 42u));
EXPECT_EQ("-42 ", format("{0:<5}", -42l));
EXPECT_EQ("42 ", format("{0:<5}", 42ul));
EXPECT_EQ("-42 ", format("{0:<5}", -42ll));
EXPECT_EQ("42 ", format("{0:<5}", 42ull));
EXPECT_EQ("-42 ", format("{0:<5}", -42.0));
EXPECT_EQ("-42 ", format("{0:<5}", -42.0l));
EXPECT_EQ("c ", format("{0:<5}", 'c'));
EXPECT_EQ("abc ", format("{0:<5}", "abc"));
EXPECT_EQ("0xface ", format("{0:<8}", reinterpret_cast<void*>(0xface)));
EXPECT_EQ("def ", format("{0:<5}", TestString("def")));
}
TEST(FormatterTest, RightAlign) {
EXPECT_EQ(" 42", str(format("{0:>4}", 42)));
EXPECT_EQ(" 42", str(format("{0:>4o}", 042)));
EXPECT_EQ(" 42", str(format("{0:>4x}", 0x42)));
EXPECT_EQ(" -42", str(format("{0:>5}", -42)));
EXPECT_EQ(" 42", str(format("{0:>5}", 42u)));
EXPECT_EQ(" -42", str(format("{0:>5}", -42l)));
EXPECT_EQ(" 42", str(format("{0:>5}", 42ul)));
EXPECT_EQ(" -42", str(format("{0:>5}", -42ll)));
EXPECT_EQ(" 42", str(format("{0:>5}", 42ull)));
EXPECT_EQ(" -42", str(format("{0:>5}", -42.0)));
EXPECT_EQ(" -42", str(format("{0:>5}", -42.0l)));
EXPECT_EQ(" c", str(format("{0:>5}", 'c')));
EXPECT_EQ(" abc", str(format("{0:>5}", "abc")));
EXPECT_EQ(" 0xface",
str(format("{0:>8}", reinterpret_cast<void*>(0xface))));
EXPECT_EQ(" def", str(format("{0:>5}", TestString("def"))));
EXPECT_EQ(" 42", format("{0:>4}", 42));
EXPECT_EQ(" 42", format("{0:>4o}", 042));
EXPECT_EQ(" 42", format("{0:>4x}", 0x42));
EXPECT_EQ(" -42", format("{0:>5}", -42));
EXPECT_EQ(" 42", format("{0:>5}", 42u));
EXPECT_EQ(" -42", format("{0:>5}", -42l));
EXPECT_EQ(" 42", format("{0:>5}", 42ul));
EXPECT_EQ(" -42", format("{0:>5}", -42ll));
EXPECT_EQ(" 42", format("{0:>5}", 42ull));
EXPECT_EQ(" -42", format("{0:>5}", -42.0));
EXPECT_EQ(" -42", format("{0:>5}", -42.0l));
EXPECT_EQ(" c", format("{0:>5}", 'c'));
EXPECT_EQ(" abc", format("{0:>5}", "abc"));
EXPECT_EQ(" 0xface", format("{0:>8}", reinterpret_cast<void*>(0xface)));
EXPECT_EQ(" def", format("{0:>5}", TestString("def")));
}
TEST(FormatterTest, NumericAlign) {
EXPECT_EQ(" 42", str(format("{0:=4}", 42)));
EXPECT_EQ("+ 42", str(format("{0:=+4}", 42)));
EXPECT_EQ(" 42", str(format("{0:=4o}", 042)));
EXPECT_EQ("+ 42", str(format("{0:=+4o}", 042)));
EXPECT_EQ(" 42", str(format("{0:=4x}", 0x42)));
EXPECT_EQ("+ 42", str(format("{0:=+4x}", 0x42)));
EXPECT_EQ("- 42", str(format("{0:=5}", -42)));
EXPECT_EQ(" 42", str(format("{0:=5}", 42u)));
EXPECT_EQ("- 42", str(format("{0:=5}", -42l)));
EXPECT_EQ(" 42", str(format("{0:=5}", 42ul)));
EXPECT_EQ("- 42", str(format("{0:=5}", -42ll)));
EXPECT_EQ(" 42", str(format("{0:=5}", 42ull)));
EXPECT_EQ("- 42", str(format("{0:=5}", -42.0)));
EXPECT_EQ("- 42", str(format("{0:=5}", -42.0l)));
EXPECT_EQ(" 42", format("{0:=4}", 42));
EXPECT_EQ("+ 42", format("{0:=+4}", 42));
EXPECT_EQ(" 42", format("{0:=4o}", 042));
EXPECT_EQ("+ 42", format("{0:=+4o}", 042));
EXPECT_EQ(" 42", format("{0:=4x}", 0x42));
EXPECT_EQ("+ 42", format("{0:=+4x}", 0x42));
EXPECT_EQ("- 42", format("{0:=5}", -42));
EXPECT_EQ(" 42", format("{0:=5}", 42u));
EXPECT_EQ("- 42", format("{0:=5}", -42l));
EXPECT_EQ(" 42", format("{0:=5}", 42ul));
EXPECT_EQ("- 42", format("{0:=5}", -42ll));
EXPECT_EQ(" 42", format("{0:=5}", 42ull));
EXPECT_EQ("- 42", format("{0:=5}", -42.0));
EXPECT_EQ("- 42", format("{0:=5}", -42.0l));
EXPECT_THROW_MSG(format("{0:=5", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:=5}", 'c'),
@ -709,22 +706,21 @@ TEST(FormatterTest, NumericAlign) {
}
TEST(FormatterTest, CenterAlign) {
EXPECT_EQ(" 42 ", str(format("{0:^5}", 42)));
EXPECT_EQ(" 42 ", str(format("{0:^5o}", 042)));
EXPECT_EQ(" 42 ", str(format("{0:^5x}", 0x42)));
EXPECT_EQ(" -42 ", str(format("{0:^5}", -42)));
EXPECT_EQ(" 42 ", str(format("{0:^5}", 42u)));
EXPECT_EQ(" -42 ", str(format("{0:^5}", -42l)));
EXPECT_EQ(" 42 ", str(format("{0:^5}", 42ul)));
EXPECT_EQ(" -42 ", str(format("{0:^5}", -42ll)));
EXPECT_EQ(" 42 ", str(format("{0:^5}", 42ull)));
EXPECT_EQ(" -42 ", str(format("{0:^6}", -42.0)));
EXPECT_EQ(" -42 ", str(format("{0:^5}", -42.0l)));
EXPECT_EQ(" c ", str(format("{0:^5}", 'c')));
EXPECT_EQ(" abc ", str(format("{0:^6}", "abc")));
EXPECT_EQ(" 0xface ",
str(format("{0:^8}", reinterpret_cast<void*>(0xface))));
EXPECT_EQ(" def ", str(format("{0:^5}", TestString("def"))));
EXPECT_EQ(" 42 ", format("{0:^5}", 42));
EXPECT_EQ(" 42 ", format("{0:^5o}", 042));
EXPECT_EQ(" 42 ", format("{0:^5x}", 0x42));
EXPECT_EQ(" -42 ", format("{0:^5}", -42));
EXPECT_EQ(" 42 ", format("{0:^5}", 42u));
EXPECT_EQ(" -42 ", format("{0:^5}", -42l));
EXPECT_EQ(" 42 ", format("{0:^5}", 42ul));
EXPECT_EQ(" -42 ", format("{0:^5}", -42ll));
EXPECT_EQ(" 42 ", format("{0:^5}", 42ull));
EXPECT_EQ(" -42 ", format("{0:^6}", -42.0));
EXPECT_EQ(" -42 ", format("{0:^5}", -42.0l));
EXPECT_EQ(" c ", format("{0:^5}", 'c'));
EXPECT_EQ(" abc ", format("{0:^6}", "abc"));
EXPECT_EQ(" 0xface ", format("{0:^8}", reinterpret_cast<void*>(0xface)));
EXPECT_EQ(" def ", format("{0:^5}", TestString("def")));
}
TEST(FormatterTest, Fill) {
@ -732,35 +728,35 @@ TEST(FormatterTest, Fill) {
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:{<5}}", 'c'),
FormatError, "invalid fill character '{'");
EXPECT_EQ("**42", str(format("{0:*>4}", 42)));
EXPECT_EQ("**-42", str(format("{0:*>5}", -42)));
EXPECT_EQ("***42", str(format("{0:*>5}", 42u)));
EXPECT_EQ("**-42", str(format("{0:*>5}", -42l)));
EXPECT_EQ("***42", str(format("{0:*>5}", 42ul)));
EXPECT_EQ("**-42", str(format("{0:*>5}", -42ll)));
EXPECT_EQ("***42", str(format("{0:*>5}", 42ull)));
EXPECT_EQ("**-42", str(format("{0:*>5}", -42.0)));
EXPECT_EQ("**-42", str(format("{0:*>5}", -42.0l)));
EXPECT_EQ("c****", str(format("{0:*<5}", 'c')));
EXPECT_EQ("abc**", str(format("{0:*<5}", "abc")));
EXPECT_EQ("**0xface", str(format("{0:*>8}", reinterpret_cast<void*>(0xface))));
EXPECT_EQ("def**", str(format("{0:*<5}", TestString("def"))));
EXPECT_EQ("**42", format("{0:*>4}", 42));
EXPECT_EQ("**-42", format("{0:*>5}", -42));
EXPECT_EQ("***42", format("{0:*>5}", 42u));
EXPECT_EQ("**-42", format("{0:*>5}", -42l));
EXPECT_EQ("***42", format("{0:*>5}", 42ul));
EXPECT_EQ("**-42", format("{0:*>5}", -42ll));
EXPECT_EQ("***42", format("{0:*>5}", 42ull));
EXPECT_EQ("**-42", format("{0:*>5}", -42.0));
EXPECT_EQ("**-42", format("{0:*>5}", -42.0l));
EXPECT_EQ("c****", format("{0:*<5}", 'c'));
EXPECT_EQ("abc**", format("{0:*<5}", "abc"));
EXPECT_EQ("**0xface", format("{0:*>8}", reinterpret_cast<void*>(0xface)));
EXPECT_EQ("def**", format("{0:*<5}", TestString("def")));
}
TEST(FormatterTest, PlusSign) {
EXPECT_EQ("+42", str(format("{0:+}", 42)));
EXPECT_EQ("-42", str(format("{0:+}", -42)));
EXPECT_EQ("+42", str(format("{0:+}", 42)));
EXPECT_EQ("+42", format("{0:+}", 42));
EXPECT_EQ("-42", format("{0:+}", -42));
EXPECT_EQ("+42", format("{0:+}", 42));
EXPECT_THROW_MSG(format("{0:+}", 42u),
FormatError, "format specifier '+' requires signed argument");
EXPECT_EQ("+42", str(format("{0:+}", 42l)));
EXPECT_EQ("+42", format("{0:+}", 42l));
EXPECT_THROW_MSG(format("{0:+}", 42ul),
FormatError, "format specifier '+' requires signed argument");
EXPECT_EQ("+42", str(format("{0:+}", 42ll)));
EXPECT_EQ("+42", format("{0:+}", 42ll));
EXPECT_THROW_MSG(format("{0:+}", 42ull),
FormatError, "format specifier '+' requires signed argument");
EXPECT_EQ("+42", str(format("{0:+}", 42.0)));
EXPECT_EQ("+42", str(format("{0:+}", 42.0l)));
EXPECT_EQ("+42", format("{0:+}", 42.0));
EXPECT_EQ("+42", format("{0:+}", 42.0l));
EXPECT_THROW_MSG(format("{0:+", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:+}", 'c'),
@ -774,19 +770,19 @@ TEST(FormatterTest, PlusSign) {
}
TEST(FormatterTest, MinusSign) {
EXPECT_EQ("42", str(format("{0:-}", 42)));
EXPECT_EQ("-42", str(format("{0:-}", -42)));
EXPECT_EQ("42", str(format("{0:-}", 42)));
EXPECT_EQ("42", format("{0:-}", 42));
EXPECT_EQ("-42", format("{0:-}", -42));
EXPECT_EQ("42", format("{0:-}", 42));
EXPECT_THROW_MSG(format("{0:-}", 42u),
FormatError, "format specifier '-' requires signed argument");
EXPECT_EQ("42", str(format("{0:-}", 42l)));
EXPECT_EQ("42", format("{0:-}", 42l));
EXPECT_THROW_MSG(format("{0:-}", 42ul),
FormatError, "format specifier '-' requires signed argument");
EXPECT_EQ("42", str(format("{0:-}", 42ll)));
EXPECT_EQ("42", format("{0:-}", 42ll));
EXPECT_THROW_MSG(format("{0:-}", 42ull),
FormatError, "format specifier '-' requires signed argument");
EXPECT_EQ("42", str(format("{0:-}", 42.0)));
EXPECT_EQ("42", str(format("{0:-}", 42.0l)));
EXPECT_EQ("42", format("{0:-}", 42.0));
EXPECT_EQ("42", format("{0:-}", 42.0l));
EXPECT_THROW_MSG(format("{0:-", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:-}", 'c'),
@ -800,19 +796,19 @@ TEST(FormatterTest, MinusSign) {
}
TEST(FormatterTest, SpaceSign) {
EXPECT_EQ(" 42", str(format("{0: }", 42)));
EXPECT_EQ("-42", str(format("{0: }", -42)));
EXPECT_EQ(" 42", str(format("{0: }", 42)));
EXPECT_EQ(" 42", format("{0: }", 42));
EXPECT_EQ("-42", format("{0: }", -42));
EXPECT_EQ(" 42", format("{0: }", 42));
EXPECT_THROW_MSG(format("{0: }", 42u),
FormatError, "format specifier ' ' requires signed argument");
EXPECT_EQ(" 42", str(format("{0: }", 42l)));
EXPECT_EQ(" 42", format("{0: }", 42l));
EXPECT_THROW_MSG(format("{0: }", 42ul),
FormatError, "format specifier ' ' requires signed argument");
EXPECT_EQ(" 42", str(format("{0: }", 42ll)));
EXPECT_EQ(" 42", format("{0: }", 42ll));
EXPECT_THROW_MSG(format("{0: }", 42ull),
FormatError, "format specifier ' ' requires signed argument");
EXPECT_EQ(" 42", str(format("{0: }", 42.0)));
EXPECT_EQ(" 42", str(format("{0: }", 42.0l)));
EXPECT_EQ(" 42", format("{0: }", 42.0));
EXPECT_EQ(" 42", format("{0: }", 42.0l));
EXPECT_THROW_MSG(format("{0: ", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0: }", 'c'),
@ -826,40 +822,40 @@ TEST(FormatterTest, SpaceSign) {
}
TEST(FormatterTest, HashFlag) {
EXPECT_EQ("42", str(format("{0:#}", 42)));
EXPECT_EQ("-42", str(format("{0:#}", -42)));
EXPECT_EQ("0b101010", str(format("{0:#b}", 42)));
EXPECT_EQ("0B101010", str(format("{0:#B}", 42)));
EXPECT_EQ("-0b101010", str(format("{0:#b}", -42)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42)));
EXPECT_EQ("0X42", str(format("{0:#X}", 0x42)));
EXPECT_EQ("-0x42", str(format("{0:#x}", -0x42)));
EXPECT_EQ("042", str(format("{0:#o}", 042)));
EXPECT_EQ("-042", str(format("{0:#o}", -042)));
EXPECT_EQ("42", str(format("{0:#}", 42u)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42u)));
EXPECT_EQ("042", str(format("{0:#o}", 042u)));
EXPECT_EQ("42", format("{0:#}", 42));
EXPECT_EQ("-42", format("{0:#}", -42));
EXPECT_EQ("0b101010", format("{0:#b}", 42));
EXPECT_EQ("0B101010", format("{0:#B}", 42));
EXPECT_EQ("-0b101010", format("{0:#b}", -42));
EXPECT_EQ("0x42", format("{0:#x}", 0x42));
EXPECT_EQ("0X42", format("{0:#X}", 0x42));
EXPECT_EQ("-0x42", format("{0:#x}", -0x42));
EXPECT_EQ("042", format("{0:#o}", 042));
EXPECT_EQ("-042", format("{0:#o}", -042));
EXPECT_EQ("42", format("{0:#}", 42u));
EXPECT_EQ("0x42", format("{0:#x}", 0x42u));
EXPECT_EQ("042", format("{0:#o}", 042u));
EXPECT_EQ("-42", str(format("{0:#}", -42l)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42l)));
EXPECT_EQ("-0x42", str(format("{0:#x}", -0x42l)));
EXPECT_EQ("042", str(format("{0:#o}", 042l)));
EXPECT_EQ("-042", str(format("{0:#o}", -042l)));
EXPECT_EQ("42", str(format("{0:#}", 42ul)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42ul)));
EXPECT_EQ("042", str(format("{0:#o}", 042ul)));
EXPECT_EQ("-42", format("{0:#}", -42l));
EXPECT_EQ("0x42", format("{0:#x}", 0x42l));
EXPECT_EQ("-0x42", format("{0:#x}", -0x42l));
EXPECT_EQ("042", format("{0:#o}", 042l));
EXPECT_EQ("-042", format("{0:#o}", -042l));
EXPECT_EQ("42", format("{0:#}", 42ul));
EXPECT_EQ("0x42", format("{0:#x}", 0x42ul));
EXPECT_EQ("042", format("{0:#o}", 042ul));
EXPECT_EQ("-42", str(format("{0:#}", -42ll)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42ll)));
EXPECT_EQ("-0x42", str(format("{0:#x}", -0x42ll)));
EXPECT_EQ("042", str(format("{0:#o}", 042ll)));
EXPECT_EQ("-042", str(format("{0:#o}", -042ll)));
EXPECT_EQ("42", str(format("{0:#}", 42ull)));
EXPECT_EQ("0x42", str(format("{0:#x}", 0x42ull)));
EXPECT_EQ("042", str(format("{0:#o}", 042ull)));
EXPECT_EQ("-42", format("{0:#}", -42ll));
EXPECT_EQ("0x42", format("{0:#x}", 0x42ll));
EXPECT_EQ("-0x42", format("{0:#x}", -0x42ll));
EXPECT_EQ("042", format("{0:#o}", 042ll));
EXPECT_EQ("-042", format("{0:#o}", -042ll));
EXPECT_EQ("42", format("{0:#}", 42ull));
EXPECT_EQ("0x42", format("{0:#x}", 0x42ull));
EXPECT_EQ("042", format("{0:#o}", 042ull));
EXPECT_EQ("-42.0000", str(format("{0:#}", -42.0)));
EXPECT_EQ("-42.0000", str(format("{0:#}", -42.0l)));
EXPECT_EQ("-42.0000", format("{0:#}", -42.0));
EXPECT_EQ("-42.0000", format("{0:#}", -42.0l));
EXPECT_THROW_MSG(format("{0:#", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:#}", 'c'),
@ -873,15 +869,15 @@ TEST(FormatterTest, HashFlag) {
}
TEST(FormatterTest, ZeroFlag) {
EXPECT_EQ("42", str(format("{0:0}", 42)));
EXPECT_EQ("-0042", str(format("{0:05}", -42)));
EXPECT_EQ("00042", str(format("{0:05}", 42u)));
EXPECT_EQ("-0042", str(format("{0:05}", -42l)));
EXPECT_EQ("00042", str(format("{0:05}", 42ul)));
EXPECT_EQ("-0042", str(format("{0:05}", -42ll)));
EXPECT_EQ("00042", str(format("{0:05}", 42ull)));
EXPECT_EQ("-0042", str(format("{0:05}", -42.0)));
EXPECT_EQ("-0042", str(format("{0:05}", -42.0l)));
EXPECT_EQ("42", format("{0:0}", 42));
EXPECT_EQ("-0042", format("{0:05}", -42));
EXPECT_EQ("00042", format("{0:05}", 42u));
EXPECT_EQ("-0042", format("{0:05}", -42l));
EXPECT_EQ("00042", format("{0:05}", 42ul));
EXPECT_EQ("-0042", format("{0:05}", -42ll));
EXPECT_EQ("00042", format("{0:05}", 42ull));
EXPECT_EQ("-0042", format("{0:05}", -42.0));
EXPECT_EQ("-0042", format("{0:05}", -42.0l));
EXPECT_THROW_MSG(format("{0:0", 'c'),
FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(format("{0:05}", 'c'),
@ -910,19 +906,18 @@ TEST(FormatterTest, Width) {
SPrintf(format_str, "{0:%u}", INT_MAX + 1u);
EXPECT_THROW_MSG(format(format_str, 0),
FormatError, "number is too big in format");
EXPECT_EQ(" -42", str(format("{0:4}", -42)));
EXPECT_EQ(" 42", str(format("{0:5}", 42u)));
EXPECT_EQ(" -42", str(format("{0:6}", -42l)));
EXPECT_EQ(" 42", str(format("{0:7}", 42ul)));
EXPECT_EQ(" -42", str(format("{0:6}", -42ll)));
EXPECT_EQ(" 42", str(format("{0:7}", 42ull)));
EXPECT_EQ(" -1.23", str(format("{0:8}", -1.23)));
EXPECT_EQ(" -1.23", str(format("{0:9}", -1.23l)));
EXPECT_EQ(" 0xcafe",
str(format("{0:10}", reinterpret_cast<void*>(0xcafe))));
EXPECT_EQ("x ", str(format("{0:11}", 'x')));
EXPECT_EQ("str ", str(format("{0:12}", "str")));
EXPECT_EQ("test ", str(format("{0:13}", TestString("test"))));
EXPECT_EQ(" -42", format("{0:4}", -42));
EXPECT_EQ(" 42", format("{0:5}", 42u));
EXPECT_EQ(" -42", format("{0:6}", -42l));
EXPECT_EQ(" 42", format("{0:7}", 42ul));
EXPECT_EQ(" -42", format("{0:6}", -42ll));
EXPECT_EQ(" 42", format("{0:7}", 42ull));
EXPECT_EQ(" -1.23", format("{0:8}", -1.23));
EXPECT_EQ(" -1.23", format("{0:9}", -1.23l));
EXPECT_EQ(" 0xcafe", format("{0:10}", reinterpret_cast<void*>(0xcafe)));
EXPECT_EQ("x ", format("{0:11}", 'x'));
EXPECT_EQ("str ", format("{0:12}", "str"));
EXPECT_EQ("test ", format("{0:13}", TestString("test")));
}
TEST(FormatterTest, Precision) {
@ -973,8 +968,8 @@ TEST(FormatterTest, Precision) {
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(format("{0:.2f}", 42ull),
FormatError, "precision specifier requires floating-point argument");
EXPECT_EQ("1.2", str(format("{0:.2}", 1.2345)));
EXPECT_EQ("1.2", str(format("{0:.2}", 1.2345l)));
EXPECT_EQ("1.2", format("{0:.2}", 1.2345));
EXPECT_EQ("1.2", format("{0:.2}", 1.2345l));
EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)),
FormatError, "precision specifier requires floating-point argument");
@ -1065,8 +1060,8 @@ TEST(FormatterTest, RuntimePrecision) {
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(format("{0:.{1}f}", 42ull, 2),
FormatError, "precision specifier requires floating-point argument");
EXPECT_EQ("1.2", str(format("{0:.{1}}", 1.2345, 2)));
EXPECT_EQ("1.2", str(format("{1:.{0}}", 2, 1.2345l)));
EXPECT_EQ("1.2", format("{0:.{1}}", 1.2345, 2));
EXPECT_EQ("1.2", format("{1:.{0}}", 2, 1.2345l));
EXPECT_THROW_MSG(format("{0:.{1}}", reinterpret_cast<void*>(0xcafe), 2),
FormatError, "precision specifier requires floating-point argument");
@ -1108,14 +1103,14 @@ void CheckUnknownTypes(
}
TEST(FormatterTest, FormatBool) {
EXPECT_EQ(L"1", str(format(L"{}", true)));
EXPECT_EQ(L"1", format(L"{}", true));
}
TEST(FormatterTest, FormatShort) {
short s = 42;
EXPECT_EQ("42", str(format("{0:d}", s)));
EXPECT_EQ("42", format("{0:d}", s));
unsigned short us = 42;
EXPECT_EQ("42", str(format("{0:d}", us)));
EXPECT_EQ("42", format("{0:d}", us));
}
TEST(FormatterTest, FormatInt) {
@ -1125,196 +1120,196 @@ TEST(FormatterTest, FormatInt) {
}
TEST(FormatterTest, FormatBin) {
EXPECT_EQ("0", str(format("{0:b}", 0)));
EXPECT_EQ("101010", str(format("{0:b}", 42)));
EXPECT_EQ("101010", str(format("{0:b}", 42u)));
EXPECT_EQ("-101010", str(format("{0:b}", -42)));
EXPECT_EQ("11000000111001", str(format("{0:b}", 12345)));
EXPECT_EQ("10010001101000101011001111000", str(format("{0:b}", 0x12345678)));
EXPECT_EQ("10010000101010111100110111101111", str(format("{0:b}", 0x90ABCDEF)));
EXPECT_EQ("0", format("{0:b}", 0));
EXPECT_EQ("101010", format("{0:b}", 42));
EXPECT_EQ("101010", format("{0:b}", 42u));
EXPECT_EQ("-101010", format("{0:b}", -42));
EXPECT_EQ("11000000111001", format("{0:b}", 12345));
EXPECT_EQ("10010001101000101011001111000", format("{0:b}", 0x12345678));
EXPECT_EQ("10010000101010111100110111101111", format("{0:b}", 0x90ABCDEF));
EXPECT_EQ("11111111111111111111111111111111",
str(format("{0:b}", std::numeric_limits<uint32_t>::max())));
format("{0:b}", std::numeric_limits<uint32_t>::max()));
}
TEST(FormatterTest, FormatDec) {
EXPECT_EQ("0", str(format("{0}", 0)));
EXPECT_EQ("42", str(format("{0}", 42)));
EXPECT_EQ("42", str(format("{0:d}", 42)));
EXPECT_EQ("42", str(format("{0}", 42u)));
EXPECT_EQ("-42", str(format("{0}", -42)));
EXPECT_EQ("12345", str(format("{0}", 12345)));
EXPECT_EQ("67890", str(format("{0}", 67890)));
EXPECT_EQ("0", format("{0}", 0));
EXPECT_EQ("42", format("{0}", 42));
EXPECT_EQ("42", format("{0:d}", 42));
EXPECT_EQ("42", format("{0}", 42u));
EXPECT_EQ("-42", format("{0}", -42));
EXPECT_EQ("12345", format("{0}", 12345));
EXPECT_EQ("67890", format("{0}", 67890));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%d", INT_MIN);
EXPECT_EQ(buffer, str(format("{0}", INT_MIN)));
EXPECT_EQ(buffer, format("{0}", INT_MIN));
SPrintf(buffer, "%d", INT_MAX);
EXPECT_EQ(buffer, str(format("{0}", INT_MAX)));
EXPECT_EQ(buffer, format("{0}", INT_MAX));
SPrintf(buffer, "%u", UINT_MAX);
EXPECT_EQ(buffer, str(format("{0}", UINT_MAX)));
EXPECT_EQ(buffer, format("{0}", UINT_MAX));
SPrintf(buffer, "%ld", 0 - static_cast<unsigned long>(LONG_MIN));
EXPECT_EQ(buffer, str(format("{0}", LONG_MIN)));
EXPECT_EQ(buffer, format("{0}", LONG_MIN));
SPrintf(buffer, "%ld", LONG_MAX);
EXPECT_EQ(buffer, str(format("{0}", LONG_MAX)));
EXPECT_EQ(buffer, format("{0}", LONG_MAX));
SPrintf(buffer, "%lu", ULONG_MAX);
EXPECT_EQ(buffer, str(format("{0}", ULONG_MAX)));
EXPECT_EQ(buffer, format("{0}", ULONG_MAX));
}
TEST(FormatterTest, FormatHex) {
EXPECT_EQ("0", str(format("{0:x}", 0)));
EXPECT_EQ("42", str(format("{0:x}", 0x42)));
EXPECT_EQ("42", str(format("{0:x}", 0x42u)));
EXPECT_EQ("-42", str(format("{0:x}", -0x42)));
EXPECT_EQ("12345678", str(format("{0:x}", 0x12345678)));
EXPECT_EQ("90abcdef", str(format("{0:x}", 0x90abcdef)));
EXPECT_EQ("12345678", str(format("{0:X}", 0x12345678)));
EXPECT_EQ("90ABCDEF", str(format("{0:X}", 0x90ABCDEF)));
EXPECT_EQ("0", format("{0:x}", 0));
EXPECT_EQ("42", format("{0:x}", 0x42));
EXPECT_EQ("42", format("{0:x}", 0x42u));
EXPECT_EQ("-42", format("{0:x}", -0x42));
EXPECT_EQ("12345678", format("{0:x}", 0x12345678));
EXPECT_EQ("90abcdef", format("{0:x}", 0x90abcdef));
EXPECT_EQ("12345678", format("{0:X}", 0x12345678));
EXPECT_EQ("90ABCDEF", format("{0:X}", 0x90ABCDEF));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "-%x", 0 - static_cast<unsigned>(INT_MIN));
EXPECT_EQ(buffer, str(format("{0:x}", INT_MIN)));
EXPECT_EQ(buffer, format("{0:x}", INT_MIN));
SPrintf(buffer, "%x", INT_MAX);
EXPECT_EQ(buffer, str(format("{0:x}", INT_MAX)));
EXPECT_EQ(buffer, format("{0:x}", INT_MAX));
SPrintf(buffer, "%x", UINT_MAX);
EXPECT_EQ(buffer, str(format("{0:x}", UINT_MAX)));
EXPECT_EQ(buffer, format("{0:x}", UINT_MAX));
SPrintf(buffer, "-%lx", 0 - static_cast<unsigned long>(LONG_MIN));
EXPECT_EQ(buffer, str(format("{0:x}", LONG_MIN)));
EXPECT_EQ(buffer, format("{0:x}", LONG_MIN));
SPrintf(buffer, "%lx", LONG_MAX);
EXPECT_EQ(buffer, str(format("{0:x}", LONG_MAX)));
EXPECT_EQ(buffer, format("{0:x}", LONG_MAX));
SPrintf(buffer, "%lx", ULONG_MAX);
EXPECT_EQ(buffer, str(format("{0:x}", ULONG_MAX)));
EXPECT_EQ(buffer, format("{0:x}", ULONG_MAX));
}
TEST(FormatterTest, FormatOct) {
EXPECT_EQ("0", str(format("{0:o}", 0)));
EXPECT_EQ("42", str(format("{0:o}", 042)));
EXPECT_EQ("42", str(format("{0:o}", 042u)));
EXPECT_EQ("-42", str(format("{0:o}", -042)));
EXPECT_EQ("12345670", str(format("{0:o}", 012345670)));
EXPECT_EQ("0", format("{0:o}", 0));
EXPECT_EQ("42", format("{0:o}", 042));
EXPECT_EQ("42", format("{0:o}", 042u));
EXPECT_EQ("-42", format("{0:o}", -042));
EXPECT_EQ("12345670", format("{0:o}", 012345670));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "-%o", 0 - static_cast<unsigned>(INT_MIN));
EXPECT_EQ(buffer, str(format("{0:o}", INT_MIN)));
EXPECT_EQ(buffer, format("{0:o}", INT_MIN));
SPrintf(buffer, "%o", INT_MAX);
EXPECT_EQ(buffer, str(format("{0:o}", INT_MAX)));
EXPECT_EQ(buffer, format("{0:o}", INT_MAX));
SPrintf(buffer, "%o", UINT_MAX);
EXPECT_EQ(buffer, str(format("{0:o}", UINT_MAX)));
EXPECT_EQ(buffer, format("{0:o}", UINT_MAX));
SPrintf(buffer, "-%lo", 0 - static_cast<unsigned long>(LONG_MIN));
EXPECT_EQ(buffer, str(format("{0:o}", LONG_MIN)));
EXPECT_EQ(buffer, format("{0:o}", LONG_MIN));
SPrintf(buffer, "%lo", LONG_MAX);
EXPECT_EQ(buffer, str(format("{0:o}", LONG_MAX)));
EXPECT_EQ(buffer, format("{0:o}", LONG_MAX));
SPrintf(buffer, "%lo", ULONG_MAX);
EXPECT_EQ(buffer, str(format("{0:o}", ULONG_MAX)));
EXPECT_EQ(buffer, format("{0:o}", ULONG_MAX));
}
TEST(FormatterTest, FormatFloat) {
EXPECT_EQ("392.500000", str(format("{0:f}", 392.5f)));
EXPECT_EQ("392.500000", format("{0:f}", 392.5f));
}
TEST(FormatterTest, FormatDouble) {
CheckUnknownTypes(1.2, "eEfFgGaA", "double");
EXPECT_EQ("0", str(format("{0:}", 0.0)));
EXPECT_EQ("0.000000", str(format("{0:f}", 0.0)));
EXPECT_EQ("392.65", str(format("{0:}", 392.65)));
EXPECT_EQ("392.65", str(format("{0:g}", 392.65)));
EXPECT_EQ("392.65", str(format("{0:G}", 392.65)));
EXPECT_EQ("392.650000", str(format("{0:f}", 392.65)));
EXPECT_EQ("392.650000", str(format("{0:F}", 392.65)));
EXPECT_EQ("0", format("{0:}", 0.0));
EXPECT_EQ("0.000000", format("{0:f}", 0.0));
EXPECT_EQ("392.65", format("{0:}", 392.65));
EXPECT_EQ("392.65", format("{0:g}", 392.65));
EXPECT_EQ("392.65", format("{0:G}", 392.65));
EXPECT_EQ("392.650000", format("{0:f}", 392.65));
EXPECT_EQ("392.650000", format("{0:F}", 392.65));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%e", 392.65);
EXPECT_EQ(buffer, str(format("{0:e}", 392.65)));
EXPECT_EQ(buffer, format("{0:e}", 392.65));
SPrintf(buffer, "%E", 392.65);
EXPECT_EQ(buffer, str(format("{0:E}", 392.65)));
EXPECT_EQ("+0000392.6", str(format("{0:+010.4g}", 392.65)));
EXPECT_EQ(buffer, format("{0:E}", 392.65));
EXPECT_EQ("+0000392.6", format("{0:+010.4g}", 392.65));
SPrintf(buffer, "%a", -42.0);
EXPECT_EQ(buffer, str(format("{:a}", -42.0)));
EXPECT_EQ(buffer, format("{:a}", -42.0));
SPrintf(buffer, "%A", -42.0);
EXPECT_EQ(buffer, str(format("{:A}", -42.0)));
EXPECT_EQ(buffer, format("{:A}", -42.0));
}
TEST(FormatterTest, FormatNaN) {
double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_EQ("nan", str(format("{}", nan)));
EXPECT_EQ("+nan", str(format("{:+}", nan)));
EXPECT_EQ("nan", format("{}", nan));
EXPECT_EQ("+nan", format("{:+}", nan));
if (fmt::internal::SignBitNoInline(-nan))
EXPECT_EQ("-nan", str(format("{}", -nan)));
EXPECT_EQ("-nan", format("{}", -nan));
else
fmt::Print("Warning: compiler doesn't handle negative NaN correctly");
EXPECT_EQ(" nan", str(format("{: }", nan)));
EXPECT_EQ("NAN", str(format("{:F}", nan)));
EXPECT_EQ("nan ", str(format("{:<7}", nan)));
EXPECT_EQ(" nan ", str(format("{:^7}", nan)));
EXPECT_EQ(" nan", str(format("{:>7}", nan)));
EXPECT_EQ(" nan", format("{: }", nan));
EXPECT_EQ("NAN", format("{:F}", nan));
EXPECT_EQ("nan ", format("{:<7}", nan));
EXPECT_EQ(" nan ", format("{:^7}", nan));
EXPECT_EQ(" nan", format("{:>7}", nan));
}
TEST(FormatterTest, FormatInfinity) {
double inf = std::numeric_limits<double>::infinity();
EXPECT_EQ("inf", str(format("{}", inf)));
EXPECT_EQ("+inf", str(format("{:+}", inf)));
EXPECT_EQ("-inf", str(format("{}", -inf)));
EXPECT_EQ(" inf", str(format("{: }", inf)));
EXPECT_EQ("INF", str(format("{:F}", inf)));
EXPECT_EQ("inf ", str(format("{:<7}", inf)));
EXPECT_EQ(" inf ", str(format("{:^7}", inf)));
EXPECT_EQ(" inf", str(format("{:>7}", inf)));
EXPECT_EQ("inf", format("{}", inf));
EXPECT_EQ("+inf", format("{:+}", inf));
EXPECT_EQ("-inf", format("{}", -inf));
EXPECT_EQ(" inf", format("{: }", inf));
EXPECT_EQ("INF", format("{:F}", inf));
EXPECT_EQ("inf ", format("{:<7}", inf));
EXPECT_EQ(" inf ", format("{:^7}", inf));
EXPECT_EQ(" inf", format("{:>7}", inf));
}
TEST(FormatterTest, FormatLongDouble) {
EXPECT_EQ("0", str(format("{0:}", 0.0l)));
EXPECT_EQ("0.000000", str(format("{0:f}", 0.0l)));
EXPECT_EQ("392.65", str(format("{0:}", 392.65l)));
EXPECT_EQ("392.65", str(format("{0:g}", 392.65l)));
EXPECT_EQ("392.65", str(format("{0:G}", 392.65l)));
EXPECT_EQ("392.650000", str(format("{0:f}", 392.65l)));
EXPECT_EQ("392.650000", str(format("{0:F}", 392.65l)));
EXPECT_EQ("0", format("{0:}", 0.0l));
EXPECT_EQ("0.000000", format("{0:f}", 0.0l));
EXPECT_EQ("392.65", format("{0:}", 392.65l));
EXPECT_EQ("392.65", format("{0:g}", 392.65l));
EXPECT_EQ("392.65", format("{0:G}", 392.65l));
EXPECT_EQ("392.650000", format("{0:f}", 392.65l));
EXPECT_EQ("392.650000", format("{0:F}", 392.65l));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%Le", 392.65l);
EXPECT_EQ(buffer, str(format("{0:e}", 392.65l)));
EXPECT_EQ(buffer, format("{0:e}", 392.65l));
SPrintf(buffer, "%LE", 392.65l);
EXPECT_EQ("+0000392.6", str(format("{0:+010.4g}", 392.65l)));
EXPECT_EQ("+0000392.6", format("{0:+010.4g}", 392.65l));
}
TEST(FormatterTest, FormatChar) {
CheckUnknownTypes('a', "c", "char");
EXPECT_EQ("a", str(format("{0}", 'a')));
EXPECT_EQ("z", str(format("{0:c}", 'z')));
EXPECT_EQ(L"a", str(format(L"{0}", 'a')));
EXPECT_EQ("a", format("{0}", 'a'));
EXPECT_EQ("z", format("{0:c}", 'z'));
EXPECT_EQ(L"a", format(L"{0}", 'a'));
}
TEST(FormatterTest, FormatWChar) {
EXPECT_EQ(L"a", str(format(L"{0}", L'a')));
EXPECT_EQ(L"a", format(L"{0}", L'a'));
// This shouldn't compile:
//format("{}", L'a');
}
TEST(FormatterTest, FormatCString) {
CheckUnknownTypes("test", "s", "string");
EXPECT_EQ("test", str(format("{0}", "test")));
EXPECT_EQ("test", str(format("{0:s}", "test")));
EXPECT_EQ("test", format("{0}", "test"));
EXPECT_EQ("test", format("{0:s}", "test"));
char nonconst[] = "nonconst";
EXPECT_EQ("nonconst", str(format("{0}", nonconst)));
EXPECT_EQ("nonconst", format("{0}", nonconst));
EXPECT_THROW_MSG(format("{0}", reinterpret_cast<const char*>(0)),
FormatError, "string pointer is null");
}
TEST(FormatterTest, FormatPointer) {
CheckUnknownTypes(reinterpret_cast<void*>(0x1234), "p", "pointer");
EXPECT_EQ("0x0", str(format("{0}", reinterpret_cast<void*>(0))));
EXPECT_EQ("0x1234", str(format("{0}", reinterpret_cast<void*>(0x1234))));
EXPECT_EQ("0x1234", str(format("{0:p}", reinterpret_cast<void*>(0x1234))));
EXPECT_EQ("0x0", format("{0}", reinterpret_cast<void*>(0)));
EXPECT_EQ("0x1234", format("{0}", reinterpret_cast<void*>(0x1234)));
EXPECT_EQ("0x1234", format("{0:p}", reinterpret_cast<void*>(0x1234)));
EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'),
str(format("{0}", reinterpret_cast<void*>(~uintptr_t()))));
format("{0}", reinterpret_cast<void*>(~uintptr_t())));
}
TEST(FormatterTest, FormatString) {
EXPECT_EQ("test", str(format("{0}", std::string("test"))));
EXPECT_EQ("test", format("{0}", std::string("test")));
}
TEST(FormatterTest, FormatStringRef) {
EXPECT_EQ("test", str(format("{0}", StringRef("test"))));
EXPECT_EQ("test", format("{0}", StringRef("test")));
}
TEST(FormatterTest, FormatUsingIOStreams) {
EXPECT_EQ("a string", str(format("{0}", TestString("a string"))));
std::string s = str(format("The date is {0}", Date(2012, 12, 9)));
EXPECT_EQ("a string", format("{0}", TestString("a string")));
std::string s = format("The date is {0}", Date(2012, 12, 9));
EXPECT_EQ("The date is 2012-12-9", s);
Date date(2012, 12, 9);
CheckUnknownTypes(date, "", "object");
@ -1328,41 +1323,41 @@ void format(BasicWriter<Char> &w, const fmt::FormatSpec &spec, Answer) {
}
TEST(FormatterTest, CustomFormat) {
EXPECT_EQ("42", str(format("{0}", Answer())));
EXPECT_EQ("42", format("{0}", Answer()));
}
TEST(FormatterTest, WideFormatString) {
EXPECT_EQ(L"42", str(format(L"{}", 42)));
EXPECT_EQ(L"4.2", str(format(L"{}", 4.2)));
EXPECT_EQ(L"abc", str(format(L"{}", L"abc")));
EXPECT_EQ(L"z", str(format(L"{}", L'z')));
EXPECT_EQ(L"42", format(L"{}", 42));
EXPECT_EQ(L"4.2", format(L"{}", 4.2));
EXPECT_EQ(L"abc", format(L"{}", L"abc"));
EXPECT_EQ(L"z", format(L"{}", L'z'));
}
TEST(FormatterTest, FormatStringFromSpeedTest) {
EXPECT_EQ("1.2340000000:0042:+3.13:str:0x3e8:X:%",
str(format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%",
1.234, 42, 3.13, "str", reinterpret_cast<void*>(1000), 'X')));
format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%",
1.234, 42, 3.13, "str", reinterpret_cast<void*>(1000), 'X'));
}
TEST(FormatterTest, FormatExamples) {
using fmt::hex;
EXPECT_EQ("0000cafe", str(BasicWriter<char>() << pad(hex(0xcafe), 8, '0')));
std::string message = str(format("The answer is {}", 42));
std::string message = format("The answer is {}", 42);
EXPECT_EQ("The answer is 42", message);
EXPECT_EQ("42", str(format("{}", 42)));
EXPECT_EQ("42", str(format(std::string("{}"), 42)));
EXPECT_EQ("42", format("{}", 42));
EXPECT_EQ("42", format(std::string("{}"), 42));
Writer out;
out << "The answer is " << 42 << "\n";
out.format("({:+f}, {:+f})", -3.14, 3.14);
out.write("({:+f}, {:+f})", -3.14, 3.14);
EXPECT_EQ("The answer is 42\n(-3.140000, +3.140000)", out.str());
{
fmt::Writer writer;
for (int i = 0; i < 10; i++)
writer.format("{}", i);
writer.write("{}", i);
std::string s = writer.str(); // s == 0123456789
EXPECT_EQ("0123456789", s);
}
@ -1378,11 +1373,6 @@ TEST(FormatterTest, FormatExamples) {
}, error_code, "Cannot open file 'nonexistent'");
}
TEST(FormatterTest, StrNamespace) {
str(format(""));
c_str(format(""));
}
TEST(StringRefTest, Ctor) {
EXPECT_STREQ("abc", StringRef("abc").c_str());
EXPECT_EQ(3u, StringRef("abc").size());
@ -1492,51 +1482,51 @@ fmt::Formatter<PrintError> ReportError(const char *format) {
TEST(FormatterTest, Examples) {
EXPECT_EQ("First, thou shalt count to three",
str(format("First, thou shalt count to {0}", "three")));
format("First, thou shalt count to {0}", "three"));
EXPECT_EQ("Bring me a shrubbery",
str(format("Bring me a {}", "shrubbery")));
EXPECT_EQ("From 1 to 3", str(format("From {} to {}", 1, 3)));
format("Bring me a {}", "shrubbery"));
EXPECT_EQ("From 1 to 3", format("From {} to {}", 1, 3));
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%03.2f", -1.2);
EXPECT_EQ(buffer, str(format("{:03.2f}", -1.2)));
EXPECT_EQ(buffer, format("{:03.2f}", -1.2));
EXPECT_EQ("a, b, c", str(format("{0}, {1}, {2}", 'a', 'b', 'c')));
EXPECT_EQ("a, b, c", str(format("{}, {}, {}", 'a', 'b', 'c')));
EXPECT_EQ("c, b, a", str(format("{2}, {1}, {0}", 'a', 'b', 'c')));
EXPECT_EQ("abracadabra", str(format("{0}{1}{0}", "abra", "cad")));
EXPECT_EQ("a, b, c", format("{0}, {1}, {2}", 'a', 'b', 'c'));
EXPECT_EQ("a, b, c", format("{}, {}, {}", 'a', 'b', 'c'));
EXPECT_EQ("c, b, a", format("{2}, {1}, {0}", 'a', 'b', 'c'));
EXPECT_EQ("abracadabra", format("{0}{1}{0}", "abra", "cad"));
EXPECT_EQ("left aligned ",
str(format("{:<30}", "left aligned")));
format("{:<30}", "left aligned"));
EXPECT_EQ(" right aligned",
str(format("{:>30}", "right aligned")));
format("{:>30}", "right aligned"));
EXPECT_EQ(" centered ",
str(format("{:^30}", "centered")));
format("{:^30}", "centered"));
EXPECT_EQ("***********centered***********",
str(format("{:*^30}", "centered")));
format("{:*^30}", "centered"));
EXPECT_EQ("+3.140000; -3.140000",
str(format("{:+f}; {:+f}", 3.14, -3.14)));
format("{:+f}; {:+f}", 3.14, -3.14));
EXPECT_EQ(" 3.140000; -3.140000",
str(format("{: f}; {: f}", 3.14, -3.14)));
format("{: f}; {: f}", 3.14, -3.14));
EXPECT_EQ("3.140000; -3.140000",
str(format("{:-f}; {:-f}", 3.14, -3.14)));
format("{:-f}; {:-f}", 3.14, -3.14));
EXPECT_EQ("int: 42; hex: 2a; oct: 52",
str(format("int: {0:d}; hex: {0:x}; oct: {0:o}", 42)));
format("int: {0:d}; hex: {0:x}; oct: {0:o}", 42));
EXPECT_EQ("int: 42; hex: 0x2a; oct: 052",
str(format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42)));
format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42));
std::string path = "somefile";
ReportError("File not found: {0}") << path;
EXPECT_EQ("The answer is 42", str(format("The answer is {}", 42)));
EXPECT_EQ("The answer is 42", format("The answer is {}", 42));
EXPECT_THROW_MSG(
format("The answer is {:d}", "forty-two"), FormatError,
"unknown format code 'd' for string");
EXPECT_EQ(L"Cyrillic letter \x42e",
str(format(L"Cyrillic letter {}", L'\x42e')));
format(L"Cyrillic letter {}", L'\x42e'));
EXPECT_WRITE(stdout,
fmt::print("{}", std::numeric_limits<double>::infinity()), "inf");
@ -1601,8 +1591,8 @@ TEST(FormatTest, PrintColored) {
#endif
TEST(FormatTest, Variadic) {
EXPECT_EQ("abc1", str(format("{}c{}", "ab", 1)));
EXPECT_EQ(L"abc1", str(format(L"{}c{}", L"ab", 1)));
EXPECT_EQ("abc1", format("{}c{}", "ab", 1));
EXPECT_EQ(L"abc1", format(L"{}c{}", L"ab", 1));
}
template <typename T>
@ -1619,8 +1609,8 @@ TEST(StrTest, Convert) {
std::string FormatMessage(int id, const char *format,
const fmt::ArgList &args) {
fmt::Writer w;
w.format("[{}] ", id);
w.format(format, args);
w.write("[{}] ", id);
w.write(format, args);
return w.str();
}

View File

@ -305,12 +305,12 @@ TEST(ExpectTest, EXPECT_SYSTEM_ERROR) {
" Actual: it throws nothing.");
EXPECT_NONFATAL_FAILURE(
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "other"),
str(fmt::format(
fmt::format(
"ThrowSystemError() throws an exception with a different message.\n"
"Expected: {}\n"
" Actual: {}",
FormatSystemErrorMessage(EDOM, "other"),
FormatSystemErrorMessage(EDOM, "test"))));
FormatSystemErrorMessage(EDOM, "test")));
}
// Tests EXPECT_WRITE.
@ -687,7 +687,7 @@ TEST(FileTest, Dup2) {
TEST(FileTest, Dup2Error) {
File f = OpenFile();
EXPECT_SYSTEM_ERROR_NOASSERT(f.dup2(-1), EBADF,
str(fmt::format("cannot duplicate file descriptor {} to -1", f.descriptor())));
fmt::format("cannot duplicate file descriptor {} to -1", f.descriptor()));
}
TEST(FileTest, Dup2NoExcept) {
@ -768,7 +768,7 @@ TEST(OutputRedirectTest, DupErrorInCtor) {
FMT_POSIX(close(fd));
OutputRedirect *redir = 0;
EXPECT_SYSTEM_ERROR_NOASSERT(redir = new OutputRedirect(f.get()),
EBADF, str(fmt::format("cannot duplicate file descriptor {}", fd)));
EBADF, fmt::format("cannot duplicate file descriptor {}", fd));
copy.dup2(fd); // "undo" close or dtor will fail
delete redir;
}

View File

@ -246,7 +246,7 @@ TEST(FileTest, DupNoRetry) {
int stdout_fd = FMT_POSIX(fileno(stdout));
dup_count = 1;
EXPECT_SYSTEM_ERROR(File::dup(stdout_fd), EINTR,
str(fmt::format("cannot duplicate file descriptor {}", stdout_fd)));
fmt::format("cannot duplicate file descriptor {}", stdout_fd));
dup_count = 0;
}
@ -254,8 +254,8 @@ TEST(FileTest, Dup2Retry) {
int stdout_fd = FMT_POSIX(fileno(stdout));
File f1 = File::dup(stdout_fd), f2 = File::dup(stdout_fd);
EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
str(fmt::format("cannot duplicate file descriptor {} to {}",
f1.descriptor(), f2.descriptor())));
fmt::format("cannot duplicate file descriptor {} to {}",
f1.descriptor(), f2.descriptor()));
}
TEST(FileTest, Dup2NoExceptRetry) {

View File

@ -53,62 +53,62 @@ std::string MakePositional(fmt::StringRef format) {
}
#define EXPECT_PRINTF(expected_output, format, arg) \
EXPECT_EQ(expected_output, str(fmt::sprintf(format, arg))); \
EXPECT_EQ(expected_output, str(fmt::sprintf(MakePositional(format), arg)))
EXPECT_EQ(expected_output, fmt::sprintf(format, arg)); \
EXPECT_EQ(expected_output, fmt::sprintf(MakePositional(format), arg))
TEST(PrintfTest, NoArgs) {
EXPECT_EQ("test", str(fmt::sprintf("test")));
EXPECT_EQ("test", fmt::sprintf("test"));
}
TEST(PrintfTest, Escape) {
EXPECT_EQ("%", str(fmt::sprintf("%%")));
EXPECT_EQ("before %", str(fmt::sprintf("before %%")));
EXPECT_EQ("% after", str(fmt::sprintf("%% after")));
EXPECT_EQ("before % after", str(fmt::sprintf("before %% after")));
EXPECT_EQ("%s", str(fmt::sprintf("%%s")));
EXPECT_EQ("%", fmt::sprintf("%%"));
EXPECT_EQ("before %", fmt::sprintf("before %%"));
EXPECT_EQ("% after", fmt::sprintf("%% after"));
EXPECT_EQ("before % after", fmt::sprintf("before %% after"));
EXPECT_EQ("%s", fmt::sprintf("%%s"));
}
TEST(PrintfTest, PositionalArgs) {
EXPECT_EQ("42", str(fmt::sprintf("%1$d", 42)));
EXPECT_EQ("before 42", str(fmt::sprintf("before %1$d", 42)));
EXPECT_EQ("42 after", str(fmt::sprintf("%1$d after",42)));
EXPECT_EQ("before 42 after", str(fmt::sprintf("before %1$d after", 42)));
EXPECT_EQ("answer = 42", str(fmt::sprintf("%1$s = %2$d", "answer", 42)));
EXPECT_EQ("42", fmt::sprintf("%1$d", 42));
EXPECT_EQ("before 42", fmt::sprintf("before %1$d", 42));
EXPECT_EQ("42 after", fmt::sprintf("%1$d after",42));
EXPECT_EQ("before 42 after", fmt::sprintf("before %1$d after", 42));
EXPECT_EQ("answer = 42", fmt::sprintf("%1$s = %2$d", "answer", 42));
EXPECT_EQ("42 is the answer",
str(fmt::sprintf("%2$d is the %1$s", "answer", 42)));
EXPECT_EQ("abracadabra", str(fmt::sprintf("%1$s%2$s%1$s", "abra", "cad")));
fmt::sprintf("%2$d is the %1$s", "answer", 42));
EXPECT_EQ("abracadabra", fmt::sprintf("%1$s%2$s%1$s", "abra", "cad"));
}
TEST(PrintfTest, AutomaticArgIndexing) {
EXPECT_EQ("abc", str(fmt::sprintf("%c%c%c", 'a', 'b', 'c')));
EXPECT_EQ("abc", fmt::sprintf("%c%c%c", 'a', 'b', 'c'));
}
TEST(PrintfTest, NumberIsTooBigInArgIndex) {
EXPECT_THROW_MSG(fmt::sprintf(str(format("%{}$", BIG_NUM))),
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$", BIG_NUM)),
FormatError, "invalid format string");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%{}$d", BIG_NUM))),
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", BIG_NUM)),
FormatError, "number is too big in format");
}
TEST(PrintfTest, SwitchArgIndexing) {
EXPECT_THROW_MSG(fmt::sprintf("%1$d%", 1, 2),
FormatError, "invalid format string");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%1$d%{}d", BIG_NUM)), 1, 2),
EXPECT_THROW_MSG(fmt::sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
FormatError, "number is too big in format");
EXPECT_THROW_MSG(fmt::sprintf("%1$d%d", 1, 2),
FormatError, "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG(fmt::sprintf("%d%1$", 1, 2),
FormatError, "invalid format string");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%d%{}$d", BIG_NUM)), 1, 2),
EXPECT_THROW_MSG(fmt::sprintf(format("%d%{}$d", BIG_NUM), 1, 2),
FormatError, "number is too big in format");
EXPECT_THROW_MSG(fmt::sprintf("%d%1$d", 1, 2),
FormatError, "cannot switch from automatic to manual argument indexing");
// Indexing errors override width errors.
EXPECT_THROW_MSG(fmt::sprintf(str(format("%d%1${}d", BIG_NUM)), 1, 2),
EXPECT_THROW_MSG(fmt::sprintf(format("%d%1${}d", BIG_NUM), 1, 2),
FormatError, "number is too big in format");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%1$d%{}d", BIG_NUM)), 1, 2),
EXPECT_THROW_MSG(fmt::sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
FormatError, "number is too big in format");
}
@ -117,12 +117,12 @@ TEST(PrintfTest, InvalidArgIndex) {
"argument index is out of range in format");
EXPECT_THROW_MSG(fmt::sprintf("%2$d", 42), FormatError,
"argument index is out of range in format");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%{}$d", INT_MAX)), 42),
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", INT_MAX), 42),
FormatError, "argument index is out of range in format");
EXPECT_THROW_MSG(fmt::sprintf("%2$", 42),
FormatError, "invalid format string");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%{}$d", BIG_NUM)), 42),
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", BIG_NUM), 42),
FormatError, "number is too big in format");
}
@ -211,15 +211,15 @@ TEST(PrintfTest, Width) {
EXPECT_THROW_MSG(fmt::sprintf("%5-5d", 42), FormatError,
"unknown format code '-' for integer");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%{}d", BIG_NUM)), 42),
EXPECT_THROW_MSG(fmt::sprintf(format("%{}d", BIG_NUM), 42),
FormatError, "number is too big in format");
EXPECT_THROW_MSG(fmt::sprintf(str(format("%1${}d", BIG_NUM)), 42),
EXPECT_THROW_MSG(fmt::sprintf(format("%1${}d", BIG_NUM), 42),
FormatError, "number is too big in format");
}
TEST(PrintfTest, DynamicWidth) {
EXPECT_EQ(" 42", str(fmt::sprintf("%*d", 5, 42)));
EXPECT_EQ("42 ", str(fmt::sprintf("%*d", -5, 42)));
EXPECT_EQ(" 42", fmt::sprintf("%*d", 5, 42));
EXPECT_EQ("42 ", fmt::sprintf("%*d", -5, 42));
EXPECT_THROW_MSG(fmt::sprintf("%*d", 5.0, 42), FormatError,
"width is not integer");
EXPECT_THROW_MSG(fmt::sprintf("%*d"), FormatError,
@ -265,9 +265,9 @@ TEST(PrintfTest, IgnorePrecisionForNonNumericArg) {
}
TEST(PrintfTest, DynamicPrecision) {
EXPECT_EQ("00042", str(fmt::sprintf("%.*d", 5, 42)));
EXPECT_EQ("00042", fmt::sprintf("%.*d", 5, 42));
// TODO
//EXPECT_EQ("42", str(fmt::sprintf("%.*d", -5, 42)));
//EXPECT_EQ("42", fmt::sprintf("%.*d", -5, 42));
EXPECT_THROW_MSG(fmt::sprintf("%.*d", 5.0, 42), FormatError,
"precision is not integer");
EXPECT_THROW_MSG(fmt::sprintf("%.*d"), FormatError,

View File

@ -206,8 +206,8 @@ void CheckThrowError(int error_code, FormatErrorMessage format,
TEST(UtilTest, FormatSystemErrorMessage) {
fmt::Writer message;
fmt::internal::FormatSystemErrorMessage(message, EDOM, "test");
EXPECT_EQ(str(fmt::format("test: {}",
GetSystemErrorMessage(EDOM))), fmt::str(message));
EXPECT_EQ(fmt::format("test: {}",
GetSystemErrorMessage(EDOM)), fmt::str(message));
}
TEST(UtilTest, SystemErrorSink) {
@ -240,7 +240,7 @@ TEST(UtilTest, FormatWinErrorMessage) {
fmt::Writer actual_message;
fmt::internal::FormatWinErrorMessage(
actual_message, ERROR_FILE_EXISTS, "test");
EXPECT_EQ(str(fmt::format("test: {}", fmt::str(utf8_message))),
EXPECT_EQ(fmt::format("test: {}", fmt::str(utf8_message)),
fmt::str(actual_message));
}