[mlir] Allow fallback from file line col range to loc (#124321)
[llvm-project.git] / libcxx / test / std / input.output / filesystems / fs.op.funcs / fs.op.exists / exists.pass.cpp
blob50181cb8d76c84cca8854392351fa592f1403554
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
14 // <filesystem>
16 // bool exists(file_status s) noexcept
17 // bool exists(path const& p);
18 // bool exists(path const& p, std::error_code& ec) noexcept;
20 #include <filesystem>
21 #include <type_traits>
22 #include <cassert>
24 #include "assert_macros.h"
25 #include "test_macros.h"
26 #include "filesystem_test_helper.h"
27 namespace fs = std::filesystem;
28 using namespace fs;
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()
42 struct TestCase {
43 file_type type;
44 bool expect;
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.
75 #ifdef _WIN32
76 assert(exists(static_env.Dir / "dne" / "..") == true);
77 #else
78 assert(exists(static_env.Dir / "dne" / "..") == false);
79 #endif
81 std::error_code ec = GetTestEC();
82 assert(exists(p, ec) == false);
83 assert(!ec);
86 static void test_exists_fails()
88 #ifdef _WIN32
89 // Windows doesn't support setting perms::none to trigger failures
90 // reading directories; test using a special inaccessible directory
91 // instead.
92 const path p = GetWindowsInaccessibleDir();
93 if (p.empty())
94 return;
95 #else
96 scoped_test_env env;
97 const path dir = env.create_dir("dir");
98 const path p = env.create_file("dir/file", 42);
99 permissions(dir, perms::none);
100 #endif
102 std::error_code ec;
103 assert(exists(p, ec) == false);
104 assert(ec);
106 TEST_THROWS_TYPE(filesystem_error, exists(p));
109 #ifndef _WIN32
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);
116 std::error_code ec;
117 assert(exists(file, ec) == false);
118 assert(ec);
120 #endif // _WIN32
122 int main(int, char**) {
123 signature_test();
124 exists_status_test();
125 test_exist_not_found();
126 test_exists_fails();
127 #ifndef _WIN32
128 test_name_too_long();
129 #endif
131 return 0;