Exemplo n.º 1
0
            private readonly static Expression[] s_noArgs = new Expression[0]; // used in reference comparison, requires unique object identity

            private static Expression[] GetConvertedArgs(params Expression[] args)
            {
                ReadOnlyCollectionBuilder <Expression> paramArgs = new ReadOnlyCollectionBuilder <Expression>(args.Length);

                for (int i = 0; i < args.Length; i++)
                {
                    paramArgs.Add(Expression.Convert(args[i], typeof(object)));
                }

                return(paramArgs.ToArray());
            }
Exemplo n.º 2
0
        public void ReadOnlyCollectionBuilder_ToArray(int length)
        {
            var rocb = new ReadOnlyCollectionBuilder <int>();

            for (int i = 0; i < length; i++)
            {
                rocb.Add(i);
            }

            int[] array = rocb.ToArray();

            Assert.True(Enumerable.Range(0, length).SequenceEqual(array));
        }
Exemplo n.º 3
0
        public void ReadOnlyCollectionBuilder_Ctor_Collection(IEnumerable <int> collection)
        {
            var rocb = new ReadOnlyCollectionBuilder <int>(collection);

            Assert.Equal(collection.Count(), rocb.Count);
            Assert.True(collection.SequenceEqual(rocb));

            int[] array = rocb.ToArray();

            Assert.Equal(collection.Count(), array.Length);
            Assert.True(collection.SequenceEqual(array));

            ReadOnlyCollection <int> roc = rocb.ToReadOnlyCollection();

            Assert.Equal(collection.Count(), roc.Count);
            Assert.True(collection.SequenceEqual(roc));

            AssertEmpty(rocb); // ToReadOnlyCollection behavior is to empty the builder
        }
Exemplo n.º 4
0
            private static Expression[] GetConvertedArgs(params Expression[] args) {
                ReadOnlyCollectionBuilder<Expression> paramArgs = new ReadOnlyCollectionBuilder<Expression>(args.Length);

                for (int i = 0; i < args.Length; i++) {
                    paramArgs.Add(Expression.Convert(args[i], typeof(object)));
                }

                return paramArgs.ToArray();
            }
Exemplo n.º 5
0
        public override MSAst.Expression Reduce()
        {
            if (_names == _star)
            {
                // from a[.b] import *
                return(GlobalParent.AddDebugInfo(
                           Ast.Call(
                               AstMethods.ImportStar,
                               Parent.LocalContext,
                               AstUtils.Constant(_root.MakeString()),
                               AstUtils.Constant(GetLevel())
                               ),
                           Span
                           ));
            }
            else
            {
                // from a[.b] import x [as xx], [ y [ as yy] ] [ , ... ]

                ReadOnlyCollectionBuilder <MSAst.Expression> statements = new ReadOnlyCollectionBuilder <MSAst.Expression>();
                MSAst.ParameterExpression module = Ast.Variable(typeof(object), "module");

                // Create initializer of the array of names being passed to ImportWithNames
                MSAst.Expression[] names = new MSAst.Expression[_names.Length];
                for (int i = 0; i < names.Length; i++)
                {
                    names[i] = AstUtils.Constant(_names[i]);
                }

                // module = PythonOps.ImportWithNames(<context>, _root, make_array(_names))
                statements.Add(
                    GlobalParent.AddDebugInfoAndVoid(
                        AssignValue(
                            module,
                            LightExceptions.CheckAndThrow(
                                Expression.Call(
                                    AstMethods.ImportWithNames,
                                    Parent.LocalContext,
                                    AstUtils.Constant(_root.MakeString()),
                                    Ast.NewArrayInit(typeof(string), names),
                                    AstUtils.Constant(GetLevel())
                                    )
                                )
                            ),
                        _root.Span
                        )
                    );

                // now load all the names being imported and assign the variables
                for (int i = 0; i < names.Length; i++)
                {
                    statements.Add(
                        GlobalParent.AddDebugInfoAndVoid(
                            AssignValue(
                                Parent.GetVariableExpression(_variables[i]),
                                Ast.Call(
                                    AstMethods.ImportFrom,
                                    Parent.LocalContext,
                                    module,
                                    names[i]
                                    )
                                ),
                            Span
                            )
                        );
                }

                statements.Add(AstUtils.Empty());
                return(GlobalParent.AddDebugInfo(Ast.Block(new[] { module }, statements.ToArray()), Span));
            }
        }
