shapetest.cc 6 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file shapetest.cc
 * \brief test shape classes (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
 * \author Thomas Forbriger
 * \date 25/05/2002
 * 
 * test shape classes (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 
 *  - 25/05/2002   V1.0   Thomas Forbriger
32
33
34
 *  - 18/12/2002   V1.1   (thof)
 *                        - started test of Strided (aff)
 *                        - test works
thomas.forbriger's avatar
thomas.forbriger committed
35
36
 *  - 19/12/2002   V1.2   (thof)
 *                        - print valid() of stepper too
37
38
39
 *  - 09/01/2003   V1.3   (thof)
 *                        - using too many bracket operators now triggers 
 *                          an exception in aff::Shaper
thomas.forbriger's avatar
thomas.forbriger committed
40
41
42
 * 
 * ============================================================================
 */
thomas.forbriger's avatar
thomas.forbriger committed
43
#define AFF_SHAPETEST_CC_VERSION \
44
  "AFF_SHAPETEST_CC   V1.3"
thomas.forbriger's avatar
thomas.forbriger committed
45
#define AFF_SHAPETEST_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
46
  "$Id$"
thomas.forbriger's avatar
thomas.forbriger committed
47
48

#include<iostream>
thomas.forbriger's avatar
thomas.forbriger committed
49
50
#include<aff/dump.h>
#include<aff/shaper.h>
thomas.forbriger's avatar
thomas.forbriger committed
51
#include<aff/lib/stridedstepper.h>
thomas.forbriger's avatar
thomas.forbriger committed
52
53
54

using std::cout;
using std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
55
56
57
58
59
60
61
using namespace aff;

/*! \example tests/shapetest.cc
 *
 * Test shape aff::Strided, the shaper class aff::Shaper,
 * and the helper class aff::util::StridedStepper
 *
thomas.forbriger's avatar
thomas.forbriger committed
62
63
64
65
66
67
68
69
70
71
 * This test program gives an example of the usage of the following classes,
 * functions, and preprocessor macros:
 *   - aff::Strided
 *   - aff::StridedStepper
 *   - aff::Shaper
 *   - aff::dump
 *   - aff::dump_map
 *   - #DUMP
 *   - #CODE
 *
thomas.forbriger's avatar
thomas.forbriger committed
72
 * \sa tests/shapetest.cc
thomas.forbriger's avatar
thomas.forbriger committed
73
74
 */

thomas.forbriger's avatar
thomas.forbriger committed
75
/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
76

thomas.forbriger's avatar
thomas.forbriger committed
77
78
//! print headline
void section(const char* s, const char l='-')
thomas.forbriger's avatar
thomas.forbriger committed
79
{
thomas.forbriger's avatar
thomas.forbriger committed
80
81
82
  cout << endl << s << endl;
  const char* p=s;
  while (*p) { cout << l; ++p; }
thomas.forbriger's avatar
thomas.forbriger committed
83
84
85
  cout << endl;
}

thomas.forbriger's avatar
thomas.forbriger committed
86
87
/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
88
//! print value of a bool
thomas.forbriger's avatar
thomas.forbriger committed
89
90
91
const char* bvalue(const bool& b)
{ if (b) return("true"); return("false"); }

thomas.forbriger's avatar
thomas.forbriger committed
92
93
/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
94
//! print value of a bool
thomas.forbriger's avatar
thomas.forbriger committed
95
96
#define BOOLIS( S ) cout << " *** " << #S << ": " << bvalue(S);

thomas.forbriger's avatar
thomas.forbriger committed
97
98
/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
99
100
101
102
103
104
105
//! cycle steppers
void steppers(const Strided& shape, const int& nmax=30)
{
  StridedStepper s1(shape), s2(shape);
  s2.tolast();
  for (int i=0; i<nmax; i++)
  {
106
    cout.width(3); cout << i+1;
thomas.forbriger's avatar
thomas.forbriger committed
107
108
109
110
111
    cout << "    ";
    cout.width(5); cout << s1.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
112
      cout.width(2); cout << s1.index(j);
thomas.forbriger's avatar
thomas.forbriger committed
113
    }
114
    cout << "] "; 
thomas.forbriger's avatar
thomas.forbriger committed
115
116
    cout.width(5); cout << bvalue(s1.more());
    cout.width(6); cout << bvalue(s1.valid());
117
    cout << "   ";
thomas.forbriger's avatar
thomas.forbriger committed
118
119
120
121
    cout.width(5); cout << s2.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
122
      cout.width(2); cout << s2.index(j);
thomas.forbriger's avatar
thomas.forbriger committed
123
    }
124
    cout << "] "; 
thomas.forbriger's avatar
thomas.forbriger committed
125
126
    cout.width(5); cout << bvalue(s2.less());
    cout.width(6); cout << bvalue(s2.valid());
127
    cout << endl;
thomas.forbriger's avatar
thomas.forbriger committed
128
129
130
131
132
    s1.incr();
    s2.decr();
  }
}

