1 // Copyright (C) 2016-2021 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8 
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.
13 
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/>.
17 
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20 
21 #include <filesystem>
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
24 
25 namespace fs = std::filesystem;
26 
27 void
test01()28 test01()
29 {
30   std::error_code ec;
31   fs::recursive_directory_iterator dir;
32   dir.pop(ec);  // This is undefined, but our implementation
33   VERIFY( ec ); // checks and returns an error.
34   VERIFY( dir == end(dir) );
35 
36   std::error_code ec2;
37   try
38   {
39     dir.pop();
40   }
41   catch (const fs::filesystem_error& ex)
42   {
43     ec2 = ex.code();
44   }
45   VERIFY( ec2 == ec );
46 }
47 
48 void
test02()49 test02()
50 {
51   const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
52   std::error_code ec;
53   const auto p = __gnu_test::nonexistent_path();
54   create_directories(p / "d1/d2/d3");
55   for (int i = 0; i < 3; ++i)
56   {
57     fs::recursive_directory_iterator dir(p);
58     VERIFY( dir != end(dir) );
59     std::advance(dir, i);
60     VERIFY( dir != end(dir) );
61     VERIFY( dir.depth() == i );
62     ec = bad_ec;
63     dir.pop(ec);
64     VERIFY( !ec );
65     VERIFY( dir == end(dir) );
66 
67     dir = fs::recursive_directory_iterator(p);
68     std::advance(dir, i);
69     VERIFY( dir != end(dir) );
70     VERIFY( dir.depth() == i );
71     dir.pop();
72     VERIFY( dir == end(dir) );
73   }
74   remove_all(p, ec);
75 }
76 
77 void
test03()78 test03()
79 {
80   const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
81   std::error_code ec;
82   const auto p = __gnu_test::nonexistent_path();
83   create_directories(p / "d1/d2/d3");
84   create_directories(p / "d1/d2/e3");
85   create_directories(p / "d1/e2/d3");
86   for (int i = 0; i < 3; ++i)
87   {
88     fs::recursive_directory_iterator dir(p);
89     std::advance(dir, i);
90     VERIFY( dir != end(dir) );
91     int expected_depth = i;
92     VERIFY( dir.depth() == expected_depth );
93     ec = bad_ec;
94     dir.pop(ec);
95     VERIFY( !ec );
96     if (dir != end(dir))
97       VERIFY( dir.depth() == (expected_depth - 1) );
98 
99     dir = fs::recursive_directory_iterator(p);
100     std::advance(dir, i);
101     VERIFY( dir != end(dir) );
102     VERIFY( dir.depth() == i );
103     dir.pop();
104     if (dir != end(dir))
105       VERIFY( dir.depth() == (i -1) );
106   }
107   remove_all(p, ec);
108 }
109 
110 int
main()111 main()
112 {
113   test01();
114   test02();
115   test03();
116 }
117