Exemple #1
0
        public void DisjointSetForestMakeSetTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in this.values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            // testing if added correctly

            disjointSetInt.Count.ShouldBe(this.values.Length);
            disjointSetInt.SetCount.ShouldBe(this.values.Length);

            disjointSetString.Count.ShouldBe(this.values.Length);
            disjointSetString.SetCount.ShouldBe(this.values.Length);

            foreach (Int32 value in this.values)
            {
                value.ShouldBe(disjointSetInt.FindSet(value));
                disjointSetString.FindSet(value.ToString()).ShouldBe(value.ToString());
            }

            // testing element uniqueness
            for (Int32 i = 0; i < 20; i++)
            {
                disjointSetInt.MakeSet(this.values[0]);
                disjointSetString.MakeSet(this.values[0].ToString());
            }

            disjointSetInt.Count.ShouldBe(this.values.Length);
            disjointSetInt.SetCount.ShouldBe(this.values.Length);

            disjointSetString.Count.ShouldBe(this.values.Length);
            disjointSetString.SetCount.ShouldBe(this.values.Length);

            // exceptions

            Should.Throw <ArgumentNullException>(() => new DisjointSetForest <String>().MakeSet(null));
        }
        public void DisjointSetForestMakeSetTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in _values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            // testing if added correctly

            Assert.AreEqual(_values.Length, disjointSetInt.Count);
            Assert.AreEqual(_values.Length, disjointSetInt.SetCount);

            Assert.AreEqual(_values.Length, disjointSetString.Count);
            Assert.AreEqual(_values.Length, disjointSetString.SetCount);

            foreach (Int32 value in _values)
            {
                Assert.AreEqual(disjointSetInt.Find(value), value);
                Assert.AreEqual(disjointSetString.Find(value.ToString()), value.ToString());
            }

            // testing element uniqueness
            for (Int32 i = 0; i < 20; i++)
            {
                disjointSetInt.MakeSet(_values[0]);
                disjointSetString.MakeSet(_values[0].ToString());
            }

            Assert.AreEqual(_values.Length, disjointSetInt.Count);
            Assert.AreEqual(_values.Length, disjointSetInt.SetCount);

            Assert.AreEqual(_values.Length, disjointSetString.Count);
            Assert.AreEqual(_values.Length, disjointSetString.SetCount);

            // exceptions

            Assert.Throws <ArgumentNullException>(() => new DisjointSetForest <String>().MakeSet(null));
        }
Exemple #3
0
        public void DisjointSetForestJoinSetsTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in this.values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            for (Int32 i = 0; i < this.values.Length; i = i + 2)
            {
                disjointSetInt.JoinSets(this.values[i], this.values[i + 1]);
                disjointSetString.JoinSets(this.values[i].ToString(), this.values[i + 1].ToString());
            }

            // testing representatives

            for (Int32 i = 0; i < this.values.Length; i = i + 2)
            {
                disjointSetInt.FindSet(this.values[i + 1]).ShouldBe(disjointSetInt.FindSet(this.values[i]));
                disjointSetString.FindSet(this.values[i + 1].ToString()).ShouldBe(disjointSetString.FindSet(this.values[i].ToString()));
            }

            // testing setCounts

            disjointSetInt.SetCount.ShouldBe(this.values.Length / 2);
            disjointSetString.SetCount.ShouldBe(this.values.Length / 2);

            disjointSetInt.Count.ShouldBe(this.values.Length);
            disjointSetString.Count.ShouldBe(this.values.Length);

            // exceptions

            Should.Throw <ArgumentException>(() => disjointSetInt.JoinSets(this.values[0], 1000));
            Should.Throw <ArgumentException>(() => disjointSetInt.JoinSets(1000, this.values[0]));
            Should.Throw <ArgumentException>(() => disjointSetInt.JoinSets(1000, 101));

            Should.Throw <ArgumentException>(() => disjointSetString.JoinSets(this.values[0].ToString(), 1000.ToString()));
            Should.Throw <ArgumentException>(() => disjointSetString.JoinSets(1000.ToString(), this.values[0].ToString()));
            Should.Throw <ArgumentException>(() => disjointSetString.JoinSets(1000.ToString(), 101.ToString()));

            Should.Throw <ArgumentNullException>(() => disjointSetString.JoinSets(this.values[0].ToString(), null));
            Should.Throw <ArgumentNullException>(() => disjointSetString.JoinSets(null, this.values[0].ToString()));
            Should.Throw <ArgumentNullException>(() => disjointSetString.JoinSets(null, null));
        }
        public void DisjointSetForestUnionTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in _values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            for (Int32 i = 0; i < _values.Length; i = i + 2)
            {
                disjointSetInt.Union(_values[i], _values[i + 1]);
                disjointSetString.Union(_values[i].ToString(), _values[i + 1].ToString());
            }

            // testing representatives

            for (Int32 i = 0; i < _values.Length; i = i + 2)
            {
                Assert.AreEqual(disjointSetInt.Find(_values[i]), disjointSetInt.Find(_values[i + 1]));
                Assert.AreEqual(disjointSetString.Find(_values[i].ToString()), disjointSetString.Find(_values[i + 1].ToString()));
            }

            // testing setCounts

            Assert.AreEqual(disjointSetInt.SetCount, _values.Length / 2);
            Assert.AreEqual(disjointSetString.SetCount, _values.Length / 2);

            Assert.AreEqual(disjointSetInt.Count, _values.Length);
            Assert.AreEqual(disjointSetString.Count, _values.Length);

            // exceptions

            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(_values[0], 100));
            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(100, _values[0]));
            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(100, 101));

            Assert.Throws <ArgumentException>(() => disjointSetString.Union(_values[0].ToString(), 100.ToString()));
            Assert.Throws <ArgumentException>(() => disjointSetString.Union(100.ToString(), _values[0].ToString()));
            Assert.Throws <ArgumentException>(() => disjointSetString.Union(100.ToString(), 101.ToString()));

            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(_values[0].ToString(), null));
            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(null, _values[0].ToString()));
            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(null, null));
        }
Exemple #5
0
        public void DisjointSetForestFindTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in this.values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            foreach (Int32 value in this.values)
            {
                value.ShouldBe(disjointSetInt.FindSet(value));
                disjointSetString.FindSet(value.ToString()).ShouldBe(value.ToString());
            }

            // exceptions

            Should.Throw <ArgumentNullException>(() => disjointSetString.FindSet(null));
            Should.Throw <ArgumentException>(() => disjointSetInt.FindSet(10000));
            Should.Throw <ArgumentException>(() => disjointSetString.FindSet(10000.ToString()));
        }
        public void DisjointSetForestFindTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in _values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            foreach (Int32 value in _values)
            {
                Assert.AreEqual(disjointSetInt.Find(value), value);
                Assert.AreEqual(disjointSetString.Find(value.ToString()), value.ToString());
            }

            // exceptions

            Assert.Throws <ArgumentNullException>(() => disjointSetString.Find(null));
            Assert.Throws <ArgumentException>(() => disjointSetInt.Find(100));
            Assert.Throws <ArgumentException>(() => disjointSetString.Find(100.ToString()));
        }