23 #include "gtest/gtest.h" 29 constexpr
int kNumTrajectories = 10;
31 TEST(TrajectoryConnectivityTest, TransitivelyConnected) {
32 TrajectoryConnectivity trajectory_connectivity;
35 for (
int trajectory_a = 0; trajectory_a < kNumTrajectories; ++trajectory_a) {
36 for (
int trajectory_b = 0; trajectory_b < kNumTrajectories;
38 EXPECT_FALSE(trajectory_connectivity.TransitivelyConnected(trajectory_a,
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));
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));
58 TEST(TrajectoryConnectivityTest, EmptyConnectedComponents) {
59 TrajectoryConnectivity trajectory_connectivity;
60 auto connections = trajectory_connectivity.ConnectedComponents();
61 EXPECT_EQ(0, connections.size());
64 TEST(TrajectoryConnectivityTest, ConnectedComponents) {
65 TrajectoryConnectivity trajectory_connectivity;
66 for (
int i = 0; i <= 4; ++i) {
67 trajectory_connectivity.Connect(0, i);
69 for (
int i = 5; i <= 9; ++i) {
70 trajectory_connectivity.Connect(5, i);
72 auto connections = trajectory_connectivity.ConnectedComponents();
73 ASSERT_EQ(2, connections.size());
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];
82 zero_cluster = &connections[1];
83 five_cluster = &connections[0];
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) !=
93 TEST(TrajectoryConnectivityTest, ConnectionCount) {
94 TrajectoryConnectivity trajectory_connectivity;
95 for (
int i = 0; i < kNumTrajectories; ++i) {
96 trajectory_connectivity.Connect(0, 1);
98 EXPECT_EQ(i + 1, trajectory_connectivity.ConnectionCount(1, 0));
100 for (
int i = 1; i < 9; ++i) {
101 EXPECT_EQ(0, trajectory_connectivity.ConnectionCount(i, i + 1));