Skip to content
Snippets Groups Projects
Commit 9b75e051 authored by Jeremie Deray's avatar Jeremie Deray
Browse files

wip NodeBase serialization + gtest

parent 2c8838c3
No related branches found
No related tags found
1 merge request!141Serialization
#ifndef _WOLF_IO_CEREAL_NODE_BASE_H_
#define _WOLF_IO_CEREAL_NODE_BASE_H_
// Wolf includes
#include "../../node_base.h"
#include <cereal/cereal.hpp>
#include <cereal/types/polymorphic.hpp>
namespace wolf {
struct NodeBase::Serializer {
template <class Archive>
static void serialize(Archive& ar, NodeBase& o, std::uint32_t const /*version*/)
{
ar( cereal::make_nvp("node_class_", o.node_class_) );
ar( cereal::make_nvp("node_type_", o.node_type_) );
ar( cereal::make_nvp("node_name_", o.node_name_) );
ar( cereal::make_nvp("node_id_", o.node_id_) );
// ar( cereal::make_nvp("problem_ptr_", o.problem_ptr_) );
// Not sure what to do with this guy ...
//ar( cereal::make_nvp("node_id_count_", o.node_id_count_) );
}
template <class Archive>
static void load_and_construct( Archive& ar, cereal::construct<wolf::NodeBase>& construct,
std::uint32_t const /*version*/ )
{
decltype(std::declval<wolf::NodeBase>().getClass()) nb_class;
decltype(std::declval<wolf::NodeBase>().getType()) nb_type;
decltype(std::declval<wolf::NodeBase>().getName()) nb_name;
ar( cereal::make_nvp("node_class_", nb_class) );
ar( cereal::make_nvp("node_type_", nb_type) );
ar( cereal::make_nvp("node_name_", nb_name) );
construct( nb_class, nb_type, nb_name );
ar( cereal::make_nvp("node_id_", construct->node_id_) );
// ar( cereal::make_nvp("problem_ptr_", construct->problem_ptr_) );
// Not sure what to do with this guy ...
//ar( cereal::make_nvp("node_id_count_", construct->node_id_count_) );
}
};
} // namespace wolf
namespace cereal {
/// @note No default constructor thus the need
/// for these specializations
template <>
struct LoadAndConstruct<wolf::NodeBase>
{
template <class Archive>
static void load_and_construct( Archive& ar,
cereal::construct<wolf::NodeBase>& construct,
std::uint32_t const version )
{
wolf::NodeBase::Serializer::load_and_construct(ar, construct, version);
}
};
template <class Archive>
void serialize(Archive& ar, wolf::NodeBase& o, std::uint32_t const version)
{
wolf::NodeBase::Serializer::serialize(ar, o, version);
}
} // namespace cereal
#endif /* _WOLF_IO_CEREAL_NODE_BASE_H_ */
/*
* gtest_node_base_serialization.cpp
*
* Created on: Jul 16, 2017
* Author: Jeremie Deray
*/
#include "../../utils_gtest.h"
#include "../../../serialization/cereal/serialization_node_base.h"
#include "../../../serialization/cereal/archives.h"
#include <cereal/types/memory.hpp>
#include <fstream>
class WolfTestCerealSerializationNodeBase : public testing::Test
{
public:
WolfTestCerealSerializationNodeBase() /*:
nb(nb_class),
nb_ptr(std::make_shared<wolf::NodeBase>(nb_class))*/
{
//
}
const std::string path_to_io = "/tmp/";
decltype(std::declval<wolf::NodeBase>().getClass()) nb_class = "Foo";
decltype(std::declval<wolf::NodeBase>().getClass()) nb_type = "Bar";
decltype(std::declval<wolf::NodeBase>().getClass()) nb_name = "Dummy";
decltype(std::declval<wolf::NodeBase>().nodeId()) id;
// wolf::NodeBase nb;
// wolf::NodeBasePtr nb_ptr;
};
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBaseXML)
{
{
// This guy has node_id = 1
wolf::NodeBase nb(nb_class, nb_type, nb_name);
id = nb.nodeId();
std::ofstream os(path_to_io + "node_base_serialization.xml");
cereal::XMLOutputArchive xml_archive(os);
ASSERT_NO_THROW( xml_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_serialization.xml");
cereal::XMLInputArchive xml_archive(is);
// This guy has node_id = 2
wolf::NodeBase nb("SuperDummy");
ASSERT_NO_THROW( xml_archive( nb ) );
ASSERT_EQ(nb.getClass(), nb_class);
ASSERT_EQ(nb.getType(), nb_type);
ASSERT_EQ(nb.getName(), nb_name);
ASSERT_EQ(nb.nodeId(), id);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBaseXML !\n");
}
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBasePtrXML)
{
{
// This guy has node_id = 3
wolf::NodeBasePtr nb = std::make_shared<wolf::NodeBase>(nb_class, nb_type, nb_name);
id = nb->nodeId();
std::ofstream os(path_to_io + "node_base_ptr_serialization.xml");
cereal::XMLOutputArchive xml_archive(os);
ASSERT_NO_THROW( xml_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_ptr_serialization.xml");
cereal::XMLInputArchive xml_archive(is);
wolf::NodeBasePtr nb;
ASSERT_NO_THROW( xml_archive( nb ) );
ASSERT_EQ(nb->getClass(), nb_class);
ASSERT_EQ(nb->getType(), nb_type);
ASSERT_EQ(nb->getName(), nb_name);
ASSERT_EQ(nb->nodeId(), id);
ASSERT_TRUE(
std::dynamic_pointer_cast<
wolf::NodeBase>(nb) != nullptr);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBasePtrXML !\n");
}
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBaseJSON)
{
{
wolf::NodeBase nb(nb_class, nb_type, nb_name);
id = nb.nodeId();
std::ofstream os(path_to_io + "node_base_serialization.json");
cereal::JSONOutputArchive json_archive(os);
ASSERT_NO_THROW( json_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_serialization.json");
cereal::JSONInputArchive json_archive(is);
wolf::NodeBase blank("This guy has node_id = 1");
wolf::NodeBase nb("SuperDummy");
ASSERT_NO_THROW( json_archive( nb ) );
ASSERT_EQ(nb.getClass(), nb_class);
ASSERT_EQ(nb.getType(), nb_type);
ASSERT_EQ(nb.getName(), nb_name);
ASSERT_EQ(nb.nodeId(), id);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBaseJSON !\n");
}
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBasePtrJSON)
{
{
wolf::NodeBasePtr nb = std::make_shared<wolf::NodeBase>(nb_class, nb_type, nb_name);
id = nb->nodeId();
std::ofstream os(path_to_io + "node_base_ptr_serialization.json");
cereal::JSONOutputArchive json_archive(os);
ASSERT_NO_THROW( json_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_ptr_serialization.json");
cereal::JSONInputArchive json_archive(is);
wolf::NodeBasePtr nb;
ASSERT_NO_THROW( json_archive( nb ) );
ASSERT_EQ(nb->getClass(), nb_class);
ASSERT_EQ(nb->getType(), nb_type);
ASSERT_EQ(nb->getName(), nb_name);
ASSERT_EQ(nb->nodeId(), id);
ASSERT_TRUE(
std::dynamic_pointer_cast<
wolf::NodeBase>(nb) != nullptr);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBasePtrJSON !\n");
}
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBaseBinary)
{
{
wolf::NodeBase nb(nb_class, nb_type, nb_name);
id = nb.nodeId();
std::ofstream os(path_to_io + "node_base_serialization.bin");
cereal::BinaryOutputArchive bin_archive(os);
ASSERT_NO_THROW( bin_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_serialization.bin");
cereal::BinaryInputArchive bin_archive(is);
wolf::NodeBase blank("This guy has node_id = 1");
wolf::NodeBase nb("SuperDummy");
ASSERT_NO_THROW( bin_archive( nb ) );
ASSERT_EQ(nb.getClass(), nb_class);
ASSERT_EQ(nb.getType(), nb_type);
ASSERT_EQ(nb.getName(), nb_name);
ASSERT_EQ(nb.nodeId(), id);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBaseBinary !\n");
}
TEST_F(WolfTestCerealSerializationNodeBase, CerealSerializationNodeBasePtrBinary)
{
{
wolf::NodeBasePtr nb = std::make_shared<wolf::NodeBase>(nb_class, nb_type, nb_name);
id = nb->nodeId();
std::ofstream os(path_to_io + "node_base_ptr_serialization.bin");
cereal::BinaryOutputArchive bin_archive(os);
ASSERT_NO_THROW( bin_archive( nb ) );
}
{
std::ifstream is(path_to_io + "node_base_ptr_serialization.bin");
cereal::BinaryInputArchive bin_archive(is);
wolf::NodeBasePtr nb;
ASSERT_NO_THROW( bin_archive( nb ) );
ASSERT_EQ(nb->getClass(), nb_class);
ASSERT_EQ(nb->getType(), nb_type);
ASSERT_EQ(nb->getName(), nb_name);
ASSERT_EQ(nb->nodeId(), id);
ASSERT_TRUE(
std::dynamic_pointer_cast<
wolf::NodeBase>(nb) != nullptr);
}
PRINTF("All good at "
"WolfTestCerealSerializationNodeBase::CerealSerializationNodeBasePtrBinary !\n");
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment