libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

initializer_list_assign.pass.cpp (2276B)


      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 // UNSUPPORTED: c++98, c++03
     11 
     12 // <valarray>
     13 
     14 // template<class T> class valarray;
     15 
     16 // valarray& operator=(initializer_list<value_type> il);
     17 
     18 #include <valarray>
     19 #include <cassert>
     20 #include <cstddef>
     21 
     22 struct S
     23 {
     24     S() : x_(0) { default_ctor_called = true; }
     25     S(int x) : x_(x) {}
     26     int x_;
     27     static bool default_ctor_called;
     28 };
     29 
     30 bool S::default_ctor_called = false;
     31 
     32 bool operator==(const S& lhs, const S& rhs)
     33 {
     34     return lhs.x_ == rhs.x_;
     35 }
     36 
     37 int main()
     38 {
     39     {
     40         typedef int T;
     41         T a[] = {1, 2, 3, 4, 5};
     42         const unsigned N = sizeof(a)/sizeof(a[0]);
     43         std::valarray<T> v2;
     44         v2 = {1, 2, 3, 4, 5};
     45         assert(v2.size() == N);
     46         for (std::size_t i = 0; i < v2.size(); ++i)
     47             assert(v2[i] == a[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> v2;
     54         v2 = {1, 2.5, 3, 4.25, 5};
     55         assert(v2.size() == N);
     56         for (std::size_t i = 0; i < v2.size(); ++i)
     57             assert(v2[i] == a[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> v2(a, N-2);
     64         v2 = {T(1), T(2), T(3), T(4), T(5)};
     65         assert(v2.size() == N);
     66         for (unsigned i = 0; i < N; ++i)
     67         {
     68             assert(v2[i].size() == a[i].size());
     69             for (std::size_t j = 0; j < a[i].size(); ++j)
     70                 assert(v2[i][j] == a[i][j]);
     71         }
     72     }
     73     {
     74         typedef S T;
     75         T a[] = {T(1), T(2), T(3), T(4), T(5)};
     76         const unsigned N = sizeof(a)/sizeof(a[0]);
     77         std::valarray<T> v2;
     78         v2 = {T(1), T(2), T(3), T(4), T(5)};
     79         assert(v2.size() == N);
     80         for (std::size_t i = 0; i < v2.size(); ++i)
     81             assert(v2[i] == a[i]);
     82         assert(!S::default_ctor_called);
     83     }
     84 }