| 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/stuff/property.h" |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | class PropertyTest : public ::testing::Test { |
| 31 | protected: |
| 32 | void SetUp() override { |
| 33 | propertyMap_.makeProperty<g2o::IntProperty>("int", 42); |
| 34 | propertyMap_.makeProperty<g2o::DoubleProperty>("double", 133.7); |
| 35 | propertyMap_.makeProperty<g2o::StringProperty>("string", "g2o"); |
| 36 | } |
| 37 | |
| 38 | g2o::PropertyMap propertyMap_; |
| 39 | }; |
| 40 | |
| 41 | TEST_F(PropertyTest, Erase) { |
| 42 | EXPECT_EQ(propertyMap_.size(), 3); |
| 43 | EXPECT_TRUE(propertyMap_.getProperty<g2o::StringProperty>("string")); |
| 44 | bool erased = propertyMap_.eraseProperty("string"); |
| 45 | EXPECT_TRUE(erased); |
| 46 | EXPECT_EQ(propertyMap_.size(), 2); |
| 47 | EXPECT_FALSE(propertyMap_.getProperty<g2o::StringProperty>("string")); |
| 48 | } |
| 49 | |
| 50 | TEST_F(PropertyTest, Add) { |
| 51 | EXPECT_EQ(propertyMap_.size(), 3); |
| 52 | auto* property = new g2o::IntProperty("new_prop", 81); |
| 53 | propertyMap_.addProperty(property); |
| 54 | EXPECT_EQ(propertyMap_.size(), 4); |
| 55 | EXPECT_EQ(propertyMap_.getProperty<g2o::IntProperty>("new_prop"), property); |
| 56 | } |
| 57 | |
| 58 | TEST_F(PropertyTest, UpdatePropertyFromString) { |
| 59 | bool updated = propertyMap_.updatePropertyFromString("int", "21"); |
| 60 | EXPECT_TRUE(updated); |
| 61 | auto property = propertyMap_.getProperty<g2o::IntProperty>("int"); |
| 62 | EXPECT_EQ(property->value(), 21); |
| 63 | updated = propertyMap_.updatePropertyFromString("not_in", "42"); |
| 64 | EXPECT_FALSE(updated); |
| 65 | } |
| 66 | |
| 67 | TEST_F(PropertyTest, UpdateMapFromString) { |
| 68 | auto intProperty = propertyMap_.getProperty<g2o::IntProperty>("int"); |
| 69 | auto stringProperty = propertyMap_.getProperty<g2o::StringProperty>("string"); |
| 70 | bool updated = propertyMap_.updateMapFromString("int=21,string=updated"); |
| 71 | EXPECT_TRUE(updated); |
| 72 | EXPECT_EQ(intProperty->value(), 21); |
| 73 | EXPECT_EQ(stringProperty->value(), "updated"); |
| 74 | } |
| 75 | |
| 76 | TEST_F(PropertyTest, WriteCSV) { |
| 77 | std::stringstream output; |
| 78 | propertyMap_.writeToCSV(output); |
| 79 | std::string lines[2]; |
| 80 | std::getline(output, lines[0]); |
| 81 | std::getline(output, lines[1]); |
| 82 | EXPECT_EQ(lines[0], "double,int,string"); |
| 83 | EXPECT_EQ(lines[1], "133.7,42,g2o"); |
| 84 | } |