shapetest.cc 4.92 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.5 2002-12-19 18:30:08 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.5 2002-12-19 18:30:08 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
43
using namespace aff;

/*! \example tests/shapetest.cc
 *
 * Test shape aff::Strided, the shaper class aff::Shaper,
 * and the helper class aff::util::StridedStepper
 *
 * \sa tests/shapetest.cc
thomas.forbriger's avatar
thomas.forbriger committed
44
45
 */

thomas.forbriger's avatar
thomas.forbriger committed
46
/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
47

thomas.forbriger's avatar
thomas.forbriger committed
48
49
//! print headline
void section(const char* s, const char l='-')
thomas.forbriger's avatar
thomas.forbriger committed
50
{
thomas.forbriger's avatar
thomas.forbriger committed
51
52
53
  cout << endl << s << endl;
  const char* p=s;
  while (*p) { cout << l; ++p; }
thomas.forbriger's avatar
thomas.forbriger committed
54
55
56
  cout << endl;
}

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

thomas.forbriger's avatar
thomas.forbriger committed
59
//! print value of a bool
thomas.forbriger's avatar
thomas.forbriger committed
60
61
62
const char* bvalue(const bool& b)
{ if (b) return("true"); return("false"); }

thomas.forbriger's avatar
thomas.forbriger committed
63
64
/*----------------------------------------------------------------------*/

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

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

thomas.forbriger's avatar
thomas.forbriger committed
70
71
72
73
74
75
76
//! 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++)
  {
77
    cout.width(3); cout << i+1;
thomas.forbriger's avatar
thomas.forbriger committed
78
79
80
81
82
    cout << "    ";
    cout.width(5); cout << s1.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
83
      cout.width(2); cout << s1.index(j);
thomas.forbriger's avatar
thomas.forbriger committed
84
    }
85
    cout << "] "; 
thomas.forbriger's avatar
thomas.forbriger committed
86
87
    cout.width(5); cout << bvalue(s1.more());
    cout.width(6); cout << bvalue(s1.valid());
88
    cout << "   ";
thomas.forbriger's avatar
thomas.forbriger committed
89
90
91
92
    cout.width(5); cout << s2.current() << "=[";
    for (int j=0; j<Strided::Mmax_dimen; j++)
    {
      if (j) cout << ", ";
93
      cout.width(2); cout << s2.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(s2.less());
    cout.width(6); cout << bvalue(s2.valid());
98
    cout << endl;
thomas.forbriger's avatar
thomas.forbriger committed
99
100
101
102
103
    s1.incr();
    s2.decr();
  }
}

thomas.forbriger's avatar
thomas.forbriger committed
104
105
/*======================================================================*/

thomas.forbriger's avatar
thomas.forbriger committed
106
//! test shape class
thomas.forbriger's avatar
thomas.forbriger committed
107
108
int main()
{
thomas.forbriger's avatar
thomas.forbriger committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

  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); )
  }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

  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
183
184
185
}

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