Main MRPT website > C++ reference for MRPT 1.9.9
stl_serialize_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
13 #include <mrpt/io/CMemoryStream.h>
14 #include <gtest/gtest.h>
15 #include <memory> // shared_ptr
16 
17 using namespace mrpt::serialization;
18 
19 TEST(Serialization, STL_stdvector)
20 {
21  std::vector<uint32_t> m2, m1{1, 2, 3};
22 
24  auto arch = mrpt::serialization::archiveFrom(f);
25  arch << m1;
26 
27  f.Seek(0);
28  arch >> m2;
29  EXPECT_EQ(m1, m2);
30 }
31 
32 TEST(Serialization, STL_stdmap)
33 {
34  std::map<uint32_t, uint8_t> m2, m1;
35 
36  m1[2] = 21;
37  m1[9] = 91;
38 
40  auto arch = mrpt::serialization::archiveFrom(f);
41  arch << m1;
42 
43  f.Seek(0);
44  arch >> m2;
45  EXPECT_EQ(m1, m2);
46 }
47 
48 TEST(Serialization, STL_complex_error_type)
49 {
50  std::map<double, std::array<uint8_t, 2>> v1;
51  std::map<double, std::array<int8_t, 2>> v2; // different type!
52 
53  v1[0.4].fill(2);
54 
56  auto arch = mrpt::serialization::archiveFrom(f);
57  arch << v1;
58 
59  // Trying to read to a different variable raises an exception:
60  f.Seek(0);
61  EXPECT_THROW(arch >> v2, std::exception);
62 }
63 
64 struct Foo
65 {
66  Foo(int i = 0) : m_i(i) {}
67  int m_i;
68 
70  bool operator==(const Foo& b) const { return m_i == b.m_i; }
71 };
73 {
74  a << f.m_i;
75  return a;
76 }
78 {
79  a >> f.m_i;
80  return a;
81 }
82 
83 TEST(Serialization, vector_custom_type)
84 {
85  std::vector<Foo> m2, m1{1, 2, 3};
86 
88  auto arch = mrpt::serialization::archiveFrom(f);
89  arch << m1;
90 
91  f.Seek(0);
92  arch >> m2;
93  EXPECT_EQ(m1, m2);
94 }
95 
96 TEST(Serialization, vector_shared_ptr)
97 {
98  std::vector<std::shared_ptr<Foo>> m2, m1;
99  m1.push_back(std::make_shared<Foo>(1));
100  m1.push_back(std::make_shared<Foo>(2));
101  m1.push_back(std::shared_ptr<Foo>()); // nullptr
102  m1.push_back(std::make_shared<Foo>(3));
103 
105  auto arch = mrpt::serialization::archiveFrom(f);
106  arch << m1;
107 
108  f.Seek(0);
109  arch >> m2;
110  EXPECT_EQ(m1.size(), m2.size());
111  for (auto i = 0U; i < m1.size(); i++)
112  {
113  if (!m1[i])
114  {
115  EXPECT_EQ(m1[i], m2[i]);
116  }
117  else
118  {
119  EXPECT_EQ(*m1[i], *m2[i]);
120  }
121  }
122 }
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:561
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:100
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...
GLubyte GLubyte b
Definition: glext.h:6279
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:291
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
GLfloat GLfloat v1
Definition: glext.h:4105
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:240
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019