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