Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Array_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_Array.hpp"
15 #include "Teuchos_Version.hpp"
16 #include "Teuchos_getConst.hpp"
17 #include "Teuchos_as.hpp"
19 
20 #include "TestClasses.hpp"
21 
22 
23 //
24 // Main templated array test function
25 //
26 
27 
28 template<class T>
29 bool testArray( const int n, Teuchos::FancyOStream &out )
30 {
31 
32  using Teuchos::Array;
33  using Teuchos::ArrayView;
34  using Teuchos::outArg;
35  using Teuchos::getConst;
38  using Teuchos::as;
39  using Teuchos::tuple;
40  typedef typename Array<T>::size_type size_type;
41 
42  bool success = true;
43 
44  out
45  << "\n***"
46  << "\n*** Testing "<<TypeNameTraits<Array<T> >::name()<<" of size = "<<n
47  << "\n***\n";
48 
49  Teuchos::OSTab tab(out);
50 
51  //
52  out << "\nA) Initial setup ...\n\n";
53  //
54 
55  // Tests construction using size
56 
57  Array<T> a(n);
58 
59  TEST_EQUALITY_CONST( a.empty(), false );
60  TEST_EQUALITY( a.length(), n );
61  TEST_EQUALITY( as<int>(a.size()), n );
62  TEST_EQUALITY( a.getRawPtr(), &a[0] );
63  TEST_EQUALITY( a.data(), &a[0] );
64  TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
65  TEST_EQUALITY( getConst(a).data(), &getConst(a)[0] );
66  TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
67  TEST_COMPARE( as<int>(a.capacity()), >=, n );
68 
69  {
70  out << "\nInitializing data ...\n";
71  for( int i = 0; i < n; ++i )
72  a[i] = as<T>(i); // tests non-const operator[](i)
73  }
74 
75  {
76  out << "\nTest that a[i] == i ... ";
77  bool local_success = true;
78  for( int i = 0; i < n; ++i ) {
79  TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
80  }
81  if (local_success) out << "passed\n";
82  else success = false;
83  }
84 
85  {
86  out << "\nTest that a.at(i) == i ...\n";
87  bool local_success = true;
88  for( int i = 0; i < n; ++i ) {
89  TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
90  }
91  if (local_success) out << "passed\n";
92  else success = false;
93  }
94 
95  //
96  out << "\nB) Test constructors, assignment operators etc ...\n";
97  //
98 
99  {
100  out << "\nTest default constructor ...\n";
101  Array<T> a2;
102  TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
103  TEST_EQUALITY_CONST( as<bool>(a2.empty()), true );
104  TEST_EQUALITY_CONST( a2.getRawPtr(), 0 );
105  TEST_EQUALITY_CONST( a2.data(), a2.getRawPtr() );
107  TEST_EQUALITY_CONST( getConst(a2).getRawPtr(), getConst(a2).data() );
108  }
109 
110  {
111  out << "\nTest copy conversion to and from Teuchos::Array and std::vector ...\n";
112  std::vector<T> v2 = createVector(a);
113  Array<T> a2(v2);
114  TEST_COMPARE_ARRAYS( a2, a );
115  }
116 
117  {
118  out << "\nTest assignment operator taking an std::vector ...\n";
119  std::vector<T> v2 = createVector(a);
120  Array<T> a2;
121  a2 = v2;
122  TEST_COMPARE_ARRAYS( a2, a );
123  }
124 
125  {
126  out << "\nTest construction using iterators ...\n";
127  std::vector<T> v2 = createVector(a);
128  Array<T> a2(a.begin(),a.end());
129  TEST_COMPARE_ARRAYS( a2, a );
130  }
131 
132  {
133  out << "\nTest copy construction ...\n";
134  Array<T> a2(a);
135  TEST_COMPARE_ARRAYS( a2, a );
136  }
137 
138  {
139  out << "\nTest array assignment operator ...\n";
140  Array<T> a2;
141  a2 = a;
142  TEST_COMPARE_ARRAYS( a2, a );
143  }
144 
145  {
146  out << "\nTest array assign(...) ...\n";
147  Array<T> a2;
148  a2.assign(a.begin(),a.end());
149  TEST_COMPARE_ARRAYS( a2, a );
150  }
151 
152  {
153  out << "\nTest iterator access and then resize ...\n";
154  Array<T> a2(a);
155  const Array<T> &ca2 = a2;
156  Array<T> a3(ca2.begin(),ca2.end());
157  TEST_COMPARE_ARRAYS( a3, a );
158  TEST_NOTHROW(a2.resize(0)); // This used to throw exception!
159  }
160 
161  //
162  out << "\nC) Test element access ...\n";
163  //
164 
165  TEST_EQUALITY_CONST( a.front(), as<T>(0) );
166  TEST_EQUALITY( a.back(), as<T>(n-1) );
167 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
170  TEST_THROW( a.at(-1), Teuchos::RangeError );
171  TEST_THROW( a.at(n), Teuchos::RangeError );
172 #else //HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
173  TEST_THROW( a.at(-1), std::out_of_range );
174  TEST_THROW( a.at(n), std::out_of_range );
175 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
176 
177  //
178  out << "\nD) Test iterator access ...\n";
179  //
180 
181 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
182 
183  {
184  out << "\nTesting functions that should throw for empty container ...\n";
185  Array<T> a2;
186  TEST_THROW( *a2.begin(), Teuchos::NullReferenceError );
191  TEST_THROW( a2.erase(a2.begin()), Teuchos::NullReferenceError );
192  }
193 
194 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
195 
196  {
197  out << "\nTest that a2.begin() == a2.end() for empty a2 ...\n";
198  Array<T> a2;
199  TEST_ITER_EQUALITY( a2.begin(), a2.end() );
200  }
201 
202  {
203  out << "\nTest nonconst forward iterator access ... ";
204  bool local_success = true;
205  typedef typename Array<T>::iterator iter_t;
206  iter_t iter = a.begin();
207  for ( int i = 0; i < n; ++i, ++iter )
208  TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
209  iter = NullIteratorTraits<iter_t>::getNull();
210  if (local_success) out << "passed\n";
211  else success = false;
212  }
213 
214  {
215  out << "\nTest const forward iterator access ... ";
216  bool local_success = true;
217  typedef typename Array<T>::const_iterator iter_t;
218  iter_t iter = getConst(a).begin();
219  for ( int i = 0; i < n; ++i, ++iter )
220  TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
221  iter = NullIteratorTraits<iter_t>::getNull();
222  if (local_success) out << "passed\n";
223  else success = false;
224  }
225 
226 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
227 
228  {
229  out << "\nTest forward iterators dereferenced out of bounds ...\n";
230  TEST_THROW( *(a.begin()-1), Teuchos::RangeError );
231  TEST_THROW( *a.end(), Teuchos::RangeError );
232  TEST_THROW( *(getConst(a).begin()-1), Teuchos::RangeError );
234  }
235 
236 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
237 
238  {
239  out << "\nTest that a2.rbegin() == a2.rend() for empty a2 ...\n";
240  Array<T> a2;
241  TEST_ITER_EQUALITY( a2.rbegin(), a2.rend() );
242  }
243 
244  {
245  out << "\nTest nonconst reverse iterator access ... ";
246  bool local_success = true;
247  typedef typename Array<T>::reverse_iterator iter_t;
248  iter_t iter = a.rbegin();
249  for ( int i = n-1; i >= 0; --i, ++iter )
250  TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
251  iter = NullIteratorTraits<iter_t>::getNull();
252  if (local_success) out << "passed\n";
253  else success = false;
254  }
255 
256  {
257  out << "\nTest const reverse iterator access ... ";
258  bool local_success = true;
259  typedef typename Array<T>::const_reverse_iterator iter_t;
260  iter_t iter = getConst(a).rbegin();
261  for ( int i = n-1; i >= 0; --i, ++iter )
262  TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
263  iter = NullIteratorTraits<iter_t>::getNull();
264  if (local_success) out << "passed\n";
265  else success = false;
266  }
267 
268 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
269  {
270  out << "\nTest reverse iterators dereferenced out of bounds ...\n";
271  TEST_THROW( *(a.rbegin()-1), Teuchos::RangeError );
272  TEST_THROW( *a.rend(), Teuchos::RangeError );
273  TEST_THROW( *(getConst(a).rbegin()-1), Teuchos::RangeError );
274  TEST_THROW( *getConst(a).rend(), Teuchos::RangeError );
275  }
276 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
277 
278  {
279  out << "\nTest that an iterator reference set to null does not throw ...\n";
280  typedef typename Array<T>::iterator iter_t;
281  iter_t iter = NullIteratorTraits<iter_t>::getNull();
282  TEST_NOTHROW( Array<T> a2(n); iter = a2.begin();
283  iter = NullIteratorTraits<iter_t>::getNull() );
284  }
285 
286  {
287  out << "\nTest that a dangling iterator reference throws exception ...\n";
288  typedef typename Array<T>::iterator iter_t;
289  iter_t iter = NullIteratorTraits<iter_t>::getNull();
290  {
291  Array<T> a2(n);
292  iter = a2.begin();
293  }
294 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
296 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
297  }
298 
299 
300  //
301  out << "\nE) Test insertion and deletion functions ...\n";
302  //
303 
304  {
305  out << "\nTest push_back(x) ...\n";
306  Array<T> a2;
307  for ( int i = 0; i < n; ++i ) {
308  a2.push_back(as<T>(i));
309  TEST_EQUALITY_CONST(a2.front(),as<T>(0));
310  TEST_EQUALITY_CONST(getConst(a2).front(),as<T>(0));
311  TEST_EQUALITY(a2.back(),as<T>(i));
312  TEST_EQUALITY(getConst(a2).back(),as<T>(i));
313  }
314  TEST_COMPARE_ARRAYS( a2, a );
315  }
316 
317  {
318  out << "\nTest pop_back() ...\n";
319  Array<T> a2(a);
320  for ( int i = n-1; i >= 0; --i ) {
321  TEST_EQUALITY(a2.back(),as<T>(i));
322  a2.pop_back();
323  }
324  }
325 
326  {
327  out << "\nTest insert(iter,x) ...\n";
328  Array<T> a2;
329  for ( int i = 0; i < n; ++i ) {
330  const typename Array<T>::iterator
331  iter = a2.insert(a2.end(), as<T>(i));
332  TEST_EQUALITY(*iter, as<T>(i));
333  }
334  TEST_COMPARE_ARRAYS( a2, a );
335  }
336 
337  {
338  out << "\nTest insert(iter,1,x) ...\n";
339  Array<T> a2;
340  for ( int i = 0; i < n; ++i )
341  a2.insert(a2.end(),1,i);
342  TEST_COMPARE_ARRAYS( a2, a );
343  }
344 
345  {
346  out << "\nTest insert(iter,first,last) ...\n";
347  Array<T> a2;
348  for ( int i = 0; i < n; ++i )
349  a2.insert(a2.end(),a.begin()+i,a.begin()+i+1);
350  TEST_COMPARE_ARRAYS( a2, a );
351  }
352 
353  {
354  out << "\nTest append(x) ...\n";
355  Array<T> a2;
356  for ( int i = 0; i < n; ++i )
357  a2.append(as<T>(i));
358  TEST_COMPARE_ARRAYS( a2, a );
359  }
360 
361  {
362  out << "\nTest erase(iter) ...\n";
363  Array<T> a2(a);
364  for ( int i = 0; i < n; ++i ) {
365  TEST_EQUALITY( as<int>(a2.size()), n-i );
366  TEST_EQUALITY( a2.front(), as<T>(i) );
367  a2.erase(a2.begin());
368  }
369  TEST_EQUALITY_CONST( a2.empty(), true );
370  }
371 
372 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
373 
374  {
375  out << "\nTest trying to erase twice with the same iterator which should throw ...\n";
376  Array<T> a2(a);
377  const typename Array<T>::iterator iter = a2.begin();
378  a2.erase(iter); // After this point, the iterator is no longer valid!
379  // This is no longer a valid iterator and should throw!
381  }
382 
383 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
384 
385  // 2007/11/08: rabartl: ToDo: Above, I have tested one use case where the
386  // iterator should be invalidated and this tests that it throws an exception
387  // as it should. However, currently, I don't have code written that will
388  // catch the problem where the client would try to dereference the iterator
389  // or something like that. This is a big no-no. I could do this by adding
390  // an is_valid() property to RCP_node and then setting this to null when the
391  // structure of the iterator changes. Then, I would have to put asserts in
392  // ArrayRCP to constantly check is_valid() (with an assert_is_valid()
393  // function or something) on any call other than operator=(...) which would
394  // reset this iterator. Catching all of these user errors is a lot of work!
395 
396  {
397  out << "\nTest remove(i) ...\n";
398  Array<T> a2(a);
399  for ( int i = 0; i < n; ++i ) {
400  TEST_EQUALITY( as<int>(a2.size()), n-i );
401  TEST_EQUALITY( a2.front(), as<T>(i) );
402  a2.remove(0); // Always remove the "first" entry!
403  }
404  TEST_EQUALITY_CONST( a2.empty(), true );
405  }
406 
407  {
408  out << "\nTest erase(begin(),end()) ...\n";
409  Array<T> a2(a);
410  a2.erase(a2.begin(),a2.end());
411  TEST_EQUALITY_CONST( a2.empty(), true );
412  }
413 
414  {
415  out << "\nTest member swap() ...\n";
416  Array<T> a2(a);
417  Array<T> a3(a);
418  for ( int i = 0; i < n; ++i )
419  a2[i] += as<T>(1);
420  a2.swap(a3);
421  TEST_COMPARE_ARRAYS( a2, a );
422  }
423 
424  {
425  out << "\nTest non-member swap() ...\n";
426  Array<T> a2(a);
427  Array<T> a3(a);
428  for ( int i = 0; i < n; ++i )
429  a2[i] += as<T>(1);
430  swap(a2,a3);
431  TEST_COMPARE_ARRAYS( a2, a );
432  }
433 
434  {
435  out << "\nTest clear() ...\n";
436  Array<T> a2(a);
437  a2.clear();
438  TEST_EQUALITY_CONST( a2.empty(), true );
439  TEST_EQUALITY_CONST( a2.size(), 0 );
440  }
441 
442 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
443  // mfh 28 Aug 2012: This test no longer passes, because we've
444  // specialized ArrayView<T>::toString() for T = (const) float,
445  // (const) double. We've done the specialization to print float and
446  // double in scientific notation. That was a hack to fix a bug; it
447  // would make more sense to provide a standard toString() for float
448  // and double, and have the test (or even the
449  // ArrayView<T>::toString() specialization) use that.
450  //
451  // {
452  // out << "\nTest to string ...\n";
453  // std::ostringstream o;
454  // o << "{";
455  // for ( int i = 0; i < n; ++i ) {
456  // o << as<T>(i) << ( i < n-1 ? ", " : "" );
457  // }
458  // o << "}";
459  // TEST_EQUALITY( o.str(), a.toString() );
460  // }
461 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
462 
463  {
464  out << "\nTest hasArrayBoundsChecking() ... \n";
465 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
466  TEST_EQUALITY_CONST( a.hasBoundsChecking(), true );
467 #else
468  TEST_EQUALITY_CONST( a.hasBoundsChecking(), false );
469 #endif
470  }
471 
472  //
473  out << "\nG) Test views ...\n";
474  //
475 
476  {
477  out << "\nTest full non-const subview ...\n";
478  const ArrayView<T> av2 = a(0,n);
479  TEST_COMPARE_ARRAYS( av2, a );
480  }
481 
482  {
483  out << "\nTest full shorthand non-const subview ...\n";
484  const ArrayView<T> av2 = a();
485  TEST_COMPARE_ARRAYS( av2, a );
486  }
487 
488  {
489  out << "\nTest full const subview ...\n";
490  const ArrayView<const T> cav2 = getConst(a)(0, n);
491  TEST_COMPARE_ARRAYS( cav2, a );
492  }
493 
494  {
495  out << "\nTest full non-const to const subview ...\n";
496  const ArrayView<const T> cav2 = a(0, n);
497  TEST_COMPARE_ARRAYS( cav2, a );
498  }
499 
500  {
501  out << "\nTest full short-hand const subview ...\n";
502  const ArrayView<const T> cav2 = getConst(a)();
503  TEST_COMPARE_ARRAYS( cav2, a );
504  }
505 
506  {
507  out << "\nTest non-const initial range view ...\n";
508  Array<T> a2(n,as<T>(-1));
509  const ArrayView<T> av2 = a2; // Tests implicit conversion!
510  const ArrayView<T> av2_end = av2(0,n-1);
511  TEST_EQUALITY( av2_end.size(), n-1 );
512  av2_end.assign( a(0,n-1) );
513  av2.back() = as<T>(n-1);
514  TEST_COMPARE_ARRAYS( a2, a );
515  }
516 
517  {
518  out << "\nTest non-const middle range view ...\n";
519  Array<T> a2(n,as<T>(-1));
520  const ArrayView<T> av2 = a2; // Tests implicit conversion!
521  const ArrayView<T> av2_middle = av2(1,n-2);
522  TEST_EQUALITY( av2_middle.size(), n-2 );
523  av2_middle.assign( a(1,n-2) );
524  av2.front() = as<T>(0);
525  av2.back() = as<T>(n-1);
526  TEST_COMPARE_ARRAYS( a2, a );
527  }
528 
529  {
530  out << "\nTest const view ... ";
531  const ArrayView<const T> av2 = a; // Tests implicit conversion to const!
532  const ArrayView<const T> av2_middle = av2(1,n-2);
533  TEST_EQUALITY( av2_middle.size(), n-2 );
534  bool local_success = true;
535  for ( int i = 0; i < n-2; ++i )
536  TEST_ARRAY_ELE_EQUALITY( av2_middle, i, as<T>(i+1) );
537  if (local_success) out << "passed\n";
538  else success = false;
539  }
540 
541  {
542  out << "\nTest constructing Array<T> from ArrayView<T> ...\n";
543  const ArrayView<T> av2 = a;
544  Array<T> a2(av2);
545  TEST_COMPARE_ARRAYS( a2, a );
546  }
547 
548  {
549  out << "\nTest constructing Array<T> from ArrayView<const T> ...\n";
550  const ArrayView<const T> av2 = a;
551  Array<T> a2(av2);
552  TEST_COMPARE_ARRAYS( a2, a );
553  }
554 
555  {
556  out << "\nTest comparison operators ...\n";
557  Array<T> a2(a);
558  TEST_EQUALITY_CONST( (a2==a), true );
559  TEST_EQUALITY_CONST( (a2!=a), false );
560  TEST_EQUALITY_CONST( (a2<=a), true );
561  TEST_EQUALITY_CONST( (a2>=a), true );
562  TEST_EQUALITY_CONST( (a2<a), false );
563  TEST_EQUALITY_CONST( (a2>a), false );
564  }
565 
566  //
567  out << "\nH) Test tuple(...) construction ...\n";
568  //
569 
570  {
571  const size_type m = 1;
572  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
573  Array<T> am = tuple<T>(0);
574  TEST_EQUALITY_CONST(am.size(), m);
575  out << "Test that am[i] == i ... ";
576  bool local_success = true;
577  for( size_type i = 0; i < m; ++i ) {
578  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
579  }
580  if (local_success) out << "passed\n";
581  else success = false;
582  }
583 
584  {
585  const size_type m = 2;
586  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
587  Array<T> am = tuple<T>(0,1);
588  TEST_EQUALITY_CONST(am.size(),m);
589  out << "Test that am[i] == i ... ";
590  bool local_success = true;
591  for( size_type i = 0; i < m; ++i ) {
592  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
593  }
594  if (local_success) out << "passed\n";
595  else success = false;
596  }
597 
598  {
599  const size_type m = 3;
600  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
601  Array<T> am = tuple<T>(0,1,2);
602  TEST_EQUALITY_CONST(am.size(),m);
603  out << "Test that am[i] == i ... ";
604  bool local_success = true;
605  for( size_type i = 0; i < m; ++i ) {
606  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
607  }
608  if (local_success) out << "passed\n";
609  else success = false;
610  }
611 
612  {
613  const size_type m = 4;
614  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
615  Array<T> am = tuple<T>(0,1,2,3);
616  TEST_EQUALITY_CONST(am.size(),m);
617  out << "Test that am[i] == i ... ";
618  bool local_success = true;
619  for( size_type i = 0; i < m; ++i ) {
620  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
621  }
622  if (local_success) out << "passed\n";
623  else success = false;
624  }
625 
626  {
627  const size_type m = 5;
628  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
629  Array<T> am = tuple<T>(0,1,2,3,4);
630  TEST_EQUALITY_CONST(am.size(),m);
631  out << "Test that am[i] == i ... ";
632  bool local_success = true;
633  for( size_type i = 0; i < m; ++i ) {
634  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
635  }
636  if (local_success) out << "passed\n";
637  else success = false;
638  }
639 
640  {
641  const size_type m = 6;
642  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
643  Array<T> am = tuple<T>(0,1,2,3,4,5);
644  TEST_EQUALITY_CONST(am.size(),m);
645  out << "Test that am[i] == i ... ";
646  bool local_success = true;
647  for( size_type i = 0; i < m; ++i ) {
648  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
649  }
650  if (local_success) out << "passed\n";
651  else success = false;
652  }
653 
654  {
655  const size_type m = 7;
656  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
657  Array<T> am = tuple<T>(0,1,2,3,4,5,6);
658  TEST_EQUALITY_CONST(am.size(),m);
659  out << "Test that am[i] == i ... ";
660  bool local_success = true;
661  for( size_type i = 0; i < m; ++i ) {
662  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
663  }
664  if (local_success) out << "passed\n";
665  else success = false;
666  }
667 
668  {
669  const size_type m = 8;
670  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
671  Array<T> am = tuple<T>(0,1,2,3,4,5,6,7);
672  TEST_EQUALITY_CONST(am.size(),m);
673  out << "Test that am[i] == i ... ";
674  bool local_success = true;
675  for( size_type i = 0; i < m; ++i ) {
676  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
677  }
678  if (local_success) out << "passed\n";
679  else success = false;
680  }
681 
682  {
683  const size_type m = 9;
684  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
685  Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8);
686  TEST_EQUALITY_CONST(am.size(),m);
687  out << "Test that am[i] == i ... ";
688  bool local_success = true;
689  for( size_type i = 0; i < m; ++i ) {
690  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
691  }
692  if (local_success) out << "passed\n";
693  else success = false;
694  }
695 
696  {
697  const size_type m = 10;
698  out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
699  Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8,9);
700  TEST_EQUALITY_CONST(am.size(),m);
701  out << "Test that am[i] == i ... ";
702  bool local_success = true;
703  for( size_type i = 0; i < m; ++i ) {
704  TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
705  }
706  if (local_success) out << "passed\n";
707  else success = false;
708  }
709 
710  {
711  out << "\nTest taking an empty view ...\n";
712  const ArrayView<T> av = a(0,0);
713  TEST_EQUALITY_CONST( av.size(), 0 );
714  }
715 
716 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
717  {
718  out << "\nTest taking views outside of valid range ...\n";
719  TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError );
720  TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError );
721  TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError );
722  }
723 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
724 
725  return success;
726 
727 }
728 
729 
730 template<class T>
731 bool testArrayOpaqueWithoutTNT( const std::string &T_name, const int n,
732  const T &someValue, Teuchos::FancyOStream &out )
733 {
734 
735  using Teuchos::Array;
736  using Teuchos::ArrayView;
738  using Teuchos::as;
739  typedef typename Array<T>::size_type size_type;
740 
741  bool success = true;
742 
743  out
744  << "\n***"
745  << "\n*** Testing Array<"<<T_name<<"> for opaque type without TNT of size = "<<n
746  << "\n***\n";
747 
748  Teuchos::OSTab tab(out);
749 
750  //
751  out << "\nA) Initial setup ...\n\n";
752  //
753 
754  // Tests construction using size
755 
756  Array<T> a(n);
757 
758  TEST_EQUALITY_CONST( a.empty(), false );
759  TEST_EQUALITY( a.length(), n );
760  TEST_EQUALITY( as<int>(a.size()), n );
761  TEST_EQUALITY( a.getRawPtr(), &a[0] );
762  TEST_EQUALITY( a.getRawPtr(), a.data() );
763  TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
764  TEST_EQUALITY( getConst(a).getRawPtr(), getConst(a).data() );
765  TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
766  TEST_COMPARE( as<int>(a.capacity()), >=, n );
767 
768  {
769  out << "\nInitializing data ...\n";
770  for( int i = 0; i < n; ++i )
771  a[i] = someValue; // tests non-const operator[](i)
772  }
773 
774  {
775  out << "\nTest that a[i] == "<<someValue<<" ... ";
776  bool local_success = true;
777  for( int i = 0; i < n; ++i ) {
778  TEST_ARRAY_ELE_EQUALITY( a, i, someValue );
779  }
780  if (local_success) out << "passed\n";
781  else success = false;
782  }
783 
784 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
785  {
786  out << "\nTest taking a view of the array ...\n";
787  const ArrayView<T> av = a();
788  TEST_COMPARE_ARRAYS( av, a );
789  }
790  // 2008/08/01: rabartl: Above: We can not create an array view of an
791  // undefined type in debug mode without a specialization of TypeNameTraits.
792 #endif // not HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
793 
794  // ToDo: Do we need to be testing other things for opaque objects?
795 
796  return success;
797 
798 }
799 
800 
801 template<class T>
802 bool testArrayOpaqueWithTNT( const int n, const T &someValue, Teuchos::FancyOStream &out )
803 {
804 
805  using Teuchos::Array;
806  using Teuchos::ArrayView;
808  using Teuchos::as;
809  typedef typename Array<T>::size_type size_type;
810 
811  bool success = true;
812 
813  out
814  << "\n***"
815  << "\n*** Testing "<<TypeNameTraits<Array<T> >::name()<<" for opaque type with TNT of size = "<<n
816  << "\n***\n";
817 
818  Teuchos::OSTab tab(out);
819 
820  //
821  out << "\nA) Initial setup ...\n\n";
822  //
823 
824  // Tests construction using size
825 
826  Array<T> a(n);
827 
828  TEST_EQUALITY_CONST( a.empty(), false );
829  TEST_EQUALITY( a.length(), n );
830  TEST_EQUALITY( as<int>(a.size()), n );
831  TEST_EQUALITY( a.getRawPtr(), &a[0] );
832  TEST_EQUALITY( a.getRawPtr(), a.data() );
833  TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
834  TEST_EQUALITY( getConst(a).getRawPtr(), getConst(a).data() );
835  TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
836  TEST_COMPARE( as<int>(a.capacity()), >=, n );
837 
838  {
839  out << "\nInitializing data ...\n";
840  for( int i = 0; i < n; ++i )
841  a[i] = someValue; // tests non-const operator[](i)
842  }
843 
844  {
845  out << "\nTest that a[i] == "<<someValue<<" ... ";
846  bool local_success = true;
847  for( int i = 0; i < n; ++i ) {
848  TEST_ARRAY_ELE_EQUALITY( a, i, someValue );
849  }
850  if (local_success) out << "passed\n";
851  else success = false;
852  }
853 
854  {
855  out << "\nTest taking a view of the array ...\n";
856  const ArrayView<T> av = a();
857  TEST_COMPARE_ARRAYS( av, a );
858  }
859 
860 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
861  {
862  out << "\nTest taking views outside of valid range ...\n";
863  TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError );
864  TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError );
865  TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError );
866  }
867 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
868 
869  // 2008/08/01: rabartl: Above: We can create ArrayViews and any other thing
870  // that we would like since we have defined a TypeNameTraits class for the
871  // undefined type.
872 
873  // ToDo: Do we need to be testing other things for opaque objects?
874 
875  return success;
876 
877 }
878 
879 
880 //
881 // Main testing program
882 //
883 
884 int main( int argc, char* argv[] ) {
885 
887  using Teuchos::Array;
888 
889  bool success = true;
890  bool result;
891 
892  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
893  //const int procRank = Teuchos::GlobalMPISession::getRank();
894 
897 
898  try {
899 
900  //
901  // Read options from the commandline
902  //
903 
904  CommandLineProcessor clp(false); // Don't throw exceptions
905 
906  int n = 4;
907  clp.setOption( "n", &n, "Number of elements in the array" );
908 
909  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
910 
911  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
912  *out << "\nEnd Result: TEST FAILED" << std::endl;
913  return parse_return;
914  }
915 
916  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
917 
918  result = testArray<int>(n,*out);
919  if (!result) success = false;
920 
921  result = testArray<float>(n,*out);
922  if (!result) success = false;
923 
924  result = testArray<double>(n,*out);
925  if (!result) success = false;
926 
927  //result = testArray<std::complex<double> >(n,*out);
928  //if (!result) success = false;
929  // 2007/12/03: rabartl: Commented this out so I can test comparison operators
930 
931  result = testArrayOpaqueWithoutTNT<Opaque_handle>("Opaque_handle", n,
932  OPAQUE_HANDLE_NULL, *out);
933  if (!result) success = false;
934 
935  result = testArrayOpaqueWithTNT<Opaque2_handle>(n, OPAQUE2_HANDLE_NULL, *out);
936  if (!result) success = false;
937 
938  result = testArrayOpaqueWithTNT<Opaque3_handle>(n, OPAQUE3_HANDLE_NULL, *out);
939  if (!result) success = false;
940 
941  // ToDo: Fill in the rest of the types!
942 
943  }
944  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
945 
946  if (success)
947  *out << "\nEnd Result: TEST PASSED" << std::endl;
948  else
949  *out << "\nEnd Result: TEST FAILED" << std::endl;
950 
951  return ( success ? 0 : 1 );
952 
953 }
Dangling reference error exception class.
Null reference error exception class.
const Opaque_handle OPAQUE_HANDLE_NULL
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
RawPointerConversionTraits< Container >::Ptr_t getRawPtr(const Container &c)
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEST_COMPARE(v1, comp, v2)
Assert that v1 comp v2 (where comp = &#39;==&#39;, &#39;&gt;=&quot;, &quot;!=", etc).
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.
const Opaque2_handle OPAQUE2_HANDLE_NULL
Utilities to make writing tests easier.
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.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
const Opaque3_handle OPAQUE3_HANDLE_NULL
bool testArrayOpaqueWithTNT(const int n, const T &someValue, Teuchos::FancyOStream &out)
Definition: Array_test.cpp:802
bool testArray(const int n, Teuchos::FancyOStream &out)
Definition: Array_test.cpp:29
std::string Teuchos_Version()
Incompatiable iterators error exception class.
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[])
Templated array class derived from the STL std::vector.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
bool testArrayOpaqueWithoutTNT(const std::string &T_name, const int n, const T &someValue, Teuchos::FancyOStream &out)
Definition: Array_test.cpp:731
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
Test that two values are equal.
Nonowning array view.
Default traits class that just returns typeid(T).name().
std::vector< T > createVector(const Array< T > &a)
Copy conversion to an std::vector.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
Assert that a[i] == val.
Basic command line parser for input from (argc,argv[])
Smart reference counting pointer class for automatic garbage collection.
Range error exception class.
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...
void swap(Teuchos::any &a, Teuchos::any &b)
Special swap for other code to find via Argument Dependent Lookup.
Definition of Teuchos::as, for conversions between types.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...