35 #define TEST_EQUALITY_CONST( v1, v2 ) \
36 TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
38 #define TEST_EQUALITY( v1, v2 ) \
39 TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
41 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
42 TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
44 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
45 TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
47 #define TEST_COMPARE( v1, comp, v2 ) \
48 TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
50 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
52 const bool result = compareArrays(a1,#a1,a2,#a2,out); \
53 if (!result) success = false; \
56 #define TEST_THROW( code, ExceptType ) \
57 TEUCHOS_TEST_THROW( code, ExceptType, out, success )
59 #define TEST_NOTHROW( code ) \
60 TEUCHOS_TEST_NOTHROW( code, out, success )
73 using Teuchos::arrayView;
74 using Teuchos::arrayViewFromVector;
75 using Teuchos::outArg;
80 typedef typename ArrayView<T>::size_type size_type;
92 const size_type arbitrarySizeTypeValue = 0;
93 (void) arbitrarySizeTypeValue;
99 <<
"\n*** Testing "<<TypeNameTraits<ArrayView<T> >::name()<<
" of size = "<<n
105 out <<
"\nA) Initial setup testing ...\n\n";
109 out <<
"\nTesting basic null construction!\n\n";
116 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
124 ArrayView<const T> cav2(av2);
129 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
135 #ifdef SHOW_INVALID_CONST_ASSIGN
146 const ArrayView<const T> cav = av;
149 out <<
"\nInitializing data for std::vector v through view av ...\n";
150 for(
int i = 0; i <
n; ++i )
155 out <<
"\nTest that v[i] == i through ArrayView<const T> ...\n";
156 const ArrayView<const T> cav2 = cav;
157 bool local_success =
true;
158 for(
int i = 0; i <
n; ++i ) {
161 if (local_success) out <<
"passed\n";
162 else success =
false;
166 out <<
"\nTest explicit copy to std::vector from non-const array view ...\n";
167 std::vector<T> v2 = Teuchos::createVector(av);
172 out <<
"\nTest explicit copy to std::vector from const array view ...\n";
173 std::vector<T> v2 = Teuchos::createVector(cav);
178 out <<
"\nTest shallow implicit conversion from ArrayView<T> to ArrayView<T> ...\n";
179 ArrayView<T> av2(av);
184 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<const T> ...\n";
185 ArrayView<const T> cav2(cav);
190 out <<
"\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<T> ...\n";
191 ArrayView<const T> cav2(av);
196 out <<
"\nTest shallow implicit conversion from std::vector<T> to ArrayView<T> ...\n";
197 std::vector<T> v2 = Teuchos::createVector(cav);
198 ArrayView<T> cav2(v2);
203 out <<
"\nTest shallow implicit conversion from const std::vector<T> to ArrayView<const T> ...\n";
204 const std::vector<T> v2 = Teuchos::createVector(cav);
205 ArrayView<const T> cav2(v2);
211 #ifdef SHOW_INVALID_COPY_CONSTRUCTION
212 ArrayView<T> cav2(cav);
217 out <<
"\ntest assign(...) ...\n";
218 std::vector<T> v2(n);
219 ArrayView<T> av2(v2);
225 out <<
"\nB) Test element access ...\n";
233 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
238 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
241 out <<
"\nC) Test iterator access ...\n";
246 out <<
"\nTest non-const forward iterator access ...\n";
247 std::vector<T> v2(n);
248 ArrayView<T> av2(v2);
249 typedef typename ArrayView<T>::iterator iter_t;
250 iter_t iter = av2.begin();
251 for (
int i = 0; iter != av2.end(); ++i )
257 out <<
"\nTest const forward iterator access ... ";
258 bool local_success =
true;
259 typedef typename ArrayView<const T>::iterator iter_t;
260 const ArrayView<const T> cav2 = av.getConst();
261 iter_t iter = cav2.begin();
262 for (
int i = 0; i <
n; ++i, ++iter ) {
265 #ifdef SHOW_INVALID_CONST_ITER_MODIFICATION
269 iter = NullIteratorTraits<iter_t>::getNull();
270 if (local_success) out <<
"passed\n";
271 else success =
false;
275 out <<
"\nD) Test sub-views ...\n";
279 out <<
"\nTest full non-const subview ...\n";
280 const ArrayView<T> av2 = av(0,n);
285 out <<
"\nTest full shorthand non-const subview ...\n";
286 const ArrayView<T> av2 = av();
291 out <<
"\nTest full const subview ...\n";
292 const ArrayView<const T> cav2 = cav(0,n);
297 out <<
"\nTest full non-const to const subview ...\n";
298 const ArrayView<const T> cav2 = av(0,n);
303 out <<
"\nTest full short-hand const subview ...\n";
304 const ArrayView<const T> cav2 = cav();
309 out <<
"\nTest non-const initial range view ...\n";
310 std::vector<T> v2(n,as<T>(-1));
311 const ArrayView<T> av2(v2);
312 const ArrayView<T> av2_init = av2(0,n-1);
314 av2_init.assign( av(0,n-1) );
315 av2.back() = as<T>(n-1);
320 out <<
"\nTest non-const final range view ...\n";
321 std::vector<T> v2(n,as<T>(-1));
322 const ArrayView<T> av2(v2);
323 const ArrayView<T> av2_init = av2(1,n-1);
325 av2_init.assign( av(1,n-1) );
326 av2.front() = as<T>(0);
331 out <<
"\nTest non-const middle range view ...\n";
332 std::vector<T> v2(n,as<T>(-1));
333 const ArrayView<T> av2(v2);
334 const ArrayView<T> av2_init = av2(1,n-2);
336 av2_init.assign( av(1,n-2) );
337 av2.front() = as<T>(0);
338 av2.back() = as<T>(n-1);
353 int main(
int argc,
char* argv[] )
372 CommandLineProcessor clp(
false);
375 clp.setOption(
"n", &n,
"Number of elements in the array" );
377 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
379 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
380 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
386 result = testArrayView<int>(
n,*out);
387 if (!result) success =
false;
389 result = testArrayView<float>(
n,*out);
390 if (!result) success =
false;
392 result = testArrayView<double>(
n,*out);
393 if (!result) success =
false;
395 result = testArrayView<std::complex<double> >(
n,*out);
396 if (!result) success =
false;
402 *out <<
"\nEnd Result: TEST PASSED" << std::endl;
404 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
406 return ( success ? 0 : 1 );
#define TEST_NOTHROW(code)
Null reference error exception class.
#define TEST_EQUALITY_CONST(v1, v2)
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)
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.
int main(int argc, char *argv[])
bool testArrayView(const int n, Teuchos::FancyOStream &out)
Utilities to make writing tests easier.
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[])
#define TEST_THROW(code, ExceptType)
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.