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