1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <filesystem>
// class path
// path lexically_normal() const;
#include "filesystem_include.h"
#include <cstdio>
#include <string>
#include "test_macros.h"
#include "count_new.h"
#include "filesystem_test_helper.h"
int main(int, char**) {
// clang-format off
struct {
std::string input;
std::string expect;
} TestCases[] = {
{"", ""},
{"/a/b/c", "/a/b/c"},
{"/a/b//c", "/a/b/c"},
{"foo/./bar/..", "foo/"},
{"foo/.///bar/../", "foo/"},
{"/a/b/", "/a/b/"},
{"a/b", "a/b"},
{"a/b/.", "a/b/"},
{"a/b/./", "a/b/"},
{"a/..", "."},
{".", "."},
{"./", "."},
{"./.", "."},
{"./..", ".."},
{"..", ".."},
{"../..", "../.."},
{"/../", "/"},
{"/../..", "/"},
{"/../../", "/"},
{"..", ".."},
{"../", ".."},
{"/a/b/c/../", "/a/b/"},
{"/a/b/./", "/a/b/"},
{"/a/b/c/../d", "/a/b/d"},
{"/a/b/c/../d/", "/a/b/d/"},
#ifdef _WIN32
{"//a/", "//a/"},
{"//a/b/", "//a/b/"},
{"//a/b/.", "//a/b/"},
{"//a/..", "//a/"},
#else
{"//a/", "/a/"},
{"//a/b/", "/a/b/"},
{"//a/b/.", "/a/b/"},
{"//a/..", "/"},
#endif
///===---------------------------------------------------------------===//
/// Tests specifically for the clauses under [fs.path.generic]p6
///===---------------------------------------------------------------===//
// p1: If the path is empty, stop.
{"", ""},
// p2: Replace each slash character in the root-name with a preferred
// separator.
{"NO_ROOT_NAME_ON_LINUX", "NO_ROOT_NAME_ON_LINUX"},
// p3: Replace each directory-separator with a preferred-separator.
// [ Note: The generic pathname grammar ([fs.path.generic]) defines
// directory-separator as one or more slashes and preferred-separators.
// — end note ]
{"/", "/"},
{"//", "/"},
{"///", "/"},
{"a/b", "a/b"},
{"a//b", "a/b"},
{"a///b", "a/b"},
{"a/b/", "a/b/"},
{"a/b//", "a/b/"},
{"a/b///", "a/b/"},
{"///a////b//////", "/a/b/"},
// p4: Remove each dot filename and any immediately following directory
// separators
{"foo/.", "foo/"},
{"foo/./bar/.", "foo/bar/"},
{"./foo/././bar/./", "foo/bar/"},
{".///foo//.////./bar/.///", "foo/bar/"},
// p5: As long as any appear, remove a non-dot-dot filename immediately
// followed by a directory-separator and a dot-dot filename, along with
// any immediately following directory separator.
{"foo/..", "."},
{"foo/../", "."},
{"foo/bar/..", "foo/"},
{"foo/bar/../", "foo/"},
{"foo/bar/../..", "."},
{"foo/bar/../../", "."},
{"foo/bar/baz/../..", "foo/"},
{"foo/bar/baz/../../", "foo/"},
{"foo/bar/./..", "foo/"},
{"foo/bar/./../", "foo/"},
// p6: If there is a root-directory, remove all dot-dot filenames and any
// directory-separators immediately following them. [ Note: These dot-dot
// filenames attempt to refer to nonexistent parent directories. — end note ]
{"/..", "/"},
{"/../", "/"},
{"/foo/../..", "/"},
{"/../foo", "/foo"},
{"/../foo/../..", "/"},
// p7: If the last filename is dot-dot, remove any trailing
// directory-separator.
{"../", ".."},
{"../../", "../.."},
{"foo/../bar/../..///", ".."},
{"foo/../bar/..//..///../", "../.."},
// p8: If the path is empty, add a dot
{".", "."},
{"./", "."},
{"foo/..", "."}
};
// clang-format on
int ID = 0;
bool Failed = false;
for (auto& TC : TestCases) {
++ID;
fs::path p(TC.input);
const fs::path output = p.lexically_normal();
fs::path expect(TC.expect);
expect.make_preferred();
if (!PathEq(output, expect)) {
Failed = true;
std::fprintf(stderr, "TEST CASE #%d FAILED:\n"
" Input: '%s'\n"
" Expected: '%s'\n"
" Output: '%s'\n",
ID, TC.input.c_str(), expect.string().c_str(), output.string().c_str());
}
}
return Failed;
}
|