]>
gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/experimental/filesystem/iterators/directory_iterator.cc
1 // Copyright (C) 2015-2021 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
22 #include <experimental/filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
26 namespace fs
= std::experimental::filesystem
;
33 // Test non-existent path.
34 const auto p
= __gnu_test::nonexistent_path();
35 fs::directory_iterator
iter(p
, ec
);
37 VERIFY( iter
== end(iter
) );
39 // Test empty directory.
40 create_directory(p
, fs::current_path(), ec
);
42 iter
= fs::directory_iterator(p
, ec
);
44 VERIFY( iter
== end(iter
) );
46 // Test non-empty directory.
47 create_directory(p
/ "x", ec
);
49 iter
= fs::directory_iterator(p
, ec
);
51 VERIFY( iter
!= fs::directory_iterator() );
52 VERIFY( iter
->path() == p
/"x" );
54 VERIFY( iter
== end(iter
) );
56 if (__gnu_test::permissions_are_testable())
58 // Test inaccessible directory.
59 permissions(p
, fs::perms::none
, ec
);
61 iter
= fs::directory_iterator(p
, ec
);
63 VERIFY( iter
== end(iter
) );
65 // Test inaccessible directory, skipping permission denied.
66 const auto opts
= fs::directory_options::skip_permission_denied
;
67 iter
= fs::directory_iterator(p
, opts
, ec
);
69 VERIFY( iter
== end(iter
) );
71 permissions(p
, fs::perms::owner_all
, ec
);
81 const auto p
= __gnu_test::nonexistent_path();
82 create_directory(p
, fs::current_path(), ec
);
83 create_directory(p
/ "x", ec
);
86 // Test post-increment (libstdc++/71005)
87 auto iter
= fs::directory_iterator(p
, ec
);
89 VERIFY( iter
!= end(iter
) );
90 const auto entry1
= *iter
;
91 const auto entry2
= *iter
++;
92 VERIFY( entry1
== entry2
);
93 VERIFY( entry1
.path() == p
/"x" );
94 VERIFY( iter
== end(iter
) );
103 const auto p
= __gnu_test::nonexistent_path();
104 create_directories(p
/ "longer_than_small_string_buffer", ec
);
107 // Test for no reallocation on each dereference (this is a GNU extension)
108 auto iter
= fs::directory_iterator(p
, ec
);
109 const auto* s1
= iter
->path().c_str();
110 const auto* s2
= iter
->path().c_str();
119 const fs::directory_iterator it
;
120 VERIFY( it
== fs::directory_iterator() );
126 auto p
= __gnu_test::nonexistent_path();
128 create_directory(p
/ "x");
129 fs::directory_iterator
it(p
), endit
;
130 VERIFY( begin(it
) == it
);
131 static_assert( noexcept(begin(it
)), "begin is noexcept" );
132 VERIFY( end(it
) == endit
);
133 static_assert( noexcept(end(it
)), "end is noexcept" );
This page took 0.044053 seconds and 5 git commands to generate.