root/library/tests/pdf_harness.cpp @ 797

Revision 722, 2.9 kB (checked in by mido, 15 years ago)

astyler run over all test sources
general_suite added
cleanup of \test directory finished

  • Property svn:eol-style set to native
Line 
1#include "pdf_harness.h"
2#include "base/bdmbase.h"
3#include "base/user_info.h"
4#include "stat/exp_family.h"
5#include "mat_checks.h"
6#include "test_util.h"
7#include "UnitTest++.h"
8
9namespace bdm {
10
11void pdf_harness::test_config ( const char *config_file_name ) {
12        RV::clear_all();
13
14        UIFile in ( config_file_name );
15        Array<shared_ptr<pdf_harness> > input;
16        UI::get ( input, in, "data", UI::compulsory );
17        int sz = input.size();
18        CHECK ( sz > 0 );
19        for ( int i = 0; i < sz; ++i ) {
20                input ( i )->test ( config_file_name, i );
21        }
22}
23
24void pdf_harness::from_setting ( const Setting &set ) {
25        hpdf = UI::build<pdf> ( set, "pdf", UI::compulsory );
26        UI::get ( mean, set, "mean", UI::compulsory );
27        UI::get ( variance, set, "variance", UI::compulsory );
28
29        if ( !UI::get ( cond, set, "cond", UI::optional ) ) {
30                cond = zeros ( hpdf->dimensionc() );
31        }
32
33        UI::get ( nsamples, set, "nsamples", UI::optional );
34        UI::get ( R, set, "R", UI::optional );
35        UI::get ( tolerance, set, "tolerance", UI::optional );
36}
37
38void pdf_harness::test ( const char *config_name, int idx ) {
39        CurrentContext cc ( config_name, idx );
40        check_mean();
41        if ( R.rows() > 0 ) {
42                check_covariance();
43        }
44}
45
46void pdf_harness::check_mean() {
47        vec delta = make_close_tolerance ( variance, nsamples );
48
49        int tc = 0;
50        Array<vec> actual ( CurrentContext::max_trial_count );
51        do {
52                mat smp = hpdf->samplecond_mat ( cond, nsamples );
53                vec emu = smp * ones ( nsamples ) / nsamples;
54                actual ( tc ) = emu;
55                ++tc;
56        } while ( ( tc < CurrentContext::max_trial_count ) &&
57                  !UnitTest::AreClose ( mean, actual ( tc - 1 ), delta ) );
58        if ( ( tc == CurrentContext::max_trial_count ) &&
59                ( !UnitTest::AreClose ( mean, actual ( CurrentContext::max_trial_count - 1 ), delta ) ) ) {
60                UnitTest::MemoryOutStream stream;
61                stream << CurrentContext::format_context ( __LINE__ ) << "expected " << mean << " +/- " << delta << " but was " << actual;
62
63                UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false );
64
65                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
66        }
67}
68
69void pdf_harness::check_covariance() {
70        int tc = 0;
71        Array<mat> actual ( CurrentContext::max_trial_count );
72        do {
73                mat smp = hpdf->samplecond_mat ( cond, nsamples );
74                vec emu = smp * ones ( nsamples ) / nsamples;
75                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
76                actual ( tc ) = er;
77                ++tc;
78        } while ( ( tc < CurrentContext::max_trial_count ) &&
79                  !UnitTest::AreClose ( R, actual ( tc - 1 ), tolerance ) );
80        if ( ( tc == CurrentContext::max_trial_count ) &&
81                ( !UnitTest::AreClose ( R, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
82                UnitTest::MemoryOutStream stream;
83                stream << CurrentContext::format_context ( __LINE__ ) << "expected " << R << " +/- " << tolerance << " but was " << actual;
84
85                UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false );
86
87                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
88        }
89}
90
91}
Note: See TracBrowser for help on using the browser.