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 
116  bool success = true;
117 
118  out
119  << "\n***"
120  << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
121  << "\n***\n";
122 
123  Teuchos::OSTab tab(out);
124 
125  //
126  out << "\nA) Initial setup testing ...\n\n";
127  //
128 
129  Tuple<T,N> t;
130  TEST_EQUALITY_CONST(t.size(),N);
131  for( int i = 0; i < N; ++i )
132  t[i] = i; // tests non-const operator[](i)
133 
134  {
135  out << "\nTest that t[i] == i ... ";
136  const ArrayView<const T> cav2 = t;
137  bool local_success = true;
138  for( int i = 0; i < N; ++i ) {
139  TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
140  }
141  if (local_success) out << "passed\n";
142  else success = false;
143  }
144 
145  {
146  const int n = 1;
147  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
148  Tuple<T,n> tn = tuple<T>(0);
149  TEST_EQUALITY_CONST(tn.size(),n);
150  out << "Test that tn[i] == i ... ";
151  bool local_success = true;
152  for( int i = 0; i < n; ++i ) {
153  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
154  }
155  if (local_success) out << "passed\n";
156  else success = false;
157  }
158 
159  {
160  const int n = 2;
161  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
162  Tuple<T,n> tn = tuple<T>(0,1);
163  TEST_EQUALITY_CONST(tn.size(),n);
164  out << "Test that tn[i] == i ... ";
165  bool local_success = true;
166  for( int i = 0; i < n; ++i ) {
167  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
168  }
169  if (local_success) out << "passed\n";
170  else success = false;
171  }
172 
173  {
174  const int n = 3;
175  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
176  Tuple<T,n> tn = tuple<T>(0,1,2);
177  TEST_EQUALITY_CONST(tn.size(),n);
178  out << "Test that tn[i] == i ... ";
179  bool local_success = true;
180  for( int i = 0; i < n; ++i ) {
181  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
182  }
183  if (local_success) out << "passed\n";
184  else success = false;
185  }
186 
187  {
188  const int n = 4;
189  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
190  Tuple<T,n> tn = tuple<T>(0,1,2,3);
191  TEST_EQUALITY_CONST(tn.size(),n);
192  out << "Test that tn[i] == i ... ";
193  bool local_success = true;
194  for( int i = 0; i < n; ++i ) {
195  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
196  }
197  if (local_success) out << "passed\n";
198  else success = false;
199  }
200 
201  {
202  const int n = 5;
203  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
204  Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
205  TEST_EQUALITY_CONST(tn.size(),n);
206  out << "Test that tn[i] == i ... ";
207  bool local_success = true;
208  for( int i = 0; i < n; ++i ) {
209  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
210  }
211  if (local_success) out << "passed\n";
212  else success = false;
213  }
214 
215  {
216  const int n = 6;
217  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
218  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
219  TEST_EQUALITY_CONST(tn.size(),n);
220  out << "Test that tn[i] == i ... ";
221  bool local_success = true;
222  for( int i = 0; i < n; ++i ) {
223  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
224  }
225  if (local_success) out << "passed\n";
226  else success = false;
227  }
228 
229  {
230  const int n = 7;
231  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
232  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
233  TEST_EQUALITY_CONST(tn.size(),n);
234  out << "Test that tn[i] == i ... ";
235  bool local_success = true;
236  for( int i = 0; i < n; ++i ) {
237  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
238  }
239  if (local_success) out << "passed\n";
240  else success = false;
241  }
242 
243  {
244  const int n = 8;
245  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
246  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
247  TEST_EQUALITY_CONST(tn.size(),n);
248  out << "Test that tn[i] == i ... ";
249  bool local_success = true;
250  for( int i = 0; i < n; ++i ) {
251  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
252  }
253  if (local_success) out << "passed\n";
254  else success = false;
255  }
256 
257  {
258  const int n = 9;
259  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
260  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
261  TEST_EQUALITY_CONST(tn.size(),n);
262  out << "Test that tn[i] == i ... ";
263  bool local_success = true;
264  for( int i = 0; i < n; ++i ) {
265  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
266  }
267  if (local_success) out << "passed\n";
268  else success = false;
269  }
270 
271  {
272  const int n = 10;
273  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
274  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
275  TEST_EQUALITY_CONST(tn.size(),n);
276  out << "Test that tn[i] == i ... ";
277  bool local_success = true;
278  for( int i = 0; i < n; ++i ) {
279  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
280  }
281  if (local_success) out << "passed\n";
282  else success = false;
283  }
284 
285  {
286  const int n = 11;
287  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
288  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
289  TEST_EQUALITY_CONST(tn.size(),n);
290  out << "Test that tn[i] == i ... ";
291  bool local_success = true;
292  for( int i = 0; i < n; ++i ) {
293  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
294  }
295  if (local_success) out << "passed\n";
296  else success = false;
297  }
298 
299  {
300  const int n = 12;
301  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
302  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
303  TEST_EQUALITY_CONST(tn.size(),n);
304  out << "Test that tn[i] == i ... ";
305  bool local_success = true;
306  for( int i = 0; i < n; ++i ) {
307  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
308  }
309  if (local_success) out << "passed\n";
310  else success = false;
311  }
312 
313  {
314  const int n = 13;
315  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
316  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
317  TEST_EQUALITY_CONST(tn.size(),n);
318  out << "Test that tn[i] == i ... ";
319  bool local_success = true;
320  for( int i = 0; i < n; ++i ) {
321  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
322  }
323  if (local_success) out << "passed\n";
324  else success = false;
325  }
326 
327  {
328  const int n = 14;
329  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
330  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
331  TEST_EQUALITY_CONST(tn.size(),n);
332  out << "Test that tn[i] == i ... ";
333  bool local_success = true;
334  for( int i = 0; i < n; ++i ) {
335  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
336  }
337  if (local_success) out << "passed\n";
338  else success = false;
339  }
340 
341  {
342  const int n = 15;
343  out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
344  Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
345  TEST_EQUALITY_CONST(tn.size(),n);
346  out << "Test that tn[i] == i ... ";
347  bool local_success = true;
348  for( int i = 0; i < n; ++i ) {
349  TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
350  }
351  if (local_success) out << "passed\n";
352  else success = false;
353  }
354 
355  {
356  out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
357  const ArrayView<const T> av2 = t;
358  TEST_COMPARE_ARRAYS( av2, t );
359  }
360 
361  // ToDo: Add more tests!
362 
363  return success;
364 
365 }
366 
367 
368 //
369 // Main testing program
370 //
371 
372 int main( int argc, char* argv[] ) {
373 
375 
376  bool success = true;
377  bool result;
378 
379  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
380 
383 
384  try {
385 
386  //
387  // Read options from the commandline
388  //
389 
390  CommandLineProcessor clp(false); // Don't throw exceptions
391 
392  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
393 
394  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
395  *out << "\nEnd Result: TEST FAILED" << std::endl;
396  return parse_return;
397  }
398 
399  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
400 
401  const int N = 8;
402 
403  result = testTuple<int,N>(*out);
404  if (!result) success = false;
405 
406  result = testTuple<float,N>(*out);
407  if (!result) success = false;
408 
409  result = testTuple<double,N>(*out);
410  if (!result) success = false;
411 
412  result = testTuple<std::complex<double> ,N>(*out);
413  if (!result) success = false;
414 
415  }
416  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
417 
418  if (success)
419  *out << "\nEnd Result: TEST PASSED" << std::endl;
420  else
421  *out << "\nEnd Result: TEST FAILED" << std::endl;
422 
423  return ( success ? 0 : 1 );
424 
425 }
#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.
Definition: Teuchos_as.hpp:287
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.