Example #1
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));
        }
Example #2
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()));
        }
Example #3
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));
        }