helpertest.cc 7.34 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file helpertest.cc
 * \brief test helpers like iterator, subarray and slice (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: helpertest.cc,v 1.7 2007/12/10 16:10:07 tforb Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
 * \author Thomas Forbriger
 * \date 23/12/2002
 * 
 * test helpers like iterator, subarray and slice (implementation)
thomas.forbriger's avatar
thomas.forbriger committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * ----
 * 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
 * ----
thomas.forbriger's avatar
thomas.forbriger committed
27
28
29
30
31
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 23/12/2002   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
32
33
 *  - 28/12/2002   V1.1   (thof)
 *                        - new term for containers of const elements
34
35
36
37
38
 *  - 29/12/2002   V1.2   (thof)
 *                        - new subarray function
 *                        - test subarray of ConstArray
 *                        - new slice function
 *                        - test slice of ConstArray
39
40
 *                        - now no resolution conflicts with const containers
 *                          are experienced anymore
thomas.forbriger's avatar
thomas.forbriger committed
41
42
 *  - 10/12/2007   V1.3   (thof)
 *                        - scanning a const array requires a Browser
thomas.forbriger's avatar
thomas.forbriger committed
43
44
45
46
 * 
 * ============================================================================
 */
#define AFF_HELPERTEST_CC_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
47
  "AFF_HELPERTEST_CC   V1.3"
thomas.forbriger's avatar
thomas.forbriger committed
48
#define AFF_HELPERTEST_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
49
  "$Id: helpertest.cc,v 1.7 2007/12/10 16:10:07 tforb Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
50
51
52
53
54

/*! \example tests/helpertest.cc
 *
 * Test helper classes.
 *
thomas.forbriger's avatar
thomas.forbriger committed
55
56
57
 * This test program gives an example of the usage of the following classes,
 * functions, and preprocessor macros:
 *   - aff::Iterator
58
59
 *   - aff::util::Subarray
 *   - aff::subarray
60
61
 *   - aff::utl::Slice
 *   - aff::slice
thomas.forbriger's avatar
thomas.forbriger committed
62
63
64
65
66
67
68
69
 *   - aff::Array
 *   - aff::ConstArray
 *   - aff::Shaper
 *   - aff::dump
 *   - aff::dump_array
 *   - #DUMP
 *   - #CODE
 *
thomas.forbriger's avatar
thomas.forbriger committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 * \sa tests/helpertest.cc
 */

#include <aff/array.h>
#include <aff/dump.h>
#include <aff/shaper.h>
#include <aff/iterator.h>
#include <aff/subarray.h>
#include <aff/slice.h>

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<class T>
thomas.forbriger's avatar
thomas.forbriger committed
99
void printarray(const ConstArray<T>& array)
thomas.forbriger's avatar
thomas.forbriger committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{
  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<int> A(Shaper(-2,2)(-3,3)) );
  CODE( dump_array(A) );

  section("Tests for class Iterator", '=');
  {
thomas.forbriger's avatar
thomas.forbriger committed
120
    CODE(int i=0);
thomas.forbriger's avatar
thomas.forbriger committed
121
122
123
    CODE(for( Iterator<Array<int> > I(A); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
124
    CODE( ConstArray<int> CA(A.copyout()) );
125
    CODE( i=1);
thomas.forbriger's avatar
thomas.forbriger committed
126
127
    // use a browser here
    CODE(for( Browser<ConstArray<int> > I(CA); I.valid(); ++I) 
128
         { cout << i << ", " << *I << endl; i++; });
thomas.forbriger's avatar
thomas.forbriger committed
129
  }
130
131

  CODE( ConstArray<int> OrigA(A.copyout()) );
thomas.forbriger's avatar
thomas.forbriger committed
132
  
133
134
  /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
135
136
  section("Tests for class Subarray", '=');
  {
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    section("First mode: Use a Subarray object just for initialization",'.');
    CODE(Array<int> B=aff::util::Subarray<Array<int> >(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<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));
thomas.forbriger's avatar
thomas.forbriger committed
151
152
153
    CODE( dump_array(B) );
    CODE(B=-5);
    CODE( dump_array(A) );
154
155
156
157
    section("Second mode: Create a Subarray object to which you can assign",
            '.');
    CODE(subarray(A)()(1,2)=100);
    CODE(subarray(A)(1,1)=-200);
thomas.forbriger's avatar
thomas.forbriger committed
158
159
    CODE( dump_array(A) );
    // you must explicitely create an array through member function array()
160
161
162
163
164
165
166
167
168
169
    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));
    CODE( dump_array(B) );
    // you must explicitely create an array through member function array()
    CODE(printarray(subarray(OrigA)(-2,0)(-3,0).array()));
thomas.forbriger's avatar
thomas.forbriger committed
170
171
  }
  
172
173
  /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
174
175
  section("Tests for class Slice", '=');
  {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    CODE( A.copyin(OrigA) );
    section("First mode: Use a Slice object just for initialization",'.');
    CODE(Array<int> B=aff::util::Slice<Array<int> >(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<Array<int> >(A)(-2).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));
thomas.forbriger's avatar
thomas.forbriger committed
191
192
193
    CODE( dump_array(B) );
    CODE(B=555);
    CODE( dump_array(A) );
194
195
196
197
    section("Second mode: Create a Slice object to which you can assign",
            '.');
    CODE(slice(A)()(1)=666);
    CODE(slice(A)(0)=-777);
thomas.forbriger's avatar
thomas.forbriger committed
198
199
    CODE( dump_array(A) );
    // you must explicitely create an array through member function array()
200
    CODE(printarray(slice(A)(-2).array()));
thomas.forbriger's avatar
thomas.forbriger committed
201
202
  }

203
204
205
206
207
208
209
210
211
212
213
  section("Test slice of ConstArray",'-');
  {
    CODE( dump_array(OrigA) );
    CODE(ConstArray<int> 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()));
  }

  /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
214
215
216
217
218
  section("Mixed tests", '=');

  section("Iterator on a Subarray");
  {
    CODE(int i=501);
219
    CODE(Array<int> B=subarray(A)(-1,1)(-2,2));
thomas.forbriger's avatar
thomas.forbriger committed
220
221
222
223
224
225
226
227
    CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
  }

  section("Iterator on a Slice");
  {
    CODE(int i=-801);
228
    CODE(Array<int> B=slice(A)(1));
thomas.forbriger's avatar
thomas.forbriger committed
229
230
231
232
233
234
235
    CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
  }

  section("Extract copy of subarray");
  {
236
    CODE(Array<int> B=subarray(A)(-1,1)(-1,1));
thomas.forbriger's avatar
thomas.forbriger committed
237
238
239
240
241
242
243
    CODE(dump_array(B));
    CODE(Array<int> C=B.copyout());
    CODE(dump_array(C));
  }
}

/* ----- END OF helpertest.cc ----- */