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 | // void swap(path& rhs) noexcept; |
17 | |
18 | #include <filesystem> |
19 | #include <cassert> |
20 | #include <type_traits> |
21 | |
22 | #include "assert_macros.h" |
23 | #include "count_new.h" |
24 | #include "test_iterators.h" |
25 | namespace fs = std::filesystem; |
26 | |
27 | struct SwapTestcase { |
28 | const char* value1; |
29 | const char* value2; |
30 | }; |
31 | |
32 | #define LONG_STR1 "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG" |
33 | #define LONG_STR2 "_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2" |
34 | const SwapTestcase TestCases[] = |
35 | { |
36 | {.value1: "" , .value2: "" } |
37 | , {.value1: "shortstr" , LONG_STR1} |
38 | , {LONG_STR1, .value2: "shortstr" } |
39 | , {LONG_STR1, LONG_STR2} |
40 | }; |
41 | #undef LONG_STR1 |
42 | #undef LONG_STR2 |
43 | |
44 | int main(int, char**) |
45 | { |
46 | using namespace fs; |
47 | { |
48 | path p; |
49 | ASSERT_NOEXCEPT(p.swap(rhs&: p)); |
50 | ASSERT_SAME_TYPE(void, decltype(p.swap(p))); |
51 | } |
52 | for (auto const & TC : TestCases) { |
53 | path p1(TC.value1); |
54 | path p2(TC.value2); |
55 | { |
56 | DisableAllocationGuard g; |
57 | p1.swap(rhs&: p2); |
58 | } |
59 | assert(p1 == TC.value2); |
60 | assert(p2 == TC.value1); |
61 | { |
62 | DisableAllocationGuard g; |
63 | p1.swap(rhs&: p2); |
64 | } |
65 | assert(p1 == TC.value1); |
66 | assert(p2 == TC.value2); |
67 | } |
68 | // self-swap |
69 | { |
70 | const char* Val = "aoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeu" ; |
71 | path p1(Val); |
72 | assert(p1 == Val); |
73 | { |
74 | DisableAllocationGuard g; |
75 | p1.swap(rhs&: p1); |
76 | } |
77 | assert(p1 == Val); |
78 | } |
79 | |
80 | return 0; |
81 | } |
82 | |