1 | // $Id: data_lookup_1d_test.cc 865 2007-09-10 19:41:04Z 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/trac/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 | |
---|
56 | *error << "Testing Lookup Classes" << std::endl; |
---|
57 | bool ok = true; |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | if(!std::numeric_limits<double>::has_quiet_NaN) { |
---|
62 | *error << "Error: a compiler without quiet_NaN is used!\n" |
---|
63 | << "This will likely also mess up a lot of other yat tests\n"; |
---|
64 | ok=false; |
---|
65 | return end_test(error,ok); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | *error << "Testing DataLookup1D" << std::endl; |
---|
71 | utility::matrix gsl_m1(matrix(5)); |
---|
72 | std::vector<size_t> index_odd; |
---|
73 | index_odd.push_back(1); |
---|
74 | index_odd.push_back(3); |
---|
75 | std::vector<size_t> index_even; |
---|
76 | index_even.push_back(2); |
---|
77 | index_even.push_back(0); |
---|
78 | index_even.push_back(4); |
---|
79 | classifier::MatrixLookup m1(gsl_m1,index_odd,index_even); |
---|
80 | *error << gsl_m1 << std::endl << '\n'; |
---|
81 | *error << m1 << std::endl; |
---|
82 | *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
---|
83 | << " const size_t, const bool)..."; |
---|
84 | classifier::DataLookup1D v1(m1,1, true); |
---|
85 | if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) || |
---|
86 | v1(1)!=m1(1,1) ) { |
---|
87 | ok =false; |
---|
88 | *error << "\nERROR" << std::endl; |
---|
89 | *error << "size: " << v1.size() << " expected " << m1.columns() << "\n" |
---|
90 | << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n" |
---|
91 | << "v1(1): " << v1(1) << " expected " << m1(1,1) |
---|
92 | << std::endl; |
---|
93 | } |
---|
94 | else |
---|
95 | *error << "Ok" << std::endl; |
---|
96 | |
---|
97 | *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
---|
98 | << " const size_t, const bool false)..."; |
---|
99 | classifier::DataLookup1D v2(m1,1,false); |
---|
100 | if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) { |
---|
101 | ok =false; |
---|
102 | *error << "\nERROR\n" |
---|
103 | << "size: " << v2.size() << " expected " << m1.rows() << "\n" |
---|
104 | << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n" |
---|
105 | << "v2(1): " << v2(1) << " expected " << m1(1,1) |
---|
106 | << std::endl; |
---|
107 | } |
---|
108 | else |
---|
109 | *error << "Ok" << std::endl; |
---|
110 | |
---|
111 | *error << "DataLookup1D::DataLookup1D(const DataLookup1D&)..."; |
---|
112 | classifier::DataLookup1D v3(v2); |
---|
113 | if (v3.size()!=v2.size() || v3(0)!=v2(0) || v3(1)!=v2(1) ) { |
---|
114 | ok =false; |
---|
115 | *error << "\nERROR\n" |
---|
116 | << "size: " << v3.size() << " expected " << v2.size() << "\n" |
---|
117 | << "v2(0): " << v3(0) << " expected " << v2(0) << "\n" |
---|
118 | << "v2(1): " << v3(1) << " expected " << v2(1) |
---|
119 | << std::endl; |
---|
120 | } |
---|
121 | else |
---|
122 | *error << "Ok" << std::endl; |
---|
123 | |
---|
124 | *error << "DataLookup1D::DataLookup1D(const size_t, const double)..."; |
---|
125 | classifier::DataLookup1D v4(12,3); |
---|
126 | if (v4.size()!=12 || v4(10)!=3 ) { |
---|
127 | ok =false; |
---|
128 | *error << "\nERROR\n" |
---|
129 | << "size: " << v4.size() << " expected " << 12 << "\n" |
---|
130 | << "v2(0): " << v4(10) << " expected " << 3 |
---|
131 | << std::endl; |
---|
132 | } |
---|
133 | else |
---|
134 | *error << "Ok" << std::endl; |
---|
135 | |
---|
136 | *error << "Testing that output from ostream operator for DataLookup1D" |
---|
137 | << " can be used by the utility::vector istream constructor..."; |
---|
138 | |
---|
139 | // First with a vector with no missing values separated by ' '. |
---|
140 | std::ofstream my_out("data/tmp_test_datalookup1D.txt"); |
---|
141 | my_out << v1; |
---|
142 | my_out.close(); |
---|
143 | std::ifstream is("data/tmp_test_datalookup1D.txt"); |
---|
144 | utility::vector v5(is); |
---|
145 | is.close(); |
---|
146 | if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) || |
---|
147 | v5(2)!=v1(2)) { |
---|
148 | ok=false; |
---|
149 | *error << "\nERROR\n" << std::endl; |
---|
150 | } |
---|
151 | std::remove("data/tmp_test_datalookup1D.txt"); |
---|
152 | |
---|
153 | // Second with a vector with a missing value separated by '\t'. |
---|
154 | gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN(); |
---|
155 | classifier::DataLookup1D v6(m1,1, true); |
---|
156 | my_out.open("data/tmp_test_datalookup1D.txt"); |
---|
157 | char prev=my_out.fill('\t'); |
---|
158 | my_out << v1; |
---|
159 | my_out.fill(prev); |
---|
160 | my_out.close(); |
---|
161 | is.open("data/tmp_test_datalookup1D.txt"); |
---|
162 | utility::vector v7(is,'\t'); |
---|
163 | is.close(); |
---|
164 | if (v7.size()!=v6.size() || !std::isnan(v7(1))) { |
---|
165 | ok=false; |
---|
166 | *error << "\nERROR\n" |
---|
167 | << "size: " << v7.size() << " expected " << v6.size() << "\n" |
---|
168 | << "v7(1): " << v7(1) << " expected nan\n" |
---|
169 | << std::endl; |
---|
170 | } |
---|
171 | std::remove("data/tmp_test_datalookup1D.txt"); |
---|
172 | *error << "\n"; |
---|
173 | |
---|
174 | DataLookup1D dl(v5); |
---|
175 | utility::vector v8; |
---|
176 | classifier::convert(dl, v8); |
---|
177 | if (!v5.equal(v8,0.0)) { |
---|
178 | ok = false; |
---|
179 | *error << "Error: Creating a DataLookup1D(utility::vector)\n" |
---|
180 | << "and classifier::convert(DataLookup, utility::vector)\n" |
---|
181 | << "does not give back original vector\n" |
---|
182 | << "orginal: " << v5 << "\n" |
---|
183 | << "DataLookup1D: " << dl << "\n" |
---|
184 | << "final result: " << v8 << "\n" |
---|
185 | << std::endl; |
---|
186 | } |
---|
187 | |
---|
188 | return end_test(error,ok); |
---|
189 | } |
---|
190 | |
---|
191 | utility::matrix matrix(size_t n) |
---|
192 | { |
---|
193 | utility::matrix res(n,n); |
---|
194 | for (size_t i=0;i<n;i++) |
---|
195 | for (size_t j=0;j<n;j++) |
---|
196 | res(i,j)=10*i+j; |
---|
197 | return res; |
---|
198 | } |
---|
199 | |
---|
200 | int end_test(std::ostream* error, bool ok) |
---|
201 | { |
---|
202 | if (ok) |
---|
203 | *error << "Ok." << std::endl; |
---|
204 | if (error!=&std::cerr) |
---|
205 | delete error; |
---|
206 | return (ok ? 0 : -1); |
---|
207 | } |
---|