| 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 | // clang-format off |
| 28 | #include "g2o/config.h" |
| 29 | // clang-format on |
| 30 | |
| 31 | #include "g2o/core/optimization_algorithm.h" |
| 32 | #include "g2o/core/optimization_algorithm_factory.h" |
| 33 | #include "gmock/gmock.h" |
| 34 | |
| 35 | #if defined G2O_HAVE_CHOLMOD |
| 36 | G2O_USE_OPTIMIZATION_LIBRARY(cholmod); |
| 37 | #endif |
| 38 | #ifdef G2O_HAVE_CSPARSE |
| 39 | G2O_USE_OPTIMIZATION_LIBRARY(csparse); |
| 40 | #endif |
| 41 | |
| 42 | G2O_USE_OPTIMIZATION_LIBRARY(eigen); |
| 43 | G2O_USE_OPTIMIZATION_LIBRARY(dense); |
| 44 | G2O_USE_OPTIMIZATION_LIBRARY(pcg); |
| 45 | G2O_USE_OPTIMIZATION_LIBRARY(structure_only); |
| 46 | G2O_USE_OPTIMIZATION_LIBRARY(slam2d_linear); |
| 47 | |
| 48 | TEST(AlgorithmFactory, ContainsBasicSolvers) { |
| 49 | g2o::OptimizationAlgorithmFactory* factory = |
| 50 | g2o::OptimizationAlgorithmFactory::instance(); |
| 51 | |
| 52 | // collect all names |
| 53 | std::set<std::string> names; |
| 54 | for (auto& creator : factory->creatorList()) { |
| 55 | g2o::OptimizationAlgorithmProperty solverProperty; |
| 56 | names.insert(creator->property().name); |
| 57 | } |
| 58 | |
| 59 | std::vector<std::string> basicSolvers = {"gn_var", "lm_var", "dl_var"}; |
| 60 | ASSERT_THAT(names, testing::IsSupersetOf(basicSolvers)); |
| 61 | } |
| 62 | |
| 63 | TEST(AlgorithmFactory, AllocatingSolver) { |
| 64 | g2o::OptimizationAlgorithmFactory* factory = |
| 65 | g2o::OptimizationAlgorithmFactory::instance(); |
| 66 | |
| 67 | for (auto& creator : factory->creatorList()) { |
| 68 | g2o::OptimizationAlgorithmProperty solverProperty; |
| 69 | std::string name = creator->property().name; |
| 70 | |
| 71 | g2o::OptimizationAlgorithm* algo = factory->construct(name, solverProperty); |
| 72 | ASSERT_NE(nullptr, algo) << "Cannot allocate solver " << name; |
| 73 | delete algo; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | TEST(AlgorithmFactory, AllocatingInvalidReturnsNull) { |
| 78 | g2o::OptimizationAlgorithmFactory* factory = |
| 79 | g2o::OptimizationAlgorithmFactory::instance(); |
| 80 | |
| 81 | const std::string name("xyz_supper_solver"); |
| 82 | g2o::OptimizationAlgorithmProperty solverProperty; |
| 83 | g2o::OptimizationAlgorithm* algo = factory->construct(name, solverProperty); |
| 84 | ASSERT_EQ(nullptr, algo) << "Allocated a solver " << name; |
| 85 | delete algo; |
| 86 | } |
| 87 | |
| 88 | TEST(AlgorithmFactory, PrintSolverProperties) { |
| 89 | g2o::OptimizationAlgorithmFactory* factory = |
| 90 | g2o::OptimizationAlgorithmFactory::instance(); |
| 91 | |
| 92 | const std::string name("gn_var"); |
| 93 | g2o::OptimizationAlgorithmProperty solverProperty; |
| 94 | g2o::OptimizationAlgorithm* algo = factory->construct(name, solverProperty); |
| 95 | ASSERT_NE(nullptr, algo) << "Did not manager to allocate solver " << name; |
| 96 | |
| 97 | std::stringstream solverPropertyStream; |
| 98 | algo->printProperties(solverPropertyStream); |
| 99 | ASSERT_FALSE(solverPropertyStream.str().empty()); |
| 100 | |
| 101 | delete algo; |
| 102 | } |
| 103 | |
| 104 | TEST(AlgorithmFactory, ListsAllSolvers) { |
| 105 | g2o::OptimizationAlgorithmFactory* factory = |
| 106 | g2o::OptimizationAlgorithmFactory::instance(); |
| 107 | |
| 108 | // collect all names |
| 109 | std::set<std::string> names; |
| 110 | for (auto& creator : factory->creatorList()) { |
| 111 | g2o::OptimizationAlgorithmProperty solverProperty; |
| 112 | names.insert(creator->property().name); |
| 113 | } |
| 114 | |
| 115 | std::stringstream listSolverOutput; |
| 116 | factory->listSolvers(listSolverOutput); |
| 117 | |
| 118 | // read back the output |
| 119 | std::set<std::string> solversOnOutput; |
| 120 | std::stringstream line; |
| 121 | while (g2o::readLine(listSolverOutput, line) != -1) { |
| 122 | std::string token; |
| 123 | line >> token; |
| 124 | if (!token.empty()) solversOnOutput.insert(token); |
| 125 | } |
| 126 | ASSERT_THAT(solversOnOutput, testing::ElementsAreArray(names)); |
| 127 | } |