• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // path weakly_canonical(const path& p);
15 // path weakly_canonical(const path& p, error_code& ec);
16 
17 #include "filesystem_include.hpp"
18 #include <type_traits>
19 #include <vector>
20 #include <iostream>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "test_iterators.h"
25 #include "count_new.hpp"
26 #include "filesystem_test_helper.hpp"
27 
28 
main()29 int main() {
30   // clang-format off
31   struct {
32     std::string input;
33     std::string expect;
34   } TestCases[] = {
35       {"", fs::current_path()},
36       {".", fs::current_path()},
37       {"/", "/"},
38       {"/foo", "/foo"},
39       {"/.", "/"},
40       {"/./", "/"},
41       {"a/b", fs::current_path() / "a/b"},
42       {"a", fs::current_path() / "a"},
43       {"a/b/", fs::current_path() / "a/b/"},
44       {StaticEnv::File, StaticEnv::File},
45       {StaticEnv::Dir, StaticEnv::Dir},
46       {StaticEnv::SymlinkToDir, StaticEnv::Dir},
47       {StaticEnv::SymlinkToDir / "dir2/.", StaticEnv::Dir / "dir2"},
48       // FIXME? If the trailing separator occurs in a part of the path that exists,
49       // it is ommitted. Otherwise it is added to the end of the result.
50       {StaticEnv::SymlinkToDir / "dir2/./", StaticEnv::Dir / "dir2"},
51       {StaticEnv::SymlinkToDir / "dir2/DNE/./", StaticEnv::Dir / "dir2/DNE/"},
52       {StaticEnv::SymlinkToDir / "dir2", StaticEnv::Dir2},
53       {StaticEnv::SymlinkToDir / "dir2/../dir2/DNE/..", StaticEnv::Dir2 / ""},
54       {StaticEnv::SymlinkToDir / "dir2/dir3/../DNE/DNE2", StaticEnv::Dir2 / "DNE/DNE2"},
55       {StaticEnv::Dir / "../dir1", StaticEnv::Dir},
56       {StaticEnv::Dir / "./.", StaticEnv::Dir},
57       {StaticEnv::Dir / "DNE/../foo", StaticEnv::Dir / "foo"}
58   };
59   // clang-format on
60   int ID = 0;
61   bool Failed = false;
62   for (auto& TC : TestCases) {
63     ++ID;
64     fs::path p(TC.input);
65     const fs::path output = fs::weakly_canonical(p);
66     if (!PathEq(output, TC.expect)) {
67       Failed = true;
68       std::cerr << "TEST CASE #" << ID << " FAILED: \n";
69       std::cerr << "  Input: '" << TC.input << "'\n";
70       std::cerr << "  Expected: '" << TC.expect << "'\n";
71       std::cerr << "  Output: '" << output.native() << "'";
72       std::cerr << std::endl;
73     }
74   }
75   return Failed;
76 }
77