Changeset 1214 for trunk


Ignore:
Timestamp:
Mar 7, 2008, 8:40:00 PM (14 years ago)
Author:
Peter
Message:

see r1213

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/README

    r1180 r1214  
    2828must run './bootstrap' to initialize the build system. Now do
    2929'./configure' to create the Makefiles. Optionally you can run
    30 configure with '--enable-debug', which will turn on debug options. If
    31 you want to disable debugging options do './configure CXXFLAGS=""',
    32 needed since autoconf defaults to adding debug flags to the compiler.
     30configure with '--enable-debug', which will turn on debug options.
    3331
    3432Issue 'make' to compile the project.
  • trunk/test/Makefile.am

    r1122 r1214  
    88# Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
    99# Copyright (C) 2007 Jari Häkkinen, Peter Johansson
     10# Copyright (C) 2008 Peter Johansson
    1011#
    1112# This file is part of the yat library, http://trac.thep.lu.se/yat
     
    2728
    2829SUBDIRS = data
     30
     31noinst_HEADERS = Suite.h
    2932
    3033TESTS = alignment_test averager_test                  \
     
    5356
    5457alignment_test_SOURCES = alignment_test.cc
    55 averager_test_SOURCES = averager_test.cc
     58averager_test_SOURCES = averager_test.cc Suite.cc
    5659commandline_test_SOURCES = commandline_test.cc
    5760consensus_inputranker_test_SOURCES = consensus_inputranker_test.cc
  • trunk/test/averager_test.cc

    r1210 r1214  
    2424*/
    2525
     26#include "Suite.h"
     27
    2628#include "yat/statistics/Averager.h"
    2729#include "yat/statistics/AveragerPair.h"
     
    3638#include <set>
    3739
     40using namespace theplu::yat;
    3841using namespace theplu::yat::statistics;
    3942
     43using theplu::yat::test::Suite;
     44
    4045//Forward declarations
    41 bool equal(const Averager&, const Averager&);
    42 bool equal(const AveragerWeighted&, const AveragerWeighted&, const double,
    43            std::ostream* error);
    44 bool equal(const Averager&, const AveragerWeighted&, const double,
    45            std::ostream* error);
    46 bool equal(const AveragerPair&, const AveragerPair&,
    47            const double, std::ostream* error);
    48 bool equal(const AveragerPair&, const AveragerPairWeighted&,
    49            const double, std::ostream* error);
    50 bool equal(const AveragerPairWeighted&, const AveragerPairWeighted&,
    51            const double, std::ostream* error);
    52 
    53 
    54 int main(const int argc,const char* argv[])
     46bool equal(const Averager&, const Averager&, u_int, Suite&);
     47bool equal(const AveragerWeighted&, const AveragerWeighted&, u_int,
     48           Suite& suite);
     49bool equal(const Averager&, const AveragerWeighted&, u_int, Suite& suite);
     50bool equal(const AveragerPair&, const AveragerPair&, u_int, Suite& suite);
     51bool equal(const AveragerPair&, const AveragerPairWeighted&, u_int,
     52           Suite& suite);
     53bool equal(const AveragerPairWeighted&, const AveragerPairWeighted&, u_int,
     54           Suite& suite);
     55
     56
     57int main(int argc, char* argv[])
    5558
    5659
    57   std::ostream* error;
    58   if (argc>1 && argv[1]==std::string("-v"))
    59     error = &std::cerr;
    60   else {
    61     error = new std::ofstream("/dev/null");
    62     if (argc>1)
    63       std::cout << "averager_test -v : for printing extra information\n";
    64   }
    65   bool ok = true;
     60  Suite suite(argc, argv);
    6661
    6762  // Testing Averager
    68   *error << "testing Averager" << std::endl;
     63  suite.out() << "testing Averager" << std::endl;
    6964  Averager a;
    7065  a.add(1);
     
    7267  a.add(5);
    7368  if (a.n()!=3 || a.mean()!=3 || a.sum_xx()!=35){
    74     ok=false;
    75     *error << "error: add\n";
     69    suite.ok(false);
     70    suite.err() << "error: add\n";
    7671  }
    7772
    7873
    7974  Averager* a1 = new Averager(1.0+3+5,1.0+9+25,3);
    80   if (!equal(a,*a1)){
    81     ok=false;
    82     std::cout.precision(25);
    83     std::cout << equal(a,*a1) << std::endl;
    84     std::cout << a.sum_x() << '\t' << a1->sum_x() << std::endl;
    85     std::cout << a.sum_xx() << '\t' << a1->sum_xx() << std::endl;
    86     std::cout << a.n() << '\t' << a1->n() << std::endl;
    87     std::cout << a.variance() << '\t' << a1->variance() << std::endl;
    88     std::cout << a.mean() << '\t' << a1->mean() << std::endl;
    89     std::cout << a.mean() - a1->mean() << std::endl;
    90     std::cout << a.variance() - a1->variance() << std::endl;
    91     *error << "error: Averager(double x, double xx, u_long n)\n";
     75  u_int tol = 10;
     76  if (!equal(a,*a1, tol, suite)){
     77    suite.ok(false);
     78    suite.err().precision(25);
     79    suite.err() << a.sum_x() << '\t' << a1->sum_x() << std::endl;
     80    suite.err() << a.sum_xx() << '\t' << a1->sum_xx() << std::endl;
     81    suite.err() << a.n() << '\t' << a1->n() << std::endl;
     82    suite.err() << a.variance() << '\t' << a1->variance() << std::endl;
     83    suite.err() << a.mean() << '\t' << a1->mean() << std::endl;
     84    suite.err() << a.mean() - a1->mean() << std::endl;
     85    suite.err() << a.variance() - a1->variance() << std::endl;
     86    suite.err() << "error: Averager(double x, double xx, u_long n)\n";
    9287  }
    9388  delete a1;
    9489
    9590  a1 = new Averager(a);
    96   if (!equal(a,*a1)){
    97     ok=false;
    98     *error << "error: Copy constructor\n";
     91  if (!equal(a,*a1, tol, suite)){
     92    suite.ok(false);
     93    suite.err() << "error: Copy constructor\n";
    9994  }
    10095  delete a1;
    10196
    10297  a.add(3,5);
    103   if (! a.standard_error()==sqrt(a.variance()/a.n())){
    104     ok=false;
    105     *error << "error: standard_error\n";
     98  if (std::abs(a.standard_error()-sqrt(a.variance()/a.n()))>
     99      std::numeric_limits<double>().round_error() ){
     100    suite.ok(false);
     101    suite.err() << "error: standard_error\n";
    106102  }
    107103     
     
    109105  if ( std::abs(a.variance() - a.std()*a.std())>
    110106       std::numeric_limits<double>().round_error() ){
    111     ok=false;
    112     *error << "error: std squared should be variance" << std::endl;
    113     *error << "std2: " << a.std()*a.std() << std::endl;
    114     *error << "variance: " << a.variance() << std::endl;
    115     *error << "difference is: " << a.std()*a.std()-a.variance() << std::endl;
     107    suite.ok(false);
     108    suite.err() << "error: std squared should be variance" << std::endl;
     109    suite.err() << "std2: " << a.std()*a.std() << std::endl;
     110    suite.err() << "variance: " << a.variance() << std::endl;
     111    suite.err() << "difference is: " << a.std()*a.std()-a.variance() << std::endl;
    116112  }
    117113 
    118114  if ( a.variance() != a.variance(a.mean()) ){
    119     ok=false;
    120     *error << "error: variance incorrect\n" << std::endl;
    121     *error << "variance: " << a.variance() << std::endl;
    122     *error << "mean: " << a.mean() << std::endl;
    123     *error << "variance(mean) " << a.variance(a.mean()) << std::endl;
     115    suite.ok(false);
     116    suite.err() << "error: variance incorrect\n" << std::endl;
     117    suite.err() << "variance: " << a.variance() << std::endl;
     118    suite.err() << "mean: " << a.mean() << std::endl;
     119    suite.err() << "variance(mean) " << a.variance(a.mean()) << std::endl;
    124120  }
    125121  theplu::yat::utility::Vector* tmp_vec = new theplu::yat::utility::Vector(10);
     
    134130
    135131  // Testing AveragerWeighted
    136   *error << "testing AveragerWeighted" << std::endl;
     132  suite.err() << "testing AveragerWeighted" << std::endl;
    137133  theplu::yat::utility::Vector x(3,0);
    138134  x(0)=0;
     
    144140  a.reset();
    145141  add(a, x.begin(), x.end());
    146   const double tol=std::numeric_limits<double>().round_error();
    147   if (!equal(a,aw,tol,error)){
    148     *error << "error: AveragerWeighted with unitary weights should "
     142  if (!equal(a,aw,tol,suite)){
     143    suite.err() << "error: AveragerWeighted with unitary weights should "
    149144           << "be equal to Averager" << std::endl;
    150     ok=false;
     145    suite.ok(false);
    151146  }
    152147
    153148  AveragerWeighted* aw2 = new AveragerWeighted(aw);
    154   if (!equal(aw,*aw2,tol,error)){
    155     *error << "error: AveragerWeighted copy constructor " << std::endl;
    156     ok=false;
     149  if (!equal(aw,*aw2,tol,suite)){
     150    suite.err() << "error: AveragerWeighted copy constructor " << std::endl;
     151    suite.ok(false);
    157152  }
    158153   
    159154  aw2->add(12,0);
    160   if (!equal(aw,*aw2,tol,error)){
    161     *error << "error: AveragerWeighted adding a data point with weight=0 "
     155  if (!equal(aw,*aw2,tol,suite)){
     156    suite.err() << "error: AveragerWeighted adding a data point with weight=0 "
    162157           << "should make no change " << std::endl;
    163     ok=false;
     158    suite.ok(false);
    164159  }
    165160     
     
    167162  w*=17;
    168163  add(*aw2, x.begin(), x.end(), w.begin());
    169   if (!equal(aw,*aw2,tol,error)){
    170     *error << "error: AveragerWeighted rescaling weights "
     164  if (!equal(aw,*aw2,tol,suite)){
     165    suite.err() << "error: AveragerWeighted rescaling weights "
    171166           << "should make no change " << std::endl;
    172     ok=false;
     167    suite.ok(false);
    173168  }
    174169  delete aw2;
     
    185180 
    186181
    187   *error << "testing AveragerPair" << std::endl;
     182  suite.out() << "testing AveragerPair" << std::endl;
    188183  AveragerPair ap;
    189184  for (int i=0; i<10; i++)
    190185    ap.add(static_cast<double>(i),i);
    191186  if (std::abs(ap.correlation()-1)>tol){
    192     ok=false;
    193     *error << "correlation: " << ap.correlation() << std::endl;
    194     *error << "error: correlation between identical vectors should be unity"
     187    suite.ok(false);
     188    suite.err() << "correlation: " << ap.correlation() << std::endl;
     189    suite.err() << "error: correlation between identical vectors should be unity"
    195190           << std::endl;
    196191  }
    197192  if (ap.x_averager().variance()!=ap.covariance()){
    198     ok=false;
    199     *error << "error: covariance of identical vectors should equal to variance"
     193    suite.ok(false);
     194    suite.err() << "error: covariance of identical vectors should equal to variance"
    200195           << std::endl;
    201196  }
     
    203198  delete ap2;
    204199
    205   *error << "testing AveragerPairWeighted" << std::endl;
     200  suite.out() << "testing AveragerPairWeighted" << std::endl;
    206201  AveragerPairWeighted apw;
    207202  x(0)=0; x(1)=1; x(2)=2;
     
    211206  ap.reset();
    212207  add(ap, x.begin(), x.end(), y.begin());
    213   if (!equal(ap,apw,tol,error)){
    214     *error << "error: AveragerPairWeighted with unitary weights should "
     208  if (!equal(ap,apw,tol,suite)){
     209    suite.err() << "error: AveragerPairWeighted with unitary weights should "
    215210           << "be equal to AveragerPair" << std::endl;
    216     ok=false;
     211    suite.ok(false);
    217212  }
    218213
    219214  AveragerPairWeighted* apw2 = new AveragerPairWeighted(apw);
    220   if (!equal(apw,*apw2,tol,error)){
    221     *error << "error: AveragerPairWeighted copy constructor " << std::endl;
    222     ok=false;
     215  if (!equal(apw,*apw2,tol,suite)){
     216    suite.err() << "error: AveragerPairWeighted copy constructor " << std::endl;
     217    suite.ok(false);
    223218  }
    224219   
    225220  apw2->add(12,23222.03,32.3,0);
    226   if (!equal(apw,*apw2,tol,error)){
    227     *error << "error: AveragerWeighted adding a data point with weight=0 "
     221  if (!equal(apw,*apw2,tol,suite)){
     222    suite.err() << "error: AveragerWeighted adding a data point with weight=0 "
    228223           << "should make no change " << std::endl;
    229     ok=false;
     224    suite.ok(false);
    230225  }
    231226     
     
    233228  w*=17;
    234229  add(*apw2, x.begin(), x.end(), y.begin(), w.begin(), w.begin());
    235   if (!equal(apw,*apw2,tol,error)){
    236     *error << "error: AveragerWeighted rescaling weights "
     230  if (!equal(apw,*apw2,tol,suite)){
     231    suite.err() << "error: AveragerWeighted rescaling weights "
    237232           << "should make no change " << std::endl;
    238     ok=false;
     233    suite.ok(false);
    239234  }
    240235  delete apw2;
    241236
    242   if (error!=&std::cerr)
    243     delete error;
    244 
    245   if (!ok)
    246     return -1;
    247   return 0;
    248 }
    249 
    250 bool equal(const Averager& a, const Averager& b)
     237  return !suite.ok();
     238}
     239
     240bool equal(const Averager& a, const Averager& b, u_int tol,
     241           Suite& suite)
    251242{
    252243//  std::cout << (a.n()==b.n()) << std::endl;
    253244//  std::cout << (a.mean()==b.mean()) << std::endl;
    254245//  std::cout << (std::abs(a.variance()-b.variance()<1e-15)) << std::endl;
    255   return (a.n()==b.n() && a.mean()==b.mean() &&
    256           std::abs(a.variance()-b.variance()<1e-15));
     246  return (a.n()==b.n() && test::equal(a.mean(),b.mean(),tol) &&
     247          test::equal(a.variance(),b.variance(),tol));
    257248}
    258249
    259250bool equal(const AveragerWeighted& a, const AveragerWeighted& b,
    260            const double tol, std::ostream* error)
     251           const u_int tol, Suite& suite)
    261252{
    262253  bool equal = true;
    263   if  ( std::abs(a.mean()-b.mean())>tol){
     254  if  ( !test::equal(a.mean(),b.mean(),tol)){
    264255    equal=false;
    265     *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
    266   }
    267   if ( std::abs(a.variance()-b.variance())>tol ) {
     256    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
     257    suite.err() << "difference:\t" << a.mean()-b.mean() << std::endl;
     258  }
     259  if ( !test::equal(a.variance(),b.variance(),tol) ) {
    268260    equal=false;
    269     *error << "error for variance:\t" << a.variance() << " " << b.variance()
    270           << std::endl;
    271   }
    272   if ( std::abs(a.standard_error()-b.standard_error())>tol ) {
     261    suite.err() << "error for variance:\t" << a.variance() << " "
     262                << b.variance() << std::endl;
     263  }
     264  if ( !test::equal(a.standard_error(),b.standard_error(),tol) ) {
    273265    equal =false;
    274     *error << "error for standard error:\t" << std::endl;
     266    suite.err() << "error for standard error:\t" << std::endl;
    275267  }
    276268  return equal;
    277269}
    278270
    279 bool equal(const Averager& a, const AveragerWeighted& b, const double tol,
    280            std::ostream* error)
     271bool equal(const Averager& a, const AveragerWeighted& b, const u_int tol,
     272           Suite& suite)
    281273{
    282274  bool equal = true;
    283   if  ( std::abs(a.mean()-b.mean())>tol){
     275  if  ( !test::equal(a.mean(),b.mean(),tol)){
    284276    equal=false;
    285     *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
    286   }
    287   if ( std::abs(a.variance()-b.variance())>tol ) {
     277    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
     278  }
     279  if ( !test::equal(a.variance(),b.variance(),tol) ) {
    288280    equal=false;
    289     *error << "error for variance:\t" << a.variance() << " " << b.variance()
    290           << std::endl;
    291   }
    292   if ( std::abs(a.standard_error()-b.standard_error())>tol ) {
     281    suite.err() << "error for variance:\t" << a.variance() << " "
     282                << b.variance() << std::endl;
     283  }
     284  if ( !test::equal(a.standard_error(), b.standard_error(),tol) ) {
    293285    equal =false;
    294     *error << "error for standard error:\t" << std::endl;
     286    suite.err() << "error for standard error:\t" << std::endl;
    295287  }
    296288  return equal;
     
    298290
    299291bool equal(const AveragerPair& a, const AveragerPair& b,
    300            const double tol, std::ostream* error)
     292           const u_int tol, Suite& suite)
    301293{
    302294  bool ok = true;
    303295  if  ( std::abs(a.covariance()-b.covariance())>tol){
    304     ok=false;
    305     *error << "error covariance: " << a.covariance() << "\t"
     296    suite.ok(false);
     297    suite.err() << "error covariance: " << a.covariance() << "\t"
    306298           << b.covariance() << std::endl;
    307299  }
    308300  if ( std::abs(a.correlation()-b.correlation())>tol ) {
    309     ok=false;
    310     *error << "error correlation" << std::endl;
     301    suite.ok(false);
     302    suite.err() << "error correlation" << std::endl;
    311303  }
    312304  return ok;
     
    314306
    315307bool equal(const AveragerPair& a, const AveragerPairWeighted& b,
    316            const double tol, std::ostream* error)
     308           const u_int tol, Suite& suite)
    317309{
    318310  bool ok = true;
    319311  if  ( std::abs(a.covariance()-b.covariance())>tol){
    320     ok=false;
    321     *error << "error covariance: " << a.covariance() << "\t"
     312    suite.ok(false);
     313    suite.err() << "error covariance: " << a.covariance() << "\t"
    322314           << b.covariance() << std::endl;
    323315  }
    324316  if ( std::abs(a.correlation()-b.correlation())>tol ) {
    325     ok=false;
    326     *error << "error correlation" << std::endl;
    327   }
    328   if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
     317    suite.ok(false);
     318    suite.err() << "error correlation" << std::endl;
     319  }
     320  if ( !equal(a.x_averager(),b.x_averager(),tol,suite)) {
    329321    ok =false;
    330     *error << "error for x_averager():\t" << std::endl;
     322    suite.err() << "error for x_averager():\t" << std::endl;
    331323  }
    332324  return ok;
    333325}
    334326bool equal(const AveragerPairWeighted& a, const AveragerPairWeighted& b,
    335            const double tol, std::ostream* error)
     327           const u_int tol, Suite& suite)
    336328{
    337329  bool ok = true;
    338   if  ( std::abs(a.covariance()-b.covariance())>tol){
    339     ok=false;
    340     *error << "error covariance: " << a.covariance() << "\t"
     330  if  ( !test::equal(a.covariance(),b.covariance(),tol) ){
     331    suite.ok(false);
     332    suite.err() << "error covariance: " << a.covariance() << "\t"
    341333           << b.covariance() << std::endl;
    342334  }
    343   if ( std::abs(a.correlation()-b.correlation())>tol ) {
    344     ok=false;
    345     *error << "error correlation" << std::endl;
    346   }
    347   if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
     335  if ( test::equal(a.correlation(), b.correlation(), tol) ) {
     336    suite.ok(false);
     337    suite.err() << "error correlation" << std::endl;
     338  }
     339  if ( !equal(a.x_averager(),b.x_averager(),tol,suite)) {
    348340    ok =false;
    349     *error << "error for x_averager():\t" << std::endl;
     341    suite.err() << "error for x_averager():\t" << std::endl;
    350342  }
    351343  return ok;
Note: See TracChangeset for help on using the changeset viewer.