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