libstdc++/70609 fix filesystem::copy()
[gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / copy.cc
1 // { dg-options "-std=gnu++11 -lstdc++fs" }
2 // { dg-require-filesystem-ts "" }
3
4 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // 15.3 Copy [fs.op.copy]
22
23 #include <experimental/filesystem>
24 #include <fstream>
25 #include <testsuite_fs.h>
26 #include <testsuite_hooks.h>
27
28 namespace fs = std::experimental::filesystem;
29
30 // Test error conditions.
31 void
32 test01()
33 {
34 bool test __attribute__((unused)) = false;
35
36 auto p = __gnu_test::nonexistent_path();
37 std::error_code ec;
38
39 VERIFY( !fs::exists(p) );
40 fs::copy(p, ".", fs::copy_options::none, ec);
41 VERIFY( ec );
42
43 ec.clear();
44 fs::copy(".", ".", fs::copy_options::none, ec);
45 VERIFY( ec );
46
47 std::ofstream{p.native()};
48 VERIFY( fs::is_directory(".") );
49 VERIFY( fs::is_regular_file(p) );
50 ec.clear();
51 fs::copy(".", p, fs::copy_options::none, ec);
52 VERIFY( ec );
53
54 remove(p, ec);
55 }
56
57 // Test is_symlink(f) case.
58 void
59 test02()
60 {
61 bool test __attribute__((unused)) = false;
62
63 auto from = __gnu_test::nonexistent_path();
64 auto to = __gnu_test::nonexistent_path();
65 std::error_code ec;
66
67 fs::create_symlink(".", from, ec);
68 VERIFY( !ec );
69 VERIFY( fs::exists(from) );
70
71 fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
72 VERIFY( !ec );
73 VERIFY( !fs::exists(to) );
74
75 fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
76 VERIFY( !ec );
77 VERIFY( !fs::exists(to) );
78
79 fs::copy(from, to,
80 fs::copy_options::skip_symlinks|fs::copy_options::copy_symlinks,
81 ec);
82 VERIFY( !ec );
83 VERIFY( !fs::exists(to) );
84
85 fs::copy(from, to, fs::copy_options::copy_symlinks, ec);
86 VERIFY( !ec );
87 VERIFY( fs::exists(to) );
88
89 fs::copy(from, to, fs::copy_options::copy_symlinks, ec);
90 VERIFY( ec );
91
92 remove(from, ec);
93 remove(to, ec);
94 }
95
96 // Test is_regular_file(f) case.
97 void
98 test03()
99 {
100 bool test __attribute__((unused)) = false;
101
102 auto from = __gnu_test::nonexistent_path();
103 auto to = __gnu_test::nonexistent_path();
104
105 // test empty file
106 std::ofstream{from.native()};
107 VERIFY( fs::exists(from) );
108 VERIFY( fs::file_size(from) == 0 );
109 fs::copy(from, to);
110 VERIFY( fs::exists(to) );
111 VERIFY( fs::file_size(to) == 0 );
112
113 remove(to);
114 VERIFY( !fs::exists(to) );
115 std::ofstream{from.native()} << "Hello, filesystem!";
116 VERIFY( fs::file_size(from) != 0 );
117 fs::copy(from, to);
118 VERIFY( fs::exists(to) );
119 VERIFY( fs::file_size(to) == fs::file_size(from) );
120 }
121
122 // Test is_directory(f) case.
123 void
124 test04()
125 {
126 bool test __attribute__((unused)) = false;
127
128 auto from = __gnu_test::nonexistent_path();
129 auto to = __gnu_test::nonexistent_path();
130 std::error_code ec;
131
132 }
133
134 // Test no-op cases.
135 void
136 test05()
137 {
138 bool test __attribute__((unused)) = false;
139
140 auto to = __gnu_test::nonexistent_path();
141 std::error_code ec;
142
143 fs::copy("/", to, fs::copy_options::create_symlinks, ec);
144 VERIFY( !ec );
145 }
146
147 int
148 main()
149 {
150 test01();
151 test02();
152 test03();
153 test04();
154 test05();
155 }