source: trunk/test/utility_test.cc @ 1432

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

refs #416 moving quantile normalizer to new dir. Also moved the algorithm to a class (rather than free function).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: utility_test.cc 1432 2008-08-25 13:54:18Z 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_inverse(test::Suite&);
40
41template<typename InputIterator, typename Key>
42void test_inverse_validate(InputIterator first, InputIterator last,
43                           const std::map<Key, std::vector<size_t> >& m,
44                           test::Suite& suite);
45template<typename InputIterator, typename Key>
46void test_inverse_validate(InputIterator first,
47                           const std::multimap<Key, size_t>& m,
48                           test::Suite& suite);
49
50int main(int argc, char* argv[])
51{ 
52  using namespace theplu::yat;
53  test::Suite suite(argc, argv);
54  suite.err() << "testing utility ... " << std::endl;
55
56  // test float/double
57  std::string s("1.2");
58  if (!utility::is_double(s)){
59    suite.add(false);
60  }
61  else if (!utility::is_float(s)) {
62    suite.add(false);
63  }
64  else if (utility::is_int(s)) {
65    suite.add(false);
66  }
67  else if (utility::is_nan(s)) {
68    suite.add(false);
69  }
70
71  // test int
72  s="23";
73  if (!utility::is_double(s)){
74    suite.add(false);
75  }
76  else if (!utility::is_float(s)) {
77    suite.add(false);
78  }
79  else if (!utility::is_int(s)) {
80    suite.add(false);
81  }
82  else if (utility::is_nan(s)) {
83    suite.add(false);
84  }
85
86  // test nan
87  s=" nAn  ";
88  if (!utility::is_double(s)){
89    suite.add(false);
90    suite.err() << "error: " << s << " is a double\n";
91  }
92  /* we don't require NaN for float and int
93  else if (!utility::is_float(s)) {
94    suite.add(false);
95    suite.err() << "error: " << s << " is a float\n";
96  }
97  else if (!utility::is_int(s)) {
98    suite.add(false);
99    suite.err() << "error: " << s << " is a int\n";
100  }
101  */
102  else if (!utility::is_nan(s)) {
103    suite.add(false);
104    suite.err() << "error: " << s << " is nan\n";
105
106  }
107 
108  // testing trailing values
109  s=" 23 23   ";
110  if (utility::is_double(s)){
111    suite.add(false);
112  }
113  else if (utility::is_float(s)) {
114    suite.add(false);
115  }
116  else if (utility::is_int(s)) {
117    suite.add(false);
118  }
119  else if (utility::is_nan(s)) {
120    suite.add(false);
121  }
122
123  if (utility::convert<double>("1.23")!=1.23)
124    suite.add(false);
125  utility::convert<double>("-inf");
126  utility::convert<double>("inf");
127  utility::convert<double>("NaN");
128
129  if (!suite.add(utility::is<double>("-inf")))
130    suite.err() << "is<double>(\"-inf\") should return true\n";
131  if (!suite.add(utility::is<double>("inf")))
132    suite.err() << "is<double>(\"inf\") should return true\n";
133  if (!suite.add(utility::is<double>("NaN")))
134    suite.err() << "is<double>(\"NaN\") should return true\n";
135  if (!suite.add(utility::is<double>("1.23")))
136    suite.err() << "is<double>(\"1.23\") should return true\n";
137  if (!suite.add(!utility::is<double>("1.23.2")))
138    suite.err() << "is<double>(\"1.23.2\") should return false\n";
139  if (!suite.add(!utility::is<int>("1.23")))
140    suite.err() << "is<int>(\"1.23\") should return false\n";
141  if (!suite.add(!utility::is<int>("")))
142    suite.err() << "is<int>(\"\") should return false\n";
143
144  {
145    utility::Log<long double> f;
146    utility::Log<double> f2(1.3);
147    f(2.0);
148    utility::Exp<double> e;
149    e(3.2);
150  }
151
152  test_inverse(suite);
153
154  return suite.return_value();
155}
156
157
158void test_inverse(test::Suite& suite)
159{
160  suite.err() << "Testing inverse\n";
161  std::vector<std::string> vec;
162  vec.push_back("No");
163  vec.push_back("one");
164  vec.push_back("shall");
165  vec.push_back("be");
166  vec.push_back("subjected");
167  std::map<std::string, std::vector<size_t> > map;
168  utility::inverse(vec.begin(), vec.end(), map);
169  test_inverse_validate(vec.begin(), vec.end(), map, suite);
170  utility::inverse(vec.begin()+1, vec.end(), map);
171  test_inverse_validate(vec.begin()+1, vec.end(), map, suite);
172  const std::vector<std::string> vec2(vec);
173  utility::inverse(vec2.begin(), vec2.end(), map);
174  test_inverse_validate(vec2.begin(), vec2.end(), map, suite);
175  std::multimap<std::string, size_t> mmap;
176  utility::inverse(vec.begin(), vec.end(), mmap);
177  test_inverse_validate(vec.begin(), mmap, suite);
178  utility::inverse(vec.begin()+1, vec.end(), mmap);
179  test_inverse_validate(vec.begin()+1, mmap, suite);
180}
181
182
183template<typename InputIterator, typename Key>
184void test_inverse_validate(InputIterator first, InputIterator last,
185                           const std::map<Key, std::vector<size_t> >& m,
186                           test::Suite& suite)
187{
188  typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter;
189  for ( InputIterator iter=first; iter != last; ++iter) {
190    Iter map_iter = m.find(*iter);
191    if (!suite.add(map_iter!=m.end())) {
192      suite.err() << "test_inverse_validate() failed\n";
193      suite.err() << "  could not find: " << *first << " in map m\n";
194    }
195    for (size_t i=0; i<map_iter->second.size(); ++i)
196      if (!suite.add(map_iter->second[i] ==
197                     static_cast<size_t>(distance(first, iter))) ) {
198        suite.err() << "test_inverse_validate() failed\n";
199        suite.err() << "  comparing: " << map_iter->second[i] 
200                    << " and " << distance(first, iter) 
201                    << " expected them to be equal\n";
202      }
203  }
204}
205
206
207template<typename InputIterator, typename Key>
208void test_inverse_validate(InputIterator first,
209                           const std::multimap<Key, size_t>& m,
210                           test::Suite& suite)
211{
212  for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin();
213       iter!=m.end(); ++iter) {
214    suite.add(*(first + iter->second) == iter->first);
215  }
216 
217}
Note: See TracBrowser for help on using the repository browser.