mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-25 19:50:05 +00:00
Cleanup tests
This commit is contained in:
parent
ed7c4320f6
commit
c738c3431f
@ -13,8 +13,7 @@ struct test {};
|
||||
// included after fmt/format.h.
|
||||
namespace fmt {
|
||||
template <> struct formatter<test> : formatter<int> {
|
||||
template <typename FormatContext>
|
||||
typename FormatContext::iterator format(const test&, FormatContext& ctx) {
|
||||
auto format(const test&, format_context& ctx) -> decltype(ctx.out()) {
|
||||
return formatter<int>::format(42, ctx);
|
||||
}
|
||||
};
|
||||
@ -28,15 +27,12 @@ template <> struct formatter<test> : formatter<int> {
|
||||
#include "gtest-extra.h"
|
||||
#include "util.h"
|
||||
|
||||
using fmt::format;
|
||||
using fmt::format_error;
|
||||
|
||||
static std::ostream& operator<<(std::ostream& os, const Date& d) {
|
||||
std::ostream& operator<<(std::ostream& os, const Date& d) {
|
||||
os << d.year() << '-' << d.month() << '-' << d.day();
|
||||
return os;
|
||||
}
|
||||
|
||||
static std::wostream& operator<<(std::wostream& os, const Date& d) {
|
||||
std::wostream& operator<<(std::wostream& os, const Date& d) {
|
||||
os << d.year() << L'-' << d.month() << L'-' << d.day();
|
||||
return os;
|
||||
}
|
||||
@ -47,63 +43,62 @@ template <typename T> void operator,(type_with_comma_op, const T&);
|
||||
template <typename T> type_with_comma_op operator<<(T&, const Date&);
|
||||
|
||||
enum streamable_enum {};
|
||||
static std::ostream& operator<<(std::ostream& os, streamable_enum) {
|
||||
std::ostream& operator<<(std::ostream& os, streamable_enum) {
|
||||
return os << "streamable_enum";
|
||||
}
|
||||
|
||||
static std::wostream& operator<<(std::wostream& os, streamable_enum) {
|
||||
std::wostream& operator<<(std::wostream& os, streamable_enum) {
|
||||
return os << L"streamable_enum";
|
||||
}
|
||||
|
||||
enum unstreamable_enum {};
|
||||
|
||||
TEST(OStreamTest, Enum) {
|
||||
TEST(ostream_test, enum) {
|
||||
EXPECT_EQ("streamable_enum", fmt::format("{}", streamable_enum()));
|
||||
EXPECT_EQ("0", fmt::format("{}", unstreamable_enum()));
|
||||
EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));
|
||||
EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, Format) {
|
||||
EXPECT_EQ("a string", format("{0}", TestString("a string")));
|
||||
std::string s = format("The date is {0}", Date(2012, 12, 9));
|
||||
TEST(ostream_test, format) {
|
||||
EXPECT_EQ("a string", fmt::format("{0}", TestString("a string")));
|
||||
std::string s = fmt::format("The date is {0}", Date(2012, 12, 9));
|
||||
EXPECT_EQ("The date is 2012-12-9", s);
|
||||
Date date(2012, 12, 9);
|
||||
EXPECT_EQ(L"The date is 2012-12-9",
|
||||
format(L"The date is {0}", Date(2012, 12, 9)));
|
||||
fmt::format(L"The date is {0}", Date(2012, 12, 9)));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, FormatSpecs) {
|
||||
EXPECT_EQ("def ", format("{0:<5}", TestString("def")));
|
||||
EXPECT_EQ(" def", format("{0:>5}", TestString("def")));
|
||||
EXPECT_EQ(" def ", format("{0:^5}", TestString("def")));
|
||||
EXPECT_EQ("def**", format("{0:*<5}", TestString("def")));
|
||||
EXPECT_THROW_MSG(format(+"{0:+}", TestString()), format_error,
|
||||
TEST(ostream_test, format_specs) {
|
||||
using fmt::format_error;
|
||||
EXPECT_EQ("def ", fmt::format("{0:<5}", TestString("def")));
|
||||
EXPECT_EQ(" def", fmt::format("{0:>5}", TestString("def")));
|
||||
EXPECT_EQ(" def ", fmt::format("{0:^5}", TestString("def")));
|
||||
EXPECT_EQ("def**", fmt::format("{0:*<5}", TestString("def")));
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0:+}", TestString()), format_error,
|
||||
"format specifier requires numeric argument");
|
||||
EXPECT_THROW_MSG(format(+"{0:-}", TestString()), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0:-}", TestString()), format_error,
|
||||
"format specifier requires numeric argument");
|
||||
EXPECT_THROW_MSG(format(+"{0: }", TestString()), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0: }", TestString()), format_error,
|
||||
"format specifier requires numeric argument");
|
||||
EXPECT_THROW_MSG(format(+"{0:#}", TestString()), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0:#}", TestString()), format_error,
|
||||
"format specifier requires numeric argument");
|
||||
EXPECT_THROW_MSG(format(+"{0:05}", TestString()), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0:05}", TestString()), format_error,
|
||||
"format specifier requires numeric argument");
|
||||
EXPECT_EQ("test ", format("{0:13}", TestString("test")));
|
||||
EXPECT_EQ("test ", format("{0:{1}}", TestString("test"), 13));
|
||||
EXPECT_EQ("te", format("{0:.2}", TestString("test")));
|
||||
EXPECT_EQ("te", format("{0:.{1}}", TestString("test"), 2));
|
||||
EXPECT_EQ("test ", fmt::format("{0:13}", TestString("test")));
|
||||
EXPECT_EQ("test ", fmt::format("{0:{1}}", TestString("test"), 13));
|
||||
EXPECT_EQ("te", fmt::format("{0:.2}", TestString("test")));
|
||||
EXPECT_EQ("te", fmt::format("{0:.{1}}", TestString("test"), 2));
|
||||
}
|
||||
|
||||
struct EmptyTest {};
|
||||
static std::ostream& operator<<(std::ostream& os, EmptyTest) {
|
||||
return os << "";
|
||||
struct empty_test {};
|
||||
std::ostream& operator<<(std::ostream& os, empty_test) { return os << ""; }
|
||||
|
||||
TEST(ostream_test, empty_custom_output) {
|
||||
EXPECT_EQ("", fmt::format("{}", empty_test()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, EmptyCustomOutput) {
|
||||
EXPECT_EQ("", fmt::format("{}", EmptyTest()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, Print) {
|
||||
TEST(ostream_test, print) {
|
||||
std::ostringstream os;
|
||||
fmt::print(os, "Don't {}!", "panic");
|
||||
EXPECT_EQ("Don't panic!", os.str());
|
||||
@ -112,7 +107,7 @@ TEST(OStreamTest, Print) {
|
||||
EXPECT_EQ(L"Don't panic!", wos.str());
|
||||
}
|
||||
|
||||
TEST(OStreamTest, WriteToOStream) {
|
||||
TEST(ostream_test, write_to_ostream) {
|
||||
std::ostringstream os;
|
||||
fmt::memory_buffer buffer;
|
||||
const char* foo = "foo";
|
||||
@ -121,9 +116,9 @@ TEST(OStreamTest, WriteToOStream) {
|
||||
EXPECT_EQ("foo", os.str());
|
||||
}
|
||||
|
||||
TEST(OStreamTest, WriteToOStreamMaxSize) {
|
||||
size_t max_size = fmt::detail::max_value<size_t>();
|
||||
std::streamsize max_streamsize = fmt::detail::max_value<std::streamsize>();
|
||||
TEST(ostream_test, write_to_ostream_max_size) {
|
||||
auto max_size = fmt::detail::max_value<size_t>();
|
||||
auto max_streamsize = fmt::detail::max_value<std::streamsize>();
|
||||
if (max_size <= fmt::detail::to_unsigned(max_streamsize)) return;
|
||||
|
||||
struct test_buffer final : fmt::detail::buffer<char> {
|
||||
@ -147,7 +142,7 @@ TEST(OStreamTest, WriteToOStreamMaxSize) {
|
||||
|
||||
testing::InSequence sequence;
|
||||
const char* data = nullptr;
|
||||
typedef std::make_unsigned<std::streamsize>::type ustreamsize;
|
||||
using ustreamsize = std::make_unsigned<std::streamsize>::type;
|
||||
ustreamsize size = max_size;
|
||||
do {
|
||||
auto n = std::min(size, fmt::detail::to_unsigned(max_streamsize));
|
||||
@ -159,73 +154,62 @@ TEST(OStreamTest, WriteToOStreamMaxSize) {
|
||||
fmt::detail::write_buffer(os, buffer);
|
||||
}
|
||||
|
||||
TEST(OStreamTest, Join) {
|
||||
TEST(ostream_test, join) {
|
||||
int v[3] = {1, 2, 3};
|
||||
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, v + 3, ", ")));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, JoinFallbackFormatter) {
|
||||
TEST(ostream_test, join_fallback_formatter) {
|
||||
auto strs = std::vector<TestString>{TestString("foo"), TestString("bar")};
|
||||
EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", ")));
|
||||
}
|
||||
|
||||
#if FMT_USE_CONSTEXPR
|
||||
TEST(OStreamTest, ConstexprString) {
|
||||
TEST(ostream_test, constexpr_string) {
|
||||
EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));
|
||||
EXPECT_EQ("a string", format(FMT_STRING("{0}"), TestString("a string")));
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace fmt_test {
|
||||
struct ABC {};
|
||||
struct abc {};
|
||||
|
||||
template <typename Output> Output& operator<<(Output& out, ABC) {
|
||||
out << "ABC";
|
||||
return out;
|
||||
template <typename Output> Output& operator<<(Output& out, abc) {
|
||||
return out << "abc";
|
||||
}
|
||||
} // namespace fmt_test
|
||||
|
||||
template <typename T> struct TestTemplate {};
|
||||
template <typename T> struct test_template {};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
|
||||
std::ostream& operator<<(std::ostream& os, test_template<T>) {
|
||||
return os << 1;
|
||||
}
|
||||
|
||||
namespace fmt {
|
||||
template <typename T> struct formatter<TestTemplate<T>> : formatter<int> {
|
||||
template <typename FormatContext>
|
||||
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
|
||||
template <typename T> struct formatter<test_template<T>> : formatter<int> {
|
||||
auto format(test_template<T>, format_context& ctx) -> decltype(ctx.out()) {
|
||||
return formatter<int>::format(2, ctx);
|
||||
}
|
||||
};
|
||||
} // namespace fmt
|
||||
|
||||
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 407
|
||||
TEST(OStreamTest, Template) {
|
||||
EXPECT_EQ("2", fmt::format("{}", TestTemplate<int>()));
|
||||
TEST(ostream_test, template) {
|
||||
EXPECT_EQ("2", fmt::format("{}", test_template<int>()));
|
||||
}
|
||||
|
||||
TEST(FormatTest, FormatToN) {
|
||||
TEST(ostream_test, format_to_n) {
|
||||
char buffer[4];
|
||||
buffer[3] = 'x';
|
||||
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::ABC());
|
||||
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::abc());
|
||||
EXPECT_EQ(3u, result.size);
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("ABCx", fmt::string_view(buffer, 4));
|
||||
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::ABC());
|
||||
EXPECT_EQ("abcx", fmt::string_view(buffer, 4));
|
||||
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::abc());
|
||||
EXPECT_EQ(5u, result.size);
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("xABx", fmt::string_view(buffer, 4));
|
||||
EXPECT_EQ("xabx", fmt::string_view(buffer, 4));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if FMT_USE_USER_DEFINED_LITERALS
|
||||
TEST(FormatTest, UDL) {
|
||||
using namespace fmt::literals;
|
||||
EXPECT_EQ("{}"_format("test"), "test");
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T> struct convertible {
|
||||
T value;
|
||||
@ -233,7 +217,7 @@ template <typename T> struct convertible {
|
||||
operator T() const { return value; }
|
||||
};
|
||||
|
||||
TEST(OStreamTest, DisableBuiltinOStreamOperators) {
|
||||
TEST(ostream_test, disable_builtin_ostream_operators) {
|
||||
EXPECT_EQ("42", fmt::format("{:d}", convertible<unsigned short>(42)));
|
||||
EXPECT_EQ(L"42", fmt::format(L"{:d}", convertible<unsigned short>(42)));
|
||||
EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
|
||||
@ -253,7 +237,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||
return os << "bar";
|
||||
}
|
||||
|
||||
TEST(OStreamTest, FormatExplicitlyConvertibleToStringLike) {
|
||||
TEST(ostream_test, format_explicitly_convertible_to_string_like) {
|
||||
EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
|
||||
}
|
||||
|
||||
@ -269,7 +253,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||
return os << "bar";
|
||||
}
|
||||
|
||||
TEST(OStreamTest, FormatExplicitlyConvertibleToStdStringView) {
|
||||
TEST(ostream_test, format_explicitly_convertible_to_std_string_view) {
|
||||
EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
|
||||
}
|
||||
#endif // FMT_USE_STRING_VIEW
|
||||
@ -282,7 +266,7 @@ std::ostream& operator<<(std::ostream& os, streamable_and_convertible_to_bool) {
|
||||
return os << "foo";
|
||||
}
|
||||
|
||||
TEST(OStreamTest, FormatConvertibleToBool) {
|
||||
TEST(ostream_test, format_convertible_to_bool) {
|
||||
EXPECT_EQ("foo", fmt::format("{}", streamable_and_convertible_to_bool()));
|
||||
}
|
||||
|
||||
@ -294,19 +278,15 @@ std::ostream& operator<<(std::ostream& os, copyfmt_test) {
|
||||
return os << "foo";
|
||||
}
|
||||
|
||||
TEST(OStreamTest, CopyFmt) {
|
||||
TEST(ostream_test, copyfmt) {
|
||||
EXPECT_EQ("foo", fmt::format("{}", copyfmt_test()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, CompileTimeString) {
|
||||
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
|
||||
TEST(ostream_test, to_string) {
|
||||
EXPECT_EQ("abc", fmt::to_string(fmt_test::abc()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, ToString) {
|
||||
EXPECT_EQ("ABC", fmt::to_string(fmt_test::ABC()));
|
||||
}
|
||||
|
||||
TEST(OStreamTest, Range) {
|
||||
TEST(ostream_test, range) {
|
||||
auto strs = std::vector<TestString>{TestString("foo"), TestString("bar")};
|
||||
EXPECT_EQ("{foo, bar}", format("{}", strs));
|
||||
EXPECT_EQ("{foo, bar}", fmt::format("{}", strs));
|
||||
}
|
||||
|
@ -23,7 +23,6 @@
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
# undef max
|
||||
# undef ERROR
|
||||
#endif
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
@ -37,9 +36,9 @@ using testing::_;
|
||||
using testing::Return;
|
||||
using testing::StrEq;
|
||||
|
||||
template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> {
|
||||
ScopedMock() { Mock::instance = this; }
|
||||
~ScopedMock() { Mock::instance = nullptr; }
|
||||
template <typename Mock> struct scoped_mock : testing::StrictMock<Mock> {
|
||||
scoped_mock() { Mock::instance = this; }
|
||||
~scoped_mock() { Mock::instance = nullptr; }
|
||||
};
|
||||
|
||||
namespace {
|
||||
@ -58,7 +57,7 @@ size_t read_nbyte;
|
||||
size_t write_nbyte;
|
||||
bool sysconf_error;
|
||||
|
||||
enum { NONE, MAX_SIZE, ERROR } fstat_sim;
|
||||
enum { none, max_size, error } fstat_sim;
|
||||
} // namespace
|
||||
|
||||
#define EMULATE_EINTR(func, error_result) \
|
||||
@ -96,7 +95,7 @@ static off_t max_file_size() { return std::numeric_limits<off_t>::max(); }
|
||||
|
||||
int test::fstat(int fd, struct stat* buf) {
|
||||
int result = ::fstat(fd, buf);
|
||||
if (fstat_sim == MAX_SIZE) buf->st_size = max_file_size();
|
||||
if (fstat_sim == max_size) buf->st_size = max_file_size();
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -105,11 +104,11 @@ int test::fstat(int fd, struct stat* buf) {
|
||||
static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); }
|
||||
|
||||
DWORD test::GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
|
||||
if (fstat_sim == ERROR) {
|
||||
if (fstat_sim == error) {
|
||||
SetLastError(ERROR_ACCESS_DENIED);
|
||||
return INVALID_FILE_SIZE;
|
||||
}
|
||||
if (fstat_sim == MAX_SIZE) {
|
||||
if (fstat_sim == max_size) {
|
||||
DWORD max = std::numeric_limits<DWORD>::max();
|
||||
*lpFileSizeHigh = max >> 1;
|
||||
return max;
|
||||
@ -200,14 +199,14 @@ int(test::fileno)(FILE* stream) {
|
||||
#endif
|
||||
|
||||
#if FMT_USE_FCNTL
|
||||
static void write_file(fmt::cstring_view filename, fmt::string_view content) {
|
||||
void write_file(fmt::cstring_view filename, fmt::string_view content) {
|
||||
fmt::buffered_file f(filename, "w");
|
||||
f.print("{}", content);
|
||||
}
|
||||
|
||||
using fmt::file;
|
||||
|
||||
TEST(UtilTest, GetPageSize) {
|
||||
TEST(os_test, getpagesize) {
|
||||
# ifdef _WIN32
|
||||
SYSTEM_INFO si = {};
|
||||
GetSystemInfo(&si);
|
||||
@ -221,7 +220,7 @@ TEST(UtilTest, GetPageSize) {
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(FileTest, OpenRetry) {
|
||||
TEST(file_test, open_retry) {
|
||||
write_file("temp", "there must be something here");
|
||||
std::unique_ptr<file> f{nullptr};
|
||||
EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open,
|
||||
@ -232,7 +231,7 @@ TEST(FileTest, OpenRetry) {
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseNoRetryInDtor) {
|
||||
TEST(file_test, close_no_retry_in_dtor) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
std::unique_ptr<file> f(new file(std::move(read_end)));
|
||||
@ -249,7 +248,7 @@ TEST(FileTest, CloseNoRetryInDtor) {
|
||||
EXPECT_EQ(2, saved_close_count);
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseNoRetry) {
|
||||
TEST(file_test, close_no_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
close_count = 1;
|
||||
@ -258,7 +257,7 @@ TEST(FileTest, CloseNoRetry) {
|
||||
close_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, Size) {
|
||||
TEST(file_test, size) {
|
||||
std::string content = "top secret, destroy before reading";
|
||||
write_file("temp", content);
|
||||
file f("temp", file::RDONLY);
|
||||
@ -268,25 +267,25 @@ TEST(FileTest, Size) {
|
||||
fmt::memory_buffer message;
|
||||
fmt::detail::format_windows_error(message, ERROR_ACCESS_DENIED,
|
||||
"cannot get file size");
|
||||
fstat_sim = ERROR;
|
||||
fstat_sim = error;
|
||||
EXPECT_THROW_MSG(f.size(), fmt::windows_error, fmt::to_string(message));
|
||||
fstat_sim = NONE;
|
||||
fstat_sim = none;
|
||||
# else
|
||||
f.close();
|
||||
EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(FileTest, MaxSize) {
|
||||
TEST(file_test, max_size) {
|
||||
write_file("temp", "");
|
||||
file f("temp", file::RDONLY);
|
||||
fstat_sim = MAX_SIZE;
|
||||
fstat_sim = max_size;
|
||||
EXPECT_GE(f.size(), 0);
|
||||
EXPECT_EQ(max_file_size(), f.size());
|
||||
fstat_sim = NONE;
|
||||
fstat_sim = none;
|
||||
}
|
||||
|
||||
TEST(FileTest, ReadRetry) {
|
||||
TEST(file_test, read_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
enum { SIZE = 4 };
|
||||
@ -299,7 +298,7 @@ TEST(FileTest, ReadRetry) {
|
||||
EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
|
||||
}
|
||||
|
||||
TEST(FileTest, WriteRetry) {
|
||||
TEST(file_test, write_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
enum { SIZE = 4 };
|
||||
@ -317,7 +316,7 @@ TEST(FileTest, WriteRetry) {
|
||||
}
|
||||
|
||||
# ifdef _WIN32
|
||||
TEST(FileTest, ConvertReadCount) {
|
||||
TEST(file_test, convert_read_count) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
char c;
|
||||
@ -330,7 +329,7 @@ TEST(FileTest, ConvertReadCount) {
|
||||
EXPECT_EQ(UINT_MAX, read_nbyte);
|
||||
}
|
||||
|
||||
TEST(FileTest, ConvertWriteCount) {
|
||||
TEST(file_test, convert_write_count) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
char c;
|
||||
@ -344,7 +343,7 @@ TEST(FileTest, ConvertWriteCount) {
|
||||
}
|
||||
# endif
|
||||
|
||||
TEST(FileTest, DupNoRetry) {
|
||||
TEST(file_test, dup_no_retry) {
|
||||
int stdout_fd = FMT_POSIX(fileno(stdout));
|
||||
dup_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(
|
||||
@ -353,7 +352,7 @@ TEST(FileTest, DupNoRetry) {
|
||||
dup_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2Retry) {
|
||||
TEST(file_test, dup2_retry) {
|
||||
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,
|
||||
@ -361,7 +360,7 @@ TEST(FileTest, Dup2Retry) {
|
||||
f1.descriptor(), f2.descriptor()));
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2NoExceptRetry) {
|
||||
TEST(file_test, dup2_no_except_retry) {
|
||||
int stdout_fd = FMT_POSIX(fileno(stdout));
|
||||
file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
|
||||
error_code ec;
|
||||
@ -375,7 +374,7 @@ TEST(FileTest, Dup2NoExceptRetry) {
|
||||
dup2_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, PipeNoRetry) {
|
||||
TEST(file_test, pipe_no_retry) {
|
||||
file read_end, write_end;
|
||||
pipe_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(file::pipe(read_end, write_end), EINTR,
|
||||
@ -383,7 +382,7 @@ TEST(FileTest, PipeNoRetry) {
|
||||
pipe_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, FdopenNoRetry) {
|
||||
TEST(file_test, fdopen_no_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
fdopen_count = 1;
|
||||
@ -392,7 +391,7 @@ TEST(FileTest, FdopenNoRetry) {
|
||||
fdopen_count = 0;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, OpenRetry) {
|
||||
TEST(buffered_file_test, open_retry) {
|
||||
write_file("temp", "there must be something here");
|
||||
std::unique_ptr<buffered_file> f{nullptr};
|
||||
EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen,
|
||||
@ -404,7 +403,7 @@ TEST(BufferedFileTest, OpenRetry) {
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
||||
TEST(buffered_file_test, close_no_retry_in_dtor) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
|
||||
@ -421,7 +420,7 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
||||
EXPECT_EQ(2, saved_fclose_count);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseNoRetry) {
|
||||
TEST(buffered_file_test, close_no_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
buffered_file f = read_end.fdopen("r");
|
||||
@ -431,7 +430,7 @@ TEST(BufferedFileTest, CloseNoRetry) {
|
||||
fclose_count = 0;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, FilenoNoRetry) {
|
||||
TEST(buffered_file_test, fileno_no_retry) {
|
||||
file read_end, write_end;
|
||||
file::pipe(read_end, write_end);
|
||||
buffered_file f = read_end.fdopen("r");
|
||||
@ -446,9 +445,9 @@ struct test_mock {
|
||||
static test_mock* instance;
|
||||
} * test_mock::instance;
|
||||
|
||||
TEST(ScopedMock, Scope) {
|
||||
TEST(scoped_mock, scope) {
|
||||
{
|
||||
ScopedMock<test_mock> mock;
|
||||
scoped_mock<test_mock> mock;
|
||||
EXPECT_EQ(&mock, test_mock::instance);
|
||||
test_mock& copy = mock;
|
||||
static_cast<void>(copy);
|
||||
@ -458,7 +457,7 @@ TEST(ScopedMock, Scope) {
|
||||
|
||||
#ifdef FMT_LOCALE
|
||||
|
||||
typedef fmt::locale::type locale_type;
|
||||
using locale_type = fmt::locale::type;
|
||||
|
||||
struct locale_mock {
|
||||
static locale_mock* instance;
|
||||
@ -526,20 +525,20 @@ locale_t test::newlocale(int category_mask, const char* locale, locale_t base) {
|
||||
return locale_mock::instance->newlocale(category_mask, locale, base);
|
||||
}
|
||||
|
||||
TEST(LocaleTest, LocaleMock) {
|
||||
ScopedMock<locale_mock> mock;
|
||||
locale_type locale = reinterpret_cast<locale_type>(11);
|
||||
TEST(locale_test, locale_mock) {
|
||||
scoped_mock<locale_mock> mock;
|
||||
auto locale = reinterpret_cast<locale_type>(11);
|
||||
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
|
||||
FMT_SYSTEM(newlocale(222, "foo", locale));
|
||||
}
|
||||
# endif
|
||||
|
||||
TEST(LocaleTest, Locale) {
|
||||
TEST(locale_test, locale) {
|
||||
# ifndef LC_NUMERIC_MASK
|
||||
enum { LC_NUMERIC_MASK = LC_NUMERIC };
|
||||
# endif
|
||||
ScopedMock<locale_mock> mock;
|
||||
locale_type impl = reinterpret_cast<locale_type>(42);
|
||||
scoped_mock<locale_mock> mock;
|
||||
auto impl = reinterpret_cast<locale_type>(42);
|
||||
EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), nullptr))
|
||||
.WillOnce(Return(impl));
|
||||
EXPECT_CALL(mock, freelocale(impl));
|
||||
@ -547,8 +546,8 @@ TEST(LocaleTest, Locale) {
|
||||
EXPECT_EQ(impl, loc.get());
|
||||
}
|
||||
|
||||
TEST(LocaleTest, Strtod) {
|
||||
ScopedMock<locale_mock> mock;
|
||||
TEST(locale_test, strtod) {
|
||||
scoped_mock<locale_mock> mock;
|
||||
EXPECT_CALL(mock, newlocale(_, _, _))
|
||||
.WillOnce(Return(reinterpret_cast<locale_type>(42)));
|
||||
EXPECT_CALL(mock, freelocale(_));
|
||||
|
@ -20,7 +20,7 @@ using fmt::format;
|
||||
using fmt::format_error;
|
||||
using fmt::detail::max_value;
|
||||
|
||||
const unsigned BIG_NUM = INT_MAX + 1u;
|
||||
const unsigned big_num = INT_MAX + 1u;
|
||||
|
||||
// Makes format string argument positional.
|
||||
static std::string make_positional(fmt::string_view format) {
|
||||
@ -70,7 +70,7 @@ template <typename T> struct value_extractor {
|
||||
#endif
|
||||
};
|
||||
|
||||
TEST(PrintfTest, ArgConverter) {
|
||||
TEST(printf_test, arg_converter) {
|
||||
long long value = max_value<long long>();
|
||||
auto arg = fmt::detail::make_arg<fmt::format_context>(value);
|
||||
fmt::visit_format_arg(
|
||||
@ -79,12 +79,12 @@ TEST(PrintfTest, ArgConverter) {
|
||||
EXPECT_EQ(value, fmt::visit_format_arg(value_extractor<long long>(), arg));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, NoArgs) {
|
||||
TEST(printf_test, no_args) {
|
||||
EXPECT_EQ("test", test_sprintf("test"));
|
||||
EXPECT_EQ(L"test", fmt::sprintf(L"test"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Escape) {
|
||||
TEST(printf_test, escape) {
|
||||
EXPECT_EQ("%", test_sprintf("%%"));
|
||||
EXPECT_EQ("before %", test_sprintf("before %%"));
|
||||
EXPECT_EQ("% after", test_sprintf("%% after"));
|
||||
@ -97,7 +97,7 @@ TEST(PrintfTest, Escape) {
|
||||
EXPECT_EQ(L"%s", fmt::sprintf(L"%%s"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PositionalArgs) {
|
||||
TEST(printf_test, positional_args) {
|
||||
EXPECT_EQ("42", test_sprintf("%1$d", 42));
|
||||
EXPECT_EQ("before 42", test_sprintf("before %1$d", 42));
|
||||
EXPECT_EQ("42 after", test_sprintf("%1$d after", 42));
|
||||
@ -107,40 +107,40 @@ TEST(PrintfTest, PositionalArgs) {
|
||||
EXPECT_EQ("abracadabra", test_sprintf("%1$s%2$s%1$s", "abra", "cad"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, AutomaticArgIndexing) {
|
||||
TEST(printf_test, automatic_arg_indexing) {
|
||||
EXPECT_EQ("abc", test_sprintf("%c%c%c", 'a', 'b', 'c'));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, NumberIsTooBigInArgIndex) {
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%{}$", BIG_NUM)), format_error,
|
||||
TEST(printf_test, number_is_too_big_in_arg_index) {
|
||||
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,
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num)), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, SwitchArgIndexing) {
|
||||
TEST(printf_test, switch_arg_indexing) {
|
||||
EXPECT_THROW_MSG(test_sprintf("%1$d%", 1, 2), format_error,
|
||||
"cannot switch from manual to automatic argument indexing");
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
|
||||
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");
|
||||
|
||||
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,
|
||||
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");
|
||||
|
||||
// Indexing errors override width errors.
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", BIG_NUM), 1, 2),
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", big_num), 1, 2),
|
||||
format_error, "number is too big");
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
|
||||
format_error, "number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, InvalidArgIndex) {
|
||||
TEST(printf_test, invalid_arg_index) {
|
||||
EXPECT_THROW_MSG(test_sprintf("%0$d", 42), format_error,
|
||||
"argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$d", 42), format_error,
|
||||
@ -149,16 +149,16 @@ TEST(PrintfTest, InvalidArgIndex) {
|
||||
"argument not found");
|
||||
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$", 42), format_error, "argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", BIG_NUM), 42), format_error,
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num), 42), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DefaultAlignRight) {
|
||||
TEST(printf_test, default_align_right) {
|
||||
EXPECT_PRINTF(" 42", "%5d", 42);
|
||||
EXPECT_PRINTF(" abc", "%5s", "abc");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, ZeroFlag) {
|
||||
TEST(printf_test, zero_flag) {
|
||||
EXPECT_PRINTF("00042", "%05d", 42);
|
||||
EXPECT_PRINTF("-0042", "%05d", -42);
|
||||
|
||||
@ -175,7 +175,7 @@ TEST(PrintfTest, ZeroFlag) {
|
||||
EXPECT_PRINTF(" x", "%05c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PlusFlag) {
|
||||
TEST(printf_test, plus_flag) {
|
||||
EXPECT_PRINTF("+42", "%+d", 42);
|
||||
EXPECT_PRINTF("-42", "%+d", -42);
|
||||
EXPECT_PRINTF("+0042", "%+05d", 42);
|
||||
@ -197,7 +197,7 @@ TEST(PrintfTest, PlusFlag) {
|
||||
EXPECT_PRINTF("x", "% +c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, MinusFlag) {
|
||||
TEST(printf_test, minus_flag) {
|
||||
EXPECT_PRINTF("abc ", "%-5s", "abc");
|
||||
EXPECT_PRINTF("abc ", "%0--5s", "abc");
|
||||
|
||||
@ -217,7 +217,7 @@ TEST(PrintfTest, MinusFlag) {
|
||||
EXPECT_PRINTF(" 42", "%- d", 42);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, SpaceFlag) {
|
||||
TEST(printf_test, space_flag) {
|
||||
EXPECT_PRINTF(" 42", "% d", 42);
|
||||
EXPECT_PRINTF("-42", "% d", -42);
|
||||
EXPECT_PRINTF(" 0042", "% 05d", 42);
|
||||
@ -227,7 +227,7 @@ TEST(PrintfTest, SpaceFlag) {
|
||||
EXPECT_PRINTF("x", "% c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, HashFlag) {
|
||||
TEST(printf_test, hash_flag) {
|
||||
EXPECT_PRINTF("042", "%#o", 042);
|
||||
EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
|
||||
EXPECT_PRINTF("0", "%#o", 0);
|
||||
@ -262,30 +262,30 @@ TEST(PrintfTest, HashFlag) {
|
||||
EXPECT_PRINTF("x", "%#c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Width) {
|
||||
TEST(printf_test, width) {
|
||||
EXPECT_PRINTF(" abc", "%5s", "abc");
|
||||
|
||||
// Width cannot be specified twice.
|
||||
EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
|
||||
"invalid type specifier");
|
||||
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%{}d", BIG_NUM), 42), format_error,
|
||||
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,
|
||||
EXPECT_THROW_MSG(test_sprintf(format("%1${}d", big_num), 42), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DynamicWidth) {
|
||||
TEST(printf_test, dynamic_width) {
|
||||
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,
|
||||
"width is not integer");
|
||||
EXPECT_THROW_MSG(test_sprintf("%*d"), format_error, "argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf("%*d", BIG_NUM, 42), format_error,
|
||||
EXPECT_THROW_MSG(test_sprintf("%*d", big_num, 42), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, IntPrecision) {
|
||||
TEST(printf_test, int_precision) {
|
||||
EXPECT_PRINTF("00042", "%.5d", 42);
|
||||
EXPECT_PRINTF("-00042", "%.5d", -42);
|
||||
EXPECT_PRINTF("00042", "%.5x", 0x42);
|
||||
@ -306,7 +306,7 @@ TEST(PrintfTest, IntPrecision) {
|
||||
EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, FloatPrecision) {
|
||||
TEST(printf_test, float_precision) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%.3e", 1234.5678);
|
||||
EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
|
||||
@ -316,22 +316,22 @@ TEST(PrintfTest, FloatPrecision) {
|
||||
EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, StringPrecision) {
|
||||
TEST(printf_test, string_precision) {
|
||||
char test[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
EXPECT_EQ(fmt::sprintf("%.4s", test), "Hell");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, IgnorePrecisionForNonNumericArg) {
|
||||
TEST(printf_test, ignore_precision_for_non_numeric_arg) {
|
||||
EXPECT_PRINTF("abc", "%.5s", "abc");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DynamicPrecision) {
|
||||
TEST(printf_test, dynamic_precision) {
|
||||
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,
|
||||
"precision is not integer");
|
||||
EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error, "argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf("%.*d", BIG_NUM, 42), format_error,
|
||||
EXPECT_THROW_MSG(test_sprintf("%.*d", big_num, 42), format_error,
|
||||
"number is too big");
|
||||
if (sizeof(long long) != sizeof(int)) {
|
||||
long long prec = static_cast<long long>(INT_MIN) - 1;
|
||||
@ -354,7 +354,7 @@ SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
|
||||
|
||||
// Test length format specifier ``length_spec``.
|
||||
template <typename T, typename U>
|
||||
void TestLength(const char* length_spec, U value) {
|
||||
void test_length(const char* length_spec, U value) {
|
||||
long long signed_value = 0;
|
||||
unsigned long long unsigned_value = 0;
|
||||
// Apply integer promotion to the argument.
|
||||
@ -393,54 +393,54 @@ void TestLength(const char* length_spec, U value) {
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
|
||||
}
|
||||
|
||||
template <typename T> void TestLength(const char* length_spec) {
|
||||
template <typename T> void test_length(const char* length_spec) {
|
||||
T min = std::numeric_limits<T>::min(), max = max_value<T>();
|
||||
TestLength<T>(length_spec, 42);
|
||||
TestLength<T>(length_spec, -42);
|
||||
TestLength<T>(length_spec, min);
|
||||
TestLength<T>(length_spec, max);
|
||||
test_length<T>(length_spec, 42);
|
||||
test_length<T>(length_spec, -42);
|
||||
test_length<T>(length_spec, min);
|
||||
test_length<T>(length_spec, max);
|
||||
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);
|
||||
test_length<T>(length_spec, static_cast<long long>(min) - 1);
|
||||
unsigned long long long_long_max = max_value<long long>();
|
||||
if (static_cast<unsigned long long>(max) < long_long_max)
|
||||
TestLength<T>(length_spec, static_cast<long long>(max) + 1);
|
||||
TestLength<T>(length_spec, std::numeric_limits<short>::min());
|
||||
TestLength<T>(length_spec, max_value<unsigned short>());
|
||||
TestLength<T>(length_spec, std::numeric_limits<int>::min());
|
||||
TestLength<T>(length_spec, max_value<int>());
|
||||
TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
|
||||
TestLength<T>(length_spec, max_value<unsigned>());
|
||||
TestLength<T>(length_spec, std::numeric_limits<long long>::min());
|
||||
TestLength<T>(length_spec, max_value<long long>());
|
||||
TestLength<T>(length_spec, std::numeric_limits<unsigned long long>::min());
|
||||
TestLength<T>(length_spec, max_value<unsigned long long>());
|
||||
test_length<T>(length_spec, static_cast<long long>(max) + 1);
|
||||
test_length<T>(length_spec, std::numeric_limits<short>::min());
|
||||
test_length<T>(length_spec, max_value<unsigned short>());
|
||||
test_length<T>(length_spec, std::numeric_limits<int>::min());
|
||||
test_length<T>(length_spec, max_value<int>());
|
||||
test_length<T>(length_spec, std::numeric_limits<unsigned>::min());
|
||||
test_length<T>(length_spec, max_value<unsigned>());
|
||||
test_length<T>(length_spec, std::numeric_limits<long long>::min());
|
||||
test_length<T>(length_spec, max_value<long long>());
|
||||
test_length<T>(length_spec, std::numeric_limits<unsigned long long>::min());
|
||||
test_length<T>(length_spec, max_value<unsigned long long>());
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Length) {
|
||||
TestLength<char>("hh");
|
||||
TestLength<signed char>("hh");
|
||||
TestLength<unsigned char>("hh");
|
||||
TestLength<short>("h");
|
||||
TestLength<unsigned short>("h");
|
||||
TestLength<long>("l");
|
||||
TestLength<unsigned long>("l");
|
||||
TestLength<long long>("ll");
|
||||
TestLength<unsigned long long>("ll");
|
||||
TestLength<intmax_t>("j");
|
||||
TestLength<size_t>("z");
|
||||
TestLength<std::ptrdiff_t>("t");
|
||||
TEST(printf_test, length) {
|
||||
test_length<char>("hh");
|
||||
test_length<signed char>("hh");
|
||||
test_length<unsigned char>("hh");
|
||||
test_length<short>("h");
|
||||
test_length<unsigned short>("h");
|
||||
test_length<long>("l");
|
||||
test_length<unsigned long>("l");
|
||||
test_length<long long>("ll");
|
||||
test_length<unsigned long long>("ll");
|
||||
test_length<intmax_t>("j");
|
||||
test_length<size_t>("z");
|
||||
test_length<std::ptrdiff_t>("t");
|
||||
long double max = max_value<long double>();
|
||||
EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
|
||||
EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Bool) {
|
||||
TEST(printf_test, bool) {
|
||||
EXPECT_PRINTF("1", "%d", true);
|
||||
EXPECT_PRINTF("true", "%s", true);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Int) {
|
||||
TEST(printf_test, int) {
|
||||
EXPECT_PRINTF("-42", "%d", -42);
|
||||
EXPECT_PRINTF("-42", "%i", -42);
|
||||
unsigned u = 0 - 42u;
|
||||
@ -450,14 +450,14 @@ TEST(PrintfTest, Int) {
|
||||
EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, long_long) {
|
||||
TEST(printf_test, long_long) {
|
||||
// fmt::printf allows passing long long arguments to %d without length
|
||||
// specifiers.
|
||||
long long max = max_value<long long>();
|
||||
EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Float) {
|
||||
TEST(printf_test, float) {
|
||||
EXPECT_PRINTF("392.650000", "%f", 392.65);
|
||||
EXPECT_PRINTF("392.65", "%.2f", 392.65);
|
||||
EXPECT_PRINTF("392.6", "%.1f", 392.65);
|
||||
@ -479,7 +479,7 @@ TEST(PrintfTest, Float) {
|
||||
EXPECT_EQ(buffer, format("{:A}", -392.65));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Inf) {
|
||||
TEST(printf_test, inf) {
|
||||
double inf = std::numeric_limits<double>::infinity();
|
||||
for (const char* type = "fega"; *type; ++type) {
|
||||
EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
|
||||
@ -488,7 +488,7 @@ TEST(PrintfTest, Inf) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Char) {
|
||||
TEST(printf_test, char) {
|
||||
EXPECT_PRINTF("x", "%c", 'x');
|
||||
int max = max_value<int>();
|
||||
EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
|
||||
@ -497,7 +497,7 @@ TEST(PrintfTest, Char) {
|
||||
EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, String) {
|
||||
TEST(printf_test, string) {
|
||||
EXPECT_PRINTF("abc", "%s", "abc");
|
||||
const char* null_str = nullptr;
|
||||
EXPECT_PRINTF("(null)", "%s", null_str);
|
||||
@ -508,13 +508,13 @@ TEST(PrintfTest, String) {
|
||||
EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, UCharString) {
|
||||
TEST(printf_test, uchar_string) {
|
||||
unsigned char str[] = "test";
|
||||
unsigned char* pstr = str;
|
||||
EXPECT_EQ("test", fmt::sprintf("%s", pstr));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Pointer) {
|
||||
TEST(printf_test, pointer) {
|
||||
int n;
|
||||
void* p = &n;
|
||||
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
||||
@ -537,20 +537,16 @@ TEST(PrintfTest, Pointer) {
|
||||
EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Location) {
|
||||
// TODO: test %n
|
||||
}
|
||||
|
||||
enum test_enum { answer = 42 };
|
||||
|
||||
TEST(PrintfTest, Enum) {
|
||||
TEST(printf_test, enum) {
|
||||
EXPECT_PRINTF("42", "%d", answer);
|
||||
volatile test_enum volatile_enum = answer;
|
||||
EXPECT_PRINTF("42", "%d", volatile_enum);
|
||||
}
|
||||
|
||||
#if FMT_USE_FCNTL
|
||||
TEST(PrintfTest, Examples) {
|
||||
TEST(printf_test, examples) {
|
||||
const char* weekday = "Thursday";
|
||||
const char* month = "August";
|
||||
int day = 21;
|
||||
@ -558,7 +554,7 @@ TEST(PrintfTest, Examples) {
|
||||
"Thursday, 21 August");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PrintfError) {
|
||||
TEST(printf_test, printf_error) {
|
||||
fmt::file read_end, write_end;
|
||||
fmt::file::pipe(read_end, write_end);
|
||||
int result = fmt::fprintf(read_end.fdopen("r").get(), "test");
|
||||
@ -566,20 +562,22 @@ TEST(PrintfTest, PrintfError) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(PrintfTest, WideString) { EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc")); }
|
||||
TEST(printf_test, wide_string) {
|
||||
EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PrintfCustom) {
|
||||
TEST(printf_test, printf_custom) {
|
||||
EXPECT_EQ("abc", test_sprintf("%s", TestString("abc")));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, OStream) {
|
||||
TEST(printf_test, ostream) {
|
||||
std::ostringstream os;
|
||||
int ret = fmt::fprintf(os, "Don't %s!", "panic");
|
||||
EXPECT_EQ("Don't panic!", os.str());
|
||||
EXPECT_EQ(12, ret);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, VPrintf) {
|
||||
TEST(printf_test, vprintf) {
|
||||
fmt::format_arg_store<fmt::printf_context, int> as{42};
|
||||
fmt::basic_format_args<fmt::printf_context> args(as);
|
||||
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
|
||||
@ -593,15 +591,15 @@ void check_format_string_regression(fmt::string_view s, const Args&... args) {
|
||||
fmt::sprintf(s, args...);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, CheckFormatStringRegression) {
|
||||
TEST(printf_test, check_format_string_regression) {
|
||||
check_format_string_regression("%c%s", 'x', "");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, FixedLargeExponent) {
|
||||
TEST(printf_test, fixed_large_exponent) {
|
||||
EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, VSPrintfMakeArgsExample) {
|
||||
TEST(printf_test, vsprintf_make_args_example) {
|
||||
fmt::format_arg_store<fmt::printf_context, int, const char*> as{42,
|
||||
"something"};
|
||||
fmt::basic_format_args<fmt::printf_context> args(as);
|
||||
@ -610,15 +608,12 @@ TEST(PrintfTest, VSPrintfMakeArgsExample) {
|
||||
fmt::basic_format_args<fmt::printf_context> args2(as2);
|
||||
EXPECT_EQ("[42] something happened",
|
||||
fmt::vsprintf("[%d] %s happened", args2));
|
||||
// The older gcc versions can't cast the return value.
|
||||
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
||||
EXPECT_EQ("[42] something happened",
|
||||
fmt::vsprintf("[%d] %s happened",
|
||||
{fmt::make_printf_args(42, "something")}));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(PrintfTest, VSPrintfMakeWArgsExample) {
|
||||
TEST(printf_test, vsprintf_make_wargs_example) {
|
||||
fmt::format_arg_store<fmt::wprintf_context, int, const wchar_t*> as{
|
||||
42, L"something"};
|
||||
fmt::basic_format_args<fmt::wprintf_context> args(as);
|
||||
@ -628,10 +623,7 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) {
|
||||
fmt::basic_format_args<fmt::wprintf_context> args2(as2);
|
||||
EXPECT_EQ(L"[42] something happened",
|
||||
fmt::vsprintf(L"[%d] %s happened", args2));
|
||||
// the older gcc versions can't cast the return value
|
||||
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
||||
EXPECT_EQ(L"[42] something happened",
|
||||
fmt::vsprintf(L"[%d] %s happened",
|
||||
{fmt::make_wprintf_args(42, L"something")}));
|
||||
#endif
|
||||
}
|
||||
|
@ -11,7 +11,6 @@
|
||||
|
||||
#include "fmt/ranges.h"
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -28,47 +27,45 @@
|
||||
#endif
|
||||
|
||||
#ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
||||
TEST(RangesTest, FormatArray) {
|
||||
int32_t ia[] = {1, 2, 3, 5, 7, 11};
|
||||
auto iaf = fmt::format("{}", ia);
|
||||
EXPECT_EQ("{1, 2, 3, 5, 7, 11}", iaf);
|
||||
TEST(ranges_test, format_array) {
|
||||
int arr[] = {1, 2, 3, 5, 7, 11};
|
||||
EXPECT_EQ(fmt::format("{}", arr), "{1, 2, 3, 5, 7, 11}");
|
||||
}
|
||||
|
||||
TEST(RangesTest, Format2dArray) {
|
||||
int32_t ia[][2] = {{1, 2}, {3, 5}, {7, 11}};
|
||||
auto iaf = fmt::format("{}", ia);
|
||||
EXPECT_EQ("{{1, 2}, {3, 5}, {7, 11}}", iaf);
|
||||
TEST(ranges_test, format_2d_Array) {
|
||||
int arr[][2] = {{1, 2}, {3, 5}, {7, 11}};
|
||||
EXPECT_EQ(fmt::format("{}", arr), "{{1, 2}, {3, 5}, {7, 11}}");
|
||||
}
|
||||
|
||||
TEST(RangesTest, FormatArrayOfLiterals) {
|
||||
const char* aol[] = {"1234", "abcd"};
|
||||
EXPECT_EQ("{\"1234\", \"abcd\"}", fmt::format("{}", aol));
|
||||
TEST(ranges_test, format_array_of_literals) {
|
||||
const char* arr[] = {"1234", "abcd"};
|
||||
EXPECT_EQ(fmt::format("{}", arr), "{\"1234\", \"abcd\"}");
|
||||
}
|
||||
#endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
||||
|
||||
TEST(RangesTest, FormatVector) {
|
||||
TEST(ranges_test, format_vector) {
|
||||
std::vector<int32_t> iv{1, 2, 3, 5, 7, 11};
|
||||
auto ivf = fmt::format("{}", iv);
|
||||
EXPECT_EQ("{1, 2, 3, 5, 7, 11}", ivf);
|
||||
}
|
||||
|
||||
TEST(RangesTest, FormatVector2) {
|
||||
TEST(ranges_test, format_vector2) {
|
||||
std::vector<std::vector<int32_t>> ivv{{1, 2}, {3, 5}, {7, 11}};
|
||||
auto ivf = fmt::format("{}", ivv);
|
||||
EXPECT_EQ("{{1, 2}, {3, 5}, {7, 11}}", ivf);
|
||||
}
|
||||
|
||||
TEST(RangesTest, FormatMap) {
|
||||
TEST(ranges_test, FormatMap) {
|
||||
std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
|
||||
EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
|
||||
}
|
||||
|
||||
TEST(RangesTest, FormatPair) {
|
||||
TEST(ranges_test, FormatPair) {
|
||||
std::pair<int64_t, float> pa1{42, 1.5f};
|
||||
EXPECT_EQ("(42, 1.5)", fmt::format("{}", pa1));
|
||||
}
|
||||
|
||||
TEST(RangesTest, FormatTuple) {
|
||||
TEST(ranges_test, FormatTuple) {
|
||||
std::tuple<int64_t, float, std::string, char> t{42, 1.5f, "this is tuple",
|
||||
'i'};
|
||||
EXPECT_EQ("(42, 1.5, \"this is tuple\", 'i')", fmt::format("{}", t));
|
||||
@ -103,13 +100,13 @@ template <size_t N> struct tuple_element<N, my_struct> {
|
||||
|
||||
} // namespace std
|
||||
|
||||
TEST(RangesTest, FormatStruct) {
|
||||
TEST(ranges_test, FormatStruct) {
|
||||
my_struct mst{13, "my struct"};
|
||||
EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
|
||||
}
|
||||
#endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
||||
|
||||
TEST(RangesTest, FormatTo) {
|
||||
TEST(ranges_test, FormatTo) {
|
||||
char buf[10];
|
||||
auto end = fmt::format_to(buf, "{}", std::vector<int>{1, 2, 3});
|
||||
*end = '\0';
|
||||
@ -123,7 +120,7 @@ struct path_like {
|
||||
operator std::string() const;
|
||||
};
|
||||
|
||||
TEST(RangesTest, PathLike) {
|
||||
TEST(ranges_test, PathLike) {
|
||||
EXPECT_FALSE((fmt::is_range<path_like, char>::value));
|
||||
}
|
||||
|
||||
@ -135,7 +132,7 @@ struct string_like {
|
||||
explicit operator std::string_view() const { return "foo"; }
|
||||
};
|
||||
|
||||
TEST(RangesTest, FormatStringLike) {
|
||||
TEST(ranges_test, FormatStringLike) {
|
||||
EXPECT_EQ("foo", fmt::format("{}", string_like()));
|
||||
}
|
||||
#endif // FMT_USE_STRING_VIEW
|
||||
@ -178,7 +175,7 @@ template <typename T> class noncopyable_range {
|
||||
const_iterator end() const { return vec.end(); }
|
||||
};
|
||||
|
||||
TEST(RangesTest, Range) {
|
||||
TEST(ranges_test, Range) {
|
||||
noncopyable_range<int> w(3u, 0);
|
||||
EXPECT_EQ("{0, 0, 0}", fmt::format("{}", w));
|
||||
EXPECT_EQ("{0, 0, 0}", fmt::format("{}", noncopyable_range<int>(3u, 0)));
|
||||
@ -198,14 +195,14 @@ TEST(RangesTest, Range) {
|
||||
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
|
||||
struct unformattable {};
|
||||
|
||||
TEST(RangesTest, UnformattableRange) {
|
||||
TEST(ranges_test, UnformattableRange) {
|
||||
EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>,
|
||||
fmt::format_context>::value));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef FMT_RANGES_TEST_ENABLE_JOIN
|
||||
TEST(RangesTest, JoinTuple) {
|
||||
TEST(ranges_test, JoinTuple) {
|
||||
// Value tuple args
|
||||
std::tuple<char, int, float> t1 = std::make_tuple('a', 1, 2.0f);
|
||||
EXPECT_EQ("(a, 1, 2)", fmt::format("({})", fmt::join(t1, ", ")));
|
||||
@ -224,7 +221,7 @@ TEST(RangesTest, JoinTuple) {
|
||||
EXPECT_EQ("4", fmt::format("{}", fmt::join(t4, "/")));
|
||||
}
|
||||
|
||||
TEST(RangesTest, WideStringJoinTuple) {
|
||||
TEST(ranges_test, WideStringJoinTuple) {
|
||||
// Value tuple args
|
||||
std::tuple<wchar_t, int, float> t1 = std::make_tuple('a', 1, 2.0f);
|
||||
EXPECT_EQ(L"(a, 1, 2)", fmt::format(L"({})", fmt::join(t1, L", ")));
|
||||
@ -243,7 +240,7 @@ TEST(RangesTest, WideStringJoinTuple) {
|
||||
EXPECT_EQ(L"4", fmt::format(L"{}", fmt::join(t4, L"/")));
|
||||
}
|
||||
|
||||
TEST(RangesTest, JoinInitializerList) {
|
||||
TEST(ranges_test, JoinInitializerList) {
|
||||
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join({1, 2, 3}, ", ")));
|
||||
EXPECT_EQ("fmt rocks !",
|
||||
fmt::format("{}", fmt::join({"fmt", "rocks", "!"}, " ")));
|
||||
@ -260,13 +257,13 @@ struct zstring {
|
||||
zstring_sentinel end() const { return {}; }
|
||||
};
|
||||
|
||||
TEST(RangesTest, JoinSentinel) {
|
||||
TEST(ranges_test, JoinSentinel) {
|
||||
zstring hello{"hello"};
|
||||
EXPECT_EQ("{'h', 'e', 'l', 'l', 'o'}", fmt::format("{}", hello));
|
||||
EXPECT_EQ("h_e_l_l_o", fmt::format("{}", fmt::join(hello, "_")));
|
||||
}
|
||||
|
||||
TEST(RangesTest, JoinRange) {
|
||||
TEST(ranges_test, JoinRange) {
|
||||
noncopyable_range<int> w(3u, 0);
|
||||
EXPECT_EQ("0,0,0", fmt::format("{}", fmt::join(w, ",")));
|
||||
EXPECT_EQ("0,0,0",
|
||||
|
Loading…
Reference in New Issue
Block a user