Skip to content

Commit a6cdda1

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

File tree

2 files changed

+102
-80
lines changed

2 files changed

+102
-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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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>
29+
struct SerializeTest : public ::testing::Test {
30+
typedef typename Serializable::RepresentationType Representation;
31+
};
32+
33+
34+
TYPED_TEST_CASE_P(SerializeTest);
35+
36+
TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_json) {
37+
TypeParam model_and_rep;
38+
using X = typename TypeParam::RepresentationType;
39+
const X original = model_and_rep.create();
40+
41+
// Serialize it
42+
std::ostringstream os;
43+
{
44+
cereal::JSONOutputArchive oarchive(os);
45+
oarchive(original);
46+
}
47+
// Deserialize it.
48+
std::istringstream is(os.str());
49+
X deserialized;
50+
{
51+
cereal::JSONInputArchive iarchive(is);
52+
iarchive(deserialized);
53+
}
54+
// Make sure the original and deserialized representations are
55+
// equivalent.
56+
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
57+
// Reserialize the deserialized object
58+
std::ostringstream os_again;
59+
{
60+
cereal::JSONOutputArchive oarchive(os_again);
61+
oarchive(deserialized);
62+
}
63+
// And make sure the serialized strings are the same,
64+
EXPECT_EQ(os_again.str(), os.str());
65+
}
66+
67+
TYPED_TEST_P(SerializeTest, test_roundtrip_serialize_binary) {
68+
TypeParam model_and_rep;
69+
using X = typename TypeParam::RepresentationType;
70+
const X original = model_and_rep.create();
71+
72+
// Serialize it
73+
std::ostringstream os;
74+
{
75+
cereal::BinaryOutputArchive oarchive(os);
76+
oarchive(original);
77+
}
78+
// Deserialize it.
79+
std::istringstream is(os.str());
80+
X deserialized;
81+
{
82+
cereal::BinaryInputArchive iarchive(is);
83+
iarchive(deserialized);
84+
}
85+
// Make sure the original and deserialized representations are
86+
// equivalent.
87+
EXPECT_TRUE(model_and_rep.are_equal(original, deserialized));
88+
// Reserialize the deserialized object
89+
std::ostringstream os_again;
90+
{
91+
cereal::BinaryOutputArchive oarchive(os_again);
92+
oarchive(deserialized);
93+
}
94+
// And make sure the serialized strings are the same,
95+
EXPECT_EQ(os_again.str(), os.str());
96+
}
97+
98+
}

0 commit comments

Comments
 (0)