Boost logo

Boost-Commit :

From: bdawes_at_[hidden]
Date: 2007-08-26 16:54:57


Author: bemandawes
Date: 2007-08-26 16:54:57 EDT (Sun, 26 Aug 2007)
New Revision: 38978
URL: http://svn.boost.org/trac/boost/changeset/38978

Log:
Add recursive_directory_iterator test cases to convenience_test.cpp, fix !#1061 plus a pop() bug detected by new tests
Text files modified:
   branches/libs/filesystem/boost/filesystem/convenience.hpp | 26 ++++++++--
   branches/libs/filesystem/boost/filesystem/operations.hpp | 2
   branches/libs/filesystem/filesystem/test/convenience_test.cpp | 96 +++++++++++++++++++++++++++++++++++++--
   3 files changed, 112 insertions(+), 12 deletions(-)

Modified: branches/libs/filesystem/boost/filesystem/convenience.hpp
==============================================================================
--- branches/libs/filesystem/boost/filesystem/convenience.hpp (original)
+++ branches/libs/filesystem/boost/filesystem/convenience.hpp 2007-08-26 16:54:57 EDT (Sun, 26 Aug 2007)
@@ -189,6 +189,7 @@
 
       bool equal( const basic_recursive_directory_iterator & rhs ) const
         { return m_imp == rhs.m_imp; }
+
     };
 
     typedef basic_recursive_directory_iterator<path> recursive_directory_iterator;
@@ -205,6 +206,8 @@
       : m_imp( new detail::recur_dir_itr_imp<Path> )
     {
       m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path ) );
+ if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
+ { m_imp.reset (); }
     }
 
     template<class Path>
@@ -213,8 +216,10 @@
         system::error_code & ec )
       : m_imp( new detail::recur_dir_itr_imp<Path> )
     {
- m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path, std::nothrow ) );
       m_imp->m_no_throw = true;
+ m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path, ec ) );
+ if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
+ { m_imp.reset (); }
     }
 
     // increment
@@ -225,15 +230,15 @@
       
       static const basic_directory_iterator<Path> end_itr;
 
- if ( m_imp->m_no_push ) m_imp->m_no_push = false;
+ if ( m_imp->m_no_push )
+ { m_imp->m_no_push = false; }
       else if ( is_directory( m_imp->m_stack.top()->status() ) )
       {
         system::error_code ec;
         m_imp->m_stack.push(
           m_imp->m_no_throw
             ? basic_directory_iterator<Path>( *m_imp->m_stack.top(), ec )
- : basic_directory_iterator<Path>( *m_imp->m_stack.top() )
- );
+ : basic_directory_iterator<Path>( *m_imp->m_stack.top() ) );
         if ( m_imp->m_stack.top() != end_itr )
         {
           ++m_imp->m_level;
@@ -259,8 +264,17 @@
       BOOST_ASSERT( m_imp.get() && "pop on end iterator" );
       BOOST_ASSERT( m_imp->m_level > 0 && "pop with level < 1" );
 
- m_imp->m_stack.pop();
- --m_imp->m_level;
+ static const basic_directory_iterator<Path> end_itr;
+
+ do
+ {
+ m_imp->m_stack.pop();
+ --m_imp->m_level;
+ }
+ while ( !m_imp->m_stack.empty()
+ && ++m_imp->m_stack.top() == end_itr );
+
+ if ( m_imp->m_stack.empty() ) m_imp.reset(); // done, so make end iterator
     }
 
   } // namespace filesystem

Modified: branches/libs/filesystem/boost/filesystem/operations.hpp
==============================================================================
--- branches/libs/filesystem/boost/filesystem/operations.hpp (original)
+++ branches/libs/filesystem/boost/filesystem/operations.hpp 2007-08-26 16:54:57 EDT (Sun, 26 Aug 2007)
@@ -1004,7 +1004,7 @@
        m_symlink_status = symlink_st;
      }
 
- const Path & path() const { return m_path; }
+ const Path & path() const { return m_path; }
       file_status status() const;
       file_status status( system::error_code & ec ) const;
       file_status symlink_status() const;

Modified: branches/libs/filesystem/filesystem/test/convenience_test.cpp
==============================================================================
--- branches/libs/filesystem/filesystem/test/convenience_test.cpp (original)
+++ branches/libs/filesystem/filesystem/test/convenience_test.cpp 2007-08-26 16:54:57 EDT (Sun, 26 Aug 2007)
@@ -18,6 +18,7 @@
 #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>
@@ -45,7 +46,15 @@
     }
     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
@@ -63,13 +72,13 @@
   BOOST_CHECK( fs::exists( "xx" ) );
   BOOST_CHECK( fs::is_directory( "xx" ) );
 
- BOOST_CHECK( fs::create_directories( "xx/ww/zz" ) );
+ BOOST_CHECK( fs::create_directories( "xx/yy/zz" ) );
   BOOST_CHECK( fs::exists( "xx" ) );
- BOOST_CHECK( fs::exists( "xx/ww" ) );
- BOOST_CHECK( fs::exists( "xx/ww/zz" ) );
+ 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/ww" ) );
- BOOST_CHECK( fs::is_directory( "xx/ww/zz" ) );
+ BOOST_CHECK( fs::is_directory( "xx/yy" ) );
+ BOOST_CHECK( fs::is_directory( "xx/yy/zz" ) );
 
   path is_a_file( "xx/uu" );
   {
@@ -109,5 +118,82 @@
   // 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;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk