source: trunk/test/data_lookup_1d_test.cc @ 1120

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

vector is now Vector

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1// $Id: data_lookup_1d_test.cc 1120 2008-02-21 23:18:41Z 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
37using namespace theplu::yat;
38
39utility::matrix matrix(size_t n);
40int end_test(std::ostream*, bool);
41
42int 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,index_odd,index_even);
68  *error << gsl_m1 << std::endl << '\n';
69  *error << m1 << std::endl;
70  *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
71         << "                           const size_t, const bool)...";
72  classifier::DataLookup1D v1(m1,1, true);
73  if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) || 
74      v1(1)!=m1(1,1) ) {
75    ok =false;
76    *error << "\nERROR" << std::endl;
77    *error << "size: " << v1.size() << " expected " << m1.columns() << "\n" 
78           << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n"
79           << "v1(1): " << v1(1) << " expected " << m1(1,1) 
80           << std::endl;
81  }
82  else
83    *error << "Ok" << std::endl;
84
85  *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
86         << "                           const size_t, const bool false)...";
87  classifier::DataLookup1D v2(m1,1,false);
88  if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) {
89    ok =false;
90    *error << "\nERROR\n"
91           << "size: " << v2.size() << " expected " << m1.rows() << "\n" 
92           << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n"
93           << "v2(1): " << v2(1) << " expected " << m1(1,1) 
94           << std::endl;
95  }
96  else
97    *error << "Ok" << std::endl;
98
99  *error << "DataLookup1D::DataLookup1D(const DataLookup1D&)...";
100  classifier::DataLookup1D v3(v2);
101  if (v3.size()!=v2.size() || v3(0)!=v2(0) || v3(1)!=v2(1) ) {
102    ok =false;
103    *error << "\nERROR\n"
104           << "size: " << v3.size() << " expected " << v2.size() << "\n" 
105           << "v2(0): " << v3(0) << " expected " << v2(0) << "\n"
106           << "v2(1): " << v3(1) << " expected " <<  v2(1)
107           << std::endl;
108  }
109  else
110    *error << "Ok" << std::endl;
111
112  *error << "DataLookup1D::DataLookup1D(const size_t, const double)...";
113  classifier::DataLookup1D v4(12,3);
114  if (v4.size()!=12 || v4(10)!=3 ) {
115    ok =false;
116    *error << "\nERROR\n"
117           << "size: " << v4.size() << " expected " << 12 << "\n" 
118           << "v2(0): " << v4(10) << " expected " << 3 
119           << std::endl;
120  }
121  else
122    *error << "Ok" << std::endl;
123
124  *error << "Testing that output from ostream operator for DataLookup1D"
125         << " can be used by the utility::vector istream constructor...";
126
127  // First with a vector with no missing values separated by ' '.
128  std::ofstream my_out("data/tmp_test_datalookup1D.txt");
129  my_out << v1;
130  my_out.close();
131  std::ifstream is("data/tmp_test_datalookup1D.txt");
132  utility::Vector v5(is);
133  is.close();
134  if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) ||
135      v5(2)!=v1(2)) {
136    ok=false;
137    *error << "\nERROR\n" << std::endl;
138  }
139  std::remove("data/tmp_test_datalookup1D.txt");
140 
141  // Second with a vector with a missing value separated by '\t'.
142  gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN();
143  classifier::DataLookup1D v6(m1,1, true); 
144  my_out.open("data/tmp_test_datalookup1D.txt");
145  char prev=my_out.fill('\t');
146  my_out << v1;
147  my_out.fill(prev);
148  my_out.close();
149  is.open("data/tmp_test_datalookup1D.txt");
150  utility::Vector v7(is,'\t');
151  is.close();
152  if (v7.size()!=v6.size() || !std::isnan(v7(1))) {
153    ok=false;
154    *error << "\nERROR\n"
155           << "size: " << v7.size() << " expected " << v6.size() << "\n" 
156           << "v7(1): " << v7(1) << " expected nan\n" 
157           << std::endl;
158  }
159  std::remove("data/tmp_test_datalookup1D.txt");
160  *error << "\n";
161
162  DataLookup1D dl(v5);
163  utility::Vector v8;
164  classifier::convert(dl, v8);
165  if (!v5.equal(v8,0.0)) {
166    ok = false;
167    *error << "Error: Creating a DataLookup1D(utility::vector)\n"
168           << "and classifier::convert(DataLookup, utility::vector)\n"
169           << "does not give back original vector\n"
170           << "orginal:      " << v5 << "\n"
171           << "DataLookup1D: " << dl << "\n"
172           << "final result: " << v8 << "\n"
173           << std::endl;
174  }
175
176  return end_test(error,ok);
177}
178
179utility::matrix matrix(size_t n)
180{
181  utility::matrix res(n,n);
182  for (size_t i=0;i<n;i++)
183    for (size_t j=0;j<n;j++)
184      res(i,j)=10*i+j;
185  return res;
186}
187
188int end_test(std::ostream* error, bool ok) 
189{ 
190  if (ok)
191    *error << "Ok." << std::endl;
192  if (error!=&std::cerr)
193    delete error;
194  return (ok ? 0 : -1);
195}
Note: See TracBrowser for help on using the repository browser.