shapetest.cc 5.16 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: shapetest.cc,v 1.6 2002-12-29 00:09:07 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
thomas.forbriger's avatar
thomas.forbriger committed
21
22
23
 * 
 * ============================================================================
 */
thomas.forbriger's avatar
thomas.forbriger committed
24
25
26
#define AFF_SHAPETEST_CC_VERSION \
  "AFF_SHAPETEST_CC   V1.1"
#define AFF_SHAPETEST_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
27
  "$Id: shapetest.cc,v 1.6 2002-12-29 00:09:07 forbrig Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
28
29

#include<iostream>
thomas.forbriger's avatar
thomas.forbriger committed
30
31
#include<aff/dump.h>
#include<aff/shaper.h>
thomas.forbriger's avatar
thomas.forbriger committed
32
#include<aff/lib/stridedstepper.h>
thomas.forbriger's avatar
thomas.forbriger committed
33
34
35

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

thomas.forbriger's avatar
thomas.forbriger committed
56
/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
57

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

thomas.forbriger's avatar
thomas.forbriger committed
67
68
/*----------------------------------------------------------------------*/

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

thomas.forbriger's avatar
thomas.forbriger committed
73
74
/*----------------------------------------------------------------------*/

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

thomas.forbriger's avatar
thomas.forbriger committed
78
79
/*----------------------------------------------------------------------*/

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

thomas.forbriger's avatar
thomas.forbriger committed
114
115
/*======================================================================*/

thomas.forbriger's avatar
thomas.forbriger committed
116
//! test shape class
thomas.forbriger's avatar
thomas.forbriger committed
117
118
int main()
{
thomas.forbriger's avatar
thomas.forbriger committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  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) );

  cout << endl << "Fool proof with limited dimensionality:" << endl;
  DUMP( Shaper(2,10)(-12,12)(9)(2,7)(3,4) );

  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

  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); )
  }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

  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
193
194
195
}

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