source: trunk/test/utility_test.cc @ 1313

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

refs #330 for Matrix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: utility_test.cc 1313 2008-05-19 17:44:17Z 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 <string>
35
36using namespace theplu::yat;
37void test_quantile_normalize(test::Suite&);
38
39int main(int argc, char* argv[])
40{ 
41  using namespace theplu::yat;
42  test::Suite suite(argc, argv);
43  suite.err() << "testing utility ... " << std::endl;
44
45  // test float/double
46  std::string s("1.2");
47  if (!utility::is_double(s)){
48    suite.add(false);
49  }
50  else if (!utility::is_float(s)) {
51    suite.add(false);
52  }
53  else if (utility::is_int(s)) {
54    suite.add(false);
55  }
56  else if (utility::is_nan(s)) {
57    suite.add(false);
58  }
59
60  // test int
61  s="23";
62  if (!utility::is_double(s)){
63    suite.add(false);
64  }
65  else if (!utility::is_float(s)) {
66    suite.add(false);
67  }
68  else if (!utility::is_int(s)) {
69    suite.add(false);
70  }
71  else if (utility::is_nan(s)) {
72    suite.add(false);
73  }
74
75  // test nan
76  s=" nAn  ";
77  if (!utility::is_double(s)){
78    suite.add(false);
79    suite.err() << "error: " << s << " is a double\n";
80  }
81  /* we don't require NaN for float and int
82  else if (!utility::is_float(s)) {
83    suite.add(false);
84    suite.err() << "error: " << s << " is a float\n";
85  }
86  else if (!utility::is_int(s)) {
87    suite.add(false);
88    suite.err() << "error: " << s << " is a int\n";
89  }
90  */
91  else if (!utility::is_nan(s)) {
92    suite.add(false);
93    suite.err() << "error: " << s << " is nan\n";
94
95  }
96 
97  // testing trailing values
98  s=" 23 23   ";
99  if (utility::is_double(s)){
100    suite.add(false);
101  }
102  else if (utility::is_float(s)) {
103    suite.add(false);
104  }
105  else if (utility::is_int(s)) {
106    suite.add(false);
107  }
108  else if (utility::is_nan(s)) {
109    suite.add(false);
110  }
111
112  if (utility::convert<double>("1.23")!=1.23)
113    suite.add(false);
114  utility::convert<double>("-inf");
115  utility::convert<double>("inf");
116  utility::convert<double>("NaN");
117
118  if (!suite.add(utility::is<double>("-inf")))
119    suite.err() << "is<double>(\"-inf\") should return true\n";
120  if (!suite.add(utility::is<double>("inf")))
121    suite.err() << "is<double>(\"inf\") should return true\n";
122  if (!suite.add(utility::is<double>("NaN")))
123    suite.err() << "is<double>(\"NaN\") should return true\n";
124  if (!suite.add(utility::is<double>("1.23")))
125    suite.err() << "is<double>(\"1.23\") should return true\n";
126  if (!suite.add(!utility::is<double>("1.23.2")))
127    suite.err() << "is<double>(\"1.23.2\") should return false\n";
128  if (!suite.add(!utility::is<int>("1.23")))
129    suite.err() << "is<int>(\"1.23\") should return false\n";
130  if (!suite.add(!utility::is<int>("")))
131    suite.err() << "is<int>(\"\") should return false\n";
132
133  {
134    utility::Log<long double> f;
135    utility::Log<double> f2(1.3);
136    f(2.0);
137    utility::Exp<double> e;
138    e(3.2);
139  }
140
141  test_quantile_normalize(suite);
142
143  return suite.return_value();
144}
145
146void test_quantile_normalize(test::Suite& suite)
147{
148  suite.err() << "Testing quantile normalization\n";
149 
150  utility::Matrix m(2,2);
151  m(0,0) = 0;
152  m(0,1) = 10;
153  m(1,0) = 2;
154  m(1,1) = 4;
155  utility::quantile_normalize(m);
156  suite.err() << "Testing m(0,0)\n";
157  suite.add(suite.equal(m(0,0), 2));
158  suite.err() << "Testing m(0,1)\n";
159  suite.add(suite.equal(m(0,1), 6));
160  suite.err() << "Testing m(1,0)\n";
161  suite.add(suite.equal(m(1,0), 6));
162  suite.err() << "Testing m(1,1)\n";
163  suite.add(suite.equal(m(1,1), 2));
164
165}
Note: See TracBrowser for help on using the repository browser.