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// <array>
11
12// void swap(array& a);
13// namespace std { void swap(array<T, N> &x, array<T, N> &y);
14
15#include <cassert>
16#include <array>
17
18#include "test_macros.h"
19
20// std::array is explicitly allowed to be initialized with A a = { init-list };.
21// Disable the missing braces warning for this reason.
22#include "disable_missing_braces_warning.h"
23
24struct NonSwappable {
25  NonSwappable() {}
26private:
27  NonSwappable(NonSwappable const&);
28  NonSwappable& operator=(NonSwappable const&);
29};
30
31int main()
32{
33    {
34        typedef double T;
35        typedef std::array<T, 3> C;
36        C c1 = {1, 2, 3.5};
37        C c2 = {4, 5, 6.5};
38        c1.swap(c2);
39        assert(c1.size() == 3);
40        assert(c1[0] == 4);
41        assert(c1[1] == 5);
42        assert(c1[2] == 6.5);
43        assert(c2.size() == 3);
44        assert(c2[0] == 1);
45        assert(c2[1] == 2);
46        assert(c2[2] == 3.5);
47    }
48    {
49        typedef double T;
50        typedef std::array<T, 3> C;
51        C c1 = {1, 2, 3.5};
52        C c2 = {4, 5, 6.5};
53        std::swap(c1, c2);
54        assert(c1.size() == 3);
55        assert(c1[0] == 4);
56        assert(c1[1] == 5);
57        assert(c1[2] == 6.5);
58        assert(c2.size() == 3);
59        assert(c2[0] == 1);
60        assert(c2[1] == 2);
61        assert(c2[2] == 3.5);
62    }
63
64    {
65        typedef double T;
66        typedef std::array<T, 0> C;
67        C c1 = {};
68        C c2 = {};
69        c1.swap(c2);
70        assert(c1.size() == 0);
71        assert(c2.size() == 0);
72    }
73    {
74        typedef double T;
75        typedef std::array<T, 0> C;
76        C c1 = {};
77        C c2 = {};
78        std::swap(c1, c2);
79        assert(c1.size() == 0);
80        assert(c2.size() == 0);
81    }
82    {
83        typedef NonSwappable T;
84        typedef std::array<T, 0> C0;
85        C0 l = {};
86        C0 r = {};
87        l.swap(r);
88#if TEST_STD_VER >= 11
89        static_assert(noexcept(l.swap(r)), "");
90#endif
91    }
92
93}
94