📄 operations_test.cpp
字号:
// Boost operations_test.cpp -----------------------------------------------//
// Copyright Beman Dawes 2002.
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See library home page at http://www.boost.org/libs/filesystem
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
namespace fs = boost::filesystem;
#include <boost/config.hpp>
#include <boost/test/minimal.hpp>
#include <boost/concept_check.hpp>
#include <boost/bind.hpp>
using boost::bind;
#include <fstream>
#include <iostream>
#include <string>
#include <cerrno>
#include <ctime>
# ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::asctime; using ::gmtime; using ::localtime;
using ::difftime; using ::time; using ::tm; using ::mktime; }
# endif
namespace
{
bool report_throws;
fs::directory_iterator end_itr;
void create_file( const fs::path & ph, const std::string & contents )
{
std::ofstream f( ph.native_file_string().c_str() );
if ( !f )
throw fs::filesystem_error( "operations_test create_file",
ph, errno );
if ( !contents.empty() ) f << contents;
}
void verify_file( const fs::path & ph, const std::string & expected )
{
std::ifstream f( ph.native_file_string().c_str() );
if ( !f )
throw fs::filesystem_error( "operations_test verify_file",
ph, errno );
std::string contents;
f >> contents;
if ( contents != expected )
throw fs::filesystem_error( "operations_test verify_file",
ph, " contents \"" + contents
+ "\" != \"" + expected + "\"" );
}
template< typename F >
bool throws_fs_error( F func, fs::error_code ec =
::boost::filesystem::no_error ) // VC++ 7.1 build 2292 won't accept fs::
{
try { func(); }
catch ( const fs::filesystem_error & ex )
{
if ( report_throws ) std::cout << ex.what() << "\n";
if ( ec == fs::no_error || ec == ex.error() ) return true;
std::cout << "filesystem_error::error() reports " << ex.error()
<< ", should be " << ec
<< "\n native_error() is " << ex.native_error()
<< std::endl;
}
return false;
}
} // unnamed namespace
// test_main ---------------------------------------------------------------//
int test_main( int argc, char * argv[] )
{
if ( argc > 1 && *argv[1]=='-' && *(argv[1]+1)=='t' ) report_throws = true;
std::string platform( BOOST_PLATFORM );
// The choice of platform is make at runtime rather than compile-time
// so that compile errors for all platforms will be detected even though
// only the current platform is runtime tested.
# if defined( BOOST_POSIX )
platform = "POSIX";
# elif defined( BOOST_WINDOWS )
platform = "Windows";
# else
platform = ( platform == "Win32" || platform == "Win64" || platform == "Cygwin" )
? "Windows"
: "POSIX";
# endif
std::cout << "Platform is " << platform << '\n';
std::cout << "initial_path().string() is\n \""
<< fs::initial_path().string()
<< "\"\n";
std::cout << "initial_path().native_file_string() is\n \""
<< fs::initial_path().native_file_string()
<< "\"\n";
BOOST_CHECK( fs::initial_path().is_complete() );
BOOST_CHECK( fs::current_path().is_complete() );
BOOST_CHECK( fs::initial_path().string() == fs::current_path().string() );
BOOST_CHECK( fs::complete( "" ).empty() );
BOOST_CHECK( fs::complete( "/" ).string()
== fs::initial_path().root_path().string() );
BOOST_CHECK( fs::complete( "foo" ).string()
== fs::initial_path().string()+"/foo" );
BOOST_CHECK( fs::complete( "/foo" ).string()
== fs::initial_path().root_path().string()+"foo" );
fs::path dir( fs::initial_path() / "temp_fs_test_directory" );
// Windows only tests
if ( platform == "Windows" )
{
BOOST_CHECK( !fs::exists( fs::path( "//share-not/foo", fs::native ) ) );
BOOST_CHECK( dir.string().size() > 1
&& dir.string()[1] == ':' ); // verify path includes drive
BOOST_CHECK( fs::system_complete( "" ).empty() );
BOOST_CHECK( fs::system_complete( "/" ).string()
== fs::initial_path().root_path().string() );
BOOST_CHECK( fs::system_complete( "foo" ).string()
== fs::initial_path().string()+"/foo" );
BOOST_CHECK( fs::system_complete( "/foo" ).string()
== fs::initial_path().root_path().string()+"foo" );
// BOOST_CHECK( fs::complete( fs::path( "c:", fs::native ) ).string()
// == fs::initial_path().string() );
// BOOST_CHECK( fs::complete( fs::path( "c:foo", fs::native ) ).string()
// == fs::initial_path().string()+"/foo" );
BOOST_CHECK( fs::complete( fs::path( "c:/", fs::native ) ).string()
== "c:/" );
BOOST_CHECK( fs::complete( fs::path( "c:/foo", fs::native ) ).string()
== "c:/foo" );
BOOST_CHECK( fs::complete( fs::path( "//share", fs::native ) ).string()
== "//share" );
BOOST_CHECK( fs::system_complete( fs::path( fs::initial_path().root_name(),
fs::native ) ).string() == fs::initial_path().string() );
BOOST_CHECK( fs::system_complete( fs::path( fs::initial_path().root_name()
+ "foo", fs::native ) ).string() == fs::initial_path().string()+"/foo" );
BOOST_CHECK( fs::system_complete( fs::path( "c:/", fs::native ) ).string()
== "c:/" );
BOOST_CHECK( fs::system_complete( fs::path( "c:/foo", fs::native ) ).string()
== "c:/foo" );
BOOST_CHECK( fs::system_complete( fs::path( "//share", fs::native ) ).string()
== "//share" );
}
else if ( platform == "POSIX" )
{
BOOST_CHECK( fs::system_complete( "" ).empty() );
BOOST_CHECK( fs::initial_path().root_path().string() == "/" );
BOOST_CHECK( fs::system_complete( "/" ).string() == "/" );
BOOST_CHECK( fs::system_complete( "foo" ).string()
== fs::initial_path().string()+"/foo" );
BOOST_CHECK( fs::system_complete( "/foo" ).string()
== fs::initial_path().root_path().string()+"foo" );
}
fs::path ng( " no-way, Jose", fs::native );
fs::remove_all( dir ); // in case residue from prior failed tests
BOOST_CHECK( !fs::exists( dir ) );
// the bound functions should throw, so throws_fs_error() should return true
BOOST_CHECK( throws_fs_error( bind( fs::is_directory, ng ), fs::not_found_error ) );
BOOST_CHECK( throws_fs_error( bind( fs::file_size, ng ), fs::not_found_error ) );
BOOST_CHECK( throws_fs_error( bind( fs::is_directory, dir ) ) );
BOOST_CHECK( throws_fs_error( bind( fs::_is_empty, dir ) ) );
// test path::exception members
try { fs::is_directory( ng ); } // will throw
catch ( const fs::filesystem_error & ex )
{
BOOST_CHECK( ex.who() == "boost::filesystem::is_directory" );
BOOST_CHECK( ex.path1().string() == " no-way, Jose" );
}
BOOST_CHECK( fs::create_directory( dir ) );
BOOST_CHECK( fs::exists( dir ) );
BOOST_CHECK( fs::_is_empty( dir ) );
BOOST_CHECK( fs::is_directory( dir ) );
BOOST_CHECK( throws_fs_error( bind( fs::file_size, dir ),
fs::is_directory_error ) );
BOOST_CHECK( !fs::create_directory( dir ) );
BOOST_CHECK( !fs::symbolic_link_exists( dir ) );
BOOST_CHECK( !fs::symbolic_link_exists( "nosuchfileordirectory" ) );
fs::path d1( dir / "d1" );
BOOST_CHECK( fs::create_directory( d1 ) );
BOOST_CHECK( fs::exists( d1 ) );
BOOST_CHECK( fs::is_directory( d1 ) );
BOOST_CHECK( fs::_is_empty( d1 ) );
boost::function_requires< boost::InputIteratorConcept< fs::directory_iterator > >();
{
fs::directory_iterator dir_itr( dir );
BOOST_CHECK( dir_itr->leaf() == "d1" );
}
// create a second directory named d2
fs::path d2( dir / "d2" );
fs::create_directory(d2 );
BOOST_CHECK( fs::exists( d2 ) );
BOOST_CHECK( fs::is_directory( d2 ) );
// test the basic operation of directory_iterators, and test that
// stepping one iterator doesn't affect a different iterator.
{
fs::directory_iterator dir_itr( dir );
fs::directory_iterator dir_itr2( dir );
BOOST_CHECK( dir_itr->leaf() == "d1" || dir_itr->leaf() == "d2" );
BOOST_CHECK( dir_itr2->leaf() == "d1" || dir_itr2->leaf() == "d2" );
if ( dir_itr->leaf() == "d1" )
{
BOOST_CHECK( (++dir_itr)->leaf() == "d2" );
BOOST_CHECK( dir_itr2->leaf() == "d1" );
BOOST_CHECK( (++dir_itr2)->leaf() == "d2" );
}
else
{
BOOST_CHECK( (dir_itr)->leaf() == "d2" );
BOOST_CHECK( (++dir_itr)->leaf() == "d1" );
BOOST_CHECK( dir_itr2->leaf() == "d2" );
BOOST_CHECK( (++dir_itr2)->leaf() == "d1" );
}
BOOST_CHECK( ++dir_itr == fs::directory_iterator() );
BOOST_CHECK( dir_itr2 != fs::directory_iterator() );
BOOST_CHECK( ++dir_itr2 == fs::directory_iterator() );
}
{ // *i++ must work to meet the standard's InputIterator requirements
fs::directory_iterator dir_itr( dir );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -