PR libstdc++/82777 fix path normalization for dot-dot
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / generation / normal.cc
blobdf3b7154ab3e20f2c5587aafa1fcf8db154cf6f1
1 // Copyright (C) 2017 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++17 -lstdc++fs" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
22 #include <filesystem>
23 #include <testsuite_fs.h>
24 #include <testsuite_hooks.h>
26 using std::filesystem::path;
27 using __gnu_test::compare_paths;
29 void
30 test01()
32 // C++17 [fs.path.gen] p2
33 compare_paths( path("foo/./bar/..").lexically_normal(), "foo/" );
34 compare_paths( path("foo/.///bar/../").lexically_normal(), "foo/" );
37 void
38 test02()
40 compare_paths( path("foo/../bar").lexically_normal(), "bar" );
41 compare_paths( path("../foo/../bar").lexically_normal(), "../bar" );
42 compare_paths( path("foo/../").lexically_normal(), "." );
43 compare_paths( path("../../").lexically_normal(), "../.." );
44 compare_paths( path("../").lexically_normal(), ".." );
45 compare_paths( path("./").lexically_normal(), "." );
46 compare_paths( path().lexically_normal(), "" );
48 compare_paths( path("/..").lexically_normal(), "/" );
50 // PR libstdc++/82777
51 compare_paths( path("./a/b/c/../.././b/c").lexically_normal(), "a/b/c" );
52 compare_paths( path("/a/b/c/../.././b/c").lexically_normal(), "/a/b/c" );
55 void
56 test03()
58 struct
60 const char* input;
61 const char* normalized;
62 } testcases[] = {
63 {"" , "" },
64 {"." , "." },
65 {".." , ".." },
66 {"/" , "/" },
67 {"//" , "//" },
69 {"/foo" , "/foo" },
70 {"/foo/" , "/foo/" },
71 {"/foo/." , "/foo/" },
72 {"/foo/bar/.." , "/foo/" },
73 {"/foo/.." , "/" },
75 {"/." , "/" },
76 {"/./" , "/" },
77 {"/./." , "/" },
78 {"/././" , "/" },
79 {"/././." , "/" },
81 {"./" , "." },
82 {"./." , "." },
83 {"././" , "." },
84 {"././." , "." },
85 {"./././" , "." },
86 {"./././." , "." },
88 {"foo/.." , "." },
89 {"foo/../" , "." },
90 {"foo/../.." , ".." },
92 // with root name (OS-dependent):
93 #if defined(_WIN32) && !defined(__CYGWIN__)
94 {"C:bar/.." , "C:." },
95 #else
96 {"C:bar/.." , "." },
97 #endif
98 {"C:/bar/.." , "C:/" },
99 {"C:" , "C:" },
100 #ifdef __CYGWIN__
101 {"//host/bar/.." , "//host/" },
102 {"//host" , "//host" },
103 #else
104 {"//host/bar/.." , "/host/" },
105 {"//host" , "/host" },
106 #endif
108 // a few others:
109 {"foo/../foo/.." , "." },
110 {"foo/../foo/../.." , ".." },
111 {"../foo/../foo/.." , ".." },
112 {"../.f/../f" , "../f" },
113 {"../f/../.f" , "../.f" },
114 {".././../." , "../.." },
115 {".././.././" , "../.." },
116 {"/.." , "/" },
118 for (auto& test : testcases)
119 compare_paths( path(test.input).lexically_normal(), test.normalized );
123 main()
125 test01();
126 test02();
127 test03();