示例#1
0
        static void CommonFixedSizeOrReadOnlyChainListTests(IList <string> left, IList <string> right, IList <string> chained, Type expectedChainedListType)
        {
            Assert.AreEqual(expectedChainedListType, chained.GetType());
            var isReadOnly = expectedChainedListType.Name.Contains("ReadOnly");

            Assert.AreEqual(ChainedListExtensions.IsReadOnly(left) || ChainedListExtensions.IsReadOnly(right), isReadOnly);
            Assert.AreEqual(chained.IsReadOnly, isReadOnly);
            Assert.IsTrue(ChainedListExtensions.IsFixedSize(left) || ChainedListExtensions.IsFixedSize(right));
            Assert.IsTrue(ChainedListExtensions.IsFixedSize(chained));
            AssertChainedListSanity(left, right, chained);
            CollectionAssert.AreEqual(new[] { "a", "b", "c", "c", "d" }, chained);
            Assert.AreEqual(chained.GetType().ToString(), chained.ToString());
            AssertThrowsOutOfRangeException(() => _ = chained[-1]);
            AssertThrowsOutOfRangeException(() => _ = chained[chained.Count]);
            Assert.Throws(typeof(NotSupportedException), () => chained.Add("e"));
            Assert.AreEqual(2, chained.IndexOf("c"));
            Assert.AreEqual(4, chained.IndexOf("d"));
            Assert.AreEqual(-1, chained.IndexOf("nothere"));
            Assert.Throws(typeof(NotSupportedException), () => chained.Remove("c"));
            Assert.Throws(typeof(NotSupportedException), () => chained.Insert(3, "d2"));
            Assert.Throws(typeof(NotSupportedException), () => chained.Insert(chained.Count, "f"));
            Assert.Throws(typeof(NotSupportedException), () => chained.RemoveAt(2));
            Assert.Throws(typeof(NotSupportedException), () => chained.RemoveAt(chained.Count));
            var array1 = new string[chained.Count];

            chained.CopyTo(array1, 0);
            CollectionAssert.AreEqual(new[] { "a", "b", "c", "c", "d" }, array1);
            var array2 = new string[chained.Count + 2];

            chained.CopyTo(array2, 2);
            CollectionAssert.AreEqual(new[] { null, null, "a", "b", "c", "c", "d" }, array2);
            AssertThrowsOutOfRangeException(() => _ = chained[-1]);
            AssertThrowsOutOfRangeException(() => _ = chained[chained.Count]);
            if (isReadOnly)
            {
                Assert.Throws(typeof(NotSupportedException), () => chained[1]             = "b2");
                Assert.Throws(typeof(NotSupportedException), () => chained[2]             = "d3");
                Assert.Throws(typeof(NotSupportedException), () => chained[-1]            = "error");
                Assert.Throws(typeof(NotSupportedException), () => chained[chained.Count] = "error");
                AssertChainedListSanity(left, right, chained);
                CollectionAssert.AreEqual(new[] { "a", "b", "c" }, left);
                CollectionAssert.AreEqual(new[] { "c", "d" }, right);
                CollectionAssert.AreEqual(new[] { "a", "b", "c", "c", "d" }, chained);
                if (chained is IBaseRefList <string> )
                {
                    Assert.IsInstanceOf(typeof(IRefReadOnlyList <string>), chained);
                    var chainedRefList = (IRefReadOnlyList <string>)chained;
                    Assert.AreEqual(chainedRefList.ItemRef(1), "b");
                    Assert.AreEqual(chainedRefList.ItemRef(4), "d");
                    // Following line results in a compile-time error due to readonly ItemRef, as expected:
                    //chainedRefList.ItemRef(1) = "b2";
                    Assert.AreSame(chainedRefList, chainedRefList.AsReadOnly());
                    Assert.Throws(typeof(NotSupportedException), () => chainedRefList.AsNonReadOnly());
                    AssertChainedListSanity(left, right, chained);
                    CollectionAssert.AreEqual(new[] { "a", "b", "c" }, left);
                    CollectionAssert.AreEqual(new[] { "c", "d" }, right);
                    CollectionAssert.AreEqual(new[] { "a", "b", "c", "c", "d" }, chained);
                }
            }
            else
            {
                chained[2] = "c2";
                chained[3] = "c3";
                AssertThrowsOutOfRangeException(() => chained[-1]            = "error");
                AssertThrowsOutOfRangeException(() => chained[chained.Count] = "error");
                AssertChainedListSanity(left, right, chained);
                CollectionAssert.AreEqual(new[] { "a", "b", "c2" }, left);
                CollectionAssert.AreEqual(new[] { "c3", "d" }, right);
                CollectionAssert.AreEqual(new[] { "a", "b", "c2", "c3", "d" }, chained);
                if (chained is IBaseRefList <string> )
                {
                    Assert.IsInstanceOf(typeof(IRefList <string>), chained);
                    var chainedRefList = (IRefList <string>)chained;
                    Assert.AreEqual(chainedRefList.ItemRef(1), "b");
                    Assert.AreEqual(chainedRefList.ItemRef(4), "d");
                    chainedRefList.ItemRef(1) = "b2";
                    chainedRefList.ItemRef(4) = "d2";
                    Assert.IsInstanceOf(typeof(IRefReadOnlyList <string>), chainedRefList.AsReadOnly());
                    Assert.AreSame(chainedRefList, chainedRefList.AsNonReadOnly());
                    AssertChainedListSanity(left, right, chained);
                    CollectionAssert.AreEqual(new[] { "a", "b2", "c2" }, left);
                    CollectionAssert.AreEqual(new[] { "c3", "d2" }, right);
                    CollectionAssert.AreEqual(new[] { "a", "b2", "c2", "c3", "d2" }, chained);
                }
            }
            Assert.Throws(typeof(NotSupportedException), () => chained.Clear());
        }
