forked from boostorg/beast
Remove file_mode::append_new (API Change):
* Tidying * Increase test coverage * Fix file_mode::append_existing API Changes: * file_mode::append_new is removed, as it makes no sense Actions Required: * Replace file_mode::append_new with file_mode::append or file_mode::append_existing instead of file_mode::append_new
This commit is contained in:
1 parent
321af29d25
commit
61a35bd936
11 files changed
+458
-163
No files matched your search
@@ -7,8 +7,8 @@
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_TEST_BUFFER_TEST_HPP
|
||||
#define BOOST_BEAST_TEST_BUFFER_TEST_HPP
|
||||
#ifndef BOOST_BEAST_BUFFER_TEST_HPP
|
||||
#define BOOST_BEAST_BUFFER_TEST_HPP
|
||||
|
||||
#include <boost/beast/core/detail/config.hpp>
|
||||
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
void
|
||||
run()
|
||||
{
|
||||
doTestFile<file_posix>(*this);
|
||||
test_file<file_posix>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ public:
|
||||
void
|
||||
run()
|
||||
{
|
||||
doTestFile<file_stdio>(*this);
|
||||
test_file<file_stdio>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+330
-70
@@ -7,119 +7,379 @@
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_TEST_CORE_FILE_TEST_HPP
|
||||
#define BOOST_BEAST_TEST_CORE_FILE_TEST_HPP
|
||||
#ifndef BOOST_BEAST_FILE_TEST_HPP
|
||||
#define BOOST_BEAST_FILE_TEST_HPP
|
||||
|
||||
#include <boost/beast/core/type_traits.hpp>
|
||||
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class File>
|
||||
void
|
||||
doTestFile(beast::unit_test::suite& test)
|
||||
test_file(beast::unit_test::suite& test)
|
||||
{
|
||||
BOOST_STATIC_ASSERT(is_file<File>::value);
|
||||
BOOST_STATIC_ASSERT(
|
||||
is_file<File>::value);
|
||||
BOOST_STATIC_ASSERT(
|
||||
! std::is_copy_constructible<File>::value);
|
||||
BOOST_STATIC_ASSERT(
|
||||
! std::is_copy_assignable<File>::value);
|
||||
|
||||
error_code ec;
|
||||
auto const temp = boost::filesystem::unique_path();
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
class temp_path
|
||||
{
|
||||
fs::path path_;
|
||||
std::string str_;
|
||||
|
||||
public:
|
||||
temp_path()
|
||||
: path_(fs::unique_path())
|
||||
, str_(path_.string<std::string>())
|
||||
{
|
||||
}
|
||||
|
||||
operator fs::path const&()
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
operator char const*()
|
||||
{
|
||||
return str_.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
auto const create =
|
||||
[&test](fs::path const& path)
|
||||
{
|
||||
auto const s =
|
||||
path.string<std::string>();
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
FILE* f = ::fopen(s.c_str(), "w");
|
||||
if( test.BEAST_EXPECT(f != nullptr))
|
||||
::fclose(f);
|
||||
};
|
||||
|
||||
auto const remove =
|
||||
[](fs::path const& path)
|
||||
{
|
||||
error_code ec;
|
||||
fs::remove(path);
|
||||
};
|
||||
|
||||
temp_path path;
|
||||
|
||||
// bad file descriptor
|
||||
{
|
||||
File f;
|
||||
char buf[1];
|
||||
test.BEAST_EXPECT(! f.is_open());
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
{
|
||||
error_code ec;
|
||||
f.size(ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
f.pos(ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
f.seek(0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
f.read(buf, 0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
f.write(buf, 0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
// file_mode::read
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
create(path);
|
||||
f.open(path, file_mode::read, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::scan
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
create(path);
|
||||
f.open(path, file_mode::scan, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::write
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
}
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::write_new
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
f.open(path, file_mode::write_new, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
}
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
f.open(path, file_mode::write_new, ec);
|
||||
test.BEAST_EXPECT(ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::write_existing
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
f.open(path, file_mode::write_existing, ec);
|
||||
test.BEAST_EXPECT(ec);
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
}
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
create(path);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
f.open(path, file_mode::write_existing, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::append
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
f.open(path, file_mode::append, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
}
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
f.open(path, file_mode::append, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// file_mode::append_existing
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
f.open(path, file_mode::append_existing, ec);
|
||||
test.BEAST_EXPECT(ec);
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
}
|
||||
remove(path);
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
create(path);
|
||||
test.BEAST_EXPECT(fs::exists(path));
|
||||
f.open(path, file_mode::append_existing, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// special members
|
||||
{
|
||||
{
|
||||
File f1;
|
||||
test.BEAST_EXPECT(! f1.is_open());
|
||||
f1.open(temp.string<std::string>().c_str(), file_mode::write, ec);
|
||||
error_code ec;
|
||||
f1.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
File f2{std::move(f1)};
|
||||
test.BEAST_EXPECT(f1.is_open());
|
||||
|
||||
// move constructor
|
||||
File f2(std::move(f1));
|
||||
test.BEAST_EXPECT(! f1.is_open());
|
||||
test.BEAST_EXPECT(f2.is_open());
|
||||
|
||||
// move assignment
|
||||
File f3;
|
||||
f3 = std::move(f2);
|
||||
test.BEAST_EXPECT(! f2.is_open());
|
||||
test.BEAST_EXPECT(f3.is_open());
|
||||
f1.close(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
auto const temp2 = boost::filesystem::unique_path();
|
||||
f3.open(temp2.string<std::string>().c_str(), file_mode::read, ec);
|
||||
test.BEAST_EXPECT(ec);
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
boost::filesystem::remove(temp, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
File f;
|
||||
// re-open
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
test.BEAST_EXPECT(! f.is_open());
|
||||
// re-assign
|
||||
{
|
||||
temp_path path2;
|
||||
{
|
||||
error_code ec;
|
||||
|
||||
f.size(ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
ec.assign(0, ec.category());
|
||||
File f1;
|
||||
f1.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
|
||||
f.pos(ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
ec.assign(0, ec.category());
|
||||
File f2;
|
||||
f2.open(path2, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
|
||||
f.seek(0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
ec.assign(0, ec.category());
|
||||
f2 = std::move(f1);
|
||||
test.BEAST_EXPECT(! f1.is_open());
|
||||
test.BEAST_EXPECT(f2.is_open());
|
||||
}
|
||||
remove(path);
|
||||
remove(path2);
|
||||
}
|
||||
|
||||
char tmp[1];
|
||||
// self-move
|
||||
{
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
f = std::move(f);
|
||||
test.BEAST_EXPECT(f.is_open());
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
f.read(tmp, 0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
ec.assign(0, ec.category());
|
||||
// native_handle
|
||||
{
|
||||
{
|
||||
File f;
|
||||
auto none = f.native_handle();
|
||||
error_code ec;
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
auto fd = f.native_handle();
|
||||
test.BEAST_EXPECT(fd != none);
|
||||
f.native_handle(none);
|
||||
test.BEAST_EXPECT(! f.is_open());
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
f.write(tmp, 0, ec);
|
||||
test.BEAST_EXPECT(ec == errc::bad_file_descriptor);
|
||||
ec.assign(0, ec.category());
|
||||
// read and write
|
||||
{
|
||||
string_view const s = "Hello, world!";
|
||||
|
||||
f.open(temp.string<std::string>().c_str(), file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
// write
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
f.open(path, file_mode::write, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
|
||||
std::string const s = "Hello, world!";
|
||||
f.write(s.data(), s.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
f.write(s.data(), s.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
|
||||
auto size = f.size(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(size == s.size());
|
||||
auto size = f.size(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(size == s.size());
|
||||
|
||||
auto pos = f.pos(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(pos == size);
|
||||
auto pos = f.pos(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(pos == size);
|
||||
|
||||
f.close(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
f.close(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
}
|
||||
|
||||
f.open(temp.string<std::string>().c_str(), file_mode::read, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
// read
|
||||
{
|
||||
File f;
|
||||
error_code ec;
|
||||
f.open(path, file_mode::read, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
|
||||
std::string buf;
|
||||
buf.resize(s.size());
|
||||
f.read(&buf[0], buf.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(buf == s);
|
||||
std::string buf;
|
||||
buf.resize(s.size());
|
||||
f.read(&buf[0], buf.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(buf == s);
|
||||
|
||||
f.seek(1, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
buf.resize(3);
|
||||
f.read(&buf[0], buf.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(buf == "ell");
|
||||
f.seek(1, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
buf.resize(3);
|
||||
f.read(&buf[0], buf.size(), ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(buf == "ell");
|
||||
|
||||
pos = f.pos(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(pos == 4);
|
||||
auto pos = f.pos(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(pos == 4);
|
||||
}
|
||||
remove(path);
|
||||
}
|
||||
|
||||
f.close(ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
boost::filesystem::remove(temp, ec);
|
||||
test.BEAST_EXPECT(! ec);
|
||||
test.BEAST_EXPECT(! fs::exists(path));
|
||||
}
|
||||
|
||||
} // beast
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
BOOST_STATIC_ASSERT(! std::is_copy_constructible<file_win32>::value);
|
||||
|
||||
class file_win32_test
|
||||
: public beast::unit_test::suite
|
||||
{
|
||||
@@ -29,7 +27,7 @@ public:
|
||||
void
|
||||
run()
|
||||
{
|
||||
doTestFile<file_win32>(*this);
|
||||
test_file<file_win32>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user