📄 operations_test.cpp
字号:
BOOST_CHECK( !fs::is_regular_file( ng ) ); BOOST_CHECK( !fs::is_symlink( ng ) ); fs::file_status stat( fs::status( ng ) ); BOOST_CHECK( fs::status_known( stat ) ); BOOST_CHECK( !fs::exists( stat ) ); BOOST_CHECK( !fs::is_directory( stat ) ); BOOST_CHECK( !fs::is_regular_file( stat ) ); BOOST_CHECK( !fs::is_other( stat ) ); BOOST_CHECK( !fs::is_symlink( stat ) ); stat = fs::status( "" ); BOOST_CHECK( fs::status_known( stat ) ); BOOST_CHECK( !fs::exists( stat ) ); BOOST_CHECK( !fs::is_directory( stat ) ); BOOST_CHECK( !fs::is_regular_file( stat ) ); BOOST_CHECK( !fs::is_other( stat ) ); BOOST_CHECK( !fs::is_symlink( stat ) ); fs::path dir( fs::initial_path<fs::path>() / temp_dir_name ); if ( fs::exists( dir ) ) fs::remove_all( dir ); // remove residue from prior failed tests BOOST_CHECK( !fs::exists( dir ) ); // create a directory, then check it for consistency // take extra care to report problems, since if this fails // many subsequent tests will fail try { fs::create_directory( dir ); } catch ( const fs::filesystem_error & x ) { std::cout << x.what() << "\n\n" "***** Creating directory " << dir.string() << " failed. *****\n" "***** This is a serious error that will prevent further tests *****\n" "***** from returning useful results. Further testing is aborted. *****\n\n"; return 1; } catch ( ... ) { std::cout << "\n\n" "***** Creating directory " << dir.string() << " failed. *****\n" "***** This is a serious error that will prevent further tests *****\n" "***** from returning useful results. Further testing is aborted. *****\n\n"; return 1; } BOOST_CHECK( fs::exists( dir ) ); BOOST_CHECK( BOOST_FS_IS_EMPTY( dir ) ); BOOST_CHECK( fs::is_directory( dir ) ); BOOST_CHECK( !fs::is_regular_file( dir ) ); BOOST_CHECK( !fs::is_other( dir ) ); BOOST_CHECK( !fs::is_symlink( dir ) ); stat = fs::status( dir ); BOOST_CHECK( fs::exists( stat ) ); BOOST_CHECK( fs::is_directory( stat ) ); BOOST_CHECK( !fs::is_regular_file( stat ) ); BOOST_CHECK( !fs::is_other( stat ) ); BOOST_CHECK( !fs::is_symlink( stat ) ); // Windows only tests if ( platform == "Windows" ) { BOOST_CHECK( !fs::exists( fs::path( "//share-not" ) ) ); BOOST_CHECK( !fs::exists( fs::path( "//share-not/" ) ) ); BOOST_CHECK( !fs::exists( fs::path( "//share-not/foo" ) ) ); BOOST_CHECK( !fs::exists( "tools/jam/src/:sys:stat.h" ) ); // !exists() if ERROR_INVALID_NAME BOOST_CHECK( !fs::exists( ":sys:stat.h" ) ); // !exists() if ERROR_INVALID_PARAMETER 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<fs::path>().root_path().string() ); BOOST_CHECK( fs::system_complete( "foo" ).string() == fs::initial_path<fs::path>().string()+"/foo" ); BOOST_CHECK( fs::system_complete( "/foo" ).string() == fs::initial_path<fs::path>().root_path().string()+"foo" ); BOOST_CHECK( fs::complete( fs::path( "c:/" ) ).string() == "c:/" ); BOOST_CHECK( fs::complete( fs::path( "c:/foo" ) ).string() == "c:/foo" ); BOOST_CHECK( fs::system_complete( fs::path( fs::initial_path<fs::path>().root_name() ) ).string() == fs::initial_path<fs::path>().string() ); BOOST_CHECK( fs::system_complete( fs::path( fs::initial_path<fs::path>().root_name() + "foo" ) ).string() == fs::initial_path<fs::path>().string()+"/foo" ); BOOST_CHECK( fs::system_complete( fs::path( "c:/" ) ).string() == "c:/" ); BOOST_CHECK( fs::system_complete( fs::path( "c:/foo" ) ).string() == "c:/foo" ); BOOST_CHECK( fs::system_complete( fs::path( "//share" ) ).string() == "//share" ); } // Windows else if ( platform == "POSIX" ) { BOOST_CHECK( fs::system_complete( "" ).empty() ); BOOST_CHECK( fs::initial_path<fs::path>().root_path().string() == "/" ); BOOST_CHECK( fs::system_complete( "/" ).string() == "/" ); BOOST_CHECK( fs::system_complete( "foo" ).string() == fs::initial_path<fs::path>().string()+"/foo" ); BOOST_CHECK( fs::system_complete( "/foo" ).string() == fs::initial_path<fs::path>().root_path().string()+"foo" ); } // POSIX // the bound functions should throw, so CHECK_EXCEPTION() should return true BOOST_CHECK( CHECK_EXCEPTION( bad_file_size, ENOENT ) ); // test path::exception members try { fs::file_size( ng ); } // will throw catch ( const fs::filesystem_error & ex ) { BOOST_CHECK( ex.path1().string() == " no-way, Jose" ); } // several functions give unreasonable results if uintmax_t isn't 64-bits std::cout << "sizeof(boost::uintmax_t) = " << sizeof(boost::uintmax_t) << '\n'; BOOST_CHECK( sizeof( boost::uintmax_t ) >= 8 ); // set the current directory, then check it for consistency fs::path original_dir = fs::current_path<fs::path>(); BOOST_CHECK( dir != original_dir ); fs::current_path( dir ); BOOST_CHECK( fs::current_path<fs::path>() == dir ); BOOST_CHECK( fs::current_path<fs::path>() != original_dir ); fs::current_path( original_dir ); BOOST_CHECK( fs::current_path<fs::path>() == original_dir ); BOOST_CHECK( fs::current_path<fs::path>() != dir ); // make sure the overloads work fs::current_path( dir.string().c_str() ); BOOST_CHECK( fs::current_path<fs::path>() == dir ); BOOST_CHECK( fs::current_path<fs::path>() != original_dir ); fs::current_path( original_dir.string() ); BOOST_CHECK( fs::current_path<fs::path>() == original_dir ); BOOST_CHECK( fs::current_path<fs::path>() != dir ); // make some reasonable assuptions for testing purposes fs::space_info spi( fs::space( dir ) ); BOOST_CHECK( spi.capacity > 1000000 ); BOOST_CHECK( spi.free > 1000 ); BOOST_CHECK( spi.capacity > spi.free ); BOOST_CHECK( spi.free >= spi.available ); // it is convenient to display space, but older VC++ versions choke # if !defined(BOOST_MSVC) || _MSC_VER >= 1300 // 1300 == VC++ 7.0 std::cout << " capacity = " << spi.capacity << '\n'; std::cout << " free = " << spi.free << '\n'; std::cout << "available = " << spi.available << '\n';# endif if ( platform == "Windows" ) BOOST_CHECK( CHECK_EXCEPTION( bad_directory_size, ENOENT ) ); else BOOST_CHECK( CHECK_EXCEPTION( bad_directory_size, 0 ) ); BOOST_CHECK( !fs::create_directory( dir ) ); BOOST_CHECK( !fs::is_symlink( dir ) ); BOOST_CHECK( !fs::is_symlink( "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( BOOST_FS_IS_EMPTY( d1 ) );// boost::function_requires< boost::InputIteratorConcept< fs::directory_iterator > >(); bool dir_itr_exception(false); try { fs::directory_iterator it( "" ); } catch ( const fs::filesystem_error & ) { dir_itr_exception = true; } BOOST_CHECK( dir_itr_exception ); dir_itr_exception = false; try { fs::directory_iterator it( "nosuchdirectory" ); } catch ( const fs::filesystem_error & ) { dir_itr_exception = true; } BOOST_CHECK( dir_itr_exception ); dir_itr_exception = false; try { error_code ec; fs::directory_iterator it( "nosuchdirectory", ec ); BOOST_CHECK( ec ); BOOST_CHECK( ec == fs::detail::not_found_error() ); } catch ( const fs::filesystem_error & ) { dir_itr_exception = true; } BOOST_CHECK( !dir_itr_exception ); { // probe query function overloads fs::directory_iterator dir_itr( dir ); BOOST_CHECK( fs::is_directory( *dir_itr ) ); BOOST_CHECK( fs::is_directory( dir_itr->status() ) ); BOOST_CHECK( fs::is_directory( fs::symlink_status(*dir_itr) ) ); BOOST_CHECK( fs::is_directory( dir_itr->symlink_status() ) ); BOOST_CHECK( dir_itr->path().filename() == "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 ); BOOST_CHECK( fs::exists(dir_itr->status()) ); BOOST_CHECK( fs::is_directory(dir_itr->status()) ); BOOST_CHECK( !fs::is_regular_file(dir_itr->status()) ); BOOST_CHECK( !fs::is_other(dir_itr->status()) ); BOOST_CHECK( !fs::is_symlink(dir_itr->status()) ); fs::directory_iterator dir_itr2( dir ); BOOST_CHECK( dir_itr->path().filename() == "d1" || dir_itr->path().filename() == "d2" ); BOOST_CHECK( dir_itr2->path().filename() == "d1" || dir_itr2->path().filename() == "d2" ); if ( dir_itr->path().filename() == "d1" ) { BOOST_CHECK( (++dir_itr)->path().filename() == "d2" ); BOOST_CHECK( dir_itr2->path().filename() == "d1" ); BOOST_CHECK( (++dir_itr2)->path().filename() == "d2" ); } else { BOOST_CHECK( dir_itr->path().filename() == "d2" ); BOOST_CHECK( (++dir_itr)->path().filename() == "d1" ); BOOST_CHECK( (dir_itr2)->path().filename() == "d2" ); BOOST_CHECK( (++dir_itr2)->path().filename() == "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 ); BOOST_CHECK( dir_itr->path().filename() == "d1" || dir_itr->path().filename() == "d2" ); if ( dir_itr->path().filename() == "d1" ) { BOOST_CHECK( (*dir_itr++).path().filename() == "d1" ); BOOST_CHECK( dir_itr->path().filename() == "d2" ); } else { // Check C++98 input iterator requirements BOOST_CHECK( (*dir_itr++).path().filename() == "d2" ); // input iterator requirements in the current WP would require this check: // BOOST_CHECK( implicit_cast<std::string const&>(*dir_itr++).filename() == "d1" ); BOOST_CHECK( dir_itr->path().filename() == "d1" ); } // test case reported in comment to SourceForge bug tracker [937606] fs::directory_iterator it( dir ); const fs::path p1 = *it++; BOOST_CHECK( it != fs::directory_iterator() ); const fs::path p2 = *it++; BOOST_CHECK( p1 != p2 ); BOOST_CHECK( it == fs::directory_iterator() ); } // Windows has a tricky special case when just the root-name is given, // causing the rest of the path to default to the current directory. // Reported as S/F bug [ 1259176 ] if ( platform == "Windows" ) { fs::path root_name_path( fs::current_path<fs::path>().root_name() ); fs::directory_iterator it( root_name_path ); BOOST_CHECK( it != fs::directory_iterator() ); BOOST_CHECK( fs::exists( *it ) ); BOOST_CHECK( it->path().parent_path() == root_name_path ); bool found(false); do { if ( it->path().filename() == temp_dir_name ) found = true; } while ( ++it != fs::directory_iterator() ); BOOST_CHECK( found ); } // create an empty file named "f0" fs::path file_ph( dir / "f0"); create_file( file_ph, "" ); BOOST_CHECK( fs::exists( file_ph ) ); BOOST_CHECK( !fs::is_directory( file_ph ) ); BOOST_CHECK( fs::is_regular_file( file_ph ) ); BOOST_CHECK( BOOST_FS_IS_EMPTY( file_ph ) ); BOOST_CHECK( fs::file_size( file_ph ) == 0 ); bad_create_directory_path = file_ph; BOOST_CHECK( CHECK_EXCEPTION( bad_create_directory, EEXIST ) ); stat = fs::status( file_ph ); BOOST_CHECK( fs::status_known( stat ) ); BOOST_CHECK( fs::exists( stat ) ); BOOST_CHECK( !fs::is_directory( stat ) ); BOOST_CHECK( fs::is_regular_file( stat ) ); BOOST_CHECK( !fs::is_other( stat ) ); BOOST_CHECK( !fs::is_symlink( stat ) ); // create a file named "f1" file_ph = dir / "f1"; create_file( file_ph, "foobar1" ); BOOST_CHECK( fs::exists( file_ph ) ); BOOST_CHECK( !fs::is_directory( file_ph ) ); BOOST_CHECK( fs::is_regular_file( file_ph ) ); BOOST_CHECK( fs::file_size( file_ph ) == 7 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -