/*! \file helpertest.cc * \brief test helpers like iterator, subarray and slice (implementation) * * ---------------------------------------------------------------------------- * * $Id: helpertest.cc,v 1.7 2007-12-10 16:10:07 tforb Exp $ * \author Thomas Forbriger * \date 23/12/2002 * * test helpers like iterator, subarray and slice (implementation) * * ---- * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ---- * * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) * * REVISIONS and CHANGES * - 23/12/2002 V1.0 Thomas Forbriger * - 28/12/2002 V1.1 (thof) * - new term for containers of const elements * - 29/12/2002 V1.2 (thof) * - new subarray function * - test subarray of ConstArray * - new slice function * - test slice of ConstArray * - now no resolution conflicts with const containers * are experienced anymore * - 10/12/2007 V1.3 (thof) * - scanning a const array requires a Browser * * ============================================================================ */ #define AFF_HELPERTEST_CC_VERSION \ "AFF_HELPERTEST_CC V1.3" #define AFF_HELPERTEST_CC_CVSID \ "$Id: helpertest.cc,v 1.7 2007-12-10 16:10:07 tforb Exp $" /*! \example tests/helpertest.cc * * Test helper classes. * * This test program gives an example of the usage of the following classes, * functions, and preprocessor macros: * - aff::Iterator * - aff::util::Subarray * - aff::subarray * - aff::utl::Slice * - aff::slice * - aff::Array * - aff::ConstArray * - aff::Shaper * - aff::dump * - aff::dump_array * - #DUMP * - #CODE * * \sa tests/helpertest.cc */ #include #include #include #include #include #include using std::cout; using std::endl; using namespace aff; /*----------------------------------------------------------------------*/ //! print headline void section(const char* s, const char l='-') { cout << endl << s << endl; const char* p=s; while (*p) { cout << l; ++p; } cout << endl; } /*----------------------------------------------------------------------*/ //! test for passing subarrays and slices template void printarray(const ConstArray& array) { cout << endl; cout << "Received array in function:" << endl; dump_array(array); } /*======================================================================*/ //! test helper classes int main() { cout << AFF_HELPERTEST_CC_VERSION << endl; cout << AFF_HELPERTEST_CC_CVSID << endl; section("Preparing test array"); CODE( Array A(Shaper(-2,2)(-3,3)) ); CODE( dump_array(A) ); section("Tests for class Iterator", '='); { CODE(int i=0); CODE(for( Iterator > I(A); I.valid(); ++I) { *I=i; i++; }); CODE( dump_array(A) ); CODE( ConstArray CA(A.copyout()) ); CODE( i=1); // use a browser here CODE(for( Browser > I(CA); I.valid(); ++I) { cout << i << ", " << *I << endl; i++; }); } CODE( ConstArray OrigA(A.copyout()) ); /*----------------------------------------------------------------------*/ section("Tests for class Subarray", '='); { section("First mode: Use a Subarray object just for initialization",'.'); CODE(Array B=aff::util::Subarray >(A)(-1,1)(-2,2)); CODE( dump_array(B) ); CODE(B=-5); CODE( dump_array(A) ); // you must explicitely create an array through member function array() CODE(printarray(aff::util::Subarray >(A)(-2,0)(-3,0).array())); } section("Tests of subarray function", '-'); { CODE( A.copyin(OrigA) ); section("First mode: Use a Subarray object just for initialization",'.'); CODE(Array B=subarray(A)(-1,1)(-2,2)); CODE( dump_array(B) ); CODE(B=-5); CODE( dump_array(A) ); section("Second mode: Create a Subarray object to which you can assign", '.'); CODE(subarray(A)()(1,2)=100); CODE(subarray(A)(1,1)=-200); CODE( dump_array(A) ); // you must explicitely create an array through member function array() CODE(printarray(subarray(A)(-2,0)(-3,0).array())); } section("Test for subarray of ConstArray",'-'); { CODE( dump_array(OrigA) ); CODE(ConstArray B=subarray(OrigA)(-1,1)(-2,2)); CODE( dump_array(B) ); // you must explicitely create an array through member function array() CODE(printarray(subarray(OrigA)(-2,0)(-3,0).array())); } /*----------------------------------------------------------------------*/ section("Tests for class Slice", '='); { CODE( A.copyin(OrigA) ); section("First mode: Use a Slice object just for initialization",'.'); CODE(Array B=aff::util::Slice >(A)(1,-1)); CODE( dump_array(B) ); CODE(B=555); CODE( dump_array(A) ); // you must explicitely create an array through member function array() CODE(printarray(aff::util::Slice >(A)(-2).array())); } section("Test slice function", '-'); { CODE( A.copyin(OrigA) ); section("First mode: Use a Slice object just for initialization",'.'); CODE(Array B=slice(A)(1,-1)); CODE( dump_array(B) ); CODE(B=555); CODE( dump_array(A) ); section("Second mode: Create a Slice object to which you can assign", '.'); CODE(slice(A)()(1)=666); CODE(slice(A)(0)=-777); CODE( dump_array(A) ); // you must explicitely create an array through member function array() CODE(printarray(slice(A)(-2).array())); } section("Test slice of ConstArray",'-'); { CODE( dump_array(OrigA) ); CODE(ConstArray B=slice(OrigA)(1,-1)); CODE( dump_array(B) ); // you must explicitely create an array through member function array() CODE(printarray(slice(OrigA)(-2).array())); } /*----------------------------------------------------------------------*/ section("Mixed tests", '='); section("Iterator on a Subarray"); { CODE(int i=501); CODE(Array B=subarray(A)(-1,1)(-2,2)); CODE(for( Iterator > I(B); I.valid(); ++I) { *I=i; i++; }); CODE( dump_array(A) ); } section("Iterator on a Slice"); { CODE(int i=-801); CODE(Array B=slice(A)(1)); CODE(for( Iterator > I(B); I.valid(); ++I) { *I=i; i++; }); CODE( dump_array(A) ); } section("Extract copy of subarray"); { CODE(Array B=subarray(A)(-1,1)(-1,1)); CODE(dump_array(B)); CODE(Array C=B.copyout()); CODE(dump_array(C)); } } /* ----- END OF helpertest.cc ----- */