Skip to content

Commit 1ea7c39

Browse files
committed
Full union find test
1 parent 3c6e303 commit 1ea7c39

File tree

6 files changed

+138
-256
lines changed

6 files changed

+138
-256
lines changed

src/DataStructure/Set/QuickUnion.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ namespace DataStructure.Set
22
{
33
public class QuickUnion : UnionFindBase
44
{
5-
public int Count { get; private set; }
6-
75
public QuickUnion(int length) : base(length){}
86

97
public override void Connect(int p, int q)

tests/DataStructureTests/DataStructureTests.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
<ItemGroup>
4747
<Compile Include="Collection\SimpleLinkedList\SimpleLinkedListTests.cs" />
4848
<Compile Include="Properties\AssemblyInfo.cs" />
49-
<Compile Include="Set\WeightedQuickUnionTests.cs" />
50-
<Compile Include="Set\QuickUnionTests.cs" />
51-
<Compile Include="Set\QuickFindTests.cs" />
49+
<Compile Include="Set\UnionFindTests.cs" />
5250
</ItemGroup>
5351
<ItemGroup>
5452
<ProjectReference Include="..\..\src\DataStructure\DataStructure.csproj">

tests/DataStructureTests/Set/QuickFindTests.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

tests/DataStructureTests/Set/QuickUnionTests.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using DataStructure.Set;
5+
using NUnit.Framework;
6+
7+
namespace DataStructureTests.Set
8+
{
9+
public class UnionFindTests
10+
{
11+
private class UnionFindClasses
12+
{
13+
public static IEnumerable TestCases
14+
{
15+
get
16+
{
17+
yield return new TestCaseData(new Func<int, IUnionFind>(size => new QuickFind(size))).SetName("QuickFind");
18+
yield return new TestCaseData(new Func<int, IUnionFind>(size => new QuickUnion(size))).SetName("QuickUnion");
19+
yield return new TestCaseData(new Func<int, IUnionFind>(size => new WeightedQuickUnion(size))).SetName("WeightedQuickUnion");
20+
}
21+
}
22+
}
23+
24+
[Test, TestCaseSource(typeof(UnionFindClasses), nameof(UnionFindClasses.TestCases))]
25+
public void P_should_be_connected_to_is_self(Func<int, IUnionFind> ufFactory)
26+
{
27+
const int size = 10;
28+
var unionFind = ufFactory(size);
29+
30+
Assert.That(unionFind.Count, Is.EqualTo(size));
31+
32+
foreach (var i in Enumerable.Range(0, size))
33+
{
34+
Assert.That(unionFind.IsConnected(i, i), Is.True);
35+
Assert.That(unionFind.Find(i), Is.EqualTo(i));
36+
}
37+
}
38+
39+
[Test, TestCaseSource(typeof(UnionFindClasses), nameof(UnionFindClasses.TestCases))]
40+
public void Q_should_be_connected_to_P_After_connecting_Q_to_P(Func<int, IUnionFind> ufFactory)
41+
{
42+
int size = 2;
43+
var unionFind = ufFactory(size);
44+
var Q = 0;
45+
var P = 1;
46+
47+
unionFind.Connect(Q, P);
48+
49+
Assert.That(unionFind.IsConnected(Q, P), Is.True);
50+
Assert.That(unionFind.Find(Q),Is.EqualTo(unionFind.Find(P)));
51+
Assert.That(unionFind.Count, Is.EqualTo(--size));
52+
}
53+
54+
[Test, TestCaseSource(typeof(UnionFindClasses), nameof(UnionFindClasses.TestCases))]
55+
public void Q_should_be_connected_to_P_when_P_is_connected_to_Q(Func<int, IUnionFind> ufFactory)
56+
{
57+
var size = 2;
58+
var unionFind = ufFactory(size);
59+
var Q = 0;
60+
var P = 1;
61+
62+
unionFind.Connect(Q, P);
63+
64+
Assert.That(unionFind.IsConnected(Q, P), Is.EqualTo(unionFind.IsConnected(P, Q)));
65+
66+
}
67+
68+
[Test, TestCaseSource(typeof(UnionFindClasses), nameof(UnionFindClasses.TestCases))]
69+
public void P_should_be_connected_to_R_when_P_is_connected_to_Q_and_Q_is_connected_R(Func<int, IUnionFind> ufFactory)
70+
{
71+
var size = 3;
72+
var unionFind = ufFactory(size);
73+
74+
var Q = 0;
75+
var P = 1;
76+
var R = 2;
77+
78+
unionFind.Connect(P, Q);
79+
unionFind.Connect(Q, R);
80+
81+
Assert.That(unionFind.IsConnected(P, R), Is.True);
82+
Assert.That(unionFind.Find(Q), Is.EqualTo(unionFind.Find(R)));
83+
Assert.That(unionFind.Count, Is.EqualTo(1));
84+
85+
}
86+
87+
[Test, TestCaseSource(typeof(UnionFindClasses), nameof(UnionFindClasses.TestCases))]
88+
public void Senarii_tests(Func<int, IUnionFind> ufFactory)
89+
{
90+
var size = 10;
91+
var unionFind = ufFactory(size);
92+
93+
Assert.That(unionFind.Count, Is.EqualTo(size));
94+
95+
unionFind.Connect(4, 3);
96+
Assert.That(unionFind.IsConnected(4, 3), Is.True);
97+
Assert.That(unionFind.IsConnected(3, 4), Is.True);
98+
Assert.That(unionFind.Count, Is.EqualTo(--size));
99+
100+
unionFind.Connect(3, 8);
101+
Assert.That(unionFind.IsConnected(4, 8), Is.True);
102+
Assert.That(unionFind.Count, Is.EqualTo(--size));
103+
104+
unionFind.Connect(6, 5);
105+
Assert.That(unionFind.IsConnected(6, 5), Is.True);
106+
Assert.That(unionFind.Count, Is.EqualTo(--size));
107+
108+
unionFind.Connect(9, 4);
109+
Assert.That(unionFind.IsConnected(3, 9), Is.True);
110+
Assert.That(unionFind.Count, Is.EqualTo(--size));
111+
112+
unionFind.Connect(2, 1);
113+
Assert.That(unionFind.IsConnected(1, 2), Is.True);
114+
Assert.That(unionFind.Count, Is.EqualTo(--size));
115+
116+
unionFind.Connect(5, 0);
117+
Assert.That(unionFind.IsConnected(0, 5), Is.True);
118+
Assert.That(unionFind.Count, Is.EqualTo(--size));
119+
120+
unionFind.Connect(7, 2);
121+
Assert.That(unionFind.IsConnected(7, 1), Is.True);
122+
Assert.That(unionFind.Count, Is.EqualTo(--size));
123+
124+
unionFind.Connect(6, 1);
125+
Assert.That(unionFind.IsConnected(1, 5), Is.True);
126+
Assert.That(unionFind.Count, Is.EqualTo(--size));
127+
128+
unionFind.Connect(7, 3);
129+
Assert.That(unionFind.IsConnected(3, 7), Is.True);
130+
Assert.That(unionFind.Count, Is.EqualTo(--size));
131+
132+
}
133+
134+
135+
136+
}
137+
}

0 commit comments

Comments
 (0)