ExpressionTest_ArrayIndex.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }
  123. }