67 #define TEST_EQUALITY_CONST( v1, v2 ) \
68 TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
70 #define TEST_EQUALITY( v1, v2 ) \
71 TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
73 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
74 TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
76 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
77 TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
79 #define TEST_COMPARE( v1, comp, v2 ) \
80 TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
82 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
84 const bool result = compareArrays(a1,#a1,a2,#a2,out); \
85 if (!result) success = false; \
88 #define TEST_THROW( code, ExceptType ) \
89 TEUCHOS_TEST_THROW( code, ExceptType, out, success )
91 #define TEST_NOTHROW( code ) \
92 TEUCHOS_TEST_NOTHROW( code, out, success )
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";
147 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
155 ArrayView<const T> cav2(av2);
159 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
165 #ifdef SHOW_INVALID_CONST_ASSIGN
176 const ArrayView<const T> cav = av;
179 out <<
"\nInitializing data for std::vector v through view av ...\n";
180 for(
int i = 0; i <
n; ++i )
185 out <<
"\nTest that v[i] == i through ArrayView<const T> ...\n";
186 const ArrayView<const T> cav2 = cav;
187 bool local_success =
true;
188 for(
int i = 0; i <
n; ++i ) {
191 if (local_success) out <<
"passed\n";
192 else success =
false;
196 out <<
"\nTest explicit copy to std::vector from non-const array view ...\n";
197 std::vector<T> v2 = Teuchos::createVector(av);
202 out <<
"\nTest explicit copy to std::vector from const array view ...\n";
203 std::vector<T> v2 = Teuchos::createVector(cav);
208 out <<
"\nTest shallow implicit conversion from ArrayView<T> to ArrayView<T> ...\n";
209 ArrayView<T> av2(av);
214 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<const T> ...\n";
215 ArrayView<const T> cav2(cav);
220 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<T> ...\n";
221 ArrayView<const T> cav2(av);
226 out <<
"\nTest shallow implicit conversion from std::vector<T> to ArrayView<T> ...\n";
227 std::vector<T> v2 = Teuchos::createVector(cav);
228 ArrayView<T> cav2(v2);
233 out <<
"\nTest shallow implicit conversion from const std::vector<T> to ArrayView<const T> ...\n";
234 const std::vector<T> v2 = Teuchos::createVector(cav);
235 ArrayView<const T> cav2(v2);
241 #ifdef SHOW_INVALID_COPY_CONSTRUCTION
242 ArrayView<T> cav2(cav);
247 out <<
"\ntest assign(...) ...\n";
248 std::vector<T> v2(
n);
249 ArrayView<T> av2(v2);
255 out <<
"\nB) Test element access ...\n";
263 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
268 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
271 out <<
"\nC) Test iterator access ...\n";
276 out <<
"\nTest non-const forward iterator access ...\n";
277 std::vector<T> v2(
n);
278 ArrayView<T> av2(v2);
279 typedef typename ArrayView<T>::iterator iter_t;
280 iter_t iter = av2.begin();
281 for (
int i = 0; iter != av2.end(); ++i )
287 out <<
"\nTest const forward iterator access ... ";
288 bool local_success =
true;
289 typedef typename ArrayView<const T>::iterator iter_t;
290 const ArrayView<const T> cav2 = av.getConst();
291 iter_t iter = cav2.begin();
292 for (
int i = 0; i <
n; ++i, ++iter ) {
295 #ifdef SHOW_INVALID_CONST_ITER_MODIFICATION
299 iter = NullIteratorTraits<iter_t>::getNull();
300 if (local_success) out <<
"passed\n";
301 else success =
false;
305 out <<
"\nD) Test sub-views ...\n";
309 out <<
"\nTest full non-const subview ...\n";
310 const ArrayView<T> av2 = av(0,
n);
315 out <<
"\nTest full shorthand non-const subview ...\n";
316 const ArrayView<T> av2 = av();
321 out <<
"\nTest full const subview ...\n";
322 const ArrayView<const T> cav2 = cav(0,
n);
327 out <<
"\nTest full non-const to const subview ...\n";
328 const ArrayView<const T> cav2 = av(0,
n);
333 out <<
"\nTest full short-hand const subview ...\n";
334 const ArrayView<const T> cav2 = cav();
339 out <<
"\nTest non-const initial range view ...\n";
340 std::vector<T> v2(
n,as<T>(-1));
341 const ArrayView<T> av2(v2);
342 const ArrayView<T> av2_init = av2(0,
n-1);
344 av2_init.assign( av(0,
n-1) );
345 av2.back() = as<T>(
n-1);
350 out <<
"\nTest non-const final range view ...\n";
351 std::vector<T> v2(
n,as<T>(-1));
352 const ArrayView<T> av2(v2);
353 const ArrayView<T> av2_init = av2(1,
n-1);
355 av2_init.assign( av(1,
n-1) );
356 av2.front() = as<T>(0);
361 out <<
"\nTest non-const middle range view ...\n";
362 std::vector<T> v2(
n,as<T>(-1));
363 const ArrayView<T> av2(v2);
364 const ArrayView<T> av2_init = av2(1,
n-2);
366 av2_init.assign( av(1,
n-2) );
367 av2.front() = as<T>(0);
368 av2.back() = as<T>(
n-1);
383 int main(
int argc,
char* argv[] )
402 CommandLineProcessor clp(
false);
405 clp.setOption(
"n", &
n,
"Number of elements in the array" );
407 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
409 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
410 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
416 result = testArrayView<int>(
n,*out);
417 if (!result) success =
false;
419 result = testArrayView<float>(
n,*out);
420 if (!result) success =
false;
422 result = testArrayView<double>(
n,*out);
423 if (!result) success =
false;
425 result = testArrayView<std::complex<double> >(
n,*out);
426 if (!result) success =
false;
432 *out <<
"\nEnd Result: TEST PASSED" << std::endl;
434 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
436 return ( success ? 0 : 1 );