Ignore:
Timestamp:
Mar 17, 2008, 3:54:10 PM (14 years ago)
Author:
Peter
Message:

refs #223

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/distance_test.cc

    r1239 r1247  
    3434#include <fstream>
    3535#include <iostream>
     36#include <limits>
    3637#include <list>
    3738#include <vector>
     
    4546
    4647template<class Distance>
    47 void test_distance(Distance, theplu::yat::test::Suite&);
    48 
    49 template<class Distance>
    50 void test_duplicate(Distance, theplu::yat::test::Suite&);
    51 
    52 template<class Distance>
    53 void test_rescaling(Distance, theplu::yat::test::Suite&);
    54 
    55 template<class Distance>
    56 void test_unity_weights(Distance, theplu::yat::test::Suite&);
    57 
    58 template<class Distance>
    59 void test_self_distance(Distance, theplu::yat::test::Suite&);
    60 
    61 template<class Distance>
    62 void test_symmetry(Distance, theplu::yat::test::Suite&);
    63 
    64 template<class Distance>
    65 void test_zero_weight(Distance, theplu::yat::test::Suite&);
     48void test_distance(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     49
     50template<class Distance>
     51void test_duplicate(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     52
     53template<class Distance>
     54void test_rescaling(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     55
     56template<class Distance>
     57void test_unity_weights(Distance, theplu::yat::test::Suite&,
     58                        unsigned long int N=1);
     59
     60template<class Distance>
     61void test_self_distance(Distance, theplu::yat::test::Suite&,
     62                        unsigned long int N=1);
     63
     64template<class Distance>
     65void test_symmetry(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     66
     67template<class Distance>
     68void test_zero_weight(Distance, theplu::yat::test::Suite&,
     69                      unsigned long int N=1);
    6670
    6771utility::Matrix weight(void);
     
    8084  statistics::EuclideanDistance eucl_dist;
    8185  suite.err() << "testing EuclideanDistance" << std::endl;
    82   test_distance(eucl_dist, suite);
     86  test_distance(eucl_dist, suite, 100);
    8387  double dist=eucl_dist(a.begin(),a.end(),b.begin());
    8488  if(std::abs(dist-2.23607)>tolerance) {
     
    8993  statistics::PearsonDistance pear_dist;
    9094  suite.err() << "testing PearsonDistance" << std::endl;
    91   test_distance(pear_dist, suite);
     95  test_distance(pear_dist, suite, 1000);
    9296  dist=pear_dist(a.begin(),a.end(),b.begin());
    9397  if(std::abs(dist-1.5)>tolerance) {
     
    170174
    171175template<class Distance>
    172 void test_distance(Distance dist, theplu::yat::test::Suite& suite)
    173 {
    174   test_duplicate(dist, suite);
    175   test_rescaling(dist, suite);
    176   test_unity_weights(dist, suite);
    177   test_self_distance(dist, suite);
    178   test_symmetry(dist, suite);
    179   test_zero_weight(dist, suite);
    180 }
    181 
    182 template<class Distance>
    183 void test_duplicate(Distance dist, theplu::yat::test::Suite& suite)
     176void test_distance(Distance dist, theplu::yat::test::Suite& suite,
     177                   unsigned int long N)
     178{
     179  test_duplicate(dist, suite, N);
     180  test_rescaling(dist, suite, N);
     181  test_unity_weights(dist, suite, N);
     182  test_self_distance(dist, suite, N);
     183  test_symmetry(dist, suite, N);
     184  test_zero_weight(dist, suite, N);
     185}
     186
     187template<class Distance>
     188void test_duplicate(Distance dist, theplu::yat::test::Suite& suite,
     189                    unsigned long int N)
    184190{
    185191  utility::Matrix x(data());
     
    200206    w2(0,i)=0.0;
    201207  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    202   check_equality(dist1, dist2, suite, "duplicate property");
    203 }
    204 
    205 template<class Distance>
    206 void test_rescaling(Distance dist, theplu::yat::test::Suite& suite)
     208  check_equality(dist1, dist2, suite, "duplicate property", N);
     209}
     210
     211template<class Distance>
     212void test_rescaling(Distance dist, theplu::yat::test::Suite& suite,
     213                    unsigned long int N)
    207214{
    208215  utility::Matrix x=data();
     
    212219  w *= 2.13;
    213220  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    214   check_equality(dist1, dist2, suite, "rescaling", 10);
    215 }
    216 
    217 template<class Distance>
    218 void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite)
     221  check_equality(dist1, dist2, suite, "rescaling", N);
     222}
     223
     224template<class Distance>
     225void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite,
     226                        unsigned long int N)
    219227{
    220228  utility::Matrix x=data();
     
    222230  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    223231  double dist2 = dist(x.begin_row(0), x.end_row(0), x.begin_row(1));
    224   check_equality(dist1, dist2, suite, "unity weights", 10);
    225 }
    226 
    227 template<class Distance>
    228 void test_self_distance(Distance dist, theplu::yat::test::Suite& suite)
     232  check_equality(dist1, dist2, suite, "unity weights", N);
     233}
     234
     235template<class Distance>
     236void test_self_distance(Distance dist, theplu::yat::test::Suite& suite,
     237                        unsigned long int N)
    229238{
    230239  utility::Matrix x = data();
    231240  double self = dist(x.begin(), x.end(), x.begin());
    232   if (!suite.equal(self, 0.0)){
     241  if (!(std::abs(self) <= N*std::numeric_limits<double>().epsilon()) ){
    233242    suite.err() << "error: self distance is " << self << "\n"
    234243                << "supposed to be zero.\n";
     
    239248
    240249template<class Distance>
    241 void test_symmetry(Distance dist, theplu::yat::test::Suite& suite)
     250void test_symmetry(Distance dist, theplu::yat::test::Suite& suite,
     251                   unsigned long int N)
    242252{
    243253  utility::Matrix x = data();
    244254  double distab = dist(x.begin_row(0), x.end_row(0), x.begin_row(1));
    245255  double distba = dist(x.begin_row(1), x.end_row(1), x.begin_row(0));
    246   check_equality(distab, distba, suite, "symmetry test");
    247 }
    248 
    249 
    250 template<class Distance>
    251 void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite)
     256  check_equality(distab, distba, suite, "symmetry test", N);
     257}
     258
     259
     260template<class Distance>
     261void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite,
     262                      unsigned long int N)
    252263{
    253264  utility::Matrix x=data();
     
    258269  w(0,0) = 100*std::numeric_limits<double>().epsilon();
    259270  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    260   check_equality(dist1, dist2, suite, "zero weight", 1000);
     271  check_equality(dist1, dist2, suite, "zero weight", N);
    261272}
    262273
Note: See TracChangeset for help on using the changeset viewer.