1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
15 // path weakly_canonical(const path& p);
16 // path weakly_canonical(const path& p, error_code& ec);
18 #include "filesystem_include.h"
21 #include "assert_macros.h"
22 #include "concat_macros.h"
23 #include "test_macros.h"
24 #include "test_iterators.h"
25 #include "count_new.h"
26 #include "filesystem_test_helper.h"
27 #include "../../class.path/path_helper.h"
29 int main(int, char**) {
30 static_test_env static_env
;
32 fs::path root
= fs::current_path().root_path();
38 {"", fs::current_path()},
39 {".", fs::current_path()},
41 {"/foo", root
/ "foo"},
44 {"a/b", fs::current_path() / "a/b"},
45 {"a", fs::current_path() / "a"},
46 {"a/b/", fs::current_path() / "a/b/"},
47 {static_env
.File
, static_env
.File
},
48 {static_env
.Dir
, static_env
.Dir
},
49 {static_env
.SymlinkToDir
, static_env
.Dir
},
50 {static_env
.SymlinkToDir
/ "dir2/.", static_env
.Dir
/ "dir2"},
51 // Note: If the trailing separator occurs in a part of the path that exists,
52 // it is omitted. Otherwise it is added to the end of the result.
53 // MS STL and libstdc++ behave similarly.
54 {static_env
.SymlinkToDir
/ "dir2/./", static_env
.Dir
/ "dir2"},
55 {static_env
.SymlinkToDir
/ "dir2/DNE/./", static_env
.Dir
/ "dir2/DNE/"},
56 {static_env
.SymlinkToDir
/ "dir2", static_env
.Dir2
},
58 // On Windows, this path is considered to exist (even though it
59 // passes through a nonexistent directory), and thus is returned
60 // without a trailing slash, see the note above.
61 {static_env
.SymlinkToDir
/ "dir2/../dir2/DNE/..", static_env
.Dir2
},
63 {static_env
.SymlinkToDir
/ "dir2/../dir2/DNE/..", static_env
.Dir2
/ ""},
65 {static_env
.SymlinkToDir
/ "dir2/dir3/../DNE/DNE2", static_env
.Dir2
/ "DNE/DNE2"},
66 {static_env
.Dir
/ "../dir1", static_env
.Dir
},
67 {static_env
.Dir
/ "./.", static_env
.Dir
},
68 {static_env
.Dir
/ "DNE/../foo", static_env
.Dir
/ "foo"}
71 for (auto& TC
: TestCases
) {
72 fs::path p
= TC
.input
;
73 fs::path expect
= TC
.expect
;
74 expect
.make_preferred();
75 const fs::path output
= fs::weakly_canonical(p
);
77 TEST_REQUIRE(PathEq(output
, expect
),
78 TEST_WRITE_CONCATENATED(
79 "Input: ", TC
.input
.string(), "\nExpected: ", expect
.string(), "\nOutput: ", output
.string()));