MRPT  2.0.1
stl_serialize_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/io/CMemoryStream.h>
16 #include <memory> // shared_ptr
17 
18 using namespace mrpt::serialization;
19 
20 TEST(Serialization, STL_stdvector)
21 {
22  std::vector<uint32_t> m2, m1{1, 2, 3};
23 
25  auto arch = mrpt::serialization::archiveFrom(f);
26  arch << m1;
27 
28  f.Seek(0);
29  arch >> m2;
30  EXPECT_EQ(m1, m2);
31 }
32 
33 TEST(Serialization, STL_stdmap)
34 {
35  std::map<uint32_t, uint8_t> m2, m1;
36 
37  m1[2] = 21;
38  m1[9] = 91;
39 
41  auto arch = mrpt::serialization::archiveFrom(f);
42  arch << m1;
43 
44  f.Seek(0);
45  arch >> m2;
46  EXPECT_EQ(m1, m2);
47 }
48 
49 TEST(Serialization, STL_complex_error_type)
50 {
51  std::map<double, std::array<uint8_t, 2>> v1;
52  std::map<double, std::array<int8_t, 2>> v2; // different type!
53 
54  v1[0.4].fill(2);
55 
57  auto arch = mrpt::serialization::archiveFrom(f);
58  arch << v1;
59 
60  // Trying to read to a different variable raises an exception:
61  f.Seek(0);
62  EXPECT_THROW(arch >> v2, std::exception);
63 }
64 
65 TEST(Serialization, optional_STL)
66 {
67  using map2array_t = std::map<double, std::array<uint8_t, 2>>;
68 
69  std::optional<map2array_t> v1a, v1b;
70  std::optional<map2array_t> v2a, v2b;
71 
72  // v1a: leave empty.
73  // v1b: assign value
74  v1b.emplace();
75  (*v1b)[0.4].fill(2);
76 
78  auto arch = mrpt::serialization::archiveFrom(f);
79  arch << v1a << v1b;
80 
81  f.Seek(0);
82  arch >> v2a >> v2b;
83  EXPECT_FALSE(v2a.has_value());
84  EXPECT_EQ(*v1b, *v2b);
85 }
86 
87 struct Foo
88 {
89  Foo(int i = 0) : m_i(i) {}
90  int m_i;
91 
93  bool operator==(const Foo& b) const { return m_i == b.m_i; }
94 };
96 {
97  a << f.m_i;
98  return a;
99 }
101 {
102  a >> f.m_i;
103  return a;
104 }
105 
106 TEST(Serialization, vector_custom_type)
107 {
108  std::vector<Foo> m2, m1{1, 2, 3};
109 
111  auto arch = mrpt::serialization::archiveFrom(f);
112  arch << m1;
113 
114  f.Seek(0);
115  arch >> m2;
116  EXPECT_EQ(m1, m2);
117 }
118 
119 TEST(Serialization, vector_shared_ptr)
120 {
121  std::vector<std::shared_ptr<Foo>> m2, m1;
122  m1.push_back(std::make_shared<Foo>(1));
123  m1.push_back(std::make_shared<Foo>(2));
124  m1.push_back(std::shared_ptr<Foo>()); // nullptr
125  m1.push_back(std::make_shared<Foo>(3));
126 
128  auto arch = mrpt::serialization::archiveFrom(f);
129  arch << m1;
130 
131  f.Seek(0);
132  arch >> m2;
133  EXPECT_EQ(m1.size(), m2.size());
134  for (auto i = 0U; i < m1.size(); i++)
135  {
136  if (!m1[i])
137  {
138  EXPECT_EQ(m1[i], m2[i]);
139  }
140  else
141  {
142  EXPECT_EQ(*m1[i], *m2[i]);
143  }
144  }
145 }
TEST(Serialization, STL_stdvector)
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
This CStream derived class allow using a memory buffer as a CStream.
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
Definition: TTypeName.h:104
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:250
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:199



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020