コード例 #1
0
        public void Difference()
        {
            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.DifferenceWith(bag2);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 11, 11, 13, 15, 17, 17, 19 }, false);

            bag1 = bagOdds.Clone();
            bag2 = bagDigits.Clone();
            bag2.DifferenceWith(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 2, 2, 4, 5, 6, 7, 7, 7, 7, 8 }, false);

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

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

            bag1 = bagOdds.Clone();
            bag2 = bagDigits.Clone();
            bag3 = bag2.Difference(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 2, 2, 4, 5, 6, 7, 7, 7, 7, 8 }, false);

            // Make sure intersection with itself works.
            bag1 = bagDigits.Clone();
            bag1.DifferenceWith(bag1);
            Assert.AreEqual(0, bag1.Count);

            bag1 = bagDigits.Clone();
            bag3 = bag1.Difference(bag1);
            Assert.AreEqual(0, bag3.Count);
        }