Beispiel #1
0
        public void Union()
        {
            var bagOdds   = new Bag <int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
            var bagDigits = new Bag <int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });

            // Algorithms work different depending on sizes, so try both ways.
            Bag <int> bag1 = bagOdds.Clone();
            Bag <int> bag2 = bagDigits.Clone();

            bag1.UnionWith(bag2);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1,
                                                          new int[]
            {
                1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15,
                17, 17, 19
            }, false);

            bag1 = bagOdds.Clone();
            bag2 = bagDigits.Clone();
            bag2.UnionWith(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag2,
                                                          new int[]
            {
                1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15,
                17, 17, 19
            }, false);

            bag1 = bagOdds.Clone();
            bag2 = bagDigits.Clone();
            Bag <int> bag3 = bag1.Union(bag2);

            InterfaceTests.TestReadWriteCollectionGeneric(bag3,
                                                          new int[]
            {
                1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15,
                17, 17, 19
            }, false);

            bag1 = bagOdds.Clone();
            bag2 = bagDigits.Clone();
            bag3 = bag2.Union(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3,
                                                          new int[]
            {
                1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15,
                17, 17, 19
            }, false);

            // Make sure intersection with itself works.
            bag1 = bagDigits.Clone();
            bag1.UnionWith(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 },
                                                          false);

            bag1 = bagDigits.Clone();
            bag3 = bag1.Union(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 },
                                                          false);
        }
Beispiel #2
0
        public void Clone()
        {
            var bag1 = new Bag <int>(new int[] { 1, 7, 9, 11, 7, 13, 15, -17, 19, -21, 1 });

            var bag2 = bag1.Clone();
            var bag3 = bag1.Clone(); //(Bag<int>) ((ICloneable) bag1).Clone();

            Assert.IsFalse(bag2 == bag1);
            Assert.IsFalse(bag3 == bag1);

            // Modify bag1, make sure bag2, bag3 don't change.
            bag1.Remove(9);
            bag1.Remove(-17);
            bag1.Add(8);

            InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);

            bag1 = new Bag <int>();
            bag2 = bag1.Clone();
            Assert.IsFalse(bag2 == bag1);
            Assert.IsTrue(bag1.Count == 0 && bag2.Count == 0);
        }