Skip to content

Commit

Permalink
Update os-test.cc (#3883)
Browse files Browse the repository at this point in the history
uniq test file names for the os-test suite (#3878 issue).
  • Loading branch information
vgorrX authored Mar 6, 2024
1 parent 09935d8 commit dfe5b12
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions test/os-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ using fmt::buffered_file;
using testing::HasSubstr;
using wstring_view = fmt::basic_string_view<wchar_t>;

static std::string uniq_file_name(unsigned line_number) {
return "test-file" + std::to_string(line_number);
}

#ifdef _WIN32

# include <windows.h>
Expand Down Expand Up @@ -232,68 +236,75 @@ TEST(buffered_file_test, descriptor) {
}

TEST(ostream_test, move) {
fmt::ostream out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
fmt::ostream out = fmt::output_file(test_file);
fmt::ostream moved(std::move(out));
moved.print("hello");
}

TEST(ostream_test, move_while_holding_data) {
auto test_file = uniq_file_name(__LINE__);
{
fmt::ostream out = fmt::output_file("test-file");
fmt::ostream out = fmt::output_file(test_file);
out.print("Hello, ");
fmt::ostream moved(std::move(out));
moved.print("world!\n");
}
{
file in("test-file", file::RDONLY);
file in(test_file, file::RDONLY);
EXPECT_READ(in, "Hello, world!\n");
}
}

TEST(ostream_test, print) {
fmt::ostream out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
fmt::ostream out = fmt::output_file(test_file);
out.print("The answer is {}.\n", 42);
out.close();
file in("test-file", file::RDONLY);
file in(test_file, file::RDONLY);
EXPECT_READ(in, "The answer is 42.\n");
}

TEST(ostream_test, buffer_boundary) {
auto str = std::string(4096, 'x');
fmt::ostream out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
fmt::ostream out = fmt::output_file(test_file);
out.print("{}", str);
out.print("{}", str);
out.close();
file in("test-file", file::RDONLY);
file in(test_file, file::RDONLY);
EXPECT_READ(in, str + str);
}

TEST(ostream_test, buffer_size) {
fmt::ostream out = fmt::output_file("test-file", fmt::buffer_size = 1);
auto test_file = uniq_file_name(__LINE__);
fmt::ostream out = fmt::output_file(test_file, fmt::buffer_size = 1);
out.print("{}", "foo");
out.close();
file in("test-file", file::RDONLY);
file in(test_file, file::RDONLY);
EXPECT_READ(in, "foo");
}

TEST(ostream_test, truncate) {
auto test_file = uniq_file_name(__LINE__);
{
fmt::ostream out = fmt::output_file("test-file");
fmt::ostream out = fmt::output_file(test_file);
out.print("0123456789");
}
{
fmt::ostream out = fmt::output_file("test-file");
fmt::ostream out = fmt::output_file(test_file);
out.print("foo");
}
file in("test-file", file::RDONLY);
file in(test_file, file::RDONLY);
EXPECT_EQ("foo", read(in, 4));
}

TEST(ostream_test, flush) {
auto out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
auto out = fmt::output_file(test_file);
out.print("x");
out.flush();
auto in = fmt::file("test-file", file::RDONLY);
auto in = fmt::file(test_file, file::RDONLY);
EXPECT_READ(in, "x");
}

Expand All @@ -303,10 +314,11 @@ TEST(file_test, default_ctor) {
}

TEST(file_test, open_buffered_file_in_ctor) {
FILE* fp = safe_fopen("test-file", "w");
auto test_file = uniq_file_name(__LINE__);
FILE* fp = safe_fopen(test_file.c_str(), "w");
std::fputs(file_content, fp);
std::fclose(fp);
file f("test-file", file::RDONLY);
file f(test_file.c_str(), file::RDONLY);
// Check if the file is open by reading one character from it.
char buffer;
bool isopen = FMT_POSIX(read(f.descriptor(), &buffer, 1)) == 1;
Expand Down Expand Up @@ -417,7 +429,8 @@ TEST(file_test, read) {
}

TEST(file_test, read_error) {
file f("test-file", file::WRONLY);
auto test_file = uniq_file_name(__LINE__);
file f(test_file, file::WRONLY | file::CREATE);
char buf;
// We intentionally read from a file opened in the write-only mode to
// cause error.
Expand All @@ -426,13 +439,15 @@ TEST(file_test, read_error) {

TEST(file_test, write) {
auto pipe = fmt::pipe();
write(pipe.write_end, "test");
auto test_file = uniq_file_name(__LINE__);
write(pipe.write_end, test_file);
pipe.write_end.close();
EXPECT_READ(pipe.read_end, "test");
EXPECT_READ(pipe.read_end, test_file);
}

TEST(file_test, write_error) {
file f("test-file", file::RDONLY);
auto test_file = uniq_file_name(__LINE__);
file f(test_file, file::RDONLY | file::CREATE);
// 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");
Expand Down

0 comments on commit dfe5b12

Please sign in to comment.