コード例 #1
0
        public void NumberOfCopies()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase)
            {
                null, "hello", "foo"
            };

            Assert.AreEqual(1, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(1, bag1.NumberOfCopies("FOO"));
            Assert.AreEqual(1, bag1.NumberOfCopies(null));
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            Assert.AreEqual(3, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(2, bag1.NumberOfCopies(null));
            bag1.Remove(null);
            bag1.Remove(null);
            Assert.AreEqual(0, bag1.NumberOfCopies(null));
        }
コード例 #2
0
        public void Remove()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            var b = bag1.Remove("Eric");

            Assert.IsFalse(b);
            bag1.Add("hello");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            b = bag1.Remove("hello");
            Assert.IsTrue(b);
            InterfaceTests.TestCollection(bag1, new string[] { null, null, "foo", "hello", "hello" }, false);
            b = bag1.Remove("Hello");
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsFalse(b);
            bag1.Add("Hello");
            bag1.Add("Eric");
            bag1.Add(null);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            bag1.Add("ERIC");
            b = bag1.Remove("eRic");
            Assert.IsTrue(b);
            b = bag1.Remove("eRic");
            Assert.IsTrue(b);
            bag1.Clear();
            b = bag1.Remove("");
            Assert.IsFalse(b);
        }
コード例 #3
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);
        }
コード例 #4
0
        public void RandomAddDelete()
        {
            const int SIZE  = 5000;
            var       count = new int[SIZE];
            var       rand  = new Random(3);
            var       bag1  = new Bag <int>();
            bool      b;

            // Add and delete values at random.
            for (int i = 0; i < SIZE * 100; ++i)
            {
                int v = rand.Next(SIZE);

                Assert.AreEqual(count[v], bag1.NumberOfCopies(v));
                if (count[v] > 0)
                {
                    Assert.IsTrue(bag1.Contains(v));
                }

                if (count[v] == 0 || rand.Next(2) == 1)
                {
                    // Add to the bag.
                    bag1.Add(v);
                    count[v] += 1;
                }
                else
                {
                    // Remove from the bag.
                    b = bag1.Remove(v);
                    Assert.IsTrue(b);
                    count[v] -= 1;
                }
            }

            // Make sure the bag has all the correct values, not necessarily in order.
            int c = count.Sum();

            Assert.AreEqual(c, bag1.Count);

            foreach (var v in bag1)
            {
                --count[v];
            }

            foreach (var x in count)
            {
                Assert.AreEqual(0, x);
            }
        }
コード例 #5
0
        public void CountAndClear()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            Assert.AreEqual(0, bag1.Count);
            bag1.Add("hello");
            Assert.AreEqual(1, bag1.Count);
            bag1.Add("foo");
            Assert.AreEqual(2, bag1.Count);
            bag1.Add("");
            Assert.AreEqual(3, bag1.Count);
            bag1.Add("HELLO");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add("foo");
            Assert.AreEqual(5, bag1.Count);
            bag1.Remove("");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add(null);
            Assert.AreEqual(5, bag1.Count);
            bag1.Add("Hello");
            Assert.AreEqual(6, bag1.Count);
            bag1.Add("Eric");
            Assert.AreEqual(7, bag1.Count);
            bag1.RemoveAllCopies("hElLo");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add(null);
            Assert.AreEqual(5, bag1.Count);
            bag1.Clear();
            Assert.AreEqual(0, bag1.Count);

            var found = false;

            foreach (var s in bag1)
            {
                found = true;
            }

            Assert.IsFalse(found);
        }