Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestSparseArray2D.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 /**
25  @file TestSparseArray2D.cpp
26  @relates SparseArray2D
27  @brief Test sparsely occupied Array2D
28  @ingroup g_tests
29  @author MIP
30 */
31 
32 #include <Base/Common/SparseArray2D.hh>
33 #include <Base/Debug/Error.hh>
34 #include <Base/Debug/Exception.hh>
35 
36 using namespace BIAS;
37 using namespace std;
38 
39 
40 const bool verbose = true;
41 
42 /// macro testing if command has thrown an exception
43 #define SHOULD_THROW(command) \
44 { \
45  bool has_thrown = false; \
46  try { command; } \
47  catch ( BaseException b ) { has_thrown = true; } \
48  if (!has_thrown) { \
49  if (verbose) cout << #command << " was expected to throw an exception"\
50  <<", but it has not thrown. Test failed. \n"<<endl; \
51  BIASABORT; \
52  } \
53 }
54 
55 void TestInit();
56 
57 void TestAccess();
58 
59 void TestIterator();
60 
61 void TestCompileErrors();
62 
63 template <class T>
64 void Fill(SparseArray2D<T> &arg, const T& val);
65 
66 int main(int argc, char *argv[])
67 {
68  TestInit();
69  TestAccess();
70  TestIterator();
71  TestCompileErrors();
72 
73  return 0;
74 }
75 
76 
77 void TestInit()
78 {
79  const unsigned nrow = 5, ncol = 7;
80 
82  if (!foo.empty()) { BIASABORT; }
83  if (foo.nrows() != 0) { BIASABORT; }
84  if (foo.ncols() != 0) { BIASABORT; }
85  if (foo.size() != 0) { BIASABORT; }
86 
87  SparseArray2D<unsigned> foo2(nrow, ncol);
88  if (foo2.empty()) { BIASABORT; }
89  if (foo2.nrows() != nrow) { BIASABORT; }
90  if (foo2.ncols() != ncol) { BIASABORT; }
91  if (foo2.size() != ncol * nrow) { BIASABORT; }
92 
93  SparseArray2D<unsigned> cp(foo2);
94  if (cp.empty()) { BIASABORT; }
95  if (cp.nrows() != nrow) { BIASABORT; }
96  if (cp.ncols() != ncol) { BIASABORT; }
97 
98  SparseArray2D<unsigned> uta = cp;
99  if (uta.empty()) { BIASABORT; }
100  if (uta.nrows() != nrow) { BIASABORT; }
101  if (uta.ncols() != ncol) { BIASABORT; }
102 
103  foo2.clear();
104  if (!foo2.empty()) { BIASABORT; }
105  if (foo2.nrows() != 0) { BIASABORT; }
106  if (foo2.ncols() != 0) { BIASABORT; }
107 
108  foo2.resize(nrow, ncol);
109  if (foo2.empty()) { BIASABORT; }
110  if (foo2.nrows() != nrow) { BIASABORT; }
111  if (foo2.ncols() != ncol) { BIASABORT; }
112  if (foo2.size() != ncol * nrow) { BIASABORT; }
113 
114 }
115 
116 
117 void TestAccess()
118 {
119  const unsigned nrow = 11, ncol = 17;
120  const unsigned prow = nrow-1, pcol = ncol-1;
121  const int val = -1, val2 = 42;
122  // test checked access using round brackets
123  SparseArray2D<int> foo(nrow, ncol);
124  Fill(foo, val);
125  foo(prow, pcol) = val2;
126  for (unsigned r=0; r<nrow; r++){
127  for (unsigned c=0; c<ncol; c++){
128  if (r==prow && c==pcol){
129  if (foo(r,c) != val2) { BIASABORT; }
130  } else {
131  if (foo(r,c) != val) { BIASABORT; }
132  }
133  }
134  }
135  // check if operator() throws if accessing out of range
136  SHOULD_THROW(foo(nrow, 0));
137  SHOULD_THROW(foo(0, ncol));
138  SHOULD_THROW(foo(nrow, ncol));
139  SHOULD_THROW(foo(nrow+42, 0));
140  SHOULD_THROW(foo(0, ncol+42));
141  SHOULD_THROW(foo(nrow+42, ncol+42));
142 
143 }
144 
145 void TestIterator()
146 {
147  const unsigned nrow = 3, ncol = 4;
148  const unsigned prow = nrow-1, pcol = ncol-1;
149  const int val = -1, val2 = 42;
150  SparseArray2D<int> foo(nrow, ncol);
151  Fill(foo, val2);
152  foo(prow, pcol) = val;
153 
154  // test const_iterator access
155  const SparseArray2D<int> cfoo(foo);
157  unsigned pos = 0;
158  for (cit = cfoo.begin(); cit != cfoo.end(); cit++, pos++){
159  if (pos == prow * ncol + pcol){
160  if (*cit != val) { BIASABORT; }
161  } else {
162  if (*cit != val2) { BIASABORT; }
163  }
164  }
165 
166  // test iterator access
168  int ipos = 0;
169  for (it = foo.begin(); it != foo.end(); it++, ipos++){
170  *it = ipos;
171  }
172  for (unsigned r=0; r<nrow; r++){
173  for (unsigned c=0; c<ncol; c++){
174  if (foo(r,c) != (int)(r*ncol + c)) { BIASABORT; }
175  }
176  }
177 }
178 
179 
180 void TestCompileErrors()
181 {
182  const unsigned nrow = 11, ncol = 17;
183  SparseArray2D<int> foo(nrow, ncol);
184  Fill(foo, -1);
185  const SparseArray2D<int> cfoo(foo);
188 
189  cit = cfoo.begin(); // const_iterator from const object
190  cit = foo.begin(); // const_iterator from non-const object
191 
192 #ifdef TEST_COMPILE_TIME_ERROR_ConstIterator
193  it = cfoo.begin(); // cannot take iterator from const object
194 #endif
195 
196 #ifdef TEST_COMPILE_TIME_ERROR_TemplateMismatch
198  uit = foo.begin(); // template type mismatch
199 #endif
200 
201 }
202 
203 template <class T>
204 void Fill(SparseArray2D<T> &arg, const T& val)
205 {
206  const unsigned nr=arg.nrows(), nc=arg.ncols();
207  unsigned r, c;
208  for (r=0; r<nr; r++){
209  for (c=0; c<nc; c++){
210  arg(r, c) = val;
211  }
212  }
213 }
const_iterator begin() const
for iterator access todo: derive from std::iterator class(es)
bool empty() const
generic two dimensional psarsly populated rectangular array holding arbitrary data types ...
unsigned size() const
for const_iterator access todo: derive from std::iterator class(es)
const_iterator end() const
unsigned ncols() const
unsigned nrows() const