source: trunk/test/distance_test.cc @ 816

Last change on this file since 816 was 816, checked in by Peter, 15 years ago

test for Distance. Fixes #198

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: distance_test.cc 816 2007-03-17 00:11:31Z peter $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.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  statistics::Distance* distance = new statistics::PearsonDistance;
61  ok = ok && test(a, b, distance, 1.5, error);
62  delete distance;
63 
64  distance = new statistics::Euclidean;
65  ok = ok && test(a, b, distance, 5, error);
66  delete distance;
67 
68
69  if (error!=&std::cerr)
70    delete error;
71
72  if (ok=true) 
73    return 0;
74  return -1;
75}
76
77bool test(const utility::vector& a, const utility::vector& b, 
78          const statistics::Distance* distance, double facit, 
79          std::ostream* error)
80{
81  if (a!=b && !test(a, a, distance, 0, error))
82    return false;
83  if ((*distance)(a,b) != facit) {
84    *error << "Error: calculating distance with utility::vector\n"
85           << "       expected " << facit << " got " << (*distance)(a,b)
86           << std::endl;
87    return false;
88  }
89  utility::vector ones(a.size(), 1.0);
90  if ((*distance)(a,b, ones, ones) != facit) {
91    *error << "Error: calculating distance with four utility::vector\n"
92           << "       expected " << facit << " got " 
93           << (*distance)(a,b, ones, ones)
94           << std::endl;
95    return false;
96  }
97  classifier::DataLookup1D a1(a);
98  classifier::DataLookup1D b1(b);
99  if ((*distance)(a1,b1) != facit) {
100    *error << "Error: calculating distance with classifier::DataLookup1D\n"
101           << "       expected " << facit << " got " << (*distance)(a1,b1)
102           << std::endl;
103    return false;
104  }
105  classifier::DataLookup1D ones1(a1.size(), 1.0);
106  if ((*distance)(a1,b1, ones1, ones1) != facit) {
107    *error << "Error: calculating distance with four utility::vector\n"
108           << "       expected " << facit << " got " 
109           << (*distance)(a1,b1, ones1, ones1)
110           << std::endl;
111    return false;
112  }
113 
114 
115
116  return true;
117}
118
119
Note: See TracBrowser for help on using the repository browser.