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.overview
11
12 // const_reference at(size_type n) const;
13 // reference at(size_type n);
14
15 #include <__config>
16
17 #if _LIBCPP_STD_VER > 11
18
19 #include <experimental/dynarray>
20 #include <cassert>
21
22 #include <algorithm>
23 #include <complex>
24 #include <string>
25
26 using std::experimental::dynarray;
27
28 template <class T>
dyn_at_fail(dynarray<T> & dyn,size_t sz)29 void dyn_at_fail ( dynarray<T> &dyn, size_t sz ) {
30 try { dyn.at (sz); }
31 catch (const std::out_of_range &) { return; }
32 assert ( false );
33 }
34
35 template <class T>
dyn_at_fail_const(const dynarray<T> & dyn,size_t sz)36 void dyn_at_fail_const ( const dynarray<T> &dyn, size_t sz ) {
37 try { dyn.at (sz); }
38 catch (const std::out_of_range &) { return; }
39 assert ( false );
40 }
41
42
43 template <class T>
dyn_test_const(const dynarray<T> & dyn,const std::initializer_list<T> & vals)44 void dyn_test_const ( const dynarray<T> &dyn, const std::initializer_list<T> &vals ) {
45 const T *data = dyn.data ();
46 auto it = vals.begin ();
47 for ( size_t i = 0; i < dyn.size(); ++i, ++it ) {
48 assert ( data + i == &dyn.at(i));
49 assert ( *it == dyn.at(i));
50 }
51
52 dyn_at_fail_const ( dyn, dyn.size ());
53 dyn_at_fail_const ( dyn, 2*dyn.size ());
54 dyn_at_fail_const ( dyn, size_t (-1));
55 }
56
57 template <class T>
dyn_test(dynarray<T> & dyn,const std::initializer_list<T> & vals)58 void dyn_test ( dynarray<T> &dyn, const std::initializer_list<T> &vals ) {
59 T *data = dyn.data ();
60 auto it = vals.begin ();
61 for ( size_t i = 0; i < dyn.size(); ++i, ++it ) {
62 assert ( data + i == &dyn.at(i));
63 assert ( *it == dyn.at(i));
64 }
65
66 dyn_at_fail ( dyn, dyn.size ());
67 dyn_at_fail ( dyn, 2*dyn.size ());
68 dyn_at_fail ( dyn, size_t (-1));
69 }
70
71
72 template <class T>
test(std::initializer_list<T> vals)73 void test ( std::initializer_list<T> vals ) {
74 typedef dynarray<T> dynA;
75
76 dynA d1 ( vals );
77 dyn_test ( d1, vals );
78 dyn_test_const ( d1, vals );
79 }
80
main()81 int main()
82 {
83 test ( { 1, 1, 2, 3, 5, 8 } );
84 test ( { 1., 1., 2., 3., 5., 8. } );
85 test ( { std::string("1"), std::string("1"), std::string("2"), std::string("3"),
86 std::string("5"), std::string("8")} );
87
88 test<int> ( {} );
89 test<std::complex<double>> ( {} );
90 test<std::string> ( {} );
91 }
92 #else
main()93 int main() {}
94 #endif
95