Cleanup tests

This commit is contained in:
Victor Zverovich 2021-05-02 05:55:55 -07:00
parent 9155e2de4c
commit ed7c4320f6
10 changed files with 168 additions and 173 deletions

View File

@ -25,7 +25,6 @@
# define WIN32_LEAN_AND_MEAN
# endif
# include <io.h>
# include <windows.h>
# define O_CREAT _O_CREAT
# define O_TRUNC _O_TRUNC
@ -55,7 +54,7 @@
namespace {
#ifdef _WIN32
// Return type of read and write functions.
using RWResult = int;
using rwresult = int;
// On Windows the count argument to read and write is unsigned, so convert
// it from size_t preventing integer overflow.
@ -64,7 +63,7 @@ inline unsigned convert_rwcount(std::size_t count) {
}
#elif FMT_USE_FCNTL
// Return type of read and write functions.
using RWResult = ssize_t;
using rwresult = ssize_t;
inline std::size_t convert_rwcount(std::size_t count) { return count; }
#endif
@ -229,14 +228,14 @@ long long file::size() const {
}
std::size_t file::read(void* buffer, std::size_t count) {
RWResult result = 0;
rwresult result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
if (result < 0) FMT_THROW(system_error(errno, "cannot read from file"));
return detail::to_unsigned(result);
}
std::size_t file::write(const void* buffer, std::size_t count) {
RWResult result = 0;
rwresult result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
if (result < 0) FMT_THROW(system_error(errno, "cannot write to file"));
return detail::to_unsigned(result);

View File

@ -64,7 +64,7 @@ if (MSVC)
target_compile_options(format-test PRIVATE /bigobj)
endif ()
if (NOT (MSVC AND BUILD_SHARED_LIBS))
add_fmt_test(format-impl-test HEADER_ONLY)
add_fmt_test(format-impl-test HEADER_ONLY header-only-test.cc)
endif ()
add_fmt_test(locale-test)
add_fmt_test(ostream-test)
@ -107,17 +107,6 @@ if (NOT MSVC_STATIC_RUNTIME)
add_fmt_test(os-test)
endif ()
add_fmt_executable(header-only-test
header-only-test.cc header-only-test2.cc test-main.cc)
target_link_libraries(header-only-test gtest)
if (TARGET fmt-header-only)
target_link_libraries(header-only-test fmt-header-only)
else ()
target_include_directories(
header-only-test PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1)
endif ()
message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}")
if (FMT_PEDANTIC AND CXX_STANDARD LESS 20)

View File

@ -14,6 +14,8 @@
#include <memory>
#include <stdexcept>
#include "fmt/os.h"
#if defined(_WIN32) && !defined(__MINGW32__)
# include <crtdbg.h> // for _CrtSetReportMode
#endif // _WIN32
@ -336,7 +338,7 @@ TEST(OutputRedirectTest, ScopedRedirect) {
buffered_file file(write_end.fdopen("w"));
std::fprintf(file.get(), "[[[");
{
OutputRedirect redir(file.get());
output_redirect redir(file.get());
std::fprintf(file.get(), "censored");
}
std::fprintf(file.get(), "]]]");
@ -344,7 +346,7 @@ TEST(OutputRedirectTest, ScopedRedirect) {
EXPECT_READ(read_end, "[[[]]]");
}
// Test that OutputRedirect handles errors in flush correctly.
// Test that output_redirect handles errors in flush correctly.
TEST(OutputRedirectTest, FlushErrorInCtor) {
file read_end, write_end;
file::pipe(read_end, write_end);
@ -354,8 +356,8 @@ TEST(OutputRedirectTest, FlushErrorInCtor) {
// Put a character in a file buffer.
EXPECT_EQ('x', fputc('x', f.get()));
FMT_POSIX(close(write_fd));
std::unique_ptr<OutputRedirect> redir{nullptr};
EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new OutputRedirect(f.get())), EBADF,
std::unique_ptr<output_redirect> redir{nullptr};
EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new output_redirect(f.get())), EBADF,
"cannot flush stream");
redir.reset(nullptr);
write_copy.dup2(write_fd); // "undo" close or dtor will fail
@ -366,9 +368,9 @@ TEST(OutputRedirectTest, DupErrorInCtor) {
int fd = (f.fileno)();
file copy = file::dup(fd);
FMT_POSIX(close(fd));
std::unique_ptr<OutputRedirect> redir{nullptr};
std::unique_ptr<output_redirect> redir{nullptr};
EXPECT_SYSTEM_ERROR_NOASSERT(
redir.reset(new OutputRedirect(f.get())), EBADF,
redir.reset(new output_redirect(f.get())), EBADF,
fmt::format("cannot duplicate file descriptor {}", fd));
copy.dup2(fd); // "undo" close or dtor will fail
}
@ -378,7 +380,7 @@ TEST(OutputRedirectTest, RestoreAndRead) {
file::pipe(read_end, write_end);
buffered_file file(write_end.fdopen("w"));
std::fprintf(file.get(), "[[[");
OutputRedirect redir(file.get());
output_redirect redir(file.get());
std::fprintf(file.get(), "censored");
EXPECT_EQ("censored", redir.restore_and_read());
EXPECT_EQ("", redir.restore_and_read());
@ -394,7 +396,7 @@ TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
int write_fd = write_end.descriptor();
file write_copy = write_end.dup(write_fd);
buffered_file f = write_end.fdopen("w");
OutputRedirect redir(f.get());
output_redirect redir(f.get());
// Put a character in a file buffer.
EXPECT_EQ('x', fputc('x', f.get()));
FMT_POSIX(close(write_fd));
@ -409,7 +411,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
int write_fd = write_end.descriptor();
file write_copy = write_end.dup(write_fd);
buffered_file f = write_end.fdopen("w");
std::unique_ptr<OutputRedirect> redir(new OutputRedirect(f.get()));
std::unique_ptr<output_redirect> redir(new output_redirect(f.get()));
// Put a character in a file buffer.
EXPECT_EQ('x', fputc('x', f.get()));
EXPECT_WRITE(

View File

@ -11,24 +11,7 @@
using fmt::file;
void OutputRedirect::flush() {
# if EOF != -1
# error "FMT_RETRY assumes return value of -1 indicating failure"
# endif
int result = 0;
FMT_RETRY(result, fflush(file_));
if (result != 0) throw fmt::system_error(errno, "cannot flush stream");
}
void OutputRedirect::restore() {
if (original_.descriptor() == -1) return; // Already restored.
flush();
// Restore the original file.
original_.dup2(FMT_POSIX(fileno(file_)));
original_.close();
}
OutputRedirect::OutputRedirect(FILE* f) : file_(f) {
output_redirect::output_redirect(FILE* f) : file_(f) {
flush();
int fd = FMT_POSIX(fileno(f));
// Create a file object referring to the original file.
@ -40,7 +23,7 @@ OutputRedirect::OutputRedirect(FILE* f) : file_(f) {
write_end.dup2(fd);
}
OutputRedirect::~OutputRedirect() FMT_NOEXCEPT {
output_redirect::~output_redirect() FMT_NOEXCEPT {
try {
restore();
} catch (const std::exception& e) {
@ -48,7 +31,24 @@ OutputRedirect::~OutputRedirect() FMT_NOEXCEPT {
}
}
std::string OutputRedirect::restore_and_read() {
void output_redirect::flush() {
# if EOF != -1
# error "FMT_RETRY assumes return value of -1 indicating failure"
# endif
int result = 0;
FMT_RETRY(result, fflush(file_));
if (result != 0) throw fmt::system_error(errno, "cannot flush stream");
}
void output_redirect::restore() {
if (original_.descriptor() == -1) return; // Already restored.
flush();
// Restore the original file.
original_.dup2(FMT_POSIX(fileno(file_)));
original_.close();
}
std::string output_redirect::restore_and_read() {
// Restore output.
restore();

View File

@ -8,6 +8,8 @@
#ifndef FMT_GTEST_EXTRA_H_
#define FMT_GTEST_EXTRA_H_
#include <stdlib.h> // _invalid_parameter_handler
#include <string>
#include "fmt/os.h"
@ -61,20 +63,21 @@ std::string format_system_error(int error_code, fmt::string_view message);
// Captures file output by redirecting it to a pipe.
// The output it can handle is limited by the pipe capacity.
class OutputRedirect {
class output_redirect {
private:
FILE* file_;
fmt::file original_; // Original file passed to redirector.
fmt::file read_end_; // Read end of the pipe where the output is redirected.
GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect);
void flush();
void restore();
public:
explicit OutputRedirect(FILE* file);
~OutputRedirect() FMT_NOEXCEPT;
explicit output_redirect(FILE* file);
~output_redirect() FMT_NOEXCEPT;
output_redirect(const output_redirect&) = delete;
void operator=(const output_redirect&) = delete;
// Restores the original file, reads output from the pipe into a string
// and returns it.
@ -85,7 +88,7 @@ class OutputRedirect {
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
std::string gtest_expected_output = expected_output; \
OutputRedirect gtest_redir(file); \
output_redirect gtest_redir(file); \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
std::string gtest_output = gtest_redir.restore_and_read(); \
if (gtest_output != gtest_expected_output) { \
@ -106,7 +109,7 @@ class OutputRedirect {
// Suppresses Windows assertions on invalid file descriptors, making
// POSIX functions return proper error codes instead of crashing on Windows.
class SuppressAssert {
class suppress_assert {
private:
_invalid_parameter_handler original_handler_;
int original_report_mode_;
@ -115,11 +118,11 @@ class SuppressAssert {
const wchar_t*, unsigned, uintptr_t) {}
public:
SuppressAssert()
suppress_assert()
: original_handler_(
_set_invalid_parameter_handler(handle_invalid_parameter)),
original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {}
~SuppressAssert() {
~suppress_assert() {
_set_invalid_parameter_handler(original_handler_);
_CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
}
@ -127,7 +130,7 @@ class SuppressAssert {
# define SUPPRESS_ASSERT(statement) \
{ \
SuppressAssert sa; \
suppress_assert sa; \
statement; \
}
# else
@ -141,7 +144,7 @@ class SuppressAssert {
std::string read(fmt::file& f, size_t count);
# define EXPECT_READ(file, expected_content) \
EXPECT_EQ(expected_content, \
EXPECT_EQ(expected_content, \
read(file, fmt::string_view(expected_content).size()))
#else
@ -154,9 +157,4 @@ std::string read(fmt::file& f, size_t count);
} while (false)
#endif // FMT_USE_FCNTL
template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> {
ScopedMock() { Mock::instance = this; }
~ScopedMock() { Mock::instance = nullptr; }
};
#endif // FMT_GTEST_EXTRA_H_

View File

@ -1,3 +1,7 @@
// Header-only configuration test
#include "fmt/core.h"
#ifndef FMT_HEADER_ONLY
# error "Not in the header-only mode."
#endif

View File

@ -1,3 +0,0 @@
// Additional translation unit for the header-only configuration test
#include "fmt/core.h"

View File

@ -16,94 +16,97 @@ using fmt::detail::max_value;
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
template <typename Char> struct numpunct : std::numpunct<Char> {
protected:
Char do_decimal_point() const FMT_OVERRIDE { return '?'; }
std::string do_grouping() const FMT_OVERRIDE { return "\03"; }
Char do_thousands_sep() const FMT_OVERRIDE { return '~'; }
Char do_decimal_point() const override { return '?'; }
std::string do_grouping() const override { return "\03"; }
Char do_thousands_sep() const override { return '~'; }
};
template <typename Char> struct no_grouping : std::numpunct<Char> {
protected:
Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
std::string do_grouping() const FMT_OVERRIDE { return ""; }
Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
Char do_decimal_point() const override { return '.'; }
std::string do_grouping() const override { return ""; }
Char do_thousands_sep() const override { return ','; }
};
template <typename Char> struct special_grouping : std::numpunct<Char> {
protected:
Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
std::string do_grouping() const FMT_OVERRIDE { return "\03\02"; }
Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
Char do_decimal_point() const override { return '.'; }
std::string do_grouping() const override { return "\03\02"; }
Char do_thousands_sep() const override { return ','; }
};
template <typename Char> struct small_grouping : std::numpunct<Char> {
protected:
Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
std::string do_grouping() const FMT_OVERRIDE { return "\01"; }
Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
Char do_decimal_point() const override { return '.'; }
std::string do_grouping() const override { return "\01"; }
Char do_thousands_sep() const override { return ','; }
};
TEST(LocaleTest, DoubleDecimalPoint) {
std::locale loc(std::locale(), new numpunct<char>());
TEST(locale_test, double_decimal_point) {
auto loc = std::locale(std::locale(), new numpunct<char>());
EXPECT_EQ("1?23", fmt::format(loc, "{:L}", 1.23));
EXPECT_EQ("1?230000", fmt::format(loc, "{:Lf}", 1.23));
}
TEST(LocaleTest, Format) {
std::locale loc(std::locale(), new numpunct<char>());
TEST(locale_test, format) {
auto loc = std::locale(std::locale(), new numpunct<char>());
EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567));
EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
fmt::format_arg_store<fmt::format_context, int> as{1234567};
EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::format_args(as)));
std::string s;
auto s = std::string();
fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
EXPECT_EQ("1~234~567", s);
std::locale no_grouping_loc(std::locale(), new no_grouping<char>());
auto no_grouping_loc = std::locale(std::locale(), new no_grouping<char>());
EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:L}", 1234567));
std::locale special_grouping_loc(std::locale(), new special_grouping<char>());
auto special_grouping_loc =
std::locale(std::locale(), new special_grouping<char>());
EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:L}", 12345678));
EXPECT_EQ("12,345", fmt::format(special_grouping_loc, "{:L}", 12345));
std::locale small_grouping_loc(std::locale(), new small_grouping<char>());
auto small_grouping_loc =
std::locale(std::locale(), new small_grouping<char>());
EXPECT_EQ("4,2,9,4,9,6,7,2,9,5",
fmt::format(small_grouping_loc, "{:L}", max_value<uint32_t>()));
}
TEST(LocaleTest, FormatDetaultAlign) {
TEST(locale_test, format_detault_align) {
auto loc = std::locale({}, new special_grouping<char>());
EXPECT_EQ(" 12,345", fmt::format(loc, "{:8L}", 12345));
}
TEST(LocaleTest, FormatPlus) {
TEST(locale_test, format_plus) {
auto loc = std::locale({}, new special_grouping<char>());
EXPECT_EQ("+100", fmt::format(loc, "{:+L}", 100));
}
TEST(LocaleTest, WFormat) {
std::locale loc(std::locale(), new numpunct<wchar_t>());
TEST(locale_test, wformat) {
auto loc = std::locale(std::locale(), new numpunct<wchar_t>());
EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
fmt::format_arg_store<fmt::wformat_context, int> as{1234567};
EXPECT_EQ(L"1~234~567", fmt::vformat(loc, L"{:L}", fmt::wformat_args(as)));
EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
std::locale no_grouping_loc(std::locale(), new no_grouping<wchar_t>());
auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:L}", 1234567));
std::locale special_grouping_loc(std::locale(),
new special_grouping<wchar_t>());
auto special_grouping_loc =
std::locale(std::locale(), new special_grouping<wchar_t>());
EXPECT_EQ(L"1,23,45,678",
fmt::format(special_grouping_loc, L"{:L}", 12345678));
std::locale small_grouping_loc(std::locale(), new small_grouping<wchar_t>());
auto small_grouping_loc =
std::locale(std::locale(), new small_grouping<wchar_t>());
EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5",
fmt::format(small_grouping_loc, L"{:L}", max_value<uint32_t>()));
}
TEST(LocaleTest, DoubleFormatter) {
TEST(locale_test, double_formatter) {
auto loc = std::locale(std::locale(), new special_grouping<char>());
auto f = fmt::formatter<int>();
auto parse_ctx = fmt::format_parse_context("L");
@ -145,8 +148,7 @@ template <class charT> struct formatter<std::complex<double>, charT> {
auto imag = fmt::format(ctx.locale().template get<std::locale>(),
"{:" + specs + "}", c.imag());
auto fill_align_width = std::string();
if (specs_.width > 0)
fill_align_width = fmt::format(">{}", specs_.width);
if (specs_.width > 0) fill_align_width = fmt::format(">{}", specs_.width);
return format_to(
ctx.out(), "{:" + fill_align_width + "}",
fmt::format(c.real() != 0 ? "({0}+{1}i)" : "{1}i", real, imag));
@ -154,7 +156,7 @@ template <class charT> struct formatter<std::complex<double>, charT> {
};
FMT_END_NAMESPACE
TEST(FormatTest, Complex) {
TEST(locale_test, complex) {
std::string s = fmt::format("{}", std::complex<double>(1, 2));
EXPECT_EQ(s, "(1+2i)");
EXPECT_EQ(fmt::format("{:.2f}", std::complex<double>(1, 2)), "(1.00+2.00i)");

View File

@ -25,14 +25,14 @@ using fmt::error_code;
# include <windows.h>
TEST(UtilTest, UTF16ToUTF8) {
std::string s = "ёжик";
TEST(util_test, utf16_to_utf8) {
auto s = std::string("ёжик");
fmt::detail::utf16_to_utf8 u(L"\x0451\x0436\x0438\x043A");
EXPECT_EQ(s, u.str());
EXPECT_EQ(s.size(), u.size());
}
TEST(UtilTest, UTF16ToUTF8EmptyString) {
TEST(util_test, utf16_to_utf8_empty_string) {
std::string s = "";
fmt::detail::utf16_to_utf8 u(L"");
EXPECT_EQ(s, u.str());
@ -45,7 +45,7 @@ void check_utf_conversion_error(
fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 1)) {
fmt::memory_buffer out;
fmt::detail::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
fmt::system_error error(0, "");
auto error = fmt::system_error(0, "");
try {
(Converter)(str);
} catch (const fmt::system_error& e) {
@ -55,19 +55,19 @@ void check_utf_conversion_error(
EXPECT_EQ(fmt::to_string(out), error.what());
}
TEST(UtilTest, UTF16ToUTF8Error) {
TEST(util_test, utf16_to_utf8_error) {
check_utf_conversion_error<fmt::detail::utf16_to_utf8, wchar_t>(
"cannot convert string from UTF-16 to UTF-8");
}
TEST(UtilTest, UTF16ToUTF8Convert) {
TEST(util_test, utf16_to_utf8_convert) {
fmt::detail::utf16_to_utf8 u;
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 1)));
EXPECT_EQ(ERROR_INVALID_PARAMETER,
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
}
TEST(UtilTest, FormatWindowsError) {
TEST(os_test, format_windows_error) {
LPWSTR message = 0;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
@ -88,7 +88,7 @@ TEST(UtilTest, FormatWindowsError) {
fmt::to_string(actual_message));
}
TEST(UtilTest, FormatLongWindowsError) {
TEST(os_test, format_long_windows_error) {
LPWSTR message = 0;
// this error code is not available on all Windows platforms and
// Windows SDKs, so do not fail the test if the error string cannot
@ -112,8 +112,8 @@ TEST(UtilTest, FormatLongWindowsError) {
fmt::to_string(actual_message));
}
TEST(UtilTest, WindowsError) {
fmt::system_error error(0, "");
TEST(os_test, windows_error) {
auto error = fmt::system_error(0, "");
try {
throw fmt::windows_error(ERROR_FILE_EXISTS, "test {}", "error");
} catch (const fmt::system_error& e) {
@ -125,7 +125,7 @@ TEST(UtilTest, WindowsError) {
EXPECT_EQ(ERROR_FILE_EXISTS, error.error_code());
}
TEST(UtilTest, ReportWindowsError) {
TEST(os_test, report_windows_error) {
fmt::memory_buffer out;
fmt::detail::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
out.push_back('\n');
@ -140,21 +140,15 @@ TEST(UtilTest, ReportWindowsError) {
using fmt::file;
// Checks if the file is open by reading one character from it.
static bool isopen(int fd) {
bool isclosed(int fd) {
char buffer;
return FMT_POSIX(read(fd, &buffer, 1)) == 1;
}
static bool isclosed(int fd) {
char buffer;
std::streamsize result = 0;
auto result = std::streamsize();
SUPPRESS_ASSERT(result = FMT_POSIX(read(fd, &buffer, 1)));
return result == -1 && errno == EBADF;
}
// Opens a file for reading.
static file open_file() {
file open_file() {
file read_end, write_end;
file::pipe(read_end, write_end);
write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT));
@ -163,7 +157,7 @@ static file open_file() {
}
// Attempts to write a string to a file.
static void write(file& f, fmt::string_view s) {
void write(file& f, fmt::string_view s) {
size_t num_chars_left = s.size();
const char* ptr = s.data();
do {
@ -175,12 +169,12 @@ static void write(file& f, fmt::string_view s) {
} while (num_chars_left != 0);
}
TEST(BufferedFileTest, DefaultCtor) {
buffered_file f;
TEST(buffered_file_test, default_ctor) {
auto f = buffered_file();
EXPECT_TRUE(f.get() == nullptr);
}
TEST(BufferedFileTest, MoveCtor) {
TEST(buffered_file_test, move_ctor) {
buffered_file bf = open_buffered_file();
FILE* fp = bf.get();
EXPECT_TRUE(fp != nullptr);
@ -189,7 +183,7 @@ TEST(BufferedFileTest, MoveCtor) {
EXPECT_TRUE(bf.get() == nullptr);
}
TEST(BufferedFileTest, MoveAssignment) {
TEST(buffered_file_test, move_assignment) {
buffered_file bf = open_buffered_file();
FILE* fp = bf.get();
EXPECT_TRUE(fp != nullptr);
@ -199,7 +193,7 @@ TEST(BufferedFileTest, MoveAssignment) {
EXPECT_TRUE(bf.get() == nullptr);
}
TEST(BufferedFileTest, MoveAssignmentClosesFile) {
TEST(buffered_file_test, move_assignment_closes_file) {
buffered_file bf = open_buffered_file();
buffered_file bf2 = open_buffered_file();
int old_fd = bf2.fileno();
@ -207,27 +201,27 @@ TEST(BufferedFileTest, MoveAssignmentClosesFile) {
EXPECT_TRUE(isclosed(old_fd));
}
TEST(BufferedFileTest, MoveFromTemporaryInCtor) {
TEST(buffered_file_test, move_from_temporary_in_ctor) {
FILE* fp = nullptr;
buffered_file f(open_buffered_file(&fp));
buffered_file f = open_buffered_file(&fp);
EXPECT_EQ(fp, f.get());
}
TEST(BufferedFileTest, MoveFromTemporaryInAssignment) {
TEST(buffered_file_test, move_from_temporary_in_assignment) {
FILE* fp = nullptr;
buffered_file f;
auto f = buffered_file();
f = open_buffered_file(&fp);
EXPECT_EQ(fp, f.get());
}
TEST(BufferedFileTest, MoveFromTemporaryInAssignmentClosesFile) {
TEST(buffered_file_test, move_from_temporary_in_assignment_closes_file) {
buffered_file f = open_buffered_file();
int old_fd = f.fileno();
f = open_buffered_file();
EXPECT_TRUE(isclosed(old_fd));
}
TEST(BufferedFileTest, CloseFileInDtor) {
TEST(buffered_file_test, close_file_in_dtor) {
int fd = 0;
{
buffered_file f = open_buffered_file();
@ -236,8 +230,9 @@ TEST(BufferedFileTest, CloseFileInDtor) {
EXPECT_TRUE(isclosed(fd));
}
TEST(BufferedFileTest, CloseErrorInDtor) {
std::unique_ptr<buffered_file> f(new buffered_file(open_buffered_file()));
TEST(buffered_file_test, close_error_in_dtor) {
auto f =
std::unique_ptr<buffered_file>(new buffered_file(open_buffered_file()));
EXPECT_WRITE(
stderr,
{
@ -251,7 +246,7 @@ TEST(BufferedFileTest, CloseErrorInDtor) {
format_system_error(EBADF, "cannot close file") + "\n");
}
TEST(BufferedFileTest, Close) {
TEST(buffered_file_test, close) {
buffered_file f = open_buffered_file();
int fd = f.fileno();
f.close();
@ -259,27 +254,27 @@ TEST(BufferedFileTest, Close) {
EXPECT_TRUE(isclosed(fd));
}
TEST(BufferedFileTest, CloseError) {
TEST(buffered_file_test, close_error) {
buffered_file f = open_buffered_file();
FMT_POSIX(close(f.fileno()));
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
EXPECT_TRUE(f.get() == nullptr);
}
TEST(BufferedFileTest, Fileno) {
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);
}
TEST(OStreamTest, Move) {
TEST(ostream_test, move) {
fmt::ostream out = fmt::output_file("test-file");
fmt::ostream moved(std::move(out));
moved.print("hello");
}
TEST(OStreamTest, MoveWhileHoldingData) {
TEST(ostream_test, move_while_holding_data) {
{
fmt::ostream out = fmt::output_file("test-file");
out.print("Hello, ");
@ -292,15 +287,16 @@ TEST(OStreamTest, MoveWhileHoldingData) {
}
}
TEST(OStreamTest, Print) {
TEST(ostream_test, print) {
fmt::ostream out = fmt::output_file("test-file");
out.print("The answer is {}.\n", fmt::join(std::initializer_list<int>{42}, ", "));
out.print("The answer is {}.\n",
fmt::join(std::initializer_list<int>{42}, ", "));
out.close();
file in("test-file", file::RDONLY);
EXPECT_READ(in, "The answer is 42.\n");
}
TEST(OStreamTest, BufferBoundary) {
TEST(ostream_test, buffer_boundary) {
auto str = std::string(4096, 'x');
fmt::ostream out = fmt::output_file("test-file");
out.print("{}", str);
@ -310,15 +306,15 @@ TEST(OStreamTest, BufferBoundary) {
EXPECT_READ(in, str + str);
}
TEST(OStreamTest, BufferSize) {
fmt::ostream out = fmt::output_file("test-file", fmt::buffer_size=1);
TEST(ostream_test, buffer_size) {
fmt::ostream out = fmt::output_file("test-file", fmt::buffer_size = 1);
out.print("{}", "foo");
out.close();
file in("test-file", file::RDONLY);
EXPECT_READ(in, "foo");
}
TEST(OStreamTest, Truncate) {
TEST(ostream_test, truncate) {
{
fmt::ostream out = fmt::output_file("test-file");
out.print("0123456789");
@ -331,25 +327,28 @@ TEST(OStreamTest, Truncate) {
EXPECT_EQ("foo", read(in, 4));
}
TEST(FileTest, DefaultCtor) {
TEST(file_test, default_ctor) {
file f;
EXPECT_EQ(-1, f.descriptor());
}
TEST(FileTest, OpenBufferedFileInCtor) {
TEST(file_test, open_buffered_file_in_ctor) {
FILE* fp = safe_fopen("test-file", "w");
std::fputs(FILE_CONTENT, fp);
std::fclose(fp);
file f("test-file", file::RDONLY);
ASSERT_TRUE(isopen(f.descriptor()));
// Check if the file is open by reading one character from it.
char buffer;
bool isopen = FMT_POSIX(read(f.descriptor(), &buffer, 1)) == 1;
ASSERT_TRUE(isopen);
}
TEST(FileTest, OpenBufferedFileError) {
TEST(file_test, open_buffered_file_error) {
EXPECT_SYSTEM_ERROR(file("nonexistent", file::RDONLY), ENOENT,
"cannot open file nonexistent");
}
TEST(FileTest, MoveCtor) {
TEST(file_test, move_ctor) {
file f = open_file();
int fd = f.descriptor();
EXPECT_NE(-1, fd);
@ -358,7 +357,7 @@ TEST(FileTest, MoveCtor) {
EXPECT_EQ(-1, f.descriptor());
}
TEST(FileTest, MoveAssignment) {
TEST(file_test, move_assignment) {
file f = open_file();
int fd = f.descriptor();
EXPECT_NE(-1, fd);
@ -368,7 +367,7 @@ TEST(FileTest, MoveAssignment) {
EXPECT_EQ(-1, f.descriptor());
}
TEST(FileTest, MoveAssignmentClosesFile) {
TEST(file_test, move_assignment_closes_file) {
file f = open_file();
file f2 = open_file();
int old_fd = f2.descriptor();
@ -376,34 +375,34 @@ TEST(FileTest, MoveAssignmentClosesFile) {
EXPECT_TRUE(isclosed(old_fd));
}
static file OpenBufferedFile(int& fd) {
file open_buffered_file(int& fd) {
file f = open_file();
fd = f.descriptor();
return f;
}
TEST(FileTest, MoveFromTemporaryInCtor) {
TEST(file_test, move_from_temporary_in_ctor) {
int fd = 0xdead;
file f(OpenBufferedFile(fd));
file f(open_buffered_file(fd));
EXPECT_EQ(fd, f.descriptor());
}
TEST(FileTest, MoveFromTemporaryInAssignment) {
TEST(file_test, move_from_temporary_in_assignment) {
int fd = 0xdead;
file f;
f = OpenBufferedFile(fd);
f = open_buffered_file(fd);
EXPECT_EQ(fd, f.descriptor());
}
TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
TEST(file_test, move_from_temporary_in_assignment_closes_file) {
int fd = 0xdead;
file f = open_file();
int old_fd = f.descriptor();
f = OpenBufferedFile(fd);
f = open_buffered_file(fd);
EXPECT_TRUE(isclosed(old_fd));
}
TEST(FileTest, CloseFileInDtor) {
TEST(file_test, close_file_in_dtor) {
int fd = 0;
{
file f = open_file();
@ -412,7 +411,7 @@ TEST(FileTest, CloseFileInDtor) {
EXPECT_TRUE(isclosed(fd));
}
TEST(FileTest, CloseErrorInDtor) {
TEST(file_test, close_error_in_dtor) {
std::unique_ptr<file> f(new file(open_file()));
EXPECT_WRITE(
stderr,
@ -427,7 +426,7 @@ TEST(FileTest, CloseErrorInDtor) {
format_system_error(EBADF, "cannot close file") + "\n");
}
TEST(FileTest, Close) {
TEST(file_test, close) {
file f = open_file();
int fd = f.descriptor();
f.close();
@ -435,19 +434,19 @@ TEST(FileTest, Close) {
EXPECT_TRUE(isclosed(fd));
}
TEST(FileTest, CloseError) {
TEST(file_test, close_error) {
file f = open_file();
FMT_POSIX(close(f.descriptor()));
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
EXPECT_EQ(-1, f.descriptor());
}
TEST(FileTest, Read) {
TEST(file_test, read) {
file f = open_file();
EXPECT_READ(f, FILE_CONTENT);
}
TEST(FileTest, ReadError) {
TEST(file_test, read_error) {
file f("test-file", file::WRONLY);
char buf;
// We intentionally read from a file opened in the write-only mode to
@ -455,7 +454,7 @@ TEST(FileTest, ReadError) {
EXPECT_SYSTEM_ERROR(f.read(&buf, 1), EBADF, "cannot read from file");
}
TEST(FileTest, Write) {
TEST(file_test, write) {
file read_end, write_end;
file::pipe(read_end, write_end);
write(write_end, "test");
@ -463,14 +462,14 @@ TEST(FileTest, Write) {
EXPECT_READ(read_end, "test");
}
TEST(FileTest, WriteError) {
TEST(file_test, write_error) {
file f("test-file", file::RDONLY);
// We intentionally write to a file opened in the read-only mode to
// cause error.
EXPECT_SYSTEM_ERROR(f.write(" ", 1), EBADF, "cannot write to file");
}
TEST(FileTest, Dup) {
TEST(file_test, dup) {
file f = open_file();
file copy = file::dup(f.descriptor());
EXPECT_NE(f.descriptor(), copy.descriptor());
@ -478,14 +477,14 @@ TEST(FileTest, Dup) {
}
# ifndef __COVERITY__
TEST(FileTest, DupError) {
TEST(file_test, dup_error) {
int value = -1;
EXPECT_SYSTEM_ERROR_NOASSERT(file::dup(value), EBADF,
"cannot duplicate file descriptor -1");
}
# endif
TEST(FileTest, Dup2) {
TEST(file_test, dup2) {
file f = open_file();
file copy = open_file();
f.dup2(copy.descriptor());
@ -493,14 +492,14 @@ TEST(FileTest, Dup2) {
EXPECT_READ(copy, FILE_CONTENT);
}
TEST(FileTest, Dup2Error) {
TEST(file_test, dup2_error) {
file f = open_file();
EXPECT_SYSTEM_ERROR_NOASSERT(
f.dup2(-1), EBADF,
fmt::format("cannot duplicate file descriptor {} to -1", f.descriptor()));
}
TEST(FileTest, Dup2NoExcept) {
TEST(file_test, dup2_noexcept) {
file f = open_file();
file copy = open_file();
error_code ec;
@ -510,14 +509,14 @@ TEST(FileTest, Dup2NoExcept) {
EXPECT_READ(copy, FILE_CONTENT);
}
TEST(FileTest, Dup2NoExceptError) {
TEST(file_test, dup2_noexcept_error) {
file f = open_file();
error_code ec;
SUPPRESS_ASSERT(f.dup2(-1, ec));
EXPECT_EQ(EBADF, ec.get());
}
TEST(FileTest, Pipe) {
TEST(file_test, pipe) {
file read_end, write_end;
file::pipe(read_end, write_end);
EXPECT_NE(-1, read_end.descriptor());
@ -526,7 +525,7 @@ TEST(FileTest, Pipe) {
EXPECT_READ(read_end, "test");
}
TEST(FileTest, Fdopen) {
TEST(file_test, fdopen) {
file read_end, write_end;
file::pipe(read_end, write_end);
int read_fd = read_end.descriptor();
@ -534,7 +533,7 @@ TEST(FileTest, Fdopen) {
}
# ifdef FMT_LOCALE
TEST(LocaleTest, Strtod) {
TEST(locale_test, strtod) {
fmt::locale loc;
const char *start = "4.2", *ptr = start;
EXPECT_EQ(4.2, loc.strtod(ptr));

View File

@ -37,6 +37,11 @@ using testing::_;
using testing::Return;
using testing::StrEq;
template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> {
ScopedMock() { Mock::instance = this; }
~ScopedMock() { Mock::instance = nullptr; }
};
namespace {
int open_count;
int close_count;