trajectory_connectivity_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 <memory>
21 #include <vector>
22 
23 #include "gtest/gtest.h"
24 
25 namespace cartographer {
26 namespace mapping {
27 namespace {
28 
29 constexpr int kNumTrajectories = 10;
30 
31 TEST(TrajectoryConnectivityTest, TransitivelyConnected) {
32  TrajectoryConnectivity trajectory_connectivity;
33 
34  // Make sure nothing's connected until we connect some things.
35  for (int trajectory_a = 0; trajectory_a < kNumTrajectories; ++trajectory_a) {
36  for (int trajectory_b = 0; trajectory_b < kNumTrajectories;
37  ++trajectory_b) {
38  EXPECT_FALSE(trajectory_connectivity.TransitivelyConnected(trajectory_a,
39  trajectory_b));
40  }
41  }
42 
43  // Connect some stuff up.
44  trajectory_connectivity.Connect(0, 1);
45  EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(0, 1));
46  trajectory_connectivity.Connect(8, 9);
47  EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(8, 9));
48  EXPECT_FALSE(trajectory_connectivity.TransitivelyConnected(0, 9));
49 
50  trajectory_connectivity.Connect(1, 8);
51  for (int i : {0, 1}) {
52  for (int j : {8, 9}) {
53  EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(i, j));
54  }
55  }
56 }
57 
58 TEST(TrajectoryConnectivityTest, EmptyConnectedComponents) {
59  TrajectoryConnectivity trajectory_connectivity;
60  auto connections = trajectory_connectivity.ConnectedComponents();
61  EXPECT_EQ(0, connections.size());
62 }
63 
64 TEST(TrajectoryConnectivityTest, ConnectedComponents) {
65  TrajectoryConnectivity trajectory_connectivity;
66  for (int i = 0; i <= 4; ++i) {
67  trajectory_connectivity.Connect(0, i);
68  }
69  for (int i = 5; i <= 9; ++i) {
70  trajectory_connectivity.Connect(5, i);
71  }
72  auto connections = trajectory_connectivity.ConnectedComponents();
73  ASSERT_EQ(2, connections.size());
74  // The clustering is arbitrary; we need to figure out which one is which.
75  const std::vector<int>* zero_cluster = nullptr;
76  const std::vector<int>* five_cluster = nullptr;
77  if (std::find(connections[0].begin(), connections[0].end(), 0) !=
78  connections[0].end()) {
79  zero_cluster = &connections[0];
80  five_cluster = &connections[1];
81  } else {
82  zero_cluster = &connections[1];
83  five_cluster = &connections[0];
84  }
85  for (int i = 0; i <= 9; ++i) {
86  EXPECT_EQ(i <= 4, std::find(zero_cluster->begin(), zero_cluster->end(),
87  i) != zero_cluster->end());
88  EXPECT_EQ(i > 4, std::find(five_cluster->begin(), five_cluster->end(), i) !=
89  five_cluster->end());
90  }
91 }
92 
93 TEST(TrajectoryConnectivityTest, ConnectionCount) {
94  TrajectoryConnectivity trajectory_connectivity;
95  for (int i = 0; i < kNumTrajectories; ++i) {
96  trajectory_connectivity.Connect(0, 1);
97  // Permute the arguments to check invariance.
98  EXPECT_EQ(i + 1, trajectory_connectivity.ConnectionCount(1, 0));
99  }
100  for (int i = 1; i < 9; ++i) {
101  EXPECT_EQ(0, trajectory_connectivity.ConnectionCount(i, i + 1));
102  }
103 }
104 
105 } // namespace
106 } // namespace mapping
107 } // namespace cartographer


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