[RISCV] Fix the cost of `llvm.vector.reduce.and` (#119160)
[llvm-project.git] / libcxx / test / std / input.output / filesystems / fs.op.funcs / fs.op.copy / copy.pass.cpp
blob3f3effbf267520447f4a428e619ff36122dfafba
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 // Starting in Android N (API 24), SELinux policy prevents the shell user from
15 // creating a FIFO file.
16 // XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22|23}}
18 // <filesystem>
20 // void copy(const path& from, const path& to);
21 // void copy(const path& from, const path& to, error_code& ec);
22 // void copy(const path& from, const path& to, copy_options options);
23 // void copy(const path& from, const path& to, copy_options options,
24 // error_code& ec);
26 #include <filesystem>
27 #include <type_traits>
28 #include <cstddef>
29 #include <cassert>
31 #include "test_macros.h"
32 #include "filesystem_test_helper.h"
33 namespace fs = std::filesystem;
34 using namespace fs;
36 using CO = fs::copy_options;
38 static void signature_test()
40 const path p; ((void)p);
41 std::error_code ec; ((void)ec);
42 const copy_options opts{}; ((void)opts);
43 ASSERT_NOT_NOEXCEPT(fs::copy(p, p));
44 ASSERT_NOT_NOEXCEPT(fs::copy(p, p, ec));
45 ASSERT_NOT_NOEXCEPT(copy(p, p, opts));
46 ASSERT_NOT_NOEXCEPT(copy(p, p, opts, ec));
49 // There are 4 cases is the proposal for absolute path.
50 // Each scope tests one of the cases.
51 static void test_error_reporting()
53 auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
55 #ifndef TEST_HAS_NO_EXCEPTIONS
56 try {
57 fs::copy(f, t);
58 return false;
59 } catch (filesystem_error const& err) {
60 return err.path1() == f
61 && err.path2() == t
62 && err.code() == ec;
64 #else
65 ((void)f); ((void)t); ((void)ec);
66 return true;
67 #endif
70 static_test_env static_env;
71 scoped_test_env env;
72 const path file = env.create_file("file1", 42);
73 const path dir = env.create_dir("dir");
74 #ifndef _WIN32
75 const path fifo = env.create_fifo("fifo");
76 assert(is_other(fifo));
77 #endif
79 const auto test_ec = GetTestEC();
81 // !exists(f)
83 std::error_code ec = test_ec;
84 const path f = static_env.DNE;
85 const path t = env.test_root;
86 fs::copy(f, t, ec);
87 assert(ec);
88 assert(ec != test_ec);
89 assert(checkThrow(f, t, ec));
91 { // equivalent(f, t) == true
92 std::error_code ec = test_ec;
93 fs::copy(file, file, ec);
94 assert(ec);
95 assert(ec != test_ec);
96 assert(checkThrow(file, file, ec));
98 { // is_directory(from) && is_file(to)
99 std::error_code ec = test_ec;
100 fs::copy(dir, file, ec);
101 assert(ec);
102 assert(ec != test_ec);
103 assert(checkThrow(dir, file, ec));
105 #ifndef _WIN32
106 { // is_other(from)
107 std::error_code ec = test_ec;
108 fs::copy(fifo, dir, ec);
109 assert(ec);
110 assert(ec != test_ec);
111 assert(checkThrow(fifo, dir, ec));
113 { // is_other(to)
114 std::error_code ec = test_ec;
115 fs::copy(file, fifo, ec);
116 assert(ec);
117 assert(ec != test_ec);
118 assert(checkThrow(file, fifo, ec));
120 #endif
123 static void from_is_symlink()
125 scoped_test_env env;
126 const path file = env.create_file("file", 42);
127 const path symlink = env.create_symlink(file, "sym");
128 const path dne = env.make_env_path("dne");
130 { // skip symlinks
131 std::error_code ec = GetTestEC();
132 fs::copy(symlink, dne, copy_options::skip_symlinks, ec);
133 assert(!ec);
134 assert(!exists(dne));
137 const path dest = env.make_env_path("dest");
138 std::error_code ec = GetTestEC();
139 fs::copy(symlink, dest, copy_options::copy_symlinks, ec);
140 assert(!ec);
141 assert(exists(dest));
142 assert(is_symlink(dest));
144 { // copy symlink but target exists
145 std::error_code ec = GetTestEC();
146 fs::copy(symlink, file, copy_options::copy_symlinks, ec);
147 assert(ec);
148 assert(ec != GetTestEC());
150 { // create symlinks but target exists
151 std::error_code ec = GetTestEC();
152 fs::copy(symlink, file, copy_options::create_symlinks, ec);
153 assert(ec);
154 assert(ec != GetTestEC());
158 static void from_is_regular_file()
160 scoped_test_env env;
161 const path file = env.create_file("file", 42);
162 const path dir = env.create_dir("dir");
163 { // skip copy because of directory
164 const path dest = env.make_env_path("dest1");
165 std::error_code ec = GetTestEC();
166 fs::copy(file, dest, CO::directories_only, ec);
167 assert(!ec);
168 assert(!exists(dest));
170 { // create symlink to file
171 const path dest = env.make_env_path("sym");
172 std::error_code ec = GetTestEC();
173 fs::copy(file, dest, CO::create_symlinks, ec);
174 assert(!ec);
175 assert(is_symlink(dest));
176 assert(equivalent(file, canonical(dest)));
178 { // create hard link to file
179 const path dest = env.make_env_path("hardlink");
180 assert(hard_link_count(file) == 1);
181 std::error_code ec = GetTestEC();
182 fs::copy(file, dest, CO::create_hard_links, ec);
183 assert(!ec);
184 assert(exists(dest));
185 assert(hard_link_count(file) == 2);
187 { // is_directory(t)
188 const path dest_dir = env.create_dir("dest_dir");
189 const path expect_dest = dest_dir / file.filename();
190 std::error_code ec = GetTestEC();
191 fs::copy(file, dest_dir, ec);
192 assert(!ec);
193 assert(is_regular_file(expect_dest));
195 { // otherwise copy_file(from, to, ...)
196 const path dest = env.make_env_path("file_copy");
197 std::error_code ec = GetTestEC();
198 fs::copy(file, dest, ec);
199 assert(!ec);
200 assert(is_regular_file(dest));
204 static void from_is_directory()
206 struct FileInfo {
207 path filename;
208 std::size_t size;
210 const FileInfo files[] = {
211 {"file1", 0},
212 {"file2", 42},
213 {"file3", 300}
215 scoped_test_env env;
216 const path dir = env.create_dir("dir");
217 const path nested_dir_name = "dir2";
218 const path nested_dir = env.create_dir("dir/dir2");
220 for (auto& FI : files) {
221 env.create_file(dir / FI.filename, FI.size);
222 env.create_file(nested_dir / FI.filename, FI.size);
224 { // test for non-existent directory
225 const path dest = env.make_env_path("dest_dir1");
226 std::error_code ec = GetTestEC();
227 fs::copy(dir, dest, ec);
228 assert(!ec);
229 assert(is_directory(dest));
230 for (auto& FI : files) {
231 path created = dest / FI.filename;
232 assert(is_regular_file(created));
233 assert(file_size(created) == FI.size);
235 assert(!is_directory(dest / nested_dir_name));
237 { // test for existing directory
238 const path dest = env.create_dir("dest_dir2");
239 std::error_code ec = GetTestEC();
240 fs::copy(dir, dest, ec);
241 assert(!ec);
242 assert(is_directory(dest));
243 for (auto& FI : files) {
244 path created = dest / FI.filename;
245 assert(is_regular_file(created));
246 assert(file_size(created) == FI.size);
248 assert(!is_directory(dest / nested_dir_name));
250 { // test recursive copy
251 const path dest = env.make_env_path("dest_dir3");
252 std::error_code ec = GetTestEC();
253 fs::copy(dir, dest, CO::recursive, ec);
254 assert(!ec);
255 assert(is_directory(dest));
256 const path nested_dest = dest / nested_dir_name;
257 assert(is_directory(nested_dest));
258 for (auto& FI : files) {
259 path created = dest / FI.filename;
260 path nested_created = nested_dest / FI.filename;
261 assert(is_regular_file(created));
262 assert(file_size(created) == FI.size);
263 assert(is_regular_file(nested_created));
264 assert(file_size(nested_created) == FI.size);
269 static void test_copy_symlinks_to_symlink_dir()
271 scoped_test_env env;
272 const path file1 = env.create_file("file1", 42);
273 const path file2 = env.create_file("file2", 101);
274 const path file2_sym = env.create_symlink(file2, "file2_sym");
275 const path dir = env.create_dir("dir");
276 const path dir_sym = env.create_directory_symlink(dir, "dir_sym");
278 std::error_code ec = GetTestEC();
279 fs::copy(file1, dir_sym, copy_options::copy_symlinks, ec);
280 assert(!ec);
281 const path dest = env.make_env_path("dir/file1");
282 assert(exists(dest));
283 assert(!is_symlink(dest));
284 assert(file_size(dest) == 42);
289 static void test_dir_create_symlink()
291 scoped_test_env env;
292 const path dir = env.create_dir("dir1");
293 const path dest = env.make_env_path("dne");
295 std::error_code ec = GetTestEC();
296 fs::copy(dir, dest, copy_options::create_symlinks, ec);
297 assert(ErrorIs(ec, std::errc::is_a_directory));
298 assert(!exists(dest));
299 assert(!is_symlink(dest));
302 std::error_code ec = GetTestEC();
303 fs::copy(dir, dest, copy_options::create_symlinks|copy_options::recursive, ec);
304 assert(ErrorIs(ec, std::errc::is_a_directory));
305 assert(!exists(dest));
306 assert(!is_symlink(dest));
310 static void test_otherwise_no_effects_clause()
312 scoped_test_env env;
313 const path dir = env.create_dir("dir1");
314 { // skip copy because of directory
315 const path dest = env.make_env_path("dest1");
316 std::error_code ec;
317 fs::copy(dir, dest, CO::directories_only, ec);
318 assert(!ec);
319 assert(!exists(dest));
323 int main(int, char**) {
324 signature_test();
325 test_error_reporting();
326 from_is_symlink();
327 from_is_regular_file();
328 from_is_directory();
329 test_copy_symlinks_to_symlink_dir();
330 test_dir_create_symlink();
331 test_otherwise_no_effects_clause();
333 return 0;