ExpressionTest_ArrayIndex.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <[email protected]>
  21. using System;
  22. using System.Reflection;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Linq.Expressions;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Linq.Expressions
  28. {
  29. [TestFixture]
  30. [Category("SRE")]
  31. public class ExpressionTest_ArrayIndex
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.ArrayIndex (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null1 ()
  42. {
  43. Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression)null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (ArgumentNullException))]
  47. public void Arg2Null2 ()
  48. {
  49. Expression.ArrayIndex (Expression.Constant (new int[1]), (IEnumerable<Expression>)null);
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentNullException))]
  53. public void Arg2Null3 ()
  54. {
  55. Expression.ArrayIndex (Expression.Constant (new int[1]), (Expression[])null);
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentException))]
  59. public void Arg2WrongType1 ()
  60. {
  61. Expression.ArrayIndex (Expression.Constant (new int[1]), Expression.Constant (true));
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentException))]
  65. public void Arg1NotArray ()
  66. {
  67. Expression.ArrayIndex (Expression.Constant ("This is not an array!"), Expression.Constant (1));
  68. }
  69. [Test]
  70. [ExpectedException (typeof (ArgumentException))]
  71. public void Arg2WrongType2 ()
  72. {
  73. Expression[] indexes = { Expression.Constant (1), Expression.Constant (1L) };
  74. Expression.ArrayIndex (Expression.Constant (new int[1,1]), indexes);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentException))]
  78. public void Arg2WrongNumber1 ()
  79. {
  80. Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
  81. Expression.ArrayIndex (Expression.Constant (new int[1]), indexes);
  82. }
  83. [Test]
  84. public void Rank1Struct ()
  85. {
  86. int[] array = { 42 };
  87. BinaryExpression expr = Expression.ArrayIndex (Expression.Constant (array), Expression.Constant(0));
  88. Assert.AreEqual (ExpressionType.ArrayIndex, expr.NodeType, "ArrayIndex#01");
  89. Assert.AreEqual (typeof (int), expr.Type, "ArrayIndex#02");
  90. Assert.IsNull (expr.Method, "ArrayIndex#03");
  91. Assert.AreEqual ("value(System.Int32[])[0]", expr.ToString(), "ArrayIndex#04");
  92. }
  93. [Test]
  94. public void Rank1UserDefinedClass ()
  95. {
  96. NoOpClass[] array = { new NoOpClass() };
  97. BinaryExpression expr = Expression.ArrayIndex (Expression.Constant (array), Expression.Constant(0));
  98. Assert.AreEqual (ExpressionType.ArrayIndex, expr.NodeType, "ArrayIndex#05");
  99. Assert.AreEqual (typeof (NoOpClass), expr.Type, "ArrayIndex#06");
  100. Assert.IsNull (expr.Method, "ArrayIndex#07");
  101. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.NoOpClass[])[0]", expr.ToString(), "ArrayIndex#08");
  102. }
  103. [Test]
  104. public void Rank2Struct ()
  105. {
  106. int[,] array = { {42}, {42} };
  107. Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
  108. MethodCallExpression expr = Expression.ArrayIndex (Expression.Constant (array), indexes);
  109. Assert.AreEqual (ExpressionType.Call, expr.NodeType, "ArrayIndex#09");
  110. Assert.AreEqual (typeof (int), expr.Type, "ArrayIndex#10");
  111. Assert.AreEqual ("value(System.Int32[,]).Get(1, 0)", expr.ToString(), "ArrayIndex#12");
  112. }
  113. [Test]
  114. public void Rank2UserDefinedClass ()
  115. {
  116. NoOpClass[,] array = { {new NoOpClass()}, {new NoOpClass()} };
  117. Expression[] indexes = { Expression.Constant (1), Expression.Constant (0) };
  118. MethodCallExpression expr = Expression.ArrayIndex (Expression.Constant (array), indexes);
  119. Assert.AreEqual (ExpressionType.Call, expr.NodeType, "ArrayIndex#13");
  120. Assert.AreEqual (typeof (NoOpClass), expr.Type, "ArrayIndex#14");
  121. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.NoOpClass[,]).Get(1, 0)", expr.ToString(), "ArrayIndex#16");
  122. }
  123. static Func<T [], int, T> CreateArrayAccess<T> ()
  124. {
  125. var a = Expression.Parameter (typeof (T []), "a");
  126. var i = Expression.Parameter (typeof (int), "i");
  127. return Expression.Lambda<Func<T [], int, T>> (
  128. Expression.ArrayIndex (a, i), a, i).Compile ();
  129. }
  130. [Test]
  131. public void CompileIntArrayAccess ()
  132. {
  133. var array = new int [] { 1, 2, 3, 4 };
  134. var at = CreateArrayAccess<int> ();
  135. Assert.AreEqual (1, at (array, 0));
  136. Assert.AreEqual (4, at (array, 3));
  137. }
  138. [Test]
  139. public void CompileShortArrayAccess ()
  140. {
  141. var array = new short [] { 1, 2, 3, 4 };
  142. var at = CreateArrayAccess<short> ();
  143. Assert.AreEqual (array [0], at (array, 0));
  144. Assert.AreEqual (array [3], at (array, 3));
  145. }
  146. enum Months { Jan, Feb, Mar, Apr };
  147. [Test]
  148. public void CompileEnumArrayAccess ()
  149. {
  150. var array = new Months [] { Months.Jan, Months.Feb, Months.Mar, Months.Apr };
  151. var at = CreateArrayAccess<Months> ();
  152. Assert.AreEqual (array [0], at (array, 0));
  153. Assert.AreEqual (array [3], at (array, 3));
  154. }
  155. class Foo {
  156. }
  157. [Test]
  158. public void CompileClassArrayAccess ()
  159. {
  160. var array = new Foo [] { new Foo (), new Foo (), new Foo (), new Foo () };
  161. var at = CreateArrayAccess<Foo> ();
  162. Assert.AreEqual (array [0], at (array, 0));
  163. Assert.AreEqual (array [3], at (array, 3));
  164. }
  165. struct Bar {
  166. public int bar;
  167. public Bar (int b) { bar = b; }
  168. }
  169. [Test]
  170. public void CompileStructArrayAccess ()
  171. {
  172. var array = new Bar [] { new Bar (0), new Bar (1), new Bar (2), new Bar (3) };
  173. var at = CreateArrayAccess<Bar> ();
  174. Assert.AreEqual (array [0], at (array, 0));
  175. Assert.AreEqual (array [3], at (array, 3));
  176. }
  177. }
  178. }