ExpressionTest_ArrayIndex.cs 6.5 KB

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