AFF --- A container for numbers (array) by Friederich and Forbriger.
tests/helpertest.cc

Test helper classes.

This test program gives an example of the usage of the following classes, functions, and preprocessor macros:

See also
tests/helpertest.cc
#define AFF_HELPERTEST_CC_VERSION \
"AFF_HELPERTEST_CC V1.4"
#include <aff/array.h>
#include <aff/dump.h>
#include <aff/shaper.h>
#include <aff/iterator.h>
#include <aff/subarray.h>
#include <aff/slice.h>
#include <aff/Carray.h>
#include <aff/converters.h>
using std::cout;
using std::endl;
using namespace aff;
/*----------------------------------------------------------------------*/
void section(const char* s, const char l='-')
{
cout << endl << s << endl;
const char* p=s;
while (*p) { cout << l; ++p; }
cout << endl;
}
/*----------------------------------------------------------------------*/
template<class T>
void printarray(const ConstArray<T>& array)
{
cout << endl;
cout << "Received array in function:" << endl;
dump_array(array);
}
/*======================================================================*/
int main()
{
cout << AFF_HELPERTEST_CC_VERSION << endl;
section("Preparing test array");
CODE( Array<int> A(Shaper(-2,2)(-3,3)) );
section("Tests for class Iterator", '=');
{
CODE(int i=0);
CODE(for( Iterator<Array<int> > I(A); I.valid(); ++I)
{ *I=i; i++; });
CODE( ConstArray<int> CA(A.copyout()) );
CODE( i=1);
// use a browser here
CODE(for( Browser<ConstArray<int> > I(CA); I.valid(); ++I)
{ cout << i << ", " << *I << endl; i++; });
}
CODE( ConstArray<int> OrigA(A.copyout()) );
/*----------------------------------------------------------------------*/
section("Tests for class Subarray", '=');
{
section("First mode: Use a Subarray object just for initialization",'.');
CODE(B=-5);
// you must explicitely create an array through member function array()
CODE(printarray(aff::util::Subarray<Array<int> >(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<int> B=subarray(A)(-1,1)(-2,2));
CODE(B=-5);
section("Second mode: Create a Subarray object to which you can assign",
'.');
CODE(subarray(A)()(1,2)=100);
CODE(subarray(A)(1,1)=-200);
// 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<int> B=subarray(OrigA)(-1,1)(-2,2));
// 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(B=555);
// you must explicitely create an array through member function array()
}
section("Test slice function", '-');
{
CODE( A.copyin(OrigA) );
section("First mode: Use a Slice object just for initialization",'.');
CODE(Array<int> B=slice(A)(1,-1));
CODE(B=555);
section("Second mode: Create a Slice object to which you can assign",
'.');
CODE(slice(A)()(1)=666);
CODE(slice(A)(0)=-777);
// 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<int> B=slice(OrigA)(1,-1));
// 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<int> B=subarray(A)(-1,1)(-2,2));
CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I)
{ *I=i; i++; });
}
section("Iterator on a Slice");
{
CODE(int i=-801);
CODE(Array<int> B=slice(A)(1));
CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I)
{ *I=i; i++; });
}
section("Extract copy of subarray");
{
CODE(Array<int> B=subarray(A)(-1,1)(-1,1));
}
/*----------------------------------------------------------------------*/
section("Test external access interfaces", '=');
section("Test interface to raw memory array: aff::CArray");
{
CODE( Array<int> A(Shaper(2)(10,16)(-2,2)(-1,0)); );
section("fill array");
for(int i=A.f(0); i<=A.l(0); i++)
{
for(int j=A.f(1); j<=A.l(1); j++)
{
for(int k=A.f(2); k<=A.l(2); k++)
{
for(int l=A.f(3); l<=A.l(3); l++)
{
A(i,j,k,l)=(i-A.f(0)+1)+(j-A.f(1)+1)*10
+(k-A.f(2)+1)*100+(l-A.f(3)+1)*1000;
}
}
}
}
CODE( dump_array(A,3); );
CODE( Array<int> B=aff::slice(A)(0,2)(2,-1); )
CODE( CArray<int> C(B); )
section("fill C array");
CODE( int* p=C.pointer(); )
CODE(
for (unsigned i=0; i<C.size(0); ++i)
{
for (unsigned j=0; j<C.size(1); ++j)
{
p[i*C.stride(0)+j*C.stride(1)]=i+10*j;
}
}
)
}
/*----------------------------------------------------------------------*/
section("Test interface to raw memory array: aff::CSeries");
{
CODE( aff::Series<int> A(-3,4); )
CODE( for (int i=A.f(); i<=A.l(); ++i) { A(i)=i; })
CODE( dump(A); )
CODE( int* p=CS.pointer(); )
CODE( for (int i=0; i<CS.size(); ++i) { p[i]=i; })
CODE( dump(A); )
CODE( dump(S); )
}
/*----------------------------------------------------------------------*/
section("Test converters", '=');
section("Test conversion from Array to Series");
{
CODE( Array<int> A(Shaper(2,9)(10,12)(0,2)(2,4)); );
section("fill array");
for(int i=A.f(0); i<=A.l(0); i++)
{
for(int j=A.f(1); j<=A.l(1); j++)
{
for(int k=A.f(2); k<=A.l(2); k++)
{
for(int l=A.f(3); l<=A.l(3); l++)
{
A(i,j,k,l)=(i-A.f(0)+1)+(j-A.f(1)+1)*10
+(k-A.f(2)+1)*100+(l-A.f(3)+1)*1000;
}
}
}
}
CODE( dump_array(A,3); );
CODE( Array<int> B=aff::slice(A)(1,11)(1,1)(1,3); )
CODE( dump(C); )
CODE( dump(E); )
}
}
/* ----- END OF helpertest.cc ----- */