PageRenderTime 43ms CodeModel.GetById 18ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
C# | 220 lines | 164 code | 41 blank | 15 comment | 0 complexity | 71d7e46cba72e6fe3d15353f02814c92 MD5 | raw file
  1#if !CLR2
  2using System.Linq.Expressions;
  3#else
  4using Microsoft.Scripting.Ast;
  5using Microsoft.Scripting.Utils;
  6#endif
  7
  8using System;
  9using System.Collections;
 10using System.Collections.Generic;
 11using System.Reflection;
 12
 13namespace ETScenarios.Operators {
 14    using EU = ETUtils.ExpressionUtils;
 15    using Expr = Expression;
 16    using AstUtils = Microsoft.Scripting.Ast.Utils;
 17
 18    public class Decrement {
 19        // Decrement of numeric types
 20        // Regression for Dev10 Bug 546775
 21        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 1", new string[] { "positive", "decrement", "operators", "Pri1" })]
 22        public static Expr Decrement1(EU.IValidator V) {
 23            List<Expression> Expressions = new List<Expression>();
 24
 25            Expressions.Add(EU.GenAreEqual(Expr.Constant((short)(-1), typeof(short)), Expr.Decrement(Expr.Constant((short)(0), typeof(short))), "Decrement 1"));
 26            Expressions.Add(EU.GenAreEqual(Expr.Constant((ushort)1, typeof(ushort)), Expr.Decrement(Expr.Constant((ushort)2, typeof(ushort))), "Decrement 2"));
 27            Expressions.Add(EU.GenAreEqual(Expr.Constant((Int16)0, typeof(Int16)), Expr.Decrement(Expr.Constant((Int16)1, typeof(Int16))), "Decrement 3"));
 28            Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt16)1, typeof(UInt16)), Expr.Decrement(Expr.Constant((UInt16)2, typeof(UInt16))), "Decrement 4"));
 29            Expressions.Add(EU.GenAreEqual(Expr.Constant((Int32)Int32.MaxValue, typeof(Int32)), Expr.Decrement(Expr.Constant((Int32)Int32.MinValue, typeof(Int32))), "Decrement 5"));
 30            Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt32)1, typeof(UInt32)), Expr.Decrement(Expr.Constant((UInt32)2, typeof(UInt32))), "Decrement 6"));
 31            Expressions.Add(EU.GenAreEqual(Expr.Constant((double)1.0, typeof(double)), Expr.Decrement(Expr.Constant((double)2.0, typeof(double))), "Decrement 7"));
 32            Expressions.Add(EU.GenAreEqual(Expr.Constant((long)1.1, typeof(long)), Expr.Decrement(Expr.Constant((long)2.1, typeof(long))), "Decrement 8"));
 33            Expressions.Add(EU.GenAreEqual(Expr.Constant((ulong)1.001, typeof(ulong)), Expr.Decrement(Expr.Constant((ulong)2.001, typeof(ulong))), "Decrement 9"));
 34            Expressions.Add(EU.GenAreEqual(Expr.Constant((decimal)(0.001), typeof(decimal)), Expr.Decrement(Expr.Constant((decimal)1.001, typeof(decimal))), "Decrement 10"));
 35
 36            var tree = Expr.Block(Expressions);
 37            V.Validate(tree);
 38            return tree;
 39        }
 40
 41        // Decrement of void
 42        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 2", new string[] { "negative", "decrement", "operators", "Pri1" }, Exception = typeof(ArgumentException))]
 43        public static Expr Decrement2(EU.IValidator V) {
 44            List<Expression> Expressions = new List<Expression>();
 45
 46            var Result = EU.Throws<ArgumentException>(() => { Expr.Variable(typeof(void), "Result"); });
 47
 48            Expr.Decrement(Result);
 49
 50            return Expr.Empty();
 51        }
 52
 53        // Pass null to expression
 54        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 3", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(ArgumentNullException))]
 55        public static Expr Decrement3(EU.IValidator V) {
 56            List<Expression> Expressions = new List<Expression>();
 57			EU.Throws<System.ArgumentNullException>(() => {
 58	            Expressions.Add(Expr.Decrement(null)	);
 59				});
 60            return Expr.Empty();
 61        }
 62
 63        public class TestClass {
 64            public int X { get; set; }
 65            public TestClass(int val) { X = val; }
 66            public static TestClass operator --(TestClass a) {
 67                a.X -= 1;
 68                return a;
 69            }
 70            public static int TestMethod(int x) {
 71                return x - 2;
 72            }
 73        }
 74
 75        // Decrement of a user defined type that defines the decrement operator
 76        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 4", new string[] { "positive", "decrement", "operators", "Pri2" })]
 77        public static Expr Decrement4(EU.IValidator V) {
 78            List<Expression> Expressions = new List<Expression>();
 79
 80            ParameterExpression Result = Expr.Variable(typeof(TestClass), "Result");
 81            var ci = typeof(TestClass).GetConstructor(new Type[] { typeof(int) });
 82            Expressions.Add(Expr.Assign(Result, Expr.New(ci, new Expression[] { Expr.Constant(0) })));
 83
 84            PropertyInfo pi = typeof(TestClass).GetProperty("X");
 85            Expr newValue = Expr.Decrement(Result);
 86            Expressions.Add(EU.GenAreEqual(Expr.Constant(-1), Expr.Property(newValue, pi), "Decrement 1"));
 87
 88            var tree = Expr.Block(new[] { Result }, Expressions);
 89            V.Validate(tree);
 90            return tree;
 91        }
 92
 93        // Decrement of string
 94        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 5", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(InvalidOperationException))]
 95        public static Expr Decrement5(EU.IValidator V) {
 96            List<Expression> Expressions = new List<Expression>();
 97            Expressions.Add(EU.Throws<InvalidOperationException>(() => { Expr.Decrement(Expr.Constant("1")); }));
 98            return Expr.Empty();
 99        }
