ordered_multi_queue_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 <vector>
20 
22 #include "gtest/gtest.h"
23 
24 namespace cartographer {
25 namespace sensor {
26 namespace {
27 
28 class OrderedMultiQueueTest : public ::testing::Test {
29  protected:
30  // These are keys are chosen so that they sort first, second, third.
31  const QueueKey kFirst{1, "a"};
32  const QueueKey kSecond{1, "b"};
33  const QueueKey kThird{2, "b"};
34 
35  void SetUp() {
36  for (const auto& queue_key : {kFirst, kSecond, kThird}) {
37  queue_.AddQueue(queue_key, [this](std::unique_ptr<Data> data) {
38  if (!values_.empty()) {
39  EXPECT_GE(data->GetTime(), values_.back()->GetTime());
40  }
41  values_.push_back(std::move(data));
42  });
43  }
44  }
45 
46  std::unique_ptr<Data> MakeImu(const int ordinal) {
47  return MakeDispatchable(
48  "imu",
49  sensor::ImuData{common::FromUniversal(ordinal), Eigen::Vector3d::Zero(),
50  Eigen::Vector3d::Zero()});
51  }
52 
53  std::vector<std::unique_ptr<Data>> values_;
54  OrderedMultiQueue queue_;
55 };
56 
57 TEST_F(OrderedMultiQueueTest, Ordering) {
58  queue_.Add(kFirst, MakeImu(0));
59  queue_.Add(kFirst, MakeImu(4));
60  queue_.Add(kFirst, MakeImu(5));
61  queue_.Add(kFirst, MakeImu(6));
62  EXPECT_TRUE(values_.empty());
63  queue_.Add(kSecond, MakeImu(0));
64  queue_.Add(kSecond, MakeImu(1));
65  EXPECT_TRUE(values_.empty());
66  queue_.Add(kThird, MakeImu(0));
67  queue_.Add(kThird, MakeImu(2));
68  EXPECT_EQ(values_.size(), 4);
69  queue_.Add(kSecond, MakeImu(3));
70  EXPECT_EQ(values_.size(), 5);
71  queue_.Add(kSecond, MakeImu(7));
72  queue_.Add(kThird, MakeImu(8));
73  queue_.Flush();
74 
75  EXPECT_EQ(11, values_.size());
76  for (size_t i = 0; i < values_.size() - 1; ++i) {
77  EXPECT_LE(values_[i]->GetTime(), values_[i + 1]->GetTime());
78  }
79 }
80 
81 TEST_F(OrderedMultiQueueTest, MarkQueueAsFinished) {
82  queue_.Add(kFirst, MakeImu(1));
83  queue_.Add(kFirst, MakeImu(2));
84  queue_.Add(kFirst, MakeImu(3));
85  EXPECT_TRUE(values_.empty());
86  queue_.MarkQueueAsFinished(kFirst);
87  EXPECT_TRUE(values_.empty());
88  queue_.MarkQueueAsFinished(kSecond);
89  EXPECT_TRUE(values_.empty());
90  queue_.MarkQueueAsFinished(kThird);
91 
92  EXPECT_EQ(3, values_.size());
93  for (size_t i = 0; i < values_.size(); ++i) {
94  EXPECT_EQ(i + 1, common::ToUniversal(values_[i]->GetTime()));
95  }
96 }
97 
98 TEST_F(OrderedMultiQueueTest, CommonStartTimePerTrajectory) {
99  queue_.Add(kFirst, MakeImu(0));
100  queue_.Add(kFirst, MakeImu(1));
101  queue_.Add(kFirst, MakeImu(2));
102  queue_.Add(kFirst, MakeImu(3));
103  queue_.Add(kSecond, MakeImu(2));
104  EXPECT_TRUE(values_.empty());
105  queue_.Add(kThird, MakeImu(4));
106  EXPECT_EQ(values_.size(), 2);
107  queue_.MarkQueueAsFinished(kFirst);
108  EXPECT_EQ(values_.size(), 2);
109  queue_.MarkQueueAsFinished(kSecond);
110  EXPECT_EQ(values_.size(), 4);
111  queue_.MarkQueueAsFinished(kThird);
112  EXPECT_EQ(values_.size(), 4);
113 }
114 
115 } // namespace
116 } // namespace sensor
117 } // namespace cartographer
const QueueKey kThird
const QueueKey kFirst
OrderedMultiQueue queue_
common::Time GetTime(const T &t)
Definition: id.h:49
Time FromUniversal(const int64 ticks)
Definition: time.cc:34
int64 ToUniversal(const Time time)
Definition: time.cc:36
std::vector< std::unique_ptr< Data > > values_
std::unique_ptr< Dispatchable< DataType > > MakeDispatchable(const std::string &sensor_id, const DataType &data)
Definition: dispatchable.h:44
const QueueKey kSecond


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