1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
16 // bool exists(file_status s) noexcept
17 // bool exists(path const& p);
18 // bool exists(path const& p, std::error_code& ec) noexcept;
21 #include <type_traits>
24 #include "assert_macros.h"
25 #include "test_macros.h"
26 #include "filesystem_test_helper.h"
27 namespace fs
= std::filesystem
;
30 static void signature_test()
32 file_status s
; ((void)s
);
33 const path p
; ((void)p
);
34 std::error_code ec
; ((void)ec
);
35 ASSERT_NOEXCEPT(exists(s
));
36 ASSERT_NOEXCEPT(exists(p
, ec
));
37 ASSERT_NOT_NOEXCEPT(exists(p
));
40 static void exists_status_test()
46 const TestCase testCases
[] = {
47 {file_type::none
, false},
48 {file_type::not_found
, false},
49 {file_type::regular
, true},
50 {file_type::directory
, true},
51 {file_type::symlink
, true},
52 {file_type::block
, true},
53 {file_type::character
, true},
54 {file_type::fifo
, true},
55 {file_type::socket
, true},
56 {file_type::unknown
, true}
58 for (auto& TC
: testCases
) {
59 file_status
s(TC
.type
);
60 assert(exists(s
) == TC
.expect
);
64 static void test_exist_not_found()
66 static_test_env static_env
;
67 const path p
= static_env
.DNE
;
68 assert(exists(p
) == false);
70 assert(exists(static_env
.Dir
) == true);
71 assert(exists(static_env
.Dir
/ "dne") == false);
72 // Whether <dir>/dne/.. is considered to exist or not is not necessarily
73 // something we need to define, but the platform specific behaviour
74 // does affect a few other tests, so clarify the root cause here.
76 assert(exists(static_env
.Dir
/ "dne" / "..") == true);
78 assert(exists(static_env
.Dir
/ "dne" / "..") == false);
81 std::error_code ec
= GetTestEC();
82 assert(exists(p
, ec
) == false);
86 static void test_exists_fails()
89 // Windows doesn't support setting perms::none to trigger failures
90 // reading directories; test using a special inaccessible directory
92 const path p
= GetWindowsInaccessibleDir();
97 const path dir
= env
.create_dir("dir");
98 const path p
= env
.create_file("dir/file", 42);
99 permissions(dir
, perms::none
);
103 assert(exists(p
, ec
) == false);
106 TEST_THROWS_TYPE(filesystem_error
, exists(p
));
110 // Checking for the existence of an invalid long path name doesn't
111 // trigger errors on windows.
112 static void test_name_too_long() {
113 std::string
long_name(2500, 'a');
114 const path
file(long_name
);
117 assert(exists(file
, ec
) == false);
122 int main(int, char**) {
124 exists_status_test();
125 test_exist_not_found();
128 test_name_too_long();