| 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 <gtest/gtest.h> |
| 28 | |
| 29 | #include <cstdlib> |
| 30 | #include <sstream> |
| 31 | |
| 32 | #include "g2o/stuff/string_tools.h" |
| 33 | |
| 34 | TEST(Stuff, Trim) { |
| 35 | ASSERT_EQ("abc", g2o::trim("abc ")); |
| 36 | ASSERT_EQ("abc", g2o::trim(" abc")); |
| 37 | ASSERT_EQ("abc", g2o::trim(" abc ")); |
| 38 | ASSERT_EQ("abc", g2o::trim("abc")); |
| 39 | ASSERT_EQ("", g2o::trim(" ")); |
| 40 | ASSERT_EQ("abc ", g2o::trimLeft("abc ")); |
| 41 | ASSERT_EQ("abc", g2o::trimLeft(" abc")); |
| 42 | ASSERT_EQ("abc ", g2o::trimLeft(" abc ")); |
| 43 | ASSERT_EQ("abc", g2o::trimLeft("abc")); |
| 44 | ASSERT_EQ("", g2o::trimLeft(" ")); |
| 45 | ASSERT_EQ("abc", g2o::trimRight("abc ")); |
| 46 | ASSERT_EQ(" abc", g2o::trimRight(" abc")); |
| 47 | ASSERT_EQ(" abc", g2o::trimRight(" abc ")); |
| 48 | ASSERT_EQ("abc", g2o::trimRight("abc")); |
| 49 | ASSERT_EQ("", g2o::trimRight(" ")); |
| 50 | } |
| 51 | |
| 52 | TEST(Stuff, StrToLower) { |
| 53 | ASSERT_EQ("abc", g2o::strToLower("abc")); |
| 54 | ASSERT_EQ("abc", g2o::strToLower("Abc")); |
| 55 | ASSERT_EQ("abc", g2o::strToLower("AbC")); |
| 56 | ASSERT_EQ("abc", g2o::strToLower("ABC")); |
| 57 | ASSERT_EQ("abc !!$", g2o::strToLower("ABC !!$")); |
| 58 | } |
| 59 | |
| 60 | TEST(Stuff, StrToUpper) { |
| 61 | ASSERT_EQ("ABC", g2o::strToUpper("abc")); |
| 62 | ASSERT_EQ("ABC", g2o::strToUpper("Abc")); |
| 63 | ASSERT_EQ("ABC", g2o::strToUpper("AbC")); |
| 64 | ASSERT_EQ("ABC", g2o::strToUpper("ABC")); |
| 65 | ASSERT_EQ("ABC !!$", g2o::strToUpper("ABC !!$")); |
| 66 | } |
| 67 | |
| 68 | TEST(Stuff, FormatString) { |
| 69 | ASSERT_EQ("42", g2o::formatString("%d", 42)); |
| 70 | ASSERT_EQ("3.1415", g2o::formatString("%g", 3.1415)); |
| 71 | ASSERT_EQ("3.141500", g2o::formatString("%f", 3.1415)); |
| 72 | ASSERT_EQ("3.142", g2o::formatString("%.3f", 3.1415)); |
| 73 | ASSERT_EQ("Hello 42 World", g2o::formatString("Hello %d World", 42)); |
| 74 | } |
| 75 | |
| 76 | TEST(Stuff, StrPrintf) { |
| 77 | std::string output; |
| 78 | g2o::strPrintf(output, "%d", 42); |
| 79 | ASSERT_EQ("42", output); |
| 80 | g2o::strPrintf(output, "%g", 3.1415); |
| 81 | ASSERT_EQ("3.1415", output); |
| 82 | g2o::strPrintf(output, "%f", 3.1415); |
| 83 | ASSERT_EQ("3.141500", output); |
| 84 | g2o::strPrintf(output, "%.3f", 3.1415); |
| 85 | ASSERT_EQ("3.142", output); |
| 86 | g2o::strPrintf(output, "Hello %d World", 42); |
| 87 | ASSERT_EQ("Hello 42 World", output); |
| 88 | } |
| 89 | |
| 90 | TEST(Stuff, StrSplit) { |
| 91 | std::vector<std::string> tokens; |
| 92 | tokens = g2o::strSplit("", ","); |
| 93 | ASSERT_EQ(0, tokens.size()); |
| 94 | tokens = g2o::strSplit("42", ","); |
| 95 | ASSERT_EQ(1, tokens.size()); |
| 96 | ASSERT_EQ("42", tokens[0]); |
| 97 | tokens = g2o::strSplit("1,2;3:4", ";,:"); |
| 98 | ASSERT_EQ(4, tokens.size()); |
| 99 | for (size_t i = 0; i < tokens.size(); ++i) |
| 100 | ASSERT_EQ(g2o::formatString("%d", int(i + 1)), tokens[i]); |
| 101 | } |
| 102 | |
| 103 | TEST(Stuff, StrStartsWith) { |
| 104 | ASSERT_FALSE(g2o::strStartsWith("Hello World!", "World!")); |
| 105 | ASSERT_TRUE(g2o::strStartsWith("Hello World!", "Hello")); |
| 106 | ASSERT_TRUE(g2o::strStartsWith("Hello World!", "Hello World!")); |
| 107 | ASSERT_FALSE(g2o::strStartsWith("Hello World!", "Hello World!!!")); |
| 108 | } |
| 109 | |
| 110 | TEST(Stuff, StrEndsWith) { |
| 111 | ASSERT_TRUE(g2o::strEndsWith("Hello World!", "World!")); |
| 112 | ASSERT_FALSE(g2o::strEndsWith("Hello World!", "Hello")); |
| 113 | ASSERT_TRUE(g2o::strEndsWith("Hello World!", "Hello World!")); |
| 114 | ASSERT_FALSE(g2o::strEndsWith("Hello World!", "!!Hello World!")); |
| 115 | } |
| 116 | |
| 117 | TEST(Stuff, ReadLine) { |
| 118 | std::stringstream content; |
| 119 | content << "foo" << std::endl; |
| 120 | content << "" << std::endl; |
| 121 | content << "bar" << std::endl; |
| 122 | content << "foo bar" << std::endl; |
| 123 | |
| 124 | std::stringstream currentLine; |
| 125 | ASSERT_EQ(g2o::readLine(content, currentLine), 3); |
| 126 | ASSERT_EQ(currentLine.str(), "foo"); |
| 127 | ASSERT_EQ(g2o::readLine(content, currentLine), 0); |
| 128 | ASSERT_EQ(currentLine.str(), ""); |
| 129 | ASSERT_EQ(g2o::readLine(content, currentLine), 3); |
| 130 | ASSERT_EQ(currentLine.str(), "bar"); |
| 131 | ASSERT_EQ(g2o::readLine(content, currentLine), 7); |
| 132 | ASSERT_EQ(currentLine.str(), "foo bar"); |
| 133 | ASSERT_EQ(g2o::readLine(content, currentLine), -1); |
| 134 | ASSERT_EQ(g2o::readLine(content, currentLine), -1); |
| 135 | |
| 136 | content = std::stringstream(); |
| 137 | content << "foobar"; |
| 138 | ASSERT_EQ(g2o::readLine(content, currentLine), 6); |
| 139 | ASSERT_EQ(currentLine.str(), "foobar"); |
| 140 | ASSERT_EQ(g2o::readLine(content, currentLine), -1); |
| 141 | } |
| 142 | |
| 143 | #if defined(UNIX) && !defined(ANDROID) |
| 144 | TEST(Stuff, StrExpand) { |
| 145 | char* envVar = getenv("HOME"); |
| 146 | if (envVar == nullptr) { |
| 147 | std::cerr << "HOME not defined" << std::endl; |
| 148 | SUCCEED(); |
| 149 | return; |
| 150 | } |
| 151 | const std::string expanded = g2o::strExpandFilename("$HOME/filename.txt"); |
| 152 | const std::string expected = g2o::formatString("%s/filename.txt", envVar); |
| 153 | EXPECT_EQ(expanded, expected); |
| 154 | } |
| 155 | #endif |