Пример #1
0
        public void UpcastSet_implements_ICollection_of_Base()
        {
            ICollection<Base> upcasted = new UpCastSet<Derive, Base>();
            Assert.AreEqual(false, upcasted.IsReadOnly);
            Assert.AreEqual(0, upcasted.Count);

            //Adds an element
            var d = new Derive(1);
            upcasted.Add(d);
            Assert.AreEqual(1, upcasted.Count);
            Assert.AreEqual(true, upcasted.Contains(d));

            //CopyTo copies all elements to an array of Base
            var a1 = new Base[1];
            upcasted.CopyTo(a1, 0);
            Assert.AreSame(d, a1[0]);

            //Removes the element
            upcasted.Remove(d);
            Assert.AreEqual(0, upcasted.Count);
            Assert.AreEqual(false, upcasted.Contains(d));

            //CopyTo copies nothing after all elements are removed
            var a2 = new Base[0];
            upcasted.CopyTo(a2, 0);
            Assert.AreEqual(0, a2.Count());

            var b = new Base(1);
            Assert.Throws<InvalidCastException>(() => upcasted.Add(b));
            Assert.Throws<InvalidCastException>(() => upcasted.Remove(b));
        }
Пример #2
0
 public void UpcastSet_throws_when_working_on_Base()
 {
     ICollection<Base> upcasted = new UpCastSet<Derive, Base>();
     var b = new Base(1);
     Assert.Throws<InvalidCastException>(() => upcasted.Add(b));
     Assert.Throws<InvalidCastException>(() => upcasted.Remove(b));
     Assert.Throws<InvalidCastException>(() => { var flag = upcasted.Contains(b); });
 }