1 | // $Id: iterator_test.cc 898 2007-09-26 13:44:19Z markus $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.thep.lu.se/trac/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/Iterator.h" |
---|
29 | #include "yat/utility/IteratorWeighted.h" |
---|
30 | #include "yat/utility/matrix.h" |
---|
31 | #include "yat/utility/vector.h" |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <fstream> |
---|
35 | |
---|
36 | using namespace theplu::yat; |
---|
37 | |
---|
38 | int main(const int argc,const char* argv[]) |
---|
39 | { |
---|
40 | std::ostream* message; |
---|
41 | if (argc>1 && argv[1]==std::string("-v")) |
---|
42 | message = &std::cerr; |
---|
43 | else { |
---|
44 | message = new std::ofstream("/dev/null"); |
---|
45 | if (argc>1) |
---|
46 | std::cout << "iterator_test -v : for printing extra " << "information\n"; |
---|
47 | } |
---|
48 | *message << "testing iterator" << std::endl; |
---|
49 | bool ok = true; |
---|
50 | |
---|
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 | classifier::DataLookup1D::const_iterator lbegin=lookup.begin(); |
---|
63 | classifier::DataLookup1D::const_iterator lend=lookup.end(); |
---|
64 | |
---|
65 | std::copy(lbegin, lend, begin); |
---|
66 | std::copy(begin, end, begin); |
---|
67 | std::sort(begin, end); |
---|
68 | |
---|
69 | // test std algorithm on IteratorWeighted |
---|
70 | utility::matrix m(1,3,1); |
---|
71 | m(0,1)=2.0; |
---|
72 | utility::matrix w(1,3,1); |
---|
73 | classifier::MatrixLookupWeighted mw(m,w); |
---|
74 | classifier::DataLookupWeighted1D aw(mw,0,true); |
---|
75 | size_t nof1=std::count(aw.begin(),aw.end(),1.0); |
---|
76 | if(nof1!=2) { |
---|
77 | *message << "std algoritm with IteratorWeighted failed" << std::endl; |
---|
78 | ok=false; |
---|
79 | } |
---|
80 | |
---|
81 | if (!ok) |
---|
82 | *message << "iterator test failed" << std::endl; |
---|
83 | |
---|
84 | return (ok ? 0 : -1); |
---|
85 | } |
---|