testSerializationBase.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/inference/Key.h>
20 
22 #include <gtsam/base/Matrix.h>
24 #include <gtsam/base/Vector.h>
25 #include <gtsam/base/FastList.h>
26 #include <gtsam/base/FastMap.h>
27 #include <gtsam/base/FastSet.h>
28 #include <gtsam/base/FastVector.h>
29 
33 
34 using namespace std;
35 using namespace gtsam;
36 using namespace gtsam::serializationTestHelpers;
37 
38 Vector v1 = Vector2(1.0, 2.0);
39 Vector v2 = Vector2(3.0, 4.0);
40 Vector v3 = Vector2(5.0, 6.0);
41 
42 /* ************************************************************************* */
43 TEST (Serialization, FastList) {
44  FastList<Vector> list;
45  list.push_back(v1);
46  list.push_back(v2);
47  list.push_back(v3);
48 
49  EXPECT(equality(list));
50  EXPECT(equalityXML(list));
51  EXPECT(equalityBinary(list));
52 }
53 
54 /* ************************************************************************* */
55 TEST (Serialization, FastMap) {
57  map.insert(make_pair(1, v1));
58  map.insert(make_pair(2, v2));
59  map.insert(make_pair(3, v3));
60 
61  EXPECT(equality(map));
62  EXPECT(equalityXML(map));
63  EXPECT(equalityBinary(map));
64 }
65 
66 /* ************************************************************************* */
67 TEST (Serialization, FastSet) {
68  KeySet set;
69  set.insert(1);
70  set.insert(2);
71  set.insert(3);
72 
74  EXPECT(equalityXML(set));
75  EXPECT(equalityBinary(set));
76 }
77 
78 /* ************************************************************************* */
79 TEST (Serialization, FastVector) {
80  FastVector<Vector> vector;
81  vector.push_back(v1);
82  vector.push_back(v2);
83  vector.push_back(v3);
84 
85  EXPECT(equality(vector));
86  EXPECT(equalityXML(vector));
87  EXPECT(equalityBinary(vector));
88 }
89 
90 /* ************************************************************************* */
91 TEST (Serialization, matrix_vector) {
92  EXPECT(equality<Vector>((Vector(4) << 1.0, 2.0, 3.0, 4.0).finished()));
93  EXPECT(equality<Vector2>(Vector2(1.0, 2.0)));
94  EXPECT(equality<Vector3>(Vector3(1.0, 2.0, 3.0)));
95  EXPECT(equality<Vector6>((Vector6() << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0).finished()));
96  EXPECT(equality<Matrix>((Matrix(2, 2) << 1.0, 2.0, 3.0, 4.0).finished()));
97 
98  EXPECT(equalityXML<Vector>((Vector(4) << 1.0, 2.0, 3.0, 4.0).finished()));
99  EXPECT(equalityXML<Vector2>(Vector2(1.0, 2.0)));
100  EXPECT(equalityXML<Vector3>(Vector3(1.0, 2.0, 3.0)));
101  EXPECT(equalityXML<Vector6>((Vector6() << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0).finished()));
102  EXPECT(equalityXML<Matrix>((Matrix(2, 2) << 1.0, 2.0, 3.0, 4.0).finished()));
103 
104  EXPECT(equalityBinary<Vector>((Vector(4) << 1.0, 2.0, 3.0, 4.0).finished()));
105  EXPECT(equalityBinary<Vector2>(Vector2(1.0, 2.0)));
106  EXPECT(equalityBinary<Vector3>(Vector3(1.0, 2.0, 3.0)));
107  EXPECT(equalityBinary<Vector6>((Vector6() << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0).finished()));
108  EXPECT(equalityBinary<Matrix>((Matrix(2, 2) << 1.0, 2.0, 3.0, 4.0).finished()));
109 }
110 
111 /* ************************************************************************* */
112 TEST (Serialization, ConcurrentMap) {
113 
115 
116  map.insert(make_pair(1, "apple"));
117  map.insert(make_pair(2, "banana"));
118 
119  std::string binaryPath = "saved_map.dat";
120  try {
121  std::ofstream outputStream(binaryPath);
122  boost::archive::binary_oarchive outputArchive(outputStream);
123  outputArchive << map;
124  } catch(...) {
125  EXPECT(false);
126  }
127 
128  // Verify that the existing map contents are replaced by the archive data
130  mapFromDisk.insert(make_pair(3, "clam"));
131  EXPECT(mapFromDisk.exists(3));
132  try {
133  std::ifstream ifs(binaryPath);
134  boost::archive::binary_iarchive inputArchive(ifs);
135  inputArchive >> mapFromDisk;
136  } catch(...) {
137  EXPECT(false);
138  }
139  EXPECT(mapFromDisk.exists(1));
140  EXPECT(mapFromDisk.exists(2));
141  EXPECT(!mapFromDisk.exists(3));
142 }
143 
144 /* ************************************************************************* */
145 
146 
147 /* ************************************************************************* */
148 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
149 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::ConcurrentMap
Definition: ConcurrentMap.h:69
MatrixSerialization.h
Serialization for matrices.
Vector.h
typedef and functions to augment Eigen's VectorXd
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
gtsam::Vector2
Eigen::Vector2d Vector2
Definition: Vector.h:42
TestHarness.h
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Matrix.h
typedef and functions to augment Eigen's MatrixXd
ConcurrentMap.h
gtsam::FastMap
Definition: FastMap.h:39
gtsam::FastSet
Definition: FastSet.h:51
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
gtsam::ConcurrentMap::exists
bool exists(const KEY &e) const
Definition: ConcurrentMap.h:89
Key.h
ceres::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Definition: gtsam/3rdparty/ceres/eigen.h:42
FastSet.h
A thin wrapper around std::set that uses boost's fast_pool_allocator.
main
int main()
Definition: testSerializationBase.cpp:148
gtsam::FastList
Definition: FastList.h:43
serializationTestHelpers.h
TestResult
Definition: TestResult.h:26
std_optional_serialization.h
set
void set(Container &c, Position position, const Value &value)
Definition: stdlist_overload.cpp:37
gtsam::equality
bool equality(const Errors &actual, const Errors &expected, double tol)
Definition: Errors.cpp:52
gtsam
traits
Definition: SFMdata.h:40
std
Definition: BFloat16.h:88
v2
Vector v2
Definition: testSerializationBase.cpp:39
TEST
TEST(Serialization, FastList)
Definition: testSerializationBase.cpp:43
FastList.h
A thin wrapper around std::list that uses boost's fast_pool_allocator.
v3
Vector v3
Definition: testSerializationBase.cpp:40
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
v1
Vector v1
Definition: testSerializationBase.cpp:38
FastMap.h
A thin wrapper around std::map that uses boost's fast_pool_allocator.


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:41:19