shapetest.cc 4.93 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*! \file shapetest.cc
 * \brief test shape classes (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id: shapetest.cc,v 1.1 2002-12-06 19:21:08 forbrig Exp $
 * \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
 *  - 25/11/2002   V1.1   testing stepper
 * 
 * ============================================================================
 */
#define TF_SHAPETEST_CC_VERSION \
  "TF_SHAPETEST_CC   V1.1   "
#define TF_SHAPETEST_CC_CVSID \
  "$Id: shapetest.cc,v 1.1 2002-12-06 19:21:08 forbrig Exp $"

#include<iostream>
#include<string>
#include <contxx/shape/shaper.h>
#include <contxx/shape/strided.h>
#include <contxx/shape/densestrided.h>
#include <contxx/shaper/fortran.h>

using std::cout;
using std::endl;
using std::string;
using namespace contxx::shape;
using namespace contxx::util;

/*
 * function to print index relation of shape
 */
template<Tdim N, template<Tdim NN> class Shape>
  void indexer(const char* name, const Shape<N>& shape)
{
  cout << "shape " << name << ":" << endl;
  typename Shape<N>::Tlimits index;
  for (Tdim i=0; i<shape.dimensionality() ;i++)
  { index[i]=shape.first(i); }
  while (index[N-1] < shape.last(N-1))
  {
    Tsubscript theindex=shape.offset();
    for (Tdim i=0; i<shape.dimensionality(); i++)
    { theindex+=index[i]*shape.stride(i); }
    cout << name << "(";
    for (Tdim i=0; i<shape.dimensionality(); i++)
    { cout << index[i]; if (i+1<shape.dimensionality()) { cout << ","; } }
    cout << "): " << theindex << endl;
    Tdim i=0;
    while (i<N) 
    {
      ++index[i];
      if (index[i]>shape.last(i))
      {
        index[i]=shape.first(i);
        i++;
      }
      else
      {
        i=N;
      }
    }
  }
  cout << endl;
}

#define INDEXER( shape ) indexer( #shape , shape );

/*
 * function to print values of simple array
 */
template<class S>
void saprint(const char* name, const S& s)
{
  cout << "simple array: " << name << ":" << endl;
  for (Tsize i=0; i<s.size(); i++)
  {
    cout << "  " << i << ":" << s[i];
  }
  cout << endl;
}

#define SAPRINTER( array ) saprint( #array , array );

//! print current position of stepper
template<class S>
void position(const S& s)
{
  cout << "cur: " << s.current() << " ";
  cout << "idx: ";
  for (int i=0; i<int(s.index().size()); i++)
  { cout << s.index()[i] << " "; }
}

#define POSITION( S ) cout << " ### " << #S << ": "; position(S)

const char* bvalue(const bool& b)
{ if (b) return("true"); return("false"); }

#define BOOLIS( S ) cout << " *** " << #S << ": " << bvalue(S);

int main()
{
  cout << TF_SHAPETEST_CC_VERSION << endl;
  cout << TF_SHAPETEST_CC_CVSID << endl << endl;

  {
    Strided<3> A(tuple<Tsize>(3,3,3));

    INDEXER( A )
  }

  {
    cout << endl;
    cout << "Test SimpleRigidArray" << endl;
    cout << "=====================" << endl;
    cout << endl;

    SAPRINTER( tuple<Tsize>(5,8,2,5,4) )
    SAPRINTER( tuple(6,300,12,-18,2) )
    SAPRINTER( tuple(3.31e64,300.,12.,-18.,2.) )
    SAPRINTER( tuple(string("Hallo"),
                     string("das"),
                     string("klappt"),
                     string("auch"),
                     string("so")) )
  }

  {
    cout << endl;
    cout << "Test Stepper" << endl;
    cout << "============" << endl;
    cout << endl;

    Strided<3>::Tlimits first(tuple<Tsubscript>(2,1,5));
    Strided<3>::Tlimits last(tuple<Tsubscript>(4,4,6));
    Strided<3> S1(first,last);
    DenseStrided<3> S2(first,last);

    Strided<3>::Tstepper ST1(S1.stepper());
    DenseStrided<3>::Tstepper ST2(S2.stepper());

    POSITION(ST1);
    POSITION(ST2);
    cout << endl;
    cout << "to last:" << endl;
    ST1.tolast();
    ST2.tolast();
    POSITION(ST1);
    POSITION(ST2);
    cout << endl;
    cout << "to first:" << endl;
    ST1.tofirst();
    ST2.tofirst();
    POSITION(ST1);
    POSITION(ST2);
    cout << endl;
    cout << "increment:" << endl;
    for (int i=0; i<30; i++)
    {
      POSITION(ST1.incr());
      POSITION(ST2.incr());
      cout << endl;
    }
    cout << "while less:" << endl;
    do {
      POSITION(ST1.decr());
      POSITION(ST2.decr());
      cout << endl;
      BOOLIS(ST1.less());
      BOOLIS(ST2.less());
      cout << endl;
    } while (ST1.less() && ST2.less());
    cout << "to first:" << endl;
    ST1.tofirst();
    ST2.tofirst();
    POSITION(ST1);
    POSITION(ST2);
    cout << endl;
    cout << "to last:" << endl;
    ST1.tolast();
    ST2.tolast();
    POSITION(ST1);
    POSITION(ST2);
    cout << endl;
    cout << "decrement:" << endl;
    for (int i=0; i<30; i++)
    {
      POSITION(ST1.decr());
      POSITION(ST2.decr());
      cout << endl;
    }
    cout << "while more:" << endl;
    do {
      POSITION(ST1.incr());
      POSITION(ST2.incr());
      cout << endl;
    } while (ST1.more() && ST2.more());
  }
}

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