shapetest.cc 5.39 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file shapetest.cc
 * \brief test shape classes (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id: shapetest.cc,v 1.7 2003-01-09 17:30:11 forbrig Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
11
12
13
14
15
 * \author Thomas Forbriger
 * \date 25/05/2002
 * 
 * test shape classes (implementation)
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 25/05/2002   V1.0   Thomas Forbriger
16
17
18
 *  - 18/12/2002   V1.1   (thof)
 *                        - started test of Strided (aff)
 *                        - test works
thomas.forbriger's avatar
thomas.forbriger committed
19
20
 *  - 19/12/2002   V1.2   (thof)
 *                        - print valid() of stepper too
21
22
23
 *  - 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
24
25
26
 * 
 * ============================================================================
 */
thomas.forbriger's avatar
thomas.forbriger committed
27
#define AFF_SHAPETEST_CC_VERSION \
28
  "AFF_SHAPETEST_CC   V1.3"
thomas.forbriger's avatar
thomas.forbriger committed
29
#define AFF_SHAPETEST_CC_CVSID \
30
  "$Id: shapetest.cc,v 1.7 2003-01-09 17:30:11 forbrig Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
31
32

#include<iostream>
thomas.forbriger's avatar
thomas.forbriger committed
33
34
#include<aff/dump.h>
#include<aff/shaper.h>
thomas.forbriger's avatar
thomas.forbriger committed
35
#include<aff/lib/stridedstepper.h>
thomas.forbriger's avatar
thomas.forbriger committed
36
37
38

using std::cout;
using std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
39
40
41
42
43
44
45
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
46
47
48
49
50
51
52
53
54
55
 * 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
56
 * \sa tests/shapetest.cc
thomas.forbriger's avatar
thomas.forbriger committed
57
58
 */

thomas.forbriger's avatar
thomas.forbriger committed
59
/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
60

thomas.forbriger's avatar
thomas.forbriger committed
61
62
//! print headline
void section(const char* s, const char l='-')
thomas.forbriger's avatar
thomas.forbriger committed
63
{
thomas.forbriger's avatar
thomas.forbriger committed
64
65
66
  cout << endl << s << endl;
  const char* p=s;
  while (*p) { cout << l; ++p; }
thomas.forbriger's avatar
thomas.forbriger committed
67
68
69
  cout << endl;
}

thomas.forbriger's avatar
thomas.forbriger committed
70
71
/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
72
//! print value of a bool
thomas.forbriger's avatar
thomas.forbriger committed
73
74
75
const char* bvalue(const bool& b)
{ if (b) return("true"); return("false"); }

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

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

thomas.forbriger's avatar
thomas.forbriger committed
81
82
/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
83
84
85
86
87
88
89
//! 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++)
  {
90
    cout.width(3); cout << i+1;
thomas.forbriger's avatar
thomas.forbriger committed
91
92
93
94
95
    cout << "    ";
    cout.width(5); cout << s1.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
96
      cout.width(2); cout << s1.index(j);
thomas.forbriger's avatar
thomas.forbriger committed
97
    }
98
    cout << "] "; 
thomas.forbriger's avatar
thomas.forbriger committed
99
100
    cout.width(5); cout << bvalue(s1.more());
    cout.width(6); cout << bvalue(s1.valid());
101
    cout << "   ";
thomas.forbriger's avatar
thomas.forbriger committed
102
103
104
105
    cout.width(5); cout << s2.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
106
      cout.width(2); cout << s2.index(j);
thomas.forbriger's avatar
thomas.forbriger committed
107
    }
108
    cout << "] "; 
thomas.forbriger's avatar
thomas.forbriger committed
109
110
    cout.width(5); cout << bvalue(s2.less());
    cout.width(6); cout << bvalue(s2.valid());
111
    cout << endl;
thomas.forbriger's avatar
thomas.forbriger committed
112
113
114
115
116
    s1.incr();
    s2.decr();
  }
}

thomas.forbriger's avatar
thomas.forbriger committed
117
118
/*======================================================================*/

thomas.forbriger's avatar
thomas.forbriger committed
119
//! test shape class
thomas.forbriger's avatar
thomas.forbriger committed
120
121
int main()
{
thomas.forbriger's avatar
thomas.forbriger committed
122
123
124
125
126
127
128
129
130
131
  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) );

132
133
134
#ifdef ILLEGAL1
#warning intentionally compiling illegal code
  cout << endl << "Illegal with limited dimensionality:" << endl;
thomas.forbriger's avatar
thomas.forbriger committed
135
  DUMP( Shaper(2,10)(-12,12)(9)(2,7)(3,4) );
136
#endif
thomas.forbriger's avatar
thomas.forbriger committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

  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); )
  }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

  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
199
200
201
}

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