]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/iterators/pop.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / iterators / pop.cc
CommitLineData
a5544970 1// Copyright (C) 2016-2019 Free Software Foundation, Inc.
641cb5a6
JW
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 "-std=gnu++17 -lstdc++fs" }
19// { dg-do run { target c++17 } }
20// { dg-require-filesystem-ts "" }
21
22#include <filesystem>
23#include <testsuite_hooks.h>
24#include <testsuite_fs.h>
25
26namespace fs = std::filesystem;
27
28void
29test01()
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
49void
50test02()
51{
52 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
53 std::error_code ec;
54 const auto p = __gnu_test::nonexistent_path();
55 create_directories(p / "d1/d2/d3");
56 for (int i = 0; i < 3; ++i)
57 {
58 fs::recursive_directory_iterator dir(p);
59 VERIFY( dir != end(dir) );
60 std::advance(dir, i);
61 VERIFY( dir != end(dir) );
62 VERIFY( dir.depth() == i );
63 ec = bad_ec;
64 dir.pop(ec);
65 VERIFY( !ec );
66 VERIFY( dir == end(dir) );
67
68 dir = fs::recursive_directory_iterator(p);
69 std::advance(dir, i);
70 VERIFY( dir != end(dir) );
71 VERIFY( dir.depth() == i );
72 dir.pop();
73 VERIFY( dir == end(dir) );
74 }
75 remove_all(p, ec);
76}
77
78void
79test03()
80{
81 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
82 std::error_code ec;
83 const auto p = __gnu_test::nonexistent_path();
84 create_directories(p / "d1/d2/d3");
85 create_directories(p / "d1/d2/e3");
86 create_directories(p / "d1/e2/d3");
87 for (int i = 0; i < 3; ++i)
88 {
89 fs::recursive_directory_iterator dir(p);
90 std::advance(dir, i);
91 VERIFY( dir != end(dir) );
92 int expected_depth = i;
93 VERIFY( dir.depth() == expected_depth );
94 ec = bad_ec;
95 dir.pop(ec);
96 VERIFY( !ec );
97 if (dir != end(dir))
98 VERIFY( dir.depth() == (expected_depth - 1) );
99
100 dir = fs::recursive_directory_iterator(p);
101 std::advance(dir, i);
102 VERIFY( dir != end(dir) );
103 VERIFY( dir.depth() == i );
104 dir.pop();
105 if (dir != end(dir))
106 VERIFY( dir.depth() == (i -1) );
107 }
108 remove_all(p, ec);
109}
110
111int
112main()
113{
114 test01();
115 test02();
116 test03();
117}