Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
b76bb796
Commit
b76bb796
authored
May 19, 2018
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve naming consistency
parent
fbd51534
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
153 additions
and
153 deletions
+153
-153
include/fmt/posix.h
include/fmt/posix.h
+25
-25
src/posix.cc
src/posix.cc
+16
-16
test/gtest-extra-test.cc
test/gtest-extra-test.cc
+18
-18
test/gtest-extra.cc
test/gtest-extra.cc
+8
-8
test/gtest-extra.h
test/gtest-extra.h
+3
-3
test/posix-mock-test.cc
test/posix-mock-test.cc
+33
-33
test/posix-test.cc
test/posix-test.cc
+46
-46
test/printf-test.cc
test/printf-test.cc
+2
-2
test/util.cc
test/util.cc
+2
-2
No files found.
include/fmt/posix.h
View file @
b76bb796
...
@@ -113,12 +113,12 @@ typedef basic_cstring_view<char> cstring_view;
...
@@ -113,12 +113,12 @@ typedef basic_cstring_view<char> cstring_view;
typedef
basic_cstring_view
<
wchar_t
>
wcstring_view
;
typedef
basic_cstring_view
<
wchar_t
>
wcstring_view
;
// An error code.
// An error code.
class
ErrorC
ode
{
class
error_c
ode
{
private:
private:
int
value_
;
int
value_
;
public:
public:
explicit
ErrorC
ode
(
int
value
=
0
)
FMT_NOEXCEPT
:
value_
(
value
)
{}
explicit
error_c
ode
(
int
value
=
0
)
FMT_NOEXCEPT
:
value_
(
value
)
{}
int
get
()
const
FMT_NOEXCEPT
{
return
value_
;
}
int
get
()
const
FMT_NOEXCEPT
{
return
value_
;
}
};
};
...
@@ -128,7 +128,7 @@ class buffered_file {
...
@@ -128,7 +128,7 @@ class buffered_file {
private:
private:
FILE
*
file_
;
FILE
*
file_
;
friend
class
F
ile
;
friend
class
f
ile
;
explicit
buffered_file
(
FILE
*
f
)
:
file_
(
f
)
{}
explicit
buffered_file
(
FILE
*
f
)
:
file_
(
f
)
{}
...
@@ -222,18 +222,18 @@ public:
...
@@ -222,18 +222,18 @@ public:
}
}
};
};
// A file. Closed file is represented by a
F
ile object with descriptor -1.
// A file. Closed file is represented by a
f
ile object with descriptor -1.
// Methods that are not declared with FMT_NOEXCEPT may throw
// Methods that are not declared with FMT_NOEXCEPT may throw
// fmt::system_error in case of failure. Note that some errors such as
// fmt::system_error in case of failure. Note that some errors such as
// closing the file multiple times will cause a crash on Windows rather
// closing the file multiple times will cause a crash on Windows rather
// than an exception. You can get standard behavior by overriding the
// than an exception. You can get standard behavior by overriding the
// invalid parameter handler with _set_invalid_parameter_handler.
// invalid parameter handler with _set_invalid_parameter_handler.
class
F
ile
{
class
f
ile
{
private:
private:
int
fd_
;
// File descriptor.
int
fd_
;
// File descriptor.
// Constructs a
F
ile object with a given descriptor.
// Constructs a
f
ile object with a given descriptor.
explicit
F
ile
(
int
fd
)
:
fd_
(
fd
)
{}
explicit
f
ile
(
int
fd
)
:
fd_
(
fd
)
{}
public:
public:
// Possible values for the oflag argument to the constructor.
// Possible values for the oflag argument to the constructor.
...
@@ -243,11 +243,11 @@ class File {
...
@@ -243,11 +243,11 @@ class File {
RDWR
=
FMT_POSIX
(
O_RDWR
)
// Open for reading and writing.
RDWR
=
FMT_POSIX
(
O_RDWR
)
// Open for reading and writing.
};
};
// Constructs a
F
ile object which doesn't represent any file.
// Constructs a
f
ile object which doesn't represent any file.
F
ile
()
FMT_NOEXCEPT
:
fd_
(
-
1
)
{}
f
ile
()
FMT_NOEXCEPT
:
fd_
(
-
1
)
{}
// Opens a file and constructs a
F
ile object representing this file.
// Opens a file and constructs a
f
ile object representing this file.
FMT_API
F
ile
(
cstring_view
path
,
int
oflag
);
FMT_API
f
ile
(
cstring_view
path
,
int
oflag
);
#if !FMT_USE_RVALUE_REFERENCES
#if !FMT_USE_RVALUE_REFERENCES
// Emulate a move constructor and a move assignment operator if rvalue
// Emulate a move constructor and a move assignment operator if rvalue
...
@@ -262,22 +262,22 @@ class File {
...
@@ -262,22 +262,22 @@ class File {
public:
public:
// A "move constructor" for moving from a temporary.
// A "move constructor" for moving from a temporary.
F
ile
(
Proxy
p
)
FMT_NOEXCEPT
:
fd_
(
p
.
fd
)
{}
f
ile
(
Proxy
p
)
FMT_NOEXCEPT
:
fd_
(
p
.
fd
)
{}
// A "move constructor" for moving from an lvalue.
// A "move constructor" for moving from an lvalue.
File
(
F
ile
&
other
)
FMT_NOEXCEPT
:
fd_
(
other
.
fd_
)
{
file
(
f
ile
&
other
)
FMT_NOEXCEPT
:
fd_
(
other
.
fd_
)
{
other
.
fd_
=
-
1
;
other
.
fd_
=
-
1
;
}
}
// A "move assignment operator" for moving from a temporary.
// A "move assignment operator" for moving from a temporary.
F
ile
&
operator
=
(
Proxy
p
)
{
f
ile
&
operator
=
(
Proxy
p
)
{
close
();
close
();
fd_
=
p
.
fd
;
fd_
=
p
.
fd
;
return
*
this
;
return
*
this
;
}
}
// A "move assignment operator" for moving from an lvalue.
// A "move assignment operator" for moving from an lvalue.
File
&
operator
=
(
F
ile
&
other
)
{
file
&
operator
=
(
f
ile
&
other
)
{
close
();
close
();
fd_
=
other
.
fd_
;
fd_
=
other
.
fd_
;
other
.
fd_
=
-
1
;
other
.
fd_
=
-
1
;
...
@@ -285,7 +285,7 @@ class File {
...
@@ -285,7 +285,7 @@ class File {
}
}
// Returns a proxy object for moving from a temporary:
// Returns a proxy object for moving from a temporary:
//
File file = F
ile(...);
//
file f = f
ile(...);
operator
Proxy
()
FMT_NOEXCEPT
{
operator
Proxy
()
FMT_NOEXCEPT
{
Proxy
p
=
{
fd_
};
Proxy
p
=
{
fd_
};
fd_
=
-
1
;
fd_
=
-
1
;
...
@@ -294,14 +294,14 @@ class File {
...
@@ -294,14 +294,14 @@ class File {
#else
#else
private:
private:
FMT_DISALLOW_COPY_AND_ASSIGN
(
F
ile
);
FMT_DISALLOW_COPY_AND_ASSIGN
(
f
ile
);
public:
public:
File
(
F
ile
&&
other
)
FMT_NOEXCEPT
:
fd_
(
other
.
fd_
)
{
file
(
f
ile
&&
other
)
FMT_NOEXCEPT
:
fd_
(
other
.
fd_
)
{
other
.
fd_
=
-
1
;
other
.
fd_
=
-
1
;
}
}
File
&
operator
=
(
F
ile
&&
other
)
{
file
&
operator
=
(
f
ile
&&
other
)
{
close
();
close
();
fd_
=
other
.
fd_
;
fd_
=
other
.
fd_
;
other
.
fd_
=
-
1
;
other
.
fd_
=
-
1
;
...
@@ -310,7 +310,7 @@ class File {
...
@@ -310,7 +310,7 @@ class File {
#endif
#endif
// Destroys the object closing the file it represents if any.
// Destroys the object closing the file it represents if any.
FMT_API
~
F
ile
()
FMT_DTOR_NOEXCEPT
;
FMT_API
~
f
ile
()
FMT_DTOR_NOEXCEPT
;
// Returns the file descriptor.
// Returns the file descriptor.
int
descriptor
()
const
FMT_NOEXCEPT
{
return
fd_
;
}
int
descriptor
()
const
FMT_NOEXCEPT
{
return
fd_
;
}
...
@@ -330,7 +330,7 @@ class File {
...
@@ -330,7 +330,7 @@ class File {
// Duplicates a file descriptor with the dup function and returns
// Duplicates a file descriptor with the dup function and returns
// the duplicate as a file object.
// the duplicate as a file object.
FMT_API
static
F
ile
dup
(
int
fd
);
FMT_API
static
f
ile
dup
(
int
fd
);
// Makes fd be the copy of this file descriptor, closing fd first if
// Makes fd be the copy of this file descriptor, closing fd first if
// necessary.
// necessary.
...
@@ -338,14 +338,14 @@ class File {
...
@@ -338,14 +338,14 @@ class File {
// Makes fd be the copy of this file descriptor, closing fd first if
// Makes fd be the copy of this file descriptor, closing fd first if
// necessary.
// necessary.
FMT_API
void
dup2
(
int
fd
,
ErrorC
ode
&
ec
)
FMT_NOEXCEPT
;
FMT_API
void
dup2
(
int
fd
,
error_c
ode
&
ec
)
FMT_NOEXCEPT
;
// Creates a pipe setting up read_end and write_end file objects for reading
// Creates a pipe setting up read_end and write_end file objects for reading
// and writing respectively.
// and writing respectively.
FMT_API
static
void
pipe
(
File
&
read_end
,
F
ile
&
write_end
);
FMT_API
static
void
pipe
(
file
&
read_end
,
f
ile
&
write_end
);
// Creates a buffered_file object associated with this file and detaches
// Creates a buffered_file object associated with this file and detaches
// this
F
ile object from the file.
// this
f
ile object from the file.
FMT_API
buffered_file
fdopen
(
const
char
*
mode
);
FMT_API
buffered_file
fdopen
(
const
char
*
mode
);
};
};
...
@@ -410,7 +410,7 @@ FMT_END_NAMESPACE
...
@@ -410,7 +410,7 @@ FMT_END_NAMESPACE
namespace
std
{
namespace
std
{
// For compatibility with C++98.
// For compatibility with C++98.
inline
fmt
::
buffered_file
&
move
(
fmt
::
buffered_file
&
f
)
{
return
f
;
}
inline
fmt
::
buffered_file
&
move
(
fmt
::
buffered_file
&
f
)
{
return
f
;
}
inline
fmt
::
File
&
move
(
fmt
::
F
ile
&
f
)
{
return
f
;
}
inline
fmt
::
file
&
move
(
fmt
::
f
ile
&
f
)
{
return
f
;
}
}
}
#endif
#endif
...
...
src/posix.cc
View file @
b76bb796
...
@@ -97,7 +97,7 @@ int buffered_file::fileno() const {
...
@@ -97,7 +97,7 @@ int buffered_file::fileno() const {
return
fd
;
return
fd
;
}
}
File
::
F
ile
(
cstring_view
path
,
int
oflag
)
{
file
::
f
ile
(
cstring_view
path
,
int
oflag
)
{
int
mode
=
S_IRUSR
|
S_IWUSR
;
int
mode
=
S_IRUSR
|
S_IWUSR
;
#if defined(_WIN32) && !defined(__MINGW32__)
#if defined(_WIN32) && !defined(__MINGW32__)
fd_
=
-
1
;
fd_
=
-
1
;
...
@@ -109,14 +109,14 @@ File::File(cstring_view path, int oflag) {
...
@@ -109,14 +109,14 @@ File::File(cstring_view path, int oflag) {
FMT_THROW
(
system_error
(
errno
,
"cannot open file {}"
,
path
.
c_str
()));
FMT_THROW
(
system_error
(
errno
,
"cannot open file {}"
,
path
.
c_str
()));
}
}
File
::~
F
ile
()
FMT_NOEXCEPT
{
file
::~
f
ile
()
FMT_NOEXCEPT
{
// Don't retry close in case of EINTR!
// Don't retry close in case of EINTR!
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
if
(
fd_
!=
-
1
&&
FMT_POSIX_CALL
(
close
(
fd_
))
!=
0
)
if
(
fd_
!=
-
1
&&
FMT_POSIX_CALL
(
close
(
fd_
))
!=
0
)
report_system_error
(
errno
,
"cannot close file"
);
report_system_error
(
errno
,
"cannot close file"
);
}
}
void
F
ile
::
close
()
{
void
f
ile
::
close
()
{
if
(
fd_
==
-
1
)
if
(
fd_
==
-
1
)
return
;
return
;
// Don't retry close in case of EINTR!
// Don't retry close in case of EINTR!
...
@@ -127,7 +127,7 @@ void File::close() {
...
@@ -127,7 +127,7 @@ void File::close() {
FMT_THROW
(
system_error
(
errno
,
"cannot close file"
));
FMT_THROW
(
system_error
(
errno
,
"cannot close file"
));
}
}
long
long
F
ile
::
size
()
const
{
long
long
f
ile
::
size
()
const
{
#ifdef _WIN32
#ifdef _WIN32
// Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT
// Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT
// is less than 0x0500 as is the case with some default MinGW builds.
// is less than 0x0500 as is the case with some default MinGW builds.
...
@@ -148,12 +148,12 @@ long long File::size() const {
...
@@ -148,12 +148,12 @@ long long File::size() const {
if
(
FMT_POSIX_CALL
(
fstat
(
fd_
,
&
file_stat
))
==
-
1
)
if
(
FMT_POSIX_CALL
(
fstat
(
fd_
,
&
file_stat
))
==
-
1
)
FMT_THROW
(
system_error
(
errno
,
"cannot get file attributes"
));
FMT_THROW
(
system_error
(
errno
,
"cannot get file attributes"
));
static_assert
(
sizeof
(
long
long
)
>=
sizeof
(
file_stat
.
st_size
),
static_assert
(
sizeof
(
long
long
)
>=
sizeof
(
file_stat
.
st_size
),
"return type of
F
ile::size is not large enough"
);
"return type of
f
ile::size is not large enough"
);
return
file_stat
.
st_size
;
return
file_stat
.
st_size
;
#endif
#endif
}
}
std
::
size_t
F
ile
::
read
(
void
*
buffer
,
std
::
size_t
count
)
{
std
::
size_t
f
ile
::
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
))));
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
read
(
fd_
,
buffer
,
convert_rwcount
(
count
))));
if
(
result
<
0
)
if
(
result
<
0
)
...
@@ -161,7 +161,7 @@ std::size_t File::read(void *buffer, std::size_t count) {
...
@@ -161,7 +161,7 @@ std::size_t File::read(void *buffer, std::size_t count) {
return
internal
::
to_unsigned
(
result
);
return
internal
::
to_unsigned
(
result
);
}
}
std
::
size_t
F
ile
::
write
(
const
void
*
buffer
,
std
::
size_t
count
)
{
std
::
size_t
f
ile
::
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
))));
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
write
(
fd_
,
buffer
,
convert_rwcount
(
count
))));
if
(
result
<
0
)
if
(
result
<
0
)
...
@@ -169,16 +169,16 @@ std::size_t File::write(const void *buffer, std::size_t count) {
...
@@ -169,16 +169,16 @@ std::size_t File::write(const void *buffer, std::size_t count) {
return
internal
::
to_unsigned
(
result
);
return
internal
::
to_unsigned
(
result
);
}
}
File
F
ile
::
dup
(
int
fd
)
{
file
f
ile
::
dup
(
int
fd
)
{
// Don't retry as dup doesn't return EINTR.
// Don't retry as dup doesn't return EINTR.
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html
int
new_fd
=
FMT_POSIX_CALL
(
dup
(
fd
));
int
new_fd
=
FMT_POSIX_CALL
(
dup
(
fd
));
if
(
new_fd
==
-
1
)
if
(
new_fd
==
-
1
)
FMT_THROW
(
system_error
(
errno
,
"cannot duplicate file descriptor {}"
,
fd
));
FMT_THROW
(
system_error
(
errno
,
"cannot duplicate file descriptor {}"
,
fd
));
return
F
ile
(
new_fd
);
return
f
ile
(
new_fd
);
}
}
void
F
ile
::
dup2
(
int
fd
)
{
void
f
ile
::
dup2
(
int
fd
)
{
int
result
=
0
;
int
result
=
0
;
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
dup2
(
fd_
,
fd
)));
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
dup2
(
fd_
,
fd
)));
if
(
result
==
-
1
)
{
if
(
result
==
-
1
)
{
...
@@ -187,14 +187,14 @@ void File::dup2(int fd) {
...
@@ -187,14 +187,14 @@ void File::dup2(int fd) {
}
}
}
}
void
File
::
dup2
(
int
fd
,
ErrorC
ode
&
ec
)
FMT_NOEXCEPT
{
void
file
::
dup2
(
int
fd
,
error_c
ode
&
ec
)
FMT_NOEXCEPT
{
int
result
=
0
;
int
result
=
0
;
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
dup2
(
fd_
,
fd
)));
FMT_RETRY
(
result
,
FMT_POSIX_CALL
(
dup2
(
fd_
,
fd
)));
if
(
result
==
-
1
)
if
(
result
==
-
1
)
ec
=
ErrorC
ode
(
errno
);
ec
=
error_c
ode
(
errno
);
}
}
void
File
::
pipe
(
File
&
read_end
,
F
ile
&
write_end
)
{
void
file
::
pipe
(
file
&
read_end
,
f
ile
&
write_end
)
{
// Close the descriptors first to make sure that assignments don't throw
// Close the descriptors first to make sure that assignments don't throw
// and there are no leaks.
// and there are no leaks.
read_end
.
close
();
read_end
.
close
();
...
@@ -213,11 +213,11 @@ void File::pipe(File &read_end, File &write_end) {
...
@@ -213,11 +213,11 @@ void File::pipe(File &read_end, File &write_end) {
FMT_THROW
(
system_error
(
errno
,
"cannot create pipe"
));
FMT_THROW
(
system_error
(
errno
,
"cannot create pipe"
));
// The following assignments don't throw because read_fd and write_fd
// The following assignments don't throw because read_fd and write_fd
// are closed.
// are closed.
read_end
=
F
ile
(
fds
[
0
]);
read_end
=
f
ile
(
fds
[
0
]);
write_end
=
F
ile
(
fds
[
1
]);
write_end
=
f
ile
(
fds
[
1
]);
}
}
buffered_file
F
ile
::
fdopen
(
const
char
*
mode
)
{
buffered_file
f
ile
::
fdopen
(
const
char
*
mode
)
{
// Don't retry as fdopen doesn't return EINTR.
// Don't retry as fdopen doesn't return EINTR.
FILE
*
f
=
FMT_POSIX_CALL
(
fdopen
(
fd_
,
mode
));
FILE
*
f
=
FMT_POSIX_CALL
(
fdopen
(
fd_
,
mode
));
if
(
!
f
)
if
(
!
f
)
...
...
test/gtest-extra-test.cc
View file @
b76bb796
...
@@ -307,17 +307,17 @@ TEST(UtilTest, FormatSystemError) {
...
@@ -307,17 +307,17 @@ TEST(UtilTest, FormatSystemError) {
#if FMT_USE_FILE_DESCRIPTORS
#if FMT_USE_FILE_DESCRIPTORS
using
fmt
::
buffered_file
;
using
fmt
::
buffered_file
;
using
fmt
::
ErrorC
ode
;
using
fmt
::
error_c
ode
;
using
fmt
::
F
ile
;
using
fmt
::
f
ile
;
TEST
(
ErrorCodeTest
,
Ctor
)
{
TEST
(
ErrorCodeTest
,
Ctor
)
{
EXPECT_EQ
(
0
,
ErrorC
ode
().
get
());
EXPECT_EQ
(
0
,
error_c
ode
().
get
());
EXPECT_EQ
(
42
,
ErrorC
ode
(
42
).
get
());
EXPECT_EQ
(
42
,
error_c
ode
(
42
).
get
());
}
}
TEST
(
OutputRedirectTest
,
ScopedRedirect
)
{
TEST
(
OutputRedirectTest
,
ScopedRedirect
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
{
{
buffered_file
file
(
write_end
.
fdopen
(
"w"
));
buffered_file
file
(
write_end
.
fdopen
(
"w"
));
std
::
fprintf
(
file
.
get
(),
"[[["
);
std
::
fprintf
(
file
.
get
(),
"[[["
);
...
@@ -332,10 +332,10 @@ TEST(OutputRedirectTest, ScopedRedirect) {
...
@@ -332,10 +332,10 @@ TEST(OutputRedirectTest, ScopedRedirect) {
// Test that OutputRedirect handles errors in flush correctly.
// Test that OutputRedirect handles errors in flush correctly.
TEST
(
OutputRedirectTest
,
FlushErrorInCtor
)
{
TEST
(
OutputRedirectTest
,
FlushErrorInCtor
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
int
write_fd
=
write_end
.
descriptor
();
int
write_fd
=
write_end
.
descriptor
();
F
ile
write_copy
=
write_end
.
dup
(
write_fd
);
f
ile
write_copy
=
write_end
.
dup
(
write_fd
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
// Put a character in a file buffer.
// Put a character in a file buffer.
EXPECT_EQ
(
'x'
,
fputc
(
'x'
,
f
.
get
()));
EXPECT_EQ
(
'x'
,
fputc
(
'x'
,
f
.
get
()));
...
@@ -350,7 +350,7 @@ TEST(OutputRedirectTest, FlushErrorInCtor) {
...
@@ -350,7 +350,7 @@ TEST(OutputRedirectTest, FlushErrorInCtor) {
TEST
(
OutputRedirectTest
,
DupErrorInCtor
)
{
TEST
(
OutputRedirectTest
,
DupErrorInCtor
)
{
buffered_file
f
=
open_buffered_file
();
buffered_file
f
=
open_buffered_file
();
int
fd
=
(
f
.
fileno
)();
int
fd
=
(
f
.
fileno
)();
File
copy
=
F
ile
::
dup
(
fd
);
file
copy
=
f
ile
::
dup
(
fd
);
FMT_POSIX
(
close
(
fd
));
FMT_POSIX
(
close
(
fd
));
scoped_ptr
<
OutputRedirect
>
redir
;
scoped_ptr
<
OutputRedirect
>
redir
;
EXPECT_SYSTEM_ERROR_NOASSERT
(
redir
.
reset
(
new
OutputRedirect
(
f
.
get
())),
EXPECT_SYSTEM_ERROR_NOASSERT
(
redir
.
reset
(
new
OutputRedirect
(
f
.
get
())),
...
@@ -359,8 +359,8 @@ TEST(OutputRedirectTest, DupErrorInCtor) {
...
@@ -359,8 +359,8 @@ TEST(OutputRedirectTest, DupErrorInCtor) {
}
}
TEST
(
OutputRedirectTest
,
RestoreAndRead
)
{
TEST
(
OutputRedirectTest
,
RestoreAndRead
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
buffered_file
file
(
write_end
.
fdopen
(
"w"
));
buffered_file
file
(
write_end
.
fdopen
(
"w"
));
std
::
fprintf
(
file
.
get
(),
"[[["
);
std
::
fprintf
(
file
.
get
(),
"[[["
);
OutputRedirect
redir
(
file
.
get
());
OutputRedirect
redir
(
file
.
get
());
...
@@ -374,10 +374,10 @@ TEST(OutputRedirectTest, RestoreAndRead) {
...
@@ -374,10 +374,10 @@ TEST(OutputRedirectTest, RestoreAndRead) {
// Test that OutputRedirect handles errors in flush correctly.
// Test that OutputRedirect handles errors in flush correctly.
TEST
(
OutputRedirectTest
,
FlushErrorInRestoreAndRead
)
{
TEST
(
OutputRedirectTest
,
FlushErrorInRestoreAndRead
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
int
write_fd
=
write_end
.
descriptor
();
int
write_fd
=
write_end
.
descriptor
();
F
ile
write_copy
=
write_end
.
dup
(
write_fd
);
f
ile
write_copy
=
write_end
.
dup
(
write_fd
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
OutputRedirect
redir
(
f
.
get
());
OutputRedirect
redir
(
f
.
get
());
// Put a character in a file buffer.
// Put a character in a file buffer.
...
@@ -389,10 +389,10 @@ TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
...
@@ -389,10 +389,10 @@ TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
}
}
TEST
(
OutputRedirectTest
,
ErrorInDtor
)
{
TEST
(
OutputRedirectTest
,
ErrorInDtor
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
int
write_fd
=
write_end
.
descriptor
();
int
write_fd
=
write_end
.
descriptor
();
F
ile
write_copy
=
write_end
.
dup
(
write_fd
);
f
ile
write_copy
=
write_end
.
dup
(
write_fd
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
buffered_file
f
=
write_end
.
fdopen
(
"w"
);
scoped_ptr
<
OutputRedirect
>
redir
(
new
OutputRedirect
(
f
.
get
()));
scoped_ptr
<
OutputRedirect
>
redir
(
new
OutputRedirect
(
f
.
get
()));
// Put a character in a file buffer.
// Put a character in a file buffer.
...
...
test/gtest-extra.cc
View file @
b76bb796
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
#if FMT_USE_FILE_DESCRIPTORS
#if FMT_USE_FILE_DESCRIPTORS
using
fmt
::
F
ile
;
using
fmt
::
f
ile
;
void
OutputRedirect
::
flush
()
{
void
OutputRedirect
::
flush
()
{
#if EOF != -1
#if EOF != -1
...
@@ -30,14 +30,14 @@ void OutputRedirect::restore() {
...
@@ -30,14 +30,14 @@ void OutputRedirect::restore() {
original_
.
close
();
original_
.
close
();
}
}
OutputRedirect
::
OutputRedirect
(
FILE
*
f
ile
)
:
file_
(
file
)
{
OutputRedirect
::
OutputRedirect
(
FILE
*
f
)
:
file_
(
f
)
{
flush
();
flush
();
int
fd
=
FMT_POSIX
(
fileno
(
f
ile
));
int
fd
=
FMT_POSIX
(
fileno
(
f
));
// Create a
F
ile object referring to the original file.
// Create a
f
ile object referring to the original file.
original_
=
F
ile
::
dup
(
fd
);
original_
=
f
ile
::
dup
(
fd
);
// Create a pipe.
// Create a pipe.
F
ile
write_end
;
f
ile
write_end
;
F
ile
::
pipe
(
read_end_
,
write_end
);
f
ile
::
pipe
(
read_end_
,
write_end
);
// Connect the passed FILE object to the write end of the pipe.
// Connect the passed FILE object to the write end of the pipe.
write_end
.
dup2
(
fd
);
write_end
.
dup2
(
fd
);
}
}
...
@@ -69,7 +69,7 @@ std::string OutputRedirect::restore_and_read() {
...
@@ -69,7 +69,7 @@ std::string OutputRedirect::restore_and_read() {
return
content
;
return
content
;
}
}
std
::
string
read
(
F
ile
&
f
,
std
::
size_t
count
)
{
std
::
string
read
(
f
ile
&
f
,
std
::
size_t
count
)
{
std
::
string
buffer
(
count
,
'\0'
);
std
::
string
buffer
(
count
,
'\0'
);
std
::
size_t
n
=
0
,
offset
=
0
;
std
::
size_t
n
=
0
,
offset
=
0
;
do
{
do
{
...
...
test/gtest-extra.h
View file @
b76bb796
...
@@ -74,8 +74,8 @@ std::string format_system_error(int error_code, fmt::string_view message);
...
@@ -74,8 +74,8 @@ std::string format_system_error(int error_code, fmt::string_view message);
class
OutputRedirect
{
class
OutputRedirect
{
private:
private:
FILE
*
file_
;
FILE
*
file_
;
fmt
::
F
ile
original_
;
// Original file passed to redirector.
fmt
::
f
ile
original_
;
// Original file passed to redirector.
fmt
::
F
ile
read_end_
;
// Read end of the pipe where the output is redirected.
fmt
::
f
ile
read_end_
;
// Read end of the pipe where the output is redirected.
GTEST_DISALLOW_COPY_AND_ASSIGN_
(
OutputRedirect
);
GTEST_DISALLOW_COPY_AND_ASSIGN_
(
OutputRedirect
);
...
@@ -145,7 +145,7 @@ class SuppressAssert {
...
@@ -145,7 +145,7 @@ class SuppressAssert {
EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
// Attempts to read count characters from a file.
// Attempts to read count characters from a file.
std
::
string
read
(
fmt
::
F
ile
&
f
,
std
::
size_t
count
);
std
::
string
read
(
fmt
::
f
ile
&
f
,
std
::
size_t
count
);
#define EXPECT_READ(file, expected_content) \
#define EXPECT_READ(file, expected_content) \
EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
...
...
test/posix-mock-test.cc
View file @
b76bb796
...
@@ -26,8 +26,8 @@
...
@@ -26,8 +26,8 @@
#include "util.h"
#include "util.h"
using
fmt
::
buffered_file
;
using
fmt
::
buffered_file
;
using
fmt
::
ErrorC
ode
;
using
fmt
::
error_c
ode
;
using
fmt
::
F
ile
;
using
fmt
::
f
ile
;
using
testing
::
internal
::
scoped_ptr
;
using
testing
::
internal
::
scoped_ptr
;
using
testing
::
_
;
using
testing
::
_
;
...
@@ -214,8 +214,8 @@ TEST(UtilTest, GetPageSize) {
...
@@ -214,8 +214,8 @@ TEST(UtilTest, GetPageSize) {
TEST
(
FileTest
,
OpenRetry
)
{
TEST
(
FileTest
,
OpenRetry
)
{
write_file
(
"test"
,
"there must be something here"
);
write_file
(
"test"
,
"there must be something here"
);
scoped_ptr
<
F
ile
>
f
;
scoped_ptr
<
f
ile
>
f
;
EXPECT_RETRY
(
f
.
reset
(
new
File
(
"test"
,
F
ile
::
RDONLY
)),
EXPECT_RETRY
(
f
.
reset
(
new
file
(
"test"
,
f
ile
::
RDONLY
)),
open
,
"cannot open file test"
);
open
,
"cannot open file test"
);
#ifndef _WIN32
#ifndef _WIN32
char
c
=
0
;
char
c
=
0
;
...
@@ -224,9 +224,9 @@ TEST(FileTest, OpenRetry) {
...
@@ -224,9 +224,9 @@ TEST(FileTest, OpenRetry) {
}
}
TEST
(
FileTest
,
CloseNoRetryInDtor
)
{
TEST
(
FileTest
,
CloseNoRetryInDtor
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
scoped_ptr
<
File
>
f
(
new
F
ile
(
std
::
move
(
read_end
)));
scoped_ptr
<
file
>
f
(
new
f
ile
(
std
::
move
(
read_end
)));
int
saved_close_count
=
0
;
int
saved_close_count
=
0
;
EXPECT_WRITE
(
stderr
,
{
EXPECT_WRITE
(
stderr
,
{
close_count
=
1
;
close_count
=
1
;
...
@@ -238,8 +238,8 @@ TEST(FileTest, CloseNoRetryInDtor) {
...
@@ -238,8 +238,8 @@ TEST(FileTest, CloseNoRetryInDtor) {
}
}
TEST
(
FileTest
,
CloseNoRetry
)
{
TEST
(
FileTest
,
CloseNoRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
close_count
=
1
;
close_count
=
1
;
EXPECT_SYSTEM_ERROR
(
read_end
.
close
(),
EINTR
,
"cannot close file"
);
EXPECT_SYSTEM_ERROR
(
read_end
.
close
(),
EINTR
,
"cannot close file"
);
EXPECT_EQ
(
2
,
close_count
);
EXPECT_EQ
(
2
,
close_count
);
...
@@ -249,7 +249,7 @@ TEST(FileTest, CloseNoRetry) {
...
@@ -249,7 +249,7 @@ TEST(FileTest, CloseNoRetry) {
TEST
(
FileTest
,
Size
)
{
TEST
(
FileTest
,
Size
)
{
std
::
string
content
=
"top secret, destroy before reading"
;
std
::
string
content
=
"top secret, destroy before reading"
;
write_file
(
"test"
,
content
);
write_file
(
"test"
,
content
);
File
f
(
"test"
,
F
ile
::
RDONLY
);
file
f
(
"test"
,
f
ile
::
RDONLY
);
EXPECT_GE
(
f
.
size
(),
0
);
EXPECT_GE
(
f
.
size
(),
0
);
EXPECT_EQ
(
content
.
size
(),
static_cast
<
unsigned
long
long
>
(
f
.
size
()));
EXPECT_EQ
(
content
.
size
(),
static_cast
<
unsigned
long
long
>
(
f
.
size
()));
#ifdef _WIN32
#ifdef _WIN32
...
@@ -267,7 +267,7 @@ TEST(FileTest, Size) {
...
@@ -267,7 +267,7 @@ TEST(FileTest, Size) {
TEST
(
FileTest
,
MaxSize
)
{
TEST
(
FileTest
,
MaxSize
)
{
write_file
(
"test"
,
""
);
write_file
(
"test"
,
""
);
File
f
(
"test"
,
F
ile
::
RDONLY
);
file
f
(
"test"
,
f
ile
::
RDONLY
);
fstat_sim
=
MAX_SIZE
;
fstat_sim
=
MAX_SIZE
;
EXPECT_GE
(
f
.
size
(),
0
);
EXPECT_GE
(
f
.
size
(),
0
);
EXPECT_EQ
(
max_file_size
(),
f
.
size
());
EXPECT_EQ
(
max_file_size
(),
f
.
size
());
...
@@ -275,8 +275,8 @@ TEST(FileTest, MaxSize) {
...
@@ -275,8 +275,8 @@ TEST(FileTest, MaxSize) {
}
}
TEST
(
FileTest
,
ReadRetry
)
{
TEST
(
FileTest
,
ReadRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
enum
{
SIZE
=
4
};
enum
{
SIZE
=
4
};
write_end
.
write
(
"test"
,
SIZE
);
write_end
.
write
(
"test"
,
SIZE
);
write_end
.
close
();
write_end
.
close
();
...
@@ -288,8 +288,8 @@ TEST(FileTest, ReadRetry) {
...
@@ -288,8 +288,8 @@ TEST(FileTest, ReadRetry) {
}
}
TEST
(
FileTest
,
WriteRetry
)
{
TEST
(
FileTest
,
WriteRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
enum
{
SIZE
=
4
};
enum
{
SIZE
=
4
};
std
::
size_t
count
=
0
;
std
::
size_t
count
=
0
;
EXPECT_RETRY
(
count
=
write_end
.
write
(
"test"
,
SIZE
),
EXPECT_RETRY
(
count
=
write_end
.
write
(
"test"
,
SIZE
),
...
@@ -306,8 +306,8 @@ TEST(FileTest, WriteRetry) {
...
@@ -306,8 +306,8 @@ TEST(FileTest, WriteRetry) {
#ifdef _WIN32
#ifdef _WIN32
TEST
(
FileTest
,
ConvertReadCount
)
{
TEST
(
FileTest
,
ConvertReadCount
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
char
c
;
char
c
;
std
::
size_t
size
=
UINT_MAX
;
std
::
size_t
size
=
UINT_MAX
;
if
(
sizeof
(
unsigned
)
!=
sizeof
(
std
::
size_t
))
if
(
sizeof
(
unsigned
)
!=
sizeof
(
std
::
size_t
))
...
@@ -320,8 +320,8 @@ TEST(FileTest, ConvertReadCount) {
...
@@ -320,8 +320,8 @@ TEST(FileTest, ConvertReadCount) {
}
}
TEST
(
FileTest
,
ConvertWriteCount
)
{
TEST
(
FileTest
,
ConvertWriteCount
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
char
c
;
char
c
;
std
::
size_t
size
=
UINT_MAX
;
std
::
size_t
size
=
UINT_MAX
;
if
(
sizeof
(
unsigned
)
!=
sizeof
(
std
::
size_t
))
if
(
sizeof
(
unsigned
)
!=
sizeof
(
std
::
size_t
))
...
@@ -337,14 +337,14 @@ TEST(FileTest, ConvertWriteCount) {
...
@@ -337,14 +337,14 @@ TEST(FileTest, ConvertWriteCount) {
TEST
(
FileTest
,
DupNoRetry
)
{
TEST
(
FileTest
,
DupNoRetry
)
{
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
dup_count
=
1
;
dup_count
=
1
;
EXPECT_SYSTEM_ERROR
(
F
ile
::
dup
(
stdout_fd
),
EINTR
,
EXPECT_SYSTEM_ERROR
(
f
ile
::
dup
(
stdout_fd
),
EINTR
,
fmt
::
format
(
"cannot duplicate file descriptor {}"
,
stdout_fd
));
fmt
::
format
(
"cannot duplicate file descriptor {}"
,
stdout_fd
));
dup_count
=
0
;
dup_count
=
0
;
}
}
TEST
(
FileTest
,
Dup2Retry
)
{
TEST
(
FileTest
,
Dup2Retry
)
{
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
File
f1
=
File
::
dup
(
stdout_fd
),
f2
=
F
ile
::
dup
(
stdout_fd
);
file
f1
=
file
::
dup
(
stdout_fd
),
f2
=
f
ile
::
dup
(
stdout_fd
);
EXPECT_RETRY
(
f1
.
dup2
(
f2
.
descriptor
()),
dup2
,
EXPECT_RETRY
(
f1
.
dup2
(
f2
.
descriptor
()),
dup2
,
fmt
::
format
(
"cannot duplicate file descriptor {} to {}"
,
fmt
::
format
(
"cannot duplicate file descriptor {} to {}"
,
f1
.
descriptor
(),
f2
.
descriptor
()));
f1
.
descriptor
(),
f2
.
descriptor
()));
...
@@ -352,8 +352,8 @@ TEST(FileTest, Dup2Retry) {
...
@@ -352,8 +352,8 @@ TEST(FileTest, Dup2Retry) {
TEST
(
FileTest
,
Dup2NoExceptRetry
)
{
TEST
(
FileTest
,
Dup2NoExceptRetry
)
{
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
int
stdout_fd
=
FMT_POSIX
(
fileno
(
stdout
));
File
f1
=
File
::
dup
(
stdout_fd
),
f2
=
F
ile
::
dup
(
stdout_fd
);
file
f1
=
file
::
dup
(
stdout_fd
),
f2
=
f
ile
::
dup
(
stdout_fd
);
ErrorC
ode
ec
;
error_c
ode
ec
;
dup2_count
=
1
;
dup2_count
=
1
;
f1
.
dup2
(
f2
.
descriptor
(),
ec
);
f1
.
dup2
(
f2
.
descriptor
(),
ec
);
#ifndef _WIN32
#ifndef _WIN32
...
@@ -365,16 +365,16 @@ TEST(FileTest, Dup2NoExceptRetry) {
...
@@ -365,16 +365,16 @@ TEST(FileTest, Dup2NoExceptRetry) {
}
}
TEST
(
FileTest
,
PipeNoRetry
)
{
TEST
(
FileTest
,
PipeNoRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
pipe_count
=
1
;
pipe_count
=
1
;
EXPECT_SYSTEM_ERROR
(
EXPECT_SYSTEM_ERROR
(
F
ile
::
pipe
(
read_end
,
write_end
),
EINTR
,
"cannot create pipe"
);
f
ile
::
pipe
(
read_end
,
write_end
),
EINTR
,
"cannot create pipe"
);
pipe_count
=
0
;
pipe_count
=
0
;
}
}
TEST
(
FileTest
,
FdopenNoRetry
)
{
TEST
(
FileTest
,
FdopenNoRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
fdopen_count
=
1
;
fdopen_count
=
1
;
EXPECT_SYSTEM_ERROR
(
read_end
.
fdopen
(
"r"
),
EXPECT_SYSTEM_ERROR
(
read_end
.
fdopen
(
"r"
),
EINTR
,
"cannot associate stream with file descriptor"
);
EINTR
,
"cannot associate stream with file descriptor"
);
...
@@ -394,8 +394,8 @@ TEST(BufferedFileTest, OpenRetry) {
...
@@ -394,8 +394,8 @@ TEST(BufferedFileTest, OpenRetry) {
}
}
TEST
(
BufferedFileTest
,
CloseNoRetryInDtor
)
{
TEST
(
BufferedFileTest
,
CloseNoRetryInDtor
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
scoped_ptr
<
buffered_file
>
f
(
new
buffered_file
(
read_end
.
fdopen
(
"r"
)));
scoped_ptr
<
buffered_file
>
f
(
new
buffered_file
(
read_end
.
fdopen
(
"r"
)));
int
saved_fclose_count
=
0
;
int
saved_fclose_count
=
0
;
EXPECT_WRITE
(
stderr
,
{
EXPECT_WRITE
(
stderr
,
{
...
@@ -408,8 +408,8 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) {
...
@@ -408,8 +408,8 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) {
}
}
TEST
(
BufferedFileTest
,
CloseNoRetry
)
{
TEST
(
BufferedFileTest
,
CloseNoRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
buffered_file
f
=
read_end
.
fdopen
(
"r"
);
buffered_file
f
=
read_end
.
fdopen
(
"r"
);
fclose_count
=
1
;
fclose_count
=
1
;
EXPECT_SYSTEM_ERROR
(
f
.
close
(),
EINTR
,
"cannot close file"
);
EXPECT_SYSTEM_ERROR
(
f
.
close
(),
EINTR
,
"cannot close file"
);
...
@@ -418,8 +418,8 @@ TEST(BufferedFileTest, CloseNoRetry) {
...
@@ -418,8 +418,8 @@ TEST(BufferedFileTest, CloseNoRetry) {
}
}
TEST
(
BufferedFileTest
,
FilenoNoRetry
)
{
TEST
(
BufferedFileTest
,
FilenoNoRetry
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
buffered_file
f
=
read_end
.
fdopen
(
"r"
);
buffered_file
f
=
read_end
.
fdopen
(
"r"
);
fileno_count
=
1
;
fileno_count
=
1
;
EXPECT_SYSTEM_ERROR
((
f
.
fileno
)(),
EINTR
,
"cannot get file descriptor"
);
EXPECT_SYSTEM_ERROR
((
f
.
fileno
)(),
EINTR
,
"cannot get file descriptor"
);
...
...
test/posix-test.cc
View file @
b76bb796
...
@@ -17,8 +17,8 @@
...
@@ -17,8 +17,8 @@
#endif
#endif
using
fmt
::
buffered_file
;
using
fmt
::
buffered_file
;
using
fmt
::
ErrorC
ode
;
using
fmt
::
error_c
ode
;
using
fmt
::
F
ile
;
using
fmt
::
f
ile
;
using
testing
::
internal
::
scoped_ptr
;
using
testing
::
internal
::
scoped_ptr
;
...
@@ -36,16 +36,16 @@ bool isclosed(int fd) {
...
@@ -36,16 +36,16 @@ bool isclosed(int fd) {
}
}
// Opens a file for reading.
// Opens a file for reading.
F
ile
open_file
()
{
f
ile
open_file
()
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
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
;
}
}
// Attempts to write a string to a file.
// Attempts to write a string to a file.
void
write
(
F
ile
&
f
,
fmt
::
string_view
s
)
{
void
write
(
f
ile
&
f
,
fmt
::
string_view
s
)
{
std
::
size_t
num_chars_left
=
s
.
size
();
std
::
size_t
num_chars_left
=
s
.
size
();
const
char
*
ptr
=
s
.
data
();
const
char
*
ptr
=
s
.
data
();
do
{
do
{
...
@@ -160,12 +160,12 @@ TEST(BufferedFileTest, Fileno) {
...
@@ -160,12 +160,12 @@ TEST(BufferedFileTest, Fileno) {
#endif
#endif
f
=
open_buffered_file
();
f
=
open_buffered_file
();
EXPECT_TRUE
(
f
.
fileno
()
!=
-
1
);
EXPECT_TRUE
(
f
.
fileno
()
!=
-
1
);
File
copy
=
F
ile
::
dup
(
f
.
fileno
());
file
copy
=
f
ile
::
dup
(
f
.
fileno
());
EXPECT_READ
(
copy
,
FILE_CONTENT
);
EXPECT_READ
(
copy
,
FILE_CONTENT
);
}
}
TEST
(
FileTest
,
DefaultCtor
)
{
TEST
(
FileTest
,
DefaultCtor
)
{
F
ile
f
;
f
ile
f
;
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
}
}
...
@@ -173,64 +173,64 @@ TEST(FileTest, OpenBufferedFileInCtor) {
...
@@ -173,64 +173,64 @@ TEST(FileTest, OpenBufferedFileInCtor) {
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"
,
F
ile
::
RDONLY
);
file
f
(
"test-file"
,
f
ile
::
RDONLY
);
ASSERT_TRUE
(
isopen
(
f
.
descriptor
()));
ASSERT_TRUE
(
isopen
(
f
.
descriptor
()));
}
}
TEST
(
FileTest
,
OpenBufferedFileError
)
{
TEST
(
FileTest
,
OpenBufferedFileError
)
{
EXPECT_SYSTEM_ERROR
(
File
(
"nonexistent"
,
F
ile
::
RDONLY
),
EXPECT_SYSTEM_ERROR
(
file
(
"nonexistent"
,
f
ile
::
RDONLY
),
ENOENT
,
"cannot open file nonexistent"
);
ENOENT
,
"cannot open file nonexistent"
);
}
}
TEST
(
FileTest
,
MoveCtor
)
{
TEST
(
FileTest
,
MoveCtor
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
int
fd
=
f
.
descriptor
();
int
fd
=
f
.
descriptor
();
EXPECT_NE
(
-
1
,
fd
);
EXPECT_NE
(
-
1
,
fd
);
F
ile
f2
(
std
::
move
(
f
));
f
ile
f2
(
std
::
move
(
f
));
EXPECT_EQ
(
fd
,
f2
.
descriptor
());
EXPECT_EQ
(
fd
,
f2
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
}
}
TEST
(
FileTest
,
MoveAssignment
)
{
TEST
(
FileTest
,
MoveAssignment
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
int
fd
=
f
.
descriptor
();
int
fd
=
f
.
descriptor
();
EXPECT_NE
(
-
1
,
fd
);
EXPECT_NE
(
-
1
,
fd
);
F
ile
f2
;
f
ile
f2
;
f2
=
std
::
move
(
f
);
f2
=
std
::
move
(
f
);
EXPECT_EQ
(
fd
,
f2
.
descriptor
());
EXPECT_EQ
(
fd
,
f2
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
}
}
TEST
(
FileTest
,
MoveAssignmentClosesFile
)
{
TEST
(
FileTest
,
MoveAssignmentClosesFile
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
F
ile
f2
=
open_file
();
f
ile
f2
=
open_file
();
int
old_fd
=
f2
.
descriptor
();
int
old_fd
=
f2
.
descriptor
();
f2
=
std
::
move
(
f
);
f2
=
std
::
move
(
f
);
EXPECT_TRUE
(
isclosed
(
old_fd
));
EXPECT_TRUE
(
isclosed
(
old_fd
));
}
}
F
ile
OpenBufferedFile
(
int
&
fd
)
{
f
ile
OpenBufferedFile
(
int
&
fd
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
fd
=
f
.
descriptor
();
fd
=
f
.
descriptor
();
return
f
;
return
f
;
}
}
TEST
(
FileTest
,
MoveFromTemporaryInCtor
)
{
TEST
(
FileTest
,
MoveFromTemporaryInCtor
)
{
int
fd
=
0xdead
;
int
fd
=
0xdead
;
F
ile
f
(
OpenBufferedFile
(
fd
));
f
ile
f
(
OpenBufferedFile
(
fd
));
EXPECT_EQ
(
fd
,
f
.
descriptor
());
EXPECT_EQ
(
fd
,
f
.
descriptor
());
}
}
TEST
(
FileTest
,
MoveFromTemporaryInAssignment
)
{
TEST
(
FileTest
,
MoveFromTemporaryInAssignment
)
{
int
fd
=
0xdead
;
int
fd
=
0xdead
;
F
ile
f
;
f
ile
f
;
f
=
OpenBufferedFile
(
fd
);
f
=
OpenBufferedFile
(
fd
);
EXPECT_EQ
(
fd
,
f
.
descriptor
());
EXPECT_EQ
(
fd
,
f
.
descriptor
());
}
}
TEST
(
FileTest
,
MoveFromTemporaryInAssignmentClosesFile
)
{
TEST
(
FileTest
,
MoveFromTemporaryInAssignmentClosesFile
)
{
int
fd
=
0xdead
;
int
fd
=
0xdead
;
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
int
old_fd
=
f
.
descriptor
();
int
old_fd
=
f
.
descriptor
();
f
=
OpenBufferedFile
(
fd
);
f
=
OpenBufferedFile
(
fd
);
EXPECT_TRUE
(
isclosed
(
old_fd
));
EXPECT_TRUE
(
isclosed
(
old_fd
));
...
@@ -239,14 +239,14 @@ TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
...
@@ -239,14 +239,14 @@ TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
TEST
(
FileTest
,
CloseFileInDtor
)
{
TEST
(
FileTest
,
CloseFileInDtor
)
{
int
fd
=
0
;
int
fd
=
0
;
{
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
fd
=
f
.
descriptor
();
fd
=
f
.
descriptor
();
}
}
EXPECT_TRUE
(
isclosed
(
fd
));
EXPECT_TRUE
(
isclosed
(
fd
));
}
}
TEST
(
FileTest
,
CloseErrorInDtor
)
{
TEST
(
FileTest
,
CloseErrorInDtor
)
{
scoped_ptr
<
File
>
f
(
new
F
ile
(
open_file
()));
scoped_ptr
<
file
>
f
(
new
f
ile
(
open_file
()));
EXPECT_WRITE
(
stderr
,
{
EXPECT_WRITE
(
stderr
,
{
// The close function must be called inside EXPECT_WRITE, otherwise
// The close function must be called inside EXPECT_WRITE, otherwise
// the system may recycle closed file descriptor when redirecting the
// the system may recycle closed file descriptor when redirecting the
...
@@ -258,7 +258,7 @@ TEST(FileTest, CloseErrorInDtor) {
...
@@ -258,7 +258,7 @@ TEST(FileTest, CloseErrorInDtor) {
}
}
TEST
(
FileTest
,
Close
)
{
TEST
(
FileTest
,
Close
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
int
fd
=
f
.
descriptor
();
int
fd
=
f
.
descriptor
();
f
.
close
();
f
.
close
();
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
...
@@ -266,19 +266,19 @@ TEST(FileTest, Close) {
...
@@ -266,19 +266,19 @@ TEST(FileTest, Close) {
}
}
TEST
(
FileTest
,
CloseError
)
{
TEST
(
FileTest
,
CloseError
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
FMT_POSIX
(
close
(
f
.
descriptor
()));
FMT_POSIX
(
close
(
f
.
descriptor
()));
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
.
close
(),
EBADF
,
"cannot close file"
);
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
.
close
(),
EBADF
,
"cannot close file"
);
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
EXPECT_EQ
(
-
1
,
f
.
descriptor
());
}
}
TEST
(
FileTest
,
Read
)
{
TEST
(
FileTest
,
Read
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
EXPECT_READ
(
f
,
FILE_CONTENT
);
EXPECT_READ
(
f
,
FILE_CONTENT
);
}
}
TEST
(
FileTest
,
ReadError
)
{
TEST
(
FileTest
,
ReadError
)
{
File
f
(
"test-file"
,
F
ile
::
WRONLY
);
file
f
(
"test-file"
,
f
ile
::
WRONLY
);
char
buf
;
char
buf
;
// We intentionally read from a file opened in the write-only mode to
// We intentionally read from a file opened in the write-only mode to
// cause error.
// cause error.
...
@@ -286,23 +286,23 @@ TEST(FileTest, ReadError) {
...
@@ -286,23 +286,23 @@ TEST(FileTest, ReadError) {
}
}
TEST
(
FileTest
,
Write
)
{
TEST
(
FileTest
,
Write
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
write
(
write_end
,
"test"
);
write
(
write_end
,
"test"
);
write_end
.
close
();
write_end
.
close
();
EXPECT_READ
(
read_end
,
"test"
);
EXPECT_READ
(
read_end
,
"test"
);
}
}
TEST
(
FileTest
,
WriteError
)
{
TEST
(
FileTest
,
WriteError
)
{
File
f
(
"test-file"
,
F
ile
::
RDONLY
);
file
f
(
"test-file"
,
f
ile
::
RDONLY
);
// We intentionally write to a file opened in the read-only mode to
// We intentionally write to a file opened in the read-only mode to
// cause error.
// cause error.
EXPECT_SYSTEM_ERROR
(
f
.
write
(
" "
,
1
),
EBADF
,
"cannot write to file"
);
EXPECT_SYSTEM_ERROR
(
f
.
write
(
" "
,
1
),
EBADF
,
"cannot write to file"
);
}
}
TEST
(
FileTest
,
Dup
)
{
TEST
(
FileTest
,
Dup
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
File
copy
=
F
ile
::
dup
(
f
.
descriptor
());
file
copy
=
f
ile
::
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
)));
}
}
...
@@ -310,29 +310,29 @@ TEST(FileTest, Dup) {
...
@@ -310,29 +310,29 @@ TEST(FileTest, Dup) {
#ifndef __COVERITY__
#ifndef __COVERITY__
TEST
(
FileTest
,
DupError
)
{
TEST
(
FileTest
,
DupError
)
{
int
value
=
-
1
;
int
value
=
-
1
;
EXPECT_SYSTEM_ERROR_NOASSERT
(
F
ile
::
dup
(
value
),
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
ile
::
dup
(
value
),
EBADF
,
"cannot duplicate file descriptor -1"
);
EBADF
,
"cannot duplicate file descriptor -1"
);
}
}
#endif
#endif
TEST
(
FileTest
,
Dup2
)
{
TEST
(
FileTest
,
Dup2
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
F
ile
copy
=
open_file
();
f
ile
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
(
FileTest
,
Dup2Error
)
{
TEST
(
FileTest
,
Dup2Error
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
.
dup2
(
-
1
),
EBADF
,
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
.
dup2
(
-
1
),
EBADF
,
fmt
::
format
(
"cannot duplicate file descriptor {} to -1"
,
f
.
descriptor
()));
fmt
::
format
(
"cannot duplicate file descriptor {} to -1"
,
f
.
descriptor
()));
}
}
TEST
(
FileTest
,
Dup2NoExcept
)
{
TEST
(
FileTest
,
Dup2NoExcept
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
F
ile
copy
=
open_file
();
f
ile
copy
=
open_file
();
ErrorC
ode
ec
;
error_c
ode
ec
;
f
.
dup2
(
copy
.
descriptor
(),
ec
);
f
.
dup2
(
copy
.
descriptor
(),
ec
);
EXPECT_EQ
(
0
,
ec
.
get
());
EXPECT_EQ
(
0
,
ec
.
get
());
EXPECT_NE
(
f
.
descriptor
(),
copy
.
descriptor
());
EXPECT_NE
(
f
.
descriptor
(),
copy
.
descriptor
());
...
@@ -340,15 +340,15 @@ TEST(FileTest, Dup2NoExcept) {
...
@@ -340,15 +340,15 @@ TEST(FileTest, Dup2NoExcept) {
}
}
TEST
(
FileTest
,
Dup2NoExceptError
)
{
TEST
(
FileTest
,
Dup2NoExceptError
)
{
F
ile
f
=
open_file
();
f
ile
f
=
open_file
();
ErrorC
ode
ec
;
error_c
ode
ec
;
SUPPRESS_ASSERT
(
f
.
dup2
(
-
1
,
ec
));
SUPPRESS_ASSERT
(
f
.
dup2
(
-
1
,
ec
));
EXPECT_EQ
(
EBADF
,
ec
.
get
());
EXPECT_EQ
(
EBADF
,
ec
.
get
());
}
}
TEST
(
FileTest
,
Pipe
)
{
TEST
(
FileTest
,
Pipe
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
EXPECT_NE
(
-
1
,
read_end
.
descriptor
());
EXPECT_NE
(
-
1
,
read_end
.
descriptor
());
EXPECT_NE
(
-
1
,
write_end
.
descriptor
());
EXPECT_NE
(
-
1
,
write_end
.
descriptor
());
write
(
write_end
,
"test"
);
write
(
write_end
,
"test"
);
...
@@ -356,14 +356,14 @@ TEST(FileTest, Pipe) {
...
@@ -356,14 +356,14 @@ TEST(FileTest, Pipe) {
}
}
TEST
(
FileTest
,
Fdopen
)
{
TEST
(
FileTest
,
Fdopen
)
{
F
ile
read_end
,
write_end
;
f
ile
read_end
,
write_end
;
F
ile
::
pipe
(
read_end
,
write_end
);
f
ile
::
pipe
(
read_end
,
write_end
);
int
read_fd
=
read_end
.
descriptor
();
int
read_fd
=
read_end
.
descriptor
();
EXPECT_EQ
(
read_fd
,
FMT_POSIX
(
fileno
(
read_end
.
fdopen
(
"r"
).
get
())));
EXPECT_EQ
(
read_fd
,
FMT_POSIX
(
fileno
(
read_end
.
fdopen
(
"r"
).
get
())));
}
}
TEST
(
FileTest
,
FdopenError
)
{
TEST
(
FileTest
,
FdopenError
)
{
F
ile
f
;
f
ile
f
;
EXPECT_SYSTEM_ERROR_NOASSERT
(
EXPECT_SYSTEM_ERROR_NOASSERT
(
f
.
fdopen
(
"r"
),
EBADF
,
"cannot associate stream with file descriptor"
);
f
.
fdopen
(
"r"
),
EBADF
,
"cannot associate stream with file descriptor"
);
}
}
...
...
test/printf-test.cc
View file @
b76bb796
...
@@ -478,8 +478,8 @@ TEST(PrintfTest, Examples) {
...
@@ -478,8 +478,8 @@ TEST(PrintfTest, Examples) {
}
}
TEST
(
PrintfTest
,
PrintfError
)
{
TEST
(
PrintfTest
,
PrintfError
)
{
fmt
::
F
ile
read_end
,
write_end
;
fmt
::
f
ile
read_end
,
write_end
;
fmt
::
F
ile
::
pipe
(
read_end
,
write_end
);
fmt
::
f
ile
::
pipe
(
read_end
,
write_end
);
int
result
=
fmt
::
fprintf
(
read_end
.
fdopen
(
"r"
).
get
(),
"test"
);
int
result
=
fmt
::
fprintf
(
read_end
.
fdopen
(
"r"
).
get
(),
"test"
);
EXPECT_LT
(
result
,
0
);
EXPECT_LT
(
result
,
0
);
}
}
...
...
test/util.cc
View file @
b76bb796
...
@@ -33,8 +33,8 @@ std::string get_system_error(int error_code) {
...
@@ -33,8 +33,8 @@ std::string get_system_error(int error_code) {
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
)
{
fmt
::
F
ile
read_end
,
write_end
;
fmt
::
f
ile
read_end
,
write_end
;
fmt
::
F
ile
::
pipe
(
read_end
,
write_end
);
fmt
::
f
ile
::
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"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment