Fix filesystem::path::lexically_normal algorithm
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / generation / normal.cc
blob789ce186f82a8eeb4c5baba94ebc2b306cb7bccc
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(), "/" );
51 void
52 test03()
54 struct
56 const char* input;
57 const char* normalized;
58 } testcases[] = {
59 {"" , "" },
60 {"." , "." },
61 {".." , ".." },
62 {"/" , "/" },
63 {"//" , "//" },
65 {"/foo" , "/foo" },
66 {"/foo/" , "/foo/" },
67 {"/foo/." , "/foo/" },
68 {"/foo/bar/.." , "/foo/" },
69 {"/foo/.." , "/" },
71 {"/." , "/" },
72 {"/./" , "/" },
73 {"/./." , "/" },
74 {"/././" , "/" },
75 {"/././." , "/" },
77 {"./" , "." },
78 {"./." , "." },
79 {"././" , "." },
80 {"././." , "." },
81 {"./././" , "." },
82 {"./././." , "." },
84 {"foo/.." , "." },
85 {"foo/../" , "." },
86 {"foo/../.." , ".." },
88 // with root name (OS-dependent):
89 #if defined(_WIN32) && !defined(__CYGWIN__)
90 {"C:bar/.." , "C:." },
91 #else
92 {"C:bar/.." , "." },
93 #endif
94 {"C:/bar/.." , "C:/" },
95 {"C:" , "C:" },
96 #ifdef __CYGWIN__
97 {"//host/bar/.." , "//host/" },
98 {"//host" , "//host" },
99 #else
100 {"//host/bar/.." , "/host/" },
101 {"//host" , "/host" },
102 #endif
104 // a few others:
105 {"foo/../foo/.." , "." },
106 {"foo/../foo/../.." , ".." },
107 {"../foo/../foo/.." , ".." },
108 {"../.f/../f" , "../f" },
109 {"../f/../.f" , "../.f" },
110 {".././../." , "../.." },
111 {".././.././" , "../.." },
112 {"/.." , "/" },
114 for (auto& test : testcases)
115 compare_paths( path(test.input).lexically_normal(), test.normalized );
119 main()
121 test01();
122 test02();
123 test03();