Exemple #1
0
        private static void WithHomemadeGeneric()
        {
            Bag <Apple> bagOfApples = new Bag <Apple>();

            bagOfApples.Add(new Apple {
                Name = "Granny Smith"
            });
            bagOfApples.Add(new Apple {
                Name = "Cox's Orange Pippin"
            });
            bagOfApples.Add(new Apple {
                Name = "Golden Delicious"
            });

            IBag <Fruit> bagOfFruit = bagOfApples;

            Console.WriteLine(bagOfFruit.Get(0).Name);
            Console.WriteLine(bagOfFruit.Get(1).Name);

            //bagOfApples.Add(new Banana { Name = "Blue Java" });
            //bagOfFruit.Add(new Banana { Name = "Blue Java" });
        }
Exemple #2
0
        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));
        }