xref: /llvm-project/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/swap.pass.cpp (revision ac8c9f1e39e1a773fd81ce23dbf1c80ea186f226)
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 // void swap(path& lhs, path& rhs) noexcept;
15 
16 #include <filesystem>
17 #include <type_traits>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "count_new.h"
22 namespace fs = std::filesystem;
23 
24 // NOTE: this is tested in path.members/path.modifiers via the member swap.
main(int,char **)25 int main(int, char**)
26 {
27   using namespace fs;
28   const char* value1 = "foo/bar/baz";
29   const char* value2 = "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG";
30   {
31     path p1(value1);
32     path p2(value2);
33     fs::path::string_type ps1 = p1.native();
34     fs::path::string_type ps2 = p2.native();
35 
36     DisableAllocationGuard g;
37     swap(p1, p2);
38     ASSERT_NOEXCEPT(swap(p1, p2));
39     ASSERT_SAME_TYPE(void, decltype(swap(p1, p2)));
40     assert(p1.native() == ps2);
41     assert(p2.native() == ps1);
42     swap(p1, p2);
43     assert(p1.native() == ps1);
44     assert(p2.native() == ps2);
45   }
46 
47   // Test the swap two-step idiom
48   {
49     path p1(value1);
50     path p2(value2);
51     fs::path::string_type ps1 = p1.native();
52     fs::path::string_type ps2 = p2.native();
53 
54     DisableAllocationGuard g;
55     using namespace std;
56     swap(p1, p2);
57     ASSERT_NOEXCEPT(swap(p1, p2));
58     ASSERT_SAME_TYPE(void, decltype(swap(p1, p2)));
59     assert(p1.native() == ps2);
60     assert(p2.native() == ps1);
61     swap(p1, p2);
62     assert(p1.native() == ps1);
63     assert(p2.native() == ps2);
64   }
65 
66   return 0;
67 }
68