1 | // libs/filesystem/test/convenience_test.cpp -------------------------------// |
---|
2 | |
---|
3 | // Copyright Beman Dawes, 2002 |
---|
4 | // Copyright Vladimir Prus, 2002 |
---|
5 | // Use, modification, and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | |
---|
9 | // See library home page at http://www.boost.org/libs/filesystem |
---|
10 | |
---|
11 | // VC++ 8.0 warns on various less-than-safe practices. |
---|
12 | // See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx |
---|
13 | // But at least in VC++ 8.0 betas, their own libraries use the problem |
---|
14 | // practices. So turn off the warnings. |
---|
15 | #define _CRT_SECURE_NO_DEPRECATE |
---|
16 | #define _SCL_SECURE_NO_DEPRECATE |
---|
17 | |
---|
18 | #include <boost/filesystem/convenience.hpp> |
---|
19 | namespace fs = boost::filesystem; |
---|
20 | using fs::path; |
---|
21 | |
---|
22 | #include <boost/test/minimal.hpp> |
---|
23 | #include <boost/bind.hpp> |
---|
24 | #include <fstream> |
---|
25 | #include <iostream> |
---|
26 | |
---|
27 | #ifndef BOOST_FILESYSTEM_NARROW_ONLY |
---|
28 | # define BOOST_FS_IS_EMPTY fs::is_empty |
---|
29 | # define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO<fs::path> |
---|
30 | #else |
---|
31 | # define BOOST_FS_IS_EMPTY fs::_is_empty |
---|
32 | # define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO |
---|
33 | #endif |
---|
34 | |
---|
35 | namespace |
---|
36 | { |
---|
37 | template< typename F > |
---|
38 | bool throws_fs_error( F func, fs::errno_type ec = 0 ) |
---|
39 | { |
---|
40 | try { func(); } |
---|
41 | |
---|
42 | catch ( const fs::filesystem_error & ex ) |
---|
43 | { |
---|
44 | if ( ec == 0 |
---|
45 | || ec == fs::lookup_error_code(ex.system_error()) ) return true; |
---|
46 | std::cout |
---|
47 | << "exception reports " << fs::lookup_error_code(ex.system_error()) |
---|
48 | << ", should be " << ec |
---|
49 | << "\n system_error() is " << ex.system_error() |
---|
50 | << std::endl; |
---|
51 | return false; |
---|
52 | } |
---|
53 | return false; |
---|
54 | } |
---|
55 | } |
---|
56 | int test_main( int, char*[] ) |
---|
57 | { |
---|
58 | path::default_name_check( fs::no_check ); // names below not valid on all O/S's |
---|
59 | // but they must be tested anyhow |
---|
60 | |
---|
61 | // create_directories() tests ----------------------------------------------// |
---|
62 | |
---|
63 | BOOST_CHECK( !fs::create_directories( "" ) ); // should be harmless |
---|
64 | BOOST_CHECK( !fs::create_directories( "/" ) ); // ditto |
---|
65 | |
---|
66 | fs::remove_all( "xx" ); // make sure slate is blank |
---|
67 | BOOST_CHECK( !fs::exists( "xx" ) ); // reality check |
---|
68 | |
---|
69 | BOOST_CHECK( fs::create_directories( "xx" ) ); |
---|
70 | BOOST_CHECK( fs::exists( "xx" ) ); |
---|
71 | BOOST_CHECK( fs::is_directory( "xx" ) ); |
---|
72 | |
---|
73 | BOOST_CHECK( fs::create_directories( "xx/ww/zz" ) ); |
---|
74 | BOOST_CHECK( fs::exists( "xx" ) ); |
---|
75 | BOOST_CHECK( fs::exists( "xx/ww" ) ); |
---|
76 | BOOST_CHECK( fs::exists( "xx/ww/zz" ) ); |
---|
77 | BOOST_CHECK( fs::is_directory( "xx" ) ); |
---|
78 | BOOST_CHECK( fs::is_directory( "xx/ww" ) ); |
---|
79 | BOOST_CHECK( fs::is_directory( "xx/ww/zz" ) ); |
---|
80 | |
---|
81 | path is_a_file( "xx/uu" ); |
---|
82 | { |
---|
83 | std::ofstream f( is_a_file.native_file_string().c_str() ); |
---|
84 | BOOST_CHECK( !!f ); |
---|
85 | } |
---|
86 | BOOST_CHECK( throws_fs_error( |
---|
87 | boost::bind( BOOST_BND(fs::create_directories), is_a_file ) ) ); |
---|
88 | BOOST_CHECK( throws_fs_error( |
---|
89 | boost::bind( BOOST_BND(fs::create_directories), is_a_file / "aa" ) ) ); |
---|
90 | |
---|
91 | // extension() tests ---------------------------------------------------------// |
---|
92 | |
---|
93 | BOOST_CHECK( fs::extension("a/b") == "" ); |
---|
94 | BOOST_CHECK( fs::extension("a/b.txt") == ".txt" ); |
---|
95 | BOOST_CHECK( fs::extension("a/b.") == "." ); |
---|
96 | BOOST_CHECK( fs::extension("a.b.c") == ".c" ); |
---|
97 | BOOST_CHECK( fs::extension("a.b.c.") == "." ); |
---|
98 | BOOST_CHECK( fs::extension("") == "" ); |
---|
99 | BOOST_CHECK( fs::extension("a/") == "." ); |
---|
100 | |
---|
101 | // basename() tests ----------------------------------------------------------// |
---|
102 | |
---|
103 | BOOST_CHECK( fs::basename("b") == "b" ); |
---|
104 | BOOST_CHECK( fs::basename("a/b.txt") == "b" ); |
---|
105 | BOOST_CHECK( fs::basename("a/b.") == "b" ); |
---|
106 | BOOST_CHECK( fs::basename("a.b.c") == "a.b" ); |
---|
107 | BOOST_CHECK( fs::basename("a.b.c.") == "a.b.c" ); |
---|
108 | BOOST_CHECK( fs::basename("") == "" ); |
---|
109 | |
---|
110 | // change_extension tests ---------------------------------------------------// |
---|
111 | |
---|
112 | BOOST_CHECK( fs::change_extension("a.txt", ".tex").string() == "a.tex" ); |
---|
113 | BOOST_CHECK( fs::change_extension("a.", ".tex").string() == "a.tex" ); |
---|
114 | BOOST_CHECK( fs::change_extension("a", ".txt").string() == "a.txt" ); |
---|
115 | BOOST_CHECK( fs::change_extension("a.b.txt", ".tex").string() == "a.b.tex" ); |
---|
116 | // see the rationale in html docs for explanation why this works |
---|
117 | BOOST_CHECK( fs::change_extension("", ".png").string() == ".png" ); |
---|
118 | |
---|
119 | // what() tests --------------------------------------------------------------// |
---|
120 | |
---|
121 | try { throw fs::filesystem_path_error( "abc", "p1", "p2", 0 ); } |
---|
122 | catch ( const fs::filesystem_path_error & ex ) |
---|
123 | { |
---|
124 | # if !defined( BOOST_MSVC ) || BOOST_MSVC >= 1300 // > VC++ 7.0 |
---|
125 | BOOST_CHECK( fs::what(ex) == std::string( "abc: \"p1\", \"p2\"" ) ); |
---|
126 | # endif |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|