source: trunk/test/distance_test.cc @ 865

Last change on this file since 865 was 865, checked in by Peter, 16 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: distance_test.cc 865 2007-09-10 19:41:04Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/classifier/DataLookup1D.h"
25#include "yat/statistics/Distance.h"
26#include "yat/statistics/Euclidean.h"
27#include "yat/statistics/PearsonDistance.h"
28#include "yat/utility/vector.h"
29
30#include <cassert>
31#include <cmath>
32#include <fstream>
33#include <iostream>
34
35
36using namespace theplu::yat;
37
38bool test(const utility::vector&, const utility::vector&, 
39          const statistics::Distance*, double, std::ostream*);
40
41int main(const int argc,const char* argv[])
42
43{ 
44  std::ostream* error;
45  if (argc>1 && argv[1]==std::string("-v"))
46    error = &std::cerr;
47  else {
48    error = new std::ofstream("/dev/null");
49    if (argc>1)
50      std::cout << "distance_test -v : for printing extra information\n";
51  }
52  *error << "testing distance" << std::endl;
53  bool ok = true;
54
55  utility::vector a(3,1);
56  a(1) = 2;
57  utility::vector b(3,0);
58  b(2) = 1;
59
60  *error << "testing PearsonDistance" << std::endl;
61  statistics::Distance* distance = new statistics::PearsonDistance;
62  ok = ok && test(a, b, distance, 1.5, error);
63  delete distance;
64 
65  *error << "testing Euclidean" << std::endl;
66  distance = new statistics::Euclidean;
67  ok = ok && test(a, b, distance, 5, error);
68  delete distance;
69 
70
71  if (error!=&std::cerr)
72    delete error;
73
74  if (ok=true) 
75    return 0;
76  return -1;
77}
78
79bool test(const utility::vector& a, const utility::vector& b, 
80          const statistics::Distance* distance, double facit, 
81          std::ostream* error)
82{
83  // distance between a and a is always zero
84  if (a!=b && !test(a, a, distance, 0, error))
85    return false;
86  else 
87    *error << "Testing that distance between x and x is zero" 
88           << std::endl;
89  if ((*distance)(a,b) != facit) {
90    *error << "Error: calculating distance with utility::vector\n"
91           << "       expected " << facit << " got " << (*distance)(a,b)
92           << std::endl;
93    return false;
94  }
95  utility::vector ones(a.size(), 1.0);
96  if ((*distance)(a,b, ones, ones) != facit) {
97    *error << "Error: calculating distance with four utility::vector\n"
98           << "       expected " << facit << " got " 
99           << (*distance)(a,b, ones, ones)
100           << std::endl;
101    return false;
102  }
103  classifier::DataLookup1D a1(a);
104  classifier::DataLookup1D b1(b);
105  if ((*distance)(a1,b1) != facit) {
106    *error << "Error: calculating distance with classifier::DataLookup1D\n"
107           << "       expected " << facit << " got " << (*distance)(a1,b1)
108           << std::endl;
109    return false;
110  }
111  classifier::DataLookup1D ones1(a1.size(), 1.0);
112  if ((*distance)(a1,b1, ones1, ones1) != facit) {
113    *error << "Error: calculating distance with four utility::vector\n"
114           << "       expected " << facit << " got " 
115           << (*distance)(a1,b1, ones1, ones1)
116           << std::endl;
117    return false;
118  }
119 
120 
121
122  return true;
123}
124
125
Note: See TracBrowser for help on using the repository browser.