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 struct NonSwappable { 20 TEST_CONSTEXPR NonSwappable() { } 21 private: 22 NonSwappable(NonSwappable const&); 23 NonSwappable& operator=(NonSwappable const&); 24 }; 25 26 TEST_CONSTEXPR_CXX20 bool tests() 27 { 28 { 29 typedef double T; 30 typedef std::array<T, 3> C; 31 C c1 = {1, 2, 3.5}; 32 C c2 = {4, 5, 6.5}; 33 c1.swap(c2); 34 assert(c1.size() == 3); 35 assert(c1[0] == 4); 36 assert(c1[1] == 5); 37 assert(c1[2] == 6.5); 38 assert(c2.size() == 3); 39 assert(c2[0] == 1); 40 assert(c2[1] == 2); 41 assert(c2[2] == 3.5); 42 } 43 { 44 typedef double T; 45 typedef std::array<T, 3> C; 46 C c1 = {1, 2, 3.5}; 47 C c2 = {4, 5, 6.5}; 48 std::swap(c1, c2); 49 assert(c1.size() == 3); 50 assert(c1[0] == 4); 51 assert(c1[1] == 5); 52 assert(c1[2] == 6.5); 53 assert(c2.size() == 3); 54 assert(c2[0] == 1); 55 assert(c2[1] == 2); 56 assert(c2[2] == 3.5); 57 } 58 59 { 60 typedef double T; 61 typedef std::array<T, 0> C; 62 C c1 = {}; 63 C c2 = {}; 64 c1.swap(c2); 65 assert(c1.size() == 0); 66 assert(c2.size() == 0); 67 } 68 { 69 typedef double T; 70 typedef std::array<T, 0> C; 71 C c1 = {}; 72 C c2 = {}; 73 std::swap(c1, c2); 74 assert(c1.size() == 0); 75 assert(c2.size() == 0); 76 } 77 { 78 typedef NonSwappable T; 79 typedef std::array<T, 0> C0; 80 C0 l = {}; 81 C0 r = {}; 82 l.swap(r); 83 #if TEST_STD_VER >= 11 84 static_assert(noexcept(l.swap(r)), ""); 85 #endif 86 } 87 88 return true; 89 } 90 91 int main(int, char**) 92 { 93 tests(); 94 #if TEST_STD_VER >= 20 95 static_assert(tests(), ""); 96 #endif 97 return 0; 98 } 99