96 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// <filesystem>14 15// path weakly_canonical(const path& p);16// path weakly_canonical(const path& p, error_code& ec);17 18#include <filesystem>19#include <string>20 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"28namespace fs = std::filesystem;29 30int main(int, char**) {31 static_test_env static_env;32 33 fs::path root = fs::current_path().root_path();34 // clang-format off35 struct {36 fs::path input;37 fs::path expect;38 } TestCases[] = {39 {"", fs::current_path()},40 {".", fs::current_path()},41 {"/", root},42 {"/foo", root / "foo"},43 {"/.", root},44 {"/./", root},45 {"a/b", fs::current_path() / "a/b"},46 {"a", fs::current_path() / "a"},47 {"a/b/", fs::current_path() / "a/b/"},48 {static_env.File, static_env.File},49 {static_env.Dir, static_env.Dir},50 {static_env.SymlinkToDir, static_env.Dir},51 {static_env.SymlinkToDir / "dir2/.", static_env.Dir / "dir2"},52 // Note: If the trailing separator occurs in a part of the path that exists,53 // it is omitted. Otherwise it is added to the end of the result.54 // MS STL and libstdc++ behave similarly.55 {static_env.SymlinkToDir / "dir2/./", static_env.Dir / "dir2"},56 {static_env.SymlinkToDir / "dir2/DNE/./", static_env.Dir / "dir2/DNE/"},57 {static_env.SymlinkToDir / "dir2", static_env.Dir2},58#ifdef _WIN3259 // On Windows, this path is considered to exist (even though it60 // passes through a nonexistent directory), and thus is returned61 // without a trailing slash, see the note above.62 {static_env.SymlinkToDir / "dir2/../dir2/DNE/..", static_env.Dir2},63#else64 {static_env.SymlinkToDir / "dir2/../dir2/DNE/..", static_env.Dir2 / ""},65#endif66 {static_env.SymlinkToDir / "dir2/dir3/../DNE/DNE2", static_env.Dir2 / "DNE/DNE2"},67 {static_env.Dir / "../dir1", static_env.Dir},68 {static_env.Dir / "./.", static_env.Dir},69 {static_env.Dir / "DNE/../foo", static_env.Dir / "foo"}70 };71 // clang-format on72 for (auto& TC : TestCases) {73 fs::path p = TC.input;74 fs::path expect = TC.expect;75 expect.make_preferred();76 77 {78 const fs::path output = fs::weakly_canonical(p);79 TEST_REQUIRE(PathEq(output, expect),80 TEST_WRITE_CONCATENATED(81 "Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output.string()));82 }83 84 // Test the error_code variant85 {86 std::error_code ec;87 const fs::path output_c = fs::weakly_canonical(p, ec);88 89 TEST_REQUIRE(PathEq(output_c, expect),90 TEST_WRITE_CONCATENATED(91 "Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output_c.string()));92 }93 }94 return 0;95}96