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