Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestArray2D.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
22 
23 /**
24  @file TestArray2D.cpp
25  @relates Array2D
26  @brief Test Array 2D
27  @ingroup g_tests
28  @author MIP
29 */
30 
31 #include <Base/Common/Array2D.hh>
32 #include <Base/Debug/Error.hh>
33 #include <Base/Debug/Exception.hh>
34 
35 #include <vector>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 
41 const bool verbose = true;
42 
43 /// macro testing if command has thrown an exception
44 #define SHOULD_THROW(command) \
45 { \
46  bool has_thrown = false; \
47  try { command; } \
48  catch ( BaseException b ) { has_thrown = true; } \
49  if (!has_thrown) { \
50  if (verbose) cout << #command << " was expected to throw an exception"\
51  <<", but it has not thrown. Test failed. \n"<<endl; \
52  BIASABORT; \
53  } \
54 }
55 
56 void TestInit();
57 
58 void TestAccess();
59 
60 void TestIterator();
61 
62 void TestCompileErrors();
63 
64 int main(int argc, char *argv[])
65 {
66  TestInit();
67  TestAccess();
68  TestIterator();
69  TestCompileErrors();
70 
71  return 0;
72 }
73 
74 
75 void TestInit()
76 {
77  const unsigned nrow = 5, ncol = 7;
78 
79  Array2D<int> foo;
80  if (!foo.empty()) { BIASABORT; }
81  if (foo.nrows() != 0) { BIASABORT; }
82  if (foo.ncols() != 0) { BIASABORT; }
83  if (foo.size() != 0) { BIASABORT; }
84 
85  Array2D<unsigned> foo2(nrow, ncol);
86  if (foo2.empty()) { BIASABORT; }
87  if (foo2.nrows() != nrow) { BIASABORT; }
88  if (foo2.ncols() != ncol) { BIASABORT; }
89  if (foo2.size() != ncol * nrow) { BIASABORT; }
90 
91  Array2D<unsigned> cp(foo2);
92  if (cp.empty()) { BIASABORT; }
93  if (cp.nrows() != nrow) { BIASABORT; }
94  if (cp.ncols() != ncol) { BIASABORT; }
95 
96  Array2D<unsigned> uta = cp;
97  if (uta.empty()) { BIASABORT; }
98  if (uta.nrows() != nrow) { BIASABORT; }
99  if (uta.ncols() != ncol) { BIASABORT; }
100 
101  foo2.clear();
102  if (!foo2.empty()) { BIASABORT; }
103  if (foo2.nrows() != 0) { BIASABORT; }
104  if (foo2.ncols() != 0) { BIASABORT; }
105 
106  foo2.resize(nrow, ncol);
107  if (foo2.empty()) { BIASABORT; }
108  if (foo2.nrows() != nrow) { BIASABORT; }
109  if (foo2.ncols() != ncol) { BIASABORT; }
110  if (foo2.size() != ncol * nrow) { BIASABORT; }
111 
112 }
113 
114 
115 void TestAccess()
116 {
117  const unsigned nrow = 11, ncol = 17;
118  const unsigned prow = nrow-1, pcol = ncol-1;
119  const int val = -1, val2 = 42;
120  // test checked access using round brackets
121  Array2D<int> foo(nrow, ncol);
122  foo.fill(val);
123  foo(prow, pcol) = val2;
124  for (unsigned r=0; r<nrow; r++){
125  for (unsigned c=0; c<ncol; c++){
126  if (r==prow && c==pcol){
127  if (foo(r,c) != val2) { BIASABORT; }
128  } else {
129  if (foo(r,c) != val) { BIASABORT; }
130  }
131  }
132  }
133  // check if operator() throws if accessing out of range
134  SHOULD_THROW(foo(nrow, 0));
135  SHOULD_THROW(foo(0, ncol));
136  SHOULD_THROW(foo(nrow, ncol));
137  SHOULD_THROW(foo(nrow+42, 0));
138  SHOULD_THROW(foo(0, ncol+42));
139  SHOULD_THROW(foo(nrow+42, ncol+42));
140  //
141 
142 
143  // test access using square brackets
144  foo.fill(val2);
145  foo[prow][pcol] = val;
146  for (unsigned r=0; r<nrow; r++){
147  for (unsigned c=0; c<ncol; c++){
148  if (r==prow && c==pcol){
149  if (foo[r][c] != val) { BIASABORT; }
150  } else {
151  if (foo[r][c] != val2) { BIASABORT; }
152  }
153  }
154  }
155 }
156 
157 
158 void TestIterator()
159 {
160  const unsigned nrow = 3, ncol = 4;
161  const unsigned prow = nrow-1, pcol = ncol-1;
162  const int val = -1, val2 = 42;
163  Array2D<int> foo(nrow, ncol);
164  foo.fill(val2);
165  foo[prow][pcol] = val;
166 
167  // test const_iterator access
168  const Array2D<int> cfoo(foo);
170  unsigned pos = 0;
171  for (cit = cfoo.begin(); cit != cfoo.end(); cit++, pos++){
172  if (pos == prow * ncol + pcol){
173  if (*cit != val) { BIASABORT; }
174  } else {
175  if (*cit != val2) { BIASABORT; }
176  }
177  }
178 
179  // test iterator access
181  int ipos = 0;
182  for (it = foo.begin(); it != foo.end(); it++, ipos++){
183  *it = ipos;
184  }
185  for (unsigned r=0; r<nrow; r++){
186  for (unsigned c=0; c<ncol; c++){
187  if (foo[r][c] != (int)(r*ncol + c)) { BIASABORT; }
188  }
189  }
190 
191  // test operator-> of const iterator
192  Array2D<vector<int> > vfoo(nrow, ncol);
193  const vector<int> vval(1, val2), vval2(1, val);
194  vfoo.fill(vval);
195  vfoo[prow][pcol] = vval2;
196  Array2D<vector<int> > cvfoo(vfoo);
197 
198  Array2D<vector<int> >::const_iterator cvit;
199  Array2D<vector<int> >::iterator vit;
200  pos = 0;
201  for (cvit = cvfoo.begin(); cvit != cvfoo.end(); cvit++, pos++){
202  if (pos == prow * ncol + pcol){
203  if (cvit->operator[](0) != val) { BIASABORT; }
204  } else {
205  if (cvit->operator[](0) != val2) { BIASABORT; }
206  }
207  }
208 
209  // test operator-> of iterator
210  ipos = 0;
211  for (vit = vfoo.begin(); vit != vfoo.end(); vit++, ipos++){
212  vit->resize(2);
213  vit->operator[](1) = ipos;
214  }
215  for (unsigned r=0; r<nrow; r++){
216  for (unsigned c=0; c<ncol; c++){
217  if (vfoo[r][c][1] != (int)(r*ncol + c)) { BIASABORT; }
218  }
219  }
220 
221 }
222 
223 
224 void TestCompileErrors()
225 {
226  const unsigned nrow = 11, ncol = 17;
227  Array2D<int> foo(nrow, ncol);
228  foo.fill(-1);
229  const Array2D<int> cfoo(foo);
230  // non-const iterator from non-const object
231  Array2D<int>::iterator it = foo.begin();
232  // const_iterator from const object
233  Array2D<int>::const_iterator cit = cfoo.begin();
234  // const_iterator from non-const object
235  Array2D<int>::const_iterator cit2 = foo.begin();
236 
237 #ifdef TEST_COMPILE_TIME_ERROR_ConstIterator
238  // cannot take iterator from const object
239  Array2D<int>::iterator it2 = cfoo.begin();
240 #endif
241 
242 #ifdef TEST_COMPILE_TIME_ERROR_TemplateMismatch
243  // template type mismatch
244  Array2D<unsigned>::iterator uit = foo.begin();
245 #endif
246 
247 }
for const_iterator access todo: derive from std::iterator class(es)
Definition: Array2D.hh:113
generic two dimensional rectangular array holding arbitrary data types
Definition: Array2D.hh:31
unsigned ncols() const
Definition: Array2D.hh:71
for iterator access todo: derive from std::iterator class(es)
Definition: Array2D.hh:90
unsigned size() const
Definition: Array2D.hh:75
void resize(const unsigned nrows, const unsigned ncols)
preserves the content
Definition: Array2D.hh:203
const_iterator end() const
Definition: Array2D.hh:137
unsigned nrows() const
Definition: Array2D.hh:73
bool empty() const
Definition: Array2D.hh:65
void fill(const T &val)
fills the array with val
Definition: Array2D.hh:227
const_iterator begin() const
Definition: Array2D.hh:136