2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./tests/Test_serialisation.cc
|
|
|
|
|
2017-01-23 14:57:38 +00:00
|
|
|
Copyright (C) 2015-2016
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2017-01-23 14:57:38 +00:00
|
|
|
Author: Guido Cossu <guido.cossu@ed.ac.uk>
|
2016-01-02 14:51:32 +00:00
|
|
|
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-09-23 13:23:45 +01:00
|
|
|
namespace Grid {
|
2015-12-08 13:54:00 +00:00
|
|
|
|
|
|
|
GRID_SERIALIZABLE_ENUM(myenum, undef, red, 1, blue, 2, green, 3);
|
|
|
|
|
2015-11-16 18:14:37 +00:00
|
|
|
class myclass: Serializable {
|
|
|
|
public:
|
|
|
|
|
2015-12-08 13:54:00 +00:00
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
|
|
|
|
myenum, e,
|
|
|
|
std::vector<myenum>, ve,
|
|
|
|
std::string, name,
|
2015-11-16 18:14:37 +00:00
|
|
|
int, x,
|
|
|
|
double, y,
|
|
|
|
bool , b,
|
|
|
|
std::vector<double>, array,
|
|
|
|
std::vector<std::vector<double>>, twodimarray,
|
|
|
|
);
|
|
|
|
|
|
|
|
myclass() {}
|
|
|
|
myclass(int i)
|
2017-01-24 13:57:32 +00:00
|
|
|
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)), ve({myenum::blue, myenum::red})
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2015-12-08 13:54:00 +00:00
|
|
|
e=myenum::red;
|
2015-11-16 18:14:37 +00:00
|
|
|
x=i;
|
|
|
|
y=2*i;
|
|
|
|
b=true;
|
|
|
|
name="bother said pooh";
|
|
|
|
}
|
|
|
|
};
|
2015-12-08 13:54:00 +00:00
|
|
|
|
2017-01-23 14:57:38 +00:00
|
|
|
|
2015-09-23 13:23:45 +01:00
|
|
|
}
|
2015-08-20 16:21:26 +01:00
|
|
|
|
2015-12-08 13:54:00 +00:00
|
|
|
using namespace Grid;
|
|
|
|
|
2015-11-16 18:14:37 +00:00
|
|
|
int16_t i16 = 1;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint16_t u16 = 2;
|
2015-11-16 18:14:37 +00:00
|
|
|
int32_t i32 = 3;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint32_t u32 = 4;
|
2015-11-16 18:14:37 +00:00
|
|
|
int64_t i64 = 5;
|
2015-08-20 16:21:26 +01:00
|
|
|
uint64_t u64 = 6;
|
|
|
|
float f = M_PI;
|
|
|
|
double d = 2*M_PI;
|
|
|
|
bool b = false;
|
|
|
|
|
|
|
|
int main(int argc,char **argv)
|
|
|
|
{
|
|
|
|
{
|
2015-11-16 18:14:37 +00:00
|
|
|
XmlWriter WR("bother.xml");
|
|
|
|
|
|
|
|
// test basic type writing
|
2015-08-20 16:21:26 +01:00
|
|
|
push(WR,"BasicTypes");
|
2015-09-23 13:23:45 +01:00
|
|
|
write(WR,std::string("i16"),i16);
|
2015-08-20 16:21:26 +01:00
|
|
|
write(WR,"u16",u16);
|
|
|
|
write(WR,"i32",i32);
|
2015-09-23 13:23:45 +01:00
|
|
|
write(WR,"u32",u32);
|
2015-08-20 16:21:26 +01:00
|
|
|
write(WR,"i64",i64);
|
2015-09-23 13:23:45 +01:00
|
|
|
write(WR,"u64",u64);
|
2015-08-20 16:21:26 +01:00
|
|
|
write(WR,"f",f);
|
|
|
|
write(WR,"d",d);
|
|
|
|
write(WR,"b",b);
|
|
|
|
pop(WR);
|
2015-11-16 18:14:37 +00:00
|
|
|
|
|
|
|
// test serializable class writing
|
2015-08-20 23:04:38 +01:00
|
|
|
myclass obj(1234); // non-trivial constructor
|
2015-08-20 16:21:26 +01:00
|
|
|
write(WR,"obj",obj);
|
2015-11-16 18:14:37 +00:00
|
|
|
WR.write("obj2", obj);
|
2015-12-08 13:54:00 +00:00
|
|
|
std::cout << obj << std::endl;
|
2015-11-16 18:14:37 +00:00
|
|
|
|
|
|
|
std::vector<myclass> vec;
|
|
|
|
vec.push_back(myclass(1234));
|
|
|
|
vec.push_back(myclass(5678));
|
|
|
|
vec.push_back(myclass(3838));
|
|
|
|
write(WR, "objvec", vec);
|
2015-08-20 16:21:26 +01:00
|
|
|
};
|
2015-11-16 18:14:37 +00:00
|
|
|
|
|
|
|
// read tests
|
|
|
|
myclass copy1, copy2, copy3;
|
|
|
|
std::vector<myclass> veccopy1, veccopy2, veccopy3;
|
|
|
|
//// XML
|
|
|
|
{
|
|
|
|
XmlReader RD("bother.xml");
|
|
|
|
read(RD,"obj",copy1);
|
|
|
|
read(RD,"objvec", veccopy1);
|
|
|
|
std::cout << "Loaded (XML) -----------------" << std::endl;
|
|
|
|
std::cout << copy1 << std::endl << veccopy1 << std::endl;
|
|
|
|
}
|
|
|
|
//// binary
|
2015-08-20 23:04:38 +01:00
|
|
|
{
|
|
|
|
BinaryWriter BWR("bother.bin");
|
|
|
|
write(BWR,"discard",copy1 );
|
2015-11-16 18:14:37 +00:00
|
|
|
write(BWR,"discard",veccopy1 );
|
2015-08-20 23:04:38 +01:00
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2015-08-20 23:04:38 +01:00
|
|
|
BinaryReader BRD("bother.bin");
|
|
|
|
read (BRD,"discard",copy2 );
|
2015-11-16 18:14:37 +00:00
|
|
|
read (BRD,"discard",veccopy2 );
|
|
|
|
std::cout << "Loaded (bin) -----------------" << std::endl;
|
|
|
|
std::cout << copy2 << std::endl << veccopy2 << std::endl;
|
2015-08-20 23:04:38 +01:00
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
//// text
|
2015-08-20 23:04:38 +01:00
|
|
|
{
|
|
|
|
TextWriter TWR("bother.txt");
|
|
|
|
write(TWR,"discard",copy1 );
|
2015-11-16 18:14:37 +00:00
|
|
|
write(TWR,"discard",veccopy1 );
|
2015-08-20 23:04:38 +01:00
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2015-08-20 23:04:38 +01:00
|
|
|
TextReader TRD("bother.txt");
|
|
|
|
read (TRD,"discard",copy3 );
|
2015-11-16 18:14:37 +00:00
|
|
|
read (TRD,"discard",veccopy3 );
|
|
|
|
std::cout << "Loaded (txt) -----------------" << std::endl;
|
|
|
|
std::cout << copy3 << std::endl << veccopy3 << std::endl;
|
2015-08-20 23:04:38 +01:00
|
|
|
}
|
2016-04-30 08:15:24 +01:00
|
|
|
|
|
|
|
std::vector<int> iv = strToVec<int>("1 2 2 4");
|
|
|
|
std::vector<std::string> sv = strToVec<std::string>("bli bla blu");
|
|
|
|
|
|
|
|
for (auto &e: iv)
|
|
|
|
{
|
|
|
|
std::cout << e << " ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
for (auto &e: sv)
|
|
|
|
{
|
|
|
|
std::cout << e << " ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
std::cout << ".:::::: Testing JSON classes "<< std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
JSONWriter JW("bother.json");
|
|
|
|
|
|
|
|
// test basic type writing
|
|
|
|
push(JW,"BasicTypes");
|
|
|
|
write(JW,std::string("i16"),i16);
|
|
|
|
write(JW,"u16",u16);
|
|
|
|
write(JW,"i32",i32);
|
|
|
|
write(JW,"u32",u32);
|
|
|
|
write(JW,"i64",i64);
|
|
|
|
write(JW,"u64",u64);
|
|
|
|
write(JW,"f",f);
|
|
|
|
write(JW,"d",d);
|
|
|
|
write(JW,"b",b);
|
|
|
|
pop(JW);
|
|
|
|
|
|
|
|
// test serializable class writing
|
|
|
|
myclass obj(1234); // non-trivial constructor
|
|
|
|
write(JW,"obj",obj);
|
|
|
|
JW.write("obj2", obj);
|
|
|
|
|
|
|
|
std::cout << obj << std::endl;
|
|
|
|
|
|
|
|
std::vector<myclass> vec;
|
|
|
|
vec.push_back(myclass(1234));
|
|
|
|
vec.push_back(myclass(5678));
|
|
|
|
vec.push_back(myclass(3838));
|
|
|
|
write(JW, "objvec", vec);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JSONReader RD("bother.json");
|
|
|
|
myclass jcopy1;
|
|
|
|
std::vector<myclass> jveccopy1;
|
|
|
|
read(RD,"obj",jcopy1);
|
|
|
|
read(RD,"objvec", jveccopy1);
|
|
|
|
std::cout << "Loaded (JSON) -----------------" << std::endl;
|
|
|
|
std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:57:32 +00:00
|
|
|
{
|
|
|
|
// Testing the next element function
|
|
|
|
JSONReader RD("test.json");
|
|
|
|
RD.push("grid");
|
|
|
|
RD.push("Observable");
|
|
|
|
std::string name;
|
|
|
|
read(RD,"name", name);
|
|
|
|
}
|
2017-01-23 14:57:38 +00:00
|
|
|
|
2015-08-20 16:21:26 +01:00
|
|
|
}
|