helpertest.cc 4.18 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file helpertest.cc
 * \brief test helpers like iterator, subarray and slice (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: helpertest.cc,v 1.2 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 23/12/2002
 * 
 * test helpers like iterator, subarray and slice (implementation)
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 23/12/2002   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
16
17
 *  - 28/12/2002   V1.1   (thof)
 *                        - new term for containers of const elements
thomas.forbriger's avatar
thomas.forbriger committed
18
19
20
21
 * 
 * ============================================================================
 */
#define AFF_HELPERTEST_CC_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
22
  "AFF_HELPERTEST_CC   V1.1"
thomas.forbriger's avatar
thomas.forbriger committed
23
#define AFF_HELPERTEST_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
24
  "$Id: helpertest.cc,v 1.2 2002-12-29 00:09:07 forbrig Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
25
26
27
28
29

/*! \example tests/helpertest.cc
 *
 * Test helper classes.
 *
thomas.forbriger's avatar
thomas.forbriger committed
30
31
32
33
34
35
36
37
38
39
40
41
42
 * This test program gives an example of the usage of the following classes,
 * functions, and preprocessor macros:
 *   - aff::Iterator
 *   - aff::Subarray
 *   - aff::Slice
 *   - aff::Array
 *   - aff::ConstArray
 *   - aff::Shaper
 *   - aff::dump
 *   - aff::dump_array
 *   - #DUMP
 *   - #CODE
 *
thomas.forbriger's avatar
thomas.forbriger committed
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
 * \sa tests/helpertest.cc
 */

#include <aff/array.h>
#include <aff/dump.h>
#include <aff/shaper.h>
#include <aff/iterator.h>
#include <aff/subarray.h>
#include <aff/slice.h>

using std::cout;
using std::endl;
using namespace aff;

/*----------------------------------------------------------------------*/

//! print headline
void section(const char* s, const char l='-')
{
  cout << endl << s << endl;
  const char* p=s;
  while (*p) { cout << l; ++p; }
  cout << endl;
}

/*----------------------------------------------------------------------*/

//! test for passing subarrays and slices
template<class T>
thomas.forbriger's avatar
thomas.forbriger committed
72
void printarray(const ConstArray<T>& array)
thomas.forbriger's avatar
thomas.forbriger committed
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
{
  cout << endl;
  cout << "Received array in function:" << endl;
  dump_array(array);
}

/*======================================================================*/

//! test helper classes
int main()
{
  cout << AFF_HELPERTEST_CC_VERSION << endl;
  cout << AFF_HELPERTEST_CC_CVSID << endl;

  section("Preparing test array");
  CODE( Array<int> A(Shaper(-2,2)(-3,3)) );
  CODE( dump_array(A) );

  section("Tests for class Iterator", '=');
  {
    CODE(int i=1);
    CODE(for( Iterator<Array<int> > I(A); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
  }
  
  section("Tests for class Subarray", '=');
  {
    section("First mode: Use a Subarray object just for initialization");
    CODE(Array<int> B=Subarray<int>(A)(-1,1)(-2,2));
    CODE( dump_array(B) );
    CODE(B=-5);
    CODE( dump_array(A) );
    section("Second mode: Create a Subarray object to which you can assign");
    CODE(Subarray<int> S);
    CODE(S(A)()(1,2)=100);
    CODE(S(A)(1,1)=-200);
    CODE( dump_array(A) );
    // you must explicitely create an array through member function array()
    CODE(printarray(Subarray<int>(A)(-2,0)(-3,0).array()));
  }
  
  section("Tests for class Slice", '=');
  {
    section("First mode: Use a Slice object just for initialization");
    CODE(Array<int> B=Slice<int>(A)(1,-1));
    CODE( dump_array(B) );
    CODE(B=555);
    CODE( dump_array(A) );
    section("Second mode: Create a Slice object to which you can assign");
    CODE(Slice<int> S);
    CODE(S(A)()(1)=666);
    CODE(S(A)(0)=-777);
    CODE( dump_array(A) );
    // you must explicitely create an array through member function array()
    CODE(printarray(Slice<int>(A)(-2).array()));
  }

  section("Mixed tests", '=');

  section("Iterator on a Subarray");
  {
    CODE(int i=501);
    CODE(Array<int> B=Subarray<int>(A)(-1,1)(-2,2));
    CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
  }

  section("Iterator on a Slice");
  {
    CODE(int i=-801);
    CODE(Array<int> B=Slice<int>(A)(1));
    CODE(for( Iterator<Array<int> > I(B); I.valid(); ++I) 
         { *I=i; i++; });
    CODE( dump_array(A) );
  }

  section("Extract copy of subarray");
  {
    CODE(Array<int> B=Subarray<int>(A)(-1,1)(-1,1));
    CODE(dump_array(B));
    CODE(Array<int> C=B.copyout());
    CODE(dump_array(C));
  }
}

/* ----- END OF helpertest.cc ----- */