1 // Compiler options: -unsafe
4 using System
.Collections
.Generic
;
6 using System
.Linq
.Expressions
;
7 using System
.Reflection
;
9 public struct InverseLogicalOperator
12 public InverseLogicalOperator (bool value)
17 public static bool operator true (InverseLogicalOperator u
)
22 public static bool operator false (InverseLogicalOperator u
)
28 /* TODO: Add tests for every numeric expression where a type has only 1 implicit
30 public struct MyType<T>
34 public MyType (T value)
39 public static implicit operator T (MyType<T> o)
47 // TODO: Create a clone which uses +(MyType, int) pattern and an implicit conversion
48 // is required to do the user-conversion
54 public MyType (int value) : this ()
59 public short ShortProp { get; set; }
61 public override int GetHashCode ()
63 throw new NotImplementedException ();
66 public static implicit operator int (MyType o
)
71 public static bool operator true (MyType a
)
76 public static bool operator false (MyType a
)
81 public static MyType
operator + (MyType a
, MyType b
)
83 return new MyType (a
.value + b
.value);
86 public static MyType
operator - (MyType a
, MyType b
)
88 return new MyType (a
.value - b
.value);
91 public static MyType
operator / (MyType a
, MyType b
)
93 return new MyType (a
.value / b
.value);
96 public static MyType
operator * (MyType a
, MyType b
)
98 return new MyType (a
.value * b
.value);
101 public static MyType
operator % (MyType a
, MyType b
)
103 return new MyType (a
.value % b
.value);
106 public static MyType
operator &(MyType a
, MyType b
)
108 return new MyType (a
.value & b
.value);
111 public static MyType
operator | (MyType a
, MyType b
)
113 return new MyType (a
.value | b
.value);
116 public static MyType
operator ^
(MyType a
, MyType b
)
118 return new MyType (a
.value ^ b
.value);
121 public static bool operator == (MyType a
, MyType b
)
123 return a
.value == b
.value;
126 public static bool operator != (MyType a
, MyType b
)
128 return a
.value != b
.value;
131 public static bool operator > (MyType a
, MyType b
)
133 return a
.value > b
.value;
136 public static bool operator < (MyType a
, MyType b
)
138 return a
.value < b
.value;
141 public static bool operator >= (MyType a
, MyType b
)
143 return a
.value >= b
.value;
146 public static bool operator <= (MyType a
, MyType b
)
148 return a
.value <= b
.value;
151 public static bool operator ! (MyType a
)
156 public static int operator >> (MyType a
, int b
)
161 public static int operator << (MyType a
, int b
)
166 public static MyType
operator - (MyType a
)
168 return new MyType (-a
.value);
171 public static MyType
operator + (MyType a
)
173 return new MyType (+a
.value);
176 public override string ToString ()
178 return value.ToString ();
187 public MyTypeExplicit (int value)
192 public static explicit operator int (MyTypeExplicit m
)
198 struct MyTypeImplicitOnly
202 public MyTypeImplicitOnly (short b
)
207 public static implicit operator short (MyTypeImplicitOnly m
)
213 class MemberAccessData
215 public bool BoolValue
;
216 public static decimal DecimalValue
= decimal.MinValue
;
217 public volatile uint VolatileValue
;
218 public string [] StringValues
;
219 public List
<string> ListValues
;
221 event Func
<bool> EventField
;
222 public Expression
<Func
<Func
<bool>>> GetEvent ()
224 return () => EventField
;
228 public MyType MyTypeProperty
{
237 public static string StaticProperty
{
250 enum MyEnumUlong
: ulong
264 public NewTest (params T
[] t
)
269 public override int GetHashCode ()
271 return base.GetHashCode ();
274 public override bool Equals (object obj
)
276 NewTest
<T
> obj_t
= obj
as NewTest
<T
>;
280 for (int i
= 0; i
< t
.Length
; ++i
) {
281 if (!t
[i
].Equals (obj_t
.t
[i
]))
291 public int this [int i
] { get { return i; }
set { }
}
292 public string this [params string[] i
] { get { return string.Concat (i); }
}
296 // TODO: Add more nullable tests, follow AddTest pattern.
300 delegate void EmptyDelegate ();
301 delegate int IntDelegate ();
303 static void AssertNodeType (LambdaExpression e
, ExpressionType et
)
305 if (e
.Body
.NodeType
!= et
)
306 throw new ApplicationException (e
.Body
.NodeType
+ " != " + et
);
309 static void Assert
<T
> (T expected
, T
value)
311 Assert (expected
, value, null);
314 static void Assert
<T
> (T expected
, T
value, string name
)
316 if (!EqualityComparer
<T
>.Default
.Equals (expected
, value)) {
317 if (!string.IsNullOrEmpty (name
))
319 throw new ApplicationException (name
+ expected
+ " != " + value);
323 static void Assert
<T
> (T
[] expected
, T
[] value)
325 if (expected
== null) {
327 throw new ApplicationException ("Both arrays expected to be null");
331 if (expected
.Length
!= value.Length
)
332 throw new ApplicationException ("Array length does not match " + expected
.Length
+ " != " + value.Length
);
334 for (int i
= 0; i
< expected
.Length
; ++i
) {
335 if (!EqualityComparer
<T
>.Default
.Equals (expected
[i
], value [i
]))
336 throw new ApplicationException ("Index " + i
+ ": " + expected
[i
] + " != " + value [i
]);
340 #pragma warning disable 169
344 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
+ b
;
345 AssertNodeType (e
, ExpressionType
.Add
);
346 Assert (50, e
.Compile ().Invoke (20, 30));
351 Expression
<Func
<int?, int?, int?>> e2
= (a
, b
) => a
+ b
;
352 AssertNodeType (e2
, ExpressionType
.Add
);
353 Assert (null, e2
.Compile ().Invoke (null, 3));
358 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
+ b
;
359 AssertNodeType (e3
, ExpressionType
.Add
);
360 Assert (10, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
365 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
+ b
;
366 AssertNodeType (e4
, ExpressionType
.Add
);
367 Assert (new MyType (10), e4
.Compile ().Invoke (new MyType (-20), new MyType (30)));
368 Assert (null, e4
.Compile ().Invoke (null, new MyType (30)));
373 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
+ b
;
374 AssertNodeType (e5
, ExpressionType
.Add
);
375 Assert (31, e5
.Compile ().Invoke (1, new MyType (30)));
380 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
+ b
;
381 AssertNodeType (e6
, ExpressionType
.Add
);
382 Assert (-1, e6
.Compile ().Invoke (-31, new MyType (30)));
387 Expression
<Func
<MyEnum
, byte, MyEnum
>> e7
= (a
, b
) => a
+ b
;
388 AssertNodeType (e7
, ExpressionType
.Convert
);
389 Assert (MyEnum
.Value_2
, e7
.Compile ().Invoke (MyEnum
.Value_1
, 1));
394 // CSC BUG: probably due to missing numeric promotion
395 Expression
<Func
<MyEnum
?, byte?, MyEnum
?>> e8
= (a
, b
) => a
+ b
;
396 AssertNodeType (e8
, ExpressionType
.Convert
);
397 Assert
<MyEnum
?> (0, e8
.Compile ().Invoke (MyEnum
.Value_1
, 255));
398 Assert (null, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
399 Assert (null, e8
.Compile ().Invoke (null, null));
404 Expression
<Func
<byte, MyEnum
, MyEnum
>> e9
= (a
, b
) => a
+ b
;
405 AssertNodeType (e9
, ExpressionType
.Convert
);
406 Assert (MyEnum
.Value_2
, e9
.Compile ().Invoke (1, MyEnum
.Value_1
));
409 void AddCheckedTest ()
412 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
+ b
;
413 AssertNodeType (e
, ExpressionType
.AddChecked
);
414 Assert (50, e
.Compile ().Invoke (20, 30));
418 void AddCheckedTest_2 ()
421 Expression
<Func
<int?, int?, int?>> e2
= (a
, b
) => a
+ b
;
422 AssertNodeType (e2
, ExpressionType
.AddChecked
);
423 Assert (null, e2
.Compile ().Invoke (null, 3));
427 void AddCheckedTest_3 ()
430 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
+ b
;
431 AssertNodeType (e3
, ExpressionType
.Add
);
432 Assert (10, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
436 void AddStringTest ()
438 Expression
<Func
<string, string>> e6
= (a
) => 1 + a
;
439 AssertNodeType (e6
, ExpressionType
.Add
);
440 Assert ("1to", e6
.Compile ().Invoke ("to"));
443 void AddStringTest_2 ()
445 Expression
<Func
<object, string, string>> e7
= (object a
, string b
) => a
+ b
;
446 AssertNodeType (e7
, ExpressionType
.Add
);
447 Assert ("testme", e7
.Compile ().Invoke ("test", "me"));
448 Assert ("test", e7
.Compile ().Invoke ("test", null));
449 Assert ("", e7
.Compile ().Invoke (null, null));
452 void AddStringTest_3 ()
454 Expression
<Func
<string, int, string>> e8
= (a
, b
) => a
+ " " + "-" + "> " + b
;
455 AssertNodeType (e8
, ExpressionType
.Add
);
456 Assert ("test -> 2", e8
.Compile ().Invoke ("test", 2));
459 void AddStringTest_4 ()
461 Expression
<Func
<string, ushort?, string>> e9
= (a
, b
) => a
+ b
;
462 AssertNodeType (e9
, ExpressionType
.Add
);
463 Assert ("test2", e9
.Compile ().Invoke ("test", 2));
464 Assert ("test", e9
.Compile ().Invoke ("test", null));
469 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
& b
;
471 AssertNodeType (e
, ExpressionType
.And
);
472 Func
<bool, bool, bool> c
= e
.Compile ();
474 Assert (true, c (true, true));
475 Assert (false, c (true, false));
476 Assert (false, c (false, true));
477 Assert (false, c (false, false));
482 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
& b
;
484 AssertNodeType (e2
, ExpressionType
.And
);
485 var c2
= e2
.Compile ();
487 Assert (new MyType (0), c2 (new MyType (0), new MyType (1)));
488 Assert (new MyType (1), c2 (new MyType (0xFF), new MyType (0x01)));
493 Expression
<Func
<MyEnum
, MyEnum
, MyEnum
>> e3
= (a
, b
) => a
& b
;
494 AssertNodeType (e3
, ExpressionType
.Convert
);
495 Assert
<MyEnum
> (0, e3
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
496 Assert (MyEnum
.Value_2
, e3
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
501 Expression
<Func
<int, int>> e
= (a
) => a
& 0;
502 AssertNodeType (e
, ExpressionType
.And
);
503 var c
= e
.Compile ();
508 void AndNullableTest ()
510 Expression
<Func
<bool?, bool?, bool?>> e
= (bool? a
, bool? b
) => a
& b
;
512 AssertNodeType (e
, ExpressionType
.And
);
513 Func
<bool?, bool?, bool?> c
= e
.Compile ();
515 Assert (true, c (true, true));
516 Assert (false, c (true, false));
517 Assert (false, c (false, true));
518 Assert (false, c (false, false));
520 Assert (null, c (true, null));
521 Assert (false, c (false, null));
522 Assert (false, c (null, false));
523 Assert (null, c (true, null));
524 Assert (null, c (null, null));
527 void AndNullableTest_2 ()
529 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e2
= (MyType
? a
, MyType
? b
) => a
& b
;
531 AssertNodeType (e2
, ExpressionType
.And
);
532 var c2
= e2
.Compile ();
534 Assert (new MyType (0), c2 (new MyType (0), new MyType (1)));
535 Assert (new MyType (1), c2 (new MyType (0xFF), new MyType (0x01)));
536 Assert (null, c2 (new MyType (0xFF), null));
539 void AndNullableTest_3 ()
541 Expression
<Func
<MyEnum
?, MyEnum
?, MyEnum
?>> e3
= (a
, b
) => a
& b
;
542 AssertNodeType (e3
, ExpressionType
.Convert
);
543 Assert (null, e3
.Compile ().Invoke (null, MyEnum
.Value_2
));
544 Assert (MyEnum
.Value_2
, e3
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
549 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
&& b
;
550 AssertNodeType (e
, ExpressionType
.AndAlso
);
551 Assert (false, e
.Compile ().Invoke (true, false));
554 void AndAlsoTest_2 ()
556 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
&& b
;
557 AssertNodeType (e2
, ExpressionType
.AndAlso
);
558 Assert (new MyType (64), e2
.Compile ().Invoke (new MyType (64), new MyType (64)));
559 Assert (new MyType (0), e2
.Compile ().Invoke (new MyType (32), new MyType (64)));
562 void AndAlsoTest_3 ()
564 Expression
<Func
<bool, bool>> e3
= (bool a
) => a
&& true;
565 AssertNodeType (e3
, ExpressionType
.AndAlso
);
566 Assert (false, e3
.Compile ().Invoke (false));
567 Assert (true, e3
.Compile ().Invoke (true));
570 void ArrayIndexTest ()
572 Expression
<Func
<string [], long, string>> e
= (string [] a
, long i
) => a
[i
];
573 AssertNodeType (e
, ExpressionType
.ArrayIndex
);
574 Assert ("b", e
.Compile ().Invoke (new string [] { "a", "b", "c" }
, 1));
577 void ArrayIndexTest_2 ()
579 Expression
<Func
<string [], string>> e2
= (string [] a
) => a
[0];
580 AssertNodeType (e2
, ExpressionType
.ArrayIndex
);
581 Assert ("a", e2
.Compile ().Invoke (new string [] { "a", "b" }
));
584 void ArrayIndexTest_3 ()
586 Expression
<Func
<object [,], int, int, object>> e3
= (object [,] a
, int i
, int j
) => a
[i
, j
];
587 AssertNodeType (e3
, ExpressionType
.Call
);
589 Assert ("z", e3
.Compile ().Invoke (
590 new object [,] { { 1, 2 }
, { "x", "z" }
}, 1, 1));
593 void ArrayIndexTest_4 ()
595 Expression
<Func
<decimal [] [], byte, decimal>> e4
= (decimal [] [] a
, byte b
) => a
[b
] [1];
596 AssertNodeType (e4
, ExpressionType
.ArrayIndex
);
598 decimal [] [] array
= { new decimal [] { 1, 9 }
, new decimal [] { 10, 90 }
};
599 Assert (90, e4
.Compile ().Invoke (array
, 1));
602 void ArrayIndexTest_5 ()
604 Expression
<Func
<int>> e5
= () => (new int [1]) [0];
605 AssertNodeType (e5
, ExpressionType
.ArrayIndex
);
606 Assert (0, e5
.Compile ().Invoke ());
609 void ArrayIndexTest_6 ()
613 Expression
<Func
<int[], int>> e
= a
=> a
[max
];
614 AssertNodeType (e
, ExpressionType
.ArrayIndex
);
615 Assert (4, e
.Compile ().Invoke (new int [] { 1, 2, 3, 4, 5 }
));
618 void ArrayIndexTest_7 ()
620 const ulong max
= uint.MaxValue
;
622 Expression
<Func
<int[], int>> e
= a
=> a
[max
];
623 AssertNodeType (e
, ExpressionType
.ArrayIndex
);
626 e
.Compile ().Invoke (new int [0]);
627 throw new ApplicationException ("ArrayIndexTest_7");
628 } catch (System
.OverflowException
) {
629 // Check whether CheckedConversion was generated
633 void ArrayLengthTest ()
635 Expression
<Func
<double [], int>> e
= (double [] a
) => a
.Length
;
636 AssertNodeType (e
, ExpressionType
.ArrayLength
);
637 Assert (0, e
.Compile ().Invoke (new double [0]));
638 Assert (9, e
.Compile ().Invoke (new double [9]));
641 void ArrayLengthTest_2 ()
643 Expression
<Func
<string [,], int>> e2
= (string [,] a
) => a
.Length
;
644 AssertNodeType (e2
, ExpressionType
.MemberAccess
);
645 Assert (0, e2
.Compile ().Invoke (new string [0, 0]));
650 Expression
<Func
<int, int>> e
= (int a
) => Math
.Max (a
, 5);
651 AssertNodeType (e
, ExpressionType
.Call
);
652 Assert (5, e
.Compile ().Invoke (2));
653 Assert (9, e
.Compile ().Invoke (9));
658 Expression
<Func
<string, string>> e2
= (string a
) => InstanceMethod (a
);
659 AssertNodeType (e2
, ExpressionType
.Call
);
660 Assert ("abc", e2
.Compile ().Invoke ("abc"));
665 Expression
<Func
<int, string, int, object>> e3
= (int index
, string a
, int b
) => InstanceParamsMethod (index
, a
, b
);
666 AssertNodeType (e3
, ExpressionType
.Call
);
667 Assert
<object> (4, e3
.Compile ().Invoke (1, "a", 4));
672 Expression
<Func
<object>> e4
= () => InstanceParamsMethod (0);
673 AssertNodeType (e4
, ExpressionType
.Call
);
674 Assert
<object> ("<empty>", e4
.Compile ().Invoke ());
679 Expression
<Func
<int, int>> e5
= (int a
) => GenericMethod (a
);
680 AssertNodeType (e5
, ExpressionType
.Call
);
681 Assert (5, e5
.Compile ().Invoke (5));
686 Expression
<Action
> e6
= () => Console
.WriteLine ("call test");
687 AssertNodeType (e6
, ExpressionType
.Call
);
692 Expression
<Func
<Indexer
, int, int>> e7
= (a
, b
) => a
[b
];
693 AssertNodeType (e7
, ExpressionType
.Call
);
694 Assert (3, e7
.Compile ().Invoke (new Indexer (), 3));
699 Expression
<Func
<Indexer
, string, string, string, string>> e8
= (a
, b
, c
, d
) => a
[b
, c
, d
];
700 AssertNodeType (e8
, ExpressionType
.Call
);
701 Assert ("zyb", e8
.Compile ().Invoke (new Indexer (), "z", "y", "b"));
706 Expression
<Action
<int>> e9
= (a
) => RefMethod (ref a
);
707 AssertNodeType (e9
, ExpressionType
.Call
);
708 e9
.Compile ().Invoke (1);
713 Expression
<Func
<uint?, uint>> e
= (uint? a
) => a
?? 99;
714 AssertNodeType (e
, ExpressionType
.Coalesce
);
715 var r
= e
.Compile ();
716 Assert ((uint) 5, r
.Invoke (5));
717 Assert ((uint) 99, r
.Invoke (null));
720 void CoalesceTest_2 ()
722 Expression
<Func
<MyType
?, int>> e2
= (MyType
? a
) => a
?? -3;
723 AssertNodeType (e2
, ExpressionType
.Coalesce
);
724 var r2
= e2
.Compile ();
725 Assert (2, r2
.Invoke (new MyType (2)));
726 Assert (-3, r2
.Invoke (null));
729 void ConditionTest ()
731 Expression
<Func
<bool, byte, int, int>> e
= (bool a
, byte b
, int c
) => (a
? b
: c
);
732 AssertNodeType (e
, ExpressionType
.Conditional
);
733 var r
= e
.Compile ();
734 Assert (3, r
.Invoke (true, 3, 999999));
735 Assert (999999, r
.Invoke (false, 3, 999999));
738 void ConditionTest_2 ()
740 Expression
<Func
<int, decimal, decimal?>> e2
= (int a
, decimal d
) => (a
> 0 ? d
: a
< 0 ? -d
: (decimal?) null);
741 AssertNodeType (e2
, ExpressionType
.Conditional
);
742 var r2
= e2
.Compile ();
743 Assert (null, r2
.Invoke (0, 10));
744 Assert (50, r2
.Invoke (1, 50));
745 Assert (30, r2
.Invoke (-7, -30));
748 void ConditionTest_3 ()
750 Expression
<Func
<bool?, int?>> e3
= (bool? a
) => ((bool) a
? 3 : -2);
751 AssertNodeType (e3
, ExpressionType
.Convert
);
752 var r3
= e3
.Compile ();
753 Assert (3, r3
.Invoke (true));
754 Assert (-2, r3
.Invoke (false));
757 void ConditionTest_4 ()
759 Expression
<Func
<InverseLogicalOperator
, byte, byte, byte>> e4
= (InverseLogicalOperator a
, byte b
, byte c
) => (a
? b
: c
);
760 AssertNodeType (e4
, ExpressionType
.Conditional
);
761 var r4
= e4
.Compile ();
762 Assert (3, r4
.Invoke (new InverseLogicalOperator (true), 3, 4));
763 Assert (4, r4
.Invoke (new InverseLogicalOperator (false), 3, 4));
766 void ConditionTest_5 ()
769 Expression
<Func
<int>> e
= () => false ? 1 : 4;
770 AssertNodeType (e
, ExpressionType
.Conditional
);
771 var r
= e
.Compile ();
772 Assert (4, r
.Invoke ());
777 Expression
<Func
<int>> e1
= () => default (int);
778 AssertNodeType (e1
, ExpressionType
.Constant
);
779 Assert (0, e1
.Compile ().Invoke ());
782 void ConstantTest_2 ()
784 Expression
<Func
<int?>> e2
= () => default (int?);
785 AssertNodeType (e2
, ExpressionType
.Constant
);
786 Assert (null, e2
.Compile ().Invoke ());
789 void ConstantTest_3 ()
791 Expression
<Func
<Tester
>> e3
= () => default (Tester
);
792 AssertNodeType (e3
, ExpressionType
.Constant
);
793 Assert (null, e3
.Compile ().Invoke ());
796 void ConstantTest_4 ()
798 Expression
<Func
<object>> e4
= () => null;
799 AssertNodeType (e4
, ExpressionType
.Constant
);
800 Assert (null, e4
.Compile ().Invoke ());
803 void ConstantTest_5 ()
805 Expression
<Func
<int>> e5
= () => 8 / 4;
806 AssertNodeType (e5
, ExpressionType
.Constant
);
807 Assert (2, e5
.Compile ().Invoke ());
810 void ConstantTest_6 ()
812 Expression
<Func
<int>> e6
= () => 0xFFFFFF >> 0x40;
813 AssertNodeType (e6
, ExpressionType
.Constant
);
814 Assert (0xFFFFFF, e6
.Compile ().Invoke ());
817 void ConstantTest_7 ()
819 Expression
<Func
<object>> e7
= () => "Alleluia";
820 AssertNodeType (e7
, ExpressionType
.Constant
);
821 Assert ("Alleluia", e7
.Compile ().Invoke ());
824 void ConstantTest_8 ()
826 Expression
<Func
<Type
>> e8
= () => typeof (int);
827 AssertNodeType (e8
, ExpressionType
.Constant
);
828 Assert (typeof (int), e8
.Compile ().Invoke ());
831 void ConstantTest_9 ()
833 Expression
<Func
<Type
>> e9
= () => typeof (void);
834 AssertNodeType (e9
, ExpressionType
.Constant
);
835 Assert (typeof (void), e9
.Compile ().Invoke ());
838 void ConstantTest_10 ()
840 Expression
<Func
<Type
>> e10
= () => typeof (Func
<,>);
841 AssertNodeType (e10
, ExpressionType
.Constant
);
842 Assert (typeof (Func
<,>), e10
.Compile ().Invoke ());
845 void ConstantTest_11 ()
847 Expression
<Func
<MyEnum
>> e11
= () => MyEnum
.Value_2
;
848 AssertNodeType (e11
, ExpressionType
.Constant
);
849 Assert (MyEnum
.Value_2
, e11
.Compile ().Invoke ());
852 void ConstantTest_13 ()
854 Expression
<Func
<int>> e13
= () => sizeof (byte);
855 AssertNodeType (e13
, ExpressionType
.Constant
);
856 Assert (1, e13
.Compile ().Invoke ());
860 unsafe void ConstantTest_14 ()
862 Expression
<Func
<Type
>> e14
= () => typeof (bool*);
863 AssertNodeType (e14
, ExpressionType
.Constant
);
864 Assert (typeof (bool*), e14
.Compile ().Invoke ());
867 void ConstantTest_15 ()
869 Expression
<Func
<int?>> e15
= () => null;
870 AssertNodeType (e15
, ExpressionType
.Constant
);
871 Assert (null, e15
.Compile ().Invoke ());
876 Expression
<Func
<int, byte>> e
= (int a
) => ((byte) a
);
877 AssertNodeType (e
, ExpressionType
.Convert
);
878 Assert (100, e
.Compile ().Invoke (100));
881 void ConvertTest_2 ()
883 Expression
<Func
<long, ushort>> e2
= (long a
) => ((ushort) a
);
884 AssertNodeType (e2
, ExpressionType
.Convert
);
885 Assert (100, e2
.Compile ().Invoke (100));
888 void ConvertTest_3 ()
890 Expression
<Func
<float?, float>> e3
= (float? a
) => ((float) a
);
891 AssertNodeType (e3
, ExpressionType
.Convert
);
892 Assert (-0.456f
, e3
.Compile ().Invoke (-0.456f
));
895 void ConvertTest_4 ()
897 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => (a
);
898 AssertNodeType (e4
, ExpressionType
.Convert
);
899 Assert (-9, e4
.Compile ().Invoke (new MyType (-9)));
902 void ConvertTest_5 ()
904 Expression
<Func
<MyType
, MyType
, bool?>> e5
= (MyType a
, MyType b
) => a
== b
;
905 AssertNodeType (e5
, ExpressionType
.Convert
);
908 void ConvertTest_6 ()
910 Expression
<Func
<MyType
?, MyType
?, bool?>> e6
= (MyType
? a
, MyType
? b
) => a
== b
;
911 AssertNodeType (e6
, ExpressionType
.Convert
);
912 Assert (false, e6
.Compile ().Invoke (null, new MyType (-20)));
913 Assert (true, e6
.Compile ().Invoke (null, null));
914 Assert (true, e6
.Compile ().Invoke (new MyType (120), new MyType (120)));
917 void ConvertTest_7 ()
919 Expression
<Func
<MyTypeExplicit
, int?>> e7
= x
=> (int?)x
;
920 AssertNodeType (e7
, ExpressionType
.Convert
);
921 Assert (33, e7
.Compile ().Invoke (new MyTypeExplicit (33)));
924 void ConvertTest_8 ()
926 Expression
<Func
<int?, object>> e8
= x
=> (object)x
;
927 AssertNodeType (e8
, ExpressionType
.Convert
);
928 Assert (null, e8
.Compile ().Invoke (null));
929 Assert (-100, e8
.Compile ().Invoke (-100));
932 unsafe void ConvertTest_9 ()
934 int*[] p
= new int* [1];
935 Expression
<Func
<object>> e9
= () => (object)p
;
936 AssertNodeType (e9
, ExpressionType
.Convert
);
937 Assert (p
, e9
.Compile ().Invoke ());
940 void ConvertTest_10 ()
942 Expression
<Func
<Func
<int>, Delegate
>> e10
= (a
) => a
+ a
;
943 AssertNodeType (e10
, ExpressionType
.Convert
);
944 Assert (null, e10
.Compile ().Invoke (null));
945 Assert (new Func
<int> (TestInt
) + new Func
<int> (TestInt
), e10
.Compile ().Invoke (TestInt
));
948 void ConvertTest_11 ()
950 Expression
<Func
<Func
<int>, Delegate
>> e11
= (a
) => a
- a
;
951 AssertNodeType (e11
, ExpressionType
.Convert
);
952 Assert (null, e11
.Compile ().Invoke (null));
955 void ConvertTest_12 ()
957 Expression
<Func
<Func
<int>>> e12
= () => TestInt
;
958 AssertNodeType (e12
, ExpressionType
.Convert
);
959 Assert (29, e12
.Compile ().Invoke () ());
962 void ConvertTest_13 ()
964 Expression
<Func
<decimal, sbyte>> e13
= a
=> (sbyte)a
;
965 AssertNodeType (e13
, ExpressionType
.Convert
);
966 Assert (6, e13
.Compile ().Invoke (6));
969 void ConvertTest_14 ()
971 Expression
<Func
<long, decimal>> e14
= a
=> a
;
972 AssertNodeType (e14
, ExpressionType
.Convert
);
973 Assert (-66, e14
.Compile ().Invoke (-66));
976 void ConvertTest_15 ()
978 Expression
<Func
<ulong?, decimal?>> e15
= a
=> a
;
979 AssertNodeType (e15
, ExpressionType
.Convert
);
980 Assert (null, e15
.Compile ().Invoke (null));
981 Assert (9, e15
.Compile ().Invoke (9));
984 void ConvertCheckedTest ()
986 Expression
<Func
<int, byte>> e
= (int a
) => checked((byte) a
);
987 AssertNodeType (e
, ExpressionType
.ConvertChecked
);
988 Assert (100, e
.Compile ().Invoke (100));
991 void ConvertCheckedTest_2 ()
994 Expression
<Func
<long, ushort>> e2
= (long a
) => unchecked((ushort) a
);
995 AssertNodeType (e2
, ExpressionType
.Convert
);
996 Assert (100, e2
.Compile ().Invoke (100));
1000 void ConvertCheckedTest_3 ()
1003 Expression
<Func
<float?, float>> e3
= (float? a
) => ((float) a
);
1004 AssertNodeType (e3
, ExpressionType
.ConvertChecked
);
1005 Assert (-0.456f
, e3
.Compile ().Invoke (-0.456f
));
1009 void ConvertCheckedTest_4 ()
1012 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => (a
);
1013 AssertNodeType (e4
, ExpressionType
.Convert
);
1014 Assert (-9, e4
.Compile ().Invoke (new MyType (-9)));
1020 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
/ b
;
1021 AssertNodeType (e
, ExpressionType
.Divide
);
1022 Assert (2, e
.Compile ().Invoke (60, 30));
1025 void DivideTest_2 ()
1027 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
/ b
;
1028 AssertNodeType (e2
, ExpressionType
.Divide
);
1029 Assert (null, e2
.Compile ().Invoke (null, 3));
1030 Assert (1.5, e2
.Compile ().Invoke (3, 2));
1033 void DivideTest_3 ()
1035 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
/ b
;
1036 AssertNodeType (e3
, ExpressionType
.Divide
);
1037 Assert (1, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1040 void DivideTest_4 ()
1042 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
/ b
;
1043 AssertNodeType (e4
, ExpressionType
.Divide
);
1044 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1045 Assert (new MyType (-6), e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1048 void DivideTest_5 ()
1050 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
/ b
;
1051 AssertNodeType (e5
, ExpressionType
.Divide
);
1052 Assert (50, e5
.Compile ().Invoke (100, new MyType (2)));
1055 void DivideTest_6 ()
1057 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
/ b
;
1058 AssertNodeType (e6
, ExpressionType
.Divide
);
1059 Assert (50, e6
.Compile ().Invoke (100, new MyType (2)));
1060 Assert (null, e6
.Compile ().Invoke (20, null));
1065 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
== b
;
1066 AssertNodeType (e
, ExpressionType
.Equal
);
1067 Assert (false, e
.Compile ().Invoke (60, 30));
1068 Assert (true, e
.Compile ().Invoke (-1, -1));
1073 Expression
<Func
<double?, double?, bool>> e2
= (a
, b
) => a
== b
;
1074 AssertNodeType (e2
, ExpressionType
.Equal
);
1075 Assert (true, e2
.Compile ().Invoke (3, 3));
1076 Assert (false, e2
.Compile ().Invoke (3, 2));
1081 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
== b
;
1082 AssertNodeType (e3
, ExpressionType
.Equal
);
1083 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1088 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
== b
;
1089 AssertNodeType (e4
, ExpressionType
.Equal
);
1090 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1091 Assert (true, e4
.Compile ().Invoke (null, null));
1092 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (120)));
1097 Expression
<Func
<bool?, bool?, bool>> e5
= (bool? a
, bool? b
) => a
== b
;
1098 AssertNodeType (e5
, ExpressionType
.Equal
);
1099 Assert (false, e5
.Compile ().Invoke (true, null));
1100 Assert (true, e5
.Compile ().Invoke (null, null));
1101 Assert (true, e5
.Compile ().Invoke (false, false));
1106 Expression
<Func
<bool, bool>> e6
= (bool a
) => a
== null;
1107 AssertNodeType (e6
, ExpressionType
.Equal
);
1108 Assert (false, e6
.Compile ().Invoke (true));
1109 Assert (false, e6
.Compile ().Invoke (false));
1114 Expression
<Func
<string, string, bool>> e7
= (string a
, string b
) => a
== b
;
1115 AssertNodeType (e7
, ExpressionType
.Equal
);
1116 Assert (true, e7
.Compile ().Invoke (null, null));
1117 Assert (false, e7
.Compile ().Invoke ("a", "A"));
1118 Assert (true, e7
.Compile ().Invoke ("a", "a"));
1123 Expression
<Func
<object, bool>> e8
= (object a
) => null == a
;
1124 AssertNodeType (e8
, ExpressionType
.Equal
);
1125 Assert (true, e8
.Compile ().Invoke (null));
1126 Assert (false, e8
.Compile ().Invoke ("a"));
1127 Assert (false, e8
.Compile ().Invoke (this));
1132 Expression
<Func
<MyEnum
, MyEnum
, bool>> e9
= (a
, b
) => a
== b
;
1133 AssertNodeType (e9
, ExpressionType
.Equal
);
1134 Assert (false, e9
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
1135 Assert (true, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1138 void EqualTest_10 ()
1140 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e10
= (a
, b
) => a
== b
;
1141 AssertNodeType (e10
, ExpressionType
.Equal
);
1142 Assert (false, e10
.Compile ().Invoke (MyEnum
.Value_1
, null));
1143 Assert (true, e10
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1146 void EqualTest_11 ()
1148 Expression
<Func
<MyEnum
?, bool>> e11
= (a
) => a
== null;
1149 AssertNodeType (e11
, ExpressionType
.Equal
);
1150 Assert (false, e11
.Compile ().Invoke (MyEnum
.Value_1
));
1151 Assert (true, e11
.Compile ().Invoke (null));
1154 void EqualTest_12 ()
1156 Expression
<Func
<MyEnumUlong
, bool>> e12
= (a
) => a
== 0;
1157 AssertNodeType (e12
, ExpressionType
.Equal
);
1158 Assert (false, e12
.Compile ().Invoke (MyEnumUlong
.Value_1
));
1159 Assert (true, e12
.Compile ().Invoke (0));
1162 void EqualTest_13 ()
1164 Expression
<Func
<MyEnum
, bool>> e13
= (a
) => a
== MyEnum
.Value_2
;
1165 AssertNodeType (e13
, ExpressionType
.Equal
);
1166 Assert (true, e13
.Compile ().Invoke (MyEnum
.Value_2
));
1167 Assert (false, e13
.Compile ().Invoke (0));
1170 void EqualTest_14 ()
1172 Expression
<Func
<MyEnum
, bool>> e
= (a
) => a
== null;
1173 AssertNodeType (e
, ExpressionType
.Equal
);
1174 Assert (false, e
.Compile ().Invoke (MyEnum
.Value_1
));
1177 void EqualTest_15 ()
1179 Expression
<Func
<int?, uint, bool>> e
= (a
, b
) => a
== b
;
1180 AssertNodeType (e
, ExpressionType
.Equal
);
1181 Assert (false, e
.Compile ().Invoke (null, 0));
1182 Assert (true, e
.Compile ().Invoke (4, 4));
1185 void EqualTestDelegate ()
1187 Expression
<Func
<Delegate
, Delegate
, bool>> e1
= (a
, b
) => a
== b
;
1188 AssertNodeType (e1
, ExpressionType
.Equal
);
1189 Assert (true, e1
.Compile ().Invoke (null, null));
1192 void EqualTestDelegate_2 ()
1194 EmptyDelegate ed
= delegate () {};
1196 Expression
<Func
<EmptyDelegate
, EmptyDelegate
, bool>> e2
= (a
, b
) => a
== b
;
1197 AssertNodeType (e2
, ExpressionType
.Equal
);
1198 Assert (false, e2
.Compile ().Invoke (delegate () {}, null));
1199 Assert (false, e2
.Compile ().Invoke (delegate () {}, delegate {}
));
1200 Assert (false, e2
.Compile ().Invoke (ed
, delegate {}));
1201 Assert (true, e2
.Compile ().Invoke (ed
, ed
));
1204 void ExclusiveOrTest ()
1206 Expression
<Func
<int, short, int>> e
= (int a
, short b
) => a ^ b
;
1207 AssertNodeType (e
, ExpressionType
.ExclusiveOr
);
1208 Assert (34, e
.Compile ().Invoke (60, 30));
1211 void ExclusiveOrTest_2 ()
1213 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a ^ b
;
1214 AssertNodeType (e2
, ExpressionType
.ExclusiveOr
);
1215 Assert (null, e2
.Compile ().Invoke (null, 3));
1216 Assert (1, e2
.Compile ().Invoke (3, 2));
1219 void ExclusiveOrTest_3 ()
1221 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a ^ b
;
1222 AssertNodeType (e3
, ExpressionType
.ExclusiveOr
);
1223 Assert (0, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1226 void ExclusiveOrTest_4 ()
1228 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a ^ b
;
1229 AssertNodeType (e4
, ExpressionType
.ExclusiveOr
);
1230 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1231 Assert (new MyType (-108), e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1234 void ExclusiveOrTest_5 ()
1236 Expression
<Func
<MyType
?, byte, int?>> e5
= (MyType
? a
, byte b
) => a ^ b
;
1237 AssertNodeType (e5
, ExpressionType
.ExclusiveOr
);
1238 Assert (null, e5
.Compile ().Invoke (null, 64));
1239 Assert (96, e5
.Compile ().Invoke (new MyType (64), 32));
1242 void ExclusiveOrTest_6 ()
1244 Expression
<Func
<MyEnum
, MyEnum
, MyEnum
>> e6
= (a
, b
) => a ^ b
;
1245 AssertNodeType (e6
, ExpressionType
.Convert
);
1246 Assert ((MyEnum
)3, e6
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
1247 Assert
<MyEnum
> (0, e6
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1250 void ExclusiveOrTest_7 ()
1252 Expression
<Func
<MyEnum
?, MyEnum
?, MyEnum
?>> e7
= (a
, b
) => a ^ b
;
1253 AssertNodeType (e7
, ExpressionType
.Convert
);
1254 Assert (null, e7
.Compile ().Invoke (MyEnum
.Value_1
, null));
1255 Assert
<MyEnum
?> (0, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1258 void ExclusiveOrTest_8 ()
1260 Expression
<Func
<MyEnum
?, MyEnum
?>> e8
= (a
) => a ^
null;
1261 AssertNodeType (e8
, ExpressionType
.Convert
);
1262 Assert (null, e8
.Compile ().Invoke (MyEnum
.Value_1
));
1263 Assert (null, e8
.Compile ().Invoke (null));
1266 void GreaterThanTest ()
1268 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
> b
;
1269 AssertNodeType (e
, ExpressionType
.GreaterThan
);
1270 Assert (true, e
.Compile ().Invoke (60, 30));
1273 void GreaterThanTest_2 ()
1275 Expression
<Func
<uint?, byte?, bool>> e2
= (a
, b
) => a
> b
;
1276 AssertNodeType (e2
, ExpressionType
.GreaterThan
);
1277 Assert (false, e2
.Compile ().Invoke (null, 3));
1278 Assert (false, e2
.Compile ().Invoke (2, 2));
1281 void GreaterThanTest_3 ()
1283 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
> b
;
1284 AssertNodeType (e3
, ExpressionType
.GreaterThan
);
1285 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1288 void GreaterThanTest_4 ()
1290 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
> b
;
1291 AssertNodeType (e4
, ExpressionType
.GreaterThan
);
1292 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1293 Assert (false, e4
.Compile ().Invoke (null, null));
1294 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1297 void GreaterThanTest_5 ()
1299 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
> b
;
1300 AssertNodeType (e5
, ExpressionType
.GreaterThan
);
1301 Assert (false, e5
.Compile ().Invoke (null, 33));
1302 Assert (false, e5
.Compile ().Invoke (null, 0));
1303 Assert (true, e5
.Compile ().Invoke (new MyType (120), 3));
1306 void GreaterThanTest_6 ()
1308 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
> null;
1309 AssertNodeType (e6
, ExpressionType
.GreaterThan
);
1310 Assert (false, e6
.Compile ().Invoke (60));
1313 void GreaterThanTest_7 ()
1315 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
> b
;
1316 AssertNodeType (e7
, ExpressionType
.GreaterThan
);
1317 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1318 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1321 void GreaterThanTest_8 ()
1323 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
> b
;
1324 AssertNodeType (e8
, ExpressionType
.GreaterThan
);
1325 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1326 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1329 void GreaterThanOrEqualTest ()
1331 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
>= b
;
1332 AssertNodeType (e
, ExpressionType
.GreaterThanOrEqual
);
1333 Assert (true, e
.Compile ().Invoke (60, 30));
1336 void GreaterThanOrEqualTest_2 ()
1338 Expression
<Func
<byte?, byte?, bool>> e2
= (a
, b
) => a
>= b
;
1339 AssertNodeType (e2
, ExpressionType
.GreaterThanOrEqual
);
1340 Assert (false, e2
.Compile ().Invoke (null, 3));
1341 Assert (true, e2
.Compile ().Invoke (2, 2));
1344 void GreaterThanOrEqualTest_3 ()
1346 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
>= b
;
1347 AssertNodeType (e3
, ExpressionType
.GreaterThanOrEqual
);
1348 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)), "D1");
1351 void GreaterThanOrEqualTest_4 ()
1353 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
>= b
;
1354 AssertNodeType (e4
, ExpressionType
.GreaterThanOrEqual
);
1355 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1356 Assert (false, e4
.Compile ().Invoke (null, null));
1357 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1360 void GreaterThanOrEqualTest_5 ()
1362 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
>= b
;
1363 AssertNodeType (e5
, ExpressionType
.GreaterThanOrEqual
);
1364 Assert (false, e5
.Compile ().Invoke (null, 33));
1365 Assert (false, e5
.Compile ().Invoke (null, 0));
1366 Assert (true, e5
.Compile ().Invoke (new MyType (120), 3));
1369 void GreaterThanOrEqualTest_6 ()
1371 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
>= null;
1372 AssertNodeType (e6
, ExpressionType
.GreaterThanOrEqual
);
1373 Assert (false, e6
.Compile ().Invoke (60));
1376 void GreaterThanOrEqualTest_7 ()
1378 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
>= b
;
1379 AssertNodeType (e7
, ExpressionType
.GreaterThanOrEqual
);
1380 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1381 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1384 void GreaterThanOrEqualTest_8 ()
1386 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
>= b
;
1387 AssertNodeType (e8
, ExpressionType
.GreaterThanOrEqual
);
1388 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1389 Assert (true, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1394 var del
= new IntDelegate (TestInt
);
1395 Expression
<Func
<IntDelegate
, int>> e
= (a
) => a ();
1396 AssertNodeType (e
, ExpressionType
.Invoke
);
1397 Assert (29, e
.Compile ().Invoke (del
));
1400 void InvokeTest_2 ()
1402 Expression
<Func
<Func
<int, string>, int, string>> e2
= (a
, b
) => a (b
);
1403 AssertNodeType (e2
, ExpressionType
.Invoke
);
1404 Assert ("4", e2
.Compile ().Invoke ((a
) => (a
+1).ToString (), 3));
1409 Expression
<Func
<string, Func
<string>>> e
= (string s
) => () => s
;
1410 AssertNodeType (e
, ExpressionType
.Lambda
);
1411 Assert ("xx", e
.Compile ().Invoke ("xx") ());
1414 void LeftShiftTest ()
1416 Expression
<Func
<ulong, short, ulong>> e
= (ulong a
, short b
) => a
<< b
;
1417 AssertNodeType (e
, ExpressionType
.LeftShift
);
1418 Assert ((ulong) 0x7F000, e
.Compile ().Invoke (0xFE, 11));
1419 Assert ((ulong) 0x1FFFFFFFE, e
.Compile ().Invoke (0xFFFFFFFF, 0xA01));
1421 // .net produces a strange result
1422 // see https://bugzilla.novell.com/show_bug.cgi?id=398358
1423 // Assert ((ulong) 0xFFFFFFFE00000000, e.Compile ().Invoke (0xFFFFFFFF, 0xA01));
1426 void LeftShiftTest_2 ()
1428 Expression
<Func
<MyType
, MyType
, int>> e2
= (MyType a
, MyType b
) => a
<< b
;
1429 AssertNodeType (e2
, ExpressionType
.LeftShift
);
1430 var c2
= e2
.Compile ();
1431 Assert (1024, c2 (new MyType (256), new MyType (2)));
1434 void LeftShiftTest_3 ()
1436 Expression
<Func
<long?, sbyte, long?>> e3
= (long? a
, sbyte b
) => a
<< b
;
1437 AssertNodeType (e3
, ExpressionType
.LeftShift
);
1438 Assert (null, e3
.Compile ().Invoke (null, 11));
1439 Assert (2048, e3
.Compile ().Invoke (1024, 1));
1442 void LeftShiftTest_4 ()
1444 Expression
<Func
<MyType
?, MyType
?, int?>> e4
= (MyType
? a
, MyType
? b
) => a
<< b
;
1445 AssertNodeType (e4
, ExpressionType
.LeftShift
);
1446 var c4
= e4
.Compile ();
1447 Assert (null, c4 (new MyType (8), null));
1448 Assert (null, c4 (null, new MyType (8)));
1449 Assert (1024, c4 (new MyType (256), new MyType (2)));
1452 void LeftShiftTest_5 ()
1454 Expression
<Func
<ushort, int?>> e5
= (ushort a
) => a
<< null;
1455 AssertNodeType (e5
, ExpressionType
.LeftShift
);
1456 Assert (null, e5
.Compile ().Invoke (30));
1459 void LeftShiftTest_6 ()
1461 Expression
<Func
<int, MyTypeImplicitOnly
, int>> e
= (a
, b
) => a
<< b
;
1462 AssertNodeType (e
, ExpressionType
.LeftShift
);
1463 Assert (0x7F0, e
.Compile ().Invoke (0xFE, new MyTypeImplicitOnly (3)));
1466 void LessThanTest ()
1468 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
< b
;
1469 AssertNodeType (e
, ExpressionType
.LessThan
);
1470 Assert (false, e
.Compile ().Invoke (60, 30));
1473 void LessThanTest_2 ()
1475 Expression
<Func
<uint?, byte?, bool>> e2
= (a
, b
) => a
< b
;
1476 AssertNodeType (e2
, ExpressionType
.LessThan
);
1477 Assert (false, e2
.Compile ().Invoke (null, 3));
1478 Assert (false, e2
.Compile ().Invoke (2, 2));
1481 void LessThanTest_3 ()
1483 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
< b
;
1484 AssertNodeType (e3
, ExpressionType
.LessThan
);
1485 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1488 void LessThanTest_4 ()
1490 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
< b
;
1491 AssertNodeType (e4
, ExpressionType
.LessThan
);
1492 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1493 Assert (false, e4
.Compile ().Invoke (null, null));
1494 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1497 void LessThanTest_5 ()
1499 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
< b
;
1500 AssertNodeType (e5
, ExpressionType
.LessThan
);
1501 Assert (false, e5
.Compile ().Invoke (null, 33));
1502 Assert (false, e5
.Compile ().Invoke (null, 0));
1503 Assert (false, e5
.Compile ().Invoke (new MyType (120), 3));
1506 void LessThanTest_6 ()
1508 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
< null;
1509 AssertNodeType (e6
, ExpressionType
.LessThan
);
1510 Assert (false, e6
.Compile ().Invoke (60));
1513 void LessThanTest_7 ()
1515 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
< b
;
1516 AssertNodeType (e7
, ExpressionType
.LessThan
);
1517 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1518 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1521 void LessThanTest_8 ()
1523 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
< b
;
1524 AssertNodeType (e8
, ExpressionType
.LessThan
);
1525 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1526 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1529 void LessThanOrEqualTest ()
1531 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
<= b
;
1532 AssertNodeType (e
, ExpressionType
.LessThanOrEqual
);
1533 Assert (false, e
.Compile ().Invoke (60, 30));
1536 void LessThanOrEqualTest_2 ()
1538 Expression
<Func
<byte?, byte?, bool>> e2
= (a
, b
) => a
<= b
;
1539 AssertNodeType (e2
, ExpressionType
.LessThanOrEqual
);
1540 Assert (false, e2
.Compile ().Invoke (null, 3));
1541 Assert (true, e2
.Compile ().Invoke (2, 2));
1544 void LessThanOrEqualTest_3 ()
1546 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
<= b
;
1547 AssertNodeType (e3
, ExpressionType
.LessThanOrEqual
);
1548 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1551 void LessThanOrEqualTest_4 ()
1553 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
<= b
;
1554 AssertNodeType (e4
, ExpressionType
.LessThanOrEqual
);
1555 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1556 Assert (false, e4
.Compile ().Invoke (null, null));
1557 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1560 void LessThanOrEqualTest_5 ()
1562 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
<= b
;
1563 AssertNodeType (e5
, ExpressionType
.LessThanOrEqual
);
1564 Assert (false, e5
.Compile ().Invoke (null, 33));
1565 Assert (false, e5
.Compile ().Invoke (null, 0));
1566 Assert (false, e5
.Compile ().Invoke (new MyType (120), 3));
1569 void LessThanOrEqualTest_6 ()
1571 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
<= null;
1572 AssertNodeType (e6
, ExpressionType
.LessThanOrEqual
);
1573 Assert (false, e6
.Compile ().Invoke (60));
1576 void LessThanOrEqualTest_7 ()
1578 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
<= b
;
1579 AssertNodeType (e7
, ExpressionType
.LessThanOrEqual
);
1580 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1581 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1584 void LessThanOrEqualTest_8 ()
1586 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
<= b
;
1587 AssertNodeType (e8
, ExpressionType
.LessThanOrEqual
);
1588 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1589 Assert (true, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1592 void ListInitTest ()
1594 Expression
<Func
<List
<object>>> e1
= () => new List
<object> { "Hello", "", null, "World", 5 }
;
1595 AssertNodeType (e1
, ExpressionType
.ListInit
);
1596 var re1
= e1
.Compile ().Invoke ();
1597 Assert (null, re1
[2]);
1598 Assert ("World", re1
[3]);
1599 Assert (5, re1
[4]);
1602 void ListInitTest_2 ()
1604 Expression
<Func
<int, Dictionary
<string, int>>> e2
= (int value) => new Dictionary
<string, int> (3) { { "A", value }
, { "B", 2 }
};
1605 AssertNodeType (e2
, ExpressionType
.ListInit
);
1606 var re2
= e2
.Compile ().Invoke (3456);
1607 Assert (3456, re2
["A"]);
1610 void MemberAccessTest ()
1612 MemberAccessData d
= new MemberAccessData ();
1614 Expression
<Func
<bool>> e
= () => d
.BoolValue
;
1615 AssertNodeType (e
, ExpressionType
.MemberAccess
);
1616 Assert (true, e
.Compile ().Invoke ());
1617 d
.BoolValue
= false;
1618 Assert (false, e
.Compile ().Invoke ());
1621 void MemberAccessTest_2 ()
1623 Expression
<Func
<decimal>> e2
= () => MemberAccessData
.DecimalValue
;
1624 AssertNodeType (e2
, ExpressionType
.MemberAccess
);
1625 Assert (decimal.MinValue
, e2
.Compile ().Invoke ());
1628 void MemberAccessTest_3 ()
1630 MemberAccessData d
= new MemberAccessData ();
1631 d
.VolatileValue
= 492;
1632 Expression
<Func
<uint>> e3
= () => d
.VolatileValue
;
1633 AssertNodeType (e3
, ExpressionType
.MemberAccess
);
1634 Assert
<uint> (492, e3
.Compile ().Invoke ());
1637 void MemberAccessTest_4 ()
1639 MemberAccessData d
= new MemberAccessData ();
1640 Expression
<Func
<string[]>> e4
= () => d
.StringValues
;
1641 AssertNodeType (e4
, ExpressionType
.MemberAccess
);
1642 Assert (null, e4
.Compile ().Invoke ());
1645 void MemberAccessTest_5 ()
1647 MemberAccessData d
= new MemberAccessData ();
1648 var e5
= d
.GetEvent ();
1649 AssertNodeType (e5
, ExpressionType
.MemberAccess
);
1650 Assert (null, e5
.Compile ().Invoke ());
1653 void MemberAccessTest_6 ()
1655 MemberAccessData d
= new MemberAccessData ();
1656 Expression
<Func
<MyType
>> e6
= () => d
.MyTypeProperty
;
1657 AssertNodeType (e6
, ExpressionType
.MemberAccess
);
1658 Assert (new MyType (), e6
.Compile ().Invoke ());
1661 void MemberAccessTest_7 ()
1663 MemberAccessData d
= new MemberAccessData ();
1664 Expression
<Func
<MyType
, short>> e7
= a
=> a
.ShortProp
;
1665 AssertNodeType (e7
, ExpressionType
.MemberAccess
);
1666 MyType mt
= new MyType ();
1668 Assert (124, e7
.Compile ().Invoke (mt
));
1671 void MemberAccessTest_8 ()
1673 Expression
<Func
<string>> e8
= () => MemberAccessData
.StaticProperty
;
1674 AssertNodeType (e8
, ExpressionType
.MemberAccess
);
1675 Assert ("alo", e8
.Compile ().Invoke ());
1678 void MemberAccessTest_9 ()
1680 string s
= "localvar";
1681 Expression
<Func
<string>> e9
= () => s
;
1684 AssertNodeType (e9
, ExpressionType
.MemberAccess
);
1685 Assert ("changed", e9
.Compile ().Invoke ());
1688 void MemberInitTest ()
1690 Expression
<Func
<MemberAccessData
>> e
= () => new MemberAccessData
{
1691 VolatileValue
= 2, StringValues
= new string [] { "sv" }
, MyTypeProperty
= new MyType (692)
1693 AssertNodeType (e
, ExpressionType
.MemberInit
);
1694 var r1
= e
.Compile ().Invoke ();
1695 Assert
<uint> (2, r1
.VolatileValue
);
1696 Assert (new string[] { "sv" }
, r1
.StringValues
);
1697 Assert (new MyType (692), r1
.MyTypeProperty
);
1700 void MemberInitTest_2 ()
1702 Expression
<Func
<MemberAccessData
>> e2
= () => new MemberAccessData
{
1703 ListValues
= new List
<string> { "a", null }
1706 AssertNodeType (e2
, ExpressionType
.MemberInit
);
1707 var r2
= e2
.Compile ().Invoke ();
1708 Assert ("a", r2
.ListValues
[0]);
1711 void MemberInitTest_3 ()
1713 Expression
<Func
<short, MyType
>> e3
= a
=> new MyType { ShortProp = a }
;
1714 AssertNodeType (e3
, ExpressionType
.MemberInit
);
1715 var r3
= e3
.Compile ().Invoke (33);
1716 Assert (33, r3
.ShortProp
);
1719 void MemberInitTest_4 ()
1721 Expression
<Func
<int>> e
= () => new int { }
;
1723 AssertNodeType (e
, ExpressionType
.MemberInit
);
1724 var r
= e
.Compile ().Invoke ();
1730 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
% b
;
1731 AssertNodeType (e
, ExpressionType
.Modulo
);
1732 Assert (29, e
.Compile ().Invoke (60, 31));
1735 void ModuloTest_2 ()
1737 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
% b
;
1738 AssertNodeType (e2
, ExpressionType
.Modulo
);
1739 Assert (null, e2
.Compile ().Invoke (null, 3));
1740 Assert (1.1, e2
.Compile ().Invoke (3.1, 2));
1743 void ModuloTest_3 ()
1745 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
% b
;
1746 AssertNodeType (e3
, ExpressionType
.Modulo
);
1747 Assert (0, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1750 void ModuloTest_4 ()
1752 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
% b
;
1753 AssertNodeType (e4
, ExpressionType
.Modulo
);
1754 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1755 Assert (new MyType (12), e4
.Compile ().Invoke (new MyType (12), new MyType (-20)));
1758 void ModuloTest_5 ()
1760 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
% b
;
1761 AssertNodeType (e5
, ExpressionType
.Modulo
);
1762 Assert (1, e5
.Compile ().Invoke (99, new MyType (2)));
1765 void ModuloTest_6 ()
1767 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
% b
;
1768 AssertNodeType (e6
, ExpressionType
.Modulo
);
1769 Assert (100, e6
.Compile ().Invoke (100, new MyType (200)));
1770 Assert (null, e6
.Compile ().Invoke (20, null));
1773 void ModuloTest_7 ()
1775 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => a
% null;
1776 AssertNodeType (e7
, ExpressionType
.Modulo
);
1777 Assert (null, e7
.Compile ().Invoke (60));
1780 void MultiplyTest ()
1782 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
* b
;
1783 AssertNodeType (e
, ExpressionType
.Multiply
);
1784 Assert (1860, e
.Compile ().Invoke (60, 31));
1785 Assert (2147483617, e
.Compile ().Invoke (int.MaxValue
, 31));
1788 void MultiplyTest_2 ()
1790 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
* b
;
1791 AssertNodeType (e2
, ExpressionType
.Multiply
);
1792 Assert (null, e2
.Compile ().Invoke (null, 3));
1793 Assert (6.2, e2
.Compile ().Invoke (3.1, 2));
1796 void MultiplyTest_3 ()
1798 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
* b
;
1799 AssertNodeType (e3
, ExpressionType
.Multiply
);
1800 Assert (400, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1803 void MultiplyTest_4 ()
1805 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
* b
;
1806 AssertNodeType (e4
, ExpressionType
.Multiply
);
1807 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1808 Assert (new MyType (-240), e4
.Compile ().Invoke (new MyType (12), new MyType (-20)));
1811 void MultiplyTest_5 ()
1813 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
* b
;
1814 AssertNodeType (e5
, ExpressionType
.Multiply
);
1815 Assert (198, e5
.Compile ().Invoke (99, new MyType (2)));
1818 void MultiplyTest_6 ()
1820 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
* b
;
1821 AssertNodeType (e6
, ExpressionType
.Multiply
);
1822 Assert (0, e6
.Compile ().Invoke (int.MinValue
, new MyType (200)));
1823 Assert (null, e6
.Compile ().Invoke (20, null));
1826 void MultiplyTest_7 ()
1828 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => a
* null;
1829 AssertNodeType (e7
, ExpressionType
.Multiply
);
1830 Assert (null, e7
.Compile ().Invoke (60));
1833 void MultiplyCheckedTest ()
1836 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
* b
;
1837 AssertNodeType (e
, ExpressionType
.MultiplyChecked
);
1839 e
.Compile ().Invoke (int.MaxValue
, 309);
1840 throw new ApplicationException ("MultiplyCheckedTest #1");
1841 } catch (OverflowException
) { }
1845 void MultiplyCheckedTest_2 ()
1848 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a
* b
;
1849 AssertNodeType (e2
, ExpressionType
.MultiplyChecked
);
1850 Assert (null, e2
.Compile ().Invoke (null, 3));
1851 Assert (14025, e2
.Compile ().Invoke (byte.MaxValue
, 55));
1855 void MultiplyCheckedTest_3 ()
1858 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
* b
;
1859 AssertNodeType (e3
, ExpressionType
.Multiply
);
1860 Assert (-600, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
1864 void MultiplyCheckedTest_4 ()
1867 Expression
<Func
<double, double, double>> e4
= (a
, b
) => a
* b
;
1868 AssertNodeType (e4
, ExpressionType
.Multiply
);
1869 Assert (double.PositiveInfinity
, e4
.Compile ().Invoke (double.MaxValue
, int.MaxValue
));
1873 void MultiplyCheckedTest_5 ()
1876 Expression
<Func
<float?, float?, float?>> e5
= (a
, b
) => b
* a
;
1877 AssertNodeType (e5
, ExpressionType
.MultiplyChecked
);
1878 Assert (float.PositiveInfinity
, e5
.Compile ().Invoke (float.Epsilon
, float.PositiveInfinity
));
1884 Expression
<Func
<int, int>> e
= (a
) => -a
;
1885 AssertNodeType (e
, ExpressionType
.Negate
);
1886 Assert (30, e
.Compile ().Invoke (-30));
1889 void NegateTest_2 ()
1891 Expression
<Func
<sbyte, int>> e2
= (a
) => -(-a
);
1892 AssertNodeType (e2
, ExpressionType
.Negate
);
1893 Assert (-10, e2
.Compile ().Invoke (-10));
1896 void NegateTest_3 ()
1898 Expression
<Func
<long?, long?>> e3
= (a
) => -a
;
1899 AssertNodeType (e3
, ExpressionType
.Negate
);
1900 Assert (long.MinValue
+ 1, e3
.Compile ().Invoke (long.MaxValue
));
1901 Assert (null, e3
.Compile ().Invoke (null));
1904 void NegateTest_4 ()
1906 Expression
<Func
<MyType
, MyType
>> e4
= (a
) => -a
;
1907 AssertNodeType (e4
, ExpressionType
.Negate
);
1908 Assert (new MyType (14), e4
.Compile ().Invoke (new MyType (-14)));
1911 void NegateTest_5 ()
1913 Expression
<Func
<MyType
?, MyType
?>> e5
= (a
) => -a
;
1914 AssertNodeType (e5
, ExpressionType
.Negate
);
1915 Assert (new MyType (-33), e5
.Compile ().Invoke (new MyType (33)));
1916 Assert (null, e5
.Compile ().Invoke (null));
1919 void NegateTest_6 ()
1921 Expression
<Func
<MyTypeImplicitOnly
, int>> e6
= (MyTypeImplicitOnly a
) => -a
;
1922 AssertNodeType (e6
, ExpressionType
.Negate
);
1923 Assert (-4, e6
.Compile ().Invoke (new MyTypeImplicitOnly (4)));
1926 void NegateTest_7 ()
1928 Expression
<Func
<MyTypeImplicitOnly
?, int?>> e7
= (MyTypeImplicitOnly
? a
) => -a
;
1929 AssertNodeType (e7
, ExpressionType
.Negate
);
1930 Assert (-46, e7
.Compile ().Invoke (new MyTypeImplicitOnly (46)));
1932 // Another version of MS bug when predefined conversion is required on nullable user operator
1933 // Assert (null, e7.Compile ().Invoke (null));
1936 void NegateTest_8 ()
1938 Expression
<Func
<sbyte?, int?>> e8
= (a
) => -a
;
1939 AssertNodeType (e8
, ExpressionType
.Negate
);
1940 Assert (11, e8
.Compile ().Invoke (-11));
1943 void NegateTest_9 ()
1945 Expression
<Func
<uint, long>> e9
= (a
) => -a
;
1946 AssertNodeType (e9
, ExpressionType
.Negate
);
1947 Assert (-2, e9
.Compile ().Invoke (2));
1950 void NegateTestChecked ()
1953 Expression
<Func
<int, int>> e
= (int a
) => -a
;
1954 AssertNodeType (e
, ExpressionType
.NegateChecked
);
1956 e
.Compile ().Invoke (int.MinValue
);
1957 throw new ApplicationException ("NegateTestChecked #1");
1958 } catch (OverflowException
) { }
1962 void NegateTestChecked_2 ()
1965 Expression
<Func
<byte?, int?>> e2
= (a
) => -a
;
1966 AssertNodeType (e2
, ExpressionType
.NegateChecked
);
1967 Assert (null, e2
.Compile ().Invoke (null));
1968 Assert (-255, e2
.Compile ().Invoke (byte.MaxValue
));
1972 void NegateTestChecked_3 ()
1975 Expression
<Func
<MyType
, MyType
>> e3
= (MyType a
) => -a
;
1976 AssertNodeType (e3
, ExpressionType
.Negate
);
1977 Assert (20, e3
.Compile ().Invoke (new MyType (-20)));
1981 void NegateTestChecked_4 ()
1984 Expression
<Func
<double, double>> e4
= (a
) => -a
;
1985 AssertNodeType (e4
, ExpressionType
.Negate
);
1986 Assert (double.NegativeInfinity
, e4
.Compile ().Invoke (double.PositiveInfinity
));
1990 void NewArrayInitTest ()
1992 Expression
<Func
<int []>> e
= () => new int [1] { 5 }
;
1993 AssertNodeType (e
, ExpressionType
.NewArrayInit
);
1994 Assert (new int [1] { 5 }
, e
.Compile ().Invoke ());
1997 void NewArrayInitTest_2 ()
1999 Expression
<Func
<int []>> e1
= () => new int [] { }
;
2000 AssertNodeType (e1
, ExpressionType
.NewArrayInit
);
2001 Assert (new int [0], e1
.Compile ().Invoke ());
2004 void NewArrayInitTest_3 ()
2006 Expression
<Func
<ushort, ulong? []>> e2
= (ushort a
) => new ulong? [] { a }
;
2007 AssertNodeType (e2
, ExpressionType
.NewArrayInit
);
2008 Assert (new ulong? [1] { ushort.MaxValue }
, e2
.Compile ().Invoke (ushort.MaxValue
));
2011 void NewArrayInitTest_4 ()
2013 Expression
<Func
<char [] []>> e3
= () => new char [] [] { new char [] { 'a' }
};
2014 AssertNodeType (e3
, ExpressionType
.NewArrayInit
);
2015 Assert (new char [] { 'a' }
, e3
.Compile ().Invoke () [0]);
2018 void NewArrayInitTest_5 ()
2020 Expression
<Func
<int?[]>> e
= () => new int?[] { null, 3, 4 }
;
2021 AssertNodeType (e
, ExpressionType
.NewArrayInit
);
2022 Assert (3, e
.Compile ().Invoke ().Length
);
2025 void NewArrayInitTest_6 ()
2027 Expression
<Func
<string []>> e
= () => new [] { null, "a" }
;
2028 AssertNodeType (e
, ExpressionType
.NewArrayInit
);
2029 Assert (2, e
.Compile ().Invoke ().Length
);
2032 void NewArrayBoundsTest ()
2034 Expression
<Func
<int [,]>> e
= () => new int [2,3];
2035 AssertNodeType (e
, ExpressionType
.NewArrayBounds
);
2036 Assert (new int [2,3].Length
, e
.Compile ().Invoke ().Length
);
2039 void NewArrayBoundsTest_2 ()
2041 Expression
<Func
<int[,]>> e2
= () => new int [0,0];
2042 AssertNodeType (e2
, ExpressionType
.NewArrayBounds
);
2043 Assert (new int [0, 0].Length
, e2
.Compile ().Invoke ().Length
);
2046 void NewArrayBoundsTest_3 ()
2048 Expression
<Func
<int []>> e
= () => new int [0];
2049 AssertNodeType (e
, ExpressionType
.NewArrayBounds
);
2050 Assert (0, e
.Compile ().Invoke ().Length
);
2053 void NewArrayBoundsTest_4 ()
2055 const ulong max
= ulong.MaxValue
;
2057 Expression
<Func
<bool[]>> e
= () => new bool [max
];
2058 AssertNodeType (e
, ExpressionType
.NewArrayBounds
);
2063 Expression
<Func
<MyType
>> e
= () => new MyType (2);
2064 AssertNodeType (e
, ExpressionType
.New
);
2065 Assert (new MyType (2), e
.Compile ().Invoke ());
2070 Expression
<Func
<MyType
>> e2
= () => new MyType ();
2071 AssertNodeType (e2
, ExpressionType
.New
);
2072 Assert (new MyType (), e2
.Compile ().Invoke ());
2077 Expression
<Func
<NewTest
<bool>>> e3
= () => new NewTest
<bool> (true);
2078 AssertNodeType (e3
, ExpressionType
.New
);
2079 Assert (new NewTest
<bool> (true), e3
.Compile ().Invoke ());
2084 Expression
<Func
<decimal, NewTest
<decimal>>> e4
= (decimal d
) => new NewTest
<decimal> (1, 5, d
);
2085 AssertNodeType (e4
, ExpressionType
.New
);
2086 Assert (new NewTest
<decimal> (1, 5, -9), e4
.Compile ().Invoke (-9));
2091 Expression
<Func
<object>> e5
= () => new { A = 9, Value = "a" }
;
2092 AssertNodeType (e5
, ExpressionType
.New
);
2093 var ne
= ((NewExpression
) e5
.Body
);
2095 Assert (2, ne
.Members
.Count
, "members count");
2097 // Behaviour is different between .NET 3.5 and .NET 4.0
2098 if (ne
.Members
[0].MemberType
== MemberTypes
.Property
) {
2099 Assert ("A", ne
.Members
[0].Name
, "Name #1");
2100 Assert ("Value", ne
.Members
[1].Name
, "Name #2");
2102 Assert ("get_A", ne
.Members
[0].Name
, "Name #1");
2103 Assert ("get_Value", ne
.Members
[1].Name
, "Name #2");
2106 Assert (new { A = 9, Value = "a" }
, e5
.Compile ().Invoke ());
2111 Expression
<Func
<object>> e5
= () => new { A = 9, Value = new MyType (5) }
;
2112 AssertNodeType (e5
, ExpressionType
.New
);
2115 // CSC bug: emits new MyEnum as a constant
2118 Expression
<Func
<MyEnum
>> e
= () => new MyEnum ();
2119 AssertNodeType (e
, ExpressionType
.New
);
2120 Assert
<MyEnum
> (0, e
.Compile ().Invoke ());
2125 Expression
<Func
<bool, bool>> e
= (bool a
) => !a
;
2126 AssertNodeType (e
, ExpressionType
.Not
);
2127 Assert (false, e
.Compile ().Invoke (true));
2132 Expression
<Func
<MyType
, bool>> e2
= (MyType a
) => !a
;
2133 AssertNodeType (e2
, ExpressionType
.Not
);
2134 Assert (true, e2
.Compile ().Invoke (new MyType (1)));
2135 Assert (false, e2
.Compile ().Invoke (new MyType (-1)));
2140 Expression
<Func
<int, int>> e3
= (int a
) => ~a
;
2141 AssertNodeType (e3
, ExpressionType
.Not
);
2142 Assert (-8, e3
.Compile ().Invoke (7));
2147 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => ~a
;
2148 AssertNodeType (e4
, ExpressionType
.Not
);
2149 Assert (0, e4
.Compile ().Invoke (new MyType (-1)));
2154 Expression
<Func
<ulong, ulong>> e5
= (ulong a
) => ~a
;
2155 AssertNodeType (e5
, ExpressionType
.Not
);
2156 Assert
<ulong> (18446744073709551608, e5
.Compile ().Invoke (7));
2161 Expression
<Func
<MyEnum
, MyEnum
>> e6
= (MyEnum a
) => ~a
;
2162 AssertNodeType (e6
, ExpressionType
.Convert
);
2163 Assert ((MyEnum
)254, e6
.Compile ().Invoke (MyEnum
.Value_1
));
2166 void NotNullableTest ()
2168 Expression
<Func
<bool?, bool?>> e
= (bool? a
) => !a
;
2169 AssertNodeType (e
, ExpressionType
.Not
);
2170 Assert (false, e
.Compile ().Invoke (true));
2171 Assert (null, e
.Compile ().Invoke (null));
2174 void NotNullableTest_2 ()
2176 Expression
<Func
<MyType
?, bool?>> e2
= (MyType
? a
) => !a
;
2177 AssertNodeType (e2
, ExpressionType
.Not
);
2178 Assert (true, e2
.Compile ().Invoke (new MyType (1)));
2179 Assert (null, e2
.Compile ().Invoke (null));
2182 void NotNullableTest_3 ()
2184 Expression
<Func
<sbyte?, int?>> e3
= (sbyte? a
) => ~a
;
2185 AssertNodeType (e3
, ExpressionType
.Not
);
2186 Assert (-5, e3
.Compile ().Invoke (4));
2187 Assert (null, e3
.Compile ().Invoke (null));
2190 void NotNullableTest_4 ()
2192 Expression
<Func
<MyType
?, int?>> e4
= (MyType
? a
) => ~a
;
2193 AssertNodeType (e4
, ExpressionType
.Not
);
2194 Assert (0, e4
.Compile ().Invoke (new MyType (-1)));
2195 Assert (null, e4
.Compile ().Invoke (null));
2198 void NotNullableTest_5 ()
2200 Expression
<Func
<MyEnum
?, MyEnum
?>> e5
= (MyEnum
? a
) => ~a
;
2201 AssertNodeType (e5
, ExpressionType
.Convert
);
2202 Assert ((MyEnum
) 254, e5
.Compile ().Invoke (MyEnum
.Value_1
));
2203 Assert (null, e5
.Compile ().Invoke (null));
2206 void NotEqualTest ()
2208 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
!= b
;
2209 AssertNodeType (e
, ExpressionType
.NotEqual
);
2210 Assert (true, e
.Compile ().Invoke (60, 30));
2211 Assert (false, e
.Compile ().Invoke (-1, -1));
2214 void NotEqualTest_2 ()
2216 Expression
<Func
<sbyte?, sbyte?, bool>> e2
= (a
, b
) => a
!= b
;
2217 AssertNodeType (e2
, ExpressionType
.NotEqual
);
2218 Assert (false, e2
.Compile ().Invoke (3, 3));
2219 Assert (true, e2
.Compile ().Invoke (3, 2));
2222 void NotEqualTest_3 ()
2224 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
!= b
;
2225 AssertNodeType (e3
, ExpressionType
.NotEqual
);
2226 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
2229 void NotEqualTest_4 ()
2231 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
!= b
;
2232 AssertNodeType (e4
, ExpressionType
.NotEqual
);
2233 Assert (true, e4
.Compile ().Invoke (null, new MyType (-20)));
2234 Assert (false, e4
.Compile ().Invoke (null, null));
2235 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (120)));
2238 void NotEqualTest_5 ()
2240 Expression
<Func
<bool?, bool?, bool>> e5
= (bool? a
, bool? b
) => a
!= b
;
2241 AssertNodeType (e5
, ExpressionType
.NotEqual
);
2242 Assert (true, e5
.Compile ().Invoke (true, null));
2243 Assert (false, e5
.Compile ().Invoke (null, null));
2244 Assert (false, e5
.Compile ().Invoke (false, false));
2247 void NotEqualTest_6 ()
2249 Expression
<Func
<bool, bool>> e6
= (bool a
) => a
!= null;
2250 AssertNodeType (e6
, ExpressionType
.NotEqual
);
2251 Assert (true, e6
.Compile ().Invoke (true));
2252 Assert (true, e6
.Compile ().Invoke (false));
2255 void NotEqualTest_7 ()
2257 Expression
<Func
<string, string, bool>> e7
= (string a
, string b
) => a
!= b
;
2258 AssertNodeType (e7
, ExpressionType
.NotEqual
);
2259 Assert (false, e7
.Compile ().Invoke (null, null));
2260 Assert (true, e7
.Compile ().Invoke ("a", "A"));
2261 Assert (false, e7
.Compile ().Invoke ("a", "a"));
2264 void NotEqualTest_8 ()
2266 Expression
<Func
<object, bool>> e8
= (object a
) => null != a
;
2267 AssertNodeType (e8
, ExpressionType
.NotEqual
);
2268 Assert (false, e8
.Compile ().Invoke (null));
2269 Assert (true, e8
.Compile ().Invoke ("a"));
2270 Assert (true, e8
.Compile ().Invoke (this));
2273 void NotEqualTest_9 ()
2275 Expression
<Func
<MyEnum
, MyEnum
, bool>> e9
= (a
, b
) => a
!= b
;
2276 AssertNodeType (e9
, ExpressionType
.NotEqual
);
2277 Assert (true, e9
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2278 Assert (false, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2281 void NotEqualTest_10 ()
2283 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e10
= (a
, b
) => a
!= b
;
2284 AssertNodeType (e10
, ExpressionType
.NotEqual
);
2285 Assert (true, e10
.Compile ().Invoke (MyEnum
.Value_1
, null));
2286 Assert (false, e10
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2289 void NotEqualTest_11 ()
2291 Expression
<Func
<MyEnum
?, bool>> e11
= (a
) => a
!= null;
2292 AssertNodeType (e11
, ExpressionType
.NotEqual
);
2293 Assert (true, e11
.Compile ().Invoke (MyEnum
.Value_1
));
2294 Assert (false, e11
.Compile ().Invoke (null));
2299 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
| b
;
2301 AssertNodeType (e
, ExpressionType
.Or
);
2302 Func
<bool, bool, bool> c
= e
.Compile ();
2304 Assert (true, c (true, true));
2305 Assert (true, c (true, false));
2306 Assert (true, c (false, true));
2307 Assert (false, c (false, false));
2312 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
| b
;
2313 AssertNodeType (e2
, ExpressionType
.Or
);
2314 var c2
= e2
.Compile ();
2315 Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
2320 Expression
<Func
<MyEnum
, MyEnum
, MyEnum
>> e3
= (a
, b
) => a
| b
;
2321 AssertNodeType (e3
, ExpressionType
.Convert
);
2322 Assert ((MyEnum
)3, e3
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2323 Assert (MyEnum
.Value_2
, e3
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2326 void OrNullableTest ()
2328 Expression
<Func
<bool?, bool?, bool?>> e
= (bool? a
, bool? b
) => a
| b
;
2330 AssertNodeType (e
, ExpressionType
.Or
);
2331 Func
<bool?, bool?, bool?> c
= e
.Compile ();
2333 Assert (true, c (true, true));
2334 Assert (true, c (true, false));
2335 Assert (true, c (false, true));
2336 Assert (false, c (false, false));
2338 Assert (true, c (true, null));
2339 Assert (null, c (false, null));
2340 Assert (null, c (null, false));
2341 Assert (true, c (true, null));
2342 Assert (null, c (null, null));
2345 void OrNullableTest_2 ()
2347 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e2
= (MyType
? a
, MyType
? b
) => a
| b
;
2348 AssertNodeType (e2
, ExpressionType
.Or
);
2349 var c2
= e2
.Compile ();
2350 Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
2351 Assert (null, c2 (new MyType (1), null));
2354 void OrNullableTest_3 ()
2356 Expression
<Func
<MyType
?, uint, long?>> e3
= (MyType
? a
, uint b
) => a
| b
;
2357 AssertNodeType (e3
, ExpressionType
.Or
);
2358 var c3
= e3
.Compile ();
2359 Assert (9, c3 (new MyType (1), 8));
2362 void OrNullableTest_4 ()
2364 Expression
<Func
<MyEnum
?, MyEnum
?, MyEnum
?>> e4
= (a
, b
) => a
| b
;
2365 AssertNodeType (e4
, ExpressionType
.Convert
);
2366 Assert (null, e4
.Compile ().Invoke (null, MyEnum
.Value_2
));
2367 Assert ((MyEnum
)3, e4
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2372 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
|| b
;
2373 AssertNodeType (e
, ExpressionType
.OrElse
);
2374 Assert (true, e
.Compile ().Invoke (true, false));
2377 void OrElseTest_2 ()
2379 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
|| b
;
2380 AssertNodeType (e2
, ExpressionType
.OrElse
);
2381 Assert (new MyType (64), e2
.Compile ().Invoke (new MyType (64), new MyType (64)));
2382 Assert (new MyType (32), e2
.Compile ().Invoke (new MyType (32), new MyType (64)));
2385 void ParameterTest ()
2387 Expression
<Func
<string, string>> e
= (string a
) => a
;
2388 AssertNodeType (e
, ExpressionType
.Parameter
);
2389 Assert ("t", e
.Compile ().Invoke ("t"));
2392 void ParameterTest_2 ()
2394 Expression
<Func
<object[], object[]>> e2
= (object[] a
) => a
;
2395 AssertNodeType (e2
, ExpressionType
.Parameter
);
2396 Assert (new object [0], e2
.Compile ().Invoke (new object [0]));
2399 void ParameterTest_3 ()
2401 Expression
<Func
<IntPtr
, IntPtr
>> e3
= a
=> a
;
2402 AssertNodeType (e3
, ExpressionType
.Parameter
);
2403 Assert (IntPtr
.Zero
, e3
.Compile ().Invoke (IntPtr
.Zero
));
2406 unsafe void ParameterTest_4 ()
2408 Expression
<Func
<int*[], int* []>> e4
= (a
) => a
;
2409 AssertNodeType (e4
, ExpressionType
.Parameter
);
2410 Assert
<int*[]> (null, e4
.Compile ().Invoke (null));
2411 int* e4_el
= stackalloc int [5];
2412 int*[] ptr
= new int*[] { e4_el }
;
2413 Assert
<int*[]> (ptr
, e4
.Compile ().Invoke (ptr
));
2418 Expression
<Func
<Expression
<Func
<int>>>> e
= () => () => 2;
2419 AssertNodeType (e
, ExpressionType
.Quote
);
2420 Assert (2, e
.Compile ().Invoke ().Compile ().Invoke ());
2425 Expression
<Func
<string, Expression
<Func
<string>>>> e
= (string s
) => () => s
;
2426 AssertNodeType (e
, ExpressionType
.Quote
);
2428 Assert ("data", e
.Compile ().Invoke ("data").Compile ().Invoke ());
2431 void RightShiftTest ()
2433 Expression
<Func
<ulong, short, ulong>> e
= (ulong a
, short b
) => a
>> b
;
2434 AssertNodeType (e
, ExpressionType
.RightShift
);
2435 Assert ((ulong) 0x1FD940L
, e
.Compile ().Invoke (0xFECA0000, 11));
2436 Assert ((ulong) 0x7FFFFFFF, e
.Compile ().Invoke (0xFFFFFFFF, 0xA01));
2438 // .net produces a strange result
2439 // see https://bugzilla.novell.com/show_bug.cgi?id=398358
2440 // Assert ((ulong)0, e.Compile ().Invoke (0xFFFFFFFF, 0xA01));
2443 void RightShiftTest_2 ()
2445 Expression
<Func
<MyType
, MyType
, int>> e2
= (MyType a
, MyType b
) => a
>> b
;
2446 AssertNodeType (e2
, ExpressionType
.RightShift
);
2447 var c2
= e2
.Compile ();
2448 Assert (64, c2 (new MyType (256), new MyType (2)));
2451 void RightShiftTest_3 ()
2453 Expression
<Func
<long?, sbyte, long?>> e3
= (long? a
, sbyte b
) => a
>> b
;
2454 AssertNodeType (e3
, ExpressionType
.RightShift
);
2455 Assert (null, e3
.Compile ().Invoke (null, 11));
2456 Assert (512, e3
.Compile ().Invoke (1024, 1));
2459 void RightShiftTest_4 ()
2461 Expression
<Func
<MyType
?, MyType
?, int?>> e4
= (MyType
? a
, MyType
? b
) => a
>> b
;
2462 AssertNodeType (e4
, ExpressionType
.RightShift
);
2463 var c4
= e4
.Compile ();
2464 Assert (null, c4 (new MyType (8), null));
2465 Assert (null, c4 (null, new MyType (8)));
2466 Assert (64, c4 (new MyType (256), new MyType (2)));
2469 void RightShiftTest_5 ()
2471 Expression
<Func
<int, MyTypeImplicitOnly
, int>> e
= (a
, b
) => a
>> b
;
2472 AssertNodeType (e
, ExpressionType
.RightShift
);
2473 Assert (31, e
.Compile ().Invoke (0xFE, new MyTypeImplicitOnly (3)));
2476 void SubtractTest ()
2478 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
- b
;
2479 AssertNodeType (e
, ExpressionType
.Subtract
);
2480 Assert (-10, e
.Compile ().Invoke (20, 30));
2483 void SubtractTest_2 ()
2485 Expression
<Func
<int?, int?, int?>> e2
= (a
, b
) => a
- b
;
2486 AssertNodeType (e2
, ExpressionType
.Subtract
);
2487 Assert (null, e2
.Compile ().Invoke (null, 3));
2490 void SubtractTest_3 ()
2492 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
- b
;
2493 AssertNodeType (e3
, ExpressionType
.Subtract
);
2494 Assert (-50, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2497 void SubtractTest_4 ()
2499 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
- b
;
2500 AssertNodeType (e4
, ExpressionType
.Subtract
);
2501 Assert (new MyType (-50), e4
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2502 Assert (null, e4
.Compile ().Invoke (null, new MyType (30)));
2505 void SubtractTest_5 ()
2507 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
- b
;
2508 AssertNodeType (e5
, ExpressionType
.Subtract
);
2509 Assert (-29, e5
.Compile ().Invoke (1, new MyType (30)));
2512 void SubtractTest_6 ()
2514 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
- b
;
2515 AssertNodeType (e6
, ExpressionType
.Subtract
);
2516 Assert (-61, e6
.Compile ().Invoke (-31, new MyType (30)));
2519 void SubtractTest_7 ()
2521 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => null - a
;
2522 AssertNodeType (e7
, ExpressionType
.Subtract
);
2523 Assert (null, e7
.Compile ().Invoke (690));
2526 void SubtractTest_8 ()
2528 Expression
<Func
<MyEnum
, byte, MyEnum
>> e8
= (a
, b
) => a
- b
;
2529 AssertNodeType (e8
, ExpressionType
.Convert
);
2530 Assert ((MyEnum
)255, e8
.Compile ().Invoke (MyEnum
.Value_1
, 2));
2533 void SubtractTest_9 ()
2535 Expression
<Func
<MyEnum
, MyEnum
, byte>> e9
= (a
, b
) => a
- b
;
2536 AssertNodeType (e9
, ExpressionType
.Convert
);
2537 Assert (1, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
2541 void SubtractTest_10 ()
2543 Expression
<Func
<MyEnum
?, byte?, MyEnum
?>> e10
= (a
, b
) => a
- b
;
2544 AssertNodeType (e10
, ExpressionType
.Convert
);
2545 Assert ((MyEnum
) 255, e10
.Compile ().Invoke (MyEnum
.Value_1
, 2));
2549 void SubtractTest_11 ()
2551 Expression
<Func
<MyEnum
?, MyEnum
?, byte?>> e11
= (a
, b
) => a
- b
;
2552 AssertNodeType (e11
, ExpressionType
.Convert
);
2553 Assert
<byte?> (1, e11
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
2556 void SubtractCheckedTest ()
2559 Expression
<Func
<long, long, long>> e
= (long a
, long b
) => a
- b
;
2560 AssertNodeType (e
, ExpressionType
.SubtractChecked
);
2562 e
.Compile ().Invoke (long.MinValue
, 309);
2563 throw new ApplicationException ("SubtractCheckedTest #1");
2564 } catch (OverflowException
) { }
2568 void SubtractCheckedTest_2 ()
2571 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a
- b
;
2572 AssertNodeType (e2
, ExpressionType
.SubtractChecked
);
2573 Assert (null, e2
.Compile ().Invoke (null, 3));
2574 Assert (-55, e2
.Compile ().Invoke (byte.MinValue
, 55));
2578 void SubtractCheckedTest_3 ()
2581 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
- b
;
2582 AssertNodeType (e3
, ExpressionType
.Subtract
);
2583 Assert (-50, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2587 void SubtractCheckedTest_4 ()
2590 Expression
<Func
<double, double, double>> e4
= (a
, b
) => a
- b
;
2591 AssertNodeType (e4
, ExpressionType
.Subtract
);
2592 Assert (double.PositiveInfinity
, e4
.Compile ().Invoke (double.MinValue
, double.NegativeInfinity
));
2598 Expression
<Func
<object, Tester
>> e
= (object a
) => a
as Tester
;
2599 AssertNodeType (e
, ExpressionType
.TypeAs
);
2600 Assert (this, e
.Compile ().Invoke (this));
2603 void TypeAsTest_2 ()
2605 Expression
<Func
<object, int?>> e2
= (object a
) => a
as int?;
2606 AssertNodeType (e2
, ExpressionType
.TypeAs
);
2607 Assert (null, e2
.Compile ().Invoke (null));
2608 Assert (null, e2
.Compile ().Invoke (this));
2609 Assert (44, e2
.Compile ().Invoke (44));
2612 void TypeAsTest_3 ()
2614 Expression
<Func
<object, object>> e3
= (object a
) => null as object;
2615 AssertNodeType (e3
, ExpressionType
.TypeAs
);
2616 Assert (null, e3
.Compile ().Invoke (null));
2621 Expression
<Func
<object, bool>> e
= (object a
) => a
is Tester
;
2622 AssertNodeType (e
, ExpressionType
.TypeIs
);
2623 Assert (true, e
.Compile ().Invoke (this));
2624 Assert (false, e
.Compile ().Invoke (1));
2627 void TypeIsTest_2 ()
2629 Expression
<Func
<object, bool>> e2
= (object a
) => a
is int?;
2630 AssertNodeType (e2
, ExpressionType
.TypeIs
);
2631 Assert (false, e2
.Compile ().Invoke (null));
2632 Assert (true, e2
.Compile ().Invoke (1));
2635 void TypeIsTest_3 ()
2637 Expression
<Func
<object, bool>> e3
= (object a
) => null is object;
2638 AssertNodeType (e3
, ExpressionType
.TypeIs
);
2639 Assert (false, e3
.Compile ().Invoke (null));
2642 void TypeIsTest_5 ()
2644 Expression
<Func
<bool>> e5
= () => 1 is int;
2645 AssertNodeType (e5
, ExpressionType
.TypeIs
);
2646 Assert (true, e5
.Compile ().Invoke ());
2649 void TypeIsTest_6 ()
2651 Expression
<Func
<int?, bool>> e6
= (a
) => a
is int;
2652 AssertNodeType (e6
, ExpressionType
.TypeIs
);
2653 Assert (true, e6
.Compile ().Invoke (1));
2654 Assert (false, e6
.Compile ().Invoke (null));
2657 void UnaryPlusTest ()
2659 Expression
<Func
<int, int>> e
= (a
) => +a
;
2660 AssertNodeType (e
, ExpressionType
.Parameter
);
2661 Assert (-30, e
.Compile ().Invoke (-30));
2664 void UnaryPlusTest_2 ()
2666 Expression
<Func
<long?, long?>> e2
= (a
) => +a
;
2667 AssertNodeType (e2
, ExpressionType
.Parameter
);
2670 void UnaryPlusTest_3 ()
2672 Expression
<Func
<MyType
, MyType
>> e4
= (a
) => +a
;
2673 AssertNodeType (e4
, ExpressionType
.UnaryPlus
);
2674 Assert (new MyType (-14), e4
.Compile ().Invoke (new MyType (-14)));
2677 void UnaryPlusTest_4 ()
2679 Expression
<Func
<MyType
?, MyType
?>> e5
= (a
) => +a
;
2680 AssertNodeType (e5
, ExpressionType
.UnaryPlus
);
2681 Assert (new MyType (33), e5
.Compile ().Invoke (new MyType (33)));
2682 Assert (null, e5
.Compile ().Invoke (null));
2685 void UnaryPlusTest_5 ()
2687 Expression
<Func
<sbyte?, long?>> e6
= (a
) => +a
;
2688 AssertNodeType (e6
, ExpressionType
.Convert
);
2689 Assert (3, e6
.Compile ().Invoke (3));
2690 Assert (null, e6
.Compile ().Invoke (null));
2693 #pragma warning restore 169
2698 string InstanceMethod (string arg
)
2703 object InstanceParamsMethod (int index
, params object [] args
)
2707 if (args
.Length
== 0)
2709 return args
[index
];
2712 static int TestInt ()
2717 T GenericMethod
<T
> (T t
)
2722 static void RefMethod (ref int i
)
2727 static bool RunTest (MethodInfo test
)
2729 Console
.Write ("Running test {0, -25}", test
.Name
);
2731 test
.Invoke (new Tester (), null);
2732 Console
.WriteLine ("OK");
2734 } catch (Exception e
) {
2735 Console
.WriteLine ("FAILED");
2736 Console
.WriteLine (e
.ToString ());
2741 public static int Main ()
2743 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
2744 where test
.GetParameters ().Length
== 0
2746 select RunTest (test
);
2748 int failures
= tests
.Count (a
=> !a
);
2749 Console
.WriteLine (failures
+ " tests failed");