Esempio n. 1
0
        public void ReadOnlyCollectionBuilder_RemoveAt()
        {
            var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 3, 4 });

            Assert.True(new[] { 1, 2, 3, 4 }.SequenceEqual(rocb));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(-1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(4));

            rocb.RemoveAt(0);

            Assert.True(new[] { 2, 3, 4 }.SequenceEqual(rocb));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(3));

            rocb.RemoveAt(1);

            Assert.True(new[] { 2, 4 }.SequenceEqual(rocb));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(2));

            rocb.RemoveAt(1);

            Assert.True(new[] { 2 }.SequenceEqual(rocb));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(1));

            rocb.RemoveAt(0);

            Assert.Equal(0, rocb.Count);
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => rocb.RemoveAt(0));
        }
Esempio n. 2
0
        public void ReadOnlyCollectionBuilder_Capacity3()
        {
            var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1 });

            Assert.Equal(1, rocb.Capacity);
            Assert.Equal(1, rocb.Count);

            rocb.RemoveAt(0);

            Assert.Equal(1, rocb.Capacity);
            Assert.Equal(0, rocb.Count);

            rocb.Capacity = 0;

            Assert.Equal(0, rocb.Capacity);
            Assert.Equal(0, rocb.Count);
        }
Esempio n. 3
0
        private Expression VisitAssign(BinaryExpression node)
        {
            int        yields = _yields.Count;
            Expression left   = Visit(node.Left);
            Expression right  = Visit(node.Right);

            if (left == node.Left && right == node.Right)
            {
                return(node);
            }
            if (yields == _yields.Count)
            {
                return(Expression.Assign(left, right));
            }

            var block = new ReadOnlyCollectionBuilder <Expression>();

            if (_generator.RewriteAssignments)
            {
                // We need to make sure that LHS is evaluated before RHS. For example,
                //
                // {expr0}[{expr1},..,{exprN}] = {rhs}
                // ->
                // { l0 = {expr0}; l1 = {expr1}; ..; lN = {exprN}; r = {rhs}; l0[l1,..,lN] = r }
                //
                if (left == node.Left)
                {
                    switch (left.NodeType)
                    {
                    case ExpressionType.MemberAccess:
                        var member = (MemberExpression)node.Left;
                        if (member.Expression != null)
                        {
                            left = member.Update(ToTemp(block, member.Expression));
                        }
                        break;

                    case ExpressionType.Index:
                        var index = (IndexExpression)node.Left;
                        left = index.Update((index.Object != null ? ToTemp(block, index.Object) : null), ToTemp(block, index.Arguments));
                        break;

                    case ExpressionType.Parameter:
                        // no action needed
                        break;

                    default:
                        // Extension should've been reduced by Visit above,
                        // and returned a different node
                        throw Assert.Unreachable;
                    }
                }
                else
                {
                    // Get the last expression of the rewritten left side
                    var leftBlock = (BlockExpression)left;
                    block.AddRange(leftBlock.Expressions);
                    block.RemoveAt(block.Count - 1);
                    left = leftBlock.Expressions[leftBlock.Expressions.Count - 1];
                }
            }

            if (right != node.Right)
            {
                right = ToTemp(block, right);
            }

            block.Add(Expression.Assign(left, right));
            return(Expression.Block(block));
        }
