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"
29 namespace fs = std::filesystem;
30 
test_signature_0()31 static void test_signature_0() {
32   fs::path p("");
33   const fs::path output = fs::weakly_canonical(p);
34   assert(output == fs::path::string_type(fs::current_path()));
35 }
36 
test_signature_1()37 static void test_signature_1() {
38   fs::path p(".");
39   const fs::path output = fs::weakly_canonical(p);
40   assert(output == fs::path::string_type(fs::current_path()));
41 }
42 
test_signature_2()43 static 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);
47   assert(output == fs::path::string_type(static_env.File));
48 }
49 
test_signature_3()50 static 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);
54   assert(output == fs::path::string_type(static_env.Dir));
55 }
56 
test_signature_4()57 static 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);
61   assert(output == fs::path::string_type(static_env.Dir));
62 }
63 
test_signature_5()64 static 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);
68   assert(output == fs::path::string_type(static_env.Dir / "dir2"));
69 }
70 
test_signature_6()71 static 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);
77   assert(output == fs::path::string_type(static_env.Dir / "dir2"));
78 }
79 
test_signature_7()80 static 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);
84   assert(output == fs::path::string_type(static_env.Dir / "dir2/DNE/"));
85 }
86 
test_signature_8()87 static 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);
91   assert(output == fs::path::string_type(static_env.Dir2));
92 }
93 
test_signature_9()94 static 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);
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 
test_signature_10()111 static 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);
115   assert(output == fs::path::string_type(static_env.Dir2 / "DNE/DNE2"));
116 }
117 
test_signature_11()118 static 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);
122   assert(output == fs::path::string_type(static_env.Dir));
123 }
124 
test_signature_12()125 static 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);
129   assert(output == fs::path::string_type(static_env.Dir));
130 }
131 
test_signature_13()132 static 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);
136   assert(output == fs::path::string_type(static_env.Dir / "foo"));
137 }
138 
main(int,char **)139 int 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