lua_parameter_dictionary_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/common/lua_parameter_dictionary.h"
00018 
00019 #include <algorithm>
00020 #include <cmath>
00021 #include <memory>
00022 
00023 #include "absl/memory/memory.h"
00024 #include "cartographer/common/lua_parameter_dictionary_test_helpers.h"
00025 #include "gtest/gtest.h"
00026 
00027 namespace cartographer {
00028 namespace common {
00029 namespace {
00030 
00031 std::unique_ptr<LuaParameterDictionary> MakeNonReferenceCounted(
00032     const std::string& code) {
00033   return LuaParameterDictionary::NonReferenceCounted(
00034       code, absl::make_unique<DummyFileResolver>());
00035 }
00036 
00037 class LuaParameterDictionaryTest : public ::testing::Test {
00038  protected:
00039   void ReferenceAllKeysAsIntegers(LuaParameterDictionary* dict) {
00040     for (const std::string& key : dict->GetKeys()) {
00041       dict->GetInt(key);
00042     }
00043   }
00044 };
00045 
00046 TEST_F(LuaParameterDictionaryTest, GetInt) {
00047   auto dict = MakeDictionary("return { blah = 100 }");
00048   ASSERT_EQ(dict->GetInt("blah"), 100);
00049 }
00050 
00051 TEST_F(LuaParameterDictionaryTest, GetString) {
00052   auto dict = MakeDictionary("return { blah = 'is_a_string' }\n");
00053   ASSERT_EQ(dict->GetString("blah"), "is_a_string");
00054 }
00055 
00056 TEST_F(LuaParameterDictionaryTest, GetDouble) {
00057   auto dict = MakeDictionary("return { blah = 3.1415 }");
00058   ASSERT_DOUBLE_EQ(dict->GetDouble("blah"), 3.1415);
00059 }
00060 
00061 TEST_F(LuaParameterDictionaryTest, GetBoolTrue) {
00062   auto dict = MakeDictionary("return { blah = true }");
00063   ASSERT_TRUE(dict->GetBool("blah"));
00064 }
00065 
00066 TEST_F(LuaParameterDictionaryTest, GetBoolFalse) {
00067   auto dict = MakeDictionary("return { blah = false }");
00068   ASSERT_FALSE(dict->GetBool("blah"));
00069 }
00070 
00071 TEST_F(LuaParameterDictionaryTest, GetDictionary) {
00072   auto dict =
00073       MakeDictionary("return { blah = { blue = 100, red = 200 }, fasel = 10 }");
00074 
00075   std::unique_ptr<LuaParameterDictionary> sub_dict(dict->GetDictionary("blah"));
00076   std::vector<std::string> keys = sub_dict->GetKeys();
00077   ASSERT_EQ(keys.size(), 2);
00078   std::sort(keys.begin(), keys.end());
00079   ASSERT_EQ(keys[0], "blue");
00080   ASSERT_EQ(keys[1], "red");
00081   ASSERT_TRUE(sub_dict->HasKey("blue"));
00082   ASSERT_TRUE(sub_dict->HasKey("red"));
00083   ASSERT_EQ(sub_dict->GetInt("blue"), 100);
00084   ASSERT_EQ(sub_dict->GetInt("red"), 200);
00085 
00086   ASSERT_EQ(dict->GetString("fasel"), "10");
00087 }
00088 
00089 TEST_F(LuaParameterDictionaryTest, GetKeys) {
00090   auto dict = MakeDictionary("return { blah = 100, blah1 = 200}");
00091 
00092   std::vector<std::string> keys = dict->GetKeys();
00093   ASSERT_EQ(keys.size(), 2);
00094   std::sort(keys.begin(), keys.end());
00095   ASSERT_EQ(keys[0], "blah");
00096   ASSERT_EQ(keys[1], "blah1");
00097 
00098   ReferenceAllKeysAsIntegers(dict.get());
00099 }
00100 
00101 TEST_F(LuaParameterDictionaryTest, NonExistingKey) {
00102   auto dict = MakeDictionary("return { blah = 100 }");
00103   ReferenceAllKeysAsIntegers(dict.get());
00104   ASSERT_DEATH(dict->GetInt("blah_fasel"), "Key.* not in dictionary.");
00105 }
00106 
00107 TEST_F(LuaParameterDictionaryTest, UintNegative) {
00108   auto dict = MakeDictionary("return { blah = -100}");
00109   ASSERT_DEATH(dict->GetNonNegativeInt("blah"), ".*-100 is negative.");
00110   ReferenceAllKeysAsIntegers(dict.get());
00111 }
00112 
00113 TEST_F(LuaParameterDictionaryTest, ToString) {
00114   auto dict = MakeDictionary(R"(return {
00115   ceta = { yolo = "hurray" },
00116   fasel = 1234.456786,
00117   fasel1 = -math.huge,
00118   fasel2 = math.huge,
00119   blubber = 123,
00120   blub = 'hello',
00121   alpha = true,
00122   alpha1 = false,
00123   })");
00124 
00125   const std::string golden = R"({
00126   alpha = true,
00127   alpha1 = false,
00128   blub = "hello",
00129   blubber = 123.000000,
00130   ceta = {
00131     yolo = "hurray",
00132   },
00133   fasel = 1234.456786,
00134   fasel1 = -math.huge,
00135   fasel2 = math.huge,
00136 })";
00137   EXPECT_EQ(golden, dict->ToString());
00138 
00139   auto dict1 = MakeDictionary("return " + dict->ToString());
00140 
00141   EXPECT_EQ(dict1->GetBool("alpha"), true);
00142   EXPECT_EQ(dict1->GetBool("alpha1"), false);
00143   EXPECT_EQ(dict1->GetInt("blubber"), 123);
00144   EXPECT_EQ(dict1->GetString("blub"), "hello");
00145   EXPECT_EQ(dict1->GetDictionary("ceta")->GetString("yolo"), "hurray");
00146   EXPECT_NEAR(dict1->GetDouble("fasel"), 1234.456786, 1e-3);
00147   EXPECT_TRUE(std::isinf(-dict1->GetDouble("fasel1")));
00148   EXPECT_TRUE(std::isinf(dict1->GetDouble("fasel2")));
00149 
00150   EXPECT_EQ(dict->GetBool("alpha"), true);
00151   EXPECT_EQ(dict->GetBool("alpha1"), false);
00152   EXPECT_EQ(dict->GetInt("blubber"), 123);
00153   EXPECT_EQ(dict->GetString("blub"), "hello");
00154   EXPECT_EQ(dict->GetDictionary("ceta")->GetString("yolo"), "hurray");
00155   EXPECT_NEAR(dict->GetDouble("fasel"), 1234.456786, 1e-3);
00156   EXPECT_TRUE(std::isinf(-dict->GetDouble("fasel1")));
00157   EXPECT_TRUE(std::isinf(dict->GetDouble("fasel2")));
00158 }
00159 
00160 TEST_F(LuaParameterDictionaryTest, ToStringForArrays) {
00161   auto dict = MakeNonReferenceCounted(
00162       R"(return {
00163       "blub", 3, 3.1,
00164       foo = "ups",
00165   })");
00166 
00167   const std::string golden = R"({
00168   "blub",
00169   3.000000,
00170   3.100000,
00171   foo = "ups",
00172 })";
00173   EXPECT_EQ(golden, dict->ToString());
00174 }
00175 
00176 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsStrings) {
00177   auto dict = MakeDictionary("return { 'a', 'b', 'c' }");
00178   EXPECT_EQ(0, dict->GetKeys().size());
00179   const std::vector<std::string> values = dict->GetArrayValuesAsStrings();
00180   EXPECT_EQ(3, values.size());
00181   EXPECT_EQ("a", values[0]);
00182   EXPECT_EQ("b", values[1]);
00183   EXPECT_EQ("c", values[2]);
00184 }
00185 
00186 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDoubles) {
00187   auto dict = MakeDictionary("return { 1., 2., 3. }");
00188   EXPECT_EQ(0, dict->GetKeys().size());
00189   const std::vector<double> values = dict->GetArrayValuesAsDoubles();
00190   EXPECT_EQ(3, values.size());
00191   EXPECT_NEAR(1., values[0], 1e-7);
00192   EXPECT_NEAR(2., values[1], 1e-7);
00193   EXPECT_NEAR(3., values[2], 1e-7);
00194 }
00195 
00196 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDictionaries) {
00197   auto dict = MakeDictionary("return { { a = 1 }, { b = 3 } }");
00198   EXPECT_EQ(0, dict->GetKeys().size());
00199   const std::vector<std::unique_ptr<LuaParameterDictionary>> values =
00200       dict->GetArrayValuesAsDictionaries();
00201   EXPECT_EQ(2, values.size());
00202   EXPECT_EQ(1., values[0]->GetInt("a"));
00203   EXPECT_EQ(3., values[1]->GetInt("b"));
00204 }
00205 
00206 TEST_F(LuaParameterDictionaryTest, TestChooseTrue) {
00207   auto dict = MakeDictionary("return { a = choose(true, 1, 0) }");
00208   EXPECT_EQ(1, dict->GetInt("a"));
00209 }
00210 
00211 TEST_F(LuaParameterDictionaryTest, TestChoseFalse) {
00212   auto dict = MakeDictionary("return { a = choose(false, 1, 0) }");
00213   EXPECT_EQ(0, dict->GetInt("a"));
00214 }
00215 
00216 TEST_F(LuaParameterDictionaryTest, TestChooseInvalidArgument) {
00217   EXPECT_DEATH(MakeDictionary("return { a = choose('truish', 1, 0) }"),
00218                "condition is not a boolean value.");
00219 }
00220 
00221 }  // namespace
00222 }  // namespace common
00223 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35