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 // valarray& operator=(const valarray& v);
15 
16 #include <valarray>
17 #include <cassert>
18 #include <cstddef>
19 
20 struct S
21 {
SS22     S() : x_(0) { default_ctor_called = true; }
SS23     S(int x) : x_(x) {}
24     int x_;
25     static bool default_ctor_called;
26 };
27 
28 bool S::default_ctor_called = false;
29 
operator ==(const S & lhs,const S & rhs)30 bool operator==(const S& lhs, const S& rhs)
31 {
32     return lhs.x_ == rhs.x_;
33 }
34 
main()35 int main()
36 {
37     {
38         typedef int T;
39         T a[] = {1, 2, 3, 4, 5};
40         const unsigned N = sizeof(a)/sizeof(a[0]);
41         std::valarray<T> v(a, N);
42         std::valarray<T> v2;
43         v2 = v;
44         assert(v2.size() == v.size());
45         for (std::size_t i = 0; i < v2.size(); ++i)
46             assert(v2[i] == v[i]);
47     }
48     {
49         typedef double T;
50         T a[] = {1, 2.5, 3, 4.25, 5};
51         const unsigned N = sizeof(a)/sizeof(a[0]);
52         std::valarray<T> v(a, N);
53         std::valarray<T> v2;
54         v2 = v;
55         assert(v2.size() == v.size());
56         for (std::size_t i = 0; i < v2.size(); ++i)
57             assert(v2[i] == v[i]);
58     }
59     {
60         typedef std::valarray<double> T;
61         T a[] = {T(1), T(2), T(3), T(4), T(5)};
62         const unsigned N = sizeof(a)/sizeof(a[0]);
63         std::valarray<T> v(a, N);
64         std::valarray<T> v2(a, N-2);
65         v2 = v;
66         assert(v2.size() == v.size());
67         for (unsigned i = 0; i < N; ++i)
68         {
69             assert(v2[i].size() == v[i].size());
70             for (std::size_t j = 0; j < v[i].size(); ++j)
71                 assert(v2[i][j] == v[i][j]);
72         }
73     }
74     {
75         typedef S T;
76         T a[] = {T(1), T(2), T(3), T(4), T(5)};
77         const unsigned N = sizeof(a)/sizeof(a[0]);
78         std::valarray<T> v(a, N);
79         std::valarray<T> v2;
80         v2 = v;
81         assert(v2.size() == v.size());
82         for (std::size_t i = 0; i < v2.size(); ++i)
83             assert(v2[i] == v[i]);
84         assert(!S::default_ctor_called);
85     }
86 }
87