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
823ce5fc
Commit
823ce5fc
authored
May 03, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More tests.
parent
c6f40e33
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
17 deletions
+23
-17
test/gtest-extra-test.cc
test/gtest-extra-test.cc
+19
-13
test/gtest-extra.cc
test/gtest-extra.cc
+4
-4
No files found.
test/gtest-extra-test.cc
View file @
823ce5fc
...
...
@@ -204,7 +204,7 @@ TEST(FileTest, MoveAssignmentClosesFile) {
File
f2
(
"CMakeLists.txt"
,
File
::
RDONLY
);
int
old_fd
=
f2
.
descriptor
();
f2
=
std
::
move
(
f
);
EXPECT_
CLOSED
(
old_fd
);
EXPECT_
TRUE
(
IsClosedInternal
(
old_fd
)
);
}
File
OpenFile
(
int
&
fd
)
{
...
...
@@ -272,7 +272,7 @@ TEST(FileTest, CloseError) {
EXPECT_EQ
(
message
,
error
.
what
());
}
// Attempts to read count characters from
the
file.
// Attempts to read count characters from
a
file.
std
::
string
Read
(
File
&
f
,
std
::
size_t
count
)
{
std
::
string
buffer
(
count
,
'\0'
);
std
::
streamsize
offset
=
0
,
n
=
0
;
...
...
@@ -284,6 +284,17 @@ std::string Read(File &f, std::size_t count) {
return
buffer
;
}
// Attempts to write a string to a file.
void
Write
(
File
&
f
,
fmt
::
StringRef
s
)
{
std
::
size_t
num_chars_left
=
s
.
size
();
const
char
*
ptr
=
s
.
c_str
();
do
{
std
::
streamsize
count
=
f
.
write
(
ptr
,
num_chars_left
);
ptr
+=
count
;
num_chars_left
-=
count
;
}
while
(
num_chars_left
!=
0
);
}
#define EXPECT_READ(file, expected_content) \
EXPECT_EQ(expected_content, Read(file, std::strlen(expected_content)))
...
...
@@ -299,17 +310,11 @@ TEST(FileTest, ReadError) {
}
TEST
(
FileTest
,
Write
)
{
const
char
MESSAGE
[]
=
"test"
;
File
read_end
,
write_end
;
File
::
pipe
(
read_end
,
write_end
);
enum
{
SIZE
=
sizeof
(
MESSAGE
)
-
1
};
std
::
streamsize
offset
=
0
,
count
=
0
;
do
{
count
=
write_end
.
write
(
MESSAGE
+
offset
,
SIZE
-
offset
);
offset
+=
count
;
}
while
(
offset
<
SIZE
&&
count
!=
0
);
write_end
=
File
();
// Close file.
EXPECT_READ
(
read_end
,
MESSAGE
);
Write
(
write_end
,
"test"
);
write_end
.
close
();
EXPECT_READ
(
read_end
,
"test"
);
}
TEST
(
FileTest
,
WriteError
)
{
...
...
@@ -341,7 +346,7 @@ TEST(FileTest, Dup2) {
TEST
(
FileTest
,
Dup2Error
)
{
File
f
(
".travis.yml"
,
File
::
RDONLY
);
EXPECT_SYSTEM_ERROR
(
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
)
{
...
...
@@ -366,7 +371,8 @@ TEST(FileTest, Pipe) {
File
::
pipe
(
read_end
,
write_end
);
EXPECT_NE
(
-
1
,
read_end
.
descriptor
());
EXPECT_NE
(
-
1
,
write_end
.
descriptor
());
// TODO: try writing to write_end and reading from read_end
Write
(
write_end
,
"test"
);
EXPECT_READ
(
read_end
,
"test"
);
}
// TODO: test pipe
...
...
test/gtest-extra.cc
View file @
823ce5fc
...
...
@@ -82,7 +82,7 @@ void File::close() {
std
::
streamsize
File
::
read
(
void
*
buffer
,
std
::
size_t
count
)
{
std
::
streamsize
result
=
0
;
FMT_RETRY
(
result
,
::
read
(
fd_
,
buffer
,
count
));
FMT_RETRY
(
result
,
::
FMT_POSIX
(
read
(
fd_
,
buffer
,
count
)
));
if
(
result
==
-
1
)
fmt
::
ThrowSystemError
(
errno
,
"cannot read from file"
);
return
result
;
...
...
@@ -90,7 +90,7 @@ std::streamsize File::read(void *buffer, std::size_t count) {
std
::
streamsize
File
::
write
(
const
void
*
buffer
,
std
::
size_t
count
)
{
std
::
streamsize
result
=
0
;
FMT_RETRY
(
result
,
::
write
(
fd_
,
buffer
,
count
));
FMT_RETRY
(
result
,
::
FMT_POSIX
(
write
(
fd_
,
buffer
,
count
)
));
if
(
result
==
-
1
)
fmt
::
ThrowSystemError
(
errno
,
"cannot write to file"
);
return
result
;
...
...
@@ -145,7 +145,7 @@ void File::pipe(File &read_end, File &write_end) {
OutputRedirector
::
OutputRedirector
(
FILE
*
file
)
:
file_
(
file
)
{
if
(
std
::
fflush
(
file
)
!=
0
)
fmt
::
ThrowSystemError
(
errno
,
"cannot flush stream"
);
int
fd
=
fileno
(
file
);
int
fd
=
FMT_POSIX
(
fileno
(
file
)
);
saved_
=
File
::
dup
(
fd
);
File
write_end
;
File
::
pipe
(
read_end_
,
write_end
);
...
...
@@ -156,7 +156,7 @@ OutputRedirector::~OutputRedirector() {
if
(
std
::
fflush
(
file_
)
!=
0
)
fmt
::
ReportSystemError
(
errno
,
"cannot flush stream"
);
ErrorCode
ec
;
saved_
.
dup2
(
fileno
(
file_
),
ec
);
saved_
.
dup2
(
FMT_POSIX
(
fileno
(
file_
)
),
ec
);
if
(
ec
.
get
())
fmt
::
ReportSystemError
(
errno
,
"cannot restore output"
);
}
...
...
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