1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03
11 
12 // <filesystem>
13 
14 // class path
15 
16 // void swap(path& rhs) noexcept;
17 
18 #include "filesystem_include.hpp"
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 #include "count_new.hpp"
25 #include "filesystem_test_helper.hpp"
26 
27 
28 struct SwapTestcase {
29   const char* value1;
30   const char* value2;
31 };
32 
33 #define LONG_STR1 "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG"
34 #define LONG_STR2 "_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2"
35 const SwapTestcase TestCases[] =
36   {
37       {"", ""}
38     , {"shortstr", LONG_STR1}
39     , {LONG_STR1, "shortstr"}
40     , {LONG_STR1, LONG_STR2}
41   };
42 #undef LONG_STR1
43 #undef LONG_STR2
44 
main()45 int main()
46 {
47   using namespace fs;
48   {
49     path p;
50     ASSERT_NOEXCEPT(p.swap(p));
51     ASSERT_SAME_TYPE(void, decltype(p.swap(p)));
52   }
53   for (auto const & TC : TestCases) {
54     path p1(TC.value1);
55     path p2(TC.value2);
56     {
57       DisableAllocationGuard g;
58       p1.swap(p2);
59     }
60     assert(p1 == TC.value2);
61     assert(p2 == TC.value1);
62     {
63       DisableAllocationGuard g;
64       p1.swap(p2);
65     }
66     assert(p1 == TC.value1);
67     assert(p2 == TC.value2);
68   }
69   // self-swap
70   {
71     const char* Val = "aoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeu";
72     path p1(Val);
73     assert(p1 == Val);
74     {
75       DisableAllocationGuard g;
76       p1.swap(p1);
77     }
78     assert(p1 == Val);
79   }
80 }
81