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