Cleanup tests

This commit is contained in:
Victor Zverovich 2021-05-05 17:43:06 -07:00
parent 9d7b53cb9b
commit 9c3af11a92
9 changed files with 98 additions and 102 deletions

View File

@ -306,7 +306,7 @@ TEST(fp_test, grisu_format_compiles_with_on_ieee_double) {
TEST(format_impl_test, strerror) { TEST(format_impl_test, strerror) {
char* message = nullptr; char* message = nullptr;
char buffer[BUFFER_SIZE]; char buffer[256];
EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = nullptr, 0), EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = nullptr, 0),
"invalid buffer"); "invalid buffer");
EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = buffer, 0), EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = buffer, 0),
@ -320,11 +320,10 @@ TEST(format_impl_test, strerror) {
int error_code = EDOM; int error_code = EDOM;
#endif #endif
int result = int result = fmt::detail::safe_strerror(error_code, message = buffer, 256);
fmt::detail::safe_strerror(error_code, message = buffer, BUFFER_SIZE);
EXPECT_EQ(result, 0); EXPECT_EQ(result, 0);
size_t message_size = std::strlen(message); size_t message_size = std::strlen(message);
EXPECT_GE(BUFFER_SIZE - 1u, message_size); EXPECT_GE(255u, message_size);
EXPECT_EQ(get_system_error(error_code), message); EXPECT_EQ(get_system_error(error_code), message);
// safe_strerror never uses buffer on MinGW. // safe_strerror never uses buffer on MinGW.

View File

@ -35,6 +35,8 @@ using fmt::detail::max_value;
using testing::Return; using testing::Return;
using testing::StrictMock; using testing::StrictMock;
enum { buffer_size = 256 };
struct uint32_pair { struct uint32_pair {
uint32_t u[2]; uint32_t u[2];
}; };
@ -397,7 +399,7 @@ TEST(format_test, arg_errors) {
EXPECT_THROW_MSG(fmt::format(+"{00}", 42), format_error, EXPECT_THROW_MSG(fmt::format(+"{00}", 42), format_error,
"invalid format string"); "invalid format string");
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
safe_sprintf(format_str, "{%u", INT_MAX); safe_sprintf(format_str, "{%u", INT_MAX);
EXPECT_THROW_MSG(fmt::format(+format_str), format_error, EXPECT_THROW_MSG(fmt::format(+format_str), format_error,
"invalid format string"); "invalid format string");
@ -698,7 +700,7 @@ TEST(format_test, zero_flag) {
} }
TEST(format_test, width) { TEST(format_test, width) {
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
safe_sprintf(format_str, "{0:%u", UINT_MAX); safe_sprintf(format_str, "{0:%u", UINT_MAX);
increment(format_str + 3); increment(format_str + 3);
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error, EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
@ -735,7 +737,7 @@ TEST(format_test, width) {
} }
TEST(format_test, runtime_width) { TEST(format_test, runtime_width) {
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
safe_sprintf(format_str, "{0:{%u", UINT_MAX); safe_sprintf(format_str, "{0:{%u", UINT_MAX);
increment(format_str + 4); increment(format_str + 4);
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error, EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
@ -796,7 +798,7 @@ TEST(format_test, runtime_width) {
} }
TEST(format_test, precision) { TEST(format_test, precision) {
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
safe_sprintf(format_str, "{0:.%u", UINT_MAX); safe_sprintf(format_str, "{0:.%u", UINT_MAX);
increment(format_str + 4); increment(format_str + 4);
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error, EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
@ -911,7 +913,7 @@ TEST(format_test, precision) {
} }
TEST(format_test, runtime_precision) { TEST(format_test, runtime_precision) {
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
safe_sprintf(format_str, "{0:.{%u", UINT_MAX); safe_sprintf(format_str, "{0:.{%u", UINT_MAX);
increment(format_str + 5); increment(format_str + 5);
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error, EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
@ -1018,7 +1020,7 @@ TEST(format_test, format_short) {
template <typename T> template <typename T>
void check_unknown_types(const T& value, const char* types, const char*) { void check_unknown_types(const T& value, const char* types, const char*) {
char format_str[BUFFER_SIZE]; char format_str[buffer_size];
const char* special = ".0123456789L}"; const char* special = ".0123456789L}";
for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) { for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
char c = static_cast<char>(i); char c = static_cast<char>(i);
@ -1083,7 +1085,7 @@ TEST(format_test, format_dec) {
fmt::format("{0}", uint128_max)); fmt::format("{0}", uint128_max));
#endif #endif
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "%d", INT_MIN); safe_sprintf(buffer, "%d", INT_MIN);
EXPECT_EQ(buffer, fmt::format("{0}", INT_MIN)); EXPECT_EQ(buffer, fmt::format("{0}", INT_MIN));
safe_sprintf(buffer, "%d", INT_MAX); safe_sprintf(buffer, "%d", INT_MAX);
@ -1124,7 +1126,7 @@ TEST(format_test, format_hex) {
fmt::format("{0:x}", uint128_max)); fmt::format("{0:x}", uint128_max));
#endif #endif
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "-%x", 0 - static_cast<unsigned>(INT_MIN)); safe_sprintf(buffer, "-%x", 0 - static_cast<unsigned>(INT_MIN));
EXPECT_EQ(buffer, fmt::format("{0:x}", INT_MIN)); EXPECT_EQ(buffer, fmt::format("{0:x}", INT_MIN));
safe_sprintf(buffer, "%x", INT_MAX); safe_sprintf(buffer, "%x", INT_MAX);
@ -1162,7 +1164,7 @@ TEST(format_test, format_oct) {
fmt::format("{0:o}", uint128_max)); fmt::format("{0:o}", uint128_max));
#endif #endif
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "-%o", 0 - static_cast<unsigned>(INT_MIN)); safe_sprintf(buffer, "-%o", 0 - static_cast<unsigned>(INT_MIN));
EXPECT_EQ(buffer, fmt::format("{0:o}", INT_MIN)); EXPECT_EQ(buffer, fmt::format("{0:o}", INT_MIN));
safe_sprintf(buffer, "%o", INT_MAX); safe_sprintf(buffer, "%o", INT_MAX);
@ -1199,7 +1201,7 @@ TEST(format_test, format_double) {
EXPECT_EQ("392.650000", fmt::format("{:f}", 392.65)); EXPECT_EQ("392.650000", fmt::format("{:f}", 392.65));
EXPECT_EQ("392.650000", fmt::format("{:F}", 392.65)); EXPECT_EQ("392.650000", fmt::format("{:F}", 392.65));
EXPECT_EQ("42", fmt::format("{:L}", 42.0)); EXPECT_EQ("42", fmt::format("{:L}", 42.0));
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "%e", 392.65); safe_sprintf(buffer, "%e", 392.65);
EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65)); EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65));
safe_sprintf(buffer, "%E", 392.65); safe_sprintf(buffer, "%E", 392.65);
@ -1287,7 +1289,7 @@ TEST(format_test, format_long_double) {
EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l)); EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));
EXPECT_EQ("392.650000", fmt::format("{0:f}", 392.65l)); EXPECT_EQ("392.650000", fmt::format("{0:f}", 392.65l));
EXPECT_EQ("392.650000", fmt::format("{0:F}", 392.65l)); EXPECT_EQ("392.650000", fmt::format("{0:F}", 392.65l));
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "%Le", 392.65l); safe_sprintf(buffer, "%Le", 392.65l);
EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l)); EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l));
EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l)); EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l));
@ -1456,7 +1458,7 @@ TEST(format_test, format_foreign_strings) {
class Answer {}; class Answer {};
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
template <> struct formatter<Date> { template <> struct formatter<date> {
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(); auto it = ctx.begin();
@ -1464,7 +1466,7 @@ template <> struct formatter<Date> {
return it; return it;
} }
auto format(const Date& d, format_context& ctx) -> decltype(ctx.out()) { auto format(const date& d, format_context& ctx) -> decltype(ctx.out()) {
format_to(ctx.out(), "{}-{}-{}", d.year(), d.month(), d.day()); format_to(ctx.out(), "{}-{}-{}", d.year(), d.month(), d.day());
return ctx.out(); return ctx.out();
} }
@ -1479,8 +1481,7 @@ template <> struct formatter<Answer> : formatter<int> {
FMT_END_NAMESPACE FMT_END_NAMESPACE
TEST(format_test, format_custom) { TEST(format_test, format_custom) {
Date date(2012, 12, 9); EXPECT_THROW_MSG(fmt::format(+"{:s}", date(2012, 12, 9)), format_error,
EXPECT_THROW_MSG(fmt::format(+"{:s}", date), format_error,
"unknown format specifier"); "unknown format specifier");
EXPECT_EQ("42", fmt::format("{0}", Answer())); EXPECT_EQ("42", fmt::format("{0}", Answer()));
EXPECT_EQ("0042", fmt::format("{:04}", Answer())); EXPECT_EQ("0042", fmt::format("{:04}", Answer()));
@ -1538,7 +1539,7 @@ TEST(format_test, format_examples) {
EXPECT_EQ("Bring me a shrubbery", fmt::format("Bring me a {}", "shrubbery")); EXPECT_EQ("Bring me a shrubbery", fmt::format("Bring me a {}", "shrubbery"));
EXPECT_EQ("From 1 to 3", fmt::format("From {} to {}", 1, 3)); EXPECT_EQ("From 1 to 3", fmt::format("From {} to {}", 1, 3));
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
safe_sprintf(buffer, "%03.2f", -1.2); safe_sprintf(buffer, "%03.2f", -1.2);
EXPECT_EQ(buffer, fmt::format("{:03.2f}", -1.2)); EXPECT_EQ(buffer, fmt::format("{:03.2f}", -1.2));
@ -1763,8 +1764,7 @@ TEST(format_test, udl_template) {
} }
TEST(format_test, udl_pass_user_defined_object_as_lvalue) { TEST(format_test, udl_pass_user_defined_object_as_lvalue) {
auto date = Date(2015, 10, 21); EXPECT_EQ("2015-10-21", "{}"_format(date(2015, 10, 21)));
EXPECT_EQ("2015-10-21", "{}"_format(date));
} }
#endif // FMT_USE_USER_DEFINED_LITERALS #endif // FMT_USE_USER_DEFINED_LITERALS
@ -2307,7 +2307,7 @@ FMT_CONSTEXPR bool test_error(const char* fmt, const char* expected_error) {
TEST(format_test, format_string_errors) { TEST(format_test, format_string_errors) {
EXPECT_ERROR_NOARGS("foo", nullptr); EXPECT_ERROR_NOARGS("foo", nullptr);
EXPECT_ERROR_NOARGS("}", "unmatched '}' in format string"); EXPECT_ERROR_NOARGS("}", "unmatched '}' in format string");
EXPECT_ERROR("{0:s", "unknown format specifier", Date); EXPECT_ERROR("{0:s", "unknown format specifier", date);
# if !FMT_MSC_VER || FMT_MSC_VER >= 1916 # if !FMT_MSC_VER || FMT_MSC_VER >= 1916
// This causes an detail compiler error in MSVC2017. // This causes an detail compiler error in MSVC2017.
EXPECT_ERROR("{:{<}", "invalid fill character '{'", int); EXPECT_ERROR("{:{<}", "invalid fill character '{'", int);

View File

@ -151,7 +151,7 @@ bool isclosed(int fd) {
file open_file() { file open_file() {
file read_end, write_end; file read_end, write_end;
file::pipe(read_end, write_end); file::pipe(read_end, write_end);
write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT)); write_end.write(file_content, std::strlen(file_content));
write_end.close(); write_end.close();
return read_end; return read_end;
} }
@ -265,7 +265,7 @@ TEST(buffered_file_test, fileno) {
auto f = open_buffered_file(); auto f = open_buffered_file();
EXPECT_TRUE(f.fileno() != -1); EXPECT_TRUE(f.fileno() != -1);
file copy = file::dup(f.fileno()); file copy = file::dup(f.fileno());
EXPECT_READ(copy, FILE_CONTENT); EXPECT_READ(copy, file_content);
} }
TEST(ostream_test, move) { TEST(ostream_test, move) {
@ -334,7 +334,7 @@ TEST(file_test, default_ctor) {
TEST(file_test, open_buffered_file_in_ctor) { TEST(file_test, open_buffered_file_in_ctor) {
FILE* fp = safe_fopen("test-file", "w"); FILE* fp = safe_fopen("test-file", "w");
std::fputs(FILE_CONTENT, fp); std::fputs(file_content, fp);
std::fclose(fp); std::fclose(fp);
file f("test-file", file::RDONLY); file f("test-file", file::RDONLY);
// Check if the file is open by reading one character from it. // Check if the file is open by reading one character from it.
@ -443,7 +443,7 @@ TEST(file_test, close_error) {
TEST(file_test, read) { TEST(file_test, read) {
file f = open_file(); file f = open_file();
EXPECT_READ(f, FILE_CONTENT); EXPECT_READ(f, file_content);
} }
TEST(file_test, read_error) { TEST(file_test, read_error) {
@ -473,7 +473,7 @@ TEST(file_test, dup) {
file f = open_file(); file f = open_file();
file copy = file::dup(f.descriptor()); file copy = file::dup(f.descriptor());
EXPECT_NE(f.descriptor(), copy.descriptor()); EXPECT_NE(f.descriptor(), copy.descriptor());
EXPECT_EQ(FILE_CONTENT, read(copy, std::strlen(FILE_CONTENT))); EXPECT_EQ(file_content, read(copy, std::strlen(file_content)));
} }
# ifndef __COVERITY__ # ifndef __COVERITY__
@ -489,7 +489,7 @@ TEST(file_test, dup2) {
file copy = open_file(); file copy = open_file();
f.dup2(copy.descriptor()); f.dup2(copy.descriptor());
EXPECT_NE(f.descriptor(), copy.descriptor()); EXPECT_NE(f.descriptor(), copy.descriptor());
EXPECT_READ(copy, FILE_CONTENT); EXPECT_READ(copy, file_content);
} }
TEST(file_test, dup2_error) { TEST(file_test, dup2_error) {
@ -506,7 +506,7 @@ TEST(file_test, dup2_noexcept) {
f.dup2(copy.descriptor(), ec); f.dup2(copy.descriptor(), ec);
EXPECT_EQ(ec.get(), 0); EXPECT_EQ(ec.get(), 0);
EXPECT_NE(f.descriptor(), copy.descriptor()); EXPECT_NE(f.descriptor(), copy.descriptor());
EXPECT_READ(copy, FILE_CONTENT); EXPECT_READ(copy, file_content);
} }
TEST(file_test, dup2_noexcept_error) { TEST(file_test, dup2_noexcept_error) {

View File

@ -27,12 +27,12 @@ template <> struct formatter<test> : formatter<int> {
#include "gtest-extra.h" #include "gtest-extra.h"
#include "util.h" #include "util.h"
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(); os << d.year() << '-' << d.month() << '-' << d.day();
return os; return os;
} }
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(); os << d.year() << L'-' << d.month() << L'-' << d.day();
return os; return os;
} }
@ -40,7 +40,7 @@ std::wostream& operator<<(std::wostream& os, const Date& d) {
// Make sure that overloaded comma operators do no harm to is_streamable. // Make sure that overloaded comma operators do no harm to is_streamable.
struct type_with_comma_op {}; struct type_with_comma_op {};
template <typename T> void operator,(type_with_comma_op, const T&); template <typename T> void operator,(type_with_comma_op, const T&);
template <typename T> type_with_comma_op operator<<(T&, const Date&); template <typename T> type_with_comma_op operator<<(T&, const date&);
enum streamable_enum {}; enum streamable_enum {};
std::ostream& operator<<(std::ostream& os, streamable_enum) { std::ostream& operator<<(std::ostream& os, streamable_enum) {
@ -61,34 +61,33 @@ TEST(ostream_test, enum) {
} }
TEST(ostream_test, format) { TEST(ostream_test, format) {
EXPECT_EQ("a string", fmt::format("{0}", TestString("a string"))); EXPECT_EQ("a string", fmt::format("{0}", test_string("a string")));
std::string s = fmt::format("The date is {0}", Date(2012, 12, 9)); EXPECT_EQ("The date is 2012-12-9",
EXPECT_EQ("The date is 2012-12-9", s); fmt::format("The date is {0}", date(2012, 12, 9)));
Date date(2012, 12, 9);
EXPECT_EQ(L"The date is 2012-12-9", EXPECT_EQ(L"The date is 2012-12-9",
fmt::format(L"The date is {0}", Date(2012, 12, 9))); fmt::format(L"The date is {0}", date(2012, 12, 9)));
} }
TEST(ostream_test, format_specs) { TEST(ostream_test, format_specs) {
using fmt::format_error; using fmt::format_error;
EXPECT_EQ("def ", fmt::format("{0:<5}", TestString("def"))); EXPECT_EQ("def ", fmt::format("{0:<5}", test_string("def")));
EXPECT_EQ(" def", fmt::format("{0:>5}", TestString("def"))); EXPECT_EQ(" def", fmt::format("{0:>5}", test_string("def")));
EXPECT_EQ(" def ", fmt::format("{0:^5}", TestString("def"))); EXPECT_EQ(" def ", fmt::format("{0:^5}", test_string("def")));
EXPECT_EQ("def**", fmt::format("{0:*<5}", TestString("def"))); EXPECT_EQ("def**", fmt::format("{0:*<5}", test_string("def")));
EXPECT_THROW_MSG(fmt::format(+"{0:+}", TestString()), format_error, EXPECT_THROW_MSG(fmt::format(+"{0:+}", test_string()), format_error,
"format specifier requires numeric argument"); "format specifier requires numeric argument");
EXPECT_THROW_MSG(fmt::format(+"{0:-}", TestString()), format_error, EXPECT_THROW_MSG(fmt::format(+"{0:-}", test_string()), format_error,
"format specifier requires numeric argument"); "format specifier requires numeric argument");
EXPECT_THROW_MSG(fmt::format(+"{0: }", TestString()), format_error, EXPECT_THROW_MSG(fmt::format(+"{0: }", test_string()), format_error,
"format specifier requires numeric argument"); "format specifier requires numeric argument");
EXPECT_THROW_MSG(fmt::format(+"{0:#}", TestString()), format_error, EXPECT_THROW_MSG(fmt::format(+"{0:#}", test_string()), format_error,
"format specifier requires numeric argument"); "format specifier requires numeric argument");
EXPECT_THROW_MSG(fmt::format(+"{0:05}", TestString()), format_error, EXPECT_THROW_MSG(fmt::format(+"{0:05}", test_string()), format_error,
"format specifier requires numeric argument"); "format specifier requires numeric argument");
EXPECT_EQ("test ", fmt::format("{0:13}", TestString("test"))); EXPECT_EQ("test ", fmt::format("{0:13}", test_string("test")));
EXPECT_EQ("test ", fmt::format("{0:{1}}", TestString("test"), 13)); EXPECT_EQ("test ", fmt::format("{0:{1}}", test_string("test"), 13));
EXPECT_EQ("te", fmt::format("{0:.2}", TestString("test"))); EXPECT_EQ("te", fmt::format("{0:.2}", test_string("test")));
EXPECT_EQ("te", fmt::format("{0:.{1}}", TestString("test"), 2)); EXPECT_EQ("te", fmt::format("{0:.{1}}", test_string("test"), 2));
} }
struct empty_test {}; struct empty_test {};
@ -160,14 +159,14 @@ TEST(ostream_test, join) {
} }
TEST(ostream_test, join_fallback_formatter) { TEST(ostream_test, join_fallback_formatter) {
auto strs = std::vector<TestString>{TestString("foo"), TestString("bar")}; auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", "))); EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", ")));
} }
#if FMT_USE_CONSTEXPR #if FMT_USE_CONSTEXPR
TEST(ostream_test, constexpr_string) { TEST(ostream_test, constexpr_string) {
EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42"))); EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));
EXPECT_EQ("a string", format(FMT_STRING("{0}"), TestString("a string"))); EXPECT_EQ("a string", format(FMT_STRING("{0}"), test_string("a string")));
} }
#endif #endif
@ -287,6 +286,6 @@ TEST(ostream_test, to_string) {
} }
TEST(ostream_test, range) { TEST(ostream_test, range) {
auto strs = std::vector<TestString>{TestString("foo"), TestString("bar")}; auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
EXPECT_EQ("[foo, bar]", fmt::format("{}", strs)); EXPECT_EQ("[foo, bar]", fmt::format("{}", strs));
} }

View File

@ -244,7 +244,7 @@ TEST(printf_test, hash_flag) {
EXPECT_PRINTF("-42.000000", "%#f", -42.0); EXPECT_PRINTF("-42.000000", "%#f", -42.0);
EXPECT_PRINTF("-42.000000", "%#F", -42.0); EXPECT_PRINTF("-42.000000", "%#F", -42.0);
char buffer[BUFFER_SIZE]; char buffer[256];
safe_sprintf(buffer, "%#e", -42.0); safe_sprintf(buffer, "%#e", -42.0);
EXPECT_PRINTF(buffer, "%#e", -42.0); EXPECT_PRINTF(buffer, "%#e", -42.0);
safe_sprintf(buffer, "%#E", -42.0); safe_sprintf(buffer, "%#E", -42.0);
@ -307,7 +307,7 @@ TEST(printf_test, int_precision) {
} }
TEST(printf_test, float_precision) { TEST(printf_test, float_precision) {
char buffer[BUFFER_SIZE]; char buffer[256];
safe_sprintf(buffer, "%.3e", 1234.5678); safe_sprintf(buffer, "%.3e", 1234.5678);
EXPECT_PRINTF(buffer, "%.3e", 1234.5678); EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
EXPECT_PRINTF("1234.568", "%.3f", 1234.5678); EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
@ -463,7 +463,7 @@ TEST(printf_test, float) {
EXPECT_PRINTF("392.6", "%.1f", 392.65); EXPECT_PRINTF("392.6", "%.1f", 392.65);
EXPECT_PRINTF("393", "%.f", 392.65); EXPECT_PRINTF("393", "%.f", 392.65);
EXPECT_PRINTF("392.650000", "%F", 392.65); EXPECT_PRINTF("392.650000", "%F", 392.65);
char buffer[BUFFER_SIZE]; char buffer[256];
safe_sprintf(buffer, "%e", 392.65); safe_sprintf(buffer, "%e", 392.65);
EXPECT_PRINTF(buffer, "%e", 392.65); EXPECT_PRINTF(buffer, "%e", 392.65);
safe_sprintf(buffer, "%E", 392.65); safe_sprintf(buffer, "%E", 392.65);
@ -567,7 +567,7 @@ TEST(printf_test, wide_string) {
} }
TEST(printf_test, printf_custom) { TEST(printf_test, printf_custom) {
EXPECT_EQ("abc", test_sprintf("%s", TestString("abc"))); EXPECT_EQ("abc", test_sprintf("%s", test_string("abc")));
} }
TEST(printf_test, ostream) { TEST(printf_test, ostream) {

View File

@ -14,22 +14,22 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest-extra.h" #include "gtest-extra.h"
TEST(ScanTest, ReadText) { TEST(scan_test, read_text) {
fmt::string_view s = "foo"; auto s = fmt::string_view("foo");
auto end = fmt::scan(s, "foo"); auto end = fmt::scan(s, "foo");
EXPECT_EQ(end, s.end()); EXPECT_EQ(end, s.end());
EXPECT_THROW_MSG(fmt::scan("fob", "foo"), fmt::format_error, "invalid input"); EXPECT_THROW_MSG(fmt::scan("fob", "foo"), fmt::format_error, "invalid input");
} }
TEST(ScanTest, ReadInt) { TEST(scan_test, read_int) {
int n = 0; auto n = int();
fmt::scan("42", "{}", n); fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42); EXPECT_EQ(n, 42);
fmt::scan("-42", "{}", n); fmt::scan("-42", "{}", n);
EXPECT_EQ(n, -42); EXPECT_EQ(n, -42);
} }
TEST(ScanTest, ReadLongLong) { TEST(scan_test, read_longlong) {
long long n = 0; long long n = 0;
fmt::scan("42", "{}", n); fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42); EXPECT_EQ(n, 42);
@ -37,15 +37,15 @@ TEST(ScanTest, ReadLongLong) {
EXPECT_EQ(n, -42); EXPECT_EQ(n, -42);
} }
TEST(ScanTest, ReadUInt) { TEST(scan_test, read_uint) {
unsigned n = 0; auto n = unsigned();
fmt::scan("42", "{}", n); fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42); EXPECT_EQ(n, 42);
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error, EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
"invalid input"); "invalid input");
} }
TEST(ScanTest, ReadULongLong) { TEST(scan_test, read_ulonglong) {
unsigned long long n = 0; unsigned long long n = 0;
fmt::scan("42", "{}", n); fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42); EXPECT_EQ(n, 42);
@ -53,14 +53,14 @@ TEST(ScanTest, ReadULongLong) {
"invalid input"); "invalid input");
} }
TEST(ScanTest, ReadString) { TEST(scan_test, read_string) {
std::string s; auto s = std::string();
fmt::scan("foo", "{}", s); fmt::scan("foo", "{}", s);
EXPECT_EQ(s, "foo"); EXPECT_EQ(s, "foo");
} }
TEST(ScanTest, ReadStringView) { TEST(scan_test, read_string_view) {
fmt::string_view s; auto s = fmt::string_view();
fmt::scan("foo", "{}", s); fmt::scan("foo", "{}", s);
EXPECT_EQ(s, "foo"); EXPECT_EQ(s, "foo");
} }
@ -90,8 +90,8 @@ template <> struct scanner<tm> {
}; };
} // namespace fmt } // namespace fmt
TEST(ScanTest, ReadCustom) { TEST(scan_test, read_custom) {
const char* input = "Date: 1985-10-25"; auto input = "Date: 1985-10-25";
auto t = tm(); auto t = tm();
fmt::scan(input, "Date: {0:%Y-%m-%d}", t); fmt::scan(input, "Date: {0:%Y-%m-%d}", t);
EXPECT_EQ(t.tm_year, 85); EXPECT_EQ(t.tm_year, 85);
@ -100,16 +100,16 @@ TEST(ScanTest, ReadCustom) {
} }
#endif #endif
TEST(ScanTest, InvalidFormat) { TEST(scan_test, invalid_format) {
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error, EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,
"argument index out of range"); "argument index out of range");
EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error, EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error,
"invalid format string"); "invalid format string");
} }
TEST(ScanTest, Example) { TEST(scan_test, example) {
std::string key; auto key = std::string();
int value; auto value = int();
fmt::scan("answer = 42", "{} = {}", key, value); fmt::scan("answer = 42", "{} = {}", key, value);
EXPECT_EQ(key, "answer"); EXPECT_EQ(key, "answer");
EXPECT_EQ(value, 42); EXPECT_EQ(value, 42);

View File

@ -2,13 +2,13 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
TEST(StdFormatTest, Escaping) { TEST(std_format_test, escaping) {
using namespace std; using namespace std;
string s = format("{0}-{{", 8); // s == "8-{" string s = format("{0}-{{", 8); // s == "8-{"
EXPECT_EQ(s, "8-{"); EXPECT_EQ(s, "8-{");
} }
TEST(StdFormatTest, Indexing) { TEST(std_format_test, indexing) {
using namespace std; using namespace std;
string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing
string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing
@ -20,7 +20,7 @@ TEST(StdFormatTest, Indexing) {
EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error); EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error);
} }
TEST(StdFormatTest, Alignment) { TEST(std_format_test, alignment) {
using namespace std; using namespace std;
char c = 120; char c = 120;
string s0 = format("{:6}", 42); // s0 == " 42" string s0 = format("{:6}", 42); // s0 == " 42"
@ -41,7 +41,7 @@ TEST(StdFormatTest, Alignment) {
EXPECT_EQ(s7, "true "); EXPECT_EQ(s7, "true ");
} }
TEST(StdFormatTest, Float) { TEST(std_format_test, float) {
using namespace std; using namespace std;
double inf = numeric_limits<double>::infinity(); double inf = numeric_limits<double>::infinity();
double nan = numeric_limits<double>::quiet_NaN(); double nan = numeric_limits<double>::quiet_NaN();
@ -57,7 +57,7 @@ TEST(StdFormatTest, Float) {
EXPECT_EQ(s3, "nan +nan nan nan"); EXPECT_EQ(s3, "nan +nan nan nan");
} }
TEST(StdFormatTest, Int) { TEST(std_format_test, int) {
using namespace std; using namespace std;
string s0 = format("{}", 42); // s0 == "42" string s0 = format("{}", 42); // s0 == "42"
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a" string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a"
@ -83,7 +83,7 @@ template <> struct std::formatter<color> : std::formatter<const char*> {
struct err {}; struct err {};
TEST(StdFormatTest, Formatter) { TEST(std_format_test, formatter) {
std::string s0 = std::format("{}", 42); // OK: library-provided formatter std::string s0 = std::format("{}", 42); // OK: library-provided formatter
// std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled // std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled
// formatter // formatter
@ -139,7 +139,7 @@ template <> struct std::formatter<S> {
} }
}; };
TEST(StdFormatTest, Parsing) { TEST(std_format_test, parsing) {
std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42" std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42"
EXPECT_EQ(s, " 42"); EXPECT_EQ(s, " 42");
} }
@ -153,7 +153,7 @@ template <> struct std::formatter<__int128_t> : std::formatter<long long> {
} }
}; };
TEST(StdFormatTest, Int128) { TEST(std_format_test, int128) {
__int128_t n = 42; __int128_t n = 42;
auto s = std::format("{}", n); auto s = std::format("{}", n);
EXPECT_EQ(s, "42"); EXPECT_EQ(s, "42");

View File

@ -13,27 +13,27 @@ std::string get_system_error(int error_code) {
#if defined(__MINGW32__) || !defined(_WIN32) #if defined(__MINGW32__) || !defined(_WIN32)
return strerror(error_code); return strerror(error_code);
#else #else
enum { BUFFER_SIZE = 200 }; enum { buffer_size = 200 };
char buffer[BUFFER_SIZE]; char buffer[buffer_size];
if (strerror_s(buffer, BUFFER_SIZE, error_code)) if (strerror_s(buffer, buffer_size, error_code))
throw std::exception("strerror_s failed"); throw std::exception("strerror_s failed");
return buffer; return buffer;
#endif #endif
} }
const char* const FILE_CONTENT = "Don't panic!"; const char* const file_content = "Don't panic!";
fmt::buffered_file open_buffered_file(FILE** fp) { fmt::buffered_file open_buffered_file(FILE** fp) {
#if FMT_USE_FCNTL #if FMT_USE_FCNTL
fmt::file read_end, write_end; fmt::file read_end, write_end;
fmt::file::pipe(read_end, write_end); fmt::file::pipe(read_end, write_end);
write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT)); write_end.write(file_content, std::strlen(file_content));
write_end.close(); write_end.close();
fmt::buffered_file f = read_end.fdopen("r"); fmt::buffered_file f = read_end.fdopen("r");
if (fp) *fp = f.get(); if (fp) *fp = f.get();
#else #else
fmt::buffered_file f("test-file", "w"); fmt::buffered_file f("test-file", "w");
fputs(FILE_CONTENT, f.get()); fputs(file_content, f.get());
if (fp) *fp = f.get(); if (fp) *fp = f.get();
#endif #endif
return f; return f;

View File

@ -11,8 +11,6 @@
#include "fmt/os.h" #include "fmt/os.h"
enum { BUFFER_SIZE = 256 };
#ifdef _MSC_VER #ifdef _MSC_VER
# define FMT_VSNPRINTF vsprintf_s # define FMT_VSNPRINTF vsprintf_s
#else #else
@ -29,7 +27,7 @@ void safe_sprintf(char (&buffer)[SIZE], const char* format, ...) {
std::string get_system_error(int error_code); std::string get_system_error(int error_code);
extern const char* const FILE_CONTENT; extern const char* const file_content;
// Opens a buffered file for reading. // Opens a buffered file for reading.
fmt::buffered_file open_buffered_file(FILE** fp = nullptr); fmt::buffered_file open_buffered_file(FILE** fp = nullptr);
@ -45,35 +43,35 @@ inline FILE* safe_fopen(const char* filename, const char* mode) {
#endif #endif
} }
template <typename Char> class BasicTestString { template <typename Char> class basic_test_string {
private: private:
std::basic_string<Char> value_; std::basic_string<Char> value_;
static const Char EMPTY[]; static const Char empty[];
public: public:
explicit BasicTestString(const Char* value = EMPTY) : value_(value) {} explicit basic_test_string(const Char* value = empty) : value_(value) {}
const std::basic_string<Char>& value() const { return value_; } const std::basic_string<Char>& value() const { return value_; }
}; };
template <typename Char> const Char BasicTestString<Char>::EMPTY[] = {0}; template <typename Char> const Char basic_test_string<Char>::empty[] = {0};
typedef BasicTestString<char> TestString; typedef basic_test_string<char> test_string;
typedef BasicTestString<wchar_t> TestWString; typedef basic_test_string<wchar_t> test_wstring;
template <typename Char> template <typename Char>
std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& os, std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& os,
const BasicTestString<Char>& s) { const basic_test_string<Char>& s) {
os << s.value(); os << s.value();
return os; return os;
} }
class Date { class date {
int year_, month_, day_; int year_, month_, day_;
public: public:
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {} date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
int year() const { return year_; } int year() const { return year_; }
int month() const { return month_; } int month() const { return month_; }