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->time, values_.back().time);
40  }
41  values_.push_back(*data);
42  });
43  }
44  }
45 
46  std::unique_ptr<Data> MakeImu(const int ordinal) {
47  return common::make_unique<Data>(
48  common::FromUniversal(ordinal),
49  Data::Imu{Eigen::Vector3d::Zero(), Eigen::Vector3d::Zero()});
50  }
51 
52  std::vector<Data> values_;
53  OrderedMultiQueue queue_;
54 };
55 
56 TEST_F(OrderedMultiQueueTest, Ordering) {
57  queue_.Add(kFirst, MakeImu(0));
58  queue_.Add(kFirst, MakeImu(4));
59  queue_.Add(kFirst, MakeImu(5));
60  queue_.Add(kFirst, MakeImu(6));
61  EXPECT_TRUE(values_.empty());
62  queue_.Add(kSecond, MakeImu(0));
63  queue_.Add(kSecond, MakeImu(1));
64  EXPECT_TRUE(values_.empty());
65  queue_.Add(kThird, MakeImu(0));
66  queue_.Add(kThird, MakeImu(2));
67  EXPECT_EQ(values_.size(), 4);
68  queue_.Add(kSecond, MakeImu(3));
69  EXPECT_EQ(values_.size(), 5);
70  queue_.Add(kSecond, MakeImu(7));
71  queue_.Add(kThird, MakeImu(8));
72  queue_.Flush();
73 
74  EXPECT_EQ(11, values_.size());
75  for (size_t i = 0; i < values_.size() - 1; ++i) {
76  EXPECT_LE(values_[i].time, values_[i + 1].time);
77  }
78 }
79 
80 TEST_F(OrderedMultiQueueTest, MarkQueueAsFinished) {
81  queue_.Add(kFirst, MakeImu(1));
82  queue_.Add(kFirst, MakeImu(2));
83  queue_.Add(kFirst, MakeImu(3));
84  EXPECT_TRUE(values_.empty());
85  queue_.MarkQueueAsFinished(kFirst);
86  EXPECT_TRUE(values_.empty());
87  queue_.MarkQueueAsFinished(kSecond);
88  EXPECT_TRUE(values_.empty());
89  queue_.MarkQueueAsFinished(kThird);
90 
91  EXPECT_EQ(3, values_.size());
92  for (size_t i = 0; i < values_.size(); ++i) {
93  EXPECT_EQ(i + 1, common::ToUniversal(values_[i].time));
94  }
95 }
96 
97 TEST_F(OrderedMultiQueueTest, CommonStartTimePerTrajectory) {
98  queue_.Add(kFirst, MakeImu(0));
99  queue_.Add(kFirst, MakeImu(1));
100  queue_.Add(kFirst, MakeImu(2));
101  queue_.Add(kFirst, MakeImu(3));
102  queue_.Add(kSecond, MakeImu(2));
103  EXPECT_TRUE(values_.empty());
104  queue_.Add(kThird, MakeImu(4));
105  EXPECT_EQ(values_.size(), 2);
106  queue_.MarkQueueAsFinished(kFirst);
107  EXPECT_EQ(values_.size(), 2);
108  queue_.MarkQueueAsFinished(kSecond);
109  EXPECT_EQ(values_.size(), 4);
110  queue_.MarkQueueAsFinished(kThird);
111  EXPECT_EQ(values_.size(), 4);
112 }
113 
114 } // namespace
115 } // namespace sensor
116 } // namespace cartographer
std::vector< Data > values_
const QueueKey kThird
const QueueKey kFirst
OrderedMultiQueue queue_
Time FromUniversal(const int64 ticks)
Definition: time.cc:34
int64 ToUniversal(const Time time)
Definition: time.cc:36
const QueueKey kSecond


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39