| 1 |
#include <cassert> |
| 2 |
#include <sstream> |
| 3 |
|
| 4 |
#include "UnitTest++.h" |
| 5 |
#include "test_utils.hh" |
| 6 |
|
| 7 |
#include "geners/Record.hh" |
| 8 |
#include "geners/Reference.hh" |
| 9 |
#include "geners/StringArchive.hh" |
| 10 |
#include "geners/BinaryFileArchive.hh" |
| 11 |
#include "geners/MultiFileArchive.hh" |
| 12 |
#include "geners/arrayIO.hh" |
| 13 |
|
| 14 |
#define DIM 5 |
| 15 |
#define NPOINTS 50 |
| 16 |
|
| 17 |
using namespace gs; |
| 18 |
using namespace std; |
| 19 |
|
| 20 |
namespace { |
| 21 |
class Persistent2 |
| 22 |
{ |
| 23 |
public: |
| 24 |
inline Persistent2(int n=0) : n_(n) {} |
| 25 |
|
| 26 |
inline bool operator==(const Persistent2& r) const |
| 27 |
{return n_ == r.n_;} |
| 28 |
inline bool operator!=(const Persistent2& r) const |
| 29 |
{return !(*this == r);} |
| 30 |
|
| 31 |
inline Persistent2& operator+=(const int& r) |
| 32 |
{ |
| 33 |
n_ += r; |
| 34 |
return *this; |
| 35 |
} |
| 36 |
|
| 37 |
inline ClassId classId() const {return ClassId(*this);} |
| 38 |
|
| 39 |
inline bool write(std::ostream& os) const |
| 40 |
{ |
| 41 |
write_pod(os, n_); |
| 42 |
return !os.bad() && !os.fail(); |
| 43 |
} |
| 44 |
|
| 45 |
static inline const char* classname() {return "gs::test::Persistent2";} |
| 46 |
static inline unsigned version() {return 1;} |
| 47 |
static inline Persistent2* read(const ClassId& id, std::istream& in) |
| 48 |
{ |
| 49 |
assert(id == ClassId::makeId<Persistent2>()); |
| 50 |
int n; |
| 51 |
read_pod(in, &n); |
| 52 |
if (in.fail()) |
| 53 |
throw gs::IOReadFailure("In Persistent2::read: " |
| 54 |
"input stream failure"); |
| 55 |
else |
| 56 |
return new Persistent2(n); |
| 57 |
} |
| 58 |
|
| 59 |
private: |
| 60 |
int n_; |
| 61 |
}; |
| 62 |
|
| 63 |
void test_archive(AbsArchive& ar, const bool readStuff=true) |
| 64 |
{ |
| 65 |
typedef CPP11_array<Persistent2,DIM> Point; |
| 66 |
typedef CPP11_array<double,DIM> Point2; |
| 67 |
|
| 68 |
CHECK(ar.isOpen()); |
| 69 |
if (readStuff) |
| 70 |
CHECK(ar.isReadable()); |
| 71 |
CHECK(ar.isWritable()); |
| 72 |
|
| 73 |
// Create a table of objects |
| 74 |
std::vector<Point> data; |
| 75 |
Point location; |
| 76 |
for (unsigned i=0; i<NPOINTS; ++i) |
| 77 |
{ |
| 78 |
for (unsigned idim=0; idim<DIM; ++idim) |
| 79 |
location[idim] = Persistent2( |
| 80 |
static_cast<int>(test_rng()*1000000)); |
| 81 |
data.push_back(location); |
| 82 |
} |
| 83 |
CHECK(data[0] != data[1] || data[0] != data[2]); |
| 84 |
|
| 85 |
// Write out the table of objects |
| 86 |
ar << Record(data, "name1", "category1"); |
| 87 |
|
| 88 |
for (unsigned i=0; i<NPOINTS; ++i) |
| 89 |
{ |
| 90 |
for (unsigned idim=0; idim<DIM; ++idim) |
| 91 |
location[idim] = Persistent2( |
| 92 |
static_cast<int>(test_rng()*1000000)); |
| 93 |
data.push_back(location); |
| 94 |
} |
| 95 |
ArchiveRecord<std::vector<Point> > rto( |
| 96 |
Record(data, "name1", "category1")); |
| 97 |
ar << rto; |
| 98 |
|
| 99 |
// Create a table of PODs |
| 100 |
std::vector<Point2> data2; |
| 101 |
Point2 datum; |
| 102 |
for (unsigned i=0; i<NPOINTS; ++i) |
| 103 |
{ |
| 104 |
for (unsigned idim=0; idim<DIM; ++idim) |
| 105 |
datum[idim] = test_rng(); |
| 106 |
data2.push_back(datum); |
| 107 |
} |
| 108 |
|
| 109 |
// Write out the table of PODs |
| 110 |
ArchiveRecord<std::vector<Point2> > rtp( |
| 111 |
Record(data2, "name2", "category2")); |
| 112 |
ar << rtp; |
| 113 |
|
| 114 |
// Duplicate some records in another archive |
| 115 |
StringArchive dup; |
| 116 |
if (readStuff && ar.isReadable()) |
| 117 |
{ |
| 118 |
ar.copyItem(rto.id(), &dup, "hren", "blin"); |
| 119 |
ar.copyItem(rtp.id(), &dup); |
| 120 |
} |
| 121 |
|
| 122 |
// Write out a POD |
| 123 |
unsigned mynumber = 12345; |
| 124 |
ar << Record(mynumber, "my number", "who cares"); |
| 125 |
|
| 126 |
// Write out an archive |
| 127 |
StringArchive sa; |
| 128 |
sa << Record(mynumber, "clap", "dup"); |
| 129 |
ar << Record(sa, "my archive", "yok"); |
| 130 |
|
| 131 |
if (readStuff) |
| 132 |
{ |
| 133 |
const std::vector<std::string>& cats = ar.allCategories(); |
| 134 |
CHECK_EQUAL(4U, cats.size()); |
| 135 |
} |
| 136 |
|
| 137 |
// Read back the archive |
| 138 |
if (readStuff) |
| 139 |
{ |
| 140 |
Reference<StringArchive> ref(ar, "my archive", "yok"); |
| 141 |
CHECK(ref.unique()); |
| 142 |
CPP11_auto_ptr<StringArchive> psa = ref.get(0); |
| 143 |
CHECK(sa == *psa); |
| 144 |
|
| 145 |
Reference<unsigned> ref4(*psa, "clap", "dup"); |
| 146 |
unsigned dummy = 0; |
| 147 |
ref4.restore(0, &dummy); |
| 148 |
CHECK_EQUAL(mynumber, dummy); |
| 149 |
} |
| 150 |
|
| 151 |
// Read back the table of PODs and check for identity |
| 152 |
// with what was written |
| 153 |
if (readStuff) |
| 154 |
{ |
| 155 |
std::vector<Point2> read2; |
| 156 |
Reference<std::vector<Point2> >( |
| 157 |
ar, "name2", "category2").restore(0, &read2); |
| 158 |
CHECK(data2 == read2); |
| 159 |
|
| 160 |
if (ar.isReadable()) |
| 161 |
{ |
| 162 |
std::vector<Point2> read3; |
| 163 |
Reference<std::vector<Point2> >( |
| 164 |
dup, "name2", "category2").restore(0, &read3); |
| 165 |
CHECK(data2 == read3); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Read back a pod |
| 170 |
if (readStuff) |
| 171 |
{ |
| 172 |
Reference<unsigned> ref4(ar, "my number", "who cares"); |
| 173 |
unsigned dummy = 0; |
| 174 |
ref4.restore(0, &dummy); |
| 175 |
CHECK_EQUAL(mynumber, dummy); |
| 176 |
|
| 177 |
CPP11_auto_ptr<unsigned> uns2 = Reference<unsigned>( |
| 178 |
ar, "my number", "who cares").get(0); |
| 179 |
CHECK_EQUAL(mynumber, *uns2); |
| 180 |
} |
| 181 |
|
| 182 |
// Write out a vector of PODs |
| 183 |
std::vector<double> vecd; |
| 184 |
for (unsigned i=0; i<NPOINTS; ++i) |
| 185 |
vecd.push_back(test_rng()); |
| 186 |
ar << Record(vecd, "my vector", 0); |
| 187 |
|
| 188 |
// Write out a vector of objects |
| 189 |
std::vector<Persistent2> veco; |
| 190 |
for (unsigned i=0; i<NPOINTS; ++i) |
| 191 |
veco.push_back(Persistent2(static_cast<int>(test_rng()*1000000))); |
| 192 |
ar << Record(veco, "my vector 2", "duh"); |
| 193 |
|
| 194 |
// Write out a copy of a string |
| 195 |
ar << ValueRecord(std::string("message 1"), "some message", "uh"); |
| 196 |
|
| 197 |
// Read back and check for identity with what was written |
| 198 |
if (readStuff) |
| 199 |
{ |
| 200 |
Reference<std::vector<Point> > ref(ar, "name1", "category1"); |
| 201 |
CHECK(ref.size() == 2); |
| 202 |
std::vector<Point> read; |
| 203 |
ref.restore(0, &read); |
| 204 |
CHECK(data != read); |
| 205 |
ref.restore(1, &read); |
| 206 |
CHECK(data == read); |
| 207 |
CPP11_auto_ptr<std::vector<Point> > tmp = ref.get(1); |
| 208 |
CHECK(data == *tmp); |
| 209 |
|
| 210 |
if (ar.isReadable()) |
| 211 |
{ |
| 212 |
Reference<std::vector<Point> > copyref(dup, "hren", "blin"); |
| 213 |
CHECK(copyref.unique()); |
| 214 |
CPP11_auto_ptr<std::vector<Point> > copytmp = copyref.get(0); |
| 215 |
CHECK(data == *copytmp); |
| 216 |
} |
| 217 |
|
| 218 |
// Read back the vector of PODs |
| 219 |
std::vector<double> haha; |
| 220 |
Reference<std::vector<double> >(ar, "my vector", 0).restore( |
| 221 |
0, &haha); |
| 222 |
CHECK(haha == vecd); |
| 223 |
|
| 224 |
// Read back the vector of objects |
| 225 |
CPP11_auto_ptr<std::vector<Persistent2> > rv = |
| 226 |
Reference<std::vector<Persistent2> >( |
| 227 |
ar, "my vector 2", "duh").get(0); |
| 228 |
CHECK(*rv == veco); |
| 229 |
|
| 230 |
// Read back the string message |
| 231 |
Reference<std::string> sref(ar, "some message", "uh"); |
| 232 |
std::string rb; |
| 233 |
sref.restore(0, &rb); |
| 234 |
CHECK(rb == std::string("message 1")); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
TEST(ArchiveIO_string) |
| 239 |
{ |
| 240 |
StringArchive ar("Test Archive"); |
| 241 |
test_archive(ar); |
| 242 |
|
| 243 |
const std::string& st(ar.str()); |
| 244 |
CHECK_EQUAL(st.size(), ar.dataSize()); |
| 245 |
|
| 246 |
std::ostringstream os; |
| 247 |
write_item(os, ar); |
| 248 |
|
| 249 |
std::istringstream is(os.str()); |
| 250 |
ClassId myid(is, 1); |
| 251 |
CPP11_auto_ptr<StringArchive> ar2(StringArchive::read(myid, is)); |
| 252 |
CHECK(ar2.get()); |
| 253 |
CHECK(ar == *ar2); |
| 254 |
} |
| 255 |
|
| 256 |
TEST(ArchiveIO_binary) |
| 257 |
{ |
| 258 |
BinaryFileArchive ar("archive", "w+:cat=i", "binary file archive test"); |
| 259 |
test_archive(ar); |
| 260 |
ar.flush(); |
| 261 |
|
| 262 |
BinaryFileArchive ar2("archive", "r"); |
| 263 |
const unsigned long long nItems = ar.size(); |
| 264 |
CHECK_EQUAL(nItems, ar2.size()); |
| 265 |
const unsigned long long offset = 1ULL; |
| 266 |
for (unsigned long long i=offset; i<nItems+offset; ++i) |
| 267 |
CHECK(*ar.catalogEntry(i) == *ar2.catalogEntry(i)); |
| 268 |
|
| 269 |
BinaryFileArchive ar3("archive", "w:cat=i", |
| 270 |
"binary file archive test"); |
| 271 |
test_archive(ar3, false); |
| 272 |
ar3.flush(); |
| 273 |
|
| 274 |
BinaryFileArchive ar4("archive", "r"); |
| 275 |
CHECK_EQUAL(nItems, ar4.size()); |
| 276 |
for (unsigned long long i=offset; i<nItems+offset; ++i) |
| 277 |
CHECK(*ar.catalogEntry(i) == *ar4.catalogEntry(i)); |
| 278 |
} |
| 279 |
|
| 280 |
TEST(ArchiveIO_multifile) |
| 281 |
{ |
| 282 |
MultiFileArchive ar("archive_m", "w+:cat=i", |
| 283 |
"multifile archive test", 0); |
| 284 |
test_archive(ar); |
| 285 |
ar.flush(); |
| 286 |
|
| 287 |
MultiFileArchive ar2("archive_m", "r"); |
| 288 |
const unsigned long long nItems = ar.size(); |
| 289 |
CHECK_EQUAL(nItems, ar2.size()); |
| 290 |
const unsigned long long offset = 1ULL; |
| 291 |
for (unsigned long long i=offset; i<nItems+offset; ++i) |
| 292 |
CHECK(*ar.catalogEntry(i) == *ar2.catalogEntry(i)); |
| 293 |
|
| 294 |
MultiFileArchive ar3("archive_m", "w:cat=i", |
| 295 |
"multifile archive test", 0); |
| 296 |
test_archive(ar3, false); |
| 297 |
ar3.flush(); |
| 298 |
|
| 299 |
MultiFileArchive ar4("archive_m", "r"); |
| 300 |
CHECK_EQUAL(nItems, ar4.size()); |
| 301 |
for (unsigned long long i=offset; i<nItems+offset; ++i) |
| 302 |
CHECK(*ar.catalogEntry(i) == *ar4.catalogEntry(i)); |
| 303 |
} |
| 304 |
|
| 305 |
TEST(ArchiveIO_compression_bzip2) |
| 306 |
{ |
| 307 |
BinaryFileArchive ar("archive_bzip2", "w+:z=b", |
| 308 |
"bzip2 compressed archive"); |
| 309 |
test_archive(ar); |
| 310 |
ar.flush(); |
| 311 |
} |
| 312 |
|
| 313 |
TEST(ArchiveIO_compression_zlib) |
| 314 |
{ |
| 315 |
BinaryFileArchive ar("archive_zlib", "w+:z=z", |
| 316 |
"zlib compressed archive"); |
| 317 |
test_archive(ar); |
| 318 |
ar.flush(); |
| 319 |
} |
| 320 |
|
| 321 |
TEST(ArchiveIO_compression_options) |
| 322 |
{ |
| 323 |
BinaryFileArchive ar("archive_zlib", "w+:z=z:cl=9:cb=4096:cm=2048", |
| 324 |
"zlib compression options"); |
| 325 |
test_archive(ar); |
| 326 |
ar.flush(); |
| 327 |
|
| 328 |
CHECK(ar.compressionMode() == CStringStream::ZLIB); |
| 329 |
CHECK_EQUAL(4096U, ar.compressionBufferSize()); |
| 330 |
CHECK_EQUAL(2048U, ar.minSizeToCompress()); |
| 331 |
CHECK_EQUAL(9, ar.compressionLevel()); |
| 332 |
} |
| 333 |
|
| 334 |
TEST(ArchiveIO_readwrite) |
| 335 |
{ |
| 336 |
{ |
| 337 |
BinaryFileArchive ar("archive_add", "w"); |
| 338 |
std::string dummy = "dummy"; |
| 339 |
ar << Record(dummy, "A string", "some"); |
| 340 |
} |
| 341 |
{ |
| 342 |
BinaryFileArchive ar("archive_add", "r+"); |
| 343 |
std::string dummy = "dummy2"; |
| 344 |
ar << Record(dummy, "Another string", "category"); |
| 345 |
} |
| 346 |
} |
| 347 |
} |