source: trunk/test/iterator_test.cc @ 1877

Last change on this file since 1877 was 1877, checked in by Peter, 14 years ago

moving tests for iterator features from iterator_test.cc to class test::Suite so they can be utilized in several places.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.4 KB
Line 
1// $Id: iterator_test.cc 1877 2009-03-21 04:40:44Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
5  Copyright (C) 2009 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library 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 GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/classifier/DataLookup1D.h"
26
27#include "yat/classifier/DataLookupWeighted1D.h"
28#include "yat/classifier/MatrixLookup.h"
29#include "yat/classifier/MatrixLookupWeighted.h"
30#include "yat/utility/Container2DIterator.h"
31#include "yat/utility/DataWeight.h"
32#include "yat/utility/DataIterator.h"
33#include "yat/utility/Matrix.h"
34#include "yat/utility/MatrixWeighted.h"
35#include "yat/utility/stl_utility.h"
36#include "yat/utility/Vector.h"
37#include "yat/utility/VectorView.h"
38#include "yat/utility/VectorConstView.h"
39#include "yat/utility/WeightedIterator.h"
40#include "yat/utility/WeightIterator.h"
41
42#include <boost/iterator/transform_iterator.hpp>
43
44#include <algorithm>
45#include <fstream>
46#include <iterator>
47#include <string>
48#include <vector>
49
50using namespace theplu::yat;
51
52void old_main(test::Suite&);
53
54template<typename Iterator>
55void is_weighted(Iterator, test::Suite&);
56
57bool is_weighted(utility::unweighted_iterator_tag x) { return false; } 
58bool is_weighted(utility::weighted_iterator_tag x) { return true; }
59
60void test_boost_util(test::Suite&);
61
62void test_stride_iterator(test::Suite& suite);
63void test_weighted_iterator(test::Suite& suite);
64void test_matrix_lookup_iterator(test::Suite& suite);
65
66int main(int argc, char* argv[])
67{ 
68  test::Suite suite(argc, argv);
69  suite.err() << "testing iterator" << std::endl;
70
71  test_stride_iterator(suite);
72  old_main(suite);
73  test_boost_util(suite);
74  test_weighted_iterator(suite);
75  return suite.return_value();
76}
77 
78template<typename Iterator>
79void is_weighted(Iterator i, test::Suite& suite)
80{
81  suite.err() << "testing that iterator is unweighted... ";
82  typename utility::weighted_iterator_traits<Iterator>::type tag;
83  if (is_weighted(tag))
84    suite.err() << "ok.\n";
85  else {
86    suite.err() << "failed.\n";
87    suite.add(false);
88  }
89   
90}
91
92
93void old_main(test::Suite& suite)
94{
95  suite.err() << "testing utility::Vector::iterator" << std::endl;
96  utility::Vector vec(12);
97  classifier::DataLookup1D lookup(vec);
98  utility::Vector::iterator begin=vec.begin();
99  suite.test_random_access_iterator(begin);
100  // test iterator to const_iterator conversion
101  utility::Vector::const_iterator ci = vec.begin();
102  suite.test_random_access_iterator(ci);
103  ci = begin;
104  if (begin!=ci)
105    suite.add(false);
106
107  suite.err() << "sorting...";
108  utility::Vector::iterator end=vec.end();
109  std::sort(begin, end);
110  suite.err() << " sorting done\n";
111
112  suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl;
113  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
114  classifier::DataLookup1D::const_iterator lend=lookup.end();
115  suite.test_random_access_iterator(lbegin);
116 
117  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
118  std::copy(lbegin, lend, begin);
119  suite.err() << "copy from Vector to Vector" << std::endl;
120  std::copy(begin, end, begin);
121  suite.err() << "sort Vector" << std::endl;
122  std::sort(begin, end);
123
124  // test std algorithm on IteratorWeighted
125  utility::MatrixWeighted m_w(1, 3, 1, 1);
126  m_w(0,1).data()=2.0;
127  classifier::MatrixLookupWeighted mw(m_w);
128  classifier::DataLookupWeighted1D aw(mw,0,true);
129  size_t nof1=std::count(aw.begin(),aw.end(),utility::DataWeight(1.0, 1.0));
130  if(nof1!=2) {
131    suite.err() << "std algoritm with IteratorWeighted failed" << std::endl;
132    suite.add(false);
133  }
134  if (aw.begin()!=mw.begin_row(0))
135    suite.add(false);
136  if (aw.end()!=mw.end_row(0))
137    suite.add(false);
138  classifier::DataLookupWeighted1D aw2(mw,0,false);
139  if (aw2.begin()!=mw.begin_column(0))
140    suite.add(false);
141  if (aw2.end()!=mw.end_column(0))
142    suite.add(false);
143
144  utility::DataIterator<classifier::DataLookupWeighted1D::const_iterator> 
145    data_iter(aw2.begin());
146  suite.test_random_access_iterator(data_iter);
147  suite.test_random_access_iterator(weight_iterator(aw2.begin()));
148  suite.add(*data_iter == 1.0);
149  suite.add(*data_iterator(aw2.begin()) == 1.0);
150  std::vector<double> stl_vec(1,0);
151  utility::DataIterator<std::vector<double>::iterator> 
152    data_iter2(stl_vec.begin());
153  suite.add(*data_iter2 == 0.0);
154  *data_iter2 = 3.14;
155  suite.add(*data_iter2 == 3.14);
156  utility::WeightIterator<std::vector<double>::iterator> 
157    data_iter3(stl_vec.begin());
158  suite.add(*data_iter3 == 1.0);
159
160  // testing constness conversion
161  std::vector<double>::const_iterator c_iter(stl_vec.begin());
162  std::vector<double>::iterator iter(stl_vec.begin());
163  suite.add(iter==c_iter);
164  utility::DataIterator<std::vector<double>::const_iterator> 
165    data_iter4(c_iter);
166  utility::DataIterator<std::vector<double>::iterator> 
167    data_iter5(iter);
168  suite.add(data_iter2 == data_iter5);
169  suite.add(data_iter4 == data_iter5);
170  utility::StrideIterator<std::vector<double>::const_iterator> stride_ci(c_iter);
171  utility::StrideIterator<std::vector<double>::iterator> stride_i(iter);
172  suite.add(stride_ci==stride_i);
173  suite.add(stride_i==stride_ci);
174
175  utility::MatrixWeighted x_weighted(3,4);
176  x_weighted.begin();
177  *x_weighted.begin();
178  utility::DataWeight element = *x_weighted.begin();
179  *(x_weighted.begin()+1) = element;
180  double element_data = *data_iterator(x_weighted.begin());
181  suite.add(element_data==x_weighted.begin()->data());
182
183  utility::Matrix m(mw.rows(), mw.columns());
184  classifier::MatrixLookup ml(m);
185  classifier::DataLookup1D dl1(ml,0,true);
186  if (dl1.begin()!=ml.begin_row(0))
187    suite.add(false);
188  if (dl1.end()!=ml.end_row(0))
189    suite.add(false);
190  classifier::DataLookup1D dl2(ml,0,false);
191  if (dl2.begin()!=ml.begin_column(0))
192    suite.add(false);
193  if (dl2.end()!=ml.end_column(0))
194    suite.add(false);
195
196}
197
198void test_boost_util(test::Suite& suite)
199{
200  bool ok_cached=suite.ok();
201  utility::PairFirst<std::pair<std::string, int> > pf;
202  std::pair<std::string, int> p("July", 31);
203  std::string str=pf(p);
204  suite.add(str=="July");
205  pf(p)="juli";
206  suite.add(pf(p)=="juli");
207
208  typedef utility::PairFirst<std::pair<const std::string, int> > PF2;
209  PF2 pf2;
210  std::pair<const std::string, int> p2("July", 31);
211  suite.add(pf2(p2)=="July");
212
213  utility::PairFirst<const std::pair<std::string, int> > pf3;
214  std::pair<std::string, int> p3("July", 31);
215  suite.add(pf3(p3)=="July");
216
217  utility::PairFirst<std::pair<std::string, const int> > pf4;
218  std::pair<std::string, const int> p4("July", 31);
219  suite.add(pf4(p4)=="July");
220  pf4(p4)="juli";
221  suite.add(pf4(p4)=="juli");
222
223  utility::PairFirst<std::pair<const std::string, const int> > pf5;
224  std::pair<const std::string, const int> p5("July", 31);
225  suite.add(pf5(p5)=="July");
226
227  typedef std::map<std::string, int> Map;
228  Map m;
229  m["July"]=31;
230  m["September"]=30;
231  m["June"]=30;
232  m["April"]=30;
233
234  boost::transform_iterator<PF2, Map::iterator> first_iterator(m.begin(), pf2);
235  boost::transform_iterator<PF2, Map::iterator> first_iterator_end(m.end(), pf2);
236  std::vector<std::string> vec(m.size());
237  std::copy(first_iterator, first_iterator_end, vec.begin());
238  std::vector<std::string> correct;
239  correct.push_back("April");
240  correct.push_back("July");
241  correct.push_back("June");
242  correct.push_back("September");
243  for (size_t i=0; i<vec.size(); ++i)
244    if (!suite.add(vec[i]==correct[i]))
245      suite.err() << "Error: vec[" << i << "] = " << vec[i] 
246                  << " expected " << correct[i] << "\n";
247
248  std::vector<std::string> vec2(m.size());
249  std::copy(utility::pair_first_iterator(m.begin()), 
250            utility::pair_first_iterator(m.end()),
251            vec2.begin());
252  suite.add(vec2==vec);
253 
254  std::vector<int> days;
255  days.resize(m.size());
256  std::copy(utility::pair_second_iterator(m.begin()), 
257            utility::pair_second_iterator(m.end()),
258            days.begin());
259  std::vector<int> days_correct;
260  days_correct.push_back(30);
261  days_correct.push_back(31);
262  days_correct.push_back(30);
263  days_correct.push_back(30);
264  for (size_t i=0; i<days.size(); ++i)
265    if (!suite.add(days[i]==days_correct[i]))
266      suite.err() << "Error: days[" << i << "] = " << days[i] 
267                  << " expected " << days_correct[i] << "\n";
268  days = std::vector<int>(days.size(), 0);
269  std::copy(days.begin(), days.end(), utility::pair_second_iterator(m.begin()));
270  for (std::map<std::string, int>::const_iterator i=m.begin(); i!=m.end(); ++i)
271    if (!suite.add(i->second==0) )
272      suite.err() << "Error: m[" << i->first << "] = " << i->second
273                  << " expected 0\n";
274
275  if (ok_cached && !suite.ok())
276    suite.err() << "test_bool_util failed" << std::endl;
277}
278
279void test_stride_iterator(test::Suite& suite)
280{
281  suite.err() << "testing StrideIterator" << std::endl;
282  using utility::Vector;
283  Vector a(10);
284  // stride 2
285  utility::VectorConstView b(a, 0, 5, 2);
286  if (!suite.add(b.end()-b.begin()==5))
287    suite.err() << "ERROR: StrideIterator::operator- returned: " 
288                << b.end()-b.begin() 
289                << " expected 5\n";
290  utility::VectorView c(a, 0, 3, 2);
291  Vector::iterator begin = c.begin();
292  Vector::iterator end = c.end();
293  Vector::const_iterator cbegin = begin;
294  Vector::const_iterator cend = end;
295
296  if (!suite.add(c.size()==3))
297    suite.err() << "c.size() incorrect" << std::endl;
298  if (!suite.add(cend-cbegin == 3))
299    suite.err() << "cend-cbegin failed\n"; 
300}
301
302void test_weighted_iterator(test::Suite& suite)
303{
304  std::vector<double> vec(3,1);
305  typedef std::vector<double>::iterator Iter;
306  typedef utility::WeightedIterator<Iter, Iter> WIter;
307  WIter iter(vec.begin(), vec.begin());
308
309  utility::DataWeight tmp = *iter;
310
311  suite.test_bidirectional_iterator(iter);
312  suite.test_random_access_iterator(iter);
313  std::vector<double> data(vec.size());
314  std::vector<double> weight(vec.size());
315  WIter iter2(data.begin(), weight.begin());
316  utility::DataWeight tmp2(6.89, 0.79);
317  *iter2 = tmp2;
318  if (!suite.add(tmp2==*iter2))
319    suite.err() << "error: tmp2==*iter2" << std::endl;
320  utility::DataWeight tmp3(*iter2);
321  suite.add(suite.equal(tmp3.data(), tmp2.data()));
322  suite.add(suite.equal(tmp3.weight(), tmp2.weight()));
323  *iter2 = *iter;
324  if (!suite.add(*iter2 == *iter))
325    suite.err() << "error: *iter2 == *iter\n";
326  tmp = *iter2;
327  suite.add(suite.equal(tmp.data(), 1));
328  std::copy(iter, iter+3, iter2);
329  suite.add(suite.equal(vec.front(), 1));
330  suite.add(suite.equal(data.front(), 1));
331
332  suite.err() << "  testing const conversion\n";
333  typedef std::vector<double>::const_iterator const_Iter;
334  typedef utility::WeightedIterator<const_Iter, const_Iter> const_WIter;
335  const_WIter const_iter(vec.begin(), vec.begin());
336  const_iter = iter;
337 
338  suite.err() << "  testing assignment between different iterators\n";
339  const std::vector<double> const_vec(10, 10.7);
340  const_WIter const_iter2(const_vec.begin(), const_vec.begin());
341  utility::DataWeight tmp4;
342  tmp4 = *const_iter2;
343  *iter = *const_iter2;
344  suite.add(*iter==*const_iter2);
345  *iter = *const_iter;
346  suite.add(*iter==*const_iter);
347
348  double x=101;
349  utility::WeightedIterator<double*, double*> iter_p(&x, &x);
350  *iter_p = *iter;
351  suite.add(*iter_p==*iter); 
352}
353
354void test_matrix_lookup_iterator(test::Suite& suite)
355{
356  classifier::MatrixLookup ml(10, 10);
357  suite.test_random_access_iterator(ml.begin());
358  std::vector<double> vec(ml.rows()*ml.columns());
359  std::copy(ml.begin(), ml.end(), vec.begin());
360}
361
Note: See TracBrowser for help on using the repository browser.