root/library/tests/test_util.cpp @ 469

Revision 469, 2.9 kB (checked in by vbarta, 15 years ago)

made to compile on Windows (again); tests fail

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{
31    char buffer[8192];
32    memset(buffer, 0, sizeof(buffer));
33    std::ifstream src(fname, std::ios_base::binary);
34    src.read(buffer, sizeof(buffer) - 1);
35    return std::string(buffer);
36}
37
38bool remove_all(const char *path) {
39    DIR *dir;
40    dirent *de;
41
42    bool rv = true;
43    if ((dir = opendir(path)) != 0) {
44        try {
45            std::string top(path);
46            top += "/";
47
48            while ((de = readdir(dir)) != 0) {
49                if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
50                    std::string subpath(top);
51                    subpath += de->d_name;
52                    remove_all(subpath.c_str());
53                }
54            }
55        } catch (...) {
56            closedir(dir);
57            throw;
58        }
59
60        closedir(dir);
61
62        if (rmdir(path)) {
63            std::string msg = "can't remove dir ";
64            msg += path;
65            throw std::runtime_error(msg);
66        }
67    } else { 
68        if (errno == ENOTDIR) {
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.