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_extension(path const& p = path())
17
18#include <filesystem>
19#include <cassert>
20#include <string>
21#include <type_traits>
22
23#include "count_new.h"
24#include "test_iterators.h"
25namespace fs = std::filesystem;
26
27struct ReplaceExtensionTestcase {
28 const char* value;
29 const char* expect;
30 const char* extension;
31};
32
33const ReplaceExtensionTestcase TestCases[] =
34 {
35 {.value: "", .expect: "", .extension: ""}
36 , {.value: "foo.cpp", .expect: "foo", .extension: ""}
37 , {.value: "foo.cpp", .expect: "foo.", .extension: "."}
38 , {.value: "foo..cpp", .expect: "foo..txt", .extension: "txt"}
39 , {.value: "", .expect: ".txt", .extension: "txt"}
40 , {.value: "", .expect: ".txt", .extension: ".txt"}
41 , {.value: "/foo", .expect: "/foo.txt", .extension: ".txt"}
42 , {.value: "/foo", .expect: "/foo.txt", .extension: "txt"}
43 , {.value: "/foo.cpp", .expect: "/foo.txt", .extension: ".txt"}
44 , {.value: "/foo.cpp", .expect: "/foo.txt", .extension: "txt"}
45 };
46const ReplaceExtensionTestcase NoArgCases[] =
47 {
48 {.value: "", .expect: "", .extension: ""}
49 , {.value: "foo", .expect: "foo", .extension: ""}
50 , {.value: "foo.cpp", .expect: "foo", .extension: ""}
51 , {.value: "foo..cpp", .expect: "foo.", .extension: ""}
52};
53
54int main(int, char**)
55{
56 using namespace fs;
57 for (auto const & TC : TestCases) {
58 path p(TC.value);
59 assert(p == TC.value);
60 path& Ref = (p.replace_extension(replacement: TC.extension));
61 assert(p == TC.expect);
62 assert(&Ref == &p);
63 }
64 for (auto const& TC : NoArgCases) {
65 path p(TC.value);
66 assert(p == TC.value);
67 path& Ref = (p.replace_extension());
68 assert(p == TC.expect);
69 assert(&Ref == &p);
70 }
71
72 return 0;
73}
74

source code of libcxx/test/std/input.output/filesystems/class.path/path.member/path.modifiers/replace_extension.pass.cpp