|
| 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