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