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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_Tuple.hpp"
47 #include "Teuchos_Version.hpp"
48 #include "Teuchos_getConst.hpp"
49 #include "Teuchos_as.hpp"
51 
52 
53 // Uncomment to show compile errors from invalid usage
54 //#define SHOW_INVALID_COPY_CONSTRUCTION
55 //#define SHOW_INVALID_CONST_ASSIGN
56 //#define SHOW_INVALID_CONST_ITER_MODIFICATION
57 
58 //
59 // Define local macros to make defining tests easier for this particular test
60 // code.
61 //
62 // Note, macros with these types of names should only exist in a *.cpp file
63 // after all #includes are done!
64 //
65 
66 
67 #define TEST_EQUALITY_CONST( v1, v2 ) \
68  TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
69 
70 #define TEST_EQUALITY( v1, v2 ) \
71  TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
72 
73 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
74  TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
75 
76 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
77  TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
78 
79 #define TEST_COMPARE( v1, comp, v2 ) \
80  TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
81 
82 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
83  { \
84  const bool result = compareArrays(a1,#a1,a2,#a2,out); \
85  if (!result) success = false; \
86  }
87 
88 #define TEST_THROW( code, ExceptType ) \
89  TEUCHOS_TEST_THROW( code, ExceptType, out, success )
90 
91 #define TEST_NOTHROW( code ) \
92  TEUCHOS_TEST_NOTHROW( code, out, success )
93 
94 
95 //
96 // Main templated array test function
97 //
98 
99 
100 template<class T, int N>
102 {
103 
104  using Teuchos::Tuple;
105  using Teuchos::tuple;
106  using Teuchos::ArrayView;
107  using Teuchos::arrayView;
108  using Teuchos::arrayViewFromVector;
109  using Teuchos::outArg;
112  using Teuchos::getConst;
113  using Teuchos::as;
114  typedef typename ArrayView<T>::size_type size_type;
115  // mfh 03 Apr 2014: The point of the above line of code is to ensure
116  // that ArrayView<T> has a public size_type typedef. However, the
117  // above line of code in isolation causes some compilers to warn
118  // about a declared but unused typedef. We deal with this by
119  // declaring a variable (actually, the oxymoron "const variable") of
120  // type size_type, then using the "cast to void" trick to forestall
121  // compiler warnings for the declared but unused variable. (Fun
122  // fact: "oxymoron" means "sharp dull" and is itself an oxymoron.)
123  // The "cast to void" trick doesn't always work, but if it doesn't,
124  // it's easy to make it go away by printing it to the output stream
125  // 'out'.
126  const size_type arbitrarySizeTypeValue = 0;
127  (void) arbitrarySizeTypeValue;
128 
129  bool success = true;
130 
131  out
132  << "\n***"
133  << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
134  << "\n***\n";
135 
136  Teuchos::OSTab tab(out);
137 
138  //
139  out << "\nA) Initial setup testing ...\n\n";
140  //
141 
142  Tuple<T,N> t;
143  TEST_EQUALITY_CONST(t.size(),N);
144  for( int i = 0; i < N; ++i )
145  t[i] = i; // tests non-const operator[](i)
146 
147  {
148  out << "\nTest that t[i] == i ... ";
149  const ArrayView<const T> cav2 = t;
150  bool local_success = true;
151  for( int i = 0; i < N; ++i ) {
152  TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
153  }
154  if (local_success) out << "passed\n";
155  else success = false;
156  }
157 
158  {
159  const int n = 1;
160  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
161  Tuple<T,n> tn = tuple<T>(0);
162  TEST_EQUALITY_CONST(tn.size(),n);
163  out << "Test that tn[i] == i ... ";
164  bool local_success = true;
165  for( int i = 0; i < n; ++i ) {
166  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
167  }
168  if (local_success) out << "passed\n";
169  else success = false;
170  }
171 
172  {
173  const int n = 2;
174  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
175  Tuple<T,n> tn = tuple<T>(0,1);
176  TEST_EQUALITY_CONST(tn.size(),n);
177  out << "Test that tn[i] == i ... ";
178  bool local_success = true;
179  for( int i = 0; i < n; ++i ) {
180  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
181  }
182  if (local_success) out << "passed\n";
183  else success = false;
184  }
185 
186  {
187  const int n = 3;
188  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
189  Tuple<T,n> tn = tuple<T>(0,1,2);
190  TEST_EQUALITY_CONST(tn.size(),n);
191  out << "Test that tn[i] == i ... ";
192  bool local_success = true;
193  for( int i = 0; i < n; ++i ) {
194  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
195  }
196  if (local_success) out << "passed\n";
197  else success = false;
198  }
199 
200  {
201  const int n = 4;
202  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
203  Tuple<T,n> tn = tuple<T>(0,1,2,3);
204  TEST_EQUALITY_CONST(tn.size(),n);
205  out << "Test that tn[i] == i ... ";
206  bool local_success = true;
207  for( int i = 0; i < n; ++i ) {
208  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
209  }
210  if (local_success) out << "passed\n";
211  else success = false;
212  }
213 
214  {
215  const int n = 5;
216  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
217  Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
218  TEST_EQUALITY_CONST(tn.size(),n);
219  out << "Test that tn[i] == i ... ";
220  bool local_success = true;
221  for( int i = 0; i < n; ++i ) {
222  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
223  }
224  if (local_success) out << "passed\n";
225  else success = false;
226  }
227 
228  {
229  const int n = 6;
230  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
231  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
232  TEST_EQUALITY_CONST(tn.size(),n);
233  out << "Test that tn[i] == i ... ";
234  bool local_success = true;
235  for( int i = 0; i < n; ++i ) {
236  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
237  }
238  if (local_success) out << "passed\n";
239  else success = false;
240  }
241 
242  {
243  const int n = 7;
244  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
245  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
246  TEST_EQUALITY_CONST(tn.size(),n);
247  out << "Test that tn[i] == i ... ";
248  bool local_success = true;
249  for( int i = 0; i < n; ++i ) {
250  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
251  }
252  if (local_success) out << "passed\n";
253  else success = false;
254  }
255 
256  {
257  const int n = 8;
258  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
259  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
260  TEST_EQUALITY_CONST(tn.size(),n);
261  out << "Test that tn[i] == i ... ";
262  bool local_success = true;
263  for( int i = 0; i < n; ++i ) {
264  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
265  }
266  if (local_success) out << "passed\n";
267  else success = false;
268  }
269 
270  {
271  const int n = 9;
272  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
273  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
274  TEST_EQUALITY_CONST(tn.size(),n);
275  out << "Test that tn[i] == i ... ";
276  bool local_success = true;
277  for( int i = 0; i < n; ++i ) {
278  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
279  }
280  if (local_success) out << "passed\n";
281  else success = false;
282  }
283 
284  {
285  const int n = 10;
286  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
287  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
288  TEST_EQUALITY_CONST(tn.size(),n);
289  out << "Test that tn[i] == i ... ";
290  bool local_success = true;
291  for( int i = 0; i < n; ++i ) {
292  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
293  }
294  if (local_success) out << "passed\n";
295  else success = false;
296  }
297 
298  {
299  const int n = 11;
300  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
301  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
302  TEST_EQUALITY_CONST(tn.size(),n);
303  out << "Test that tn[i] == i ... ";
304  bool local_success = true;
305  for( int i = 0; i < n; ++i ) {
306  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
307  }
308  if (local_success) out << "passed\n";
309  else success = false;
310  }
311 
312  {
313  const int n = 12;
314  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
315  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
316  TEST_EQUALITY_CONST(tn.size(),n);
317  out << "Test that tn[i] == i ... ";
318  bool local_success = true;
319  for( int i = 0; i < n; ++i ) {
320  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
321  }
322  if (local_success) out << "passed\n";
323  else success = false;
324  }
325 
326  {
327  const int n = 13;
328  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
329  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
330  TEST_EQUALITY_CONST(tn.size(),n);
331  out << "Test that tn[i] == i ... ";
332  bool local_success = true;
333  for( int i = 0; i < n; ++i ) {
334  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
335  }
336  if (local_success) out << "passed\n";
337  else success = false;
338  }
339 
340  {
341  const int n = 14;
342  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
343  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
344  TEST_EQUALITY_CONST(tn.size(),n);
345  out << "Test that tn[i] == i ... ";
346  bool local_success = true;
347  for( int i = 0; i < n; ++i ) {
348  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
349  }
350  if (local_success) out << "passed\n";
351  else success = false;
352  }
353 
354  {
355  const int n = 15;
356  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
357  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
358  TEST_EQUALITY_CONST(tn.size(),n);
359  out << "Test that tn[i] == i ... ";
360  bool local_success = true;
361  for( int i = 0; i < n; ++i ) {
362  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
363  }
364  if (local_success) out << "passed\n";
365  else success = false;
366  }
367 
368  {
369  out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
370  const ArrayView<const T> av2 = t;
371  TEST_COMPARE_ARRAYS( av2, t );
372  }
373 
374  // ToDo: Add more tests!
375 
376  return success;
377 
378 }
379 
380 
381 //
382 // Main testing program
383 //
384 
385 int main( int argc, char* argv[] ) {
386 
388 
389  bool success = true;
390  bool result;
391 
392  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
393 
396 
397  try {
398 
399  //
400  // Read options from the commandline
401  //
402 
403  CommandLineProcessor clp(false); // Don't throw exceptions
404 
405  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
406 
407  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
408  *out << "\nEnd Result: TEST FAILED" << std::endl;
409  return parse_return;
410  }
411 
412  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
413 
414  const int N = 8;
415 
416  result = testTuple<int,N>(*out);
417  if (!result) success = false;
418 
419  result = testTuple<float,N>(*out);
420  if (!result) success = false;
421 
422  result = testTuple<double,N>(*out);
423  if (!result) success = false;
424 
425  result = testTuple<std::complex<double> ,N>(*out);
426  if (!result) success = false;
427 
428  }
429  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
430 
431  if (success)
432  *out << "\nEnd Result: TEST PASSED" << std::endl;
433  else
434  *out << "\nEnd Result: TEST FAILED" << std::endl;
435 
436  return ( success ? 0 : 1 );
437 
438 }
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
Definition: Tuple_test.cpp:76
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:67
Statically sized simple array (tuple) class.
#define TEST_COMPARE_ARRAYS(a1, a2)
Definition: Tuple_test.cpp:82
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:101
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.