PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/ETScenarios/Operators/GreaterThan.cs

#
C# | 634 lines | 460 code | 145 blank | 29 comment | 5 complexity | 456e2b4960d9de4933b067e5fe466820 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #if !CLR2
  2. using System.Linq.Expressions;
  3. #else
  4. using Microsoft.Scripting.Ast;
  5. #endif
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. namespace ETScenarios.Operators {
  10. using EU = ETUtils.ExpressionUtils;
  11. using Expr = Expression;
  12. using AstUtils = Microsoft.Scripting.Ast.Utils;
  13. public class GreaterThan {
  14. // GreaterThan of boolean constant
  15. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 1", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  16. public static Expr GreaterThan1(EU.IValidator V) {
  17. List<Expression> Expressions = new List<Expression>();
  18. Expressions.Add(
  19. EU.GenAreEqual(
  20. Expr.GreaterThan(Expr.Constant(1), Expr.Constant(2)),
  21. Expr.Constant(false)));
  22. Expressions.Add(
  23. EU.GenAreEqual(
  24. Expr.GreaterThan(Expr.Constant(2), Expr.Constant(1)),
  25. Expr.Constant(true)));
  26. var tree = EU.BlockVoid(Expressions);
  27. V.Validate(tree);
  28. return tree;
  29. }
  30. // GreaterThan of string constant
  31. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 2", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  32. public static Expr GreaterThan2(EU.IValidator V) {
  33. List<Expression> Expressions = new List<Expression>();
  34. ParameterExpression String1 = Expr.Variable(typeof(string), "");
  35. ParameterExpression String2 = Expr.Variable(typeof(string), "");
  36. Expressions.Add(Expr.Assign(String1, Expr.Constant("Hel")));
  37. Expressions.Add(Expr.Assign(String1, EU.ConcatEquals(String1, "lo")));
  38. Expressions.Add(Expr.Assign(String2, Expr.Constant("Hello")));
  39. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  40. {
  41. EU.GenAreEqual(
  42. Expr.GreaterThan(String1, String2),
  43. Expr.Constant(true));
  44. }));
  45. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  46. {
  47. EU.GenAreEqual(
  48. Expr.GreaterThan(Expr.Constant("Hello"), Expr.Constant("World")),
  49. Expr.Constant(false));
  50. }));
  51. return Expr.Empty();
  52. }
  53. internal class TestClass {
  54. int _x;
  55. internal TestClass(int val) { _x = val; }
  56. }
  57. // GreaterThan of class object, no user defined operator
  58. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 3", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  59. public static Expr GreaterThan3(EU.IValidator V) {
  60. List<Expression> Expressions = new List<Expression>();
  61. Expr Left = Expr.Variable(typeof(TestClass), "");
  62. Expressions.Add(Expr.Assign(Left, Expr.Constant(new TestClass(1))));
  63. Expr Right = Expr.Variable(typeof(TestClass), "");
  64. Expressions.Add(Expr.Assign(Right, Expr.Constant(new TestClass(2))));
  65. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  66. {
  67. Expr.GreaterThan(Left, Right);
  68. }));
  69. return Expr.Empty();
  70. }
  71. internal struct TestStruct {
  72. int _x;
  73. internal TestStruct(int val) { _x = val; }
  74. }
  75. // GreaterThan of struct object, no user defined operator
  76. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 4", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  77. public static Expr GreaterThan4(EU.IValidator V) {
  78. List<Expression> Expressions = new List<Expression>();
  79. Expr Left = Expr.Variable(typeof(TestStruct), "");
  80. Expressions.Add(Expr.Assign(Left, Expr.Constant(new TestStruct(1))));
  81. Expr Right = Expr.Variable(typeof(TestStruct), "");
  82. Expressions.Add(Expr.Assign(Right, Expr.Constant(new TestStruct(2))));
  83. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  84. {
  85. Expr.GreaterThan(Left, Right);
  86. }));
  87. return Expr.Empty();
  88. }
  89. // GreaterThan of sbyte, byte, short, ushort, int, uint,long,ulong, single, double, decimal constants
  90. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 5", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  91. public static Expr GreaterThan5(EU.IValidator V) {
  92. List<Expression> Expressions = new List<Expression>();
  93. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Int16)1), Expr.Constant((Int16)2)), Expr.Constant(false), "GreaterThan 1"));
  94. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Int16)(1)), Expr.Constant((Int16)(-1))), Expr.Constant(true), "GreaterThan 2"));
  95. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((UInt16)1), Expr.Constant((UInt16)2)), Expr.Constant(false), "GreaterThan 3"));
  96. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((UInt16)2), Expr.Constant((UInt16)1)), Expr.Constant(true), "GreaterThan 4"));
  97. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((short)1), Expr.Constant((short)2)), Expr.Constant(false), "GreaterThan 5"));
  98. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((short)200), Expr.Constant((short)0)), Expr.Constant(true), "GreaterThan 6"));
  99. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((ushort)1), Expr.Constant((ushort)2)), Expr.Constant(false), "GreaterThan 7"));
  100. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((ushort)4), Expr.Constant((ushort)2)), Expr.Constant(true), "GreaterThan 8"));
  101. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Int32)1), Expr.Constant((Int32)(2))), Expr.Constant(false), "GreaterThan 9"));
  102. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Int32)Int32.MaxValue), Expr.Constant((Int32)(-1))), Expr.Constant(true), "GreaterThan 10"));
  103. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((UInt32)1), Expr.Constant((UInt32)2)), Expr.Constant(false), "GreaterThan 11"));
  104. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((UInt32)1), Expr.Constant((UInt32)0)), Expr.Constant(true), "GreaterThan 12"));
  105. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((long)1.0), Expr.Constant((long)2.0)), Expr.Constant(false), "GreaterThan 13"));
  106. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((long)1.0), Expr.Constant((long)(-0.5))), Expr.Constant(true), "GreaterThan 14"));
  107. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((ulong)1.0), Expr.Constant((ulong)2.0)), Expr.Constant(false), "GreaterThan 15"));
  108. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((ulong)2.0), Expr.Constant((ulong)1.0)), Expr.Constant(true), "GreaterThan 16"));
  109. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Single)1.0), Expr.Constant((Single)2.0)), Expr.Constant(false), "GreaterThan 17"));
  110. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Single)(-2.0)), Expr.Constant((Single)(-3.0))), Expr.Constant(true), "GreaterThan 18"));
  111. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Double)1.0), Expr.Constant((Double)2.0)), Expr.Constant(false), "GreaterThan 19"));
  112. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Double)2.5), Expr.Constant((Double)2.4)), Expr.Constant(true), "GreaterThan 20"));
  113. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((decimal)1.0), Expr.Constant((decimal)2.0)), Expr.Constant(false), "GreaterThan 21"));
  114. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((decimal)1.333333), Expr.Constant((decimal)1.333332)), Expr.Constant(true), "GreaterThan 22"));
  115. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((SByte)1), Expr.Constant((SByte)2)), Expr.Constant(false), "GreaterThan 23")); // Int16 is CLS compliant equivalent type
  116. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((SByte)1), Expr.Constant((SByte)0)), Expr.Constant(true), "GreaterThan 24"));
  117. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Byte)1), Expr.Constant((Byte)2)), Expr.Constant(false), "GreaterThan 25"));
  118. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Byte)5), Expr.Constant((Byte)0)), Expr.Constant(true), "GreaterThan 26"));
  119. var tree = EU.BlockVoid(Expressions);
  120. V.Validate(tree);
  121. return tree;
  122. }
  123. // pass null to method, same typed arguments to left and right
  124. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 6", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  125. public static Expr GreaterThan6(EU.IValidator V) {
  126. List<Expression> Expressions = new List<Expression>();
  127. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(4, typeof(Int32)), Expr.Constant(4, typeof(Int32)), false, null), Expr.Constant(false), "GreaterThan 1"));
  128. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(4, typeof(Int32)), Expr.Constant(5, typeof(Int32)), false, null), Expr.Constant(false), "GreaterThan 2"));
  129. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(4, typeof(Int32)), Expr.Constant(3, typeof(Int32)), false, null), Expr.Constant(true), "GreaterThan 3"));
  130. Expr Res = Expr.GreaterThan(
  131. Expr.Constant(4, typeof(Nullable<int>)),
  132. Expr.Constant(4, typeof(Nullable<int>)),
  133. true,
  134. null
  135. );
  136. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<bool>)false, typeof(Nullable<bool>)), "GreaterThan 4"));
  137. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<bool>)));
  138. Res = Expr.GreaterThan(
  139. Expr.Constant(5, typeof(Nullable<int>)),
  140. Expr.Constant(4, typeof(Nullable<int>)),
  141. false,
  142. null
  143. );
  144. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true), "GreaterThan 5"));
  145. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  146. var tree = EU.BlockVoid(Expressions);
  147. V.Validate(tree);
  148. return tree;
  149. }
  150. // pass a MethodInfo that takes no arguments
  151. public static int GreaterThanNoArgs() {
  152. return -1;
  153. }
  154. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 7", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(ArgumentException))]
  155. public static Expr GreaterThan7(EU.IValidator V) {
  156. List<Expression> Expressions = new List<Expression>();
  157. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanNoArgs");
  158. Expressions.Add(EU.Throws<System.ArgumentException>(() =>
  159. {
  160. EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(1, typeof(Int32)), Expr.Constant(2, typeof(Int32)), false, mi), Expr.Constant(false));
  161. }));
  162. return Expr.Empty();
  163. }
  164. // pass a MethodInfo that takes a paramarray
  165. public static int GreaterThanParamArray(params int[] args) {
  166. return -1;
  167. }
  168. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 8", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(ArgumentException))]
  169. public static Expr GreaterThan8(EU.IValidator V) {
  170. List<Expression> Expressions = new List<Expression>();
  171. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanParamArray");
  172. Expressions.Add(EU.Throws<System.ArgumentException>(() =>
  173. {
  174. EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(1, typeof(Int32)), Expr.Constant(2, typeof(Int32)), false, mi), Expr.Constant(false));
  175. }));
  176. return Expr.Empty();
  177. }
  178. // with a valid MethodInfo, greaterthan two values of the same type
  179. public static bool GreaterThanInts(int x, int y) {
  180. return x > y;
  181. }
  182. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 9", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  183. public static Expr GreaterThan9(EU.IValidator V) {
  184. List<Expression> Expressions = new List<Expression>();
  185. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanInts");
  186. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(10, typeof(Int32)), Expr.Constant(3, typeof(Int32)), false, mi), Expr.Constant(true)));
  187. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(10, typeof(Int32)), Expr.Constant(10, typeof(Int32)), false, mi), Expr.Constant(false)));
  188. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(1, typeof(Int32)), Expr.Constant(10, typeof(Int32)), false, mi), Expr.Constant(false)));
  189. var tree = EU.BlockVoid(Expressions);
  190. V.Validate(tree);
  191. return tree;
  192. }
  193. // With a valid method info, pass two values of the same value type that widen to the arguments of the method
  194. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 10", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  195. public static Expr GreaterThan10(EU.IValidator V) {
  196. List<Expression> Expressions = new List<Expression>();
  197. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanInts");
  198. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  199. {
  200. EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Int16)1), Expr.Constant((Int16)2), false, mi), Expr.Constant(false));
  201. }));
  202. return Expr.Empty();
  203. }
  204. // With a valid method info, pass two values of a derived class of the methodinfo’s arguments
  205. public static bool GreaterThanExceptionMsg(Exception e1, Exception e2) {
  206. return e1.Message == e2.Message;
  207. }
  208. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 11", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  209. public static Expr GreaterThan11(EU.IValidator V) {
  210. List<Expression> Expressions = new List<Expression>();
  211. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanExceptionMsg");
  212. Expr Res =
  213. Expr.GreaterThan(
  214. Expr.Constant(new DivideByZeroException("One"), typeof(DivideByZeroException)),
  215. Expr.Constant(new RankException("One"), typeof(RankException)),
  216. false,
  217. mi
  218. );
  219. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true), "GreaterThan 1"));
  220. Expr Res2 =
  221. Expr.GreaterThan(
  222. Expr.Constant(new IndexOutOfRangeException("Two"), typeof(IndexOutOfRangeException)),
  223. Expr.Constant(new ArgumentNullException("Three"), typeof(ArgumentNullException)),
  224. false,
  225. mi
  226. );
  227. Expressions.Add(EU.GenAreEqual(Res2, Expr.Constant(false), "GreaterThan 2"));
  228. var tree = EU.BlockVoid(Expressions);
  229. V.Validate(tree);
  230. return tree;
  231. }
  232. // With a valid methodinfo, pass a value of non nullable type, the other of nullable type.
  233. public static bool GreaterThanNullableInt(int? x, int y) {
  234. return x.GetValueOrDefault() > y;
  235. }
  236. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 12", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  237. public static Expr GreaterThan12(EU.IValidator V) {
  238. List<Expression> Expressions = new List<Expression>();
  239. MethodInfo mi = typeof(GreaterThan).GetMethod("GreaterThanNullableInt");
  240. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant(1), false, mi), Expr.Constant(true), "GreaterThan 1"));
  241. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant(2), false, mi), Expr.Constant(false), "GreaterThan 2"));
  242. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant(2), false, mi), Expr.Constant(false), "GreaterThan 3"));
  243. var tree = EU.BlockVoid(Expressions);
  244. V.Validate(tree);
  245. return tree;
  246. }
  247. // With a null methodinfo, pass a value of non nullable type, the other of nullable type.
  248. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 13", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  249. public static Expr GreaterThan13(EU.IValidator V) {
  250. List<Expression> Expressions = new List<Expression>();
  251. MethodInfo mi = null;
  252. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  253. {
  254. EU.GenAreEqual(
  255. Expr.GreaterThan(Expr.Constant(1, typeof(Int32)), Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), false, mi), Expr.Constant(false)
  256. );
  257. }));
  258. return Expr.Empty();
  259. }
  260. // GreaterThan of two values of different types
  261. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 14", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  262. public static Expr GreaterThan14(EU.IValidator V) {
  263. List<Expression> Expressions = new List<Expression>();
  264. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  265. {
  266. EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(true), Expr.Constant(1)), Expr.Constant(1), "GreaterThan 1");
  267. }));
  268. return Expr.Empty();
  269. }
  270. // GreaterThaning across mixed types, no user defined operator
  271. // TODO: more types, automated generation?
  272. [ETUtils.TestAttribute(ETUtils.TestState.Disabled, "GreaterThan 15", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  273. public static Expr GreaterThan15(EU.IValidator V) {
  274. List<Expression> Expressions = new List<Expression>();
  275. MethodInfo mi = null;
  276. Expressions.Add(
  277. EU.GenAreEqual(
  278. Expr.GreaterThan(Expr.Constant(1), Expr.Constant(2.0), false, mi), Expr.Constant(3)
  279. )
  280. );
  281. var tree = EU.BlockVoid(Expressions);
  282. V.ValidateException<InvalidOperationException>(tree, false);
  283. return tree;
  284. }
  285. // User defined overloaded operator on left argument, arguments are the proper types
  286. public class MyVal {
  287. public int Val { get; set; }
  288. public MyVal(int x) { Val = x; }
  289. public static bool operator >(MyVal v1, int v2) {
  290. return (v1.Val > v2);
  291. }
  292. public static bool operator <(MyVal v1, int v2) {
  293. return (v1.Val < v2);
  294. }
  295. }
  296. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 16", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  297. public static Expr GreaterThan16(EU.IValidator V) {
  298. List<Expression> Expressions = new List<Expression>();
  299. MethodInfo mi = null;
  300. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(new MyVal(6)), Expr.Constant(4), false, mi), Expr.Constant(true)));
  301. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(new MyVal(-1)), Expr.Constant(6), false, mi), Expr.Constant(false)));
  302. Expressions.Add(EU.GenAreEqual(Expr.GreaterThan(Expr.Constant(new MyVal(6)), Expr.Constant(6), false, mi), Expr.Constant(false)));
  303. var tree = EU.BlockVoid(Expressions);
  304. V.Validate(tree);
  305. return tree;
  306. }
  307. // Verify order of evaluation of expressions on greaterthan
  308. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 17", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  309. public static Expr GreaterThan17(EU.IValidator V) {
  310. List<Expression> Expressions = new List<Expression>();
  311. ParameterExpression Result = Expr.Variable(typeof(string), "");
  312. Expr Res =
  313. Expr.Block(
  314. EU.ConcatEquals(Result, "One"),
  315. Expr.GreaterThan(
  316. Expr.Add(
  317. Expr.Block(EU.ConcatEquals(Result, "Two"), Expr.Constant(4)),
  318. Expr.Block(EU.ConcatEquals(Result, "Three"), Expr.Constant(-1))
  319. ),
  320. Expr.Add(
  321. Expr.Block(EU.ConcatEquals(Result, "Four"), Expr.Constant(1)),
  322. Expr.Block(EU.ConcatEquals(Result, "Five"), Expr.Constant(6))
  323. )
  324. )
  325. );
  326. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false)));
  327. Expressions.Add(EU.GenAreEqual(Result, Expr.Constant("OneTwoThreeFourFive")));
  328. Expressions.Add(Expr.Assign(Result, Expr.Constant("")));
  329. Res =
  330. Expr.Block(
  331. EU.ConcatEquals(Result, "One"),
  332. Expr.GreaterThan(
  333. Expr.Add(
  334. Expr.Block(EU.ConcatEquals(Result, "Two"), Expr.Constant(4)),
  335. Expr.Block(EU.ConcatEquals(Result, "Three"), Expr.Constant(-5))
  336. ),
  337. Expr.Add(
  338. Expr.Block(EU.ConcatEquals(Result, "Four"), Expr.Constant(1)),
  339. Expr.Block(EU.ConcatEquals(Result, "Five"), Expr.Constant(-6))
  340. )
  341. )
  342. );
  343. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true)));
  344. Expressions.Add(EU.GenAreEqual(Result, Expr.Constant("OneTwoThreeFourFive")));
  345. var tree = EU.BlockVoid(new[] { Result }, Expressions);
  346. V.Validate(tree);
  347. return tree;
  348. }
  349. // For: With a valid methodinfo that returns boolean, pass arguments of nullable type.
  350. public static bool testLiftNullable(int x, int y) {
  351. return x > y;
  352. }
  353. // For: With a valid methodinfo that returns non-boolean, pass arguments of nullable type.
  354. public static int testLiftNullableReturnInt(int x, int y) {
  355. return (x > y) ? 1 : 0;
  356. }
  357. public static bool compareNullables(int? x, int? y) {
  358. if (x.HasValue && y.HasValue)
  359. return x > y;
  360. return false;
  361. }
  362. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 18", new string[] { "positive", "greaterthan", "operators", "Pri1" })]
  363. public static Expr GreaterThan18(EU.IValidator V) {
  364. List<Expression> Expressions = new List<Expression>();
  365. MethodInfo mi = typeof(GreaterThan).GetMethod("testLiftNullable");
  366. // With a valid methodinfo that returns boolean, pass arguments of nullable type. Set lift to null to false.
  367. Expr Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), false, mi);
  368. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false, typeof(bool)), "GreaterThan 1"));
  369. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  370. // With a valid methodinfo that returns boolean, pass arguments of nullable type with null values. Set lift to null to false.
  371. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), false, mi);
  372. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true, typeof(bool)), "GreaterThan 2"));
  373. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  374. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), false, mi);
  375. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false, typeof(bool)), "GreaterThan 3"));
  376. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  377. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), false, mi);
  378. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false, typeof(bool)), "GreaterThan 4"));
  379. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  380. // With a valid methodinfo that returns boolean, pass arguments of nullable type with null values. Set lift to null to true.
  381. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)0, typeof(Nullable<int>)), true, mi);
  382. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(null, typeof(Nullable<bool>)), "GreaterThan 5"));
  383. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<bool>)));
  384. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), true, mi);
  385. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(null, typeof(Nullable<bool>)), "GreaterThan 6"));
  386. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<bool>)));
  387. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), true, mi);
  388. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true, typeof(Nullable<bool>)), "GreaterThan 7"));
  389. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<bool>)));
  390. // With a valid methodinfo that returns non boolean, pass arguments of nullable type. Set lift to null to true.
  391. mi = typeof(GreaterThan).GetMethod("testLiftNullableReturnInt");
  392. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), true, mi);
  393. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), "GreaterThan 8"));
  394. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<int>)));
  395. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), true, mi);
  396. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), "GreaterThan 9"));
  397. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<int>)));
  398. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), true, mi);
  399. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<int>)0, typeof(Nullable<int>)), "GreaterThan 10"));
  400. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<int>)));
  401. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), true, mi);
  402. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), "GreaterThan 11"));
  403. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<int>)));
  404. // With a valid methodinfo that returns non boolean, pass arguments of nullable type. Set lift to null to false.
  405. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), false, mi);
  406. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<Int32>)null, typeof(Nullable<Int32>)), "GreaterThan 12"));
  407. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<Int32>)));
  408. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), false, mi);
  409. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<Int32>)1, typeof(Nullable<Int32>)), "GreaterThan 13"));
  410. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<Int32>)));
  411. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)), false, mi);
  412. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<Int32>)0, typeof(Nullable<Int32>)), "GreaterThan 14"));
  413. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<Int32>)));
  414. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), false, mi);
  415. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant((Nullable<Int32>)null, typeof(Nullable<Int32>)), "GreaterThan 15"));
  416. Expressions.Add(EU.ExprTypeCheck(Res, typeof(Nullable<Int32>)));
  417. // With a valid methodinfo that returns non boolean, pass arguments of nullable type. Set lift to null to true.
  418. mi = typeof(GreaterThan).GetMethod("compareNullables");
  419. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)null, typeof(Nullable<int>)), true, mi);
  420. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false), "GreaterThan 16"));
  421. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  422. // With a valid methodinfo that returns non boolean, pass arguments of nullable type. Set lift to null to false.
  423. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)3, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)2, typeof(Nullable<int>)), false, mi);
  424. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(true), "GreaterThan 17"));
  425. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  426. Res = Expr.GreaterThan(Expr.Constant((Nullable<int>)3, typeof(Nullable<int>)), Expr.Constant((Nullable<int>)3, typeof(Nullable<int>)), false, mi);
  427. Expressions.Add(EU.GenAreEqual(Res, Expr.Constant(false), "GreaterThan 18"));
  428. Expressions.Add(EU.ExprTypeCheck(Res, typeof(bool)));
  429. var tree = EU.BlockVoid(Expressions);
  430. V.Validate(tree);
  431. return tree;
  432. }
  433. // GreaterThan on a type with IComparable definition, no user defined comparison operators
  434. public class MyGenericComparable : IComparable<MyGenericComparable> {
  435. int Val { get; set; }
  436. public MyGenericComparable(int x) { Val = x; }
  437. public int CompareTo(MyGenericComparable other) {
  438. return Val.CompareTo(other.Val);
  439. }
  440. }
  441. public class MyComparable : IComparable {
  442. int Val { get; set; }
  443. public MyComparable(int x) { Val = x; }
  444. public int CompareTo(object obj) {
  445. if (obj is MyComparable) {
  446. MyComparable other = (MyComparable)obj;
  447. return this.Val.CompareTo(other.Val);
  448. } else {
  449. throw new ArgumentException("Object is not a MyComparable");
  450. }
  451. }
  452. }
  453. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 19", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  454. public static Expr GreaterThan19(EU.IValidator V) {
  455. List<Expression> Expressions = new List<Expression>();
  456. ParameterExpression GenCompareValue1 = Expr.Variable(typeof(MyGenericComparable), "");
  457. ParameterExpression GenCompareValue2 = Expr.Variable(typeof(MyGenericComparable), "");
  458. Expressions.Add(Expr.Assign(GenCompareValue1, Expr.Constant(new MyGenericComparable(1))));
  459. Expressions.Add(Expr.Assign(GenCompareValue2, Expr.Constant(new MyGenericComparable(2))));
  460. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  461. {
  462. EU.GenAreEqual(Expr.GreaterThan(GenCompareValue1, GenCompareValue2), Expr.Constant(false), "GreaterThan 1");
  463. }));
  464. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  465. {
  466. EU.GenAreEqual(Expr.GreaterThan(GenCompareValue1, GenCompareValue1), Expr.Constant(true), "GreaterThan 2");
  467. }));
  468. return Expr.Empty();
  469. }
  470. // GreaterThan on a type with IComparable definition, no user defined comparison operators
  471. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "GreaterThan 20", new string[] { "negative", "greaterthan", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  472. public static Expr GreaterThan20(EU.IValidator V) {
  473. List<Expression> Expressions = new List<Expression>();
  474. ParameterExpression CompareValue1 = Expr.Variable(typeof(MyComparable), "");
  475. ParameterExpression CompareValue2 = Expr.Variable(typeof(MyComparable), "");
  476. Expressions.Add(Expr.Assign(CompareValue1, Expr.Constant(new MyComparable(1))));
  477. Expressions.Add(Expr.Assign(CompareValue2, Expr.Constant(new MyComparable(2))));
  478. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  479. {
  480. EU.GenAreEqual(Expr.GreaterThan(CompareValue1, CompareValue2), Expr.Constant(false), "GreaterThan 1");
  481. }));
  482. Expressions.Add(EU.Throws<System.InvalidOperationException>(() =>
  483. {
  484. EU.GenAreEqual(Expr.GreaterThan(CompareValue2, CompareValue2), Expr.Constant(true), "GreaterThan 2");
  485. }));
  486. return Expr.Empty();
  487. }
  488. }
  489. }