26 #include "gtest/gtest.h" 38 explicit Data(
int milliseconds) :
time_(CreateTime(milliseconds)) {}
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);
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());
62 EXPECT_TRUE(map_by_id.empty());
63 EXPECT_EQ(0, map_by_id.size());
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 = {
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();
84 EXPECT_TRUE(expected_id_data.empty());
87 TEST(IdTest, MapByIdTrajectoryRange) {
88 MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
89 std::deque<std::pair<NodeId, int>> expected_data = {
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();
99 EXPECT_TRUE(expected_data.empty());
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();
110 EXPECT_TRUE(expected_data.empty());
113 TEST(IdTest, MapByIdIterateByTrajectories) {
114 MapById<NodeId, int> map_by_id = CreateTestMapById<NodeId>();
115 std::deque<std::pair<NodeId, int>> expected_id_data = {
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();
129 EXPECT_TRUE(expected_id_data.empty());
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());
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));
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());
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());
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));
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})));
193 TEST(IdTest, LowerBoundFuzz) {
194 constexpr
int kMaxTimeIncrement = 20;
195 constexpr
int kMaxNumNodes = 20;
196 constexpr
int kNumTests = 100;
197 constexpr
int kTrajectoryId = 1;
200 std::uniform_int_distribution<int> dt_dist(1, kMaxTimeIncrement);
201 std::uniform_int_distribution<int> N_dist(1, kMaxNumNodes);
203 for (
int i = 0; i < kNumTests; ++i) {
204 const int N = N_dist(rng);
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));
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));
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;
222 CHECK(ground_truth == it);
230 TEST(IdTest, LowerBoundFuzzWithStruct) {
231 constexpr
int kMaxTimeIncrement = 20;
232 constexpr
int kMaxNumNodes = 20;
233 constexpr
int kNumTests = 100;
234 constexpr
int kTrajectoryId = 1;
237 std::uniform_int_distribution<int> dt_dist(1, kMaxTimeIncrement);
238 std::uniform_int_distribution<int> N_dist(1, kMaxNumNodes);
240 for (
int i = 0; i < kNumTests; ++i) {
241 const int N = N_dist(rng);
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)});
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));
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,
258 CHECK(ground_truth == it);
common::Duration FromMilliseconds(const int64 milliseconds)
UniversalTimeScaleClock::time_point Time
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)