Skip to content

Commit 77770b9

Browse files
committed
Reorganize test_serialize in a way that it can be easily extended
to test other types.
1 parent 4fd5a10 commit 77770b9

File tree

2 files changed

+99
-80
lines changed

2 files changed

+99
-80
lines changed

tests/test_serialize.cc

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
#include <functional>
1616
#include <gtest/gtest.h>
1717

18+
#include "test_serialize.h"
1819
#include "test_utils.h"
1920

2021
#include "models/gp.h"
21-
#include <cereal/archives/json.hpp>
22-
#include <cereal/types/polymorphic.hpp>
2322

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

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

61-
template <typename X> struct SerializableType {
62-
using RepresentationType = X;
63-
virtual RepresentationType create() const {
64-
RepresentationType obj;
65-
return obj;
66-
}
67-
virtual bool are_equal(const X &lhs, const X &rhs) const {
68-
return lhs == rhs;
69-
};
70-
};
71-
7260
struct EmptyEigenVectorXd : public SerializableType<Eigen::VectorXd> {
7361
Eigen::VectorXd create() const override {
7462
Eigen::VectorXd x;
@@ -358,10 +346,8 @@ class FitGaussianProcess
358346
};
359347
};
360348

361-
template <typename Serializable>
362-
struct PolymorphicSerializeTest : public ::testing::Test {
363-
typedef typename Serializable::RepresentationType Representation;
364-
};
349+
REGISTER_TYPED_TEST_CASE_P(SerializeTest, test_roundtrip_serialize_json,
350+
test_roundtrip_serialize_binary);
365351

366352
typedef ::testing::Types<
367353
LDLT, EigenMatrix3d, SerializableType<Eigen::Matrix2i>, EmptyEigenVectorXd,
@@ -374,68 +360,6 @@ typedef ::testing::Types<
374360
FitLinearSerializablePointer, UnfitGaussianProcess, FitGaussianProcess>
375361
ToTest;
376362

377-
TYPED_TEST_CASE(PolymorphicSerializeTest, ToTest);
378-
379-
TYPED_TEST(PolymorphicSerializeTest, test_roundtrip_serialize_json) {
380-
TypeParam model_and_rep;
381-
using X = typename TypeParam::RepresentationType;
382-
const X original = model_and_rep.create();
383-
384-
// Serialize it
385-
std::ostringstream os;
386-
{
387-
cereal::JSONOutputArchive oarchive(os);
388-
oarchive(original);
389-
}
390-
// Deserialize it.
391-
std::istringstream is(os.str());
392-
X deserialized;
393-
{
394-
cereal::JSONInputArchive iarchive(is);
395-
iarchive(deserialized);
396-
}
397-
// Make sure the original and deserialized representations are
398-
// equivalent.
399-
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
400-
// Reserialize the deserialized object
401-
std::ostringstream os_again;
402-
{
403-
cereal::JSONOutputArchive oarchive(os_again);
404-
oarchive(deserialized);
405-
}
406-
// And make sure the serialized strings are the same,
407-
EXPECT_EQ(os_again.str(), os.str());
408-
}
409-
410-
TYPED_TEST(PolymorphicSerializeTest, test_roundtrip_serialize_binary) {
411-
TypeParam model_and_rep;
412-
using X = typename TypeParam::RepresentationType;
413-
const X original = model_and_rep.create();
414-
415-
// Serialize it
416-
std::ostringstream os;
417-
{
418-
cereal::BinaryOutputArchive oarchive(os);
419-
oarchive(original);
420-
}
421-
// Deserialize it.
422-
std::istringstream is(os.str());
423-
X deserialized;
424-
{
425-
cereal::BinaryInputArchive iarchive(is);
426-
iarchive(deserialized);
427-
}
428-
// Make sure the original and deserialized representations are
429-
// equivalent.
430-
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
431-
// Reserialize the deserialized object
432-
std::ostringstream os_again;
433-
{
434-
cereal::BinaryOutputArchive oarchive(os_again);
435-
oarchive(deserialized);
436-
}
437-
// And make sure the serialized strings are the same,
438-
EXPECT_EQ(os_again.str(), os.str());
439-
}
363+
INSTANTIATE_TYPED_TEST_CASE_P(Albatross, SerializeTest, ToTest);
440364

441365
} // namespace albatross

tests/test_serialize.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (C) 2018 Swift Navigation Inc.
3+
* Contact: Swift Navigation <dev@swiftnav.com>
4+
*
5+
* This source is subject to the license found in the file 'LICENSE' which must
6+
* be distributed together with this source. All other rights reserved.
7+
*
8+
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
9+
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
10+
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
11+
*/
12+
#include <cereal/archives/json.hpp>
13+
#include <cereal/types/polymorphic.hpp>
14+
15+
namespace albatross {
16+
17+
template <typename X> struct SerializableType {
18+
using RepresentationType = X;
19+
virtual RepresentationType create() const {
20+
RepresentationType obj;
21+
return obj;
22+
}
23+
virtual bool are_equal(const X &lhs, const X &rhs) const {
24+
return lhs == rhs;
25+
};
26+
};
27+
28+
template <typename Serializable> struct SerializeTest : public ::testing::Test {
29+
typedef typename Serializable::RepresentationType Representation;
30+
};
31+
32+
TYPED_TEST_CASE_P(SerializeTest);
33+
34+
TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_json) {
35+
TypeParam model_and_rep;
36+
using X = typename TypeParam::RepresentationType;
37+
const X original = model_and_rep.create();
38+
39+
// Serialize it
40+
std::ostringstream os;
41+
{
42+
cereal::JSONOutputArchive oarchive(os);
43+
oarchive(original);
44+
}
45+
// Deserialize it.
46+
std::istringstream is(os.str());
47+
X deserialized;
48+
{
49+
cereal::JSONInputArchive iarchive(is);
50+
iarchive(deserialized);
51+
}
52+
// Make sure the original and deserialized representations are
53+
// equivalent.
54+
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
55+
// Reserialize the deserialized object
56+
std::ostringstream os_again;
57+
{
58+
cereal::JSONOutputArchive oarchive(os_again);
59+
oarchive(deserialized);
60+
}
61+
// And make sure the serialized strings are the same,
62+
EXPECT_EQ(os_again.str(), os.str());
63+
}
64+
65+
TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_binary) {
66+
TypeParam model_and_rep;
67+
using X = typename TypeParam::RepresentationType;
68+
const X original = model_and_rep.create();
69+
70+
// Serialize it
71+
std::ostringstream os;
72+
{
73+
cereal::BinaryOutputArchive oarchive(os);
74+
oarchive(original);
75+
}
76+
// Deserialize it.
77+
std::istringstream is(os.str());
78+
X deserialized;
79+
{
80+
cereal::BinaryInputArchive iarchive(is);
81+
iarchive(deserialized);
82+
}
83+
// Make sure the original and deserialized representations are
84+
// equivalent.
85+
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
86+
// Reserialize the deserialized object
87+
std::ostringstream os_again;
88+
{
89+
cereal::BinaryOutputArchive oarchive(os_again);
90+
oarchive(deserialized);
91+
}
92+
// And make sure the serialized strings are the same,
93+
EXPECT_EQ(os_again.str(), os.str());
94+
}
95+
}

0 commit comments

Comments
 (0)