source: trunk/test/data_lookup_1d_test.cc @ 1658

Last change on this file since 1658 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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