Debian Codegraph @

libcxx/test/std/input.output/filesystems/class.path/path.member/path.gen/lexically_relative_and_proximate.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 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: availability-filesystem-missing
11
12 // <filesystem>
13
14 // class path
15
16 // path lexically_relative(const path& p) const;
17 // path lexically_proximate(const path& p) const;
18
19 #include <filesystem>
20 #include <string>
21
22 #include "../../path_helper.h"
23 #include "assert_macros.h"
24 #include "concat_macros.h"
25 #include "count_new.h"
26 #include "test_macros.h"
27 namespace fs = std::filesystem;
28
29 int main(int, char**) {
30 // clang-format off
31 struct {
32 std::string input;
33 std::string base;
34 std::string expect;
35 } TestCases[] = {
36 {"", "", "."},
37 {"/", "a", ""},
38 {"a", "/", ""},
39 {"//net", "a", ""},
40 {"a", "//net", ""},
41 #ifdef _WIN32
42 {"//net/", "//net", ""},
43 {"//net", "//net/", ""},
44 #else
45 {"//net/", "//net", "."},
46 {"//net", "//net/", "."},
47 #endif
48 {"//base", "a", ""},
49 {"a", "a", "."},
50 {"a/b", "a/b", "."},
51 {"a/b/c/", "a/b/c/", "."},
52 {"//net", "//net", "."},
53 {"//net/", "//net/", "."},
54 {"//net/a/b", "//net/a/b", "."},
55 {"/a/d", "/a/b/c", "../../d"},
56 {"/a/b/c", "/a/d", "../b/c"},
57 {"a/b/c", "a", "b/c"},
58 {"a/b/c", "a/b/c/x/y", "../.."},
59 {"a/b/c", "a/b/c", "."},
60 {"a/b", "c/d", "../../a/b"}
61 };
62 // clang-format on
63 for (auto& TC : TestCases) {
64 const fs::path p(TC.input);
65 const fs::path output = p.lexically_relative(TC.base);
66 fs::path expect(TC.expect);
67 expect.make_preferred();
68
69 // clang-format off
70 TEST_REQUIRE(
71 PathEq(output, expect),
72 TEST_WRITE_CONCATENATED(
73 "path::lexically_relative test case failed",
74 "\nInput: ", TC.input,
75 "\nBase: ", TC.base,
76 "\nExpected: ", expect,
77 "\nOutput: ", output));
78 // clang-format on
79
80 const fs::path proximate_output = p.lexically_proximate(TC.base);
81 // [path.gen] lexically_proximate
82 // Returns: If the value of lexically_relative(base) is not an empty path,
83 // return it. Otherwise return *this.
84 const fs::path proximate_expect = expect.empty() ? p : expect;
85
86 // clang-format off
87 TEST_REQUIRE(
88 PathEq(proximate_output, proximate_expect),
89 TEST_WRITE_CONCATENATED(
90 "path::lexically_proximate test case failed",
91 "\nInput: ", TC.input,
92 "\nBase: ", TC.base,
93 "\nExpected: ", proximate_expect,
94 "\nOutput: ", proximate_output));
95 // clang-format on
96 }
97
98 return 0;
99 }