source: trunk/test/iterator_test.cc @ 1146

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

DataLookup1D is now a lookup into a MatrixLookup? closes #327

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: iterator_test.cc 1146 2008-02-25 21:03:48Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
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 "yat/classifier/DataLookup1D.h"
25
26#include "yat/classifier/DataLookupWeighted1D.h"
27#include "yat/classifier/MatrixLookupWeighted.h"
28#include "yat/utility/Container2DIterator.h"
29#include "yat/utility/Matrix.h"
30#include "yat/utility/Vector.h"
31
32#include <algorithm>
33#include <fstream>
34
35using namespace theplu::yat;
36
37int main(const int argc,const char* argv[])
38{ 
39  std::ostream* message;
40  if (argc>1 && argv[1]==std::string("-v"))
41    message = &std::cerr;
42  else {
43    message = new std::ofstream("/dev/null");
44    if (argc>1)
45      std::cout << "iterator_test -v : for printing extra "  << "information\n";
46  }
47  *message << "testing iterator" << std::endl;
48  bool ok = true;
49
50  *message << "testing utility::vector::iterator" << std::endl;
51  utility::Vector vec(12);
52  classifier::DataLookup1D lookup(vec);
53  utility::Vector::iterator begin=vec.begin();
54  // test iterator to const_iterator conversion
55  utility::Vector::const_iterator ci = vec.begin();
56  ci = begin;
57  if (begin!=ci)
58    ok = false;
59
60  utility::Vector::iterator end=vec.end();
61  std::sort(begin, end);
62
63  *message << "testing classifier::DataLookup1D::const_iterator" << std::endl;
64  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
65  classifier::DataLookup1D::const_iterator lend=lookup.end();
66 
67  *message << "copy from DataLookup1D to Vector" << std::endl;
68  std::copy(lbegin, lend, begin);
69  *message << "copy from Vector to Vector" << std::endl;
70  std::copy(begin, end, begin);
71  *message << "sort Vector" << std::endl;
72  std::sort(begin, end);
73
74  // test std algorithm on IteratorWeighted
75  utility::Matrix m(1,3,1);
76  m(0,1)=2.0;
77  utility::Matrix w(1,3,1);
78  classifier::MatrixLookupWeighted mw(m,w);
79  classifier::DataLookupWeighted1D aw(mw,0,true);
80  size_t nof1=std::count(aw.begin(),aw.end(),
81                         std::pair<double, double>(1.0, 1.0));
82  if(nof1!=2) {
83    *message << "std algoritm with IteratorWeighted failed" << std::endl;
84    ok=false;
85  }
86  if (aw.begin()!=mw.begin_row(0))
87    ok = false;
88  if (aw.end()!=mw.end_row(0))
89    ok = false;
90  classifier::DataLookupWeighted1D aw2(mw,0,false);
91  if (aw2.begin()!=mw.begin_column(0))
92    ok = false;
93  if (aw2.end()!=mw.end_column(0))
94    ok = false;
95
96  classifier::MatrixLookup ml(m);
97  classifier::DataLookup1D dl1(ml,0,true);
98  if (dl1.begin()!=ml.begin_row(0))
99    ok = false;
100  if (dl1.end()!=ml.end_row(0))
101    ok = false;
102  classifier::DataLookup1D dl2(ml,0,false);
103  if (dl2.begin()!=ml.begin_column(0))
104    ok = false;
105  if (dl2.end()!=ml.end_column(0))
106    ok = false;
107
108  if (!ok)
109    *message << "iterator test failed" << std::endl;
110 
111  return (ok ? 0 : -1);
112}
Note: See TracBrowser for help on using the repository browser.