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