mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 21:16:56 +00:00
More tests. Fix warnings.
This commit is contained in:
parent
952fd7a756
commit
f578bc3a4a
@ -716,7 +716,7 @@ TEST(FileTest, Fdopen) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
int read_fd = read_end.descriptor();
|
||||
EXPECT_EQ(read_fd, fileno(read_end.fdopen("r").get()));
|
||||
EXPECT_EQ(read_fd, FMT_POSIX(fileno(read_end.fdopen("r").get())));
|
||||
}
|
||||
|
||||
TEST(FileTest, FdopenError) {
|
||||
|
@ -69,14 +69,14 @@ inline std::size_t ConvertRWCount(std::size_t count) { return count; }
|
||||
}
|
||||
|
||||
BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
|
||||
if (file_ && std::fclose(file_) != 0)
|
||||
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
||||
fmt::ReportSystemError(errno, "cannot close file");
|
||||
}
|
||||
|
||||
void BufferedFile::close() {
|
||||
if (!file_)
|
||||
return;
|
||||
int result = std::fclose(file_);
|
||||
int result = FMT_SYSTEM(fclose(file_));
|
||||
file_ = 0;
|
||||
if (result != 0)
|
||||
fmt::ThrowSystemError(errno, "cannot close file");
|
||||
|
@ -42,11 +42,11 @@ int close_count;
|
||||
int dup_count;
|
||||
int dup2_count;
|
||||
int fdopen_count;
|
||||
int fileno_count;
|
||||
int read_count;
|
||||
int write_count;
|
||||
int pipe_count;
|
||||
int fclose_count;
|
||||
int fileno_count;
|
||||
}
|
||||
|
||||
#define EMULATE_EINTR(func, error_result) \
|
||||
@ -72,39 +72,34 @@ errno_t test::sopen_s(
|
||||
|
||||
int test::close(int fildes) {
|
||||
// Close the file first because close shouldn't be retried.
|
||||
int result = ::close(fildes);
|
||||
int result = ::FMT_POSIX(close(fildes));
|
||||
EMULATE_EINTR(close, -1);
|
||||
return result;
|
||||
}
|
||||
|
||||
int test::dup(int fildes) {
|
||||
EMULATE_EINTR(dup, -1);
|
||||
return ::dup(fildes);
|
||||
return ::FMT_POSIX(dup(fildes));
|
||||
}
|
||||
|
||||
int test::dup2(int fildes, int fildes2) {
|
||||
EMULATE_EINTR(dup2, -1);
|
||||
return ::dup2(fildes, fildes2);
|
||||
return ::FMT_POSIX(dup2(fildes, fildes2));
|
||||
}
|
||||
|
||||
FILE *test::fdopen(int fildes, const char *mode) {
|
||||
EMULATE_EINTR(fdopen, 0);
|
||||
return ::fdopen(fildes, mode);
|
||||
}
|
||||
|
||||
int test::fileno(FILE *stream) {
|
||||
EMULATE_EINTR(fileno, -1);
|
||||
return ::fileno(stream);
|
||||
return ::FMT_POSIX(fdopen(fildes, mode));
|
||||
}
|
||||
|
||||
test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
|
||||
EMULATE_EINTR(read, -1);
|
||||
return ::read(fildes, buf, nbyte);
|
||||
return ::FMT_POSIX(read(fildes, buf, nbyte));
|
||||
}
|
||||
|
||||
test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
|
||||
EMULATE_EINTR(write, -1);
|
||||
return ::write(fildes, buf, nbyte);
|
||||
return ::FMT_POSIX(write(fildes, buf, nbyte));
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
@ -124,6 +119,11 @@ int test::fclose(FILE *stream) {
|
||||
return ::fclose(stream);
|
||||
}
|
||||
|
||||
int test::fileno(FILE *stream) {
|
||||
EMULATE_EINTR(fileno, -1);
|
||||
return ::fileno(stream);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
# define EXPECT_RETRY(statement, func, message) \
|
||||
func##_count = 1; \
|
||||
@ -248,5 +248,38 @@ TEST(FileTest, FdopenNoRetry) {
|
||||
fdopen_count = 0;
|
||||
}
|
||||
|
||||
// TODO: test retry on EINTR in fclose & fileno
|
||||
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile *f = new BufferedFile(read_end.fdopen("r"));
|
||||
int saved_fclose_count = 0;
|
||||
EXPECT_WRITE(stderr, {
|
||||
fclose_count = 1;
|
||||
delete f;
|
||||
saved_fclose_count = fclose_count;
|
||||
fclose_count = 0;
|
||||
}, FormatSystemErrorMessage(EINTR, "cannot close file") + "\n");
|
||||
EXPECT_EQ(2, saved_fclose_count);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile f = read_end.fdopen("r");
|
||||
fclose_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
|
||||
EXPECT_EQ(2, fclose_count);
|
||||
fclose_count = 0;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, FilenoNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile f = read_end.fdopen("r");
|
||||
fileno_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(f.fileno(), EINTR, "cannot get file descriptor");
|
||||
EXPECT_EQ(2, fileno_count);
|
||||
fileno_count = 0;
|
||||
}
|
||||
|
||||
// TODO: test ConvertRWCount
|
||||
|
@ -51,7 +51,6 @@ int dup(int fildes);
|
||||
int dup2(int fildes, int fildes2);
|
||||
|
||||
FILE *fdopen(int fildes, const char *mode);
|
||||
int fileno(FILE *stream);
|
||||
|
||||
ssize_t read(int fildes, void *buf, size_t nbyte);
|
||||
ssize_t write(int fildes, const void *buf, size_t nbyte);
|
||||
@ -63,6 +62,7 @@ int pipe(int *pfds, unsigned psize, int textmode);
|
||||
#endif
|
||||
|
||||
int fclose(FILE *stream);
|
||||
int fileno(FILE *stream);
|
||||
|
||||
} // namespace test
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user