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 | #include <boost/filesystem/convenience.hpp> |
---|
12 | #include <boost/filesystem/exception.hpp> |
---|
13 | namespace fs = boost::filesystem; |
---|
14 | using fs::path; |
---|
15 | |
---|
16 | #include <boost/test/minimal.hpp> |
---|
17 | #include <boost/bind.hpp> |
---|
18 | #include <fstream> |
---|
19 | #include <iostream> |
---|
20 | |
---|
21 | namespace |
---|
22 | { |
---|
23 | template< typename F > |
---|
24 | bool throws_fs_error( F func, fs::error_code ec = |
---|
25 | ::boost::filesystem::no_error ) // VC++ 7.1 build 2292 won't accept fs:: |
---|
26 | { |
---|
27 | try { func(); } |
---|
28 | |
---|
29 | catch ( const fs::filesystem_error & ex ) |
---|
30 | { |
---|
31 | if ( ec == fs::no_error || ec == ex.error() ) return true; |
---|
32 | std::cout << "filesystem_error::error() reports " << ex.error() |
---|
33 | << ", should be " << ec |
---|
34 | << "\n native_error() is " << ex.native_error() |
---|
35 | << std::endl; |
---|
36 | } |
---|
37 | return false; |
---|
38 | } |
---|
39 | } |
---|
40 | int test_main( int, char*[] ) |
---|
41 | { |
---|
42 | path::default_name_check( fs::no_check ); // names below not valid on all O/S's |
---|
43 | // but they must be tested anyhow |
---|
44 | |
---|
45 | // create_directories() tests ----------------------------------------------// |
---|
46 | |
---|
47 | BOOST_CHECK( !fs::create_directories( "" ) ); // should be harmless |
---|
48 | BOOST_CHECK( !fs::create_directories( "/" ) ); // ditto |
---|
49 | |
---|
50 | fs::remove_all( "xx" ); // make sure slate is blank |
---|
51 | BOOST_CHECK( !fs::exists( "xx" ) ); // reality check |
---|
52 | |
---|
53 | BOOST_CHECK( fs::create_directories( "xx" ) ); |
---|
54 | BOOST_CHECK( fs::exists( "xx" ) ); |
---|
55 | BOOST_CHECK( fs::is_directory( "xx" ) ); |
---|
56 | |
---|
57 | BOOST_CHECK( fs::create_directories( "xx/ww/zz" ) ); |
---|
58 | BOOST_CHECK( fs::exists( "xx" ) ); |
---|
59 | BOOST_CHECK( fs::exists( "xx/ww" ) ); |
---|
60 | BOOST_CHECK( fs::exists( "xx/ww/zz" ) ); |
---|
61 | BOOST_CHECK( fs::is_directory( "xx" ) ); |
---|
62 | BOOST_CHECK( fs::is_directory( "xx/ww" ) ); |
---|
63 | BOOST_CHECK( fs::is_directory( "xx/ww/zz" ) ); |
---|
64 | |
---|
65 | path is_a_file( "xx/uu" ); |
---|
66 | { |
---|
67 | std::ofstream f( is_a_file.native_file_string().c_str() ); |
---|
68 | BOOST_CHECK( !!f ); |
---|
69 | } |
---|
70 | BOOST_CHECK( |
---|
71 | throws_fs_error( boost::bind( fs::create_directories, is_a_file ) ) ); |
---|
72 | BOOST_CHECK( |
---|
73 | throws_fs_error( boost::bind( fs::create_directories, is_a_file / "aa" ) ) ); |
---|
74 | |
---|
75 | // extension() tests ----------------------------------------------------------// |
---|
76 | |
---|
77 | BOOST_CHECK( fs::extension("a/b") == "" ); |
---|
78 | BOOST_CHECK( fs::extension("a/b.txt") == ".txt" ); |
---|
79 | BOOST_CHECK( fs::extension("a/b.") == "." ); |
---|
80 | BOOST_CHECK( fs::extension("a.b.c") == ".c" ); |
---|
81 | BOOST_CHECK( fs::extension("a.b.c.") == "." ); |
---|
82 | BOOST_CHECK( fs::extension("") == "" ); |
---|
83 | BOOST_CHECK( fs::extension("a/") == "" ); |
---|
84 | |
---|
85 | // basename() tests ----------------------------------------------------------// |
---|
86 | |
---|
87 | BOOST_CHECK( fs::basename("b") == "b" ); |
---|
88 | BOOST_CHECK( fs::basename("a/b.txt") == "b" ); |
---|
89 | BOOST_CHECK( fs::basename("a/b.") == "b" ); |
---|
90 | BOOST_CHECK( fs::basename("a.b.c") == "a.b" ); |
---|
91 | BOOST_CHECK( fs::basename("a.b.c.") == "a.b.c" ); |
---|
92 | BOOST_CHECK( fs::basename("") == "" ); |
---|
93 | |
---|
94 | // change_extension tests ---------------------------------------------------// |
---|
95 | |
---|
96 | BOOST_CHECK( fs::change_extension("a.txt", ".tex").string() == "a.tex" ); |
---|
97 | BOOST_CHECK( fs::change_extension("a.", ".tex").string() == "a.tex" ); |
---|
98 | BOOST_CHECK( fs::change_extension("a", ".txt").string() == "a.txt" ); |
---|
99 | BOOST_CHECK( fs::change_extension("a.b.txt", ".tex").string() == "a.b.tex" ); |
---|
100 | // see the rationale in html docs for explanation why this works |
---|
101 | BOOST_CHECK( fs::change_extension("", ".png").string() == ".png" ); |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|