shapetest.cc 4.42 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.4 2002-12-18 18:43:45 uid1201 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
21
 * 
 * ============================================================================
 */
thomas.forbriger's avatar
thomas.forbriger committed
22
23
24
#define AFF_SHAPETEST_CC_VERSION \
  "AFF_SHAPETEST_CC   V1.1"
#define AFF_SHAPETEST_CC_CVSID \
25
  "$Id: shapetest.cc,v 1.4 2002-12-18 18:43:45 uid1201 Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
26
27

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

using std::cout;
using std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
34
35
36
37
38
39
40
41
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
42
43
 */

thomas.forbriger's avatar
thomas.forbriger committed
44
45
//! echo
#define CODE( C ) cout << #C << endl; C
thomas.forbriger's avatar
thomas.forbriger committed
46

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

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

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

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

thomas.forbriger's avatar
thomas.forbriger committed
97
//! test shape class
thomas.forbriger's avatar
thomas.forbriger committed
98
99
int main()
{
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
120
121
122
123
124
125
126
127
  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

  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); )
  }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

  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
174
175
176
}

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