2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./tests/Test_serialisation.cc
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2016-07-07 22:31:07 +01:00
|
|
|
#include <Grid/Grid.h>
|
2015-08-20 16:21:26 +01:00
|
|
|
|
2015-12-08 13:54:00 +00:00
|
|
|
using namespace Grid;
|
|
|
|
|
2017-01-19 01:41:05 +00:00
|
|
|
GRID_SERIALIZABLE_ENUM(myenum, undef, red, 1, blue, 2, green, 3);
|
|
|
|
|
|
|
|
class myclass: Serializable {
|
|
|
|
public:
|
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
|
|
|
|
myenum, e,
|
|
|
|
std::vector<myenum>, ve,
|
|
|
|
std::string, name,
|
|
|
|
int, x,
|
|
|
|
double, y,
|
|
|
|
bool , b,
|
|
|
|
std::vector<double>, array,
|
|
|
|
std::vector<std::vector<double>>, twodimarray,
|
|
|
|
);
|
|
|
|
myclass() {}
|
|
|
|
myclass(int i)
|
|
|
|
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)), ve(2, myenum::blue)
|
|
|
|
{
|
|
|
|
e=myenum::red;
|
|
|
|
x=i;
|
|
|
|
y=2*i;
|
|
|
|
b=true;
|
|
|
|
name="bother said pooh";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int16_t i16 = 1;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint16_t u16 = 2;
|
2017-01-19 01:41:05 +00:00
|
|
|
int32_t i32 = 3;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint32_t u32 = 4;
|
2017-01-19 01:41:05 +00:00
|
|
|
int64_t i64 = 5;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint64_t u64 = 6;
|
2017-01-19 01:41:05 +00:00
|
|
|
float f = M_PI;
|
|
|
|
double d = 2*M_PI;
|
|
|
|
bool b = false;
|
2015-08-20 16:21:26 +01:00
|
|
|
|
2017-01-19 01:41:05 +00:00
|
|
|
template <typename W, typename R, typename O>
|
|
|
|
void ioTest(const std::string &filename, const O &object, const std::string &name)
|
2015-08-20 16:21:26 +01:00
|
|
|
{
|
2017-01-19 01:41:05 +00:00
|
|
|
// writer needs to be destroyed so that writing physically happens
|
2015-08-20 16:21:26 +01:00
|
|
|
{
|
2017-01-19 01:41:05 +00:00
|
|
|
W writer(filename);
|
2015-11-16 18:14:37 +00:00
|
|
|
|
2017-01-19 01:41:05 +00:00
|
|
|
write(writer, "testobject", object);
|
|
|
|
}
|
|
|
|
|
|
|
|
R reader(filename);
|
|
|
|
O buf;
|
|
|
|
bool good;
|
|
|
|
|
|
|
|
read(reader, "testobject", buf);
|
|
|
|
good = (object == buf);
|
|
|
|
std::cout << name << " IO test: " << (good ? "success" : "failure");
|
|
|
|
std::cout << std::endl;
|
|
|
|
if (!good) exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,char **argv)
|
|
|
|
{
|
|
|
|
std::cout << "==== basic IO" << std::endl;
|
|
|
|
XmlWriter WR("bother.xml");
|
|
|
|
|
|
|
|
// test basic type writing
|
|
|
|
std::cout << "-- basic writing to 'bother.xml'..." << std::endl;
|
|
|
|
push(WR,"BasicTypes");
|
|
|
|
write(WR,std::string("i16"),i16);
|
|
|
|
write(WR,"u16",u16);
|
|
|
|
write(WR,"i32",i32);
|
|
|
|
write(WR,"u32",u32);
|
|
|
|
write(WR,"i64",i64);
|
|
|
|
write(WR,"u64",u64);
|
|
|
|
write(WR,"f",f);
|
|
|
|
write(WR,"d",d);
|
|
|
|
write(WR,"b",b);
|
|
|
|
pop(WR);
|
|
|
|
|
|
|
|
// test serializable class writing
|
|
|
|
myclass obj(1234); // non-trivial constructor
|
|
|
|
std::vector<myclass> vec;
|
|
|
|
|
|
|
|
std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl;
|
|
|
|
write(WR,"obj",obj);
|
|
|
|
WR.write("obj2", obj);
|
|
|
|
vec.push_back(myclass(1234));
|
|
|
|
vec.push_back(myclass(5678));
|
|
|
|
vec.push_back(myclass(3838));
|
|
|
|
write(WR, "objvec", vec);
|
|
|
|
std::cout << "-- serialisable class writing to std::cout:" << std::endl;
|
|
|
|
std::cout << obj << std::endl;
|
|
|
|
std::cout << "-- serialisable class comparison:" << std::endl;
|
|
|
|
std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl;
|
|
|
|
std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl;
|
2015-11-16 18:14:37 +00:00
|
|
|
|
|
|
|
// read tests
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "\n==== IO self-consistency tests" << std::endl;
|
2015-11-16 18:14:37 +00:00
|
|
|
//// XML
|
2017-01-19 01:41:05 +00:00
|
|
|
ioTest<XmlWriter, XmlReader>("iotest.xml", obj, "XML (object) ");
|
|
|
|
ioTest<XmlWriter, XmlReader>("iotest.xml", vec, "XML (vector of objects)");
|
2015-11-16 18:14:37 +00:00
|
|
|
//// binary
|
2017-01-19 01:41:05 +00:00
|
|
|
ioTest<BinaryWriter, BinaryReader>("iotest.bin", obj, "binary (object) ");
|
|
|
|
ioTest<BinaryWriter, BinaryReader>("iotest.bin", vec, "binary (vector of objects)");
|
2015-11-16 18:14:37 +00:00
|
|
|
//// text
|
2017-01-19 01:41:05 +00:00
|
|
|
ioTest<TextWriter, TextReader>("iotest.dat", obj, "text (object) ");
|
|
|
|
ioTest<TextWriter, TextReader>("iotest.dat", vec, "text (vector of objects)");
|
2017-01-18 00:21:18 +00:00
|
|
|
//// HDF5
|
2017-01-19 01:41:05 +00:00
|
|
|
#ifdef HAVE_HDF5
|
|
|
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", obj, "HDF5 (object) ");
|
|
|
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", vec, "HDF5 (vector of objects)");
|
2017-01-18 00:21:18 +00:00
|
|
|
#endif
|
2016-04-30 08:15:24 +01:00
|
|
|
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "\n==== vector flattening/reconstruction" << std::endl;
|
2017-01-19 00:50:21 +00:00
|
|
|
typedef std::vector<std::vector<std::vector<double>>> vec3d;
|
2016-04-30 08:15:24 +01:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
vec3d dv, buf;
|
|
|
|
double d = 0.;
|
|
|
|
|
|
|
|
dv.resize(4);
|
|
|
|
for (auto &v1: dv)
|
2016-04-30 08:15:24 +01:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
v1.resize(3);
|
|
|
|
for (auto &v2: v1)
|
|
|
|
{
|
|
|
|
v2.resize(5);
|
|
|
|
for (auto &x: v2)
|
|
|
|
{
|
|
|
|
x = d++;
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 08:15:24 +01:00
|
|
|
}
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "original 3D vector:" << std::endl;
|
2017-01-19 00:50:21 +00:00
|
|
|
std::cout << dv << std::endl;
|
|
|
|
|
|
|
|
Flatten<vec3d> flatdv(dv);
|
|
|
|
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "\ndimensions:" << std::endl;
|
2017-01-19 00:50:21 +00:00
|
|
|
std::cout << flatdv.getDim() << std::endl;
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "\nflattened vector:" << std::endl;
|
2017-01-19 00:50:21 +00:00
|
|
|
std::cout << flatdv.getFlatVector() << std::endl;
|
|
|
|
|
|
|
|
Reconstruct<vec3d> rec(flatdv.getFlatVector(), flatdv.getDim());
|
2017-01-19 01:41:05 +00:00
|
|
|
std::cout << "\nreconstructed vector:" << std::endl;
|
2017-01-19 00:50:21 +00:00
|
|
|
std::cout << flatdv.getVector() << std::endl;
|
2015-08-20 16:21:26 +01:00
|
|
|
}
|