예제 #1
0
        public void NoDuplicates()
        {
            var bag = new SimpleBag <int>();

            VerifyBag(bag, new int[0]);
            bag.Add(5);
            Assert.AreEqual(1, bag.Occurrences(5));
            Assert.AreEqual(0, bag.Occurrences(6));
            VerifyBag(bag, new[] { 5 });
            bag.Add(6);
            Assert.AreEqual(1, bag.Occurrences(5));
            Assert.AreEqual(1, bag.Occurrences(6));
            Assert.AreEqual(0, bag.Occurrences(2));
            VerifyBag(bag, new[] { 5, 6 });
            bag.Add(2);
            Assert.AreEqual(1, bag.Occurrences(5));
            Assert.AreEqual(1, bag.Occurrences(6));
            Assert.AreEqual(1, bag.Occurrences(2));
            Assert.AreEqual(0, bag.Occurrences(10));
            VerifyBag(bag, new[] { 2, 5, 6 });

            Assert.IsTrue(bag.Remove(2));
            VerifyBag(bag, new[] { 5, 6 });
            Assert.IsTrue(bag.Remove(6));
            VerifyBag(bag, new[] { 5 });
            Assert.IsTrue(bag.Remove(5));
            VerifyBag(bag, new int[0]);
        }
예제 #2
0
        void VerifyBag(SimpleBag <int> bag, int[] expected)
        {
            Assert.AreEqual(expected.Length, bag.Count);
            var values = new List <int>(bag);

            values.Sort();
            Assert.AreEqual(expected.Length, values.Count);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], values[i]);
            }
            values = new List <int>(bag.Items);
            values.Sort();
            int iex = 0;
            int iv  = 0;

            while (iex < expected.Length)
            {
                Assert.AreEqual(expected[iex], values[iv]);
                // Skip duplicates in expected.
                while (iex < expected.Length - 1 && expected[iex] == expected[iex + 1])
                {
                    iex++;
                }
                iex++;
                iv++;
            }
        }
예제 #3
0
        public void RemoveNotFound()
        {
            var bag = new SimpleBag <int>();

            //Check when the bag is empty.
            Assert.IsFalse(bag.Remove(3));

            //Check when just one thing is in the bag.
            bag.Add(2);
            Assert.IsFalse(bag.Remove(3));

            //Check the case where there are multiple different items in the bag.
            bag.Add(4);
            Assert.IsFalse(bag.Remove(3));

            //The case where there are duplicate items.
            bag.Add(2);
            Assert.IsFalse(bag.Remove(3));
        }
예제 #4
0
        public void Duplicates()
        {
            var bag = new SimpleBag <int>();

            VerifyBag(bag, new int[0]);
            bag.Add(5);
            bag.Add(5);
            bag.Add(5);
            Assert.AreEqual(3, bag.Occurrences(5));
            Assert.AreEqual(0, bag.Occurrences(10));
            VerifyBag(bag, new[] { 5, 5, 5 });
            Assert.IsTrue(bag.Remove(5));
            VerifyBag(bag, new[] { 5, 5 });
            Assert.IsTrue(bag.Remove(5));
            VerifyBag(bag, new[] { 5 });
            Assert.IsTrue(bag.Remove(5));
            VerifyBag(bag, new int[0]);

            bag = new SimpleBag <int>();
            bag.Add(5);
            bag.Add(5);
            bag.Add(5);
            bag.Add(6);
            bag.Add(6);
            Assert.AreEqual(3, bag.Occurrences(5));
            Assert.AreEqual(2, bag.Occurrences(6));
            Assert.AreEqual(0, bag.Occurrences(10));
            VerifyBag(bag, new[] { 5, 5, 5, 6, 6 });

            // Subtly different: this takes it to a set before a dictionary.
            bag = new SimpleBag <int>();
            bag.Add(5);
            bag.Add(6);
            bag.Add(5);
            bag.Add(5);
            bag.Add(6);
            Assert.AreEqual(3, bag.Occurrences(5));
            Assert.AreEqual(2, bag.Occurrences(6));
            Assert.AreEqual(0, bag.Occurrences(10));
            VerifyBag(bag, new[] { 5, 5, 5, 6, 6 });
        }