mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-22 10:30:05 +00:00
Cleanup tests
This commit is contained in:
parent
9d7b53cb9b
commit
9c3af11a92
@ -306,7 +306,7 @@ TEST(fp_test, grisu_format_compiles_with_on_ieee_double) {
|
||||
|
||||
TEST(format_impl_test, strerror) {
|
||||
char* message = nullptr;
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[256];
|
||||
EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = nullptr, 0),
|
||||
"invalid buffer");
|
||||
EXPECT_ASSERT(fmt::detail::safe_strerror(EDOM, message = buffer, 0),
|
||||
@ -320,11 +320,10 @@ TEST(format_impl_test, strerror) {
|
||||
int error_code = EDOM;
|
||||
#endif
|
||||
|
||||
int result =
|
||||
fmt::detail::safe_strerror(error_code, message = buffer, BUFFER_SIZE);
|
||||
int result = fmt::detail::safe_strerror(error_code, message = buffer, 256);
|
||||
EXPECT_EQ(result, 0);
|
||||
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);
|
||||
|
||||
// safe_strerror never uses buffer on MinGW.
|
||||
|
@ -35,6 +35,8 @@ using fmt::detail::max_value;
|
||||
using testing::Return;
|
||||
using testing::StrictMock;
|
||||
|
||||
enum { buffer_size = 256 };
|
||||
|
||||
struct uint32_pair {
|
||||
uint32_t u[2];
|
||||
};
|
||||
@ -397,7 +399,7 @@ TEST(format_test, arg_errors) {
|
||||
EXPECT_THROW_MSG(fmt::format(+"{00}", 42), format_error,
|
||||
"invalid format string");
|
||||
|
||||
char format_str[BUFFER_SIZE];
|
||||
char format_str[buffer_size];
|
||||
safe_sprintf(format_str, "{%u", INT_MAX);
|
||||
EXPECT_THROW_MSG(fmt::format(+format_str), format_error,
|
||||
"invalid format string");
|
||||
@ -698,7 +700,7 @@ TEST(format_test, zero_flag) {
|
||||
}
|
||||
|
||||
TEST(format_test, width) {
|
||||
char format_str[BUFFER_SIZE];
|
||||
char format_str[buffer_size];
|
||||
safe_sprintf(format_str, "{0:%u", UINT_MAX);
|
||||
increment(format_str + 3);
|
||||
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
|
||||
@ -735,7 +737,7 @@ TEST(format_test, width) {
|
||||
}
|
||||
|
||||
TEST(format_test, runtime_width) {
|
||||
char format_str[BUFFER_SIZE];
|
||||
char format_str[buffer_size];
|
||||
safe_sprintf(format_str, "{0:{%u", UINT_MAX);
|
||||
increment(format_str + 4);
|
||||
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
|
||||
@ -796,7 +798,7 @@ TEST(format_test, runtime_width) {
|
||||
}
|
||||
|
||||
TEST(format_test, precision) {
|
||||
char format_str[BUFFER_SIZE];
|
||||
char format_str[buffer_size];
|
||||
safe_sprintf(format_str, "{0:.%u", UINT_MAX);
|
||||
increment(format_str + 4);
|
||||
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
|
||||
@ -911,7 +913,7 @@ TEST(format_test, precision) {
|
||||
}
|
||||
|
||||
TEST(format_test, runtime_precision) {
|
||||
char format_str[BUFFER_SIZE];
|
||||
char format_str[buffer_size];
|
||||
safe_sprintf(format_str, "{0:.{%u", UINT_MAX);
|
||||
increment(format_str + 5);
|
||||
EXPECT_THROW_MSG(fmt::format(+format_str, 0), format_error,
|
||||
@ -1018,7 +1020,7 @@ TEST(format_test, format_short) {
|
||||
|
||||
template <typename T>
|
||||
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}";
|
||||
for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
|
||||
char c = static_cast<char>(i);
|
||||
@ -1083,7 +1085,7 @@ TEST(format_test, format_dec) {
|
||||
fmt::format("{0}", uint128_max));
|
||||
#endif
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[buffer_size];
|
||||
safe_sprintf(buffer, "%d", INT_MIN);
|
||||
EXPECT_EQ(buffer, fmt::format("{0}", INT_MIN));
|
||||
safe_sprintf(buffer, "%d", INT_MAX);
|
||||
@ -1124,7 +1126,7 @@ TEST(format_test, format_hex) {
|
||||
fmt::format("{0:x}", uint128_max));
|
||||
#endif
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[buffer_size];
|
||||
safe_sprintf(buffer, "-%x", 0 - static_cast<unsigned>(INT_MIN));
|
||||
EXPECT_EQ(buffer, fmt::format("{0:x}", INT_MIN));
|
||||
safe_sprintf(buffer, "%x", INT_MAX);
|
||||
@ -1162,7 +1164,7 @@ TEST(format_test, format_oct) {
|
||||
fmt::format("{0:o}", uint128_max));
|
||||
#endif
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[buffer_size];
|
||||
safe_sprintf(buffer, "-%o", 0 - static_cast<unsigned>(INT_MIN));
|
||||
EXPECT_EQ(buffer, fmt::format("{0:o}", INT_MIN));
|
||||
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("42", fmt::format("{:L}", 42.0));
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[buffer_size];
|
||||
safe_sprintf(buffer, "%e", 392.65);
|
||||
EXPECT_EQ(buffer, fmt::format("{0: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.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);
|
||||
EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l));
|
||||
EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l));
|
||||
@ -1456,7 +1458,7 @@ TEST(format_test, format_foreign_strings) {
|
||||
class Answer {};
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <> struct formatter<Date> {
|
||||
template <> struct formatter<date> {
|
||||
template <typename ParseContext>
|
||||
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||
auto it = ctx.begin();
|
||||
@ -1464,7 +1466,7 @@ template <> struct formatter<Date> {
|
||||
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());
|
||||
return ctx.out();
|
||||
}
|
||||
@ -1479,8 +1481,7 @@ template <> struct formatter<Answer> : formatter<int> {
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
TEST(format_test, format_custom) {
|
||||
Date date(2012, 12, 9);
|
||||
EXPECT_THROW_MSG(fmt::format(+"{:s}", date), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{:s}", date(2012, 12, 9)), format_error,
|
||||
"unknown format specifier");
|
||||
EXPECT_EQ("42", fmt::format("{0}", 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("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);
|
||||
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) {
|
||||
auto date = Date(2015, 10, 21);
|
||||
EXPECT_EQ("2015-10-21", "{}"_format(date));
|
||||
EXPECT_EQ("2015-10-21", "{}"_format(date(2015, 10, 21)));
|
||||
}
|
||||
#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) {
|
||||
EXPECT_ERROR_NOARGS("foo", nullptr);
|
||||
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
|
||||
// This causes an detail compiler error in MSVC2017.
|
||||
EXPECT_ERROR("{:{<}", "invalid fill character '{'", int);
|
||||
|
@ -151,7 +151,7 @@ bool isclosed(int fd) {
|
||||
file open_file() {
|
||||
file 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();
|
||||
return read_end;
|
||||
}
|
||||
@ -265,7 +265,7 @@ TEST(buffered_file_test, fileno) {
|
||||
auto f = open_buffered_file();
|
||||
EXPECT_TRUE(f.fileno() != -1);
|
||||
file copy = file::dup(f.fileno());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
EXPECT_READ(copy, file_content);
|
||||
}
|
||||
|
||||
TEST(ostream_test, move) {
|
||||
@ -334,7 +334,7 @@ TEST(file_test, default_ctor) {
|
||||
|
||||
TEST(file_test, open_buffered_file_in_ctor) {
|
||||
FILE* fp = safe_fopen("test-file", "w");
|
||||
std::fputs(FILE_CONTENT, fp);
|
||||
std::fputs(file_content, fp);
|
||||
std::fclose(fp);
|
||||
file f("test-file", file::RDONLY);
|
||||
// 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) {
|
||||
file f = open_file();
|
||||
EXPECT_READ(f, FILE_CONTENT);
|
||||
EXPECT_READ(f, file_content);
|
||||
}
|
||||
|
||||
TEST(file_test, read_error) {
|
||||
@ -473,7 +473,7 @@ TEST(file_test, dup) {
|
||||
file f = open_file();
|
||||
file copy = file::dup(f.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__
|
||||
@ -489,7 +489,7 @@ TEST(file_test, dup2) {
|
||||
file copy = open_file();
|
||||
f.dup2(copy.descriptor());
|
||||
EXPECT_NE(f.descriptor(), copy.descriptor());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
EXPECT_READ(copy, file_content);
|
||||
}
|
||||
|
||||
TEST(file_test, dup2_error) {
|
||||
@ -506,7 +506,7 @@ TEST(file_test, dup2_noexcept) {
|
||||
f.dup2(copy.descriptor(), ec);
|
||||
EXPECT_EQ(ec.get(), 0);
|
||||
EXPECT_NE(f.descriptor(), copy.descriptor());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
EXPECT_READ(copy, file_content);
|
||||
}
|
||||
|
||||
TEST(file_test, dup2_noexcept_error) {
|
||||
|
@ -27,12 +27,12 @@ template <> struct formatter<test> : formatter<int> {
|
||||
#include "gtest-extra.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();
|
||||
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();
|
||||
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.
|
||||
struct type_with_comma_op {};
|
||||
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 {};
|
||||
std::ostream& operator<<(std::ostream& os, streamable_enum) {
|
||||
@ -61,34 +61,33 @@ TEST(ostream_test, enum) {
|
||||
}
|
||||
|
||||
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("a string", fmt::format("{0}", test_string("a string")));
|
||||
EXPECT_EQ("The date is 2012-12-9",
|
||||
fmt::format("The date is {0}", date(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) {
|
||||
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,
|
||||
EXPECT_EQ("def ", fmt::format("{0:<5}", test_string("def")));
|
||||
EXPECT_EQ(" def", fmt::format("{0:>5}", test_string("def")));
|
||||
EXPECT_EQ(" def ", fmt::format("{0:^5}", test_string("def")));
|
||||
EXPECT_EQ("def**", fmt::format("{0:*<5}", test_string("def")));
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0:+}", test_string()), format_error,
|
||||
"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");
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0: }", TestString()), format_error,
|
||||
EXPECT_THROW_MSG(fmt::format(+"{0: }", test_string()), format_error,
|
||||
"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");
|
||||
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");
|
||||
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));
|
||||
EXPECT_EQ("test ", fmt::format("{0:13}", test_string("test")));
|
||||
EXPECT_EQ("test ", fmt::format("{0:{1}}", test_string("test"), 13));
|
||||
EXPECT_EQ("te", fmt::format("{0:.2}", test_string("test")));
|
||||
EXPECT_EQ("te", fmt::format("{0:.{1}}", test_string("test"), 2));
|
||||
}
|
||||
|
||||
struct empty_test {};
|
||||
@ -160,14 +159,14 @@ TEST(ostream_test, join) {
|
||||
}
|
||||
|
||||
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, ", ")));
|
||||
}
|
||||
|
||||
#if FMT_USE_CONSTEXPR
|
||||
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")));
|
||||
EXPECT_EQ("a string", format(FMT_STRING("{0}"), test_string("a string")));
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -287,6 +286,6 @@ TEST(ostream_test, to_string) {
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
@ -244,7 +244,7 @@ TEST(printf_test, hash_flag) {
|
||||
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);
|
||||
EXPECT_PRINTF(buffer, "%#e", -42.0);
|
||||
safe_sprintf(buffer, "%#E", -42.0);
|
||||
@ -307,7 +307,7 @@ TEST(printf_test, int_precision) {
|
||||
}
|
||||
|
||||
TEST(printf_test, float_precision) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[256];
|
||||
safe_sprintf(buffer, "%.3e", 1234.5678);
|
||||
EXPECT_PRINTF(buffer, "%.3e", 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("393", "%.f", 392.65);
|
||||
EXPECT_PRINTF("392.650000", "%F", 392.65);
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[256];
|
||||
safe_sprintf(buffer, "%e", 392.65);
|
||||
EXPECT_PRINTF(buffer, "%e", 392.65);
|
||||
safe_sprintf(buffer, "%E", 392.65);
|
||||
@ -567,7 +567,7 @@ TEST(printf_test, wide_string) {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -14,22 +14,22 @@
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest-extra.h"
|
||||
|
||||
TEST(ScanTest, ReadText) {
|
||||
fmt::string_view s = "foo";
|
||||
TEST(scan_test, read_text) {
|
||||
auto s = fmt::string_view("foo");
|
||||
auto end = fmt::scan(s, "foo");
|
||||
EXPECT_EQ(end, s.end());
|
||||
EXPECT_THROW_MSG(fmt::scan("fob", "foo"), fmt::format_error, "invalid input");
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadInt) {
|
||||
int n = 0;
|
||||
TEST(scan_test, read_int) {
|
||||
auto n = int();
|
||||
fmt::scan("42", "{}", n);
|
||||
EXPECT_EQ(n, 42);
|
||||
fmt::scan("-42", "{}", n);
|
||||
EXPECT_EQ(n, -42);
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadLongLong) {
|
||||
TEST(scan_test, read_longlong) {
|
||||
long long n = 0;
|
||||
fmt::scan("42", "{}", n);
|
||||
EXPECT_EQ(n, 42);
|
||||
@ -37,15 +37,15 @@ TEST(ScanTest, ReadLongLong) {
|
||||
EXPECT_EQ(n, -42);
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadUInt) {
|
||||
unsigned n = 0;
|
||||
TEST(scan_test, read_uint) {
|
||||
auto n = unsigned();
|
||||
fmt::scan("42", "{}", n);
|
||||
EXPECT_EQ(n, 42);
|
||||
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
|
||||
"invalid input");
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadULongLong) {
|
||||
TEST(scan_test, read_ulonglong) {
|
||||
unsigned long long n = 0;
|
||||
fmt::scan("42", "{}", n);
|
||||
EXPECT_EQ(n, 42);
|
||||
@ -53,14 +53,14 @@ TEST(ScanTest, ReadULongLong) {
|
||||
"invalid input");
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadString) {
|
||||
std::string s;
|
||||
TEST(scan_test, read_string) {
|
||||
auto s = std::string();
|
||||
fmt::scan("foo", "{}", s);
|
||||
EXPECT_EQ(s, "foo");
|
||||
}
|
||||
|
||||
TEST(ScanTest, ReadStringView) {
|
||||
fmt::string_view s;
|
||||
TEST(scan_test, read_string_view) {
|
||||
auto s = fmt::string_view();
|
||||
fmt::scan("foo", "{}", s);
|
||||
EXPECT_EQ(s, "foo");
|
||||
}
|
||||
@ -90,8 +90,8 @@ template <> struct scanner<tm> {
|
||||
};
|
||||
} // namespace fmt
|
||||
|
||||
TEST(ScanTest, ReadCustom) {
|
||||
const char* input = "Date: 1985-10-25";
|
||||
TEST(scan_test, read_custom) {
|
||||
auto input = "Date: 1985-10-25";
|
||||
auto t = tm();
|
||||
fmt::scan(input, "Date: {0:%Y-%m-%d}", t);
|
||||
EXPECT_EQ(t.tm_year, 85);
|
||||
@ -100,16 +100,16 @@ TEST(ScanTest, ReadCustom) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(ScanTest, InvalidFormat) {
|
||||
TEST(scan_test, invalid_format) {
|
||||
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,
|
||||
"argument index out of range");
|
||||
EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error,
|
||||
"invalid format string");
|
||||
}
|
||||
|
||||
TEST(ScanTest, Example) {
|
||||
std::string key;
|
||||
int value;
|
||||
TEST(scan_test, example) {
|
||||
auto key = std::string();
|
||||
auto value = int();
|
||||
fmt::scan("answer = 42", "{} = {}", key, value);
|
||||
EXPECT_EQ(key, "answer");
|
||||
EXPECT_EQ(value, 42);
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(StdFormatTest, Escaping) {
|
||||
TEST(std_format_test, escaping) {
|
||||
using namespace std;
|
||||
string s = format("{0}-{{", 8); // s == "8-{"
|
||||
EXPECT_EQ(s, "8-{");
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Indexing) {
|
||||
TEST(std_format_test, indexing) {
|
||||
using namespace std;
|
||||
string s0 = format("{} to {}", "a", "b"); // OK: automatic 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);
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Alignment) {
|
||||
TEST(std_format_test, alignment) {
|
||||
using namespace std;
|
||||
char c = 120;
|
||||
string s0 = format("{:6}", 42); // s0 == " 42"
|
||||
@ -41,7 +41,7 @@ TEST(StdFormatTest, Alignment) {
|
||||
EXPECT_EQ(s7, "true ");
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Float) {
|
||||
TEST(std_format_test, float) {
|
||||
using namespace std;
|
||||
double inf = numeric_limits<double>::infinity();
|
||||
double nan = numeric_limits<double>::quiet_NaN();
|
||||
@ -57,7 +57,7 @@ TEST(StdFormatTest, Float) {
|
||||
EXPECT_EQ(s3, "nan +nan nan nan");
|
||||
}
|
||||
|
||||
TEST(StdFormatTest, Int) {
|
||||
TEST(std_format_test, int) {
|
||||
using namespace std;
|
||||
string s0 = format("{}", 42); // s0 == "42"
|
||||
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a"
|
||||
@ -83,7 +83,7 @@ template <> struct std::formatter<color> : std::formatter<const char*> {
|
||||
|
||||
struct err {};
|
||||
|
||||
TEST(StdFormatTest, Formatter) {
|
||||
TEST(std_format_test, formatter) {
|
||||
std::string s0 = std::format("{}", 42); // OK: library-provided formatter
|
||||
// std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled
|
||||
// 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"
|
||||
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;
|
||||
auto s = std::format("{}", n);
|
||||
EXPECT_EQ(s, "42");
|
||||
|
12
test/util.cc
12
test/util.cc
@ -13,27 +13,27 @@ std::string get_system_error(int error_code) {
|
||||
#if defined(__MINGW32__) || !defined(_WIN32)
|
||||
return strerror(error_code);
|
||||
#else
|
||||
enum { BUFFER_SIZE = 200 };
|
||||
char buffer[BUFFER_SIZE];
|
||||
if (strerror_s(buffer, BUFFER_SIZE, error_code))
|
||||
enum { buffer_size = 200 };
|
||||
char buffer[buffer_size];
|
||||
if (strerror_s(buffer, buffer_size, error_code))
|
||||
throw std::exception("strerror_s failed");
|
||||
return buffer;
|
||||
#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) {
|
||||
#if FMT_USE_FCNTL
|
||||
fmt::file 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();
|
||||
fmt::buffered_file f = read_end.fdopen("r");
|
||||
if (fp) *fp = f.get();
|
||||
#else
|
||||
fmt::buffered_file f("test-file", "w");
|
||||
fputs(FILE_CONTENT, f.get());
|
||||
fputs(file_content, f.get());
|
||||
if (fp) *fp = f.get();
|
||||
#endif
|
||||
return f;
|
||||
|
22
test/util.h
22
test/util.h
@ -11,8 +11,6 @@
|
||||
|
||||
#include "fmt/os.h"
|
||||
|
||||
enum { BUFFER_SIZE = 256 };
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define FMT_VSNPRINTF vsprintf_s
|
||||
#else
|
||||
@ -29,7 +27,7 @@ void safe_sprintf(char (&buffer)[SIZE], const char* format, ...) {
|
||||
|
||||
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.
|
||||
fmt::buffered_file open_buffered_file(FILE** fp = nullptr);
|
||||
@ -45,35 +43,35 @@ inline FILE* safe_fopen(const char* filename, const char* mode) {
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename Char> class BasicTestString {
|
||||
template <typename Char> class basic_test_string {
|
||||
private:
|
||||
std::basic_string<Char> value_;
|
||||
|
||||
static const Char EMPTY[];
|
||||
static const Char empty[];
|
||||
|
||||
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_; }
|
||||
};
|
||||
|
||||
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 BasicTestString<wchar_t> TestWString;
|
||||
typedef basic_test_string<char> test_string;
|
||||
typedef basic_test_string<wchar_t> test_wstring;
|
||||
|
||||
template <typename Char>
|
||||
std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& os,
|
||||
const BasicTestString<Char>& s) {
|
||||
const basic_test_string<Char>& s) {
|
||||
os << s.value();
|
||||
return os;
|
||||
}
|
||||
|
||||
class Date {
|
||||
class date {
|
||||
int year_, month_, day_;
|
||||
|
||||
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 month() const { return month_; }
|
||||
|
Loading…
Reference in New Issue
Block a user