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