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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 // This test requires C++11 (for static_assert), so why not use the
31 // standard type traits
32 #include <type_traits>
33 #include <utility>
38 
39 #include "Sacado_mpl_vector.hpp"
40 #include "Sacado_mpl_find.hpp"
41 #include "Sacado_mpl_size.hpp"
42 
43 // These tests are all compile-time tests, so if the test compiles,
44 // it passes...
45 
46 TEUCHOS_UNIT_TEST( MPL, Vector )
47 {
48  using std::is_same;
49  using Sacado::mpl::vector;
50  using Sacado::mpl::size;
52  using Sacado::mpl::at;
53  using Sacado::mpl::find;
54 
55  // Some types to stick into a vector -- make some of these non-class types
56  struct T1 {};
57  typedef int T2;
58  struct T3 {};
59  struct T4 {};
60  typedef char T5;
61 
62  // length-0 vector
63  typedef vector<> vec0;
64  static_assert( size<vec0>::value == 0, "" );
65 
66  // length-1 vector -- test push_back, at, size
67  typedef typename push_back<vec0,T1>::type vec1;
68  static_assert( is_same< vec1, vector<T1> >::value, "");
69  static_assert( is_same< at<vec1,0>::type, T1>::value, "");
70  static_assert( size<vec1>::value == 1, "" );
71 
72  // length-2 vector -- test push_back, at, size
73  typedef typename push_back<vec1,T2>::type vec2;
74  static_assert( is_same< vec2, vector<T1,T2> >::value, "");
75  static_assert( is_same< at<vec2,0>::type, T1>::value, "");
76  static_assert( is_same< at<vec2,1>::type, T2>::value, "");
77  static_assert( size<vec2>::value == 2, "" );
78 
79  // length-3 vector -- test push_back, at, size
80  typedef typename push_back<vec2,T3>::type vec3;
81  static_assert( is_same< vec3, vector<T1,T2,T3> >::value, "");
82  static_assert( is_same< at<vec3,0>::type, T1>::value, "");
83  static_assert( is_same< at<vec3,1>::type, T2>::value, "");
84  static_assert( is_same< at<vec3,2>::type, T3>::value, "");
85  static_assert( size<vec3>::value == 3, "" );
86 
87  // length-4 vector -- test push_back, at, size
88  typedef typename push_back<vec3,T4>::type vec4;
89  static_assert( is_same< vec4, vector<T1,T2,T3,T4> >::value, "");
90  static_assert( is_same< at<vec4,0>::type, T1>::value, "");
91  static_assert( is_same< at<vec4,1>::type, T2>::value, "");
92  static_assert( is_same< at<vec4,2>::type, T3>::value, "");
93  static_assert( is_same< at<vec4,3>::type, T4>::value, "");
94  static_assert( size<vec4>::value == 4, "" );
95 
96  // length-5 vector -- test push_back, at, size
97  typedef typename push_back<vec4,T5>::type vec5;
98  static_assert( is_same< vec5, vector<T1,T2,T3,T4,T5> >::value, "");
99  static_assert( is_same< at<vec5,0>::type, T1>::value, "");
100  static_assert( is_same< at<vec5,1>::type, T2>::value, "");
101  static_assert( is_same< at<vec5,2>::type, T3>::value, "");
102  static_assert( is_same< at<vec5,3>::type, T4>::value, "");
103  static_assert( is_same< at<vec5,4>::type, T5>::value, "");
104  static_assert( size<vec5>::value == 5, "" );
105 
106  // length-6 vector -- test push_back, at, size
107  typedef typename push_back<vec5,T5>::type vec6;
108  static_assert( is_same< vec6, vector<T1,T2,T3,T4,T5,T5> >::value, "");
109  static_assert( is_same< at<vec6,0>::type, T1>::value, "");
110  static_assert( is_same< at<vec6,1>::type, T2>::value, "");
111  static_assert( is_same< at<vec6,2>::type, T3>::value, "");
112  static_assert( is_same< at<vec6,3>::type, T4>::value, "");
113  static_assert( is_same< at<vec6,4>::type, T5>::value, "");
114  static_assert( is_same< at<vec6,5>::type, T5>::value, "");
115  static_assert( size<vec6>::value == 6, "" );
116 
117  // length-7 vector -- test push_back, at, size
118  typedef typename push_back<vec6,T4>::type vec7;
119  static_assert( is_same< vec7, vector<T1,T2,T3,T4,T5,T5,T4> >::value, "");
120  static_assert( is_same< at<vec7,0>::type, T1>::value, "");
121  static_assert( is_same< at<vec7,1>::type, T2>::value, "");
122  static_assert( is_same< at<vec7,2>::type, T3>::value, "");
123  static_assert( is_same< at<vec7,3>::type, T4>::value, "");
124  static_assert( is_same< at<vec7,4>::type, T5>::value, "");
125  static_assert( is_same< at<vec7,5>::type, T5>::value, "");
126  static_assert( is_same< at<vec7,6>::type, T4>::value, "");
127  static_assert( size<vec7>::value == 7, "" );
128 
129  // length-8 vector -- test push_back, at, size
130  typedef typename push_back<vec7,T3>::type vec8;
131  static_assert( is_same< vec8, vector<T1,T2,T3,T4,T5,T5,T4,T3> >::value, "");
132  static_assert( is_same< vec7, vector<T1,T2,T3,T4,T5,T5,T4> >::value, "");
133  static_assert( is_same< at<vec8,0>::type, T1>::value, "");
134  static_assert( is_same< at<vec8,1>::type, T2>::value, "");
135  static_assert( is_same< at<vec8,2>::type, T3>::value, "");
136  static_assert( is_same< at<vec8,3>::type, T4>::value, "");
137  static_assert( is_same< at<vec8,4>::type, T5>::value, "");
138  static_assert( is_same< at<vec8,5>::type, T5>::value, "");
139  static_assert( is_same< at<vec8,6>::type, T4>::value, "");
140  static_assert( is_same< at<vec8,7>::type, T3>::value, "");
141  static_assert( size<vec8>::value == 8, "" );
142 
143  // length-9 vector -- test push_back, at, size
144  typedef typename push_back<vec8,T2>::type vec9;
145  static_assert( is_same< vec9, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2> >::value, "");
146  static_assert( is_same< at<vec9,0>::type, T1>::value, "");
147  static_assert( is_same< at<vec9,1>::type, T2>::value, "");
148  static_assert( is_same< at<vec9,2>::type, T3>::value, "");
149  static_assert( is_same< at<vec9,3>::type, T4>::value, "");
150  static_assert( is_same< at<vec9,4>::type, T5>::value, "");
151  static_assert( is_same< at<vec9,5>::type, T5>::value, "");
152  static_assert( is_same< at<vec9,6>::type, T4>::value, "");
153  static_assert( is_same< at<vec9,7>::type, T3>::value, "");
154  static_assert( is_same< at<vec9,8>::type, T2>::value, "");
155  static_assert( size<vec9>::value == 9, "" );
156 
157  // length-10 vector -- test push_back, at, size
158  typedef typename push_back<vec9,T1>::type vec10;
159  static_assert( is_same< vec10, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1> >::value, "");
160  static_assert( is_same< at<vec10,0>::type, T1>::value, "");
161  static_assert( is_same< at<vec10,1>::type, T2>::value, "");
162  static_assert( is_same< at<vec10,2>::type, T3>::value, "");
163  static_assert( is_same< at<vec10,3>::type, T4>::value, "");
164  static_assert( is_same< at<vec10,4>::type, T5>::value, "");
165  static_assert( is_same< at<vec10,5>::type, T5>::value, "");
166  static_assert( is_same< at<vec10,6>::type, T4>::value, "");
167  static_assert( is_same< at<vec10,7>::type, T3>::value, "");
168  static_assert( is_same< at<vec10,8>::type, T2>::value, "");
169  static_assert( is_same< at<vec10,9>::type, T1>::value, "");
170  static_assert( size<vec10>::value == 10, "" );
171 
172  // length-11 vector -- test push_back, at, size
173  typedef typename push_back<vec10,T1>::type vec11;
174  static_assert( is_same< vec11, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1> >::value, "");
175  static_assert( is_same< at<vec11,0>::type, T1>::value, "");
176  static_assert( is_same< at<vec11,1>::type, T2>::value, "");
177  static_assert( is_same< at<vec11,2>::type, T3>::value, "");
178  static_assert( is_same< at<vec11,3>::type, T4>::value, "");
179  static_assert( is_same< at<vec11,4>::type, T5>::value, "");
180  static_assert( is_same< at<vec11,5>::type, T5>::value, "");
181  static_assert( is_same< at<vec11,6>::type, T4>::value, "");
182  static_assert( is_same< at<vec11,7>::type, T3>::value, "");
183  static_assert( is_same< at<vec11,8>::type, T2>::value, "");
184  static_assert( is_same< at<vec11,9>::type, T1>::value, "");
185  static_assert( is_same< at<vec11,10>::type, T1>::value, "");
186  static_assert( size<vec11>::value == 11, "" );
187 
188  // length-12 vector -- test push_back, at, size
189  typedef typename push_back<vec11,T2>::type vec12;
190  static_assert( is_same< vec12, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2> >::value, "");
191  static_assert( is_same< at<vec12,0>::type, T1>::value, "");
192  static_assert( is_same< at<vec12,1>::type, T2>::value, "");
193  static_assert( is_same< at<vec12,2>::type, T3>::value, "");
194  static_assert( is_same< at<vec12,3>::type, T4>::value, "");
195  static_assert( is_same< at<vec12,4>::type, T5>::value, "");
196  static_assert( is_same< at<vec12,5>::type, T5>::value, "");
197  static_assert( is_same< at<vec12,6>::type, T4>::value, "");
198  static_assert( is_same< at<vec12,7>::type, T3>::value, "");
199  static_assert( is_same< at<vec12,8>::type, T2>::value, "");
200  static_assert( is_same< at<vec12,9>::type, T1>::value, "");
201  static_assert( is_same< at<vec12,10>::type, T1>::value, "");
202  static_assert( is_same< at<vec12,11>::type, T2>::value, "");
203  static_assert( size<vec12>::value == 12, "" );
204 
205  // length-13 vector -- test push_back, at, size
206  typedef typename push_back<vec12,T3>::type vec13;
207  static_assert( is_same< vec13, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3> >::value, "");
208  static_assert( is_same< at<vec13,0>::type, T1>::value, "");
209  static_assert( is_same< at<vec13,1>::type, T2>::value, "");
210  static_assert( is_same< at<vec13,2>::type, T3>::value, "");
211  static_assert( is_same< at<vec13,3>::type, T4>::value, "");
212  static_assert( is_same< at<vec13,4>::type, T5>::value, "");
213  static_assert( is_same< at<vec13,5>::type, T5>::value, "");
214  static_assert( is_same< at<vec13,6>::type, T4>::value, "");
215  static_assert( is_same< at<vec13,7>::type, T3>::value, "");
216  static_assert( is_same< at<vec13,8>::type, T2>::value, "");
217  static_assert( is_same< at<vec13,9>::type, T1>::value, "");
218  static_assert( is_same< at<vec13,10>::type, T1>::value, "");
219  static_assert( is_same< at<vec13,11>::type, T2>::value, "");
220  static_assert( is_same< at<vec13,12>::type, T3>::value, "");
221  static_assert( size<vec13>::value == 13, "" );
222 
223  // length-14 vector -- test push_back, at, size
224  typedef typename push_back<vec13,T4>::type vec14;
225  static_assert( is_same< vec14, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3,T4> >::value, "");
226  static_assert( is_same< at<vec14,0>::type, T1>::value, "");
227  static_assert( is_same< at<vec14,1>::type, T2>::value, "");
228  static_assert( is_same< at<vec14,2>::type, T3>::value, "");
229  static_assert( is_same< at<vec14,3>::type, T4>::value, "");
230  static_assert( is_same< at<vec14,4>::type, T5>::value, "");
231  static_assert( is_same< at<vec14,5>::type, T5>::value, "");
232  static_assert( is_same< at<vec14,6>::type, T4>::value, "");
233  static_assert( is_same< at<vec14,7>::type, T3>::value, "");
234  static_assert( is_same< at<vec14,8>::type, T2>::value, "");
235  static_assert( is_same< at<vec14,9>::type, T1>::value, "");
236  static_assert( is_same< at<vec14,10>::type, T1>::value, "");
237  static_assert( is_same< at<vec14,11>::type, T2>::value, "");
238  static_assert( is_same< at<vec14,12>::type, T3>::value, "");
239  static_assert( is_same< at<vec14,13>::type, T4>::value, "");
240  static_assert( size<vec14>::value == 14, "" );
241 
242  // length-15 vector -- test push_back, at, size
243  typedef typename push_back<vec14,T5>::type vec15;
244  static_assert( is_same< vec15, vector<T1,T2,T3,T4,T5,T5,T4,T3,T2,T1,T1,T2,T3,T4,T5> >::value, "");
245  static_assert( is_same< at<vec15,0>::type, T1>::value, "");
246  static_assert( is_same< at<vec15,1>::type, T2>::value, "");
247  static_assert( is_same< at<vec15,2>::type, T3>::value, "");
248  static_assert( is_same< at<vec15,3>::type, T4>::value, "");
249  static_assert( is_same< at<vec15,4>::type, T5>::value, "");
250  static_assert( is_same< at<vec15,5>::type, T5>::value, "");
251  static_assert( is_same< at<vec15,6>::type, T4>::value, "");
252  static_assert( is_same< at<vec15,7>::type, T3>::value, "");
253  static_assert( is_same< at<vec15,8>::type, T2>::value, "");
254  static_assert( is_same< at<vec15,9>::type, T1>::value, "");
255  static_assert( is_same< at<vec15,10>::type, T1>::value, "");
256  static_assert( is_same< at<vec15,11>::type, T2>::value, "");
257  static_assert( is_same< at<vec15,12>::type, T3>::value, "");
258  static_assert( is_same< at<vec15,13>::type, T4>::value, "");
259  static_assert( is_same< at<vec15,14>::type, T5>::value, "");
260  static_assert( size<vec15>::value == 15, "" );
261 
262  // The implementation now uses variatic templates, so there
263  // is no hard limit on the length of mpl::vector. However
264  // sizes up to 15 still seems like a good length to test.
265 
266  // Check find
267  static_assert( Sacado::mpl::find<vec5, T1>::value == 0, "" );
268  static_assert( Sacado::mpl::find<vec5, T2>::value == 1, "" );
269  static_assert( Sacado::mpl::find<vec5, T3>::value == 2, "" );
270  static_assert( Sacado::mpl::find<vec5, T4>::value == 3, "" );
271  static_assert( Sacado::mpl::find<vec5, T5>::value == 4, "" );
272  static_assert( Sacado::mpl::find<vec10, T1>::value == 0, "" );
273  static_assert( Sacado::mpl::find<vec10, T2>::value == 1, "" );
274  static_assert( Sacado::mpl::find<vec10, T3>::value == 2, "" );
275  static_assert( Sacado::mpl::find<vec10, T4>::value == 3, "" );
276  static_assert( Sacado::mpl::find<vec10, T5>::value == 4, "" );
277  static_assert( Sacado::mpl::find<vec15, T1>::value == 0, "" );
278  static_assert( Sacado::mpl::find<vec15, T2>::value == 1, "" );
279  static_assert( Sacado::mpl::find<vec15, T3>::value == 2, "" );
280  static_assert( Sacado::mpl::find<vec15, T4>::value == 3, "" );
281  static_assert( Sacado::mpl::find<vec15, T5>::value == 4, "" );
282 
283  success = true;
284 }
285 
286 int main( int argc, char* argv[] ) {
287  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
289 }
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
static int runUnitTestsFromMain(int argc, char *argv[])
#define T2(r, f)
Definition: Sacado_rad.hpp:578
int main()
Definition: ad_example.cpp:191
#define T1(r, f)
Definition: Sacado_rad.hpp:603
int value