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()
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     , {"\\", "bar", "bar"}
39     , {"///", "///bar", "bar"}
40     , {"\\\\", "bar", "bar"}
41     , {"\\/\\", "\\/bar", "bar"}
42     , {".", "bar", "bar"}
43     , {"..", "bar", "bar"}
44     , {"/foo\\baz/bong/", "/foo\\baz/bong/bar", "bar"}
45     , {"/foo\\baz/bong", "/foo\\baz/bar", "bar"}
46   };
47 
48 int main(int, char**)
49 {
50   using namespace fs;
51   for (auto const & TC : TestCases) {
52     path p(TC.value);
53     assert(p == TC.value);
54     path& Ref = p.replace_filename(TC.filename);
55     assert(p == TC.expect);
56     assert(&Ref == &p);
57     // Tests Effects "as-if": remove_filename() append(filename)
58     {
59       path p2(TC.value);
60       path replace(TC.filename);
61       p2.remove_filename();
62       p2 /= replace;
63       assert(p == p2);
64     }
65   }
66 
67   return 0;
68 }
69