id_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 <deque>
21 #include <iterator>
22 #include <random>
23 #include <utility>
24 
26 #include "gtest/gtest.h"
27 
28 namespace cartographer {
29 namespace mapping {
30 namespace {
31 
32 common::Time CreateTime(const int milliseconds) {
33  return common::Time(common::FromMilliseconds(milliseconds));
34 }
35 
36 class Data {
37  public:
38  explicit Data(int milliseconds) : time_(CreateTime(milliseconds)) {}
39 
40  const common::Time& time() const { return time_; }
41 
42  private:
44 };
45 
46 template <typename IdType>
47 static MapById<IdType, int> CreateTestMapById() {
48  MapById<IdType, int> map_by_id;
49  map_by_id.Append(7, 2);
50  map_by_id.Append(42, 3);
51  map_by_id.Append(0, 0);
52  map_by_id.Append(0, 1);
53  return map_by_id;
54 }
55 
56 TEST(IdTest, EmptyMapById) {
57  MapById<NodeId, int> map_by_id;
58  EXPECT_TRUE(map_by_id.empty());
59  const NodeId id = map_by_id.Append(42, 42);
60  EXPECT_FALSE(map_by_id.empty());
61  map_by_id.Trim(id);
62  EXPECT_TRUE(map_by_id.empty());
63  EXPECT_EQ(0, map_by_id.size());
64 }
65 
66 TEST(IdTest, MapByIdIterator) {
67  MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
68  EXPECT_EQ(4, map_by_id.size());
69  EXPECT_EQ(2, map_by_id.BeginOfTrajectory(7)->data);
70  EXPECT_TRUE(std::next(map_by_id.BeginOfTrajectory(7)) ==
71  map_by_id.EndOfTrajectory(7));
72  std::deque<std::pair<NodeId, int>> expected_id_data = {
73  {NodeId{0, 0}, 0},
74  {NodeId{0, 1}, 1},
75  {NodeId{7, 0}, 2},
76  {NodeId{42, 0}, 3},
77  };
78  for (const auto& id_data : map_by_id) {
79  ASSERT_FALSE(expected_id_data.empty());
80  EXPECT_EQ(expected_id_data.front().first, id_data.id);
81  EXPECT_EQ(expected_id_data.front().second, id_data.data);
82  expected_id_data.pop_front();
83  }
84  EXPECT_TRUE(expected_id_data.empty());
85 }
86 
87 TEST(IdTest, MapByIdTrajectoryRange) {
88  MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
89  std::deque<std::pair<NodeId, int>> expected_data = {
90  {NodeId{0, 0}, 0},
91  {NodeId{0, 1}, 1},
92  };
93  for (const auto& entry : map_by_id.trajectory(0)) {
94  ASSERT_FALSE(expected_data.empty());
95  EXPECT_EQ(expected_data.front().first, entry.id);
96  EXPECT_EQ(expected_data.front().second, entry.data);
97  expected_data.pop_front();
98  }
99  EXPECT_TRUE(expected_data.empty());
100 }
101 
102 TEST(IdTest, MapByIdTrajectoryIdRange) {
103  MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
104  std::deque<int> expected_data = {0, 7, 42};
105  for (const int trajectory_id : map_by_id.trajectory_ids()) {
106  ASSERT_FALSE(expected_data.empty());
107  EXPECT_EQ(expected_data.front(), trajectory_id);
108  expected_data.pop_front();
109  }
110  EXPECT_TRUE(expected_data.empty());
111 }
112 
113 TEST(IdTest, MapByIdIterateByTrajectories) {
114  MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
115  std::deque<std::pair<NodeId, int>> expected_id_data = {
116  {NodeId{0, 0}, 0},
117  {NodeId{0, 1}, 1},
118  {NodeId{7, 0}, 2},
119  {NodeId{42, 0}, 3},
120  };
121  for (int trajectory_id : map_by_id.trajectory_ids()) {
122  for (const auto& entry : map_by_id.trajectory(trajectory_id)) {
123  ASSERT_FALSE(expected_id_data.empty());
124  EXPECT_EQ(expected_id_data.front().first, entry.id);
125  EXPECT_EQ(expected_id_data.front().second, entry.data);
126  expected_id_data.pop_front();
127  }
128  }
129  EXPECT_TRUE(expected_id_data.empty());
130 }
131 
132 TEST(IdTest, MapByIdPrevIterator) {
133  MapById<NodeId, int> map_by_id;
134  map_by_id.Append(42, 42);
135  auto it = map_by_id.end();
136  ASSERT_TRUE(it != map_by_id.begin());
137  std::advance(it, -1);
138  EXPECT_TRUE(it == map_by_id.begin());
139 }
140 
141 TEST(IdTest, InsertIntoMapById) {
142  MapById<NodeId, int> map_by_id;
143  EXPECT_EQ(0, map_by_id.SizeOfTrajectoryOrZero(42));
144  map_by_id.Append(42, 42);
145  map_by_id.Insert(NodeId{42, 5}, 42);
146  EXPECT_EQ(2, map_by_id.SizeOfTrajectoryOrZero(42));
147 }
148 
149 TEST(IdTest, FindNodeId) {
150  MapById<NodeId, int> map_by_id;
151  map_by_id.Append(42, 42);
152  map_by_id.Append(42, 43);
153  map_by_id.Append(42, 44);
154  CHECK_EQ(map_by_id.find(NodeId{42, 1})->data, 43);
155  EXPECT_TRUE(map_by_id.find(NodeId{42, 3}) == map_by_id.end());
156 }
157 
158 TEST(IdTest, FindSubmapId) {
159  MapById<SubmapId, int> map_by_id;
160  map_by_id.Append(42, 42);
161  map_by_id.Append(42, 43);
162  map_by_id.Append(42, 44);
163  CHECK_EQ(map_by_id.find(SubmapId{42, 1})->data, 43);
164  EXPECT_TRUE(map_by_id.find(SubmapId{42, 3}) == map_by_id.end());
165 }
166 
167 TEST(IdTest, LowerBoundEdgeCases) {
168  MapById<SubmapId, Data> map_by_id;
169  map_by_id.Append(0, Data(1));
170  map_by_id.Append(2, Data(2));
171  CHECK(map_by_id.lower_bound(1, CreateTime(10)) ==
172  map_by_id.EndOfTrajectory(1));
173  CHECK(map_by_id.lower_bound(2, CreateTime(3)) ==
174  map_by_id.EndOfTrajectory(2));
175  CHECK(map_by_id.lower_bound(2, CreateTime(1)) ==
176  map_by_id.BeginOfTrajectory(2));
177 }
178 
179 TEST(IdTest, LowerBound) {
180  MapById<SubmapId, Data> map_by_id;
181  map_by_id.Append(0, Data(1));
182  map_by_id.Append(0, Data(2));
183  map_by_id.Append(0, Data(4));
184  map_by_id.Append(0, Data(5));
185  CHECK(map_by_id.lower_bound(0, CreateTime(3)) ==
186  (MapById<SubmapId, Data>::ConstIterator(map_by_id, SubmapId{0, 2})));
187  CHECK(map_by_id.lower_bound(0, CreateTime(2)) ==
188  (MapById<SubmapId, Data>::ConstIterator(map_by_id, SubmapId{0, 1})));
189  CHECK(map_by_id.lower_bound(0, CreateTime(4)) ==
190  (MapById<SubmapId, Data>::ConstIterator(map_by_id, SubmapId{0, 2})));
191 }
192 
193 TEST(IdTest, LowerBoundFuzz) {
194  constexpr int kMaxTimeIncrement = 20;
195  constexpr int kMaxNumNodes = 20;
196  constexpr int kNumTests = 100;
197  constexpr int kTrajectoryId = 1;
198 
199  std::mt19937 rng;
200  std::uniform_int_distribution<int> dt_dist(1, kMaxTimeIncrement);
201  std::uniform_int_distribution<int> N_dist(1, kMaxNumNodes);
202 
203  for (int i = 0; i < kNumTests; ++i) {
204  const int N = N_dist(rng);
205  int t = 0;
206  MapById<SubmapId, Data> map_by_id;
207  for (int j = 0; j < N; ++j) {
208  t = t + dt_dist(rng);
209  map_by_id.Append(kTrajectoryId, Data(t));
210  }
211  std::uniform_int_distribution<int> t0_dist(1, N * kMaxTimeIncrement + 1);
212  int t0 = t0_dist(rng);
213  auto it = map_by_id.lower_bound(kTrajectoryId, CreateTime(t0));
214 
215  auto ground_truth = std::lower_bound(
216  map_by_id.BeginOfTrajectory(kTrajectoryId),
217  map_by_id.EndOfTrajectory(kTrajectoryId), CreateTime(t0),
218  [](MapById<SubmapId, Data>::IdDataReference a, const common::Time& t) {
219  return a.data.time() < t;
220  });
221 
222  CHECK(ground_truth == it);
223  }
224 }
225 
226 struct DataStruct {
228 };
229 
230 TEST(IdTest, LowerBoundFuzzWithStruct) {
231  constexpr int kMaxTimeIncrement = 20;
232  constexpr int kMaxNumNodes = 20;
233  constexpr int kNumTests = 100;
234  constexpr int kTrajectoryId = 1;
235 
236  std::mt19937 rng;
237  std::uniform_int_distribution<int> dt_dist(1, kMaxTimeIncrement);
238  std::uniform_int_distribution<int> N_dist(1, kMaxNumNodes);
239 
240  for (int i = 0; i < kNumTests; ++i) {
241  const int N = N_dist(rng);
242  int t = 0;
243  MapById<SubmapId, DataStruct> map_by_id;
244  for (int j = 0; j < N; ++j) {
245  t = t + dt_dist(rng);
246  map_by_id.Append(kTrajectoryId, DataStruct{CreateTime(t)});
247  }
248  std::uniform_int_distribution<int> t0_dist(1, N * kMaxTimeIncrement + 1);
249  int t0 = t0_dist(rng);
250  auto it = map_by_id.lower_bound(kTrajectoryId, CreateTime(t0));
251 
252  auto ground_truth = std::lower_bound(
253  map_by_id.BeginOfTrajectory(kTrajectoryId),
254  map_by_id.EndOfTrajectory(kTrajectoryId), CreateTime(t0),
255  [](MapById<SubmapId, DataStruct>::IdDataReference a,
256  const common::Time& t) { return a.data.time < t; });
257 
258  CHECK(ground_truth == it);
259  }
260 }
261 } // namespace
262 } // namespace mapping
263 } // namespace cartographer
common::Duration FromMilliseconds(const int64 milliseconds)
Definition: time.cc:43
const common::Time time_
Definition: id_test.cc:43
const common::Time time
Definition: id_test.cc:227
UniversalTimeScaleClock::time_point Time
Definition: time.h:44
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58