Author: marek
Date: 2008-01-24 14:58:10 -0500 (Thu, 24 Jan 2008)
New Revision: 93831
Modified:
trunk/mcs/tests/gtest-etree-01.cs
Log:
More tests.
Modified: trunk/mcs/tests/gtest-etree-01.cs
===================================================================
--- trunk/mcs/tests/gtest-etree-01.cs 2008-01-24 19:57:00 UTC (rev 93830)
+++ trunk/mcs/tests/gtest-etree-01.cs 2008-01-24 19:58:10 UTC (rev 93831)
@@ -23,7 +23,7 @@
}
/*
-public struct MyTypeMyType<T>
+public struct MyType<T>
{
T value;
@@ -39,6 +39,56 @@
}
*/
+public struct MyType
+{
+ int value;
+
+ public MyType (int value)
+ {
+ this.value = value;
+ }
+
+ public static implicit operator int (MyType o)
+ {
+ return o.value;
+ }
+
+ public static bool operator true (MyType a)
+ {
+ return a.value == a;
+ }
+
+ public static bool operator false (MyType a)
+ {
+ return a.value != a;
+ }
+
+ //public static implicit operator MyType (int i)
+ //{
+ // return new MyType (i);
+ //}
+
+ public static MyType operator +(MyType a, MyType b)
+ {
+ return new MyType (a.value + b.value);
+ }
+
+ public static MyType operator &(MyType a, MyType b)
+ {
+ return new MyType (a.value & b.value);
+ }
+
+ public static MyType operator | (MyType a, MyType b)
+ {
+ return new MyType (a.value | b.value);
+ }
+
+ public override string ToString ()
+ {
+ return value.ToString ();
+ }
+}
+
class Tester
{
static void AssertNodeType (LambdaExpression e, ExpressionType et)
@@ -53,7 +103,7 @@
throw new ApplicationException (expected + " != " +
value);
}
- static void Assert<T> (T[] expected, T[] value)
+ static void Assert<T> (T [] expected, T [] value)
{
if (expected.Length != value.Length)
throw new ApplicationException ("Array length does not
match " + expected.Length + " != " + value.Length);
@@ -73,15 +123,28 @@
Expression<Func<int?, int?, int?>> e2 = (a, b) => a + b;
AssertNodeType (e2, ExpressionType.Add);
Assert (null, e2.Compile ().Invoke (null, 3));
+
+ Expression<Func<MyType, MyType, MyType>> e3 = (MyType a, MyType
b) => a + b;
+ AssertNodeType (e3, ExpressionType.Add);
+// FIXME: type inference
+// Assert (10, e3.Compile ().Invoke (new MyType (-20), new MyType
(30)));
}
void AddCheckedTest ()
{
checked {
- Expression<Func<int, int, int>> e = (int a, int b) => a
+ b;
+ Expression<Func<int, int, int>> e = (int a, int b) => a + b;
+ AssertNodeType (e, ExpressionType.AddChecked);
+ Assert (50, e.Compile ().Invoke (20, 30));
- AssertNodeType (e, ExpressionType.AddChecked);
- Assert (-10, e.Compile ().Invoke (20, -30));
+ Expression<Func<int?, int?, int?>> e2 = (a, b) => a + b;
+ AssertNodeType (e2, ExpressionType.AddChecked);
+ Assert (null, e2.Compile ().Invoke (null, 3));
+
+ Expression<Func<MyType, MyType, MyType>> e3 = (MyType a, MyType
b) => a + b;
+ AssertNodeType (e3, ExpressionType.Add);
+// FIXME: type inference
+// Assert (10, e3.Compile ().Invoke (new MyType (-20), new MyType
(30)));
}
}
@@ -90,44 +153,67 @@
Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a &
b;
AssertNodeType (e, ExpressionType.And);
- Func<bool,bool,bool> c = e.Compile ();
-
- Assert (true, c (true, true));
+ Func<bool, bool, bool> c = e.Compile ();
+
+ Assert (true, c (true, true));
Assert (false, c (true, false));
Assert (false, c (false, true));
Assert (false, c (false, false));
+
+ Expression<Func<MyType, MyType, MyType>> e2 = (MyType a, MyType
b) => a & b;
+
+ AssertNodeType (e2, ExpressionType.And);
+ var c2 = e2.Compile ();
+
+ Assert (new MyType (0), c2 (new MyType (0), new MyType (1)));
+ Assert (new MyType (1), c2 (new MyType (0xFF), new MyType
(0x01)));
}
-
+
void AndNullableTest ()
{
Expression<Func<bool?, bool?, bool?>> e = (bool? a, bool? b) =>
a & b;
AssertNodeType (e, ExpressionType.And);
- Func<bool?,bool?,bool?> c = e.Compile ();
-
- Assert (true, c (true, true));
+ Func<bool?, bool?, bool?> c = e.Compile ();
+
+ Assert (true, c (true, true));
Assert (false, c (true, false));
Assert (false, c (false, true));
Assert (false, c (false, false));
- Assert (null, c (true, null));
+ Assert (null, c (true, null));
Assert (false, c (false, null));
Assert (false, c (null, false));
- Assert (null, c (true, null));
+ Assert (null, c (true, null));
Assert (null, c (null, null));
+
+ Expression<Func<MyType?, MyType?, MyType?>> e2 = (MyType? a,
MyType? b) => a & b;
+
+ AssertNodeType (e2, ExpressionType.And);
+ var c2 = e2.Compile ();
+
+ Assert (new MyType (0), c2 (new MyType (0), new MyType (1)));
+ Assert (new MyType (1), c2 (new MyType (0xFF), new MyType
(0x01)));
+ Assert (null, c2 (new MyType (0xFF), null));
}
-
+
void AndAlsoTest ()
{
Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a &&
b;
AssertNodeType (e, ExpressionType.AndAlso);
Assert (false, e.Compile ().Invoke (true, false));
+
+ Expression<Func<MyType, MyType, MyType>> e2 = (MyType a, MyType
b) => a && b;
+
+ AssertNodeType (e2, ExpressionType.AndAlso);
+ Assert (new MyType (64), e2.Compile ().Invoke (new MyType (64),
new MyType (64)));
+ Assert (new MyType (0), e2.Compile ().Invoke (new MyType (32),
new MyType (64)));
}
void ArrayIndexTest ()
{
- Expression<Func<string[], long, string>> e = (string[] a, long
i) => a [i];
+ Expression<Func<string [], long, string>> e = (string [] a,
long i) => a [i];
AssertNodeType (e, ExpressionType.ArrayIndex);
Assert ("b", e.Compile ().Invoke (new string [] { "a", "b", "c"
}, 1));
@@ -137,17 +223,17 @@
Expression<Func<object [,], int, int, object>> e3 = (object [,]
a, int i, int j) => a [i, j];
AssertNodeType (e3, ExpressionType.Call);
-
+
Assert ("z", e3.Compile ().Invoke (
new object [,] { { 1, 2 }, { "x", "z" } }, 1, 1));
- Expression<Func<decimal [][], byte, decimal>> e4 = (decimal
[][] a, byte b) => a [b][1];
+ Expression<Func<decimal [] [], byte, decimal>> e4 = (decimal []
[] a, byte b) => a [b] [1];
AssertNodeType (e4, ExpressionType.ArrayIndex);
decimal [] [] array = { new decimal [] { 1, 9 }, new decimal []
{ 10, 90 } };
Assert (90, e4.Compile ().Invoke (array, 1));
}
-
+
void ArrayLengthTest ()
{
int o = new int [0].Length;
@@ -161,15 +247,15 @@
//Expression<Func<string [,], int>> e2 = (string [,] a) =>
a.Length;
//AssertNodeType (e2, ExpressionType.MemberAccess);
//Assert (0, e2.Compile ().Invoke (new string [0, 0]));
- }
-
+ }
+
void CallTest ()
{
Expression<Func<int, int>> e = (int a) => Math.Max (a, 5);
AssertNodeType (e, ExpressionType.Call);
Assert (5, e.Compile ().Invoke (2));
Assert (9, e.Compile ().Invoke (9));
-
+
Expression<Func<string, string>> e2 = (string a) =>
InstanceMethod (a);
AssertNodeType (e2, ExpressionType.Call);
Assert ("abc", e2.Compile ().Invoke ("abc"));
@@ -192,15 +278,14 @@
Expression<Func<uint?, uint>> e = (uint? a) => a ?? 99;
AssertNodeType (e, ExpressionType.Coalesce);
var r = e.Compile ();
- Assert ((uint)5, r.Invoke (5));
- Assert ((uint)99, r.Invoke (null));
+ Assert ((uint) 5, r.Invoke (5));
+ Assert ((uint) 99, r.Invoke (null));
-// TODO: implement
-// Expression<Func<MyType<bool>?, bool>> e2 = (MyType<bool>? a) =>
a ?? false;
-// AssertNodeType (e2, ExpressionType.Coalesce);
-// var r2 = e2.Compile ();
-// Assert (true, r2.Invoke (new MyType<bool> (true)));
-// Assert (false, r2.Invoke (null));
+ Expression<Func<MyType?, int>> e2 = (MyType? a) => a ?? -3;
+ AssertNodeType (e2, ExpressionType.Coalesce);
+ var r2 = e2.Compile ();
+ Assert (2, r2.Invoke (new MyType (2)));
+ Assert (-3, r2.Invoke (null));
}
void ConditionTest ()
@@ -211,14 +296,14 @@
Assert (3, r.Invoke (true, 3, 999999));
Assert (999999, r.Invoke (false, 3, 999999));
- Expression<Func<int, decimal, decimal?>> e2 = (int a, decimal
d) => (a > 0 ? d : a < 0 ? -d : (decimal?)null);
+ Expression<Func<int, decimal, decimal?>> e2 = (int a, decimal
d) => (a > 0 ? d : a < 0 ? -d : (decimal?) null);
AssertNodeType (e2, ExpressionType.Conditional);
var r2 = e2.Compile ();
Assert (null, r2.Invoke (0, 10));
Assert (50, r2.Invoke (1, 50));
Assert (30, r2.Invoke (-7, -30));
- Expression<Func<bool?, int?>> e3 = (bool? a) => ((bool)a ? 3 :
-2);
+ Expression<Func<bool?, int?>> e3 = (bool? a) => ((bool) a ? 3 :
-2);
AssertNodeType (e3, ExpressionType.Convert);
var r3 = e3.Compile ();
Assert (3, r3.Invoke (true));
@@ -231,23 +316,63 @@
Assert (4, r4.Invoke (new InverseLogicalOperator (false), 3,
4));
}
+ public void ConvertTest ()
+ {
+ Expression<Func<int, byte>> e = (int a) => ((byte) a);
+ AssertNodeType (e, ExpressionType.Convert);
+ Assert (100, e.Compile ().Invoke (100));
+
+ Expression<Func<long, ushort>> e2 = (long a) => ((ushort) a);
+ AssertNodeType (e2, ExpressionType.Convert);
+ Assert (100, e2.Compile ().Invoke (100));
+
+ Expression<Func<float?, float>> e3 = (float? a) => ((float) a);
+ AssertNodeType (e3, ExpressionType.Convert);
+ Assert (-0.456f, e3.Compile ().Invoke (-0.456f));
+
+ Expression<Func<MyType, int>> e4 = (MyType a) => (a);
+ AssertNodeType (e4, ExpressionType.Convert);
+ Assert (-9, e4.Compile ().Invoke (new MyType (-9)));
+ }
+
+ public void ConvertCheckedTest ()
+ {
+ Expression<Func<int, byte>> e = (int a) => checked((byte) a);
+ AssertNodeType (e, ExpressionType.ConvertChecked);
+ Assert (100, e.Compile ().Invoke (100));
+
+ checked {
+ Expression<Func<long, ushort>> e2 = (long a) =>
unchecked((ushort) a);
+ AssertNodeType (e2, ExpressionType.Convert);
+ Assert (100, e2.Compile ().Invoke (100));
+
+ Expression<Func<float?, float>> e3 = (float? a) =>
((float) a);
+ AssertNodeType (e3, ExpressionType.ConvertChecked);
+ Assert (-0.456f, e3.Compile ().Invoke (-0.456f));
+
+ Expression<Func<MyType, int>> e4 = (MyType a) => (a);
+ AssertNodeType (e4, ExpressionType.Convert);
+ Assert (-9, e4.Compile ().Invoke (new MyType (-9)));
+ }
+ }
+
void NewArrayInitTest ()
{
- Expression<Func<int[]>> e = () => new int[0];
+ Expression<Func<int []>> e = () => new int [0];
AssertNodeType (e, ExpressionType.NewArrayInit);
- Assert (new int[0], e.Compile ().Invoke ());
-
- e = () => new int [] {};
+ Assert (new int [0], e.Compile ().Invoke ());
+
+ e = () => new int [] { };
AssertNodeType (e, ExpressionType.NewArrayInit);
- Assert (new int [0], e.Compile ().Invoke ());
+ Assert (new int [0], e.Compile ().Invoke ());
Expression<Func<ushort, ulong? []>> e2 = (ushort a) => new
ulong? [] { a };
AssertNodeType (e2, ExpressionType.NewArrayInit);
Assert (new ulong? [1] { ushort.MaxValue }, e2.Compile
().Invoke (ushort.MaxValue));
Expression<Func<char [] []>> e3 = () => new char [] [] { new
char [] { 'a' } };
- AssertNodeType (e3, ExpressionType.NewArrayInit);
- Assert (new char[] { 'a' }, e3.Compile ().Invoke ()[0]);
+ AssertNodeType (e3, ExpressionType.NewArrayInit);
+ Assert (new char [] { 'a' }, e3.Compile ().Invoke () [0]);
}
void OrTest ()
@@ -261,6 +386,11 @@
Assert (true, c (true, false));
Assert (true, c (false, true));
Assert (false, c (false, false));
+
+ Expression<Func<MyType, MyType, MyType>> e2 = (MyType a, MyType
b) => a | b;
+ AssertNodeType (e2, ExpressionType.Or);
+ var c2 = e2.Compile ();
+ Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
}
void OrNullableTest ()
@@ -280,8 +410,14 @@
Assert (null, c (null, false));
Assert (true, c (true, null));
Assert (null, c (null, null));
+
+ Expression<Func<MyType?, MyType?, MyType?>> e2 = (MyType? a,
MyType? b) => a | b;
+ AssertNodeType (e2, ExpressionType.Or);
+ var c2 = e2.Compile ();
+ Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
+ Assert (null, c2 (new MyType (1), null));
}
-
+
//
// Test helpers
//
@@ -289,8 +425,8 @@
{
return arg;
}
-
- object InstanceParamsMethod (int index, params object[] args)
+
+ object InstanceParamsMethod (int index, params object [] args)
{
if (args == null)
return "<null>";
@@ -298,11 +434,11 @@
return "<empty>";
return args [index];
}
-
+
T GenericMethod<T> (T t)
{
return t;
- }
+ }
public static int Main ()
@@ -318,14 +454,13 @@
e.CallTest ();
e.CoalesceTest ();
e.ConditionTest ();
+ e.ConvertTest ();
+ e.ConvertCheckedTest ();
e.NewArrayInitTest ();
e.OrTest ();
e.OrNullableTest ();
-
+
return 0;
}
}
-
-
-
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches