public void ArrayAccess_Assign_SystemIndex() { var xs = Expression.Parameter(typeof(string[])); var i = Expression.Parameter(typeof(Index)); var s = Expression.Parameter(typeof(string)); var res = Expression.Lambda <Func <string[], Index, string, string> >(CSharpExpression.Assign(CSharpExpression.ArrayAccess(xs, i), s), xs, i, s); var f = res.Compile(); var vals = new string[] { "bar", "foo", "qux" }; for (int j = 0; j < vals.Length; j++) { var newVal = vals[j].ToUpper(); var assignRes = f(vals, new Index(j), newVal); Assert.AreEqual(newVal, assignRes); Assert.AreEqual(newVal, vals[j]); } for (int j = 1; j <= vals.Length; j++) { var k = new Index(j, fromEnd: true); var newVal = vals[k].ToLower(); var assignRes = f(vals, k, newVal); Assert.AreEqual(newVal, assignRes); Assert.AreEqual(newVal, vals[k]); } }
public void ArrayAccess_Assign_SideEffects() { AssertCompile((log, append) => { var array = Expression.Block(log("A"), Expression.Constant(new[] { 42 })); var index = Expression.Block(log("I"), Expression.Constant(0)); var val = Expression.Block(log("V"), Expression.Constant(43)); return(CSharpExpression.Assign(CSharpExpression.ArrayAccess(array, index), val)); }, new LogAndResult <object> { Log = { "A", "I", "V" } }); AssertCompile((log, append) => { var array = Expression.Block(log("A"), Expression.Constant(new[] { 42 })); var index = Expression.Block(log("I"), Expression.Constant(new Index(0))); var val = Expression.Block(log("V"), Expression.Constant(43)); return(CSharpExpression.Assign(CSharpExpression.ArrayAccess(array, index), val)); }, new LogAndResult <object> { Log = { "A", "I", "V" } }); AssertCompile((log, append) => { var array = Expression.Block(log("A"), Expression.Constant(new[, ] { { 42 } })); var index1 = Expression.Block(log("I1"), Expression.Constant(0)); var index2 = Expression.Block(log("I2"), Expression.Constant(0)); var val = Expression.Block(log("V"), Expression.Constant(43)); return(CSharpExpression.Assign(CSharpExpression.ArrayAccess(array, index1, index2), val)); }, new LogAndResult <object> { Log = { "A", "I1", "I2", "V" } }); }
public void ArrayAccess_Assign_Int32() { var xs = Expression.Parameter(typeof(string[])); var i = Expression.Parameter(typeof(int)); var s = Expression.Parameter(typeof(string)); var res = Expression.Lambda <Func <string[], int, string, string> >(CSharpExpression.Assign(CSharpExpression.ArrayAccess(xs, i), s), xs, i, s); var f = res.Compile(); var vals = new string[] { "bar", "foo", "qux" }; for (int j = 0; j < vals.Length; j++) { var newVal = vals[j].ToUpper(); var assignRes = f(vals, j, newVal); Assert.AreEqual(newVal, assignRes); Assert.AreEqual(newVal, vals[j]); } }