testStdOptionalSerialization.cpp
Go to the documentation of this file.
2 
3 #include <boost/archive/binary_iarchive.hpp>
4 #include <boost/archive/binary_oarchive.hpp>
5 #include <boost/archive/xml_iarchive.hpp>
6 #include <boost/archive/xml_oarchive.hpp>
7 
10 
11 using namespace gtsam;
12 
13 // A test to check that the serialization of std::optional works with boost
14 TEST(StdOptionalSerialization, SerializeIntOptional) {
15  // Create an optional
16  std::optional<int> opt(42);
17 
18  // Serialize it
19  std::stringstream ss;
20  boost::archive::text_oarchive oa(ss);
21  oa << opt;
22 
23  // Deserialize it
24  std::optional<int> opt2;
25  boost::archive::text_iarchive ia(ss);
26  ia >> opt2;
27 
28  // Check that it worked
29  EXPECT(opt2.has_value());
30  EXPECT(*opt2 == 42);
31 }
32 
33 // A test to check if we serialize an empty optional, it is deserialized as an
34 // empty optional
35 TEST(StdOptionalSerialization, SerializeEmptyOptional) {
36  // Create an optional
37  std::optional<int> opt;
38 
39  // Serialize it
40  std::stringstream ss;
41  boost::archive::text_oarchive oa(ss);
42  oa << opt;
43 
44  // Deserialize it
45  std::optional<int> opt2 = 43;
46  boost::archive::text_iarchive ia(ss);
47  ia >> opt2;
48 
49  // Check that it worked
50  EXPECT(!opt2.has_value());
51 }
52 
53 /* ************************************************************************* */
54 // Implement the equals trait for TestOptionalStruct
55 namespace gtsam {
56 // A struct which contains an optional
58 public:
59  std::optional<int> opt;
60  TestOptionalStruct() = default;
61  TestOptionalStruct(const int& opt)
62  : opt(opt) {}
63  // A copy constructor is needed for serialization
65  bool operator==(const TestOptionalStruct& other) const {
66  // check the values are equal
67  return *opt == *other.opt;
68  }
70  template <class Archive>
71  void serialize(Archive& ar, const unsigned int /*version*/) {
72  ar& BOOST_SERIALIZATION_NVP(opt);
73  }
74 };
75 
76 template <>
78  static bool Equals(const TestOptionalStruct& q1, const TestOptionalStruct& q2, double) {
79  // if both have values then compare the values
80  if (q1.opt.has_value() && q2.opt.has_value()) {
81  return *q1.opt == *q2.opt;
82  }
83  // otherwise check that both are empty
84  return q1.opt == q2.opt;
85  }
86 
87  static void Print(const TestOptionalStruct& m, const std::string& s = "") {
88  /* std::cout << s << "opt: " << m.opt << std::endl; */
89  if (m.opt.has_value()) {
90  std::cout << s << "opt: " << *m.opt << std::endl;
91  } else {
92  std::cout << s << "opt: empty" << std::endl;
93  }
94  }
95 };
96 } // namespace gtsam
97 
98 // Test for a struct with an initialized optional
99 TEST(StdOptionalSerialization, SerializTestOptionalStruct) {
100  TestOptionalStruct optStruct(10);
101  EXPECT(serializationTestHelpers::equalsObj(optStruct));
102  EXPECT(serializationTestHelpers::equalsXML(optStruct));
103  EXPECT(serializationTestHelpers::equalsBinary(optStruct));
104 }
105 
106 // Test for a struct with an uninitialized optional
107 TEST(StdOptionalSerialization, SerializTestOptionalStructUninitialized) {
108  TestOptionalStruct optStruct;
109  EXPECT(serializationTestHelpers::equalsObj(optStruct));
110  EXPECT(serializationTestHelpers::equalsXML(optStruct));
111  EXPECT(serializationTestHelpers::equalsBinary(optStruct));
112 }
113 
114 // Test for serialization of std::optional<TestOptionalStruct>
115 TEST(StdOptionalSerialization, SerializTestOptionalStructPointer) {
116  // Create an optional
117  std::optional<TestOptionalStruct> opt(TestOptionalStruct(42));
118 
119  // Serialize it
120  std::stringstream ss;
121  boost::archive::text_oarchive oa(ss);
122  oa << opt;
123 
124  // Deserialize it
125  std::optional<TestOptionalStruct> opt2;
126  boost::archive::text_iarchive ia(ss);
127  ia >> opt2;
128 
129  // Check that it worked
130  EXPECT(opt2.has_value());
131  EXPECT(*opt2 == TestOptionalStruct(42));
132 }
133 
134 // Test for serialization of std::optional<TestOptionalStruct*>
135 TEST(StdOptionalSerialization, SerializTestOptionalStructPointerPointer) {
136  // Create an optional
137  std::optional<TestOptionalStruct*> opt(new TestOptionalStruct(42));
138 
139  // Serialize it
140  std::stringstream ss;
141  boost::archive::text_oarchive oa(ss);
142  oa << opt;
143 
144  // Deserialize it
145  std::optional<TestOptionalStruct*> opt2;
146  boost::archive::text_iarchive ia(ss);
147  ia >> opt2;
148 
149  // Check that it worked
150  EXPECT(opt2.has_value());
151  EXPECT(**opt2 == TestOptionalStruct(42));
152 
153  delete (*opt);
154  delete (*opt2);
155 }
156 
157 int main() {
158  TestResult tr;
159  return TestRegistry::runAllTests(tr);
160 }
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::TestOptionalStruct::serialize
void serialize(Archive &ar, const unsigned int)
Definition: testStdOptionalSerialization.cpp:71
gtsam::TestOptionalStruct
Definition: testStdOptionalSerialization.cpp:57
gtsam::TestOptionalStruct::TestOptionalStruct
TestOptionalStruct(const int &opt)
Definition: testStdOptionalSerialization.cpp:61
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::traits< TestOptionalStruct >::Print
static void Print(const TestOptionalStruct &m, const std::string &s="")
Definition: testStdOptionalSerialization.cpp:87
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
gtsam::TestOptionalStruct::opt
std::optional< int > opt
Definition: testStdOptionalSerialization.cpp:59
ss
static std::stringstream ss
Definition: testBTree.cpp:31
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
serializationTestHelpers.h
gtsam::TestOptionalStruct::access
friend class boost::serialization::access
Definition: testStdOptionalSerialization.cpp:69
TestResult
Definition: TestResult.h:26
std_optional_serialization.h
gtsam
traits
Definition: SFMdata.h:40
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
gtsam::traits
Definition: Group.h:36
gtsam::TestOptionalStruct::TestOptionalStruct
TestOptionalStruct()=default
main
int main()
Definition: testStdOptionalSerialization.cpp:157
gtsam::traits< TestOptionalStruct >::Equals
static bool Equals(const TestOptionalStruct &q1, const TestOptionalStruct &q2, double)
Definition: testStdOptionalSerialization.cpp:78
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::TestOptionalStruct::operator==
bool operator==(const TestOptionalStruct &other) const
Definition: testStdOptionalSerialization.cpp:65


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:08:43