Example #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]);
        }
Example #2
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 });
        }