[go: up one dir, main page]

Menu

[r792]: / libmona / test / test_vfio.cc  Maximize  Restore  History

Download this file

236 lines (183 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- mona-c++ -*-
* Copyright (c) 2004 Max-Planck-Institute for Human Cognitive and Brain Science
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libmona/mona.hh>
#include <libmona/3DVectorfield.hh>
#include <algorithm>
#include <memory>
using namespace std;
using namespace mona;
void error_report(vstream::Level level, const string& message)
{
cverb << level << message << endl;
}
bool compare_lossless(const C3DFVectorfield& orig, C3DVectorfieldPtr& loaded)
{
if (loaded.get_data_ptr()) {
const C3DFVectorfield& lvf = loaded.get_data();
if ( lvf.get_size() != orig.get_size()) {
cverr() << "fields differ in size loaded:"<<lvf.get_size() <<" vs. original: "<< orig.get_size() << endl;
return false;
}
bool result = inner_product(orig.begin(),
orig.end(),
lvf.begin(),
true,
logical_and<bool>(),
equal_to<C3DFVector>());
if (!result) {
C3DFVectorfield::const_iterator j = lvf.begin();
for (C3DFVectorfield::const_iterator i = orig.begin();
i != orig.end(); ++i, ++j) {
if (*i != *j) {
cverr() << *i << *j << endl;
}
}
}
return result;
}else {
cverr() << "Got the wrapper but not the contens ... very strange" << endl;
return false;
}
}
struct small_dist {
bool operator ()(const C3DFVector& a, const C3DFVector& b)
{
return (abs(a.x - b.x) < 0.13 &&
abs(a.y - b.y) < 0.13 &&
abs(a.z - b.z) < 0.13);
}
};
bool compare_lossy(const C3DFVectorfield& orig, C3DVectorfieldPtr& loaded)
{
if (loaded.get_data_ptr()) {
const C3DFVectorfield& lvf = loaded.get_data();
if ( lvf.get_size() != orig.get_size()) {
cverr() << "fields differ in size loaded:"<<lvf.get_size() <<" vs. original: "<< orig.get_size() << endl;
return false;
}
return inner_product(orig.begin(),
orig.end(),
lvf.begin(),
true,
logical_and<bool>(),
small_dist()
);
}else {
cverr() << "Got the wrapper but not the contens ... very strange" << endl;
return false;
}
}
bool check_save_load(const C3DVectorfieldPtr& vfptr, C3DVFieldIOPluginHandler& vfio, const string& format, bool lossless)
{
bool retval = false;
string tmp_name = string("/tmp/vftest_") + format;
error_report(vstream::ml_debug, tmp_name);
if (!vfio.save(format.c_str(), vfptr,tmp_name.c_str())) {
error_report(vstream::ml_error, format + string(" save failed"));
return false;
}
error_report(vstream::ml_debug, "saved");
auto_ptr<C3DVectorfieldPtr> loaded(vfio.load(tmp_name.c_str()));
if (loaded.get()) {
error_report(vstream::ml_debug, "loaded");
if (lossless)
retval = compare_lossless(vfptr.get_data(), *loaded);
else
retval = compare_lossy(vfptr.get_data(), *loaded);
}else{
cerr << "nothing loaded" << endl;
}
if (retval)
unlink(tmp_name.c_str());
return retval;
}
bool check_save_load_zipped(const C3DVectorfieldPtr& vfptr, C3DVFieldIOPluginHandler& vfio, const string& format, const string& suffix, bool lossless)
{
bool retval = false;
string tmp_name = string("/tmp/vftest_") + format + suffix;
cvdebug() << "Saving to " << tmp_name << "\n";
if (!vfio.save(format.c_str(), vfptr,tmp_name.c_str())) {
error_report(vstream::ml_error, format + string(" save failed"));
return false;
}
cvdebug() << "Loading from " << tmp_name << "\n";
auto_ptr<C3DVectorfieldPtr> loaded(vfio.load(tmp_name.c_str()));
if (loaded.get()) {
error_report(vstream::ml_debug, "loaded");
if (lossless)
retval = compare_lossless(vfptr.get_data(), *loaded);
else
retval = compare_lossy(vfptr.get_data(), *loaded);
}else{
cerr << "nothing loaded" << endl;
}
if (retval)
unlink(tmp_name.c_str());
return retval;
}
void vf_fill(C3DFVectorfield *vf)
{
C3DFVector v(0.0, 0.1, 0.2);
C3DFVector d(0.1, -0.1, 1.2);
C3DFVectorfield::iterator i = vf->begin();
C3DFVectorfield::iterator e = vf->end();
while (i != e) {
*i++ = v;
v += d;
}
}
int main (int argc, char **args)
{
cout << "Checking vectorfield io ... ";
list<std::string> path;
int result = 0;
cverb.set_verbosity(argc > 2 ? vstream::ml_debug : vstream::ml_error);
CPluginSearchPath::instance().clear();
CPluginSearchPath::instance().prepend("../plugins/io/.libs");
C3DVFieldIOPluginHandler vfio;
C3DVFieldIOPluginHandler::const_iterator plugin = vfio.begin();
auto_ptr<C3DVectorfieldPtr> field_ptr(new C3DVectorfieldPtr(new C3DFVectorfield(C3DBounds(5,4,6))));
vf_fill(field_ptr->get_data_ptr());
while (plugin != vfio.end()) {
cverb << vstream::ml_debug << plugin->first << endl;
bool lossless = !plugin->second->has_property(io_plugin_property_compress);
if (!check_save_load(*field_ptr, vfio, plugin->first, lossless)) {
cverb << vstream::ml_error << plugin->first << string(": FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": OK") << endl; ;
if (!check_save_load_zipped(*field_ptr, vfio, plugin->first, string(".gz"), lossless)) {
cverb << vstream::ml_error << plugin->first << string(": gz FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": gz OK") << endl; ;
if (!check_save_load_zipped(*field_ptr, vfio, plugin->first, string(".bz2"), lossless)) {
cverb << vstream::ml_error << plugin->first << string(": bz2 FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": bz2 OK") << endl; ;
++plugin;
}
if (result == 0)
cout << " done." << endl;
return result;
}