root/library/tests/test_util.cpp @ 486

Revision 486, 3.0 kB (checked in by vbarta, 15 years ago)

fixed test utils for windows (debug testsuite passes)

Line 
1#define BDMLIB // not an ideal way to prevent double registration of UI factories...
2#include "test_util.h"
3#include "stat/exp_family.h"
4#include <fstream>
5#include <stdexcept>
6#include <string>
7#include <errno.h>
8#include <fcntl.h>
9#include <string.h>
10#include <stdio.h>
11#include <sys/stat.h>
12#include <sys/types.h>
13
14#ifdef WIN32
15#include "dirent.h"
16#include <direct.h>
17#define rmdir _rmdir
18#define unlink _unlink
19#else
20#include <dirent.h>
21#include <unistd.h>
22#endif
23
24namespace bdm {
25
26using itpp::vec;
27using itpp::mat;
28
29std::string load_test_file ( const char *fname ) {
30        char buffer[8192];
31        memset ( buffer, 0, sizeof ( buffer ) );
32        std::ifstream src ( fname );
33        src.read ( buffer, sizeof ( buffer ) - 1 );
34        return std::string ( buffer );
35}
36
37bool remove_all ( const char *path ) {
38        DIR *dir;
39        dirent *de;
40
41        bool rv = true;
42        if ( ( dir = opendir ( path ) ) != 0 ) {
43                try {
44                        std::string top ( path );
45                        top += "/";
46
47                        while ( ( de = readdir ( dir ) ) != 0 ) {
48                                if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) {
49                                        std::string subpath ( top );
50                                        subpath += de->d_name;
51                                        remove_all ( subpath.c_str() );
52                                }
53                        }
54                } catch ( ... ) {
55                        closedir ( dir );
56                        throw;
57                }
58
59                closedir ( dir );
60
61                if ( rmdir ( path ) ) {
62                        std::string msg = "can't remove dir ";
63                        msg += path;
64                        throw std::runtime_error ( msg );
65                }
66        } else {
67                if ( ( errno == ENOTDIR ) || // Linux
68                     ( errno == EINVAL ) ) { // Windows
69                        if ( unlink ( path ) ) {
70                                std::string msg = "can't remove file ";
71                                msg += path;
72                                throw std::runtime_error ( msg );
73                        }
74                } else {
75                        if ( errno != ENOENT ) {
76                                std::string msg = "can't remove ";
77                                msg += path;
78                                throw std::runtime_error ( msg );
79                        } else {
80                                // it wasn't there in the first place
81                                rv = false;
82                        }
83                }
84        }
85
86        return rv;
87}
88
89double normcoef ( const epdf *ep, const vec &xb, const vec &yb,
90                  int xn, int yn ) {
91        mat Pdf ( xn + 1, yn + 1 );
92        vec rgr ( 2 );
93
94        double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn;
95        double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn;
96
97        double x = xb ( 0 );
98        for ( int i = 0; i <= xn; x += xstep, i++ ) {
99                rgr ( 0 ) = x;
100                double y = yb ( 0 );
101                for ( int j = 0; j <= yn; y += ystep, j++ ) {
102                        rgr ( 1 ) = y;
103                        Pdf ( i, j ) = exp ( ep->evallog ( rgr ) );
104                }
105        }
106
107        return sumsum ( Pdf ) * xstep * ystep;
108}
109
110vec num_mean2 ( const epdf *ep, const vec &xb, const vec &yb,
111                int xn, int yn ) {
112        mat Pdf ( xn + 1, yn + 1 );
113        vec rgr ( 2 );
114
115        double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn;
116        double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn;
117
118        vec Mu ( xn + 1 );
119        vec Si ( yn + 1 );
120
121        double x = xb ( 0 );
122        for ( int i = 0; i <= xn; x += xstep, i++ ) {
123                Mu ( i ) = x;
124                rgr ( 0 ) = x;
125                double y = yb ( 0 );
126                for ( int j = 0; j <= yn; y += ystep, j++ ) {
127                        Si ( j ) = y;
128                        rgr ( 1 ) = y;
129                        Pdf ( i, j ) = exp ( ep->evallog ( rgr ) );
130                }
131        }
132
133        vec fm = sum ( Pdf, 2 );
134        double sfm = sum ( fm );
135        vec fs = sum ( Pdf, 1 );
136        double sfs = sum ( fs );
137        double vi0 = Mu * fm / sfm;
138        double vi1 = Si * fs / sfs;
139        return vec_2 ( vi0, vi1 );
140}
141
142}
Note: See TracBrowser for help on using the browser.