Esempio n. 4
0
        public void TestReadOnlyCollectionBuilder()
        {
            int cnt = 0;

            // Empty
            ReadOnlyCollectionBuilder <int> a = new ReadOnlyCollectionBuilder <int>();

            AreEqual(0, a.Count);
            AreEqual(0, a.Capacity);
            AreEqual(a.ToReadOnlyCollection().Count, 0);
            AreEqual(a.ToReadOnlyCollection().Count, 0);

            // Simple case
            a.Add(5);
            AreEqual(1, a.Count);
            AreEqual(4, a.Capacity);
            AreEqual(a.ToReadOnlyCollection()[0], 5);
            AreEqual(a.ToReadOnlyCollection().Count, 0);  // Will reset

            a = new ReadOnlyCollectionBuilder <int>(0);
            AreEqual(0, a.Count);
            AssertError <ArgumentException>(() => a = new ReadOnlyCollectionBuilder <int>(-1));

            a = new ReadOnlyCollectionBuilder <int>(5);
            for (int i = 1; i <= 10; i++)
            {
                a.Add(i);
            }

            AreEqual(10, a.Capacity);
            System.Collections.ObjectModel.ReadOnlyCollection <int> readonlyCollection = a.ToReadOnlyCollection();
            AreEqual(0, a.Capacity);
            AreEqual(readonlyCollection.Count, 10);

            ReadOnlyCollectionBuilder <int> b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);

            b.Add(11);
            AreEqual(b.Count, 11);

            AssertError <ArgumentException>(() => a = new ReadOnlyCollectionBuilder <int>(null));

            // Capacity tests
            b.Capacity = 11;
            AssertError <ArgumentException>(() => b.Capacity = 10);
            b.Capacity = 50;
            AreEqual(b.Count, 11);
            AreEqual(b.Capacity, 50);

            // IndexOf cases
            AreEqual(b.IndexOf(5), 4);
            AreEqual(b[4], 5);
            a = new ReadOnlyCollectionBuilder <int>();
            AreEqual(a.IndexOf(5), -1);

            // Insert cases
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AssertError <ArgumentException>(() => b.Insert(11, 11));
            b.Insert(2, 24);
            AreEqual(b.Count, 11);
            AreEqual(b[1], 2);
            AreEqual(b[2], 24);
            AreEqual(b[3], 3);
            b.Insert(11, 1234);
            AssertError <ArgumentException>(() => b.Insert(-1, 55));
            AreEqual(b[11], 1234);
            AreEqual(b.ToReadOnlyCollection().Count, 12);

            // Remove
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AreEqual(b.Remove(2), true);
            AreEqual(b[0], 1);
            AreEqual(b[1], 3);
            AreEqual(b[2], 4);
            AreEqual(b.Remove(2), false);

            // RemoveAt
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            b.RemoveAt(2);
            AreEqual(b[1], 2);
            AreEqual(b[2], 4);
            AreEqual(b[3], 5);
            AssertError <ArgumentException>(() => b.RemoveAt(-5));
            AssertError <ArgumentException>(() => b.RemoveAt(9));

            // Clear
            b.Clear();
            AreEqual(b.Count, 0);
            AreEqual(b.ToReadOnlyCollection().Count, 0);
            b = new ReadOnlyCollectionBuilder <int>();
            b.Clear();
            AreEqual(b.Count, 0);

            // Contains
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AreEqual(b.Contains(5), true);
            AreEqual(b.Contains(-3), false);

            ReadOnlyCollectionBuilder <object> c = new ReadOnlyCollectionBuilder <object>();

            c.Add("HI");
            AreEqual(c.Contains("HI"), true);
            AreEqual(c.Contains(null), false);
            c.Add(null);
            AreEqual(c.Contains(null), true);

            // CopyTo
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            int[] ary = new int[10];
            b.CopyTo(ary, 0);

            AreEqual(ary[0], 1);
            AreEqual(ary[9], 10);

            // Reverse
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            b.Reverse();
            // 1..10
            cnt = 10;
            for (int i = 0; i < 10; i++)
            {
                AreEqual(b[i], cnt--);
            }

            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AssertError <ArgumentException>(() => b.Reverse(-1, 5));
            AssertError <ArgumentException>(() => b.Reverse(5, -1));
            b.Reverse(3, 3);
            // 1,2,3,4,5,6,7,8,9.10
            // 1,2,3,6,5,4,7,8,9,10
            AreEqual(b[1], 2);
            AreEqual(b[2], 3);
            AreEqual(b[3], 6);
            AreEqual(b[4], 5);
            AreEqual(b[5], 4);
            AreEqual(b[6], 7);

            // ToArray
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            int[] intAry = b.ToArray();
            AreEqual(intAry[0], 1);
            AreEqual(intAry[9], 10);

            b      = new ReadOnlyCollectionBuilder <int>();
            intAry = b.ToArray();
            AreEqual(intAry.Length, 0);

            // IEnumerable cases
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            cnt = 0;
            foreach (int i in b)
            {
                cnt++;
            }
            AreEqual(cnt, 10);

            b   = new ReadOnlyCollectionBuilder <int>();
            cnt = 0;
            foreach (int i in b)
            {
                cnt++;
            }
            AreEqual(cnt, 0);

            // Error case
            AssertError <InvalidOperationException>(() => ChangeWhileEnumeratingAdd());
            AssertError <InvalidOperationException>(() => ChangeWhileEnumeratingRemove());

            // IList members
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            System.Collections.IList lst = b;

            // IsReadOnly
            AreEqual(lst.IsReadOnly, false);

            // Add
            AreEqual(lst.Add(11), 10);
            AreEqual(lst.Count, 11);
            AssertError <ArgumentException>(() => lst.Add("MOM"));
            AssertError <ArgumentException>(() => lst.Add(null));

            c = new ReadOnlyCollectionBuilder <object>();

            c.Add("HI");
            c.Add(null);
            lst = c;
            lst.Add(null);
            AreEqual(lst.Count, 3);

            // Contains
            lst = b;
            AreEqual(lst.Contains(5), true);
            AreEqual(lst.Contains(null), false);

            lst = c;
            AreEqual(lst.Contains("HI"), true);
            AreEqual(lst.Contains("hi"), false);
            AreEqual(lst.Contains(null), true);

            // IndexOf
            lst = b;
            AreEqual(lst.IndexOf(null), -1);
            AreEqual(lst.IndexOf(1234), -1);
            AreEqual(lst.IndexOf(5), 4);

            // Insert
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            lst = b;
            AssertError <ArgumentException>(() => lst.Insert(11, 11));
            lst.Insert(2, 24);
            AreEqual(lst.Count, 11);
            AreEqual(lst[1], 2);
            AreEqual(lst[2], 24);
            AreEqual(lst[3], 3);
            lst.Insert(11, 1234);
            AssertError <ArgumentException>(() => lst.Insert(-1, 55));
            AreEqual(lst[11], 1234);

            AssertError <ArgumentException>(() => lst.Insert(3, "MOM"));

            // IsFixedSize
            AreEqual(lst.IsFixedSize, false);

            // Remove
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            lst = b;
            lst.Remove(2);
            AreEqual(lst[0], 1);
            AreEqual(lst[1], 3);
            AreEqual(lst[2], 4);
            lst.Remove(2);

            // Indexing
            lst[3] = 234;
            AreEqual(lst[3], 234);
            AssertError <ArgumentException>(() => lst[3] = null);
            AssertError <ArgumentException>(() => lst[3] = "HI");

            // ICollection<T>

            // IsReadOnly
            System.Collections.Generic.ICollection <int> col = b;
            AreEqual(col.IsReadOnly, false);

            // ICollection
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            System.Collections.ICollection col2 = b;
            AreEqual(col2.IsSynchronized, false);
            Assert(col2.SyncRoot != null);
            intAry = new int[10];
            col2.CopyTo(intAry, 0);
            AreEqual(intAry[0], 1);
            AreEqual(intAry[9], 10);

            string[] str = new string[50];
            AssertError <ArrayTypeMismatchException>(() => col2.CopyTo(str, 0));
        }