source: trunk/test/utility_test.cc @ 1385

Last change on this file since 1385 was 1385, checked in by Peter, 13 years ago

adding functions inverse

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1// $Id: utility_test.cc 1385 2008-07-22 17:32:53Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, 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://trac.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 2 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 this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Suite.h"
27
28#include "yat/utility/Matrix.h"
29#include "yat/utility/utility.h"
30#include "yat/utility/stl_utility.h"
31
32#include <fstream>
33#include <iostream>
34#include <map>
35#include <string>
36#include <vector>
37
38using namespace theplu::yat;
39void test_quantile_normalize(test::Suite&);
40void test_inverse(test::Suite&);
41
42template<typename InputIterator, typename Key>
43void test_inverse_validate(InputIterator first, InputIterator last,
44                           const std::map<Key, std::vector<size_t> >& m,
45                           test::Suite& suite);
46template<typename InputIterator, typename Key>
47void test_inverse_validate(InputIterator first,
48                           const std::multimap<Key, size_t>& m,
49                           test::Suite& suite);
50
51int main(int argc, char* argv[])
52{ 
53  using namespace theplu::yat;
54  test::Suite suite(argc, argv);
55  suite.err() << "testing utility ... " << std::endl;
56
57  // test float/double
58  std::string s("1.2");
59  if (!utility::is_double(s)){
60    suite.add(false);
61  }
62  else if (!utility::is_float(s)) {
63    suite.add(false);
64  }
65  else if (utility::is_int(s)) {
66    suite.add(false);
67  }
68  else if (utility::is_nan(s)) {
69    suite.add(false);
70  }
71
72  // test int
73  s="23";
74  if (!utility::is_double(s)){
75    suite.add(false);
76  }
77  else if (!utility::is_float(s)) {
78    suite.add(false);
79  }
80  else if (!utility::is_int(s)) {
81    suite.add(false);
82  }
83  else if (utility::is_nan(s)) {
84    suite.add(false);
85  }
86
87  // test nan
88  s=" nAn  ";
89  if (!utility::is_double(s)){
90    suite.add(false);
91    suite.err() << "error: " << s << " is a double\n";
92  }
93  /* we don't require NaN for float and int
94  else if (!utility::is_float(s)) {
95    suite.add(false);
96    suite.err() << "error: " << s << " is a float\n";
97  }
98  else if (!utility::is_int(s)) {
99    suite.add(false);
100    suite.err() << "error: " << s << " is a int\n";
101  }
102  */
103  else if (!utility::is_nan(s)) {
104    suite.add(false);
105    suite.err() << "error: " << s << " is nan\n";
106
107  }
108 
109  // testing trailing values
110  s=" 23 23   ";
111  if (utility::is_double(s)){
112    suite.add(false);
113  }
114  else if (utility::is_float(s)) {
115    suite.add(false);
116  }
117  else if (utility::is_int(s)) {
118    suite.add(false);
119  }
120  else if (utility::is_nan(s)) {
121    suite.add(false);
122  }
123
124  if (utility::convert<double>("1.23")!=1.23)
125    suite.add(false);
126  utility::convert<double>("-inf");
127  utility::convert<double>("inf");
128  utility::convert<double>("NaN");
129
130  if (!suite.add(utility::is<double>("-inf")))
131    suite.err() << "is<double>(\"-inf\") should return true\n";
132  if (!suite.add(utility::is<double>("inf")))
133    suite.err() << "is<double>(\"inf\") should return true\n";
134  if (!suite.add(utility::is<double>("NaN")))
135    suite.err() << "is<double>(\"NaN\") should return true\n";
136  if (!suite.add(utility::is<double>("1.23")))
137    suite.err() << "is<double>(\"1.23\") should return true\n";
138  if (!suite.add(!utility::is<double>("1.23.2")))
139    suite.err() << "is<double>(\"1.23.2\") should return false\n";
140  if (!suite.add(!utility::is<int>("1.23")))
141    suite.err() << "is<int>(\"1.23\") should return false\n";
142  if (!suite.add(!utility::is<int>("")))
143    suite.err() << "is<int>(\"\") should return false\n";
144
145  {
146    utility::Log<long double> f;
147    utility::Log<double> f2(1.3);
148    f(2.0);
149    utility::Exp<double> e;
150    e(3.2);
151  }
152
153  test_quantile_normalize(suite);
154  test_inverse(suite);
155
156  return suite.return_value();
157}
158
159
160void test_inverse(test::Suite& suite)
161{
162  suite.err() << "Testing inverse\n";
163  std::vector<std::string> vec;
164  vec.push_back("No");
165  vec.push_back("one");
166  vec.push_back("shall");
167  vec.push_back("be");
168  vec.push_back("subjected");
169  std::map<std::string, std::vector<size_t> > map;
170  utility::inverse(vec.begin(), vec.end(), map);
171  test_inverse_validate(vec.begin(), vec.end(), map, suite);
172  utility::inverse(vec.begin()+1, vec.end(), map);
173  test_inverse_validate(vec.begin()+1, vec.end(), map, suite);
174  const std::vector<std::string> vec2(vec);
175  utility::inverse(vec2.begin(), vec2.end(), map);
176  test_inverse_validate(vec2.begin(), vec2.end(), map, suite);
177  std::multimap<std::string, size_t> mmap;
178  utility::inverse(vec.begin(), vec.end(), mmap);
179  test_inverse_validate(vec.begin(), mmap, suite);
180  utility::inverse(vec.begin()+1, vec.end(), mmap);
181  test_inverse_validate(vec.begin()+1, mmap, suite);
182}
183
184
185template<typename InputIterator, typename Key>
186void test_inverse_validate(InputIterator first, InputIterator last,
187                           const std::map<Key, std::vector<size_t> >& m,
188                           test::Suite& suite)
189{
190  typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter;
191  for ( InputIterator iter=first; iter != last; ++iter) {
192    Iter map_iter = m.find(*iter);
193    if (!suite.add(map_iter!=m.end())) {
194      suite.err() << "test_inverse_validate() failed\n";
195      suite.err() << "  could not find: " << *first << " in map m\n";
196    }
197    for (size_t i=0; i<map_iter->second.size(); ++i)
198      if (!suite.add(map_iter->second[i] ==
199                     static_cast<size_t>(distance(first, iter))) ) {
200        suite.err() << "test_inverse_validate() failed\n";
201        suite.err() << "  comparing: " << map_iter->second[i] 
202                    << " and " << distance(first, iter) 
203                    << " expected them to be equal\n";
204      }
205  }
206}
207
208
209template<typename InputIterator, typename Key>
210void test_inverse_validate(InputIterator first,
211                           const std::multimap<Key, size_t>& m,
212                           test::Suite& suite)
213{
214  for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin();
215       iter!=m.end(); ++iter) {
216    suite.add(*(first + iter->second) == iter->first);
217  }
218 
219}
220
221
222void test_quantile_normalize(test::Suite& suite)
223{
224  suite.err() << "Testing quantile normalization\n";
225 
226  utility::Matrix m(2,2);
227  m(0,0) = 0;
228  m(0,1) = 10;
229  m(1,0) = 2;
230  m(1,1) = 4;
231  utility::quantile_normalize(m);
232  suite.err() << "Testing m(0,0)\n";
233  suite.add(suite.equal(m(0,0), 2));
234  suite.err() << "Testing m(0,1)\n";
235  suite.add(suite.equal(m(0,1), 6));
236  suite.err() << "Testing m(1,0)\n";
237  suite.add(suite.equal(m(1,0), 6));
238  suite.err() << "Testing m(1,1)\n";
239  suite.add(suite.equal(m(1,1), 2));
240
241}
Note: See TracBrowser for help on using the repository browser.