| 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, c++11, c++14 |
| 10 | // UNSUPPORTED: availability-filesystem-missing |
| 11 | |
| 12 | // <filesystem> |
| 13 | |
| 14 | // class path |
| 15 | |
| 16 | // path& replace_filename( const path& replacement ); |
| 17 | |
| 18 | #include <filesystem> |
| 19 | #include <cassert> |
| 20 | #include <string> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | #include "test_iterators.h" |
| 24 | #include "count_new.h" |
| 25 | namespace fs = std::filesystem; |
| 26 | |
| 27 | struct ReplaceFilenameTestcase { |
| 28 | const char* value; |
| 29 | const char* expect; |
| 30 | const char* filename; |
| 31 | }; |
| 32 | |
| 33 | const ReplaceFilenameTestcase TestCases[] = |
| 34 | { |
| 35 | {.value: "/foo" , .expect: "/bar" , .filename: "bar" } |
| 36 | , {.value: "/foo" , .expect: "/" , .filename: "" } |
| 37 | , {.value: "foo" , .expect: "bar" , .filename: "bar" } |
| 38 | , {.value: "/" , .expect: "/bar" , .filename: "bar" } |
| 39 | #ifdef _WIN32 |
| 40 | , {"\\" , "\\bar" , "bar" } |
| 41 | #else |
| 42 | , {.value: "\\" , .expect: "bar" , .filename: "bar" } |
| 43 | #endif |
| 44 | , {.value: "///" , .expect: "///bar" , .filename: "bar" } |
| 45 | #ifdef _WIN32 |
| 46 | , {"\\\\" , "\\\\bar" , "bar" } |
| 47 | , {"\\/\\" , "\\/\\bar" , "bar" } |
| 48 | #else |
| 49 | , {.value: "\\\\" , .expect: "bar" , .filename: "bar" } |
| 50 | , {.value: "\\/\\" , .expect: "\\/bar" , .filename: "bar" } |
| 51 | #endif |
| 52 | , {.value: "." , .expect: "bar" , .filename: "bar" } |
| 53 | , {.value: ".." , .expect: "bar" , .filename: "bar" } |
| 54 | , {.value: "/foo\\baz/bong/" , .expect: "/foo\\baz/bong/bar" , .filename: "bar" } |
| 55 | , {.value: "/foo\\baz/bong" , .expect: "/foo\\baz/bar" , .filename: "bar" } |
| 56 | }; |
| 57 | |
| 58 | int main(int, char**) |
| 59 | { |
| 60 | using namespace fs; |
| 61 | for (auto const & TC : TestCases) { |
| 62 | path p(TC.value); |
| 63 | assert(p == TC.value); |
| 64 | path& Ref = p.replace_filename(replacement: TC.filename); |
| 65 | assert(p == TC.expect); |
| 66 | assert(&Ref == &p); |
| 67 | // Tests Effects "as-if": remove_filename() append(filename) |
| 68 | { |
| 69 | path p2(TC.value); |
| 70 | path replace(TC.filename); |
| 71 | p2.remove_filename(); |
| 72 | p2 /= replace; |
| 73 | assert(p == p2); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |