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 // UNSUPPORTED: c++03
10 
11 // <filesystem>
12 
13 // path proximate(const path& p, error_code &ec)
14 // path proximate(const path& p, const path& base = current_path())
15 // path proximate(const path& p, const path& base, error_code& ec);
16 
17 #include "filesystem_include.h"
18 #include <string>
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 #include "count_new.h"
25 #include "rapid-cxx-test.h"
26 #include "filesystem_test_helper.h"
27 
28 
29 TEST_SUITE(filesystem_proximate_path_test_suite)
30 
TEST_CASE(test_signature_0)31 TEST_CASE(test_signature_0) {
32   fs::path p("");
33   const fs::path output = fs::weakly_canonical(p);
34   TEST_CHECK(output == fs::path::string_type(fs::current_path()));
35 }
36 
TEST_CASE(test_signature_1)37 TEST_CASE(test_signature_1) {
38   fs::path p(".");
39   const fs::path output = fs::weakly_canonical(p);
40   TEST_CHECK(output == fs::path::string_type(fs::current_path()));
41 }
42 
TEST_CASE(test_signature_2)43 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.File));
48 }
49 
TEST_CASE(test_signature_3)50 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir));
55 }
56 
TEST_CASE(test_signature_4)57 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir));
62 }
63 
TEST_CASE(test_signature_5)64 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir / "dir2"));
69 }
70 
TEST_CASE(test_signature_6)71 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir / "dir2"));
78 }
79 
TEST_CASE(test_signature_7)80 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir / "dir2/DNE/"));
85 }
86 
TEST_CASE(test_signature_8)87 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir2));
92 }
93 
TEST_CASE(test_signature_9)94 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir2));
106 #else
107   TEST_CHECK(output == fs::path::string_type(static_env.Dir2 / ""));
108 #endif
109 }
110 
TEST_CASE(test_signature_10)111 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir2 / "DNE/DNE2"));
116 }
117 
TEST_CASE(test_signature_11)118 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir));
123 }
124 
TEST_CASE(test_signature_12)125 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir));
130 }
131 
TEST_CASE(test_signature_13)132 TEST_CASE(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   TEST_CHECK(output == fs::path::string_type(static_env.Dir / "foo"));
137 }
138 
139 TEST_SUITE_END()
140