private static void TestBagOperation(IBag <Order> bag) { var total = 0; for (var i = 0; i < 1000; i++) { bag.Add(new Order { Id = i + 1 }, i + 1); total += i + 1; } var itemNumber = 0; foreach (var item in bag) { itemNumber++; } Assert.Equal(itemNumber, bag.Count); Assert.Equal(total, bag.Count); for (var i = 0; i < 1000; i++) { var count = bag[new Order { Id = i + 1 }]; Assert.Equal(count, i + 1); Assert.True(bag.Contains(new Order { Id = i + 1 })); } for (var i = 2000; i < 3000; i++) { Assert.False(bag.Contains(new Order { Id = i })); } Assert.Equal(1000, bag.ToUnique().Count); for (var i = 400; i < 600; i++) { Assert.True(bag.Remove(new Order { Id = i }, 100)); total -= 100; Assert.Equal(total, bag.Count); } Assert.Equal(1000, bag.ToUnique().Count); bag.Clear(); Assert.Equal(0, bag.Count); }
private static void TestBagItemNotExist(IBag <string> bag) { for (var i = 0; i < 100; i++) { for (var j = 0; j < 10; j++) { bag.Add(i.ToString()); } } Assert.True(bag.Contains("50")); Assert.False(bag.Contains("200")); Assert.Throws(typeof(ArgumentException), () => bag["150"]); Assert.False(bag.Remove("199")); Assert.True(bag.Remove("99")); }
public void A() { IBag <int> bag = BagMap.New <int>(); const int size = 1000; Assert.IsTrue(bag.Count is 0); for (int i = 0; i < size; i++) { Assert.IsTrue(bag[i] is 0); Assert.IsTrue(bag.Get(i) is 0); Assert.IsTrue(bag.TryGet(i) is (true, null, 0)); Assert.IsTrue(!bag.Contains(i)); } for (int i = 0; i < size; i++) { Assert.IsTrue(bag.TryAdd(i) is (true, null)); Assert.IsTrue(bag[i] is 1); Assert.IsTrue(bag.Get(i) is 1); Assert.IsTrue(bag.TryGet(i) is (true, null, 1)); Assert.IsTrue(bag.Count == i + 1); Assert.IsTrue(bag.Contains(i)); } Assert.IsTrue(bag.Count() is size); Assert.IsTrue(bag.GetCounts().Count() is size); Assert.IsTrue(bag.GetCounts().All(x => x.Count is 1)); for (int i = 0; i < size; i++) { Assert.IsTrue(bag.TryAdd(i) is (true, null)); Assert.IsTrue(bag[i] is 2); Assert.IsTrue(bag.Get(i) is 2); Assert.IsTrue(bag.TryGet(i) is (true, null, 2)); Assert.IsTrue(bag.Count == size + i + 1); Assert.IsTrue(bag.Contains(i)); } Assert.IsTrue(bag.Count() is 2 * size); Assert.IsTrue(bag.GetCounts().Count() is size); Assert.IsTrue(bag.GetCounts().All(x => x.Count is 2)); for (int i = 0; i < size; i++) { bag.Add(i); Assert.IsTrue(bag[i] is 3); Assert.IsTrue(bag.Get(i) is 3); Assert.IsTrue(bag.TryGet(i) is (true, null, 3)); Assert.IsTrue(bag.Count == size * 2 + i + 1); Assert.IsTrue(bag.Contains(i)); } Assert.IsTrue(bag.Count() is 3 * size); Assert.IsTrue(bag.GetCounts().Count() is size); Assert.IsTrue(bag.GetCounts().All(x => x.Count is 3)); for (int i = 0; i < size; i++) { Assert.IsTrue(bag.TryRemove(i) is (true, null, 3, 2)); Assert.IsTrue(bag[i] is 2); Assert.IsTrue(bag.Get(i) is 2); Assert.IsTrue(bag.TryGet(i) is (true, null, 2)); Assert.IsTrue(bag.Count == size * 3 - i - 1); Assert.IsTrue(bag.Contains(i)); } Assert.IsTrue(bag.Count() is 2 * size); Assert.IsTrue(bag.GetCounts().Count() is size); Assert.IsTrue(bag.GetCounts().All(x => x.Count is 2)); for (int i = 0; i < size; i++) { bag.Remove(i); Assert.IsTrue(bag[i] is 1); Assert.IsTrue(bag.Get(i) is 1); Assert.IsTrue(bag.TryGet(i) is (true, null, 1)); Assert.IsTrue(bag.Count == size * 2 - i - 1); Assert.IsTrue(bag.Contains(i)); } Assert.IsTrue(bag.Count() is size); Assert.IsTrue(bag.GetCounts().Count() is size); Assert.IsTrue(bag.GetCounts().All(x => x.Count is 1)); }