Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
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
37namespace tesseract_common
38{
46template <typename SerializableType>
47void testSerialization(const SerializableType& object, const std::string& typename_string)
48{
49 { // Archive program to XML file
50 std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
51 EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileXML<SerializableType>(object, file_path));
52
53 SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileXML<SerializableType>(file_path) };
54 EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
55 }
56
57 { // Archive program to binary file
58 std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
59 EXPECT_TRUE(tesseract_common::Serialization::toArchiveFileBinary<SerializableType>(object, file_path));
60
61 SerializableType nobject{ tesseract_common::Serialization::fromArchiveFileBinary<SerializableType>(file_path) };
62 EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
63 }
64
65 { // Archive program to string
66 std::string object_string =
67 tesseract_common::Serialization::toArchiveStringXML<SerializableType>(object, typename_string);
68 EXPECT_FALSE(object_string.empty());
69
70 SerializableType nobject{ tesseract_common::Serialization::fromArchiveStringXML<SerializableType>(object_string) };
71 EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
72 }
73}
74
82template <typename SerializableType>
83void testSerializationPtr(const std::shared_ptr<SerializableType>& object, const std::string& typename_string)
84{
85 { // Archive program to XML file
86 std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
88 tesseract_common::Serialization::toArchiveFileXML<std::shared_ptr<SerializableType>>(object, file_path));
89
90 auto nobject = tesseract_common::Serialization::fromArchiveFileXML<std::shared_ptr<SerializableType>>(file_path);
91 EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
92 }
93
94 { // Archive program to binary file
95 std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
97 tesseract_common::Serialization::toArchiveFileBinary<std::shared_ptr<SerializableType>>(object, file_path));
98
99 auto nobject = tesseract_common::Serialization::fromArchiveFileBinary<std::shared_ptr<SerializableType>>(file_path);
100 EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
101 }
102
103 { // Archive program to string
104 std::string object_string =
105 tesseract_common::Serialization::toArchiveStringXML<std::shared_ptr<SerializableType>>(object, typename_string);
106 EXPECT_FALSE(object_string.empty());
107
108 auto nobject =
109 tesseract_common::Serialization::fromArchiveStringXML<std::shared_ptr<SerializableType>>(object_string);
110 EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
111 }
112}
113
121template <typename SerializableTypeBase, typename SerializableTypeDerived>
122void testSerializationDerivedClass(const std::shared_ptr<SerializableTypeBase>& object,
123 const std::string& typename_string)
124{
125 { // Archive program to XML file
126 std::string file_path = tesseract_common::getTempPath() + typename_string + ".xml";
128 tesseract_common::Serialization::toArchiveFileXML<std::shared_ptr<SerializableTypeBase>>(object, file_path));
129
130 auto nobject =
131 tesseract_common::Serialization::fromArchiveFileXML<std::shared_ptr<SerializableTypeBase>>(file_path);
132 auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
133
134 auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
135 EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
136 }
137
138 { // Archive program to binary file
139 std::string file_path = tesseract_common::getTempPath() + typename_string + ".binary";
141 tesseract_common::Serialization::toArchiveFileBinary<std::shared_ptr<SerializableTypeBase>>(object, file_path));
142
143 auto nobject =
144 tesseract_common::Serialization::fromArchiveFileBinary<std::shared_ptr<SerializableTypeBase>>(file_path);
145 auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
146
147 auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
148 EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
149 }
150
151 { // Archive program to string
152 std::string object_string =
153 tesseract_common::Serialization::toArchiveStringXML<std::shared_ptr<SerializableTypeBase>>(object,
154 typename_string);
155 EXPECT_FALSE(object_string.empty());
156
157 auto nobject =
158 tesseract_common::Serialization::fromArchiveStringXML<std::shared_ptr<SerializableTypeBase>>(object_string);
159 auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);
160
161 auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
162 EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
163 }
164}
165} // namespace tesseract_common
166#endif // TESSERACT_COMMON_UTILS_H
EXPECT_FALSE(tesseract_collision::isContactAllowed("base_link", "link_2", acm, false))
EXPECT_TRUE(tesseract_common::isIdentical< tesseract_collision::ObjectPairKey >(pairs, check_pairs, false))
Common Tesseract Macros.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
Definition: allowed_collision_matrix.h:16
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:83
void testSerialization(const SerializableType &object, const std::string &typename_string)
Tests Boost serialization for a serializable type.
Definition: unit_test_utils.h:47
std::string getTempPath()
Get the host temp directory path.
Definition: utils.cpp:209
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:122
tesseract_common::fs::path file_path(__FILE__)
static bool toArchiveFileXML(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:112
static bool toArchiveFileBinary(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:137
Additional Boost serialization wrappers.
Common Tesseract Utility Functions.
tesseract_common::testSerialization< AllowedCollisionMatrix > * object
Definition: tesseract_common_serialization_unit.cpp:105