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