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