File: convenience_test.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (199 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//  libs/filesystem/test/convenience_test.cpp  -------------------------------//

//  Copyright Beman Dawes, 2002
//  Copyright Vladimir Prus, 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

//  VC++ 8.0 warns on various less-than-safe practices.
//  See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
//  But at least in VC++ 8.0 betas, their own libraries use the problem
//  practices. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE

#include <boost/filesystem/convenience.hpp>
namespace fs = boost::filesystem;
using fs::path;
namespace sys = boost::system;

#include <boost/test/minimal.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include <iostream>

#ifndef BOOST_FILESYSTEM_NARROW_ONLY
# define BOOST_FS_IS_EMPTY fs::is_empty
# define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO<fs::path>
#else
# define BOOST_FS_IS_EMPTY fs::_is_empty
# define BOOST_BND(BOOST_FUNC_TO_DO) BOOST_FUNC_TO_DO
#endif

namespace
{
  template< typename F >
    bool throws_fs_error( F func )
  {
    try { func(); }

    catch ( const fs::filesystem_error & )
    {
      return true;
    }
    return false;
  }

    void create_recursive_iterator( const fs::path & ph )
    {
      fs::recursive_directory_iterator it( ph );
    }
}

//  --------------------------------------------------------------------------//

int test_main( int, char*[] )
{
  path::default_name_check( fs::no_check ); // names below not valid on all O/S's
                                            // but they must be tested anyhow

//  create_directories() tests  ----------------------------------------------//

  BOOST_CHECK( !fs::create_directories( "" ) );  // should be harmless
  BOOST_CHECK( !fs::create_directories( "/" ) ); // ditto

  fs::remove_all( "xx" );  // make sure slate is blank
  BOOST_CHECK( !fs::exists( "xx" ) ); // reality check

  BOOST_CHECK( fs::create_directories( "xx" ) );
  BOOST_CHECK( fs::exists( "xx" ) );
  BOOST_CHECK( fs::is_directory( "xx" ) );

  BOOST_CHECK( fs::create_directories( "xx/yy/zz" ) );
  BOOST_CHECK( fs::exists( "xx" ) );
  BOOST_CHECK( fs::exists( "xx/yy" ) );
  BOOST_CHECK( fs::exists( "xx/yy/zz" ) );
  BOOST_CHECK( fs::is_directory( "xx" ) );
  BOOST_CHECK( fs::is_directory( "xx/yy" ) );
  BOOST_CHECK( fs::is_directory( "xx/yy/zz" ) );

  path is_a_file( "xx/uu" );
  {
    std::ofstream f( is_a_file.native_file_string().c_str() );
    BOOST_CHECK( !!f );
  }
  BOOST_CHECK( throws_fs_error(
    boost::bind( BOOST_BND(fs::create_directories), is_a_file ) ) );
  BOOST_CHECK( throws_fs_error(
    boost::bind( BOOST_BND(fs::create_directories), is_a_file / "aa" ) ) );
  
// extension() tests ---------------------------------------------------------//

  BOOST_CHECK( fs::extension("a/b") == "" );
  BOOST_CHECK( fs::extension("a/b.txt") == ".txt" );
  BOOST_CHECK( fs::extension("a/b.") == "." );
  BOOST_CHECK( fs::extension("a.b.c") == ".c" );
  BOOST_CHECK( fs::extension("a.b.c.") == "." );
  BOOST_CHECK( fs::extension("") == "" );
  BOOST_CHECK( fs::extension("a/") == "." );
  
// basename() tests ----------------------------------------------------------//

  BOOST_CHECK( fs::basename("b") == "b" );
  BOOST_CHECK( fs::basename("a/b.txt") == "b" );
  BOOST_CHECK( fs::basename("a/b.") == "b" ); 
  BOOST_CHECK( fs::basename("a.b.c") == "a.b" );
  BOOST_CHECK( fs::basename("a.b.c.") == "a.b.c" );
  BOOST_CHECK( fs::basename("") == "" );
  
// change_extension tests ---------------------------------------------------//

  BOOST_CHECK( fs::change_extension("a.txt", ".tex").string() == "a.tex" );
  BOOST_CHECK( fs::change_extension("a.", ".tex").string() == "a.tex" );
  BOOST_CHECK( fs::change_extension("a", ".txt").string() == "a.txt" );
  BOOST_CHECK( fs::change_extension("a.b.txt", ".tex").string() == "a.b.tex" );  
  // see the rationale in html docs for explanation why this works
  BOOST_CHECK( fs::change_extension("", ".png").string() == ".png" );

// recursive_directory_iterator tests ----------------------------------------//

  sys::error_code ec;
  fs::recursive_directory_iterator it( "/no-such-path", ec );
  BOOST_CHECK( ec );
  BOOST_CHECK( throws_fs_error(
    boost::bind( create_recursive_iterator, "/no-such-path" ) ) );

  fs::remove( "xx/uu" );

#ifdef BOOST_WINDOWS_API
  // These tests depends on ordering of directory entries, and that's guaranteed
  // on Windows but not necessarily on other operating systems
  {
    std::ofstream f( "xx/yya" );
    BOOST_CHECK( !!f );
  }

  for ( it = fs::recursive_directory_iterator( "xx" );
        it != fs::recursive_directory_iterator(); ++it )
    { std::cout << *it << '\n'; }

  it = fs::recursive_directory_iterator( "xx" );
  BOOST_CHECK( it->path() == "xx/yy" );
  BOOST_CHECK( it.level() == 0 );
  ++it;
  BOOST_CHECK( it->path() == "xx/yy/zz" );
  BOOST_CHECK( it.level() == 1 );
  it.pop();
  BOOST_CHECK( it->path() == "xx/yya" );
  BOOST_CHECK( it.level() == 0 );
  it++;
  BOOST_CHECK( it == fs::recursive_directory_iterator() );

  it = fs::recursive_directory_iterator( "xx" );
  BOOST_CHECK( it->path() == "xx/yy" );
  it.no_push();
  ++it;
  BOOST_CHECK( it->path() == "xx/yya" );
  ++it;
  BOOST_CHECK( it == fs::recursive_directory_iterator() );

  fs::remove( "xx/yya" );
#endif

  it = fs::recursive_directory_iterator( "xx/yy/zz" );
  BOOST_CHECK( it == fs::recursive_directory_iterator() );
  
  it = fs::recursive_directory_iterator( "xx" );
  BOOST_CHECK( it->path() == "xx/yy" );
  BOOST_CHECK( it.level() == 0 );
  ++it;
  BOOST_CHECK( it->path() == "xx/yy/zz" );
  BOOST_CHECK( it.level() == 1 );
  it++;
  BOOST_CHECK( it == fs::recursive_directory_iterator() );

  it = fs::recursive_directory_iterator( "xx" );
  BOOST_CHECK( it->path() == "xx/yy" );
  it.no_push();
  ++it;
  BOOST_CHECK( it == fs::recursive_directory_iterator() );

  it = fs::recursive_directory_iterator( "xx" );
  BOOST_CHECK( it->path() == "xx/yy" );
  ++it;
  it.pop();
  BOOST_CHECK( it == fs::recursive_directory_iterator() );



  // nothrow wrong. see imp.  Make sure failed basic_directory_iterator
  // ctor creates the end iterator. 




  return 0;
}