source: trunk/test/iterator_test.cc @ 1382

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

fixing problems with MatrixWeighted? iterators

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