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// <valarray>
10
11// template<class T> class valarray;
12
13// void swap(valarray& v);
14
15#include <valarray>
16#include <cassert>
17#include <cstddef>
18
19#include "test_macros.h"
20
21int main(int, char**)
22{
23 {
24 typedef int T;
25 T a1[] = {1, 2, 3, 4, 5};
26 T a2[] = {6, 7, 8, 9, 10, 11, 12};
27 const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
28 const unsigned N2 = sizeof(a2)/sizeof(a2[0]);
29 std::valarray<T> v1(a1, N1);
30 std::valarray<T> v2(a2, N2);
31 std::valarray<T> v1_save = v1;
32 std::valarray<T> v2_save = v2;
33 v1.swap(v&: v2);
34 assert(v1.size() == v2_save.size());
35 for (std::size_t i = 0; i < v1.size(); ++i)
36 assert(v1[i] == v2_save[i]);
37 assert(v2.size() == v1_save.size());
38 for (std::size_t i = 0; i < v2.size(); ++i)
39 assert(v2[i] == v1_save[i]);
40 }
41 {
42 typedef int T;
43 T a1[] = {1, 2, 3, 4, 5};
44 const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
45 std::valarray<T> v1(a1, N1);
46 std::valarray<T> v2;
47 std::valarray<T> v1_save = v1;
48 std::valarray<T> v2_save = v2;
49 v1.swap(v&: v2);
50 assert(v1.size() == v2_save.size());
51 for (std::size_t i = 0; i < v1.size(); ++i)
52 assert(v1[i] == v2_save[i]);
53 assert(v2.size() == v1_save.size());
54 for (std::size_t i = 0; i < v2.size(); ++i)
55 assert(v2[i] == v1_save[i]);
56 }
57 {
58 typedef int T;
59 T a2[] = {6, 7, 8, 9, 10, 11, 12};
60 const unsigned N2 = sizeof(a2)/sizeof(a2[0]);
61 std::valarray<T> v1;
62 std::valarray<T> v2(a2, N2);
63 std::valarray<T> v1_save = v1;
64 std::valarray<T> v2_save = v2;
65 v1.swap(v&: v2);
66 assert(v1.size() == v2_save.size());
67 for (std::size_t i = 0; i < v1.size(); ++i)
68 assert(v1[i] == v2_save[i]);
69 assert(v2.size() == v1_save.size());
70 for (std::size_t i = 0; i < v2.size(); ++i)
71 assert(v2[i] == v1_save[i]);
72 }
73 {
74 typedef int T;
75 std::valarray<T> v1;
76 std::valarray<T> v2;
77 std::valarray<T> v1_save = v1;
78 std::valarray<T> v2_save = v2;
79 v1.swap(v&: v2);
80 assert(v1.size() == v2_save.size());
81 for (std::size_t i = 0; i < v1.size(); ++i)
82 assert(v1[i] == v2_save[i]);
83 assert(v2.size() == v1_save.size());
84 for (std::size_t i = 0; i < v2.size(); ++i)
85 assert(v2[i] == v1_save[i]);
86 }
87
88 return 0;
89}
90

source code of libcxx/test/std/numerics/numarray/template.valarray/valarray.members/swap.pass.cpp