100
101        // Decrement of date
102        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 6", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(InvalidOperationException))]
103        public static Expr Decrement6(EU.IValidator V) {
104            List<Expression> Expressions = new List<Expression>();
105            Expressions.Add(EU.Throws<InvalidOperationException>(() => { EU.GenAreEqual(Expr.Constant(DateTime.Parse("1/2/2009")), Expr.Decrement(Expr.Constant(DateTime.Parse("1/1/2009"))), "Decrement 1"); }));
106            return Expr.Empty();
107        }
108
109        // Overflow an int with the decrement operator
110        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 7", new string[] { "positive", "decrement", "operators", "Pri2" })]
111        public static Expr Decrement7(EU.IValidator V) {
112            List<Expression> Expressions = new List<Expression>();
113            Expressions.Add(EU.GenAreEqual(Expr.Constant(Int32.MaxValue), Expr.Decrement(Expr.Constant(Int32.MinValue)), "Decrement 1"));
114            var tree = Expr.Block(Expressions);
115            V.Validate(tree);
116            return tree;
117        }
118
119        // Pass null to expression
120        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 8", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(ArgumentNullException))]
121        public static Expr Decrement8(EU.IValidator V) {
122            List<Expression> Expressions = new List<Expression>();
123
124            MethodInfo mi = typeof(TestClass).GetMethod("TestMethod");
125            Expressions.Add(EU.Throws<ArgumentNullException>(() => { Expr.Decrement(null, mi); }));
126
127            return Expr.Empty();
128        }
129
130        // With a null method, decrement of numeric types
131        // Regression for Dev10 Bug 546775
132        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 9", new string[] { "positive", "decrement", "operators", "Pri2" })]
133        public static Expr Decrement9(EU.IValidator V) {
134            List<Expression> Expressions = new List<Expression>();
135
136            Expressions.Add(EU.GenAreEqual(Expr.Constant((short)(-1), typeof(short)), Expr.Decrement(Expr.Constant((short)(0), typeof(short)), null), "Decrement 1"));
137            Expressions.Add(EU.GenAreEqual(Expr.Constant((ushort)1, typeof(ushort)), Expr.Decrement(Expr.Constant((ushort)2, typeof(ushort)), null), "Decrement 2"));
138            Expressions.Add(EU.GenAreEqual(Expr.Constant((Int16)0, typeof(Int16)), Expr.Decrement(Expr.Constant((Int16)1, typeof(Int16)), null), "Decrement 3"));
139            Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt16)1, typeof(UInt16)), Expr.Decrement(Expr.Constant((UInt16)2, typeof(UInt16)), null), "Decrement 4"));
140            Expressions.Add(EU.GenAreEqual(Expr.Constant((Int32)Int32.MaxValue, typeof(Int32)), Expr.Decrement(Expr.Constant((Int32)Int32.MinValue, typeof(Int32)), null), "Decrement 5"));
141            Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt32)1, typeof(UInt32)), Expr.Decrement(Expr.Constant((UInt32)2, typeof(UInt32)), null), "Decrement 6"));
142            Expressions.Add(EU.GenAreEqual(Expr.Constant((double)1.0, typeof(double)), Expr.Decrement(Expr.Constant((double)2.0, typeof(double)), null), "Decrement 7"));
143            Expressions.Add(EU.GenAreEqual(Expr.Constant((long)1.1, typeof(long)), Expr.Decrement(Expr.Constant((long)2.1, typeof(long)), null), "Decrement 8"));
144            Expressions.Add(EU.GenAreEqual(Expr.Constant((ulong)1.001, typeof(ulong)), Expr.Decrement(Expr.Constant((ulong)2.001, typeof(ulong)), null), "Decrement 9"));
145            Expressions.Add(EU.GenAreEqual(Expr.Constant((decimal)(0.001), typeof(decimal)), Expr.Decrement(Expr.Constant((decimal)1.001, typeof(decimal)), null), "Decrement 10"));
146
147            var tree = Expr.Block(Expressions);
148            V.Validate(tree);
149            return tree;
150        }
151
152        // Pass a method that takes no arguments
153        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 10", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(ArgumentException))]
154        public static Expr Decrement10(EU.IValidator V) {
155            List<Expression> Expressions = new List<Expression>();
156
157            Func<int> f = () => 1;
158            MethodInfo mi = f.Method;
159
160            Expressions.Add(EU.Throws<System.ArgumentException>(() =>
161            {
162                EU.GenAreEqual(Expr.Constant(0), Expr.Decrement(Expr.Constant(1), mi), "Decrement 1");
163            }));
164
165            return Expr.Block(Expressions);
166        }
167
168        public static int paramTestMethod(params int[] args) {
169            return 3;
170        }
171
172        // Pass a method that takes a param array
173        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 11", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(InvalidOperationException))]
174        public static Expr Decrement11(EU.IValidator V) {
175            List<Expression> Expressions = new List<Expression>();
176
177            MethodInfo mi = typeof(Decrement).GetMethod("paramTestMethod");
178            EU.Throws<System.InvalidOperationException>(() =>
179            {
180                Expressions.Add(EU.GenAreEqual(Expr.Constant(3), Expr.Decrement(Expr.Constant(1), mi), "Decrement 1"));
181            });
182
183            return Expr.Empty();
184        }
185
186        // Pass a method that takes 2 arguments
187        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 12", new string[] { "negative", "decrement", "operators", "Pri2" }, Exception = typeof(ArgumentException))]
188        public static Expr Decrement12(EU.IValidator V) {
189            List<Expression> Expressions = new List<Expression>();
190
191            Func<int, int, int> f = (x, y) => x + y;
192            MethodInfo mi = f.Method;
193
194            Expressions.Add(EU.Throws<System.ArgumentException>(() =>
195            {
196                EU.GenAreEqual(Expr.Constant(5), Expr.Decrement(Expr.Constant(4), mi), "Decrement 1");
197            }));
198
199            return Expr.Empty();
200        }
201
202        public static double typeTestMethod(int x) {
203            return (double)x - 1.0;
204        }
205
206        // Pass a method that returns a different type from expression
207        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Decrement 13", new string[] { "positive", "decrement", "operators", "Pri2" })]
208        public static Expr Decrement13(EU.IValidator V) {
209            List<Expression> Expressions = new List<Expression>();
210
211            MethodInfo mi = typeof(Decrement).GetMethod("typeTestMethod");
212
213            Expressions.Add(EU.GenAreEqual(Expr.Constant((double)2.0, typeof(double)), Expr.Decrement(Expr.Constant(3), mi), "Decrement 1"));
214
215            var tree = Expr.Block(Expressions);
216            V.Validate(tree);
217            return tree;
218        }
219    }
220}