AFF --- A container for numbers (array) by Friederich and Forbriger.
helpertest.cc
Go to the documentation of this file.
1 
48 #define AFF_HELPERTEST_CC_VERSION \
49  "AFF_HELPERTEST_CC V1.4"
50 
76 #include <aff/array.h>
77 #include <aff/dump.h>
78 #include <aff/shaper.h>
79 #include <aff/iterator.h>
80 #include <aff/subarray.h>
81 #include <aff/slice.h>
82 #include <aff/Carray.h>
83 #include <aff/converters.h>
84 
85 using std::cout;
86 using std::endl;
87 using namespace aff;
88 
89 /*----------------------------------------------------------------------*/
90 
92 void section(const char* s, const char l='-')
93 {
94  cout << endl << s << endl;
95  const char* p=s;
96  while (*p) { cout << l; ++p; }
97  cout << endl;
98 }
99 
100 /*----------------------------------------------------------------------*/
101 
103 template<class T>
104 void printarray(const ConstArray<T>& array)
105 {
106  cout << endl;
107  cout << "Received array in function:" << endl;
108  dump_array(array);
109 }
110 
111 /*======================================================================*/
112 
114 int main()
115 {
116  cout << AFF_HELPERTEST_CC_VERSION << endl;
117 
118  section("Preparing test array");
119  CODE( Array<int> A(Shaper(-2,2)(-3,3)) );
120  CODE( dump_array(A) );
121 
122  section("Tests for class Iterator", '=');
123  {
124  CODE(int i=0);
125  CODE(for( Iterator<Array<int> > I(A); I.valid(); ++I)
126  { *I=i; i++; });
127  CODE( dump_array(A) );
128  CODE( ConstArray<int> CA(A.copyout()) );
129  CODE( i=1);
130  // use a browser here
131  CODE(for( Browser<ConstArray<int> > I(CA); I.valid(); ++I)
132  { cout << i << ", " << *I << endl; i++; });
133  }
134 
135  CODE( ConstArray<int> OrigA(A.copyout()) );
136 
137  /*----------------------------------------------------------------------*/
138 
139  section("Tests for class Subarray", '=');
140  {
141  section("First mode: Use a Subarray object just for initialization",'.');
142  CODE(Array<int> B=aff::util::Subarray<Array<int> >(A)(-1,1)(-2,2));
143  CODE( dump_array(B) );
144  CODE(B=-5);
145  CODE( dump_array(A) );
146  // you must explicitely create an array through member function array()
147  CODE(printarray(aff::util::Subarray<Array<int> >(A)(-2,0)(-3,0).array()));
148  }
149 
150  section("Tests of subarray function", '-');
151  {
152  CODE( A.copyin(OrigA) );
153  section("First mode: Use a Subarray object just for initialization",'.');
154  CODE(Array<int> B=subarray(A)(-1,1)(-2,2));
155  CODE( dump_array(B) );
156  CODE(B=-5);
157  CODE( dump_array(A) );
158  section("Second mode: Create a Subarray object to which you can assign",
159  '.');
160  CODE(subarray(A)()(1,2)=100);
161  CODE(subarray(A)(1,1)=-200);
162  CODE( dump_array(A) );
163  // you must explicitely create an array through member function array()
164  CODE(printarray(subarray(A)(-2,0)(-3,0).array()));
165  }
166 
167  section("Test for subarray of ConstArray",'-');
168  {
169  CODE( dump_array(OrigA) );
170  CODE(ConstArray<int> B=subarray(OrigA)(-1,1)(-2,2));
171  CODE( dump_array(B) );
172  // you must explicitely create an array through member function array()
173  CODE(printarray(subarray(OrigA)(-2,0)(-3,0).array()));
174  }
175 
176  /*----------------------------------------------------------------------*/
177 
178  section("Tests for class Slice", '=');
179  {
180  CODE( A.copyin(OrigA) );
181  section("First mode: Use a Slice object just for initialization",'.');
183  CODE( dump_array(B) );
184  CODE(B=555);
185  CODE( dump_array(A) );
186  // you must explicitely create an array through member function array()
187  CODE(printarray(aff::util::Slice<Array<int> >(A)(-2).array()));
188  }
189 
190  section("Test slice function", '-');
191  {
192  CODE( A.copyin(OrigA) );
193  section("First mode: Use a Slice object just for initialization",'.');
194  CODE(Array<int> B=slice(A)(1,-1));
195  CODE( dump_array(B) );
196  CODE(B=555);
197  CODE( dump_array(A) );
198  section("Second mode: Create a Slice object to which you can assign",
199  '.');
200  CODE(slice(A)()(1)=666);
201  CODE(slice(A)(0)=-777);
202  CODE( dump_array(A) );
203  // you must explicitely create an array through member function array()
204  CODE(printarray(slice(A)(-2).array()));
205  }
206 
207  section("Test slice of ConstArray",'-');
208  {
209  CODE( dump_array(OrigA) );
210  CODE(ConstArray<int> B=slice(OrigA)(1,-1));
211  CODE( dump_array(B) );
212  // you must explicitely create an array through member function array()
213  CODE(printarray(slice(OrigA)(-2).array()));
214  }
215 
216  /*----------------------------------------------------------------------*/
217 
218  section("Mixed tests", '=');
219 
220  section("Iterator on a Subarray");
221  {
222  CODE(int i=501);
223  CODE(Array<int> B=subarray(A)(-1,1)(-2,2));
224  CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I)
225  { *I=i; i++; });
226  CODE( dump_array(A) );
227  }
228 
229  section("Iterator on a Slice");
230  {
231  CODE(int i=-801);
232  CODE(Array<int> B=slice(A)(1));
233  CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I)
234  { *I=i; i++; });
235  CODE( dump_array(A) );
236  }
237 
238  section("Extract copy of subarray");
239  {
240  CODE(Array<int> B=subarray(A)(-1,1)(-1,1));
241  CODE(dump_array(B));
242  CODE(Array<int> C=B.copyout());
243  CODE(dump_array(C));
244  }
245 
246  /*----------------------------------------------------------------------*/
247 
248  section("Test external access interfaces", '=');
249 
250  section("Test interface to raw memory array: aff::CArray");
251  {
252  CODE( Array<int> A(Shaper(2)(10,16)(-2,2)(-1,0)); );
253  section("fill array");
254  for(int i=A.f(0); i<=A.l(0); i++)
255  {
256  for(int j=A.f(1); j<=A.l(1); j++)
257  {
258  for(int k=A.f(2); k<=A.l(2); k++)
259  {
260  for(int l=A.f(3); l<=A.l(3); l++)
261  {
262  A(i,j,k,l)=(i-A.f(0)+1)+(j-A.f(1)+1)*10
263  +(k-A.f(2)+1)*100+(l-A.f(3)+1)*1000;
264  }
265  }
266  }
267  }
268  CODE( dump_array(A,3); );
269  CODE( Array<int> B=aff::slice(A)(0,2)(2,-1); )
270  CODE( printarray(B) );
271  CODE( CArray<int> C(B); )
272  section("fill C array");
273  CODE( int* p=C.pointer(); )
274  CODE(
275  for (unsigned i=0; i<C.size(0); ++i)
276  {
277  for (unsigned j=0; j<C.size(1); ++j)
278  {
279  p[i*C.stride(0)+j*C.stride(1)]=i+10*j;
280  }
281  }
282  )
283  CODE( printarray(B) );
284  CODE( printarray(A) );
285  }
286 
287  /*----------------------------------------------------------------------*/
288 
289  section("Test interface to raw memory array: aff::CSeries");
290  {
291  CODE( aff::Series<int> A(-3,4); )
292  CODE( for (int i=A.f(); i<=A.l(); ++i) { A(i)=i; })
293  CODE( dump(A); )
294  CODE( aff::CSeries<int> CS(A); )
295  CODE( int* p=CS.pointer(); )
296  CODE( for (int i=0; i<CS.size(); ++i) { p[i]=i; })
297  CODE( dump(A); )
298  CODE( Series<int> S=series_from_raw_memory(p, CS.size()); )
299  CODE( dump(S); )
300  }
301 
302  /*----------------------------------------------------------------------*/
303 
304  section("Test converters", '=');
305 
306  section("Test conversion from Array to Series");
307  {
308  CODE( Array<int> A(Shaper(2,9)(10,12)(0,2)(2,4)); );
309  section("fill array");
310  for(int i=A.f(0); i<=A.l(0); i++)
311  {
312  for(int j=A.f(1); j<=A.l(1); j++)
313  {
314  for(int k=A.f(2); k<=A.l(2); k++)
315  {
316  for(int l=A.f(3); l<=A.l(3); l++)
317  {
318  A(i,j,k,l)=(i-A.f(0)+1)+(j-A.f(1)+1)*10
319  +(k-A.f(2)+1)*100+(l-A.f(3)+1)*1000;
320  }
321  }
322  }
323  }
324  CODE( dump_array(A,3); );
325  CODE( Array<int> B=aff::slice(A)(1,11)(1,1)(1,3); )
326  CODE( printarray(B) );
328  CODE( dump(C); )
329  CODE( ConstArray<int> D=B; )
331  CODE( dump(E); )
332  }
333 }
334 
335 /* ----- END OF helpertest.cc ----- */
Array< T > copyout() const
create an identical copy (deep copy) of this array
Definition: array.h:559
Root namespace of library.
Definition: array.h:148
Interface class to raw memory (C style array)
Definition: Carray.h:229
aff::util::Slice< C > slice(const C &c)
Wrapper function to select correct type.
Definition: slice.h:158
#define AFF_HELPERTEST_CC_VERSION
Definition: helpertest.cc:48
void dump(const Strided &shape, std::ostream &os)
dump shape
Definition: dump.cc:49
Browser.
Definition: iterator.h:139
void printarray(const ConstArray< T > &array)
test for passing subarrays and slices
Definition: helpertest.cc:104
access Series contents through raw memory
Definition: converters.h:146
classes to interface raw C arrays (prototypes)
Create subarrays.
Definition: subarray.h:72
aff::util::Subarray< C > subarray(const C &c)
Wrapper function to select correct type.
Definition: subarray.h:163
aff::Series< T > series_from_raw_memory(T *pointer, const unsigned int size)
create a series class from raw memory.
Definition: converters.h:129
external class to create subarrays (prototypes)
full template array class headers (prototypes)
Iterator.
Definition: iterator.h:73
#define CODE(C)
Dump code and execute (works like echo)
Definition: dump_macros.h:82
void dump_array(const ConstArray< T > &array, const Tdim &i=(Strided::Mmax_dimen-1), std::ostream &os=std::cout)
Dump array values.
Definition: dump_array.h:157
converters for AFF containers
base class
Definition: series.h:105
aff::Series< T > series_from_array(const aff::Array< T > &array)
create a series container from an array container.
Definition: converters.h:53
debug helpers (prototypes)
Full multi-dimensional array functionality.This is the full array class template. It adds no addition...
Definition: array.h:151
Create slices.
Definition: slice.h:67
int main()
test helper classes
Definition: helpertest.cc:114
external class to create slices (prototypes)
A base class for time series and spectra.
Definition: series.h:266
void section(const char *s, const char l='-')
print headline
Definition: helpertest.cc:92
Shaper class for Fortran layout.
Definition: shaper.h:66
Define the iterator class template (prototypes)
Array base classThis is a multidimensional (array) container that uses a strided memory layout (Fortr...
Definition: array.h:172
rectangular Fortran array layout (prototypes)