23 #include "gtest/gtest.h" 29 constexpr
int kNumTrajectories = 10;
31 TEST(ConnectedComponentsTest, TransitivelyConnected) {
32 ConnectedComponents connected_components;
35 for (
int trajectory_a = 0; trajectory_a < kNumTrajectories; ++trajectory_a) {
36 for (
int trajectory_b = 0; trajectory_b < kNumTrajectories;
38 EXPECT_EQ(trajectory_a == trajectory_b,
39 connected_components.TransitivelyConnected(trajectory_a,
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));
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));
59 TEST(ConnectedComponentsTest, EmptyConnectedComponents) {
60 ConnectedComponents connected_components;
61 auto connections = connected_components.Components();
62 EXPECT_EQ(0, connections.size());
65 TEST(ConnectedComponentsTest, ConnectedComponents) {
66 ConnectedComponents connected_components;
67 for (
int i = 0; i <= 4; ++i) {
68 connected_components.Connect(0, i);
70 for (
int i = 5; i <= 9; ++i) {
71 connected_components.Connect(5, i);
73 auto connections = connected_components.Components();
74 ASSERT_EQ(2, connections.size());
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];
83 zero_cluster = &connections[1];
84 five_cluster = &connections[0];
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) !=
94 TEST(ConnectedComponentsTest, ConnectionCount) {
95 ConnectedComponents connected_components;
96 for (
int i = 0; i < kNumTrajectories; ++i) {
97 connected_components.Connect(0, 1);
99 EXPECT_EQ(i + 1, connected_components.ConnectionCount(1, 0));
101 for (
int i = 1; i < 9; ++i) {
102 EXPECT_EQ(0, connected_components.ConnectionCount(i, i + 1));
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));
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)