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 {"/foo", "/bar", "bar"}
36 , {"/foo", "/", ""}
37 , {"foo", "bar", "bar"}
38 , {"/", "/bar", "bar"}
39 #ifdef _WIN32
40 , {"\\", "\\bar", "bar"}
41 #else
42 , {"\\", "bar", "bar"}
43 #endif
44 , {"///", "///bar", "bar"}
45 #ifdef _WIN32
46 , {"\\\\", "\\\\bar", "bar"}
47 , {"\\/\\", "\\/\\bar", "bar"}
48 #else
49 , {"\\\\", "bar", "bar"}
50 , {"\\/\\", "\\/bar", "bar"}
51 #endif
52 , {".", "bar", "bar"}
53 , {"..", "bar", "bar"}
54 , {"/foo\\baz/bong/", "/foo\\baz/bong/bar", "bar"}
55 , {"/foo\\baz/bong", "/foo\\baz/bar", "bar"}
56 };
57
main(int,char **)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(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