source: trunk/test/iterator_test.cc @ 1376

Last change on this file since 1376 was 1376, checked in by Peter, 13 years ago

add WeightIterator? - fixes #369

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1// $Id: iterator_test.cc 1376 2008-07-16 19:32:20Z 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#include "yat/utility/WeightIterator.h"
35
36#include <algorithm>
37#include <fstream>
38
39#include <vector>
40
41using namespace theplu::yat;
42
43int main(int argc, char* argv[])
44{ 
45  test::Suite suite(argc, argv);
46  suite.err() << "testing iterator" << std::endl;
47
48  suite.err() << "testing utility::vector::iterator" << std::endl;
49  utility::Vector vec(12);
50  classifier::DataLookup1D lookup(vec);
51  utility::Vector::iterator begin=vec.begin();
52  // test iterator to const_iterator conversion
53  utility::Vector::const_iterator ci = vec.begin();
54  ci = begin;
55  if (begin!=ci)
56    suite.add(false);
57
58  utility::Vector::iterator end=vec.end();
59  std::sort(begin, end);
60
61  suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl;
62  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
63  classifier::DataLookup1D::const_iterator lend=lookup.end();
64 
65  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
66  std::copy(lbegin, lend, begin);
67  suite.err() << "copy from Vector to Vector" << std::endl;
68  std::copy(begin, end, begin);
69  suite.err() << "sort Vector" << std::endl;
70  std::sort(begin, end);
71
72  // test std algorithm on IteratorWeighted
73  utility::Matrix m(1,3,1);
74  m(0,1)=2.0;
75  utility::Matrix w(1,3,1);
76  classifier::MatrixLookupWeighted mw(m,w);
77  classifier::DataLookupWeighted1D aw(mw,0,true);
78  size_t nof1=std::count(aw.begin(),aw.end(),
79                         std::pair<double, double>(1.0, 1.0));
80  if(nof1!=2) {
81    suite.err() << "std algoritm with IteratorWeighted failed" << std::endl;
82    suite.add(false);
83  }
84  if (aw.begin()!=mw.begin_row(0))
85    suite.add(false);
86  if (aw.end()!=mw.end_row(0))
87    suite.add(false);
88  classifier::DataLookupWeighted1D aw2(mw,0,false);
89  if (aw2.begin()!=mw.begin_column(0))
90    suite.add(false);
91  if (aw2.end()!=mw.end_column(0))
92    suite.add(false);
93
94  utility::DataIterator<classifier::DataLookupWeighted1D::const_iterator> 
95    data_iter(aw2.begin());
96  suite.add(*data_iter == 1.0);
97  suite.add(*make_data_iterator(aw2.begin()) == 1.0);
98  std::vector<double> stl_vec(1,0);
99  utility::DataIterator<std::vector<double>::iterator> 
100    data_iter2(stl_vec.begin());
101  suite.add(*data_iter2 == 0.0);
102  *data_iter2 = 3.14;
103  suite.add(*data_iter2 == 3.14);
104  utility::WeightIterator<std::vector<double>::iterator> 
105    data_iter3(stl_vec.begin());
106  suite.add(*data_iter3 == 1.0);
107
108  classifier::MatrixLookup ml(m);
109  classifier::DataLookup1D dl1(ml,0,true);
110  if (dl1.begin()!=ml.begin_row(0))
111    suite.add(false);
112  if (dl1.end()!=ml.end_row(0))
113    suite.add(false);
114  classifier::DataLookup1D dl2(ml,0,false);
115  if (dl2.begin()!=ml.begin_column(0))
116    suite.add(false);
117  if (dl2.end()!=ml.end_column(0))
118    suite.add(false);
119
120  return suite.return_value();
121}
Note: See TracBrowser for help on using the repository browser.