thomas.forbriger's avatar
thomas.forbriger committed
133
134
/*======================================================================*/

thomas.forbriger's avatar
thomas.forbriger committed
135
//! test shape class
thomas.forbriger's avatar
thomas.forbriger committed
136
137
int main()
{
thomas.forbriger's avatar
thomas.forbriger committed
138
139
140
141
142
143
144
145
146
147
  cout << AFF_SHAPETEST_CC_VERSION << endl;
  cout << AFF_SHAPETEST_CC_CVSID << endl << endl;
  
  section("Test shape class Strided", '=');

  section("Test shaper class Shaper");

  cout << endl << "Basic usage:" << endl;
  DUMP( Shaper(10)(12) );

148
149
150
#ifdef ILLEGAL1
#warning intentionally compiling illegal code
  cout << endl << "Illegal with limited dimensionality:" << endl;
thomas.forbriger's avatar
thomas.forbriger committed
151
  DUMP( Shaper(2,10)(-12,12)(9)(2,7)(3,4) );
152
#endif
thomas.forbriger's avatar
thomas.forbriger committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

  cout << endl << "Usage for an external Fortran shape:" << endl;
  DUMP( Shaper(1,6,10)(1,12,30)(1,10) );

  section("Test subarrays and slicing");

  DUMP( Shaper(10)(10)(10) );

  cout << endl;
  DUMP( Strided(Shaper(10)(10)(10)).shrink(0,2,5) );

  cout << endl;
  DUMP( Strided(Shaper(10)(10)(10)).shrink(2,5) );

  cout << endl;
  DUMP( Strided(Shaper(10)(15)(20)).collapse(1,5) );
thomas.forbriger's avatar
thomas.forbriger committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

  cout << endl;
  DUMP( Strided(Shaper(11,20)(11,20)(11,20)).shift(1,5) );

  cout << endl;
  DUMP( Strided(Shaper(11,20)(11,20)(11,20)).setfirst(1,5) );

  section("Test stepper");

  {
    cout << endl << "Basic functionality" << endl;
    CODE( Strided shape(Shaper(3)(4)(2)(3)); )
    DUMP( shape );
    CODE( steppers(shape, shape.size()+10); )
  }

  {
    cout << endl << "Shape with gaps" << endl;
    CODE( Strided shape(Shaper(0,9)(0,9)(0,9)(0,9)); )
    DUMP( shape.shrink(0,3,5).shrink(1,4,6).shrink(2,5,7).shrink(3,6,8) );
    CODE( steppers(shape, shape.size()+10); )
  }

  {
    cout << endl << "Small array" << endl;
    CODE( Strided shape(Shaper(3,5,10)(3)); )
    DUMP( shape );
    CODE( steppers(shape, shape.size()+10); )
  }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

  section("Test offset functions");
  {
    CODE( Strided shape(Shaper(5)(10)(10)(10)); )
    CODE( shape.shrink(0,2,4).shrink(1,3,7).shrink(2,5,6).shrink(3,2,3); )
    CODE( dump_map(shape, 3); )
    CODE( dump_map(shape, 2); )
    CODE( dump_map(shape, 1); )
    CODE( dump_map(shape, 0); )
    CODE( steppers(shape, shape.size()); )
    CODE( Strided copy(shape); )
    CODE( copy.collapse(2,6).collapse(2,3).shift(0,1000).setfirst(1,50); )
    CODE( dump_map(copy); )
    CODE( copy=shape; )
    CODE( copy.collapse(1,7).collapse(2,3).shift(0,1000).setfirst(1,50); )
    CODE( dump_map(copy); )
  }
thomas.forbriger's avatar
thomas.forbriger committed
215
216
217
}

/* ----- END OF shapetest.cc ----- */