StringRef -> string_view, LongLong -> long_long

This commit is contained in:
Victor Zverovich
2017-02-18 06:52:52 -08:00
parent e022c21ddc
commit 50e716737d
14 changed files with 167 additions and 180 deletions

View File

@@ -55,11 +55,11 @@ struct ValueExtractor {
};
TEST(FormatTest, ArgConverter) {
fmt::LongLong value = std::numeric_limits<fmt::LongLong>::max();
fmt::long_long value = std::numeric_limits<fmt::long_long>::max();
auto arg = fmt::internal::make_arg<fmt::context>(value);
visit(fmt::internal::ArgConverter<
fmt::LongLong, fmt::context>(arg, 'd'), arg);
EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg));
fmt::long_long, fmt::context>(arg, 'd'), arg);
EXPECT_EQ(value, visit(ValueExtractor<fmt::long_long>(), arg));
}
TEST(FormatTest, FormatNegativeNaN) {

View File

@@ -73,7 +73,7 @@ using std::size_t;
using fmt::basic_writer;
using fmt::format;
using fmt::format_error;
using fmt::StringRef;
using fmt::string_view;
using fmt::CStringRef;
using fmt::MemoryWriter;
using fmt::WMemoryWriter;
@@ -148,16 +148,16 @@ struct WriteChecker {
EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value)
} // namespace
TEST(StringRefTest, Ctor) {
EXPECT_STREQ("abc", StringRef("abc").data());
EXPECT_EQ(3u, StringRef("abc").size());
TEST(StringViewTest, Ctor) {
EXPECT_STREQ("abc", string_view("abc").data());
EXPECT_EQ(3u, string_view("abc").size());
EXPECT_STREQ("defg", StringRef(std::string("defg")).data());
EXPECT_EQ(4u, StringRef(std::string("defg")).size());
EXPECT_STREQ("defg", string_view(std::string("defg")).data());
EXPECT_EQ(4u, string_view(std::string("defg")).size());
}
TEST(StringRefTest, ConvertToString) {
std::string s = StringRef("abc").to_string();
TEST(StringViewTest, ConvertToString) {
std::string s = string_view("abc").to_string();
EXPECT_EQ("abc", s);
}
@@ -1320,7 +1320,7 @@ TEST(FormatterTest, FormatString) {
}
TEST(FormatterTest, FormatStringRef) {
EXPECT_EQ("test", format("{0}", StringRef("test")));
EXPECT_EQ("test", format("{0}", string_view("test")));
}
TEST(FormatterTest, FormatCStringRef) {

View File

@@ -103,7 +103,7 @@ std::string read(File &f, std::size_t count) {
#endif // FMT_USE_FILE_DESCRIPTORS
std::string format_system_error(int error_code, fmt::StringRef message) {
std::string format_system_error(int error_code, fmt::string_view message) {
fmt::internal::MemoryBuffer<char> out;
fmt::format_system_error(out, error_code, message);
return to_string(out);

View File

@@ -81,7 +81,7 @@
FMT_TEST_THROW_(statement, expected_exception, \
expected_message, GTEST_NONFATAL_FAILURE_)
std::string format_system_error(int error_code, fmt::StringRef message);
std::string format_system_error(int error_code, fmt::string_view message);
#define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
EXPECT_THROW_MSG(statement, fmt::SystemError, \

View File

@@ -213,7 +213,7 @@ int (test::fileno)(FILE *stream) {
# define EXPECT_EQ_POSIX(expected, actual)
#endif
void write_file(fmt::CStringRef filename, fmt::StringRef content) {
void write_file(fmt::CStringRef filename, fmt::string_view content) {
fmt::BufferedFile f(filename, "w");
f.print("{}", content);
}
@@ -277,7 +277,7 @@ TEST(FileTest, Size) {
write_file("test", content);
File f("test", File::RDONLY);
EXPECT_GE(f.size(), 0);
EXPECT_EQ(content.size(), static_cast<fmt::ULongLong>(f.size()));
EXPECT_EQ(content.size(), static_cast<fmt::ulong_long>(f.size()));
#ifdef _WIN32
fmt::internal::MemoryBuffer<char> message;
fmt::internal::format_windows_error(

View File

@@ -65,7 +65,7 @@ File open_file() {
}
// Attempts to write a string to a file.
void write(File &f, fmt::StringRef s) {
void write(File &f, fmt::string_view s) {
std::size_t num_chars_left = s.size();
const char *ptr = s.data();
do {

View File

@@ -40,7 +40,7 @@ using fmt::format_error;
const unsigned BIG_NUM = INT_MAX + 1u;
// Makes format string argument positional.
std::string make_positional(fmt::StringRef format) {
std::string make_positional(fmt::string_view format) {
std::string s(format.to_string());
s.replace(s.find('%'), 1, "%1$");
return s;
@@ -269,8 +269,8 @@ TEST(PrintfTest, DynamicPrecision) {
"argument index out of range");
EXPECT_THROW_MSG(fmt::sprintf("%.*d", BIG_NUM, 42), format_error,
"number is too big");
if (sizeof(fmt::LongLong) != sizeof(int)) {
fmt::LongLong prec = static_cast<fmt::LongLong>(INT_MIN) - 1;
if (sizeof(fmt::long_long) != sizeof(int)) {
fmt::long_long prec = static_cast<fmt::long_long>(INT_MIN) - 1;
EXPECT_THROW_MSG(fmt::sprintf("%.*d", prec, 42), format_error,
"number is too big");
}
@@ -288,16 +288,16 @@ SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
SPECIALIZE_MAKE_SIGNED(unsigned short, short);
SPECIALIZE_MAKE_SIGNED(unsigned, int);
SPECIALIZE_MAKE_SIGNED(unsigned long, long);
SPECIALIZE_MAKE_SIGNED(fmt::ULongLong, fmt::LongLong);
SPECIALIZE_MAKE_SIGNED(fmt::ulong_long, fmt::long_long);
// Test length format specifier ``length_spec``.
template <typename T, typename U>
void TestLength(const char *length_spec, U value) {
fmt::LongLong signed_value = 0;
fmt::ULongLong unsigned_value = 0;
fmt::long_long signed_value = 0;
fmt::ulong_long unsigned_value = 0;
// Apply integer promotion to the argument.
using std::numeric_limits;
fmt::ULongLong max = numeric_limits<U>::max();
fmt::ulong_long max = numeric_limits<U>::max();
using fmt::internal::const_check;
if (const_check(max <= static_cast<unsigned>(numeric_limits<int>::max()))) {
signed_value = static_cast<int>(value);
@@ -308,7 +308,7 @@ void TestLength(const char *length_spec, U value) {
}
using fmt::internal::MakeUnsigned;
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
signed_value = static_cast<fmt::LongLong>(value);
signed_value = static_cast<fmt::long_long>(value);
unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value);
} else {
signed_value = static_cast<typename MakeSigned<T>::Type>(value);
@@ -339,20 +339,20 @@ void TestLength(const char *length_spec) {
TestLength<T>(length_spec, -42);
TestLength<T>(length_spec, min);
TestLength<T>(length_spec, max);
TestLength<T>(length_spec, fmt::LongLong(min) - 1);
fmt::ULongLong long_long_max = std::numeric_limits<fmt::LongLong>::max();
if (static_cast<fmt::ULongLong>(max) < long_long_max)
TestLength<T>(length_spec, fmt::LongLong(max) + 1);
TestLength<T>(length_spec, fmt::long_long(min) - 1);
fmt::ulong_long long_long_max = std::numeric_limits<fmt::long_long>::max();
if (static_cast<fmt::ulong_long>(max) < long_long_max)
TestLength<T>(length_spec, fmt::long_long(max) + 1);
TestLength<T>(length_spec, std::numeric_limits<short>::min());
TestLength<T>(length_spec, std::numeric_limits<unsigned short>::max());
TestLength<T>(length_spec, std::numeric_limits<int>::min());
TestLength<T>(length_spec, std::numeric_limits<int>::max());
TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
TestLength<T>(length_spec, std::numeric_limits<unsigned>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::long_long>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::long_long>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::ulong_long>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::ulong_long>::max());
}
TEST(PrintfTest, Length) {
@@ -363,8 +363,8 @@ TEST(PrintfTest, Length) {
TestLength<unsigned short>("h");
TestLength<long>("l");
TestLength<unsigned long>("l");
TestLength<fmt::LongLong>("ll");
TestLength<fmt::ULongLong>("ll");
TestLength<fmt::long_long>("ll");
TestLength<fmt::ulong_long>("ll");
TestLength<intmax_t>("j");
TestLength<std::size_t>("z");
TestLength<std::ptrdiff_t>("t");
@@ -388,10 +388,10 @@ TEST(PrintfTest, Int) {
EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
}
TEST(PrintfTest, LongLong) {
TEST(PrintfTest, long_long) {
// fmt::printf allows passing long long arguments to %d without length
// specifiers.
fmt::LongLong max = std::numeric_limits<fmt::LongLong>::max();
fmt::long_long max = std::numeric_limits<fmt::long_long>::max();
EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
}

View File

@@ -54,7 +54,7 @@
using fmt::basic_arg;
using fmt::basic_buffer;
using fmt::StringRef;
using fmt::string_view;
using fmt::internal::MemoryBuffer;
using fmt::internal::value;
@@ -488,8 +488,8 @@ VISIT_TYPE(unsigned short, unsigned);
VISIT_TYPE(long, int);
VISIT_TYPE(unsigned long, unsigned);
#else
VISIT_TYPE(long, fmt::LongLong);
VISIT_TYPE(unsigned long, fmt::ULongLong);
VISIT_TYPE(long, fmt::long_long);
VISIT_TYPE(unsigned long, fmt::ulong_long);
#endif
VISIT_TYPE(float, double);
@@ -511,7 +511,7 @@ class NumericArgTest : public testing::Test {};
typedef ::testing::Types<
bool, signed char, unsigned char, signed, unsigned short,
int, unsigned, long, unsigned long, fmt::LongLong, fmt::ULongLong,
int, unsigned, long, unsigned long, fmt::long_long, fmt::ulong_long,
float, double, long double> Types;
TYPED_TEST_CASE(NumericArgTest, Types);
@@ -546,7 +546,7 @@ TEST(UtilTest, StringArg) {
CHECK_ARG_(wchar_t, cstr, str);
CHECK_ARG(cstr);
StringRef sref(str);
string_view sref(str);
CHECK_ARG_(char, sref, std::string(str));
CHECK_ARG_(wchar_t, sref, std::string(str));
CHECK_ARG(sref);
@@ -557,11 +557,11 @@ TEST(UtilTest, WStringArg) {
wchar_t *str = str_data;
const wchar_t *cstr = str;
fmt::WStringRef sref(str);
fmt::wstring_view sref(str);
CHECK_ARG_(wchar_t, sref, str);
CHECK_ARG_(wchar_t, sref, cstr);
CHECK_ARG_(wchar_t, sref, std::wstring(str));
CHECK_ARG_(wchar_t, sref, fmt::WStringRef(str));
CHECK_ARG_(wchar_t, sref, fmt::wstring_view(str));
}
TEST(UtilTest, PointerArg) {
@@ -612,7 +612,7 @@ void test_count_digits() {
TEST(UtilTest, StringRef) {
// Test that StringRef::size() returns string length, not buffer size.
char str[100] = "some string";
EXPECT_EQ(std::strlen(str), StringRef(str).size());
EXPECT_EQ(std::strlen(str), string_view(str).size());
EXPECT_LT(std::strlen(str), sizeof(str));
}
@@ -623,18 +623,18 @@ void CheckOp() {
std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
for (std::size_t i = 0; i < num_inputs; ++i) {
for (std::size_t j = 0; j < num_inputs; ++j) {
StringRef lhs(inputs[i]), rhs(inputs[j]);
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<StringRef>()(lhs, rhs));
string_view lhs(inputs[i]), rhs(inputs[j]);
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
}
}
}
TEST(UtilTest, StringRefCompare) {
EXPECT_EQ(0, StringRef("foo").compare(StringRef("foo")));
EXPECT_GT(StringRef("fop").compare(StringRef("foo")), 0);
EXPECT_LT(StringRef("foo").compare(StringRef("fop")), 0);
EXPECT_GT(StringRef("foo").compare(StringRef("fo")), 0);
EXPECT_LT(StringRef("fo").compare(StringRef("foo")), 0);
EXPECT_EQ(0, string_view("foo").compare(string_view("foo")));
EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
CheckOp<std::equal_to>();
CheckOp<std::not_equal_to>();
CheckOp<std::less>();
@@ -666,7 +666,7 @@ TEST(UtilTest, UTF8ToUTF16) {
template <typename Converter, typename Char>
void check_utf_conversion_error(
const char *message,
fmt::BasicStringRef<Char> str = fmt::BasicStringRef<Char>(0, 0)) {
fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 0)) {
fmt::internal::MemoryBuffer<char> out;
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
fmt::SystemError error(0, "");
@@ -688,19 +688,19 @@ TEST(UtilTest, UTF8ToUTF16Error) {
const char *message = "cannot convert string from UTF-8 to UTF-16";
check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(message);
check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(
message, fmt::StringRef("foo", INT_MAX + 1u));
message, fmt::string_view("foo", INT_MAX + 1u));
}
TEST(UtilTest, UTF16ToUTF8Convert) {
fmt::internal::UTF16ToUTF8 u;
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::WStringRef(0, 0)));
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 0)));
EXPECT_EQ(ERROR_INVALID_PARAMETER,
u.convert(fmt::WStringRef(L"foo", INT_MAX + 1u)));
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
}
#endif // _WIN32
typedef void (*FormatErrorMessage)(
fmt::buffer &out, int error_code, StringRef message);
fmt::buffer &out, int error_code, string_view message);
template <typename Error>
void check_throw_error(int error_code, FormatErrorMessage format) {
@@ -723,7 +723,7 @@ TEST(UtilTest, FormatSystemError) {
to_string(message));
message.clear();
fmt::format_system_error(
message, EDOM, fmt::StringRef(0, std::numeric_limits<size_t>::max()));
message, EDOM, fmt::string_view(0, std::numeric_limits<size_t>::max()));
EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message));
}
@@ -760,7 +760,7 @@ TEST(UtilTest, FormatWindowsError) {
actual_message.clear();
fmt::internal::format_windows_error(
actual_message, ERROR_FILE_EXISTS,
fmt::StringRef(0, std::numeric_limits<size_t>::max()));
fmt::string_view(0, std::numeric_limits<size_t>::max()));
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS),
fmt::to_string(actual_message));
}