source: trunk/test/iterator_test.cc @ 1749

Last change on this file since 1749 was 1749, checked in by Peter, 12 years ago

fixes #480

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.6 KB
Line 
1// $Id: iterator_test.cc 1749 2009-01-26 12:36:39Z peter $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
5  Copyright (C) 2008 Peter Johansson, Markus Ringnér
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/MatrixLookupWeighted.h"
29#include "yat/utility/Container2DIterator.h"
30#include "yat/utility/DataWeight.h"
31#include "yat/utility/DataIterator.h"
32#include "yat/utility/Matrix.h"
33#include "yat/utility/MatrixWeighted.h"
34#include "yat/utility/stl_utility.h"
35#include "yat/utility/Vector.h"
36#include "yat/utility/VectorView.h"
37#include "yat/utility/VectorConstView.h"
38#include "yat/utility/WeightedIterator.h"
39#include "yat/utility/WeightIterator.h"
40
41#include <boost/iterator/transform_iterator.hpp>
42
43#include <algorithm>
44#include <fstream>
45#include <iterator>
46#include <string>
47#include <vector>
48
49using namespace theplu::yat;
50
51void old_main(test::Suite&);
52
53template<typename Iterator>
54void is_weighted(Iterator, test::Suite&);
55
56bool is_weighted(utility::unweighted_iterator_tag x) { return false; } 
57bool is_weighted(utility::weighted_iterator_tag x) { return true; }
58
59void test_boost_util(test::Suite&);
60
61template<typename TrivialIterator>
62void test_trivial_iterator(const TrivialIterator&, test::Suite&);
63
64template<typename InputIterator>
65void test_input_iterator(InputIterator&, test::Suite&);
66
67template<typename OutputIterator>
68void test_output_iterator(OutputIterator&, test::Suite&);
69
70template<typename ForwardIterator>
71void test_forward_iterator(ForwardIterator, test::Suite&);
72
73template<typename BidirectionalIterator>
74void test_bidirectional_iterator(BidirectionalIterator, test::Suite&);
75
76template<typename RandomAccessIterator>
77void test_random_access_iterator(RandomAccessIterator, test::Suite&);
78
79void test_stride_iterator(test::Suite& suite);
80void test_weighted_iterator(test::Suite& suite);
81
82int main(int argc, char* argv[])
83{ 
84  test::Suite suite(argc, argv);
85  suite.err() << "testing iterator" << std::endl;
86
87  test_stride_iterator(suite);
88  old_main(suite);
89  test_boost_util(suite);
90  test_weighted_iterator(suite);
91  return suite.return_value();
92}
93 
94template<typename Iterator>
95void is_weighted(Iterator i, test::Suite& suite)
96{
97  suite.err() << "testing that iterator is unweighted... ";
98  typename utility::weighted_iterator_traits<Iterator>::type tag;
99  if (is_weighted(tag))
100    suite.err() << "ok.\n";
101  else {
102    suite.err() << "failed.\n";
103    suite.add(false);
104  }
105   
106}
107
108
109void old_main(test::Suite& suite)
110{
111  suite.err() << "testing utility::Vector::iterator" << std::endl;
112  utility::Vector vec(12);
113  classifier::DataLookup1D lookup(vec);
114  utility::Vector::iterator begin=vec.begin();
115  test_random_access_iterator(begin, suite);
116  // test iterator to const_iterator conversion
117  utility::Vector::const_iterator ci = vec.begin();
118  test_random_access_iterator(ci, suite);
119  ci = begin;
120  if (begin!=ci)
121    suite.add(false);
122
123  suite.err() << "sorting...";
124  utility::Vector::iterator end=vec.end();
125  std::sort(begin, end);
126  suite.err() << " sorting done\n";
127
128  suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl;
129  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
130  classifier::DataLookup1D::const_iterator lend=lookup.end();
131  test_random_access_iterator(lbegin, suite);
132 
133  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
134  std::copy(lbegin, lend, begin);
135  suite.err() << "copy from Vector to Vector" << std::endl;
136  std::copy(begin, end, begin);
137  suite.err() << "sort Vector" << std::endl;
138  std::sort(begin, end);
139
140  // test std algorithm on IteratorWeighted
141  utility::MatrixWeighted m_w(1, 3, 1, 1);
142  m_w(0,1).data()=2.0;
143  classifier::MatrixLookupWeighted mw(m_w);
144  classifier::DataLookupWeighted1D aw(mw,0,true);
145  size_t nof1=std::count(aw.begin(),aw.end(),utility::DataWeight(1.0, 1.0));
146  if(nof1!=2) {
147    suite.err() << "std algoritm with IteratorWeighted failed" << std::endl;
148    suite.add(false);
149  }
150  if (aw.begin()!=mw.begin_row(0))
151    suite.add(false);
152  if (aw.end()!=mw.end_row(0))
153    suite.add(false);
154  classifier::DataLookupWeighted1D aw2(mw,0,false);
155  if (aw2.begin()!=mw.begin_column(0))
156    suite.add(false);
157  if (aw2.end()!=mw.end_column(0))
158    suite.add(false);
159
160  utility::DataIterator<classifier::DataLookupWeighted1D::const_iterator> 
161    data_iter(aw2.begin());
162  test_random_access_iterator(data_iter, suite);
163  test_random_access_iterator(weight_iterator(aw2.begin()), suite);
164  suite.add(*data_iter == 1.0);
165  suite.add(*data_iterator(aw2.begin()) == 1.0);
166  std::vector<double> stl_vec(1,0);
167  utility::DataIterator<std::vector<double>::iterator> 
168    data_iter2(stl_vec.begin());
169  suite.add(*data_iter2 == 0.0);
170  *data_iter2 = 3.14;
171  suite.add(*data_iter2 == 3.14);
172  utility::WeightIterator<std::vector<double>::iterator> 
173    data_iter3(stl_vec.begin());
174  suite.add(*data_iter3 == 1.0);
175
176  // testing constness conversion
177  std::vector<double>::const_iterator c_iter(stl_vec.begin());
178  std::vector<double>::iterator iter(stl_vec.begin());
179  suite.add(iter==c_iter);
180  utility::DataIterator<std::vector<double>::const_iterator> 
181    data_iter4(c_iter);
182  utility::DataIterator<std::vector<double>::iterator> 
183    data_iter5(iter);
184  suite.add(data_iter2 == data_iter5);
185  suite.add(data_iter4 == data_iter5);
186  utility::StrideIterator<std::vector<double>::const_iterator> stride_ci(c_iter);
187  utility::StrideIterator<std::vector<double>::iterator> stride_i(iter);
188  suite.add(stride_ci==stride_i);
189  suite.add(stride_i==stride_ci);
190
191  utility::MatrixWeighted x_weighted(3,4);
192  x_weighted.begin();
193  *x_weighted.begin();
194  utility::DataWeight element = *x_weighted.begin();
195  *(x_weighted.begin()+1) = element;
196  double element_data = *data_iterator(x_weighted.begin());
197  suite.add(element_data==x_weighted.begin()->data());
198
199  utility::Matrix m(mw.rows(), mw.columns());
200  classifier::MatrixLookup ml(m);
201  classifier::DataLookup1D dl1(ml,0,true);
202  if (dl1.begin()!=ml.begin_row(0))
203    suite.add(false);
204  if (dl1.end()!=ml.end_row(0))
205    suite.add(false);
206  classifier::DataLookup1D dl2(ml,0,false);
207  if (dl2.begin()!=ml.begin_column(0))
208    suite.add(false);
209  if (dl2.end()!=ml.end_column(0))
210    suite.add(false);
211
212}
213
214void test_boost_util(test::Suite& suite)
215{
216  bool ok_cached=suite.ok();
217  utility::PairFirst<std::pair<std::string, int> > pf;
218  std::pair<std::string, int> p("July", 31);
219  std::string str=pf(p);
220  suite.add(str=="July");
221  pf(p)="juli";
222  suite.add(pf(p)=="juli");
223
224  typedef utility::PairFirst<std::pair<const std::string, int> > PF2;
225  PF2 pf2;
226  std::pair<const std::string, int> p2("July", 31);
227  suite.add(pf2(p2)=="July");
228
229  utility::PairFirst<const std::pair<std::string, int> > pf3;
230  std::pair<std::string, int> p3("July", 31);
231  suite.add(pf3(p3)=="July");
232
233  utility::PairFirst<std::pair<std::string, const int> > pf4;
234  std::pair<std::string, const int> p4("July", 31);
235  suite.add(pf4(p4)=="July");
236  pf4(p4)="juli";
237  suite.add(pf4(p4)=="juli");
238
239  utility::PairFirst<std::pair<const std::string, const int> > pf5;
240  std::pair<const std::string, const int> p5("July", 31);
241  suite.add(pf5(p5)=="July");
242
243  typedef std::map<std::string, int> Map;
244  Map m;
245  m["July"]=31;
246  m["September"]=30;
247  m["June"]=30;
248  m["April"]=30;
249
250  boost::transform_iterator<PF2, Map::iterator> first_iterator(m.begin(), pf2);
251  boost::transform_iterator<PF2, Map::iterator> first_iterator_end(m.end(), pf2);
252  std::vector<std::string> vec(m.size());
253  std::copy(first_iterator, first_iterator_end, vec.begin());
254  std::vector<std::string> correct;
255  correct.push_back("April");
256  correct.push_back("July");
257  correct.push_back("June");
258  correct.push_back("September");
259  for (size_t i=0; i<vec.size(); ++i)
260    if (!suite.add(vec[i]==correct[i]))
261      suite.err() << "Error: vec[" << i << "] = " << vec[i] 
262                  << " expected " << correct[i] << "\n";
263
264  std::vector<std::string> vec2(m.size());
265  std::copy(utility::pair_first_iterator(m.begin()), 
266            utility::pair_first_iterator(m.end()),
267            vec2.begin());
268  suite.add(vec2==vec);
269 
270  std::vector<int> days;
271  days.resize(m.size());
272  std::copy(utility::pair_second_iterator(m.begin()), 
273            utility::pair_second_iterator(m.end()),
274            days.begin());
275  std::vector<int> days_correct;
276  days_correct.push_back(30);
277  days_correct.push_back(31);
278  days_correct.push_back(30);
279  days_correct.push_back(30);
280  for (size_t i=0; i<days.size(); ++i)
281    if (!suite.add(days[i]==days_correct[i]))
282      suite.err() << "Error: days[" << i << "] = " << days[i] 
283                  << " expected " << days_correct[i] << "\n";
284  days = std::vector<int>(days.size(), 0);
285  std::copy(days.begin(), days.end(), utility::pair_second_iterator(m.begin()));
286  for (std::map<std::string, int>::const_iterator i=m.begin(); i!=m.end(); ++i)
287    if (!suite.add(i->second==0) )
288      suite.err() << "Error: m[" << i->first << "] = " << i->second
289                  << " expected 0\n";
290
291  if (ok_cached && !suite.ok())
292    suite.err() << "test_bool_util failed" << std::endl;
293}
294
295template<typename TrivialIterator>
296void test_trivial_iterator(const TrivialIterator& iter, test::Suite& suite)
297{
298  suite.err() << "  testing Trivial features" << std::endl;
299  typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
300  suite.add(tmp==*iter);
301}
302
303template<typename InputIterator>
304void test_input_iterator(InputIterator& iter, test::Suite& suite)
305{
306  test_trivial_iterator(iter, suite);
307  suite.err() << "  testing Input features" << std::endl;
308  // just to check compilation
309  if (false) {
310    ++iter;
311    iter++;
312  }
313}
314
315template<typename OutputIterator>
316void test_output_iterator(OutputIterator& iter, test::Suite& suite)
317{
318  test_trivial_iterator(iter, suite);
319  suite.err() << "  testing Output features" << std::endl;
320}
321
322template<typename ForwardIterator>
323void test_forward_iterator(ForwardIterator iter, test::Suite& suite)
324{
325  test_output_iterator(iter, suite);
326  test_input_iterator(iter, suite);
327  suite.err() << "  testing Forward features" << std::endl;
328
329  typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
330  // testing multiple traversing is possible and does not change the data
331  ForwardIterator iter1 = iter;
332  ++iter1;
333  suite.add(iter!=iter1);
334  ForwardIterator iter2 = iter;
335  ++iter2;
336  suite.add(tmp==*iter);
337
338}
339
340template<typename BidirectionalIterator>
341void test_bidirectional_iterator(BidirectionalIterator iter, test::Suite& suite)
342{
343  test_forward_iterator(iter, suite);
344  bool ok_cached = suite.ok();
345  suite.err() << "  testing Bidirectional features" << std::endl;
346  const BidirectionalIterator i = iter;
347  BidirectionalIterator tmp = iter--;
348  suite.add(tmp==i);
349  ++iter;
350  tmp = --iter;
351  if (!suite.add(tmp==iter))
352    suite.err() << "operator-- failed" << std::endl;
353  suite.add(++tmp==i);
354  if (ok_cached && !suite.ok())
355    suite.err() << "failed" << std::endl;
356}
357
358template<typename RandomAccessIterator>
359void test_random_access_iterator(RandomAccessIterator iter, test::Suite& suite)
360{
361  test_bidirectional_iterator(iter, suite);
362  suite.err() << "  testing RandomAccess features" << std::endl;
363  bool ok_cached = suite.ok();
364  RandomAccessIterator iter2 = iter;
365  iter2 += 1;
366  iter2 -= 1;
367  RandomAccessIterator& iter3 = (iter2 += 1);
368  RandomAccessIterator& iter4 = (iter3 -= 1);
369  if (!suite.add(iter2 == iter4))
370    suite.err() << "operator-(int) failed" << std::endl;
371  suite.add(++iter2 == iter3);
372 
373  RandomAccessIterator iter5 = iter + 0;
374  RandomAccessIterator iter6 = 0 + iter;
375  suite.add(iter6 == iter5);
376
377  RandomAccessIterator iter7 = iter - 0;
378  suite.add(iter7 == iter);
379  suite.add(iter7 - iter == 0);
380  suite.add(! (iter7<iter));
381
382  typename RandomAccessIterator::value_type tmp = iter[0];
383  typename RandomAccessIterator::value_type tmp2 = *iter;
384  tmp = tmp; // avoid compiler warning
385  if (!suite.add(tmp == tmp2))
386    suite.err() << "operator[] failed" << std::endl;
387  if (!suite.add(iter[0] == *iter))
388    suite.err() << "operator[] failed" << std::endl;
389  if (ok_cached && !suite.ok())
390    suite.err() << "failed" << std::endl;
391}
392
393void test_stride_iterator(test::Suite& suite)
394{
395  suite.err() << "testing StrideIterator" << std::endl;
396  using utility::Vector;
397  Vector a(10);
398  // stride 2
399  utility::VectorConstView b(a, 0, 5, 2);
400  if (!suite.add(b.end()-b.begin()==5))
401    suite.err() << "ERROR: StrideIterator::operator- returned: " 
402                << b.end()-b.begin() 
403                << " expected 5\n";
404  utility::VectorView c(a, 0, 3, 2);
405  Vector::iterator begin = c.begin();
406  Vector::iterator end = c.end();
407  Vector::const_iterator cbegin = begin;
408  Vector::const_iterator cend = end;
409
410  if (!suite.add(c.size()==3))
411    suite.err() << "c.size() incorrect" << std::endl;
412  if (!suite.add(cend-cbegin == 3))
413    suite.err() << "cend-cbegin failed\n"; 
414}
415
416void test_weighted_iterator(test::Suite& suite)
417{
418  std::vector<double> vec(3,1);
419  typedef std::vector<double>::iterator Iter;
420  typedef utility::WeightedIterator<Iter, Iter> WIter;
421  WIter iter(vec.begin(), vec.begin());
422
423  utility::DataWeight tmp = *iter;
424
425  test_bidirectional_iterator(iter, suite);
426  test_random_access_iterator(iter, suite);
427  std::vector<double> data(vec.size());
428  std::vector<double> weight(vec.size());
429  WIter iter2(data.begin(), weight.begin());
430  utility::DataWeight tmp2(6.89, 0.79);
431  *iter2 = tmp2;
432  if (!suite.add(tmp2==*iter2))
433    suite.err() << "error: tmp2==*iter2" << std::endl;
434  utility::DataWeight tmp3(*iter2);
435  suite.add(suite.equal(tmp3.data(), tmp2.data()));
436  suite.add(suite.equal(tmp3.weight(), tmp2.weight()));
437  *iter2 = *iter;
438  if (!suite.add(*iter2 == *iter))
439    suite.err() << "error: *iter2 == *iter\n";
440  tmp = *iter2;
441  suite.add(suite.equal(tmp.data(), 1));
442  std::copy(iter, iter+3, iter2);
443  suite.add(suite.equal(vec.front(), 1));
444  suite.add(suite.equal(data.front(), 1));
445
446  suite.err() << "  testing const conversion\n";
447  typedef std::vector<double>::const_iterator const_Iter;
448  typedef utility::WeightedIterator<const_Iter, const_Iter> const_WIter;
449  const_WIter const_iter(vec.begin(), vec.begin());
450  const_iter = iter;
451 
452  suite.err() << "  testing assignment between different iterators\n";
453  const std::vector<double> const_vec(10, 10.7);
454  const_WIter const_iter2(const_vec.begin(), const_vec.begin());
455  utility::DataWeight tmp4;
456  tmp4 = *const_iter2;
457  *iter = *const_iter2;
458  suite.add(*iter==*const_iter2);
459  *iter = *const_iter;
460  suite.add(*iter==*const_iter);
461
462  double x=101;
463  utility::WeightedIterator<double*, double*> iter_p(&x, &x);
464  *iter_p = *iter;
465  suite.add(*iter_p==*iter); 
466}
Note: See TracBrowser for help on using the repository browser.