unit_test_utils.h
Go to the documentation of this file.
1 
25 #ifndef TESSERACT_COMMON_UNIT_TEST_UTILS_H
26 #define TESSERACT_COMMON_UNIT_TEST_UTILS_H
27 
30 #include <gtest/gtest.h>
31 #include <boost/serialization/shared_ptr.hpp>
33 
36 #include <tesseract_common/utils.h>
37 
38 namespace tesseract_common
39 {
47 template <typename SerializableType>
48 void testSerialization(const SerializableType& object, const std::string& typename_string)
49 {
50  { // Archive program to XML file
51  std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
52  EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileXML<SerializableType>(object, file_path));
53 
54  SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileXML<SerializableType>(file_path) };
55  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
56  }
57 
58  { // Archive program to XML file with name
59  std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
60  EXPECT_TRUE(
61  tesseract_common::Serialization::toArchiveFileXML<SerializableType>(object, file_path, typename_string));
62 
63  SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileXML<SerializableType>(file_path) };
64  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
65  }
66 
67  { // Archive program to binary file
68  std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
69  EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileBinary<SerializableType>(object, file_path));
70 
71  SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileBinary<SerializableType>(file_path) };
72  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
73  }
74 
75  { // Archive program to binary file with name
76  std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
77  EXPECT_TRUE(
78  tesseract_common::Serialization::toArchiveFileBinary<SerializableType>(object, file_path, typename_string));
79 
80  SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileBinary<SerializableType>(file_path) };
81  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
82  }
83 
84  { // Archive program to string
85  std::string object_string = tesseract_common::Serialization::toArchiveStringXML<SerializableType>(object);
86  EXPECT_FALSE(object_string.empty());
87 
88  SerializableType nobject{ tesseract_common::Serialization::fromArchiveStringXML<SerializableType>(object_string) };
89  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
90  }
91 
92  { // Archive program to string with name
93  std::string object_string =
94  tesseract_common::Serialization::toArchiveStringXML<SerializableType>(object, typename_string);
95  EXPECT_FALSE(object_string.empty());
96 
97  SerializableType nobject{ tesseract_common::Serialization::fromArchiveStringXML<SerializableType>(object_string) };
98  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
99  }
100 
101  { // Archive program to binary data
102  std::vector<std::uint8_t> object_data =
103  tesseract_common::Serialization::toArchiveBinaryData<SerializableType>(object);
104  EXPECT_FALSE(object_data.empty());
105 
106  SerializableType nobject{ tesseract_common::Serialization::fromArchiveBinaryData<SerializableType>(object_data) };
107  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
108  }
109 
110  { // Archive program to binary data with name
111  std::vector<std::uint8_t> object_data =
112  tesseract_common::Serialization::toArchiveBinaryData<SerializableType>(object, typename_string);
113  EXPECT_FALSE(object_data.empty());
114 
115  SerializableType nobject{ tesseract_common::Serialization::fromArchiveBinaryData<SerializableType>(object_data) };
116  EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
117  }
118 }
119 
127 template <typename SerializableType>
128 void testSerializationPtr(const std::shared_ptr<SerializableType>& object, const std::string& typename_string)
129 {
130  { // Archive program to XML file
131  std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
132  EXPECT_TRUE(
133  tesseract_common::Serialization::toArchiveFileXML<std::shared_ptr<SerializableType>>(object, file_path));
134 
135  auto nobject = tesseract_common::Serialization::fromArchiveFileXML<std::shared_ptr<SerializableType>>(file_path);
136  EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
137  }
138 
139  { // Archive program to binary file
140  std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
141  EXPECT_TRUE(
142  tesseract_common::Serialization::toArchiveFileBinary<std::shared_ptr<SerializableType>>(object, file_path));
143 
144  auto nobject = tesseract_common::Serialization::fromArchiveFileBinary<std::shared_ptr<SerializableType>>(file_path);
145  EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
146  }
147 
148  { // Archive program to string
149  std::string object_string =
150  tesseract_common::Serialization::toArchiveStringXML<std::shared_ptr<SerializableType>>(object, typename_string);
151  EXPECT_FALSE(object_string.empty());
152 
153  auto nobject =
154  tesseract_common::Serialization::fromArchiveStringXML<std::shared_ptr<SerializableType>>(object_string);
155  EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
156  }
157 
158  { // Archive program to binary data
159  std::vector<std::uint8_t> object_data =
160  tesseract_common::Serialization::toArchiveBinaryData<std::shared_ptr<SerializableType>>(object,
161  typename_string);
162  EXPECT_FALSE(object_data.empty());
163 
164  auto nobject =
165  tesseract_common::Serialization::fromArchiveBinaryData<std::shared_ptr<SerializableType>>(object_data);
166  EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
167  }
168 }
169 
177 template <typename SerializableTypeBase, typename SerializableTypeDerived>
178 void testSerializationDerivedClass(const std::shared_ptr<SerializableTypeBase>& object,
179  const std::string& typename_string)
180 {
181  { // Archive program to XML file
182  std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
183  EXPECT_TRUE(
184  tesseract_common::Serialization::toArchiveFileXML<std::shared_ptr<SerializableTypeBase>>(object, file_path));
185 
186  auto nobject =
187  tesseract_common::Serialization::fromArchiveFileXML<std::shared_ptr<SerializableTypeBase>>(file_path);
188  auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
189 
190  auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
191  EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
192  }
193 
194  { // Archive program to binary file
195  std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
196  EXPECT_TRUE(
197  tesseract_common::Serialization::toArchiveFileBinary<std::shared_ptr<SerializableTypeBase>>(object, file_path));
198 
199  auto nobject =
200  tesseract_common::Serialization::fromArchiveFileBinary<std::shared_ptr<SerializableTypeBase>>(file_path);
201  auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
202 
203  auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
204  EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
205  }
206 
207  { // Archive program to string
208  std::string object_string =
209  tesseract_common::Serialization::toArchiveStringXML<std::shared_ptr<SerializableTypeBase>>(object,
210  typename_string);
211  EXPECT_FALSE(object_string.empty());
212 
213  auto nobject =
214  tesseract_common::Serialization::fromArchiveStringXML<std::shared_ptr<SerializableTypeBase>>(object_string);
215  auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
216 
217  auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
218  EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
219  }
220 
221  { // Archive program to binary data
222  std::vector<std::uint8_t> object_data =
223  tesseract_common::Serialization::toArchiveBinaryData<std::shared_ptr<SerializableTypeBase>>(object,
224  typename_string);
225  EXPECT_FALSE(object_data.empty());
226 
227  auto nobject =
228  tesseract_common::Serialization::fromArchiveBinaryData<std::shared_ptr<SerializableTypeBase>>(object_data);
229  auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
230 
231  auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
232  EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
233  }
234 }
235 
243 template <typename SerializableTypeStored>
245  const std::string& typename_string)
246 {
247  { // Archive program to XML file
248  std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
249  EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileXML<tesseract_common::AnyPoly>(object, file_path));
250 
251  auto nobject = tesseract_common::Serialization::fromArchiveFileXML<tesseract_common::AnyPoly>(file_path);
252  const auto& nobject_stored = nobject.as<SerializableTypeStored>();
253 
254  auto object_stored = object.as<SerializableTypeStored>();
255  EXPECT_FALSE(*object_stored != *nobject_stored); // Using != because it call == for code coverage
256  }
257 
258  { // Archive program to binary file
259  std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
260  EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileBinary<tesseract_common::AnyPoly>(object, file_path));
261 
262  auto nobject = tesseract_common::Serialization::fromArchiveFileBinary<tesseract_common::AnyPoly>(file_path);
263  auto nobject_stored = nobject.as<SerializableTypeStored>();
264 
265  auto object_stored = object.as<SerializableTypeStored>();
266  EXPECT_FALSE(*object_stored != *nobject_stored); // Using != because it call == for code coverage
267  }
268 
269  { // Archive program to string
270  std::string object_string =
271  tesseract_common::Serialization::toArchiveStringXML<tesseract_common::AnyPoly>(object, typename_string);
272  EXPECT_FALSE(object_string.empty());
273 
274  auto nobject = tesseract_common::Serialization::fromArchiveStringXML<tesseract_common::AnyPoly>(object_string);
275  auto nobject_stored = nobject.as<SerializableTypeStored>();
276 
277  auto object_stored = object.as<SerializableTypeStored>();
278  EXPECT_FALSE(*object_stored != *nobject_stored); // Using != because it call == for code coverage
279  }
280 
281  { // Archive program to binary data
282  std::vector<std::uint8_t> object_data =
283  tesseract_common::Serialization::toArchiveBinaryData<tesseract_common::AnyPoly>(object, typename_string);
284  EXPECT_FALSE(object_data.empty());
285 
286  auto nobject = tesseract_common::Serialization::fromArchiveBinaryData<tesseract_common::AnyPoly>(object_data);
287  auto nobject_stored = nobject.as<SerializableTypeStored>();
288 
289  auto object_stored = object.as<SerializableTypeStored>();
290  EXPECT_FALSE(*object_stored != *nobject_stored); // Using != because it call == for code coverage
291  }
292 }
293 } // namespace tesseract_common
294 #endif // TESSERACT_COMMON_UTILS_H
tesseract_common::getTempPath
std::string getTempPath()
Get the host temp directory path.
Definition: utils.cpp:262
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::testSerialization
void testSerialization(const SerializableType &object, const std::string &typename_string)
Tests Boost serialization for a serializable type.
Definition: unit_test_utils.h:48
tesseract_common::testSerializationAnyPolyStoredSharedPtr
void testSerializationAnyPolyStoredSharedPtr(const tesseract_common::AnyPoly &object, const std::string &typename_string)
Tests Boost serialization for a serializable any poly type.
Definition: unit_test_utils.h:244
any_poly.h
tesseract_common::Serialization::toArchiveFileXML
static bool toArchiveFileXML(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:120
macros.h
Common Tesseract Macros.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::testSerializationPtr
void testSerializationPtr(const std::shared_ptr< SerializableType > &object, const std::string &typename_string)
Tests Boost serialization of shared pointer for a serializable type.
Definition: unit_test_utils.h:128
utils.h
Common Tesseract Utility Functions.
tesseract_common::AnyPoly
Definition: any_poly.h:113
tesseract_common::testSerializationDerivedClass
void testSerializationDerivedClass(const std::shared_ptr< SerializableTypeBase > &object, const std::string &typename_string)
Tests Boost serialization for a serializable derived type.
Definition: unit_test_utils.h:178
tesseract_common::Serialization::toArchiveFileBinary
static bool toArchiveFileBinary(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:145
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
serialization.h
Additional Boost serialization wrappers.


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40