示例#2
0
        public void ChainConcat_Lists()
        {
            var left = new List <string> {
                "a", "b", "c"
            };
            var right = new List <string> {
                "c", "d"
            };
            var chained = left.ChainConcat(right);

            Assert.AreEqual(typeof(ChainedList <string>), chained.GetType());
            Assert.IsFalse(chained.IsReadOnly);
            Assert.IsFalse(ChainedListExtensions.IsFixedSize(chained));
            AssertChainedListSanity(left, right, chained);
            CollectionAssert.AreEqual(new[] { "a", "b", "c", "c", "d" }, chained);
            Assert.AreEqual(chained.GetType().ToString(), chained.ToString());
            chained.Add("e");
            AssertChainedListSanity(left, right, chained);
            Assert.AreEqual(2, chained.IndexOf("c"));
            Assert.AreEqual(4, chained.IndexOf("d"));
            Assert.AreEqual(-1, chained.IndexOf("nothere"));
            Assert.IsTrue(chained.Remove("c"));
            AssertChainedListSanity(left, right, chained);
            Assert.IsTrue(chained.Remove("c"));
            AssertChainedListSanity(left, right, chained);
            Assert.IsFalse(chained.Remove("nothere"));
            AssertChainedListSanity(left, right, chained);
            chained.Insert(3, "d2");
            AssertChainedListSanity(left, right, chained);
            chained.Insert(2, "c2");
            AssertChainedListSanity(left, right, chained);
            chained.Insert(chained.Count, "f");
            AssertChainedListSanity(left, right, chained);
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => chained.Insert(chained.Count + 1, "error"));
            CollectionAssert.AreEqual(new[] { "a", "b", "c2" }, left);
            CollectionAssert.AreEqual(new[] { "d", "d2", "e", "f" }, right);
            chained.RemoveAt(2);
            AssertChainedListSanity(left, right, chained);
            chained.RemoveAt(2);
            AssertChainedListSanity(left, right, chained);
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => chained.RemoveAt(chained.Count));
            CollectionAssert.AreEqual(new[] { "a", "b" }, left);
            CollectionAssert.AreEqual(new[] { "d2", "e", "f" }, right);
            var array1 = new string[chained.Count];

            chained.CopyTo(array1, 0);
            CollectionAssert.AreEqual(new[] { "a", "b", "d2", "e", "f" }, array1);
            var array2 = new string[chained.Count + 2];

            chained.CopyTo(array2, 2);
            CollectionAssert.AreEqual(new[] { null, null, "a", "b", "d2", "e", "f" }, array2);
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => _ = chained[-1]);
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => _ = chained[chained.Count]);
            chained[1] = "b2";
            chained[2] = "d3";
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => chained[-1]            = "error");
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => chained[chained.Count] = "error");
            AssertChainedListSanity(left, right, chained);
            CollectionAssert.AreEqual(new[] { "a", "b2" }, left);
            CollectionAssert.AreEqual(new[] { "d3", "e", "f" }, right);
            chained.Clear();
            AssertChainedListSanity(left, right, chained);
            CollectionAssert.AreEqual(new string[0], left);
            CollectionAssert.AreEqual(new string[0], right);
            CollectionAssert.AreEqual(new string[0], chained);
        }