Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MPLTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // This test requires C++11 (for static_assert), so why not use the
11 // standard type traits
12 #include <type_traits>
13 #include <utility>
18 
19 #include "Sacado_mpl_vector.hpp"
20 #include "Sacado_mpl_find.hpp"
21 #include "Sacado_mpl_size.hpp"
22 
23 // These tests are all compile-time tests, so if the test compiles,
24 // it passes...
25 
26 TEUCHOS_UNIT_TEST( MPL, Vector )
27 {
28  using std::is_same;
29  using Sacado::mpl::vector;
30  using Sacado::mpl::size;
32  using Sacado::mpl::at;
33  using Sacado::mpl::find;
34 
35  // Some types to stick into a vector -- make some of these non-class types
36  struct T1 {};
37  typedef int T2;
38  struct T3 {};
39  struct T4 {};
40  typedef char T5;
41 
42  // length-0 vector
43  typedef vector<> vec0;
44  static_assert( size<vec0>::value == 0, "" );
45 
46  // length-1 vector -- test push_back, at, size
47  typedef typename push_back<vec0,T1>::type vec1;
48  static_assert( is_same< vec1, vector<T1> >::value, "");
49  static_assert( is_same< at<vec1,0>::type, T1>::value, "");
50  static_assert( size<vec1>::value == 1, "" );
51 
52  // length-2 vector -- test push_back, at, size
53  typedef typename push_back<vec1,T2>::type vec2;
54  static_assert( is_same< vec2, vector<T1,T2> >::value, "");
55  static_assert( is_same< at<vec2,0>::type, T1>::value, "");
56  static_assert( is_same< at<vec2,1>::type, T2>::value, "");
57  static_assert( size<vec2>::value == 2, "" );
58 
59  // length-3 vector -- test push_back, at, size
60  typedef typename push_back<vec2,T3>::type vec3;
61  static_assert( is_same< vec3, vector<T1,T2,T3> >::value, "");
62  static_assert( is_same< at<vec3,0>::type, T1>::value, "");
63  static_assert( is_same< at<vec3,1>::type, T2>::value, "");
64  static_assert( is_same< at<vec3,2>::type, T3>::value, "");
65  static_assert( size<vec3>::value == 3, "" );
66 
67  // length-4 vector -- test push_back, at, size
68  typedef typename push_back<vec3,T4>::type vec4;
69  static_assert( is_same< vec4, vector<T1,T2,T3,T4> >::value, "");
70  static_assert( is_same< at<vec4,0>::type, T1>::value, "");
71  static_assert( is_same< at<vec4,1>::type, T2>::value, "");
72  static_assert( is_same< at<vec4,2>::type, T3>::value, "");
73  static_assert( is_same< at<vec4,3>::type, T4>::value, "");
74  static_assert( size<vec4>::value == 4, "" );
75 
76  // length-5 vector -- test push_back, at, size
77  typedef typename push_back<vec4,T5>::type vec5;
78  static_assert( is_same< vec5, vector<T1,T2,T3,T4,T5> >::value, "");
79  static_assert( is_same< at<vec5,0>::type, T1>::value, "");
80  static_assert( is_same< at<vec5,1>::type, T2>::value, "");
81  static_assert( is_same< at<vec5,2>::type, T3>::value, "");
82  static_assert( is_same< at<vec5,3>::type, T4>::value, "");
83  static_assert( is_same< at<vec5,4>::type, T5>::value, "");
84  static_assert( size<vec5>::value == 5, "" );
85 
86  // length-6 vector -- test push_back, at, size
87  typedef typename push_back<vec5,T5>::type vec6;
88  static_assert( is_same< vec6, vector<T1,T2,T3,T4,T5,T5> >::value, "");
89  static_assert( is_same< at<vec6,0>::type, T1>::value, "");
90  static_assert( is_same< at<vec6,1>::type, T2>::value, "");
91  static_assert( is_same< at<vec6,2>::type, T3>::value, "");
92  static_assert( is_same< at<vec6,3>::type, T4>::value, "");
93  static_assert( is_same< at<vec6,4>::type, T5>::value, "");
94  static_assert( is_same< at<vec6,5>::type, T5>::value, "");
95  static_assert( size<vec6>::value == 6, "" );
96 
97  // length-7 vector -- test push_back, at, size
98  typedef typename push_back<vec6,T4>::type vec7;
99  static_assert( is_same< vec7, vector<T1,T2,T3,T4,T5,T5,T4> >::value, "");
100  static_assert( is_same< at<vec7,0>::type, T1>::value, "");
101  static_assert( is_same< at<vec7,1>::type, T2>::value, "");
102  static_assert( is_same< at<vec7,2>::type, T3>::value, "");
103  static_assert( is_same< at<vec7,3>::type, T4>::value, "");
104  static_assert( is_same< at<vec7,4>::type, T5>::value, "");
105  static_assert( is_same< at<vec7,5>::type, T5>::value, "");
106  static_assert( is_same< at<vec7,6>::type, T4>::value, "");
107  static_assert( size<vec7>::value == 7, "" );
108 
109  // length-8 vector -- test push_back, at, size
110  typedef typename push_back<vec7,T3>::type vec8;
111  static_assert( is_same< vec8, vector<T1,T2,T3,T4,T5,T5,T4,T3> >::value, "");
112  static_assert( is_same< vec7, vector<T1,T2,T3,T4,T5,T5,T4> >::value, "");
113  static_assert( is_same< at<vec8,0>::type, T1>::value, "");
114  static_assert( is_same< at<vec8,1>::type, T2>::value, "");
115  static_assert( is_same< at<vec8,2>::type, T3>::value, "");
116  static_assert( is_same< at<vec8,3>::type, T4>::value, "");
117  static_assert( is_same< at<vec8,4>::type, T5>::value, "");
118  static_assert( is_same< at<vec8,5>::type, T5>::value, "");
119  static_assert( is_same< at<vec8,6>::type, T4>::value, "");
120  static_assert( is_same< at<vec8,7>::type, T3>::value, "");
121  static_assert( size<vec8>::value == 8, "" );
122 
123  // length-9 vector -- test push_back, at, size
124  typedef typename push_back<vec8,T2>::type vec9;
125  static_assert( is_same< vec9, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2> >::value, "");
126  static_assert( is_same< at<vec9,0>::type, T1>::value, "");
127  static_assert( is_same< at<vec9,1>::type, T2>::value, "");
128  static_assert( is_same< at<vec9,2>::type, T3>::value, "");
129  static_assert( is_same< at<vec9,3>::type, T4>::value, "");
130  static_assert( is_same< at<vec9,4>::type, T5>::value, "");
131  static_assert( is_same< at<vec9,5>::type, T5>::value, "");
132  static_assert( is_same< at<vec9,6>::type, T4>::value, "");
133  static_assert( is_same< at<vec9,7>::type, T3>::value, "");
134  static_assert( is_same< at<vec9,8>::type, T2>::value, "");
135  static_assert( size<vec9>::value == 9, "" );
136 
137  // length-10 vector -- test push_back, at, size
138  typedef typename push_back<vec9,T1>::type vec10;
139  static_assert( is_same< vec10, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1> >::value, "");
140  static_assert( is_same< at<vec10,0>::type, T1>::value, "");
141  static_assert( is_same< at<vec10,1>::type, T2>::value, "");
142  static_assert( is_same< at<vec10,2>::type, T3>::value, "");
143  static_assert( is_same< at<vec10,3>::type, T4>::value, "");
144  static_assert( is_same< at<vec10,4>::type, T5>::value, "");
145  static_assert( is_same< at<vec10,5>::type, T5>::value, "");
146  static_assert( is_same< at<vec10,6>::type, T4>::value, "");
147  static_assert( is_same< at<vec10,7>::type, T3>::value, "");
148  static_assert( is_same< at<vec10,8>::type, T2>::value, "");
149  static_assert( is_same< at<vec10,9>::type, T1>::value, "");
150  static_assert( size<vec10>::value == 10, "" );
151 
152  // length-11 vector -- test push_back, at, size
153  typedef typename push_back<vec10,T1>::type vec11;
154  static_assert( is_same< vec11, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1> >::value, "");
155  static_assert( is_same< at<vec11,0>::type, T1>::value, "");
156  static_assert( is_same< at<vec11,1>::type, T2>::value, "");
157  static_assert( is_same< at<vec11,2>::type, T3>::value, "");
158  static_assert( is_same< at<vec11,3>::type, T4>::value, "");
159  static_assert( is_same< at<vec11,4>::type, T5>::value, "");
160  static_assert( is_same< at<vec11,5>::type, T5>::value, "");
161  static_assert( is_same< at<vec11,6>::type, T4>::value, "");
162  static_assert( is_same< at<vec11,7>::type, T3>::value, "");
163  static_assert( is_same< at<vec11,8>::type, T2>::value, "");
164  static_assert( is_same< at<vec11,9>::type, T1>::value, "");
165  static_assert( is_same< at<vec11,10>::type, T1>::value, "");
166  static_assert( size<vec11>::value == 11, "" );
167 
168  // length-12 vector -- test push_back, at, size
169  typedef typename push_back<vec11,T2>::type vec12;
170  static_assert( is_same< vec12, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2> >::value, "");
171  static_assert( is_same< at<vec12,0>::type, T1>::value, "");
172  static_assert( is_same< at<vec12,1>::type, T2>::value, "");
173  static_assert( is_same< at<vec12,2>::type, T3>::value, "");
174  static_assert( is_same< at<vec12,3>::type, T4>::value, "");
175  static_assert( is_same< at<vec12,4>::type, T5>::value, "");
176  static_assert( is_same< at<vec12,5>::type, T5>::value, "");
177  static_assert( is_same< at<vec12,6>::type, T4>::value, "");
178  static_assert( is_same< at<vec12,7>::type, T3>::value, "");
179  static_assert( is_same< at<vec12,8>::type, T2>::value, "");
180  static_assert( is_same< at<vec12,9>::type, T1>::value, "");
181  static_assert( is_same< at<vec12,10>::type, T1>::value, "");
182  static_assert( is_same< at<vec12,11>::type, T2>::value, "");
183  static_assert( size<vec12>::value == 12, "" );
184 
185  // length-13 vector -- test push_back, at, size
186  typedef typename push_back<vec12,T3>::type vec13;
187  static_assert( is_same< vec13, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3> >::value, "");
188  static_assert( is_same< at<vec13,0>::type, T1>::value, "");
189  static_assert( is_same< at<vec13,1>::type, T2>::value, "");
190  static_assert( is_same< at<vec13,2>::type, T3>::value, "");
191  static_assert( is_same< at<vec13,3>::type, T4>::value, "");
192  static_assert( is_same< at<vec13,4>::type, T5>::value, "");
193  static_assert( is_same< at<vec13,5>::type, T5>::value, "");
194  static_assert( is_same< at<vec13,6>::type, T4>::value, "");
195  static_assert( is_same< at<vec13,7>::type, T3>::value, "");
196  static_assert( is_same< at<vec13,8>::type, T2>::value, "");
197  static_assert( is_same< at<vec13,9>::type, T1>::value, "");
198  static_assert( is_same< at<vec13,10>::type, T1>::value, "");
199  static_assert( is_same< at<vec13,11>::type, T2>::value, "");
200  static_assert( is_same< at<vec13,12>::type, T3>::value, "");
201  static_assert( size<vec13>::value == 13, "" );
202 
203  // length-14 vector -- test push_back, at, size
204  typedef typename push_back<vec13,T4>::type vec14;
205  static_assert( is_same< vec14, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3,T4> >::value, "");
206  static_assert( is_same< at<vec14,0>::type, T1>::value, "");
207  static_assert( is_same< at<vec14,1>::type, T2>::value, "");
208  static_assert( is_same< at<vec14,2>::type, T3>::value, "");
209  static_assert( is_same< at<vec14,3>::type, T4>::value, "");
210  static_assert( is_same< at<vec14,4>::type, T5>::value, "");
211  static_assert( is_same< at<vec14,5>::type, T5>::value, "");
212  static_assert( is_same< at<vec14,6>::type, T4>::value, "");
213  static_assert( is_same< at<vec14,7>::type, T3>::value, "");
214  static_assert( is_same< at<vec14,8>::type, T2>::value, "");
215  static_assert( is_same< at<vec14,9>::type, T1>::value, "");
216  static_assert( is_same< at<vec14,10>::type, T1>::value, "");
217  static_assert( is_same< at<vec14,11>::type, T2>::value, "");
218  static_assert( is_same< at<vec14,12>::type, T3>::value, "");
219  static_assert( is_same< at<vec14,13>::type, T4>::value, "");
220  static_assert( size<vec14>::value == 14, "" );
221 
222  // length-15 vector -- test push_back, at, size
223  typedef typename push_back<vec14,T5>::type vec15;
224  static_assert( is_same< vec15, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3,T4,T5> >::value, "");
225  static_assert( is_same< at<vec15,0>::type, T1>::value, "");
226  static_assert( is_same< at<vec15,1>::type, T2>::value, "");
227  static_assert( is_same< at<vec15,2>::type, T3>::value, "");
228  static_assert( is_same< at<vec15,3>::type, T4>::value, "");
229  static_assert( is_same< at<vec15,4>::type, T5>::value, "");
230  static_assert( is_same< at<vec15,5>::type, T5>::value, "");
231  static_assert( is_same< at<vec15,6>::type, T4>::value, "");
232  static_assert( is_same< at<vec15,7>::type, T3>::value, "");
233  static_assert( is_same< at<vec15,8>::type, T2>::value, "");
234  static_assert( is_same< at<vec15,9>::type, T1>::value, "");
235  static_assert( is_same< at<vec15,10>::type, T1>::value, "");
236  static_assert( is_same< at<vec15,11>::type, T2>::value, "");
237  static_assert( is_same< at<vec15,12>::type, T3>::value, "");
238  static_assert( is_same< at<vec15,13>::type, T4>::value, "");
239  static_assert( is_same< at<vec15,14>::type, T5>::value, "");
240  static_assert( size<vec15>::value == 15, "" );
241 
242  // The implementation now uses variatic templates, so there
243  // is no hard limit on the length of mpl::vector. However
244  // sizes up to 15 still seems like a good length to test.
245 
246  // Check find
247  static_assert( Sacado::mpl::find<vec5, T1>::value == 0, "" );
248  static_assert( Sacado::mpl::find<vec5, T2>::value == 1, "" );
249  static_assert( Sacado::mpl::find<vec5, T3>::value == 2, "" );
250  static_assert( Sacado::mpl::find<vec5, T4>::value == 3, "" );
251  static_assert( Sacado::mpl::find<vec5, T5>::value == 4, "" );
252  static_assert( Sacado::mpl::find<vec10, T1>::value == 0, "" );
253  static_assert( Sacado::mpl::find<vec10, T2>::value == 1, "" );
254  static_assert( Sacado::mpl::find<vec10, T3>::value == 2, "" );
255  static_assert( Sacado::mpl::find<vec10, T4>::value == 3, "" );
256  static_assert( Sacado::mpl::find<vec10, T5>::value == 4, "" );
257  static_assert( Sacado::mpl::find<vec15, T1>::value == 0, "" );
258  static_assert( Sacado::mpl::find<vec15, T2>::value == 1, "" );
259  static_assert( Sacado::mpl::find<vec15, T3>::value == 2, "" );
260  static_assert( Sacado::mpl::find<vec15, T4>::value == 3, "" );
261  static_assert( Sacado::mpl::find<vec15, T5>::value == 4, "" );
262 
263  success = true;
264 }
265 
266 int main( int argc, char* argv[] ) {
267  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
269 }
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
static int runUnitTestsFromMain(int argc, char *argv[])
#define T2(r, f)
Definition: Sacado_rad.hpp:558
int main()
Definition: ad_example.cpp:171
#define T1(r, f)
Definition: Sacado_rad.hpp:583
int value