1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03 10 11 // These tests require locale for non-char paths 12 // UNSUPPORTED: libcpp-has-no-localization 13 14 // <filesystem> 15 16 // class path 17 18 // path& operator/=(path const&) 19 // template <class Source> 20 // path& operator/=(Source const&); 21 // template <class Source> 22 // path& append(Source const&); 23 // template <class InputIterator> 24 // path& append(InputIterator first, InputIterator last); 25 26 27 #include "filesystem_include.h" 28 #include <type_traits> 29 #include <string_view> 30 #include <cassert> 31 32 // On Windows, the append function converts all inputs (pointers, iterators) 33 // to an intermediate path object, causing allocations in cases where no 34 // allocations are done on other platforms. 35 36 #include "test_macros.h" 37 #include "test_iterators.h" 38 #include "count_new.h" 39 #include "filesystem_test_helper.h" 40 41 42 struct AppendOperatorTestcase { 43 MultiStringType lhs; 44 MultiStringType rhs; 45 MultiStringType expect_posix; 46 MultiStringType expect_windows; 47 48 MultiStringType const& expected_result() const { 49 #ifdef _WIN32 50 return expect_windows; 51 #else 52 return expect_posix; 53 #endif 54 } 55 }; 56 57 #define S(Str) MKSTR(Str) 58 const AppendOperatorTestcase Cases[] = 59 { 60 {S(""), S(""), S(""), S("")} 61 , {S("p1"), S("p2"), S("p1/p2"), S("p1\\p2")} 62 , {S("p1/"), S("p2"), S("p1/p2"), S("p1/p2")} 63 , {S("p1"), S("/p2"), S("/p2"), S("/p2")} 64 , {S("p1/"), S("/p2"), S("/p2"), S("/p2")} 65 , {S("p1"), S("\\p2"), S("p1/\\p2"), S("\\p2")} 66 , {S("p1\\"), S("p2"), S("p1\\/p2"), S("p1\\p2")} 67 , {S("p1\\"), S("\\p2"), S("p1\\/\\p2"), S("\\p2")} 68 , {S(""), S("p2"), S("p2"), S("p2")} 69 , {S("/p1"), S("p2"), S("/p1/p2"), S("/p1\\p2")} 70 , {S("/p1"), S("/p2"), S("/p2"), S("/p2")} 71 , {S("/p1/p3"), S("p2"), S("/p1/p3/p2"), S("/p1/p3\\p2")} 72 , {S("/p1/p3/"), S("p2"), S("/p1/p3/p2"), S("/p1/p3/p2")} 73 , {S("/p1/"), S("p2"), S("/p1/p2"), S("/p1/p2")} 74 , {S("/p1/p3/"), S("/p2/p4"), S("/p2/p4"), S("/p2/p4")} 75 , {S("/"), S(""), S("/"), S("/")} 76 , {S("/p1"), S("/p2/"), S("/p2/"), S("/p2/")} 77 , {S("p1"), S(""), S("p1/"), S("p1\\")} 78 , {S("p1/"), S(""), S("p1/"), S("p1/")} 79 80 , {S("//host"), S("foo"), S("//host/foo"), S("//host\\foo")} 81 , {S("//host/"), S("foo"), S("//host/foo"), S("//host/foo")} 82 , {S("//host"), S(""), S("//host/"), S("//host\\")} 83 84 , {S("foo"), S("C:/bar"), S("foo/C:/bar"), S("C:/bar")} 85 , {S("foo"), S("C:"), S("foo/C:"), S("C:")} 86 87 , {S("C:"), S(""), S("C:/"), S("C:")} 88 , {S("C:foo"), S("/bar"), S("/bar"), S("C:/bar")} 89 , {S("C:foo"), S("bar"), S("C:foo/bar"), S("C:foo\\bar")} 90 , {S("C:/foo"), S("bar"), S("C:/foo/bar"), S("C:/foo\\bar")} 91 , {S("C:/foo"), S("/bar"), S("/bar"), S("C:/bar")} 92 93 , {S("C:foo"), S("C:/bar"), S("C:foo/C:/bar"), S("C:/bar")} 94 , {S("C:foo"), S("C:bar"), S("C:foo/C:bar"), S("C:foo\\bar")} 95 , {S("C:/foo"), S("C:/bar"), S("C:/foo/C:/bar"), S("C:/bar")} 96 , {S("C:/foo"), S("C:bar"), S("C:/foo/C:bar"), S("C:/foo\\bar")} 97 98 , {S("C:foo"), S("c:/bar"), S("C:foo/c:/bar"), S("c:/bar")} 99 , {S("C:foo"), S("c:bar"), S("C:foo/c:bar"), S("c:bar")} 100 , {S("C:/foo"), S("c:/bar"), S("C:/foo/c:/bar"), S("c:/bar")} 101 , {S("C:/foo"), S("c:bar"), S("C:/foo/c:bar"), S("c:bar")} 102 103 , {S("C:/foo"), S("D:bar"), S("C:/foo/D:bar"), S("D:bar")} 104 }; 105 106 107 const AppendOperatorTestcase LongLHSCases[] = 108 { 109 {S("p1"), S("p2"), S("p1/p2"), S("p1\\p2")} 110 , {S("p1/"), S("p2"), S("p1/p2"), S("p1/p2")} 111 , {S("p1"), S("/p2"), S("/p2"), S("/p2")} 112 , {S("/p1"), S("p2"), S("/p1/p2"), S("/p1\\p2")} 113 }; 114 #undef S 115 116 117 // The append operator may need to allocate a temporary buffer before a code_cvt 118 // conversion. Test if this allocation occurs by: 119 // 1. Create a path, `LHS`, and reserve enough space to append `RHS`. 120 // This prevents `LHS` from allocating during the actual appending. 121 // 2. Create a `Source` object `RHS`, which represents a "large" string. 122 // (The string must not trigger the SSO) 123 // 3. Append `RHS` to `LHS` and check for the expected allocation behavior. 124 template <class CharT> 125 void doAppendSourceAllocTest(AppendOperatorTestcase const& TC) 126 { 127 using namespace fs; 128 using Ptr = CharT const*; 129 using Str = std::basic_string<CharT>; 130 using StrView = std::basic_string_view<CharT>; 131 using InputIter = input_iterator<Ptr>; 132 133 const Ptr L = TC.lhs; 134 Str RShort = (Ptr)TC.rhs; 135 Str EShort = (Ptr)TC.expected_result(); 136 assert(RShort.size() >= 2); 137 CharT c = RShort.back(); 138 RShort.append(100, c); 139 EShort.append(100, c); 140 const Ptr R = RShort.data(); 141 const Str& E = EShort; 142 std::size_t ReserveSize = E.size() + 3; 143 // basic_string 144 { 145 path LHS(L); PathReserve(LHS, ReserveSize); 146 Str RHS(R); 147 { 148 TEST_NOT_WIN32(DisableAllocationGuard g); 149 LHS /= RHS; 150 } 151 assert(PathEq(LHS, E)); 152 } 153 // basic_string_view 154 { 155 path LHS(L); PathReserve(LHS, ReserveSize); 156 StrView RHS(R); 157 { 158 TEST_NOT_WIN32(DisableAllocationGuard g); 159 LHS /= RHS; 160 } 161 assert(PathEq(LHS, E)); 162 } 163 // CharT* 164 { 165 path LHS(L); PathReserve(LHS, ReserveSize); 166 Ptr RHS(R); 167 { 168 TEST_NOT_WIN32(DisableAllocationGuard g); 169 LHS /= RHS; 170 } 171 assert(PathEq(LHS, E)); 172 } 173 { 174 path LHS(L); PathReserve(LHS, ReserveSize); 175 Ptr RHS(R); 176 { 177 TEST_NOT_WIN32(DisableAllocationGuard g); 178 LHS.append(RHS, StrEnd(RHS)); 179 } 180 assert(PathEq(LHS, E)); 181 } 182 { 183 path LHS(L); PathReserve(LHS, ReserveSize); 184 path RHS(R); 185 { 186 DisableAllocationGuard g; 187 LHS /= RHS; 188 } 189 assert(PathEq(LHS, E)); 190 } 191 // input iterator - For non-native char types, appends needs to copy the 192 // iterator range into a contiguous block of memory before it can perform the 193 // code_cvt conversions. 194 // For "char" no allocations will be performed because no conversion is 195 // required. 196 // On Windows, the append method is more complex and uses intermediate 197 // path objects, which causes extra allocations. 198 #ifdef _WIN32 199 bool DisableAllocations = false; 200 #else 201 bool DisableAllocations = std::is_same<CharT, char>::value; 202 #endif 203 { 204 path LHS(L); PathReserve(LHS, ReserveSize); 205 InputIter RHS(R); 206 { 207 RequireAllocationGuard g; // requires 1 or more allocations occur by default 208 if (DisableAllocations) g.requireExactly(0); 209 LHS /= RHS; 210 } 211 assert(PathEq(LHS, E)); 212 } 213 { 214 path LHS(L); PathReserve(LHS, ReserveSize); 215 InputIter RHS(R); 216 InputIter REnd(StrEnd(R)); 217 { 218 RequireAllocationGuard g; 219 if (DisableAllocations) g.requireExactly(0); 220 LHS.append(RHS, REnd); 221 } 222 assert(PathEq(LHS, E)); 223 } 224 } 225 226 template <class CharT> 227 void doAppendSourceTest(AppendOperatorTestcase const& TC) 228 { 229 using namespace fs; 230 using Ptr = CharT const*; 231 using Str = std::basic_string<CharT>; 232 using StrView = std::basic_string_view<CharT>; 233 using InputIter = input_iterator<Ptr>; 234 const Ptr L = TC.lhs; 235 const Ptr R = TC.rhs; 236 const Ptr E = TC.expected_result(); 237 // basic_string 238 { 239 path Result(L); 240 Str RHS(R); 241 path& Ref = (Result /= RHS); 242 assert(Result == E); 243 assert(&Ref == &Result); 244 } 245 { 246 path LHS(L); 247 Str RHS(R); 248 path& Ref = LHS.append(RHS); 249 assert(PathEq(LHS, E)); 250 assert(&Ref == &LHS); 251 } 252 // basic_string_view 253 { 254 path LHS(L); 255 StrView RHS(R); 256 path& Ref = (LHS /= RHS); 257 assert(PathEq(LHS, E)); 258 assert(&Ref == &LHS); 259 } 260 { 261 path LHS(L); 262 StrView RHS(R); 263 path& Ref = LHS.append(RHS); 264 assert(PathEq(LHS, E)); 265 assert(&Ref == &LHS); 266 } 267 // Char* 268 { 269 path LHS(L); 270 Str RHS(R); 271 path& Ref = (LHS /= RHS); 272 assert(PathEq(LHS, E)); 273 assert(&Ref == &LHS); 274 } 275 { 276 path LHS(L); 277 Ptr RHS(R); 278 path& Ref = LHS.append(RHS); 279 assert(PathEq(LHS, E)); 280 assert(&Ref == &LHS); 281 } 282 { 283 path LHS(L); 284 Ptr RHS(R); 285 path& Ref = LHS.append(RHS, StrEnd(RHS)); 286 assert(PathEq(LHS, E)); 287 assert(&Ref == &LHS); 288 } 289 // iterators 290 { 291 path LHS(L); 292 InputIter RHS(R); 293 path& Ref = (LHS /= RHS); 294 assert(PathEq(LHS, E)); 295 assert(&Ref == &LHS); 296 } 297 { 298 path LHS(L); InputIter RHS(R); 299 path& Ref = LHS.append(RHS); 300 assert(PathEq(LHS, E)); 301 assert(&Ref == &LHS); 302 } 303 { 304 path LHS(L); 305 InputIter RHS(R); 306 InputIter REnd(StrEnd(R)); 307 path& Ref = LHS.append(RHS, REnd); 308 assert(PathEq(LHS, E)); 309 assert(&Ref == &LHS); 310 } 311 } 312 313 314 315 template <class It, class = decltype(fs::path{}.append(std::declval<It>()))> 316 constexpr bool has_append(int) { return true; } 317 template <class It> 318 constexpr bool has_append(long) { return false; } 319 320 template <class It, class = decltype(fs::path{}.operator/=(std::declval<It>()))> 321 constexpr bool has_append_op(int) { return true; } 322 template <class It> 323 constexpr bool has_append_op(long) { return false; } 324 325 template <class It> 326 constexpr bool has_append() { 327 static_assert(has_append<It>(0) == has_append_op<It>(0), "must be same"); 328 return has_append<It>(0) && has_append_op<It>(0); 329 } 330 331 void test_sfinae() 332 { 333 using namespace fs; 334 { 335 using It = const char* const; 336 static_assert(has_append<It>(), ""); 337 } 338 { 339 using It = input_iterator<const char*>; 340 static_assert(has_append<It>(), ""); 341 } 342 { 343 struct Traits { 344 using iterator_category = std::input_iterator_tag; 345 using value_type = const char; 346 using pointer = const char*; 347 using reference = const char&; 348 using difference_type = std::ptrdiff_t; 349 }; 350 using It = input_iterator<const char*, Traits>; 351 static_assert(has_append<It>(), ""); 352 } 353 { 354 using It = output_iterator<const char*>; 355 static_assert(!has_append<It>(), ""); 356 357 } 358 { 359 static_assert(!has_append<int*>(), ""); 360 } 361 { 362 static_assert(!has_append<char>(), ""); 363 static_assert(!has_append<const char>(), ""); 364 } 365 } 366 367 int main(int, char**) 368 { 369 using namespace fs; 370 for (auto const & TC : Cases) { 371 { 372 const char* LHS_In = TC.lhs; 373 const char* RHS_In = TC.rhs; 374 path LHS(LHS_In); 375 path RHS(RHS_In); 376 path& Res = (LHS /= RHS); 377 assert(PathEq(Res, (const char*)TC.expected_result())); 378 assert(&Res == &LHS); 379 } 380 doAppendSourceTest<char> (TC); 381 doAppendSourceTest<wchar_t> (TC); 382 doAppendSourceTest<char16_t>(TC); 383 doAppendSourceTest<char32_t>(TC); 384 } 385 for (auto const & TC : LongLHSCases) { 386 (void)TC; 387 LIBCPP_ONLY(doAppendSourceAllocTest<char>(TC)); 388 LIBCPP_ONLY(doAppendSourceAllocTest<wchar_t>(TC)); 389 } 390 test_sfinae(); 391 392 return 0; 393 } 394