1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // Federico Di Gregorio <fog@initd.org>
23 using System.Reflection;
24 using System.Collections.Generic;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_ArrayIndex
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.ArrayIndex (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null1 ()
45 Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression)null);
49 [ExpectedException (typeof (ArgumentNullException))]
50 public void Arg2Null2 ()
52 Expression.ArrayIndex (Expression.Constant (new int[1]), (IEnumerable<Expression>)null);
56 [ExpectedException (typeof (ArgumentNullException))]
57 public void Arg2Null3 ()
59 Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression[])null);
63 [ExpectedException (typeof (ArgumentException))]
64 public void Arg2WrongType1 ()
66 Expression.ArrayIndex (Expression.Constant (new int[1]), Expression.Constant (true));
70 [ExpectedException (typeof (ArgumentException))]
71 public void Arg1NotArray ()
73 Expression.ArrayIndex (Expression.Constant ("This is not an array!"), Expression.Constant (1));
77 [ExpectedException (typeof (ArgumentException))]
78 public void Arg2WrongType2 ()
80 Expression[] indexes = { Expression.Constant (1), Expression.Constant (1L) };
82 Expression.ArrayIndex (Expression.Constant (new int[1,1]), indexes);
86 [ExpectedException (typeof (ArgumentException))]
87 public void Arg2WrongNumber1 ()
89 Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
91 Expression.ArrayIndex (Expression.Constant (new int[1]), indexes);
95 public void Rank1Struct ()
99 BinaryExpression expr = Expression.ArrayIndex (Expression.Constant (array), Expression.Constant(0));
100 Assert.AreEqual (ExpressionType.ArrayIndex, expr.NodeType, "ArrayIndex#01");
101 Assert.AreEqual (typeof (int), expr.Type, "ArrayIndex#02");
102 Assert.IsNull (expr.Method, "ArrayIndex#03");
103 Assert.AreEqual ("value(System.Int32[])[0]", expr.ToString(), "ArrayIndex#04");
107 public void Rank1UserDefinedClass ()
109 NoOpClass[] array = { new NoOpClass() };
111 BinaryExpression expr = Expression.ArrayIndex (Expression.Constant (array), Expression.Constant(0));
112 Assert.AreEqual (ExpressionType.ArrayIndex, expr.NodeType, "ArrayIndex#05");
113 Assert.AreEqual (typeof (NoOpClass), expr.Type, "ArrayIndex#06");
114 Assert.IsNull (expr.Method, "ArrayIndex#07");
115 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.NoOpClass[])[0]", expr.ToString(), "ArrayIndex#08");
119 public void Rank2Struct ()
121 int[,] array = { {42}, {42} };
122 Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
124 MethodCallExpression expr = Expression.ArrayIndex (Expression.Constant (array), indexes);
125 Assert.AreEqual (ExpressionType.Call, expr.NodeType, "ArrayIndex#09");
126 Assert.AreEqual (typeof (int), expr.Type, "ArrayIndex#10");
127 Assert.AreEqual ("value(System.Int32[,]).Get(1, 0)", expr.ToString(), "ArrayIndex#12");
131 public void Rank2UserDefinedClass ()
133 NoOpClass[,] array = { {new NoOpClass()}, {new NoOpClass()} };
134 Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
136 MethodCallExpression expr = Expression.ArrayIndex (Expression.Constant (array), indexes);
137 Assert.AreEqual (ExpressionType.Call, expr.NodeType, "ArrayIndex#13");
138 Assert.AreEqual (typeof (NoOpClass), expr.Type, "ArrayIndex#14");
139 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.NoOpClass[,]).Get(1, 0)", expr.ToString(), "ArrayIndex#16");
142 static Func<T [], int, T> CreateArrayAccess<T> ()
144 var a = Expression.Parameter (typeof (T []), "a");
145 var i = Expression.Parameter (typeof (int), "i");
147 return Expression.Lambda<Func<T [], int, T>> (
148 Expression.ArrayIndex (a, i), a, i).Compile ();
152 public void CompileIntArrayAccess ()
154 var array = new int [] { 1, 2, 3, 4 };
155 var at = CreateArrayAccess<int> ();
157 Assert.AreEqual (1, at (array, 0));
158 Assert.AreEqual (4, at (array, 3));
162 public void CompileShortArrayAccess ()
164 var array = new short [] { 1, 2, 3, 4 };
165 var at = CreateArrayAccess<short> ();
167 Assert.AreEqual (array [0], at (array, 0));
168 Assert.AreEqual (array [3], at (array, 3));
171 enum Months { Jan, Feb, Mar, Apr };
174 public void CompileEnumArrayAccess ()
176 var array = new Months [] { Months.Jan, Months.Feb, Months.Mar, Months.Apr };
177 var at = CreateArrayAccess<Months> ();
179 Assert.AreEqual (array [0], at (array, 0));
180 Assert.AreEqual (array [3], at (array, 3));
187 public void CompileClassArrayAccess ()
189 var array = new Foo [] { new Foo (), new Foo (), new Foo (), new Foo () };
190 var at = CreateArrayAccess<Foo> ();
192 Assert.AreEqual (array [0], at (array, 0));
193 Assert.AreEqual (array [3], at (array, 3));
198 public Bar (int b) { bar = b; }
202 public void CompileStructArrayAccess ()
204 var array = new Bar [] { new Bar (0), new Bar (1), new Bar (2), new Bar (3) };
205 var at = CreateArrayAccess<Bar> ();
207 Assert.AreEqual (array [0], at (array, 0));
208 Assert.AreEqual (array [3], at (array, 3));