1 | // $Id: data_lookup_1d_test.cc 1134 2008-02-23 22:52:43Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "yat/utility/Matrix.h" |
---|
26 | #include "yat/classifier/DataLookup1D.h" |
---|
27 | #include "yat/classifier/MatrixLookup.h" |
---|
28 | #include "yat/classifier/utility.h" |
---|
29 | |
---|
30 | |
---|
31 | #include <cstdio> |
---|
32 | #include <fstream> |
---|
33 | #include <iostream> |
---|
34 | #include <vector> |
---|
35 | #include <cmath> |
---|
36 | |
---|
37 | using namespace theplu::yat; |
---|
38 | |
---|
39 | utility::Matrix matrix(size_t n); |
---|
40 | int end_test(std::ostream*, bool); |
---|
41 | |
---|
42 | int main(const int argc,const char* argv[]) |
---|
43 | { |
---|
44 | using namespace theplu::yat::classifier; |
---|
45 | |
---|
46 | std::ostream* error; |
---|
47 | if (argc>1 && argv[1]==std::string("-v")) |
---|
48 | error = &std::cerr; |
---|
49 | else { |
---|
50 | error = new std::ofstream("/dev/null"); |
---|
51 | if (argc>1) |
---|
52 | std::cout << "lookup_test -v : for printing extra information\n"; |
---|
53 | } |
---|
54 | |
---|
55 | *error << "Testing Lookup Classes" << std::endl; |
---|
56 | bool ok = true; |
---|
57 | |
---|
58 | *error << "Testing DataLookup1D" << std::endl; |
---|
59 | utility::Matrix gsl_m1(matrix(5)); |
---|
60 | std::vector<size_t> index_odd; |
---|
61 | index_odd.push_back(1); |
---|
62 | index_odd.push_back(3); |
---|
63 | std::vector<size_t> index_even; |
---|
64 | index_even.push_back(2); |
---|
65 | index_even.push_back(0); |
---|
66 | index_even.push_back(4); |
---|
67 | classifier::MatrixLookup m1(gsl_m1,utility::Index(index_odd), |
---|
68 | utility::Index(index_even)); |
---|
69 | *error << gsl_m1 << std::endl << '\n'; |
---|
70 | *error << m1 << std::endl; |
---|
71 | *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
---|
72 | << " const size_t, const bool)..."; |
---|
73 | classifier::DataLookup1D v1(m1,1, true); |
---|
74 | if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) || |
---|
75 | v1(1)!=m1(1,1) ) { |
---|
76 | ok =false; |
---|
77 | *error << "\nERROR" << std::endl; |
---|
78 | *error << "size: " << v1.size() << " expected " << m1.columns() << "\n" |
---|
79 | << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n" |
---|
80 | << "v1(1): " << v1(1) << " expected " << m1(1,1) |
---|
81 | << std::endl; |
---|
82 | } |
---|
83 | else |
---|
84 | *error << "Ok" << std::endl; |
---|
85 | |
---|
86 | *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
---|
87 | << " const size_t, const bool false)..."; |
---|
88 | classifier::DataLookup1D v2(m1,1,false); |
---|
89 | if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) { |
---|
90 | ok =false; |
---|
91 | *error << "\nERROR\n" |
---|
92 | << "size: " << v2.size() << " expected " << m1.rows() << "\n" |
---|
93 | << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n" |
---|
94 | << "v2(1): " << v2(1) << " expected " << m1(1,1) |
---|
95 | << std::endl; |
---|
96 | } |
---|
97 | else |
---|
98 | *error << "Ok" << std::endl; |
---|
99 | |
---|
100 | *error << "DataLookup1D::DataLookup1D(const DataLookup1D&)..."; |
---|
101 | classifier::DataLookup1D v3(v2); |
---|
102 | if (v3.size()!=v2.size() || v3(0)!=v2(0) || v3(1)!=v2(1) ) { |
---|
103 | ok =false; |
---|
104 | *error << "\nERROR\n" |
---|
105 | << "size: " << v3.size() << " expected " << v2.size() << "\n" |
---|
106 | << "v2(0): " << v3(0) << " expected " << v2(0) << "\n" |
---|
107 | << "v2(1): " << v3(1) << " expected " << v2(1) |
---|
108 | << std::endl; |
---|
109 | } |
---|
110 | else |
---|
111 | *error << "Ok" << std::endl; |
---|
112 | |
---|
113 | *error << "DataLookup1D::DataLookup1D(const size_t, const double)..."; |
---|
114 | classifier::DataLookup1D v4(12,3); |
---|
115 | if (v4.size()!=12 || v4(10)!=3 ) { |
---|
116 | ok =false; |
---|
117 | *error << "\nERROR\n" |
---|
118 | << "size: " << v4.size() << " expected " << 12 << "\n" |
---|
119 | << "v2(0): " << v4(10) << " expected " << 3 |
---|
120 | << std::endl; |
---|
121 | } |
---|
122 | else |
---|
123 | *error << "Ok" << std::endl; |
---|
124 | |
---|
125 | *error << "Testing that output from ostream operator for DataLookup1D" |
---|
126 | << " can be used by the utility::vector istream constructor..."; |
---|
127 | |
---|
128 | // First with a vector with no missing values separated by ' '. |
---|
129 | std::ofstream my_out("data/tmp_test_datalookup1D.txt"); |
---|
130 | my_out << v1; |
---|
131 | my_out.close(); |
---|
132 | std::ifstream is("data/tmp_test_datalookup1D.txt"); |
---|
133 | utility::Vector v5(is); |
---|
134 | is.close(); |
---|
135 | if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) || |
---|
136 | v5(2)!=v1(2)) { |
---|
137 | ok=false; |
---|
138 | *error << "\nERROR\n" << std::endl; |
---|
139 | } |
---|
140 | std::remove("data/tmp_test_datalookup1D.txt"); |
---|
141 | |
---|
142 | // Second with a vector with a missing value separated by '\t'. |
---|
143 | gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN(); |
---|
144 | classifier::DataLookup1D v6(m1,1, true); |
---|
145 | my_out.open("data/tmp_test_datalookup1D.txt"); |
---|
146 | char prev=my_out.fill('\t'); |
---|
147 | my_out << v1; |
---|
148 | my_out.fill(prev); |
---|
149 | my_out.close(); |
---|
150 | is.open("data/tmp_test_datalookup1D.txt"); |
---|
151 | utility::Vector v7(is,'\t'); |
---|
152 | is.close(); |
---|
153 | if (v7.size()!=v6.size() || !std::isnan(v7(1))) { |
---|
154 | ok=false; |
---|
155 | *error << "\nERROR\n" |
---|
156 | << "size: " << v7.size() << " expected " << v6.size() << "\n" |
---|
157 | << "v7(1): " << v7(1) << " expected nan\n" |
---|
158 | << std::endl; |
---|
159 | } |
---|
160 | std::remove("data/tmp_test_datalookup1D.txt"); |
---|
161 | *error << "\n"; |
---|
162 | |
---|
163 | DataLookup1D dl(v5); |
---|
164 | utility::Vector v8; |
---|
165 | classifier::convert(dl, v8); |
---|
166 | if (!v5.equal(v8,0.0)) { |
---|
167 | ok = false; |
---|
168 | *error << "Error: Creating a DataLookup1D(utility::vector)\n" |
---|
169 | << "and classifier::convert(DataLookup, utility::vector)\n" |
---|
170 | << "does not give back original vector\n" |
---|
171 | << "orginal: " << v5 << "\n" |
---|
172 | << "DataLookup1D: " << dl << "\n" |
---|
173 | << "final result: " << v8 << "\n" |
---|
174 | << std::endl; |
---|
175 | } |
---|
176 | |
---|
177 | return end_test(error,ok); |
---|
178 | } |
---|
179 | |
---|
180 | utility::Matrix matrix(size_t n) |
---|
181 | { |
---|
182 | utility::Matrix res(n,n); |
---|
183 | for (size_t i=0;i<n;i++) |
---|
184 | for (size_t j=0;j<n;j++) |
---|
185 | res(i,j)=10*i+j; |
---|
186 | return res; |
---|
187 | } |
---|
188 | |
---|
189 | int end_test(std::ostream* error, bool ok) |
---|
190 | { |
---|
191 | if (ok) |
---|
192 | *error << "Ok." << std::endl; |
---|
193 | if (error!=&std::cerr) |
---|
194 | delete error; |
---|
195 | return (ok ? 0 : -1); |
---|
196 | } |
---|