Main MRPT website > C++ reference for MRPT 1.9.9
base/src/utils/CSerializable_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-2017, 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 
15 #include <mrpt/random.h>
16 #include <mrpt/math/ops_vectors.h> // to serialize vectors
17 #include <mrpt/system/filesystem.h>
18 #include <mrpt/poses.h> // to test their serialization
19 #include <gtest/gtest.h>
20 
21 using namespace mrpt;
22 using namespace mrpt::utils;
23 using namespace mrpt::math;
24 using namespace mrpt::poses;
25 using namespace std;
26 
27 // Defined in tests/test_main.cpp
28 namespace mrpt
29 {
30 namespace utils
31 {
33 }
34 }
35 
36 // Load data from constant file and check for exact match.
37 TEST(SerializeTestBase, LoadDemoFile)
38 {
39  struct TRegs
40  {
41  uint8_t v1;
42  int8_t v2;
43  uint16_t v3;
44  int16_t v4;
45  uint32_t v5;
46  int32_t v6;
47  uint64_t v7;
48  int64_t v8;
49  std::string v9;
50  };
51 
52  // Reference data:
53  TRegs R;
54  R.v1 = 8;
55  R.v2 = -3;
56  R.v3 = 781;
57  R.v4 = -888;
58  R.v5 = 100000;
59  R.v6 = -100000;
60  R.v7 = 555666777;
61  R.v8 = -555666777;
62  R.v9 = "an example test";
63 
64  // Loaded data to compare with reference:
65  TRegs L;
66  const string fil =
67  MRPT_GLOBAL_UNITTEST_SRC_DIR + string("/tests/serialize_test_data.bin");
68 
69  if (!mrpt::system::fileExists(fil))
70  {
71  cerr << "WARNING: Skipping test due to missing file: " << fil << "\n";
72  }
73  else
74  {
75  CFileInputStream gg(fil);
76  gg >> L.v1 >> L.v2 >> L.v3 >> L.v4 >> L.v5 >> L.v6 >> L.v7 >> L.v8 >>
77  L.v9;
78 
79  EXPECT_EQ(R.v1, L.v1);
80  EXPECT_EQ(R.v2, L.v2);
81  EXPECT_EQ(R.v3, L.v3);
82  EXPECT_EQ(R.v4, L.v4);
83  EXPECT_EQ(R.v5, L.v5);
84  EXPECT_EQ(R.v6, L.v6);
85  EXPECT_EQ(R.v7, L.v7);
86  EXPECT_EQ(R.v8, L.v8);
87  EXPECT_EQ(R.v9, L.v9);
88  }
89 }
90 
92  // Misc:
95  // Poses:
97  // Others:
99 
100 // Create a set of classes, then serialize and deserialize to test possible
101 // bugs:
102 TEST(SerializeTestBase, WriteReadToMem)
103 {
104  for (size_t i = 0; i < sizeof(lstClasses) / sizeof(lstClasses[0]); i++)
105  {
106  try
107  {
108  CMemoryStream buf;
109  {
110  CSerializable* o =
111  static_cast<CSerializable*>(lstClasses[i]->createObject());
112  buf << *o;
113  delete o;
114  }
115 
116  CSerializable::Ptr recons;
117  buf.Seek(0);
118  buf >> recons;
119  }
120  catch (std::exception& e)
121  {
122  GTEST_FAIL() << "Exception during serialization test for class '"
123  << lstClasses[i]->className << "':\n"
124  << e.what() << endl;
125  }
126  }
127 }
128 
129 // Create a set of classes, then test that copy operators "work" (doesn't crash)
130 TEST(SerializeTestBase, CopyOperator)
131 {
132  for (size_t i = 0; i < sizeof(lstClasses) / sizeof(lstClasses[0]); i++)
133  {
134  try
135  {
136  CSerializable* o1 =
137  static_cast<CSerializable*>(lstClasses[i]->createObject());
138  CSerializable* o2 =
139  static_cast<CSerializable*>(lstClasses[i]->createObject());
140  *o2 = *o1;
141  delete o1;
142  delete o2;
143  }
144  catch (std::exception& e)
145  {
146  GTEST_FAIL() << "Exception during copy operator test for class '"
147  << lstClasses[i]->className << "':\n"
148  << e.what() << endl;
149  }
150  }
151 }
152 
153 // Create a set of classes, then serialize and deserialize to test possible
154 // bugs:
155 TEST(SerializeTestBase, CArray)
156 {
157  try
158  {
159  CMemoryStream buf;
161  for (CArrayDouble<5>::Index i = 0; i < a.size(); i++) a[i] = i + 10;
162 
163  buf << a;
164  buf.Seek(0);
165  buf >> b;
166 
167  EXPECT_TRUE(a == b);
168  }
169  catch (std::exception& e)
170  {
171  GTEST_FAIL() << "Exception:\n" << e.what() << endl;
172  }
173 }
174 
175 // Serialize and deserialize complex STL types
176 TEST(SerializeTestBase, STL_serialization)
177 {
178  try
179  {
180  // std::vector<>
181  {
182  CMemoryStream buf;
183 
184  std::vector<double> a, b;
185  a.resize(30);
186  for (size_t i = 0; i < a.size(); i++) a[i] = 50 - i;
187 
188  buf << a;
189  buf.Seek(0);
190  buf >> b;
191  EXPECT_TRUE(a == b);
192  }
193 
194  // std::list<...>
195  {
196  CMemoryStream buf;
197 
198  std::list<std::map<double, std::set<std::string>>> a, b;
199 
200  // Fill with random:
202  const size_t N = rng.drawUniform(10, 30);
203  for (size_t i = 0; i < N; i++)
204  {
205  std::map<double, std::set<std::string>> d;
206  const size_t M = rng.drawUniform(4, 9);
207  for (size_t j = 0; j < M; j++)
208  {
209  std::set<std::string>& dd =
210  d[rng.drawGaussian1D_normalized()];
211  const size_t L = rng.drawUniform(2, 15);
212  for (size_t k = 0; k < L; k++)
213  dd.insert(
214  mrpt::format(
215  "%f", rng.drawGaussian1D_normalized()));
216  }
217  a.push_back(d);
218  }
219 
220  buf << a;
221  buf.Seek(0);
222  buf >> b;
223  EXPECT_TRUE(a == b);
224  }
225  }
226  catch (std::exception& e)
227  {
228  GTEST_FAIL() << "Exception:\n" << e.what() << endl;
229  }
230 }
231 
232 // Test casting of smart pointers:
233 TEST(SerializeTestBase, CastSmartPointers)
234 {
235  using namespace mrpt::poses;
236 
237  // Create:
238  CPose2D::Ptr p1 = mrpt::make_aligned_shared<CPose2D>();
239  // Upcast:
241  // Downcast:
243  // Copy:
245 
246  EXPECT_TRUE(IS_CLASS(p1, CPose2D));
247  EXPECT_TRUE(IS_CLASS(p2, CPose2D));
248  EXPECT_TRUE(IS_CLASS(p3, CPose2D));
249  EXPECT_TRUE(IS_CLASS(p4, CPose2D));
250 }
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::string MRPT_GLOBAL_UNITTEST_SRC_DIR
unsigned __int16 uint16_t
Definition: rptypes.h:44
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
signed char int8_t
Definition: rptypes.h:40
STL namespace.
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
const mrpt::utils::TRuntimeClassId * lstClasses[]
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
std::shared_ptr< CPose2D > Ptr
Definition: CPose2D.h:43
Structure to hold the parameters of a pinhole stereo camera model.
Definition: TStereoCamera.h:25
unsigned char uint8_t
Definition: rptypes.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
mrpt::utils::CObject * createObject() const
Definition: CObject.cpp:93
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
__int16 int16_t
Definition: rptypes.h:43
__int64 int64_t
Definition: rptypes.h:49
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:27
TEST(SerializeTestBase, LoadDemoFile)
GLfloat GLfloat GLfloat GLfloat v3
Definition: glext.h:4109
GLubyte GLubyte b
Definition: glext.h:6279
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:48
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
__int32 int32_t
Definition: rptypes.h:46
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:4105
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
This CStream derived class allow using a file as a read-only, binary stream.
A structure that holds runtime class type information.
Definition: CObject.h:31
const char * className
Definition: CObject.h:34
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019