Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 4 additions & 80 deletions tests/test_serialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
#include <functional>
#include <gtest/gtest.h>

#include "test_serialize.h"
#include "test_utils.h"

#include "models/gp.h"
#include <cereal/archives/json.hpp>
#include <cereal/types/polymorphic.hpp>

CEREAL_REGISTER_TYPE_WITH_NAME(albatross::MockModel, "mock_model_name");

Expand Down Expand Up @@ -58,17 +57,6 @@ using DoubleRegressionModelPointer = std::unique_ptr<RegressionModel<double>>;
* the variants using TYPED_TEST.
*/

template <typename X> struct SerializableType {
using RepresentationType = X;
virtual RepresentationType create() const {
RepresentationType obj;
return obj;
}
virtual bool are_equal(const X &lhs, const X &rhs) const {
return lhs == rhs;
};
};

struct EmptyEigenVectorXd : public SerializableType<Eigen::VectorXd> {
Eigen::VectorXd create() const override {
Eigen::VectorXd x;
Expand Down Expand Up @@ -358,10 +346,8 @@ class FitGaussianProcess
};
};

template <typename Serializable>
struct PolymorphicSerializeTest : public ::testing::Test {
typedef typename Serializable::RepresentationType Representation;
};
REGISTER_TYPED_TEST_CASE_P(SerializeTest, test_roundtrip_serialize_json,
test_roundtrip_serialize_binary);

typedef ::testing::Types<
LDLT, EigenMatrix3d, SerializableType<Eigen::Matrix2i>, EmptyEigenVectorXd,
Expand All @@ -374,68 +360,6 @@ typedef ::testing::Types<
FitLinearSerializablePointer, UnfitGaussianProcess, FitGaussianProcess>
ToTest;

TYPED_TEST_CASE(PolymorphicSerializeTest, ToTest);

TYPED_TEST(PolymorphicSerializeTest, test_roundtrip_serialize_json) {
TypeParam model_and_rep;
using X = typename TypeParam::RepresentationType;
const X original = model_and_rep.create();

// Serialize it
std::ostringstream os;
{
cereal::JSONOutputArchive oarchive(os);
oarchive(original);
}
// Deserialize it.
std::istringstream is(os.str());
X deserialized;
{
cereal::JSONInputArchive iarchive(is);
iarchive(deserialized);
}
// Make sure the original and deserialized representations are
// equivalent.
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
// Reserialize the deserialized object
std::ostringstream os_again;
{
cereal::JSONOutputArchive oarchive(os_again);
oarchive(deserialized);
}
// And make sure the serialized strings are the same,
EXPECT_EQ(os_again.str(), os.str());
}

TYPED_TEST(PolymorphicSerializeTest, test_roundtrip_serialize_binary) {
TypeParam model_and_rep;
using X = typename TypeParam::RepresentationType;
const X original = model_and_rep.create();

// Serialize it
std::ostringstream os;
{
cereal::BinaryOutputArchive oarchive(os);
oarchive(original);
}
// Deserialize it.
std::istringstream is(os.str());
X deserialized;
{
cereal::BinaryInputArchive iarchive(is);
iarchive(deserialized);
}
// Make sure the original and deserialized representations are
// equivalent.
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
// Reserialize the deserialized object
std::ostringstream os_again;
{
cereal::BinaryOutputArchive oarchive(os_again);
oarchive(deserialized);
}
// And make sure the serialized strings are the same,
EXPECT_EQ(os_again.str(), os.str());
}
INSTANTIATE_TYPED_TEST_CASE_P(Albatross, SerializeTest, ToTest);

} // namespace albatross
95 changes: 95 additions & 0 deletions tests/test_serialize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2018 Swift Navigation Inc.
* Contact: Swift Navigation <dev@swiftnav.com>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <cereal/archives/json.hpp>
#include <cereal/types/polymorphic.hpp>

namespace albatross {

template <typename X> struct SerializableType {
using RepresentationType = X;
virtual RepresentationType create() const {
RepresentationType obj;
return obj;
}
virtual bool are_equal(const X &lhs, const X &rhs) const {
return lhs == rhs;
};
};

template <typename Serializable> struct SerializeTest : public ::testing::Test {
typedef typename Serializable::RepresentationType Representation;
};

TYPED_TEST_CASE_P(SerializeTest);

TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_json) {
TypeParam model_and_rep;
using X = typename TypeParam::RepresentationType;
const X original = model_and_rep.create();

// Serialize it
std::ostringstream os;
{
cereal::JSONOutputArchive oarchive(os);
oarchive(original);
}
// Deserialize it.
std::istringstream is(os.str());
X deserialized;
{
cereal::JSONInputArchive iarchive(is);
iarchive(deserialized);
}
// Make sure the original and deserialized representations are
// equivalent.
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
// Reserialize the deserialized object
std::ostringstream os_again;
{
cereal::JSONOutputArchive oarchive(os_again);
oarchive(deserialized);
}
// And make sure the serialized strings are the same,
EXPECT_EQ(os_again.str(), os.str());
}

TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_binary) {
TypeParam model_and_rep;
using X = typename TypeParam::RepresentationType;
const X original = model_and_rep.create();

// Serialize it
std::ostringstream os;
{
cereal::BinaryOutputArchive oarchive(os);
oarchive(original);
}
// Deserialize it.
std::istringstream is(os.str());
X deserialized;
{
cereal::BinaryInputArchive iarchive(is);
iarchive(deserialized);
}
// Make sure the original and deserialized representations are
// equivalent.
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
// Reserialize the deserialized object
std::ostringstream os_again;
{
cereal::BinaryOutputArchive oarchive(os_again);
oarchive(deserialized);
}
// And make sure the serialized strings are the same,
EXPECT_EQ(os_again.str(), os.str());
}
}