2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_ArrayIndex.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21
22 using System;
23 using System.Reflection;
24 using System.Collections.Generic;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_ArrayIndex
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.ArrayIndex (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null1 ()
44                 {
45                         Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression)null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentNullException))]
50                 public void Arg2Null2 ()
51                 {
52                         Expression.ArrayIndex (Expression.Constant (new int[1]), (IEnumerable<Expression>)null);
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (ArgumentNullException))]
57                 public void Arg2Null3 ()
58                 {
59                         Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression[])null);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (ArgumentException))]
64                 public void Arg2WrongType1 ()
65                 {
66                         Expression.ArrayIndex (Expression.Constant (new int[1]), Expression.Constant (true));
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentException))]
71                 public void Arg1NotArray ()
72                 {
73                         Expression.ArrayIndex (Expression.Constant ("This is not an array!"), Expression.Constant (1));
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentException))]
78                 public void Arg2WrongType2 ()
79                 {
80                         Expression[] indexes = { Expression.Constant (1), Expression.Constant (1L) };
81
82                         Expression.ArrayIndex (Expression.Constant (new int[1,1]), indexes);
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (ArgumentException))]
87                 public void Arg2WrongNumber1 ()
88                 {
89                         Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
90
91                         Expression.ArrayIndex (Expression.Constant (new int[1]), indexes);
92                 }
93
94                 [Test]
95                 public void Rank1Struct ()
96                 {
97                         int[] array = { 42 };
98
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");
104                 }
105
106                 [Test]
107                 public void Rank1UserDefinedClass ()
108                 {
109                         NoOpClass[] array = { new NoOpClass() };
110
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");
116                 }
117
118                 [Test]
119                 public void Rank2Struct ()
120                 {
121                         int[,] array = { {42}, {42} };
122                         Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
123
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");
128                 }
129
130                 [Test]
131                 public void Rank2UserDefinedClass ()
132                 {
133                         NoOpClass[,] array = { {new NoOpClass()}, {new NoOpClass()} };
134                         Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
135
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");
140                 }
141
142                 static Func<T [], int, T> CreateArrayAccess<T> ()
143                 {
144                         var a = Expression.Parameter (typeof (T []), "a");
145                         var i = Expression.Parameter (typeof (int), "i");
146
147                         return Expression.Lambda<Func<T [], int, T>> (
148                                 Expression.ArrayIndex (a, i), a, i).Compile ();
149                 }
150
151                 [Test]
152                 public void CompileIntArrayAccess ()
153                 {
154                         var array = new int [] { 1, 2, 3, 4 };
155                         var at = CreateArrayAccess<int> ();
156
157                         Assert.AreEqual (1, at (array, 0));
158                         Assert.AreEqual (4, at (array, 3));
159                 }
160
161                 [Test]
162                 public void CompileShortArrayAccess ()
163                 {
164                         var array = new short [] { 1, 2, 3, 4 };
165                         var at = CreateArrayAccess<short> ();
166
167                         Assert.AreEqual (array [0], at (array, 0));
168                         Assert.AreEqual (array [3], at (array, 3));
169                 }
170
171                 enum Months { Jan, Feb, Mar, Apr };
172
173                 [Test]
174                 public void CompileEnumArrayAccess ()
175                 {
176                         var array = new Months [] { Months.Jan, Months.Feb, Months.Mar, Months.Apr };
177                         var at = CreateArrayAccess<Months> ();
178
179                         Assert.AreEqual (array [0], at (array, 0));
180                         Assert.AreEqual (array [3], at (array, 3));
181                 }
182
183                 class Foo {
184                 }
185
186                 [Test]
187                 public void CompileClassArrayAccess ()
188                 {
189                         var array = new Foo [] { new Foo (), new Foo (), new Foo (), new Foo () };
190                         var at = CreateArrayAccess<Foo> ();
191
192                         Assert.AreEqual (array [0], at (array, 0));
193                         Assert.AreEqual (array [3], at (array, 3));
194                 }
195
196                 struct Bar {
197                         public int bar;
198                         public Bar (int b) { bar = b; }
199                 }
200
201                 [Test]
202                 public void CompileStructArrayAccess ()
203                 {
204                         var array = new Bar [] { new Bar (0), new Bar (1), new Bar (2), new Bar (3) };
205                         var at = CreateArrayAccess<Bar> ();
206
207                         Assert.AreEqual (array [0], at (array, 0));
208                         Assert.AreEqual (array [3], at (array, 3));
209                 }
210         }
211 }