// constants, like this case. Need to figure out what to do
// with those
//
-
+
Func<OpClass> compiled = l.Compile ();
Assert.AreEqual (left, compiled ());
#endif
return 1000;
}
}
-
+
[Test]
public void TestMethodAddition ()
{
Func<int> compiled = l.Compile ();
Assert.AreEqual (1000, compiled ());
-
+
}
}
}
Func<T> del = l.Compile ();
del ();
}
-
+
//
// SubtractChecked is not defined for small types (byte, sbyte)
//
// These are invalid:
InvalidOperation<byte> (Byte.MaxValue, 2);
InvalidOperation<sbyte> (SByte.MaxValue, 2);
-
+
// Stuff that just fits in 32 bits, does not overflow:
MustNotOverflow<short> (Int16.MaxValue, 2);
MustNotOverflow<ushort> (UInt16.MaxValue, 2);
public void AndTest ()
{
Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a & b;
-
+
Func<bool,bool,bool> c = e.Compile ();
-
+
Assert.AreEqual (true, c (true, true), "t1");
Assert.AreEqual (false, c (true, false), "t2");
Assert.AreEqual (false, c (false, true), "t3");
Assert.AreEqual (false, c (false, false), "t4");
}
-
+
[Test]
public void AndNullableTest ()
{
Expression<Func<bool?, bool?, bool?>> e = (bool? a, bool? b) => a & b;
-
+
Func<bool?,bool?,bool?> c = e.Compile ();
-
-
+
+
Assert.AreEqual (true, c (true, true), "a1");
Assert.AreEqual (false, c (true, false), "a2");
Assert.AreEqual (false, c (false, true), "a3");
Assert.AreEqual (false, c (false, false), "a4");
-
+
Assert.AreEqual (null, c (true, null), "a5");
Assert.AreEqual (false, c (false, null), "a6");
Assert.AreEqual (false, c (null, false), "a7");
[ExpectedException (typeof (ArgumentException))]
public void TestInvalidCtor_2 ()
{
- // type mismatch: int value, type == double
+ // type mismatch: int value, type == double
Expression.Constant (0, typeof (double));
}
Assert.AreEqual (typeof (int), c.Type, "#1");
Assert.AreEqual (1, c.Value, "#2");
}
-
+
[Test]
public void ConstantCodeGen ()
{
Expression<Func<int,int,bool>> pexpr = Expression.Lambda<Func<int,int,bool>> (
p, new ParameterExpression [] { a, b });
-
+
Func<int,int,bool> compiled = pexpr.Compile ();
Assert.AreEqual (true, compiled (10, 1), "tc1");
Assert.AreEqual (true, compiled (1, 0), "tc2");
static MethodInfo GM(string n)
{
MethodInfo [] m = typeof(ExpressionTest_Lifting).GetMethods(BindingFlags.Static | BindingFlags.Public);
-
+
foreach (MethodInfo mm in m)
if (mm.Name == n)
return mm;
-
+
throw new Exception("No method found: " + n);
}
-
+
static public int MyCompare (OpStruct a, OpStruct b)
{
return 1;
}
-
+
[Test]
public void TestLiftOnEqual_WithMethodInfo ()
{
BinaryExpression cmp2 = Expression.Equal (a, b, false, GM("MyCompare"));
//
- // When we use a MethodInfo, that has a non-bool return type,
+ // When we use a MethodInfo, that has a non-bool return type,
// the result is always Nullable<returntype> regardless of the
// setting of "liftToNull"
//
Assert.AreEqual (typeof(int?), cmp.Type);
Assert.AreEqual (typeof(int?), cmp2.Type);
-
+
}
}
}
{
return 1;
}
-
+
static MethodInfo GM (string n)
{
MethodInfo [] methods = typeof (ExpressionTest_MakeBinary).GetMethods (
throw new Exception (String.Format ("Method {0} not found", n));
}
-
+
[Test]
public void MethodChecks ()
{
Expression.Add (left, right, GM ("BadMethodSig_3"));
}
-
+
static void PassInt (ExpressionType nt)
{
Expression left = Expression.Constant (1);
FailInt (ExpressionType.TypeIs);
#endif
}
-
+
public delegate Expression BinaryExpr (Expression a, Expression b);
public T CodeGen<T> (BinaryExpr bin, T v1, T v2)
Func<T> fi = l.Compile ();
return fi ();
}
-
+
[Test]
public void TestOperations ()
{
BinaryExpression p = Expression.MakeBinary (node, Expression.Constant (a), Expression.Constant(b));
Expression<Func<T,T,bool>> pexpr = Expression.Lambda<Func<T,T,bool>> (
p, new ParameterExpression [] { pa, pb });
-
+
Func<T,T,bool> compiled = pexpr.Compile ();
Assert.AreEqual (r, compiled (a, b), String.Format ("{0} ({1},{2}) == {3}", node, a, b, r));
}
public void ComparisonTests ()
{
ExpressionType t = ExpressionType.Equal;
-
+
CTest<byte> (t, true, 10, 10);
CTest<sbyte> (t, false, 1, 5);
CTest<sbyte> (t, true, 1, 1);
CTest<string> (t, false, "Hey", "There");
t = ExpressionType.NotEqual;
-
+
CTest<byte> (t, false, 10, 10);
CTest<sbyte> (t, true, 1, 5);
CTest<sbyte> (t, false, 1, 1);
CTest<double> (t, false, 1.0, 1.0);
CTest<double> (t, false, 1.0, 1.0);
-
+
t = ExpressionType.LessThan;
CTest<byte> (t, false, 5, 1);
CTest<byte> (t, false, 10, 10);
CTest<double> (t, true, 1.0, 1.0);
CTest<double> (t, true, 1.0, 1.0);
-
+
t = ExpressionType.LessThanOrEqual;
CTest<byte> (t, false, 5, 1);
CTest<byte> (t, true, 10, 10);
CTest<ulong> (t, false, Int64.MaxValue, 0);
CTest<double> (t, true, 1.0, 1.0);
CTest<double> (t, true, 1.0, 1.0);
-
+
}
}
public void OrTest ()
{
Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a | b;
-
+
Func<bool,bool,bool> c = e.Compile ();
-
+
Assert.AreEqual (true, c (true, true), "o1");
Assert.AreEqual (true, c (true, false), "o2");
Assert.AreEqual (true, c (false, true), "o3");
Assert.AreEqual (false, c (false, false), "o4");
}
-
+
[Test]
public void OrNullableTest ()
{
Expression<Func<bool?, bool?, bool?>> e = (bool? a, bool? b) => a | b;
-
+
Func<bool?,bool?,bool?> c = e.Compile ();
-
+
Assert.AreEqual (true, c (true, true), "o1");
Assert.AreEqual (true, c (true, false), "o2");
Assert.AreEqual (true, c (false, true), "o3");
Assert.AreEqual (false, c (false, false), "o4");
-
+
Assert.AreEqual (true, c (true, null), "o5");
Assert.AreEqual (null, c (false, null), "o6");
Assert.AreEqual (null, c (null, false), "o7");
Assert.AreEqual (true, c (true, null), "o8");
Assert.AreEqual (null, c (null, null), "o9");
}
-
+
}
}
return null;
}
}
-
+
[Test]
[ExpectedException(typeof(ArgumentException))]
public void MethodInfoReturnType ()
ParameterExpression a = Expression.Parameter(typeof(double), "a");
ParameterExpression b = Expression.Parameter(typeof(double), "b");
BinaryExpression p = Expression.Power (a, b);
-
+
Expression<Func<double,double,double>> pexpr = Expression.Lambda<Func<double,double,double>> (p, new ParameterExpression [] { a, b });
Func<double,double,double> compiled = pexpr.Compile ();
Assert.AreEqual (1, compiled (1, 10));
}
throw new Exception (String.Format ("SubtractChecked should have thrown for the creation of a tree with {0} operands", v1.GetType ()));
}
-
+
[Test]
public void TestOverflows ()
{
MustOverflow<long> (Int64.MaxValue, -1);
MustOverflow<ushort> (UInt16.MinValue, 1);
-
+
// unsigned values use Sub_Ovf_Un, check that too:
MustOverflow<ulong> (0, 1);
MustOverflow<uint> (0, 1);
// Sounds very odd, should file a bug with MS.
MustNotOverflow<short> (Int16.MinValue, 1);
}
-
+
//
// These should not overflow
//