connected_components_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(ConnectedComponentsTest, TransitivelyConnected) {
32  ConnectedComponents connected_components;
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_EQ(trajectory_a == trajectory_b,
39  connected_components.TransitivelyConnected(trajectory_a,
40  trajectory_b));
41  }
42  }
43 
44  // Connect some stuff up.
45  connected_components.Connect(0, 1);
46  EXPECT_TRUE(connected_components.TransitivelyConnected(0, 1));
47  connected_components.Connect(8, 9);
48  EXPECT_TRUE(connected_components.TransitivelyConnected(8, 9));
49  EXPECT_FALSE(connected_components.TransitivelyConnected(0, 9));
50 
51  connected_components.Connect(1, 8);
52  for (int i : {0, 1}) {
53  for (int j : {8, 9}) {
54  EXPECT_TRUE(connected_components.TransitivelyConnected(i, j));
55  }
56  }
57 }
58 
59 TEST(ConnectedComponentsTest, EmptyConnectedComponents) {
60  ConnectedComponents connected_components;
61  auto connections = connected_components.Components();
62  EXPECT_EQ(0, connections.size());
63 }
64 
65 TEST(ConnectedComponentsTest, ConnectedComponents) {
66  ConnectedComponents connected_components;
67  for (int i = 0; i <= 4; ++i) {
68  connected_components.Connect(0, i);
69  }
70  for (int i = 5; i <= 9; ++i) {
71  connected_components.Connect(5, i);
72  }
73  auto connections = connected_components.Components();
74  ASSERT_EQ(2, connections.size());
75  // The clustering is arbitrary; we need to figure out which one is which.
76  const std::vector<int>* zero_cluster = nullptr;
77  const std::vector<int>* five_cluster = nullptr;
78  if (std::find(connections[0].begin(), connections[0].end(), 0) !=
79  connections[0].end()) {
80  zero_cluster = &connections[0];
81  five_cluster = &connections[1];
82  } else {
83  zero_cluster = &connections[1];
84  five_cluster = &connections[0];
85  }
86  for (int i = 0; i <= 9; ++i) {
87  EXPECT_EQ(i <= 4, std::find(zero_cluster->begin(), zero_cluster->end(),
88  i) != zero_cluster->end());
89  EXPECT_EQ(i > 4, std::find(five_cluster->begin(), five_cluster->end(), i) !=
90  five_cluster->end());
91  }
92 }
93 
94 TEST(ConnectedComponentsTest, ConnectionCount) {
95  ConnectedComponents connected_components;
96  for (int i = 0; i < kNumTrajectories; ++i) {
97  connected_components.Connect(0, 1);
98  // Permute the arguments to check invariance.
99  EXPECT_EQ(i + 1, connected_components.ConnectionCount(1, 0));
100  }
101  for (int i = 1; i < 9; ++i) {
102  EXPECT_EQ(0, connected_components.ConnectionCount(i, i + 1));
103  }
104 }
105 
106 TEST(ConnectedComponentsTest, ReflexiveConnectivity) {
107  ConnectedComponents connected_components;
108  EXPECT_TRUE(connected_components.TransitivelyConnected(0, 0));
109  EXPECT_EQ(0, connected_components.ConnectionCount(0, 0));
110  connected_components.Add(0);
111  EXPECT_TRUE(connected_components.TransitivelyConnected(0, 0));
112  EXPECT_EQ(0, connected_components.ConnectionCount(0, 0));
113 }
114 
115 } // namespace
116 } // namespace mapping
117 } // namespace cartographer
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)


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