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)); }
public void UpcastSet_CopyTo_checks_arguments() { ICollection<Base> upcasted = new UpCastSet<Derive, Base>(Derives(1)); // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws<ArgumentNullException>(() => upcasted.CopyTo(null, 0)).ForArg("array"); Assert.Throws<ArgumentOutOfRangeException>(() => upcasted.CopyTo(new Base[3], -1)).ForArg("arrayIndex"); Assert.Throws<ArgumentException>(() => upcasted.CopyTo(new Base[1], 1)).ForArg("arrayIndex"); }