Home | History | Annotate | Download | only in dynarray.cons
      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 // dynarray.cons
     11 
     12 // explicit dynarray(size_type c);
     13 // dynarray(size_type c, const T& v);
     14 // dynarray(initializer_list<T>);
     15 // dynarray(const dynarray& d);
     16 
     17 // ~dynarray();
     18 
     19 
     20 #include <__config>
     21 
     22 #if _LIBCPP_STD_VER > 11
     23 
     24 #include <experimental/dynarray>
     25 #include <cassert>
     26 
     27 #include <algorithm>
     28 #include <complex>
     29 #include <string>
     30 
     31 using std::experimental::dynarray;
     32 
     33 template <class T>
     34 void test ( const std::initializer_list<T> &vals ) {
     35     typedef dynarray<T> dynA;
     36 
     37     dynA d1 ( vals );
     38     assert ( d1.size () == vals.size() );
     39     assert ( std::equal ( vals.begin (), vals.end (), d1.begin (), d1.end ()));
     40     }
     41 
     42 
     43 template <class T>
     44 void test ( const T &val ) {
     45     typedef dynarray<T> dynA;
     46 
     47     dynA d1 ( 4 );
     48     assert ( d1.size () == 4 );
     49     assert ( std::all_of ( d1.begin (), d1.end (), []( const T &item ){ return item == T(); } ));
     50 
     51     dynA d2 ( 7, val );
     52     assert ( d2.size () == 7 );
     53     assert ( std::all_of ( d2.begin (), d2.end (), [&val]( const T &item ){ return item == val; } ));
     54 
     55     dynA d3 ( d2 );
     56     assert ( d3.size () == 7 );
     57     assert ( std::all_of ( d3.begin (), d3.end (), [&val]( const T &item ){ return item == val; } ));
     58     }
     59 
     60 void test_bad_length () {
     61     try { dynarray<int> ( std::numeric_limits<size_t>::max() / sizeof ( int ) + 1 ); }
     62     catch ( std::bad_array_length & ) { return ; }
     63     assert ( false );
     64     }
     65 
     66 void test_bad_alloc () {
     67     try { dynarray<int> ( std::numeric_limits<size_t>::max() / sizeof ( int ) - 1 ); }
     68     catch ( std::bad_alloc & ) { return ; }
     69     assert ( false );
     70     }
     71 
     72 int main()
     73 {
     74 //  test<int> ( 14 );       // ints don't get default initialized
     75     test<long> ( 0 );
     76     test<double> ( 14.0 );
     77     test<std::complex<double>> ( std::complex<double> ( 14, 0 ));
     78     test<std::string> ( "fourteen" );
     79 
     80     test ( { 1, 1, 2, 3, 5, 8 } );
     81     test ( { 1., 1., 2., 3., 5., 8. } );
     82     test ( { std::string("1"), std::string("1"), std::string("2"), std::string("3"),
     83                 std::string("5"), std::string("8")} );
     84 
     85 //  Make sure we don't pick up the Allocator version here
     86     dynarray<long> d1 ( 20, 3 );
     87     assert ( d1.size() == 20 );
     88     assert ( std::all_of ( d1.begin (), d1.end (), []( long item ){ return item == 3L; } ));
     89 
     90     test_bad_length ();
     91     test_bad_alloc ();
     92 }
     93 #else
     94 int main() {}
     95 #endif
     96