Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tuple_test.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_Tuple.hpp"
15 #include "Teuchos_Version.hpp"
16 #include "Teuchos_getConst.hpp"
17 #include "Teuchos_as.hpp"
19 
20 
21 // Uncomment to show compile errors from invalid usage
22 //#define SHOW_INVALID_COPY_CONSTRUCTION
23 //#define SHOW_INVALID_CONST_ASSIGN
24 //#define SHOW_INVALID_CONST_ITER_MODIFICATION
25 
26 //
27 // Define local macros to make defining tests easier for this particular test
28 // code.
29 //
30 // Note, macros with these types of names should only exist in a *.cpp file
31 // after all #includes are done!
32 //
33 
34 
35 #define TEST_EQUALITY_CONST( v1, v2 ) \
36  TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
37 
38 #define TEST_EQUALITY( v1, v2 ) \
39  TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
40 
41 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
42  TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
43 
44 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
45  TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
46 
47 #define TEST_COMPARE( v1, comp, v2 ) \
48  TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
49 
50 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
51  { \
52  const bool result = compareArrays(a1,#a1,a2,#a2,out); \
53  if (!result) success = false; \
54  }
55 
56 #define TEST_THROW( code, ExceptType ) \
57  TEUCHOS_TEST_THROW( code, ExceptType, out, success )
58 
59 #define TEST_NOTHROW( code ) \
60  TEUCHOS_TEST_NOTHROW( code, out, success )
61 
62 
63 //
64 // Main templated array test function
65 //
66 
67 
68 template<class T, int N>
70 {
71 
72  using Teuchos::Tuple;
73  using Teuchos::tuple;
74  using Teuchos::ArrayView;
75  using Teuchos::arrayView;
76  using Teuchos::arrayViewFromVector;
77  using Teuchos::outArg;
80  using Teuchos::getConst;
81  using Teuchos::as;
82  typedef typename ArrayView<T>::size_type size_type;
83  // mfh 03 Apr 2014: The point of the above line of code is to ensure
84  // that ArrayView<T> has a public size_type typedef. However, the
85  // above line of code in isolation causes some compilers to warn
86  // about a declared but unused typedef. We deal with this by
87  // declaring a variable (actually, the oxymoron "const variable") of
88  // type size_type, then using the "cast to void" trick to forestall
89  // compiler warnings for the declared but unused variable. (Fun
90  // fact: "oxymoron" means "sharp dull" and is itself an oxymoron.)
91  // The "cast to void" trick doesn't always work, but if it doesn't,
92  // it's easy to make it go away by printing it to the output stream
93  // 'out'.
94  const size_type arbitrarySizeTypeValue = 0;
95  (void) arbitrarySizeTypeValue;
96 
97  bool success = true;
98 
99  out
100  << "\n***"
101  << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
102  << "\n***\n";
103 
104  Teuchos::OSTab tab(out);
105 
106  //
107  out << "\nA) Initial setup testing ...\n\n";
108  //
109 
110  Tuple<T,N> t;
111  TEST_EQUALITY_CONST(t.size(),N);
112  for( int i = 0; i < N; ++i )
113  t[i] = i; // tests non-const operator[](i)
114 
115  {
116  out << "\nTest that t[i] == i ... ";
117  const ArrayView<const T> cav2 = t;
118  bool local_success = true;
119  for( int i = 0; i < N; ++i ) {
120  TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
121  }
122  if (local_success) out << "passed\n";
123  else success = false;
124  }
125 
126  {
127  const int n = 1;
128  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
129  Tuple<T,n> tn = tuple<T>(0);
130  TEST_EQUALITY_CONST(tn.size(),n);
131  out << "Test that tn[i] == i ... ";
132  bool local_success = true;
133  for( int i = 0; i < n; ++i ) {
134  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
135  }
136  if (local_success) out << "passed\n";
137  else success = false;
138  }
139 
140  {
141  const int n = 2;
142  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
143  Tuple<T,n> tn = tuple<T>(0,1);
144  TEST_EQUALITY_CONST(tn.size(),n);
145  out << "Test that tn[i] == i ... ";
146  bool local_success = true;
147  for( int i = 0; i < n; ++i ) {
148  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
149  }
150  if (local_success) out << "passed\n";
151  else success = false;
152  }
153 
154  {
155  const int n = 3;
156  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
157  Tuple<T,n> tn = tuple<T>(0,1,2);
158  TEST_EQUALITY_CONST(tn.size(),n);
159  out << "Test that tn[i] == i ... ";
160  bool local_success = true;
161  for( int i = 0; i < n; ++i ) {
162  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
163  }
164  if (local_success) out << "passed\n";
165  else success = false;
166  }
167 
168  {
169  const int n = 4;
170  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
171  Tuple<T,n> tn = tuple<T>(0,1,2,3);
172  TEST_EQUALITY_CONST(tn.size(),n);
173  out << "Test that tn[i] == i ... ";
174  bool local_success = true;
175  for( int i = 0; i < n; ++i ) {
176  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
177  }
178  if (local_success) out << "passed\n";
179  else success = false;
180  }
181 
182  {
183  const int n = 5;
184  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
185  Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
186  TEST_EQUALITY_CONST(tn.size(),n);
187  out << "Test that tn[i] == i ... ";
188  bool local_success = true;
189  for( int i = 0; i < n; ++i ) {
190  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
191  }
192  if (local_success) out << "passed\n";
193  else success = false;
194  }
195 
196  {
197  const int n = 6;
198  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
199  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
200  TEST_EQUALITY_CONST(tn.size(),n);
201  out << "Test that tn[i] == i ... ";
202  bool local_success = true;
203  for( int i = 0; i < n; ++i ) {
204  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
205  }
206  if (local_success) out << "passed\n";
207  else success = false;
208  }
209 
210  {
211  const int n = 7;
212  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
213  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
214  TEST_EQUALITY_CONST(tn.size(),n);
215  out << "Test that tn[i] == i ... ";
216  bool local_success = true;
217  for( int i = 0; i < n; ++i ) {
218  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
219  }
220  if (local_success) out << "passed\n";
221  else success = false;
222  }
223 
224  {
225  const int n = 8;
226  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
227  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
228  TEST_EQUALITY_CONST(tn.size(),n);
229  out << "Test that tn[i] == i ... ";
230  bool local_success = true;
231  for( int i = 0; i < n; ++i ) {
232  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
233  }
234  if (local_success) out << "passed\n";
235  else success = false;
236  }
237 
238  {
239  const int n = 9;
240  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
241  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
242  TEST_EQUALITY_CONST(tn.size(),n);
243  out << "Test that tn[i] == i ... ";
244  bool local_success = true;
245  for( int i = 0; i < n; ++i ) {
246  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
247  }
248  if (local_success) out << "passed\n";
249  else success = false;
250  }
251 
252  {
253  const int n = 10;
254  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
255  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
256  TEST_EQUALITY_CONST(tn.size(),n);
257  out << "Test that tn[i] == i ... ";
258  bool local_success = true;
259  for( int i = 0; i < n; ++i ) {
260  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
261  }
262  if (local_success) out << "passed\n";
263  else success = false;
264  }
265 
266  {
267  const int n = 11;
268  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
269  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
270  TEST_EQUALITY_CONST(tn.size(),n);
271  out << "Test that tn[i] == i ... ";
272  bool local_success = true;
273  for( int i = 0; i < n; ++i ) {
274  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
275  }
276  if (local_success) out << "passed\n";
277  else success = false;
278  }
279 
280  {
281  const int n = 12;
282  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
283  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
284  TEST_EQUALITY_CONST(tn.size(),n);
285  out << "Test that tn[i] == i ... ";
286  bool local_success = true;
287  for( int i = 0; i < n; ++i ) {
288  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
289  }
290  if (local_success) out << "passed\n";
291  else success = false;
292  }
293 
294  {
295  const int n = 13;
296  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
297  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
298  TEST_EQUALITY_CONST(tn.size(),n);
299  out << "Test that tn[i] == i ... ";
300  bool local_success = true;
301  for( int i = 0; i < n; ++i ) {
302  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
303  }
304  if (local_success) out << "passed\n";
305  else success = false;
306  }
307 
308  {
309  const int n = 14;
310  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
311  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
312  TEST_EQUALITY_CONST(tn.size(),n);
313  out << "Test that tn[i] == i ... ";
314  bool local_success = true;
315  for( int i = 0; i < n; ++i ) {
316  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
317  }
318  if (local_success) out << "passed\n";
319  else success = false;
320  }
321 
322  {
323  const int n = 15;
324  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
325  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
326  TEST_EQUALITY_CONST(tn.size(),n);
327  out << "Test that tn[i] == i ... ";
328  bool local_success = true;
329  for( int i = 0; i < n; ++i ) {
330  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
331  }
332  if (local_success) out << "passed\n";
333  else success = false;
334  }
335 
336  {
337  out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
338  const ArrayView<const T> av2 = t;
339  TEST_COMPARE_ARRAYS( av2, t );
340  }
341 
342  // ToDo: Add more tests!
343 
344  return success;
345 
346 }
347 
348 
349 //
350 // Main testing program
351 //
352 
353 int main( int argc, char* argv[] ) {
354 
356 
357  bool success = true;
358  bool result;
359 
360  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
361 
364 
365  try {
366 
367  //
368  // Read options from the commandline
369  //
370 
371  CommandLineProcessor clp(false); // Don't throw exceptions
372 
373  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
374 
375  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
376  *out << "\nEnd Result: TEST FAILED" << std::endl;
377  return parse_return;
378  }
379 
380  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
381 
382  const int N = 8;
383 
384  result = testTuple<int,N>(*out);
385  if (!result) success = false;
386 
387  result = testTuple<float,N>(*out);
388  if (!result) success = false;
389 
390  result = testTuple<double,N>(*out);
391  if (!result) success = false;
392 
393  result = testTuple<std::complex<double> ,N>(*out);
394  if (!result) success = false;
395 
396  }
397  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
398 
399  if (success)
400  *out << "\nEnd Result: TEST PASSED" << std::endl;
401  else
402  *out << "\nEnd Result: TEST FAILED" << std::endl;
403 
404  return ( success ? 0 : 1 );
405 
406 }
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
Definition: Tuple_test.cpp:44
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
Initialize, finalize, and query the global MPI session.
#define TEST_EQUALITY_CONST(v1, v2)
Definition: Tuple_test.cpp:35
Statically sized simple array (tuple) class.
#define TEST_COMPARE_ARRAYS(a1, a2)
Definition: Tuple_test.cpp:50
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
std::string Teuchos_Version()
Base traits class for getting a properly initialized null pointer.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
int main(int argc, char *argv[])
Utilities to make writing tests easier.
Nonowning array view.
Default traits class that just returns typeid(T).name().
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
bool testTuple(Teuchos::FancyOStream &out)
Definition: Tuple_test.cpp:69
Smart reference counting pointer class for automatic garbage collection.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
Definition of Teuchos::as, for conversions between types.
Class that helps parse command line input arguments from (argc,argv[]) and set options.