📄 operations_test.cpp
字号:
// Boost operations_test.cpp -----------------------------------------------//// Copyright Beman Dawes 2002.// Distributed under 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/config/warning_disable.hpp>// See deprecated_test for tests of deprecated features#define BOOST_FILESYSTEM_NO_DEPRECATED#include <boost/filesystem/operations.hpp>#include <boost/filesystem/convenience.hpp>#include <boost/cerrno.hpp>namespace fs = boost::filesystem;#include <boost/config.hpp>#include <boost/test/minimal.hpp>//#include <boost/concept_check.hpp>using boost::system::error_code;using boost::system::system_category;using boost::system::system_error;#include <fstream>#include <iostream>#include <string>#include <cstring> // for strncmp, etc.#include <ctime>#include <cstdlib> // for system()#ifndef BOOST_FILESYSTEM_NARROW_ONLY# define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO<fs::path>#else# define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO#endif// VC++ 7.0 and earlier has a serious namespace bug that causes a clash// between boost::filesystem::is_empty and the unrelated type trait// boost::is_empty.#if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300# define BOOST_FS_IS_EMPTY fs::is_empty#else# define BOOST_FS_IS_EMPTY fs::_is_empty#endif# ifdef BOOST_NO_STDC_NAMESPACE namespace std { using ::asctime; using ::gmtime; using ::localtime; using ::difftime; using ::time; using ::tm; using ::mktime; using ::system; }# endif#ifdef BOOST_WINDOWS_API# include <windows.h>#endif#define CHECK_EXCEPTION(Functor,Expect) throws_fs_error(Functor,Expect,__LINE__)namespace{ typedef int errno_t; std::string platform( BOOST_PLATFORM ); bool report_throws; fs::directory_iterator end_itr; unsigned short language_id; // 0 except for Windows const char * temp_dir_name = "temp_fs_test_dir"; void create_file( const fs::path & ph, const std::string & contents ) { std::ofstream f( ph.file_string().c_str() ); if ( !f ) throw fs::filesystem_error( "operations_test create_file", ph, error_code(errno, system_category) ); if ( !contents.empty() ) f << contents; } void verify_file( const fs::path & ph, const std::string & expected ) { std::ifstream f( ph.file_string().c_str() ); if ( !f ) throw fs::filesystem_error( "operations_test verify_file", ph, error_code(errno, system_category) ); std::string contents; f >> contents; if ( contents != expected ) throw fs::filesystem_error( "operations_test verify_file contents \"" + contents + "\" != \"" + expected + "\"", ph, error_code() ); } template< typename F > bool throws_fs_error( F func, errno_t en, int line ) { try { func(); } catch ( const fs::filesystem_error & ex ) { if ( report_throws ) { // use the what() convenience function to display exceptions std::cout << "\n" << ex.what() << "\n"; } if ( en == 0 || en == ex.code().default_error_condition().value() ) return true; std::cout << "\nWarning: line " << line << " exception reports default_error_condition().value() " << ex.code().default_error_condition().value() << ", should be " << en << "\n value() is " << ex.code().value() << std::endl; return true; } return false; } // compile-only two argument "do-the-right-thing" tests // verifies that all overload combinations compile without error void do_not_call() { fs::path p; std::string s; const char * a = 0; fs::copy_file( p, p ); fs::copy_file( s, p ); fs::copy_file( a, p ); fs::copy_file( p, s ); fs::copy_file( p, a ); fs::copy_file( s, s ); fs::copy_file( a, s ); fs::copy_file( s, a ); fs::copy_file( a, a ); } void exception_tests() { bool exception_thrown; exception_thrown = false; try { fs::create_directory( "no-such-dir/foo/bar" ); } catch ( std::runtime_error x ) { exception_thrown = true; if ( report_throws ) std::cout << x.what() << std::endl; if ( platform == "Windows" && language_id == 0x0409 ) // English (United States) // the stdcxx standard library apparently appends additional info // to what(), so check only the initial portion: BOOST_CHECK( std::strncmp( x.what(), "boost::filesystem::create_directory", sizeof("boost::filesystem::create_directory")-1 ) == 0 ); } BOOST_CHECK( exception_thrown ); exception_thrown = false; try { fs::create_directory( "no-such-dir/foo/bar" ); } catch ( system_error x ) { exception_thrown = true; if ( report_throws ) std::cout << x.what() << std::endl; if ( platform == "Windows" && language_id == 0x0409 ) // English (United States) BOOST_CHECK( std::strcmp( x.what(), "boost::filesystem::create_directory: The system cannot find the path specified" ) == 0 ); } BOOST_CHECK( exception_thrown ); exception_thrown = false; try { fs::create_directory( "no-such-dir/foo/bar" ); } catch ( fs::filesystem_error x ) { exception_thrown = true; if ( report_throws ) std::cout << x.what() << std::endl; if ( platform == "Windows" && language_id == 0x0409 ) // English (United States) { bool ok ( std::strcmp( x.what(), "boost::filesystem::create_directory: The system cannot find the path specified: \"no-such-dir\\foo\\bar\"" ) == 0 ); BOOST_CHECK( ok ); if ( !ok ) { std::cout << "what returns \"" << x.what() << "\"" << std::endl; } } } BOOST_CHECK( exception_thrown ); exception_thrown = false; try { fs::create_directory( "no-such-dir/foo/bar" ); } catch ( const fs::filesystem_error & x ) { exception_thrown = true; if ( report_throws ) std::cout << x.what() << std::endl; if ( platform == "Windows" && language_id == 0x0409 ) // English (United States) { bool ok ( std::strcmp( x.what(), "boost::filesystem::create_directory: The system cannot find the path specified: \"no-such-dir\\foo\\bar\"" ) == 0 ); BOOST_CHECK( ok ); if ( !ok ) { std::cout << "what returns \"" << x.what() << "\"" << std::endl; } } } BOOST_CHECK( exception_thrown ); } void bad_file_size() { fs::file_size( " No way, Jose" ); } void bad_directory_size() { fs::file_size( fs::current_path<fs::path>() ); } fs::path bad_create_directory_path; void bad_create_directory() { fs::create_directory( bad_create_directory_path ); } void bad_equivalent() { fs::equivalent( "no-such-path", "another-not-present-path" ); } fs::path bad_remove_dir; void bad_remove() { fs::remove( bad_remove_dir ); } class renamer { public: renamer( const fs::path & p1, const fs::path & p2 ) : from(p1), to(p2) {} void operator()() { fs::rename( from, to ); } private: fs::path from; fs::path to; }; } // unnamed namespace// test_main ---------------------------------------------------------------//int test_main( int argc, char * argv[] ){ if ( argc > 1 && *argv[1]=='-' && *(argv[1]+1)=='t' ) report_throws = true; // 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_API ) platform = "POSIX";# elif defined( BOOST_WINDOWS_API ) platform = "Windows";# if !defined(__MINGW32__) && !defined(__CYGWIN__) language_id = ::GetUserDefaultUILanguage();# else language_id = 0x0409; // Assume US English# endif# else platform = ( platform == "Win32" || platform == "Win64" || platform == "Cygwin" ) ? "Windows" : "POSIX";# endif std::cout << "API is " << platform << std::endl; exception_tests(); std::cout << "\ninitial_path<path>().string() is\n \"" << fs::initial_path<fs::path>().string() << "\"\n"; std::cout << "\ninitial_path<fs::path>().file_string() is\n \"" << fs::initial_path<fs::path>().file_string() << "\"\n\n"; BOOST_CHECK( fs::initial_path<fs::path>().is_complete() ); BOOST_CHECK( fs::current_path<fs::path>().is_complete() ); BOOST_CHECK( fs::initial_path<fs::path>().string() == fs::current_path<fs::path>().string() ); BOOST_CHECK( fs::complete( "" ).empty() ); BOOST_CHECK( fs::complete( "/" ).string() == fs::initial_path<fs::path>().root_path().string() ); BOOST_CHECK( fs::complete( "foo" ).string() == fs::initial_path<fs::path>().string()+"/foo" ); BOOST_CHECK( fs::complete( "/foo" ).string() == fs::initial_path<fs::path>().root_path().string()+"foo" ); BOOST_CHECK( fs::complete( "foo", fs::path( "//net/bar" ) ).string() == "//net/bar/foo" ); // predicate and status tests BOOST_CHECK( fs::exists( "/" ) ); fs::path ng( " no-way, Jose" ); BOOST_CHECK( !fs::exists( ng ) ); BOOST_CHECK( !fs::is_directory( ng ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -