105 using Teuchos::arrayView;
106 using Teuchos::arrayViewFromVector;
107 using Teuchos::outArg;
112 typedef typename ArrayView<T>::size_type size_type;
124 const size_type arbitrarySizeTypeValue = 0;
125 (void) arbitrarySizeTypeValue;
131 <<
"\n*** Testing "<<TypeNameTraits<ArrayView<T> >::name()<<
" of size = "<<n
137 out <<
"\nA) Initial setup testing ...\n\n";
141 out <<
"\nTesting basic null construction!\n\n";
148#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
156 ArrayView<const T> cav2(av2);
161#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
167#ifdef SHOW_INVALID_CONST_ASSIGN
174 const ArrayView<T> av = arrayViewFromVector(v);
178 const ArrayView<const T> cav = av;
181 out <<
"\nInitializing data for std::vector v through view av ...\n";
182 for(
int i = 0; i < n; ++i )
187 out <<
"\nTest that v[i] == i through ArrayView<const T> ...\n";
188 const ArrayView<const T> cav2 = cav;
189 bool local_success =
true;
190 for(
int i = 0; i < n; ++i ) {
193 if (local_success) out <<
"passed\n";
194 else success =
false;
198 out <<
"\nTest explicit copy to std::vector from non-const array view ...\n";
199 std::vector<T> v2 = Teuchos::createVector(av);
204 out <<
"\nTest explicit copy to std::vector from const array view ...\n";
205 std::vector<T> v2 = Teuchos::createVector(cav);
210 out <<
"\nTest shallow implicit conversion from ArrayView<T> to ArrayView<T> ...\n";
211 ArrayView<T> av2(av);
216 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<const T> ...\n";
217 ArrayView<const T> cav2(cav);
222 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<T> ...\n";
223 ArrayView<const T> cav2(av);
228 out <<
"\nTest shallow implicit conversion from std::vector<T> to ArrayView<T> ...\n";
229 std::vector<T> v2 = Teuchos::createVector(cav);
230 ArrayView<T> cav2(v2);
235 out <<
"\nTest shallow implicit conversion from const std::vector<T> to ArrayView<const T> ...\n";
236 const std::vector<T> v2 = Teuchos::createVector(cav);
237 ArrayView<const T> cav2(v2);
243#ifdef SHOW_INVALID_COPY_CONSTRUCTION
244 ArrayView<T> cav2(cav);
249 out <<
"\ntest assign(...) ...\n";
250 std::vector<T> v2(n);
251 ArrayView<T> av2(v2);
257 out <<
"\nB) Test element access ...\n";
265#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
273 out <<
"\nC) Test iterator access ...\n";
278 out <<
"\nTest non-const forward iterator access ...\n";
279 std::vector<T> v2(n);
280 ArrayView<T> av2(v2);
281 typedef typename ArrayView<T>::iterator iter_t;
282 iter_t iter = av2.begin();
283 for (
int i = 0; iter != av2.end(); ++i )
289 out <<
"\nTest const forward iterator access ... ";
290 bool local_success =
true;
291 typedef typename ArrayView<const T>::iterator iter_t;
292 const ArrayView<const T> cav2 = av.getConst();
293 iter_t iter = cav2.begin();
294 for (
int i = 0; i < n; ++i, ++iter ) {
297#ifdef SHOW_INVALID_CONST_ITER_MODIFICATION
301 iter = NullIteratorTraits<iter_t>::getNull();
302 if (local_success) out <<
"passed\n";
303 else success =
false;
307 out <<
"\nD) Test sub-views ...\n";
311 out <<
"\nTest full non-const subview ...\n";
312 const ArrayView<T> av2 = av(0,n);
317 out <<
"\nTest full shorthand non-const subview ...\n";
318 const ArrayView<T> av2 = av();
323 out <<
"\nTest full const subview ...\n";
324 const ArrayView<const T> cav2 = cav(0,n);
329 out <<
"\nTest full non-const to const subview ...\n";
330 const ArrayView<const T> cav2 = av(0,n);
335 out <<
"\nTest full short-hand const subview ...\n";
336 const ArrayView<const T> cav2 = cav();
341 out <<
"\nTest non-const initial range view ...\n";
342 std::vector<T> v2(n,as<T>(-1));
343 const ArrayView<T> av2(v2);
344 const ArrayView<T> av2_init = av2(0,n-1);
346 av2_init.assign( av(0,n-1) );
347 av2.back() = as<T>(n-1);
352 out <<
"\nTest non-const final range view ...\n";
353 std::vector<T> v2(n,as<T>(-1));
354 const ArrayView<T> av2(v2);
355 const ArrayView<T> av2_init = av2(1,n-1);
357 av2_init.assign( av(1,n-1) );
358 av2.front() = as<T>(0);
363 out <<
"\nTest non-const middle range view ...\n";
364 std::vector<T> v2(n,as<T>(-1));
365 const ArrayView<T> av2(v2);
366 const ArrayView<T> av2_init = av2(1,n-2);
368 av2_init.assign( av(1,n-2) );
369 av2.front() = as<T>(0);
370 av2.back() = as<T>(n-1);
385int main(
int argc,
char* argv[] )
404 CommandLineProcessor clp(
false);
407 clp.setOption(
"n", &n,
"Number of elements in the array" );
409 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
411 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
412 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
418 result = testArrayView<int>(n,*out);
419 if (!result) success =
false;
421 result = testArrayView<float>(n,*out);
422 if (!result) success =
false;
424 result = testArrayView<double>(n,*out);
425 if (!result) success =
false;
427 result = testArrayView<std::complex<double> >(n,*out);
428 if (!result) success =
false;
434 *out <<
"\nEnd Result: TEST PASSED" << std::endl;
436 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
438 return ( success ? 0 : 1 );