| 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 <algorithm> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "g2o/stuff/command_args.h" |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | class CommandArgsTest : public ::testing::Test { |
| 34 | protected: |
| 35 | void SetUp() override { |
| 36 | arg.param(argumentNames[0], valueInt, 5, "integer value"); |
| 37 | arg.param(argumentNames[1], valueDouble, 1e-6, "double value"); |
| 38 | arg.param(argumentNames[2], valueBool, false, "boolean"); |
| 39 | arg.param(argumentNames[3], valueVectorInt, std::vector<int>(), |
| 40 | "vector of int"); |
| 41 | arg.param(argumentNames[4], valueVectorDouble, std::vector<double>(), |
| 42 | "vector of double"); |
| 43 | arg.param(argumentNames[5], valueString, "", "string value"); |
| 44 | arg.param(argumentNames[6], valueFloat, 1e4, "float value"); |
| 45 | arg.paramLeftOver("input", valueStringLeftOverOptional, "", |
| 46 | "optional param", true); |
| 47 | } |
| 48 | |
| 49 | void call(const std::vector<std::string>& arguments) { |
| 50 | std::string testname = "unit_test"; |
| 51 | int argc = arguments.size() + 1; |
| 52 | char** argv = new char*[argc]; |
| 53 | for (int i = 0; i < argc; ++i) argv[i] = nullptr; |
| 54 | argv[0] = copyIntoArray(testname); |
| 55 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 56 | argv[i + 1] = copyIntoArray(arguments[i]); |
| 57 | } |
| 58 | arg.parseArgs(argc, argv, false); |
| 59 | for (int i = 0; i < argc; ++i) delete[] argv[i]; |
| 60 | delete[] argv; |
| 61 | } |
| 62 | |
| 63 | static char* copyIntoArray(const std::string& s) { |
| 64 | char* cstr = new char[s.size() + 1]; |
| 65 | std::copy(s.begin(), s.end(), cstr); |
| 66 | cstr[s.size()] = '\0'; |
| 67 | return cstr; |
| 68 | } |
| 69 | |
| 70 | g2o::CommandArgs arg; |
| 71 | |
| 72 | std::vector<std::string> argumentNames = {"i", "d", "b", "vi", |
| 73 | "vd", "string", "f"}; |
| 74 | |
| 75 | int valueInt; |
| 76 | double valueDouble; |
| 77 | float valueFloat; |
| 78 | bool valueBool; |
| 79 | std::string valueString; |
| 80 | std::vector<int> valueVectorInt; |
| 81 | std::vector<double> valueVectorDouble; |
| 82 | std::string valueStringLeftOverOptional; |
| 83 | }; |
| 84 | |
| 85 | TEST_F(CommandArgsTest, DefaultValues) { |
| 86 | std::vector<std::string> arguments; |
| 87 | call(arguments); |
| 88 | for (const auto& p : argumentNames) { |
| 89 | EXPECT_FALSE(arg.parsedParam(p)); |
| 90 | } |
| 91 | EXPECT_EQ(valueInt, 5); |
| 92 | EXPECT_DOUBLE_EQ(valueDouble, 1e-6); |
| 93 | EXPECT_FLOAT_EQ(valueFloat, 1e4); |
| 94 | EXPECT_FALSE(valueBool); |
| 95 | EXPECT_EQ(valueVectorInt.size(), 0); |
| 96 | EXPECT_EQ(valueVectorDouble.size(), 0); |
| 97 | EXPECT_EQ(valueString, ""); |
| 98 | EXPECT_EQ(valueStringLeftOverOptional, ""); |
| 99 | } |
| 100 | |
| 101 | TEST_F(CommandArgsTest, ParseValues) { |
| 102 | // clang-format off |
| 103 | std::vector<std::string> arguments = { |
| 104 | "-" + argumentNames[0], "42", |
| 105 | "-" + argumentNames[1], "0.5", |
| 106 | "-" + argumentNames[2], |
| 107 | "-" + argumentNames[3], "1,2,3,4", |
| 108 | "-" + argumentNames[4], "1.,20.,3.4", |
| 109 | "-" + argumentNames[5], "summary.txt", |
| 110 | "-" + argumentNames[6], "17.4", |
| 111 | "optional.txt" |
| 112 | }; |
| 113 | // clang-format on |
| 114 | call(arguments); |
| 115 | for (const auto& p : argumentNames) { |
| 116 | EXPECT_TRUE(arg.parsedParam(p)) << "failed param: " << p; |
| 117 | } |
| 118 | EXPECT_EQ(valueInt, 42); |
| 119 | EXPECT_DOUBLE_EQ(valueDouble, 0.5); |
| 120 | EXPECT_FLOAT_EQ(valueFloat, 17.4); |
| 121 | EXPECT_TRUE(valueBool); |
| 122 | EXPECT_EQ(valueVectorInt, std::vector<int>({1, 2, 3, 4})); |
| 123 | EXPECT_EQ(valueVectorDouble, std::vector<double>({1., 20., 3.4})); |
| 124 | EXPECT_EQ(valueString, "summary.txt"); |
| 125 | EXPECT_EQ(valueStringLeftOverOptional, "optional.txt"); |
| 126 | } |
| 127 | |
| 128 | namespace { |
| 129 | class CommandArgsTestAdapter : public g2o::CommandArgs { |
| 130 | public: |
| 131 | using g2o::CommandArgs::type2str; |
| 132 | }; |
| 133 | } // namespace |
| 134 | |
| 135 | class CommandArgsTypeToStr : public testing::TestWithParam<int> { |
| 136 | protected: |
| 137 | CommandArgsTestAdapter command_args; |
| 138 | }; |
| 139 | |
| 140 | TEST_P(CommandArgsTypeToStr, type2str) { |
| 141 | const int type = GetParam(); |
| 142 | const std::string type_str = command_args.type2str(type); |
| 143 | EXPECT_FALSE(type_str.empty()); |
| 144 | } |
| 145 | |
| 146 | INSTANTIATE_TEST_SUITE_P(CommandArgsTest, CommandArgsTypeToStr, |
| 147 | testing::Range(0, 7), |
| 148 | testing::PrintToStringParamName()); |