]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/operations/is_empty.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / is_empty.cc
1 // Copyright (C) 2016-2019 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 "-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
26 namespace fs = std::filesystem;
27
28 void
29 test01()
30 {
31 auto p = __gnu_test::nonexistent_path();
32 create_directory(p);
33 permissions(p, fs::perms::none);
34 std::error_code ec, ec2;
35
36 bool result = fs::is_empty(p, ec);
37 VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
38 VERIFY( !result );
39
40 try {
41 fs::is_empty(p);
42 } catch (const fs::filesystem_error& e) {
43 ec2 = e.code();
44 }
45 VERIFY( ec2 == ec );
46
47 result = fs::is_empty(p/"f", ec);
48 VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
49 VERIFY( !result );
50
51 try {
52 fs::is_empty(p/"f");
53 } catch (const fs::filesystem_error& e) {
54 ec2 = e.code();
55 }
56 VERIFY( ec2 == ec );
57
58 permissions(p, fs::perms::owner_all, ec);
59 remove_all(p, ec);
60 }
61
62 void
63 test02()
64 {
65 auto p = __gnu_test::nonexistent_path();
66 create_directory(p);
67 std::error_code ec, bad_ec = make_error_code(std::errc::invalid_argument);
68 bool empty;
69
70 ec = bad_ec;
71 empty = is_empty(p, ec);
72 VERIFY( !ec );
73 VERIFY( empty );
74 empty = is_empty(p);
75 VERIFY( empty );
76
77 __gnu_test::scoped_file f(p/"f");
78 ec = bad_ec;
79 empty = is_empty(f.path, ec);
80 VERIFY( !ec );
81 VERIFY( empty );
82 empty = is_empty(f.path);
83 VERIFY( empty );
84
85 std::ofstream{f.path} << "data";
86 ec = bad_ec;
87 empty = is_empty(p, ec);
88 VERIFY( !ec );
89 VERIFY( !empty );
90 empty = is_empty(p);
91 VERIFY( !empty );
92
93 ec = bad_ec;
94 empty = is_empty(p, ec);
95 VERIFY( !ec );
96 VERIFY( !empty );
97 empty = is_empty(p);
98 VERIFY( !empty );
99
100 f.path.clear();
101 remove_all(p, ec);
102 }
103
104 int
105 main()
106 {
107 test01();
108 test02();
109 }