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