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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019