| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
// Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 30.10.7.4.4 path concatenation [fs.path.concat] |
| 21 |
|
| 22 |
#include <filesystem> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
#include <testsuite_iterators.h> |
| 25 |
#include <testsuite_fs.h> |
| 26 |
|
| 27 |
using std::filesystem::path; |
| 28 |
using __gnu_test::compare_paths; |
| 29 |
|
| 30 |
void |
| 31 |
test01() |
| 32 |
{ |
| 33 |
path p("/"); |
| 34 |
p += "foo"; |
| 35 |
VERIFY( p.filename().string() == "foo" ); |
| 36 |
p += "bar"; |
| 37 |
VERIFY( p.filename().string() == "foobar" ); |
| 38 |
p += '/'; |
| 39 |
VERIFY( p.parent_path().string() == "/foobar" ); |
| 40 |
VERIFY( p.filename().string() == "" ); |
| 41 |
#if _GLIBCXX_USE_WCHAR_T |
| 42 |
VERIFY( p.parent_path().wstring() == L"/foobar" ); |
| 43 |
VERIFY( p.filename().wstring() == L"" ); |
| 44 |
p += L"baz.txt"; |
| 45 |
#else |
| 46 |
p += "baz.txt"; |
| 47 |
#endif |
| 48 |
VERIFY( p.filename().string() == "baz.txt" ); |
| 49 |
p.concat("/dir/"); |
| 50 |
// N.B. on Windows p.parent_path() is "/foobar\\baz.txt\\dir" |
| 51 |
VERIFY( p.parent_path() == path("/foobar/baz.txt/dir") ); |
| 52 |
VERIFY( p.filename().string() == "" ); |
| 53 |
const char file[] = "file"; |
| 54 |
__gnu_test::test_container<const char, __gnu_test::input_iterator_wrapper> |
| 55 |
input(file, file + 4); |
| 56 |
p.concat(input.begin(), input.end()); |
| 57 |
VERIFY( p.filename().string() == file ); |
| 58 |
} |
| 59 |
|
| 60 |
void |
| 61 |
test02() |
| 62 |
{ |
| 63 |
std::basic_string_view<path::value_type> s, expected; |
| 64 |
|
| 65 |
path p = "0/1/2/3/4/5/6"; |
| 66 |
// The string_view aliases the path's internal string: |
| 67 |
s = p.native(); |
| 68 |
// Append that string_view, which must work correctly even though the |
| 69 |
// internal string will be reallocated during the operation: |
| 70 |
p += s; |
| 71 |
compare_paths(p, "0/1/2/3/4/5/60/1/2/3/4/5/6"); |
| 72 |
|
| 73 |
// Same again with a trailing slash: |
| 74 |
path p2 = "0/1/2/3/4/5/"; |
| 75 |
s = p2.native(); |
| 76 |
p2 += s; |
| 77 |
compare_paths(p2, "0/1/2/3/4/5/0/1/2/3/4/5/"); |
| 78 |
|
| 79 |
// And aliasing one of the components of the path: |
| 80 |
path p3 = "0/123456789"; |
| 81 |
path::iterator second = std::next(p3.begin()); |
| 82 |
s = second->native(); |
| 83 |
p3 += s; |
| 84 |
compare_paths(p3, "0/123456789123456789" ); |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
test03() |
| 89 |
{ |
| 90 |
const std::string s0 = "a/b/c"; |
| 91 |
path p = s0; |
| 92 |
std::string s; |
| 93 |
for (int i = 0; i < 10; ++i) |
| 94 |
s += "0/1/2/3/4/5/6/7/8/9/"; |
| 95 |
// concat a long string with many components: |
| 96 |
p += s; |
| 97 |
compare_paths(p, path(s0+s)); |
| 98 |
|
| 99 |
// Same again but with a trailing slash on the left operand: |
| 100 |
path p2 = s0 + '/'; |
| 101 |
p2 += s; |
| 102 |
compare_paths(p2, path(s0+'/'+s)); |
| 103 |
|
| 104 |
// And again but with a leading slash on the right operand: |
| 105 |
path p3 = s0; |
| 106 |
s.insert(0, 1, '/'); |
| 107 |
p3 += s; |
| 108 |
compare_paths(p2, path(s0+s)); |
| 109 |
|
| 110 |
// And again but with a slash on both operands: |
| 111 |
path p4 = s0 + '/'; |
| 112 |
p4 += s; |
| 113 |
compare_paths(p4, path(s0+'/'+s)); |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
test04() |
| 118 |
{ |
| 119 |
// Concat every test path onto every test path. |
| 120 |
for (path p : __gnu_test::test_paths) |
| 121 |
{ |
| 122 |
for (path x : __gnu_test::test_paths) |
| 123 |
{ |
| 124 |
auto prior_native = p.native(); |
| 125 |
p += x.native(); |
| 126 |
VERIFY( p.native() == prior_native + x.native() ); |
| 127 |
path copy(p); |
| 128 |
compare_paths( copy, p ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
int |
| 134 |
main() |
| 135 |
{ |
| 136 |
test01(); |
| 137 |
test02(); |
| 138 |
test03(); |
| 139 |
test04(); |
| 140 |
} |