lua_parameter_dictionary_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <memory>
22 
25 #include "gtest/gtest.h"
26 
27 namespace cartographer {
28 namespace common {
29 namespace {
30 
31 std::unique_ptr<LuaParameterDictionary> MakeNonReferenceCounted(
32  const string& code) {
34  code, common::make_unique<DummyFileResolver>());
35 }
36 
37 class LuaParameterDictionaryTest : public ::testing::Test {
38  protected:
39  void ReferenceAllKeysAsIntegers(LuaParameterDictionary* dict) {
40  for (const string& key : dict->GetKeys()) {
41  dict->GetInt(key);
42  }
43  }
44 };
45 
46 TEST_F(LuaParameterDictionaryTest, GetInt) {
47  auto dict = MakeDictionary("return { blah = 100 }");
48  ASSERT_EQ(dict->GetInt("blah"), 100);
49 }
50 
51 TEST_F(LuaParameterDictionaryTest, GetString) {
52  auto dict = MakeDictionary("return { blah = 'is_a_string' }\n");
53  ASSERT_EQ(dict->GetString("blah"), "is_a_string");
54 }
55 
56 TEST_F(LuaParameterDictionaryTest, GetDouble) {
57  auto dict = MakeDictionary("return { blah = 3.1415 }");
58  ASSERT_DOUBLE_EQ(dict->GetDouble("blah"), 3.1415);
59 }
60 
61 TEST_F(LuaParameterDictionaryTest, GetBoolTrue) {
62  auto dict = MakeDictionary("return { blah = true }");
63  ASSERT_TRUE(dict->GetBool("blah"));
64 }
65 
66 TEST_F(LuaParameterDictionaryTest, GetBoolFalse) {
67  auto dict = MakeDictionary("return { blah = false }");
68  ASSERT_FALSE(dict->GetBool("blah"));
69 }
70 
71 TEST_F(LuaParameterDictionaryTest, GetDictionary) {
72  auto dict =
73  MakeDictionary("return { blah = { blue = 100, red = 200 }, fasel = 10 }");
74 
75  std::unique_ptr<LuaParameterDictionary> sub_dict(dict->GetDictionary("blah"));
76  std::vector<string> keys = sub_dict->GetKeys();
77  ASSERT_EQ(keys.size(), 2);
78  std::sort(keys.begin(), keys.end());
79  ASSERT_EQ(keys[0], "blue");
80  ASSERT_EQ(keys[1], "red");
81  ASSERT_TRUE(sub_dict->HasKey("blue"));
82  ASSERT_TRUE(sub_dict->HasKey("red"));
83  ASSERT_EQ(sub_dict->GetInt("blue"), 100);
84  ASSERT_EQ(sub_dict->GetInt("red"), 200);
85 
86  ASSERT_EQ(dict->GetString("fasel"), "10");
87 }
88 
89 TEST_F(LuaParameterDictionaryTest, GetKeys) {
90  auto dict = MakeDictionary("return { blah = 100, blah1 = 200}");
91 
92  std::vector<string> keys = dict->GetKeys();
93  ASSERT_EQ(keys.size(), 2);
94  std::sort(keys.begin(), keys.end());
95  ASSERT_EQ(keys[0], "blah");
96  ASSERT_EQ(keys[1], "blah1");
97 
98  ReferenceAllKeysAsIntegers(dict.get());
99 }
100 
101 TEST_F(LuaParameterDictionaryTest, NonExistingKey) {
102  auto dict = MakeDictionary("return { blah = 100 }");
103  ReferenceAllKeysAsIntegers(dict.get());
104  ASSERT_DEATH(dict->GetInt("blah_fasel"), "Key.* not in dictionary.");
105 }
106 
107 TEST_F(LuaParameterDictionaryTest, UintNegative) {
108  auto dict = MakeDictionary("return { blah = -100}");
109  ASSERT_DEATH(dict->GetNonNegativeInt("blah"), ".*-100 is negative.");
110  ReferenceAllKeysAsIntegers(dict.get());
111 }
112 
113 TEST_F(LuaParameterDictionaryTest, ToString) {
114  auto dict = MakeDictionary(R"(return {
115  ceta = { yolo = "hurray" },
116  fasel = 1234.456786,
117  fasel1 = -math.huge,
118  fasel2 = math.huge,
119  blubber = 123,
120  blub = 'hello',
121  alpha = true,
122  alpha1 = false,
123  })");
124 
125  const string golden = R"({
126  alpha = true,
127  alpha1 = false,
128  blub = "hello",
129  blubber = 123.000000,
130  ceta = {
131  yolo = "hurray",
132  },
133  fasel = 1234.456786,
134  fasel1 = -math.huge,
135  fasel2 = math.huge,
136 })";
137  EXPECT_EQ(golden, dict->ToString());
138 
139  auto dict1 = MakeDictionary("return " + dict->ToString());
140 
141  EXPECT_EQ(dict1->GetBool("alpha"), true);
142  EXPECT_EQ(dict1->GetBool("alpha1"), false);
143  EXPECT_EQ(dict1->GetInt("blubber"), 123);
144  EXPECT_EQ(dict1->GetString("blub"), "hello");
145  EXPECT_EQ(dict1->GetDictionary("ceta")->GetString("yolo"), "hurray");
146  EXPECT_NEAR(dict1->GetDouble("fasel"), 1234.456786, 1e-3);
147  EXPECT_TRUE(std::isinf(-dict1->GetDouble("fasel1")));
148  EXPECT_TRUE(std::isinf(dict1->GetDouble("fasel2")));
149 
150  EXPECT_EQ(dict->GetBool("alpha"), true);
151  EXPECT_EQ(dict->GetBool("alpha1"), false);
152  EXPECT_EQ(dict->GetInt("blubber"), 123);
153  EXPECT_EQ(dict->GetString("blub"), "hello");
154  EXPECT_EQ(dict->GetDictionary("ceta")->GetString("yolo"), "hurray");
155  EXPECT_NEAR(dict->GetDouble("fasel"), 1234.456786, 1e-3);
156  EXPECT_TRUE(std::isinf(-dict->GetDouble("fasel1")));
157  EXPECT_TRUE(std::isinf(dict->GetDouble("fasel2")));
158 }
159 
160 TEST_F(LuaParameterDictionaryTest, ToStringForArrays) {
161  auto dict = MakeNonReferenceCounted(
162  R"(return {
163  "blub", 3, 3.1,
164  foo = "ups",
165  })");
166 
167  const string golden = R"({
168  "blub",
169  3.000000,
170  3.100000,
171  foo = "ups",
172 })";
173  EXPECT_EQ(golden, dict->ToString());
174 }
175 
176 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsStrings) {
177  auto dict = MakeDictionary("return { 'a', 'b', 'c' }");
178  EXPECT_EQ(0, dict->GetKeys().size());
179  const std::vector<string> values = dict->GetArrayValuesAsStrings();
180  EXPECT_EQ(3, values.size());
181  EXPECT_EQ("a", values[0]);
182  EXPECT_EQ("b", values[1]);
183  EXPECT_EQ("c", values[2]);
184 }
185 
186 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDoubles) {
187  auto dict = MakeDictionary("return { 1., 2., 3. }");
188  EXPECT_EQ(0, dict->GetKeys().size());
189  const std::vector<double> values = dict->GetArrayValuesAsDoubles();
190  EXPECT_EQ(3, values.size());
191  EXPECT_NEAR(1., values[0], 1e-7);
192  EXPECT_NEAR(2., values[1], 1e-7);
193  EXPECT_NEAR(3., values[2], 1e-7);
194 }
195 
196 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDictionaries) {
197  auto dict = MakeDictionary("return { { a = 1 }, { b = 3 } }");
198  EXPECT_EQ(0, dict->GetKeys().size());
199  const std::vector<std::unique_ptr<LuaParameterDictionary>> values =
200  dict->GetArrayValuesAsDictionaries();
201  EXPECT_EQ(2, values.size());
202  EXPECT_EQ(1., values[0]->GetInt("a"));
203  EXPECT_EQ(3., values[1]->GetInt("b"));
204 }
205 
206 TEST_F(LuaParameterDictionaryTest, TestChooseTrue) {
207  auto dict = MakeDictionary("return { a = choose(true, 1, 0) }");
208  EXPECT_EQ(1, dict->GetInt("a"));
209 }
210 
211 TEST_F(LuaParameterDictionaryTest, TestChoseFalse) {
212  auto dict = MakeDictionary("return { a = choose(false, 1, 0) }");
213  EXPECT_EQ(0, dict->GetInt("a"));
214 }
215 
216 TEST_F(LuaParameterDictionaryTest, TestChooseInvalidArgument) {
217  EXPECT_DEATH(MakeDictionary("return { a = choose('truish', 1, 0) }"),
218  "condition is not a boolean value.");
219 }
220 
221 } // namespace
222 } // namespace common
223 } // namespace cartographer
std::unique_ptr< LuaParameterDictionary > MakeDictionary(const string &code)
static std::unique_ptr< LuaParameterDictionary > NonReferenceCounted(const string &code, std::unique_ptr< FileResolver > file_resolver)


cartographer
Author(s):
autogenerated on Wed Jun 5 2019 21:57:58