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

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