Skip to content

Commit 4ed91cd

Browse files
committed
Add new tests.
1 parent 80c194f commit 4ed91cd

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,140 @@
11
//
22
// Created by andreas on 02.02.23.
33
//
4+
5+
#include <gtest/gtest.h>
6+
#include <vector>
7+
#include <optional>
8+
49
#include "union_find_disjoint_set/union_find_dense.h"
510

11+
// Helper: connect all nodes in vec to vec[0]
12+
template <typename UnionFindType, typename T>
13+
static void UniteAll(UnionFindType& union_find, const std::vector<T>& vec)
14+
{
15+
for (size_t i = 1; i < vec.size(); ++i)
16+
{
17+
union_find.union_set(vec[0], vec[i]);
18+
}
19+
}
20+
21+
TEST(UnionFindDense, InitiallyAllDisconnected)
22+
{
23+
constexpr int elements{10};
24+
UnionFindDense<int> union_find(elements);
25+
26+
for (int i{}; i < elements; ++i)
27+
{
28+
for (int j{}; j < elements; ++j)
29+
{
30+
if (i==j)
31+
EXPECT_TRUE(union_find.are_connected(i, j));
32+
else
33+
EXPECT_FALSE(union_find.are_connected(i, j));
34+
}
35+
}
36+
}
37+
38+
TEST(UnionFindDense, SingleComponentViaStarUnion)
39+
{
40+
UnionFindDense<int> union_find(10);
41+
const std::vector<int> set1{0, 2, 4, 6, 8};
42+
43+
// initially different sets are disconnected
44+
for (size_t j = 0; j + 1 < set1.size(); ++j)
45+
for (size_t i = j + 1; i < set1.size(); ++i)
46+
EXPECT_FALSE(union_find.are_connected(set1[j], set1[i]));
47+
48+
UniteAll(union_find, set1);
49+
// all connected
50+
for (size_t j = 0; j < set1.size(); ++j)
51+
for (size_t i = 0; i < set1.size(); ++i)
52+
EXPECT_TRUE(union_find.are_connected(set1[j], set1[i]));
53+
}
54+
55+
TEST(UnionFindDense, TwoComponentsThenMerged)
56+
{
57+
UnionFindDense<int> union_find(10);
58+
std::vector<int> set1{0, 2, 4, 6, 8};
59+
std::vector<int> set2{1, 3, 5, 7, 9};
60+
61+
UniteAll(union_find, set1);
62+
UniteAll(union_find, set2);
63+
64+
// Across components: not connected
65+
for (int a : set1)
66+
for (int b : set2)
67+
EXPECT_FALSE(union_find.are_connected(a, b));
68+
69+
union_find.union_set(2, 3); // merge the two components
70+
71+
for (int a : set1)
72+
for (int b : set2)
73+
EXPECT_TRUE(union_find.are_connected(a, b));
74+
}
75+
76+
TEST(UnionFindDense, FindRootValidAndInvalid)
77+
{
78+
UnionFindDense<int> union_find(5);
79+
80+
for (int i = 0; i < 5; ++i)
81+
{
82+
auto r = union_find.find_root(i);
83+
EXPECT_TRUE(r.has_value());
84+
EXPECT_EQ(*r, i);
85+
}
86+
87+
EXPECT_FALSE(union_find.find_root(-1).has_value());
88+
EXPECT_FALSE(union_find.find_root(5).has_value());
89+
EXPECT_FALSE(union_find.find_root(100).has_value());
90+
}
91+
92+
TEST(UnionFindDense, InvalidInputsAreConnectedFalseUnionNoOp)
93+
{
94+
UnionFindDense<int> union_find(3);
95+
96+
// are_connected should be false if any input invalid
97+
EXPECT_FALSE(union_find.are_connected(-1, 0));
98+
EXPECT_FALSE(union_find.are_connected(0, 3));
99+
EXPECT_FALSE(union_find.are_connected(-1, 3));
100+
101+
// union_set should not crash and should not affect valid connectivity
102+
union_find.union_set(-1, 0);
103+
union_find.union_set(0, 3);
104+
union_find.union_set(-1, 3);
105+
106+
EXPECT_FALSE(union_find.are_connected(0, 1));
107+
EXPECT_FALSE(union_find.are_connected(1, 2));
108+
}
109+
110+
TEST(UnionFindDense, UnionIsIdempotentAndSelfUnionSafe)
111+
{
112+
UnionFindDense<int> union_find(4);
113+
114+
union_find.union_set(1, 2);
115+
EXPECT_TRUE(union_find.are_connected(1, 2));
116+
117+
// repeat / reverse should not change correctness
118+
union_find.union_set(1, 2);
119+
union_find.union_set(2, 1);
120+
EXPECT_TRUE(union_find.are_connected(1, 2));
121+
122+
// self union: should not break anything
123+
union_find.union_set(2, 2);
124+
EXPECT_TRUE(union_find.are_connected(1, 2));
125+
EXPECT_FALSE(union_find.are_connected(0, 1));
126+
}
127+
128+
TEST(UnionFindDense, TransitiveConnectivity)
129+
{
130+
UnionFindDense<int> union_find(6);
131+
132+
union_find.union_set(0, 1);
133+
union_find.union_set(1, 2);
134+
union_find.union_set(2, 3);
6135

136+
EXPECT_TRUE(union_find.are_connected(0, 3));
137+
EXPECT_TRUE(union_find.are_connected(1, 3));
138+
EXPECT_FALSE(union_find.are_connected(0, 4));
139+
EXPECT_FALSE(union_find.are_connected(3, 5));
140+
}

0 commit comments

Comments
 (0)