source: trunk/test/iterator_test.cc @ 1365

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

working on ticket:369 - created a DataIterator? that seems to work. However, it can't work in a mutable fashion because of limitations in iterator_traits (see #391). Also there should be a WeightIterator? before ticket:369 can be closed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: iterator_test.cc 1365 2008-07-10 20:30:07Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Suite.h"
25
26#include "yat/classifier/DataLookup1D.h"
27
28#include "yat/classifier/DataLookupWeighted1D.h"
29#include "yat/classifier/MatrixLookupWeighted.h"
30#include "yat/utility/Container2DIterator.h"
31#include "yat/utility/DataIterator.h"
32#include "yat/utility/Matrix.h"
33#include "yat/utility/Vector.h"
34
35#include <algorithm>
36#include <fstream>
37
38#include <vector>
39
40using namespace theplu::yat;
41
42int main(int argc, char* argv[])
43{ 
44  test::Suite suite(argc, argv);
45  suite.err() << "testing iterator" << std::endl;
46
47  suite.err() << "testing utility::vector::iterator" << std::endl;
48  utility::Vector vec(12);
49  classifier::DataLookup1D lookup(vec);
50  utility::Vector::iterator begin=vec.begin();
51  // test iterator to const_iterator conversion
52  utility::Vector::const_iterator ci = vec.begin();
53  ci = begin;
54  if (begin!=ci)
55    suite.add(false);
56
57  utility::Vector::iterator end=vec.end();
58  std::sort(begin, end);
59
60  suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl;
61  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
62  classifier::DataLookup1D::const_iterator lend=lookup.end();
63 
64  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
65  std::copy(lbegin, lend, begin);
66  suite.err() << "copy from Vector to Vector" << std::endl;
67  std::copy(begin, end, begin);
68  suite.err() << "sort Vector" << std::endl;
69  std::sort(begin, end);
70
71  // test std algorithm on IteratorWeighted
72  utility::Matrix m(1,3,1);
73  m(0,1)=2.0;
74  utility::Matrix w(1,3,1);
75  classifier::MatrixLookupWeighted mw(m,w);
76  classifier::DataLookupWeighted1D aw(mw,0,true);
77  size_t nof1=std::count(aw.begin(),aw.end(),
78                         std::pair<double, double>(1.0, 1.0));
79  if(nof1!=2) {
80    suite.err() << "std algoritm with IteratorWeighted failed" << std::endl;
81    suite.add(false);
82  }
83  if (aw.begin()!=mw.begin_row(0))
84    suite.add(false);
85  if (aw.end()!=mw.end_row(0))
86    suite.add(false);
87  classifier::DataLookupWeighted1D aw2(mw,0,false);
88  if (aw2.begin()!=mw.begin_column(0))
89    suite.add(false);
90  if (aw2.end()!=mw.end_column(0))
91    suite.add(false);
92
93  utility::DataIterator<classifier::DataLookupWeighted1D::const_iterator> 
94    data_iter(aw2.begin());
95  suite.add(*data_iter == 1.0);
96  suite.add(*make_data_iterator(aw2.begin()) == 1.0);
97  std::vector<double> stl_vec(1,0);
98  utility::DataIterator<std::vector<double>::iterator> 
99    data_iter2(stl_vec.begin());
100  suite.add(*data_iter2 == 0.0);
101
102  classifier::MatrixLookup ml(m);
103  classifier::DataLookup1D dl1(ml,0,true);
104  if (dl1.begin()!=ml.begin_row(0))
105    suite.add(false);
106  if (dl1.end()!=ml.end_row(0))
107    suite.add(false);
108  classifier::DataLookup1D dl2(ml,0,false);
109  if (dl2.begin()!=ml.begin_column(0))
110    suite.add(false);
111  if (dl2.end()!=ml.end_column(0))
112    suite.add(false);
113
114  return suite.return_value();
115}
Note: See TracBrowser for help on using the repository browser.