Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.relative/relative.pass.cpp

raw
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 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12
13 // <filesystem>
14
15 // path proximate(const path& p, error_code &ec)
16 // path proximate(const path& p, const path& base = current_path())
17 // path proximate(const path& p, const path& base, error_code& ec);
18
19 #include <filesystem>
20 #include <string>
21 #include <type_traits>
22 #include <cassert>
23
24 #include "test_macros.h"
25 #include "test_iterators.h"
26 #include "count_new.h"
27 #include "filesystem_test_helper.h"
28 namespace fs = std::filesystem;
29
30 static void test_signature_0() {
31 fs::path p("");
32 const fs::path output = fs::weakly_canonical(p);
33 assert(output == fs::path::string_type(fs::current_path()));
34 }
35
36 static void test_signature_1() {
37 fs::path p(".");
38 const fs::path output = fs::weakly_canonical(p);
39 assert(output == fs::path::string_type(fs::current_path()));
40 }
41
42 static void test_signature_2() {
43 static_test_env static_env;
44 fs::path p(static_env.File);
45 const fs::path output = fs::weakly_canonical(p);
46 assert(output == fs::path::string_type(static_env.File));
47 }
48
49 static void test_signature_3() {
50 static_test_env static_env;
51 fs::path p(static_env.Dir);
52 const fs::path output = fs::weakly_canonical(p);
53 assert(output == fs::path::string_type(static_env.Dir));
54 }
55
56 static void test_signature_4() {
57 static_test_env static_env;
58 fs::path p(static_env.SymlinkToDir);
59 const fs::path output = fs::weakly_canonical(p);
60 assert(output == fs::path::string_type(static_env.Dir));
61 }
62
63 static void test_signature_5() {
64 static_test_env static_env;
65 fs::path p(static_env.SymlinkToDir / "dir2/.");
66 const fs::path output = fs::weakly_canonical(p);
67 assert(output == fs::path::string_type(static_env.Dir / "dir2"));
68 }
69
70 static void test_signature_6() {
71 static_test_env static_env;
72 // FIXME? If the trailing separator occurs in a part of the path that exists,
73 // it is omitted. Otherwise it is added to the end of the result.
74 fs::path p(static_env.SymlinkToDir / "dir2/./");
75 const fs::path output = fs::weakly_canonical(p);
76 assert(output == fs::path::string_type(static_env.Dir / "dir2"));
77 }
78
79 static void test_signature_7() {
80 static_test_env static_env;
81 fs::path p(static_env.SymlinkToDir / "dir2/DNE/./");
82 const fs::path output = fs::weakly_canonical(p);
83 assert(output == fs::path::string_type(static_env.Dir / "dir2/DNE/"));
84 }
85
86 static void test_signature_8() {
87 static_test_env static_env;
88 fs::path p(static_env.SymlinkToDir / "dir2");
89 const fs::path output = fs::weakly_canonical(p);
90 assert(output == fs::path::string_type(static_env.Dir2));
91 }
92
93 static void test_signature_9() {
94 static_test_env static_env;
95 fs::path p(static_env.SymlinkToDir / "dir2/../dir2/DNE/..");
96 const fs::path output = fs::weakly_canonical(p);
97 // weakly_canonical has a quirk - if the path is considered to exist,
98 // it's returned without a trailing slash, otherwise it's returned with
99 // one (see a note in fs.op.weakly_canonical/weakly_canonical.pass.cpp).
100 // On Windows, a path like existent/nonexistentsubdir/.. is considered
101 // to exist, on posix it's considered to not exist. Therefore, the
102 // result here differs in the trailing slash.
103 #ifdef _WIN32
104 assert(output == fs::path::string_type(static_env.Dir2));
105 #else
106 assert(output == fs::path::string_type(static_env.Dir2 / ""));
107 #endif
108 }
109
110 static void test_signature_10() {
111 static_test_env static_env;
112 fs::path p(static_env.SymlinkToDir / "dir2/dir3/../DNE/DNE2");
113 const fs::path output = fs::weakly_canonical(p);
114 assert(output == fs::path::string_type(static_env.Dir2 / "DNE/DNE2"));
115 }
116
117 static void test_signature_11() {
118 static_test_env static_env;
119 fs::path p(static_env.Dir / "../dir1");
120 const fs::path output = fs::weakly_canonical(p);
121 assert(output == fs::path::string_type(static_env.Dir));
122 }
123
124 static void test_signature_12() {
125 static_test_env static_env;
126 fs::path p(static_env.Dir / "./.");
127 const fs::path output = fs::weakly_canonical(p);
128 assert(output == fs::path::string_type(static_env.Dir));
129 }
130
131 static void test_signature_13() {
132 static_test_env static_env;
133 fs::path p(static_env.Dir / "DNE/../foo");
134 const fs::path output = fs::weakly_canonical(p);
135 assert(output == fs::path::string_type(static_env.Dir / "foo"));
136 }
137
138 int main(int, char**) {
139 test_signature_0();
140 test_signature_1();
141 test_signature_2();
142 test_signature_3();
143 test_signature_4();
144 test_signature_5();
145 test_signature_6();
146 test_signature_7();
147 test_signature_8();
148 test_signature_9();
149 test_signature_10();
150 test_signature_11();
151 test_signature_12();
152 test_signature_13();
153
154 return 0;
155 }