Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
ArrayView_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_ArrayView.hpp"
47 #include "Teuchos_Version.hpp"
48 #include "Teuchos_getConst.hpp"
49 #include "Teuchos_as.hpp"
51 
52 
53 // Uncomment to show compile errors from invalid usage
54 //#define SHOW_INVALID_COPY_CONSTRUCTION
55 //#define SHOW_INVALID_CONST_ASSIGN
56 //#define SHOW_INVALID_CONST_ITER_MODIFICATION
57 
58 //
59 // Define local macros to make defining tests easier for this particular test
60 // code.
61 //
62 // Note, macros with these types of names should only exist in a *.cpp file
63 // after all #includes are done!
64 //
65 
66 
67 #define TEST_EQUALITY_CONST( v1, v2 ) \
68  TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
69 
70 #define TEST_EQUALITY( v1, v2 ) \
71  TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
72 
73 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
74  TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
75 
76 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
77  TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
78 
79 #define TEST_COMPARE( v1, comp, v2 ) \
80  TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
81 
82 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
83  { \
84  const bool result = compareArrays(a1,#a1,a2,#a2,out); \
85  if (!result) success = false; \
86  }
87 
88 #define TEST_THROW( code, ExceptType ) \
89  TEUCHOS_TEST_THROW( code, ExceptType, out, success )
90 
91 #define TEST_NOTHROW( code ) \
92  TEUCHOS_TEST_NOTHROW( code, out, success )
93 
94 
95 //
96 // Main templated array test function
97 //
98 
99 
100 template<class T>
101 bool testArrayView( const int n, Teuchos::FancyOStream &out )
102 {
103 
104  using Teuchos::ArrayView;
105  using Teuchos::arrayView;
106  using Teuchos::arrayViewFromVector;
107  using Teuchos::outArg;
110  using Teuchos::getConst;
111  using Teuchos::as;
112  typedef typename ArrayView<T>::size_type size_type;
113 
114  bool success = true;
115 
116  out
117  << "\n***"
118  << "\n*** Testing "<<TypeNameTraits<ArrayView<T> >::name()<<" of size = "<<n
119  << "\n***\n";
120 
121  Teuchos::OSTab tab(out);
122 
123  //
124  out << "\nA) Initial setup testing ...\n\n";
125  //
126 
127  {
128  out << "\nTesting basic null construction!\n\n";
129  ArrayView<T> av2 = Teuchos::null;
130  TEST_EQUALITY_CONST(is_null(av2),true);
131  TEST_EQUALITY_CONST(av2.size(),0);
132  TEST_EQUALITY_CONST(av2.getRawPtr(),0);
133  TEST_ITER_EQUALITY(av2.begin(),av2.end());
134 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
138  TEST_THROW(av2.assign(av2), Teuchos::NullReferenceError);
141 #endif
142  ArrayView<const T> cav2(av2); // Tests copy constructor and implicit conversion operator!
143  TEST_EQUALITY_CONST(cav2.size(),0);
144  TEST_EQUALITY_CONST(cav2.getRawPtr(),0);
145  TEST_ITER_EQUALITY(cav2.begin(),av2.end());
146 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
148  TEST_THROW(*cav2.begin(), Teuchos::NullReferenceError);
151 #endif
152 #ifdef SHOW_INVALID_CONST_ASSIGN
153  TEST_NOTHROW(cav2.assign(av2)); // Should not compile!
154 #endif
155  }
156 
157  std::vector<T> v(n);
158 
159  const ArrayView<T> av = arrayViewFromVector(v);
160  TEST_EQUALITY_CONST(is_null(av), false);
161  TEST_EQUALITY( as<int>(av.size()), n );
162 
163  const ArrayView<const T> cav = av;
164 
165  {
166  out << "\nInitializing data for std::vector v through view av ...\n";
167  for( int i = 0; i < n; ++i )
168  av[i] = i; // tests non-const operator[](i)
169  }
170 
171  {
172  out << "\nTest that v[i] == i through ArrayView<const T> ...\n";
173  const ArrayView<const T> cav2 = cav;
174  bool local_success = true;
175  for( int i = 0; i < n; ++i ) {
176  TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
177  }
178  if (local_success) out << "passed\n";
179  else success = false;
180  }
181 
182  {
183  out << "\nTest explicit copy to std::vector from non-const array view ...\n";
184  std::vector<T> v2 = Teuchos::createVector(av);
185  TEST_COMPARE_ARRAYS( v2, v );
186  }
187 
188  {
189  out << "\nTest explicit copy to std::vector from const array view ...\n";
190  std::vector<T> v2 = Teuchos::createVector(cav);
191  TEST_COMPARE_ARRAYS( v2, v );
192  }
193 
194  {
195  out << "\nTest shallow implicit conversion from ArrayView<T> to ArrayView<T> ...\n";
196  ArrayView<T> av2(av);
197  TEST_COMPARE_ARRAYS( av2, av );
198  }
199 
200  {
201  out << "\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<const T> ...\n";
202  ArrayView<const T> cav2(cav);
203  TEST_COMPARE_ARRAYS( cav2, cav );
204  }
205 
206  {
207  out << "\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<T> ...\n";
208  ArrayView<const T> cav2(av);
209  TEST_COMPARE_ARRAYS( cav2, av );
210  }
211 
212  {
213  out << "\nTest shallow implicit conversion from std::vector<T> to ArrayView<T> ...\n";
214  std::vector<T> v2 = Teuchos::createVector(cav);
215  ArrayView<T> cav2(v2);
216  TEST_COMPARE_ARRAYS( cav2, av );
217  }
218 
219  {
220  out << "\nTest shallow implicit conversion from const std::vector<T> to ArrayView<const T> ...\n";
221  const std::vector<T> v2 = Teuchos::createVector(cav);
222  ArrayView<const T> cav2(v2);
223  TEST_COMPARE_ARRAYS( cav2, av );
224  }
225 
226  {
227  // Try to copy construct from ArrayView<const T> to ArrayView<T> ..
228 #ifdef SHOW_INVALID_COPY_CONSTRUCTION
229  ArrayView<T> cav2(cav); // should not compile!
230 #endif
231  }
232 
233  {
234  out << "\ntest assign(...) ...\n";
235  std::vector<T> v2(n);
236  ArrayView<T> av2(v2);
237  av2.assign(av);
238  TEST_COMPARE_ARRAYS( v2, v );
239  }
240 
241  //
242  out << "\nB) Test element access ...\n";
243  //
244 
245 
246  TEST_EQUALITY_CONST( av.front(), as<T>(0) );
247  TEST_EQUALITY( av.back(), as<T>(n-1) );
248  TEST_EQUALITY_CONST( cav.front(), as<T>(0) );
249  TEST_EQUALITY( cav.back(), as<T>(n-1) );
250 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
251  TEST_THROW( av[-1], Teuchos::RangeError );
253  TEST_THROW( cav[-1], Teuchos::RangeError );
254  TEST_THROW( cav[n], Teuchos::RangeError );
255 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
256 
257  //
258  out << "\nC) Test iterator access ...\n";
259  //
260 
261 
262  {
263  out << "\nTest non-const forward iterator access ...\n";
264  std::vector<T> v2(n);
265  ArrayView<T> av2(v2);
266  typedef typename ArrayView<T>::iterator iter_t;
267  iter_t iter = av2.begin();
268  for ( int i = 0; iter != av2.end(); ++i )
269  *iter++ = i;
270  TEST_COMPARE_ARRAYS( v2, v );
271  }
272 
273  {
274  out << "\nTest const forward iterator access ... ";
275  bool local_success = true;
276  typedef typename ArrayView<const T>::iterator iter_t;
277  const ArrayView<const T> cav2 = av.getConst();
278  iter_t iter = cav2.begin();
279  for ( int i = 0; i < n; ++i, ++iter ) {
280  TEST_ARRAY_ELE_EQUALITY( av, i, *iter );
281 
282 #ifdef SHOW_INVALID_CONST_ITER_MODIFICATION
283  *iter = as<T>(i); // Should not compile!
284 #endif
285  }
286  iter = NullIteratorTraits<iter_t>::getNull();
287  if (local_success) out << "passed\n";
288  else success = false;
289  }
290 
291  //
292  out << "\nD) Test sub-views ...\n";
293  //
294 
295  {
296  out << "\nTest full non-const subview ...\n";
297  const ArrayView<T> av2 = av(0,n);
298  TEST_COMPARE_ARRAYS( av2, av );
299  }
300 
301  {
302  out << "\nTest full shorthand non-const subview ...\n";
303  const ArrayView<T> av2 = av();
304  TEST_COMPARE_ARRAYS( av2, av );
305  }
306 
307  {
308  out << "\nTest full const subview ...\n";
309  const ArrayView<const T> cav2 = cav(0,n);
310  TEST_COMPARE_ARRAYS( cav2, cav );
311  }
312 
313  {
314  out << "\nTest full non-const to const subview ...\n";
315  const ArrayView<const T> cav2 = av(0,n);
316  TEST_COMPARE_ARRAYS( cav2, cav );
317  }
318 
319  {
320  out << "\nTest full short-hand const subview ...\n";
321  const ArrayView<const T> cav2 = cav();
322  TEST_COMPARE_ARRAYS( cav2, cav );
323  }
324 
325  {
326  out << "\nTest non-const initial range view ...\n";
327  std::vector<T> v2(n,as<T>(-1));
328  const ArrayView<T> av2(v2);
329  const ArrayView<T> av2_init = av2(0,n-1);
330  TEST_EQUALITY( av2_init.size(), n-1 );
331  av2_init.assign( av(0,n-1) );
332  av2.back() = as<T>(n-1);
333  TEST_COMPARE_ARRAYS( v2, v );
334  }
335 
336  {
337  out << "\nTest non-const final range view ...\n";
338  std::vector<T> v2(n,as<T>(-1));
339  const ArrayView<T> av2(v2);
340  const ArrayView<T> av2_init = av2(1,n-1);
341  TEST_EQUALITY( av2_init.size(), n-1 );
342  av2_init.assign( av(1,n-1) );
343  av2.front() = as<T>(0);
344  TEST_COMPARE_ARRAYS( v2, v );
345  }
346 
347  {
348  out << "\nTest non-const middle range view ...\n";
349  std::vector<T> v2(n,as<T>(-1));
350  const ArrayView<T> av2(v2);
351  const ArrayView<T> av2_init = av2(1,n-2);
352  TEST_EQUALITY( av2_init.size(), n-2 );
353  av2_init.assign( av(1,n-2) );
354  av2.front() = as<T>(0);
355  av2.back() = as<T>(n-1);
356  TEST_COMPARE_ARRAYS( v2, v );
357  }
358 
359  // ToDo: Test requesting views outside of valid range!
360 
361  return success;
362 
363 }
364 
365 
366 //
367 // Main testing program
368 //
369 
370 int main( int argc, char* argv[] )
371 {
372 
373  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
374 
376 
377  bool success = true;
378  bool result;
379 
382 
383  try {
384 
385  //
386  // Read options from the commandline
387  //
388 
389  CommandLineProcessor clp(false); // Don't throw exceptions
390 
391  int n = 4;
392  clp.setOption( "n", &n, "Number of elements in the array" );
393 
394  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
395 
396  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
397  *out << "\nEnd Result: TEST FAILED" << std::endl;
398  return parse_return;
399  }
400 
401  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
402 
403  result = testArrayView<int>(n,*out);
404  if (!result) success = false;
405 
406  result = testArrayView<float>(n,*out);
407  if (!result) success = false;
408 
409  result = testArrayView<double>(n,*out);
410  if (!result) success = false;
411 
412  result = testArrayView<std::complex<double> >(n,*out);
413  if (!result) success = false;
414 
415  }
416  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
417 
418  if (success)
419  *out << "\nEnd Result: TEST PASSED" << std::endl;
420  else
421  *out << "\nEnd Result: TEST FAILED" << std::endl;
422 
423  return ( success ? 0 : 1 );
424 
425 }
Null reference error exception class.
#define TEST_EQUALITY_CONST(v1, v2)
#define TEST_THROW(code, ExceptType )
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
ArrayView< T > arrayViewFromVector(std::vector< T > &vec)
Construct a non-const view of an std::vector.
#define TEST_ITER_EQUALITY(iter1, iter2)
#define TEST_NOTHROW(code )
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
Initialize, finalize, and query the global MPI session.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
#define TEST_COMPARE_ARRAYS(a1, a2)
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
std::string Teuchos_Version()
Base traits class for getting a properly initialized null pointer.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition: Teuchos_as.hpp:287
bool testArrayView(const int n, Teuchos::FancyOStream &out)
Utilities to make writing tests easier.
Nonowning array view.
Default traits class that just returns typeid(T).name().
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
int main(int argc, char *argv[])
Smart reference counting pointer class for automatic garbage collection.
#define TEST_ARRAY_ELE_EQUALITY(a, i, val)
#define TEST_EQUALITY(v1, v2)
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.