]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/operations/exists.cc
libstdc++: Add nodiscard attribute to filesystem operations
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / exists.cc
CommitLineData
7adcbafe 1// Copyright (C) 2015-2022 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
641cb5a6
JW
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
25using std::filesystem::path;
26
27void
28test01()
29{
30 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
9534a5e6 31 const path root = __gnu_test::root_path();
641cb5a6 32
9534a5e6
JW
33 VERIFY( exists(root) );
34 VERIFY( exists(root/".") );
641cb5a6
JW
35 VERIFY( exists(path{"."}) );
36 VERIFY( exists(path{".."}) );
37 VERIFY( exists(std::filesystem::current_path()) );
38
39 std::error_code ec;
40 ec = bad_ec;
9534a5e6 41 VERIFY( exists(root, ec) );
641cb5a6
JW
42 VERIFY( !ec );
43 ec = bad_ec;
9534a5e6 44 VERIFY( exists(root/".", ec) );
641cb5a6
JW
45 VERIFY( !ec );
46 ec = bad_ec;
47 VERIFY( exists(path{"."}, ec) );
48 VERIFY( !ec );
49 ec = bad_ec;
50 VERIFY( exists(path{".."}, ec) );
51 VERIFY( !ec );
52 ec = bad_ec;
53 VERIFY( exists(std::filesystem::current_path(), ec) );
54 VERIFY( !ec );
55}
56
57void
58test02()
59{
60 path rel = __gnu_test::nonexistent_path();
61 VERIFY( !exists(rel) );
62
63 std::error_code ec = std::make_error_code(std::errc::invalid_argument);
64 VERIFY( !exists(rel, ec) );
65 VERIFY( !ec ); // DR 2725
66}
67
68void
69test03()
70{
71 path abs = absolute(__gnu_test::nonexistent_path());
72 VERIFY( !exists(abs) );
73
74 std::error_code ec = std::make_error_code(std::errc::invalid_argument);
75 VERIFY( !exists(abs, ec) );
76 VERIFY( !ec ); // DR 2725
77}
78
79void
80test04()
81{
29b2fd37
JW
82 if (!__gnu_test::permissions_are_testable())
83 return;
edfe833a 84
641cb5a6
JW
85 using std::filesystem::perms;
86 using std::filesystem::perm_options;
87 path p = __gnu_test::nonexistent_path();
88 create_directory(p);
89 permissions(p, perms::all, perm_options::remove);
90
91 auto unr = p / "unreachable";
92 std::error_code ec;
93 VERIFY( !exists(unr, ec) );
94 VERIFY( ec == std::errc::permission_denied );
95 ec.clear();
96 try
97 {
f7a14830 98 (void) exists(unr);
641cb5a6
JW
99 }
100 catch(const std::filesystem::filesystem_error& ex)
101 {
102 ec = ex.code();
103 VERIFY( ex.path1() == unr );
104 }
105 VERIFY( ec == std::errc::permission_denied );
106
107 permissions(p, perms::owner_all);
108 remove(p);
109}
110
111int
112main()
113{
114 test01();
115 test02();
116 test03();
117 test04();
118}