Skip to content

Commit 1b89597

Browse files
Move code shared between test into own file
1 parent 9b3e19c commit 1b89597

5 files changed

Lines changed: 116 additions & 55 deletions

File tree

unit_test/general/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_executable(unittest_general
2+
allocate_optimizer.cpp allocate_optimizer.h
23
graph_operations.cpp
34
clear_and_redo.cpp
45
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// g2o - General Graph Optimization
2+
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright notice,
10+
// this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above copyright
12+
// notice, this list of conditions and the following disclaimer in the
13+
// documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
#include "g2o/core/block_solver.h"
28+
#include "g2o/core/optimization_algorithm_gauss_newton.h"
29+
#include "g2o/solvers/eigen/linear_solver_eigen.h"
30+
31+
32+
namespace g2o {
33+
namespace internal {
34+
35+
typedef g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> > SlamBlockSolver;
36+
typedef g2o::LinearSolverEigen<SlamBlockSolver::PoseMatrixType> SlamLinearSolver;
37+
38+
g2o::SparseOptimizer* createOptimizerForTests() {
39+
// Initialize the SparseOptimizer
40+
g2o::SparseOptimizer* mOptimizer = new g2o::SparseOptimizer();
41+
auto linearSolver = g2o::make_unique<SlamLinearSolver>();
42+
linearSolver->setBlockOrdering(false);
43+
auto blockSolver = g2o::make_unique<SlamBlockSolver>(std::move(linearSolver));
44+
mOptimizer->setAlgorithm(new g2o::OptimizationAlgorithmGaussNewton(std::move(blockSolver)));
45+
return mOptimizer;
46+
}
47+
48+
} // namespace internal
49+
} // namespace g2o
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// g2o - General Graph Optimization
2+
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright notice,
10+
// this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above copyright
12+
// notice, this list of conditions and the following disclaimer in the
13+
// documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
#ifndef G2O_TEST_ALLOCATE_OPTIMIZER_H
28+
#define G2O_TEST_ALLOCATE_OPTIMIZER_H
29+
30+
namespace g2o {
31+
32+
class SparseOptimizer;
33+
34+
namespace internal {
35+
36+
g2o::SparseOptimizer* createOptimizerForTests();
37+
38+
} // namespace internal
39+
} // namespace g2o
40+
41+
#endif

unit_test/general/clear_and_redo.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,51 @@
2424
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

27-
#include "gtest/gtest.h"
28-
29-
#include "g2o/core/block_solver.h"
30-
#include "g2o/core/optimization_algorithm_gauss_newton.h"
31-
#include "g2o/types/slam3d/types_slam3d.h"
32-
#include "g2o/solvers/eigen/linear_solver_eigen.h"
27+
#include "allocate_optimizer.h"
28+
#include "g2o/core/sparse_optimizer.h"
3329
#include "g2o/core/sparse_optimizer_terminate_action.h"
30+
#include "g2o/types/slam3d/types_slam3d.h"
31+
#include "gtest/gtest.h"
3432

35-
typedef g2o::BlockSolver< g2o::BlockSolverTraits<-1, -1> > SlamBlockSolver;
36-
typedef g2o::LinearSolverEigen<SlamBlockSolver::PoseMatrixType> SlamLinearSolver;
37-
38-
TEST(General, ClearAndRedo)
39-
{
33+
TEST(General, ClearAndRedo) {
4034
// Initialize the SparseOptimizer
41-
g2o::SparseOptimizer mOptimizer;
42-
auto linearSolver = g2o::make_unique<SlamLinearSolver>();
43-
linearSolver->setBlockOrdering(false);
44-
auto blockSolver = g2o::make_unique<SlamBlockSolver>(std::move(linearSolver));
45-
mOptimizer.setAlgorithm(new g2o::OptimizationAlgorithmGaussNewton(std::move(blockSolver)));
35+
std::unique_ptr<g2o::SparseOptimizer> mOptimizerPtr;
36+
mOptimizerPtr.reset(g2o::internal::createOptimizerForTests());
37+
g2o::SparseOptimizer& mOptimizer = *mOptimizerPtr;
4638

4739
// Set the default terminate action
4840
g2o::SparseOptimizerTerminateAction* terminateAction = new g2o::SparseOptimizerTerminateAction;
4941
mOptimizer.addPostIterationAction(terminateAction);
5042

51-
for(int i = 0; i < 2; i++)
52-
{
43+
for (int i = 0; i < 2; i++) {
5344
// Add vertices
5445
g2o::VertexSE3* v0 = new g2o::VertexSE3;
55-
v0->setEstimate(Eigen::Transform<number_t,3,1>(Eigen::Translation<number_t, 3>(0,0,0)));
46+
v0->setEstimate(Eigen::Transform<number_t, 3, 1>(Eigen::Translation<number_t, 3>(0, 0, 0)));
5647
v0->setId(0);
5748
mOptimizer.addVertex(v0);
5849

5950
g2o::VertexSE3* v1 = new g2o::VertexSE3;
60-
v1->setEstimate(Eigen::Transform<number_t,3,1>(Eigen::Translation<number_t, 3>(0,0,0)));
51+
v1->setEstimate(Eigen::Transform<number_t, 3, 1>(Eigen::Translation<number_t, 3>(0, 0, 0)));
6152
v1->setId(1);
6253
mOptimizer.addVertex(v1);
6354

6455
g2o::VertexSE3* v2 = new g2o::VertexSE3;
65-
v2->setEstimate(Eigen::Transform<number_t,3,1>(Eigen::Translation<number_t, 3>(0,0,0)));
56+
v2->setEstimate(Eigen::Transform<number_t, 3, 1>(Eigen::Translation<number_t, 3>(0, 0, 0)));
6657
v2->setId(2);
6758
mOptimizer.addVertex(v2);
6859

6960
// Add edges
7061
g2o::EdgeSE3* e1 = new g2o::EdgeSE3();
7162
e1->vertices()[0] = mOptimizer.vertex(0);
7263
e1->vertices()[1] = mOptimizer.vertex(1);
73-
e1->setMeasurement(g2o::Isometry3(Eigen::Translation<number_t, 3>(1,0,0)));
64+
e1->setMeasurement(g2o::Isometry3(Eigen::Translation<number_t, 3>(1, 0, 0)));
7465
e1->setInformation(g2o::MatrixN<6>::Identity());
7566
mOptimizer.addEdge(e1);
7667

7768
g2o::EdgeSE3* e2 = new g2o::EdgeSE3();
7869
e2->vertices()[0] = mOptimizer.vertex(1);
7970
e2->vertices()[1] = mOptimizer.vertex(2);
80-
e2->setMeasurement(g2o::Isometry3(Eigen::Translation<number_t, 3>(0,1,0)));
71+
e2->setMeasurement(g2o::Isometry3(Eigen::Translation<number_t, 3>(0, 1, 0)));
8172
e2->setInformation(g2o::MatrixN<6>::Identity());
8273
mOptimizer.addEdge(e2);
8374

@@ -90,14 +81,13 @@ TEST(General, ClearAndRedo)
9081

9182
v0->setFixed(true);
9283

93-
//mOptimizer.setVerbose(true);
84+
// mOptimizer.setVerbose(true);
9485
mOptimizer.initializeOptimization();
9586
mOptimizer.computeInitialGuess();
9687
mOptimizer.computeActiveErrors();
9788
int iter = mOptimizer.optimize(10);
98-
if (iter <= 0)
99-
{
100-
ADD_FAILURE();
89+
if (iter <= 0) {
90+
ADD_FAILURE() << "Optimization did not happen";
10191
} else {
10292
SUCCEED();
10393
}

unit_test/general/graph_operations.cpp

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,24 @@
2626

2727
#include <numeric>
2828

29-
#include "g2o/core/block_solver.h"
29+
#include "allocate_optimizer.h"
3030
#include "g2o/core/factory.h"
31-
#include "g2o/core/optimization_algorithm_gauss_newton.h"
32-
#include "g2o/solvers/eigen/linear_solver_eigen.h"
31+
#include "g2o/core/sparse_optimizer.h"
3332
#include "g2o/stuff/string_tools.h"
3433
#include "g2o/types/slam2d/types_slam2d.h"
35-
#include "g2o/types/slam3d/types_slam3d.h"
3634
#include "gmock/gmock.h"
3735
#include "gtest/gtest.h"
3836

39-
typedef g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1>> SlamBlockSolver;
40-
typedef g2o::LinearSolverEigen<SlamBlockSolver::PoseMatrixType> SlamLinearSolver;
41-
4237
G2O_USE_TYPE_GROUP(slam2d);
43-
G2O_USE_TYPE_GROUP(slam3d);
44-
45-
static g2o::SparseOptimizer* createOptimizer() {
46-
// Initialize the SparseOptimizer
47-
g2o::SparseOptimizer* mOptimizer = new g2o::SparseOptimizer();
48-
auto linearSolver = g2o::make_unique<SlamLinearSolver>();
49-
linearSolver->setBlockOrdering(false);
50-
auto blockSolver = g2o::make_unique<SlamBlockSolver>(std::move(linearSolver));
51-
mOptimizer->setAlgorithm(new g2o::OptimizationAlgorithmGaussNewton(std::move(blockSolver)));
52-
return mOptimizer;
53-
}
5438

5539
TEST(General, BinaryEdgeConstructor) {
56-
g2o::EdgeSE3 e1;
57-
ASSERT_EQ(NULL, e1.vertices()[0]);
58-
ASSERT_EQ(NULL, e1.vertices()[1]);
59-
6040
g2o::EdgeSE2 e2;
61-
ASSERT_EQ(NULL, e2.vertices()[0]);
62-
ASSERT_EQ(NULL, e2.vertices()[1]);
41+
ASSERT_EQ(nullptr, e2.vertices()[0]);
42+
ASSERT_EQ(nullptr, e2.vertices()[1]);
6343
}
6444

6545
TEST(General, GraphAddVertex) {
66-
g2o::SparseOptimizer* optimizer = createOptimizer();
46+
g2o::SparseOptimizer* optimizer = g2o::internal::createOptimizerForTests();
6747

6848
g2o::VertexSE2* v1 = new g2o::VertexSE2();
6949
v1->setId(0);
@@ -78,15 +58,15 @@ TEST(General, GraphAddVertex) {
7858
v2->setId(0);
7959
ASSERT_FALSE(optimizer->addVertex(v2));
8060
ASSERT_EQ(size_t(1), optimizer->vertices().size());
81-
ASSERT_EQ(NULL, v2->graph());
61+
ASSERT_EQ(nullptr, v2->graph());
8262
delete v2;
8363
}
8464

8565
delete optimizer;
8666
}
8767

8868
TEST(General, GraphAddEdge) {
89-
g2o::SparseOptimizer* optimizer = createOptimizer();
69+
g2o::SparseOptimizer* optimizer = g2o::internal::createOptimizerForTests();
9070

9171
g2o::VertexSE2* v1 = new g2o::VertexSE2();
9272
v1->setId(0);
@@ -129,7 +109,7 @@ TEST(General, GraphAddEdge) {
129109
class GeneralGraphLoadSave : public ::testing::Test {
130110
protected:
131111
void SetUp() override {
132-
optimizer.reset(createOptimizer());
112+
optimizer.reset(g2o::internal::createOptimizerForTests());
133113

134114
// Add vertices
135115
for (int i = 0; i < numVertices; ++i) {

0 commit comments

Comments
 (0)