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]); }
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)); }
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 }); }
public void Add(int item) { m_bag.Add(item); }