Exemplo n.º 6
0
        public override MSAst.Expression Reduce() {
            if (_names == _star) {
                // from a[.b] import *
                return GlobalParent.AddDebugInfo(
                    Ast.Call(
                        AstMethods.ImportStar,
                        Parent.LocalContext,
                        AstUtils.Constant(_root.MakeString()),
                        AstUtils.Constant(GetLevel())
                    ),
                    Span
                );
            } else {
                // from a[.b] import x [as xx], [ y [ as yy] ] [ , ... ]

                ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>();
                MSAst.ParameterExpression module = Ast.Variable(typeof(object), "module");

                // Create initializer of the array of names being passed to ImportWithNames
                MSAst.Expression[] names = new MSAst.Expression[_names.Length];
                for (int i = 0; i < names.Length; i++) {
                    names[i] = AstUtils.Constant(_names[i]);
                }

                // module = PythonOps.ImportWithNames(<context>, _root, make_array(_names))
                statements.Add(
                    GlobalParent.AddDebugInfoAndVoid(
                        AssignValue(
                            module,
                            LightExceptions.CheckAndThrow(
                                Expression.Call(
                                    AstMethods.ImportWithNames,
                                    Parent.LocalContext,
                                    AstUtils.Constant(_root.MakeString()),
                                    Ast.NewArrayInit(typeof(string), names),
                                    AstUtils.Constant(GetLevel())
                                )
                            )
                        ),
                        _root.Span
                    )
                );

                // now load all the names being imported and assign the variables
                for (int i = 0; i < names.Length; i++) {
                    statements.Add(
                        GlobalParent.AddDebugInfoAndVoid(
                            AssignValue(
                                Parent.GetVariableExpression(_variables[i]),
                                Ast.Call(
                                    AstMethods.ImportFrom,
                                    Parent.LocalContext,
                                    module,
                                    names[i]
                                )
                            ),
                            Span
                        )
                    );
                }

                statements.Add(AstUtils.Empty());
                return GlobalParent.AddDebugInfo(Ast.Block(new[] { module }, statements.ToArray()), Span);
            }
        }
Exemplo n.º 7
0
        internal override MSAst.Expression Transform(AstGenerator ag) {            
            if (_names == _star) {
                // from a[.b] import *
                return ag.AddDebugInfo(
                    Ast.Call(
                        AstGenerator.GetHelperMethod("ImportStar"),
                        ag.LocalContext, 
                        AstUtils.Constant(_root.MakeString()), 
                        AstUtils.Constant(GetLevel())
                    ),
                    Span
                );
            } else {
                // from a[.b] import x [as xx], [ y [ as yy] ] [ , ... ]

                ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>();
                MSAst.ParameterExpression module = ag.GetTemporary("module");

                // Create initializer of the array of names being passed to ImportWithNames
                MSAst.Expression[] names = new MSAst.Expression[_names.Length];
                for (int i = 0; i < names.Length; i++) {
                    names[i] = AstUtils.Constant(_names[i]);
                }

                // module = PythonOps.ImportWithNames(<context>, _root, make_array(_names))
                statements.Add(
                    ag.AddDebugInfoAndVoid(
                        GlobalAllocator.Assign(
                            module, 
                            Ast.Call(
                                AstGenerator.GetHelperMethod("ImportWithNames"),
                                ag.LocalContext,
                                AstUtils.Constant(_root.MakeString()),
                                Ast.NewArrayInit(typeof(string), names),
                                AstUtils.Constant(GetLevel())
                            )
                        ),
                        _root.Span
                    )
                );

                // now load all the names being imported and assign the variables
                for (int i = 0; i < names.Length; i++) {
                    statements.Add(
                        ag.AddDebugInfoAndVoid(
                            GlobalAllocator.Assign(
                                ag.Globals.GetVariable(ag, _variables[i]), 
                                Ast.Call(
                                    AstGenerator.GetHelperMethod("ImportFrom"),
                                    ag.LocalContext,
                                    module,
                                    names[i]
                                )
                            ),
                            Span
                        )
                    );
                }

                statements.Add(AstUtils.Empty());
                return ag.AddDebugInfo(Ast.Block(statements.ToArray()), Span);
            }
        }
Exemplo n.º 8
0
            private static Expression[] GetConvertedArgs(params Expression[] args)
            {
                var paramArgs = new ReadOnlyCollectionBuilder<Expression>(args.Length);

                foreach (var t in args)
                {
                    paramArgs.Add(Expression.Convert(t, typeof(object)));
                }

                return paramArgs.ToArray();
            }
Exemplo n.º 9
0
        internal Expression MakeClosureTuple()
        {
            if (_closureVariables == null)
                return Utils.Constant(null, typeof(MutableTuple));

            Debug.Assert(_closureVariables.Count > 0);
            Debug.Assert(_closureType != null);

            ReadOnlyCollectionBuilder<Expression> initializers = new ReadOnlyCollectionBuilder<Expression>();
            for (var i = 0; i < _closureVariables.Count; i++)
            {
                var closureExpression = Parent._variableMapping[_closureVariables[i]] as ClosureExpression;
                initializers.Add(closureExpression.ClosureCell);
            }

            var ret = MutableTuple.Create(
                initializers.ToArray()
            );
            Debug.Assert(ret.Type == _closureType);
            return ret;
        }
Exemplo n.º 10
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));
        }