lexically_relative_and_proximate.pass.cpp (2793B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03 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_include.hpp" 20 #include <type_traits> 21 #include <vector> 22 #include <iostream> 23 #include <cassert> 24 25 #include "test_macros.h" 26 #include "test_iterators.h" 27 #include "count_new.hpp" 28 #include "filesystem_test_helper.hpp" 29 30 31 int main() { 32 // clang-format off 33 struct { 34 std::string input; 35 std::string base; 36 std::string expect; 37 } TestCases[] = { 38 {"", "", "."}, 39 {"/", "a", ""}, 40 {"a", "/", ""}, 41 {"//net", "a", ""}, 42 {"a", "//net", ""}, 43 {"//net/", "//net", "."}, 44 {"//net", "//net/", "."}, 45 {"//base", "a", ""}, 46 {"a", "a", "."}, 47 {"a/b", "a/b", "."}, 48 {"a/b/c/", "a/b/c/", "."}, 49 {"//net", "//net", "."}, 50 {"//net/", "//net/", "."}, 51 {"//net/a/b", "//net/a/b", "."}, 52 {"/a/d", "/a/b/c", "../../d"}, 53 {"/a/b/c", "/a/d", "../b/c"}, 54 {"a/b/c", "a", "b/c"}, 55 {"a/b/c", "a/b/c/x/y", "../.."}, 56 {"a/b/c", "a/b/c", "."}, 57 {"a/b", "c/d", "../../a/b"} 58 }; 59 // clang-format on 60 int ID = 0; 61 bool Failed = false; 62 for (auto& TC : TestCases) { 63 ++ID; 64 const fs::path p(TC.input); 65 const fs::path output = p.lexically_relative(TC.base); 66 auto ReportErr = [&](const char* Testing, fs::path const& Output, 67 fs::path const& Expected) { 68 Failed = true; 69 std::cerr << "TEST CASE #" << ID << " FAILED: \n"; 70 std::cerr << " Testing: " << Testing << "\n"; 71 std::cerr << " Input: '" << TC.input << "'\n"; 72 std::cerr << " Base: '" << TC.base << "'\n"; 73 std::cerr << " Expected: '" << Expected << "'\n"; 74 std::cerr << " Output: '" << Output.native() << "'"; 75 std::cerr << std::endl; 76 }; 77 if (!PathEq(output, TC.expect)) 78 ReportErr("path::lexically_relative", output, TC.expect); 79 const fs::path proximate_output = p.lexically_proximate(TC.base); 80 // [path.gen] lexically_proximate 81 // Returns: If the value of lexically_relative(base) is not an empty path, 82 // return it.Otherwise return *this. 83 const fs::path proximate_expected = output.native().empty() ? p 84 : output; 85 if (!PathEq(proximate_expected, proximate_output)) 86 ReportErr("path::lexically_proximate", proximate_output, proximate_expected); 87 } 88 return Failed; 89 }