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 ArrayLengthTest ()
611 Expression
<Func
<double [], int>> e
= (double [] a
) => a
.Length
;
612 AssertNodeType (e
, ExpressionType
.ArrayLength
);
613 Assert (0, e
.Compile ().Invoke (new double [0]));
614 Assert (9, e
.Compile ().Invoke (new double [9]));
617 void ArrayLengthTest_2 ()
619 Expression
<Func
<string [,], int>> e2
= (string [,] a
) => a
.Length
;
620 AssertNodeType (e2
, ExpressionType
.MemberAccess
);
621 Assert (0, e2
.Compile ().Invoke (new string [0, 0]));
626 Expression
<Func
<int, int>> e
= (int a
) => Math
.Max (a
, 5);
627 AssertNodeType (e
, ExpressionType
.Call
);
628 Assert (5, e
.Compile ().Invoke (2));
629 Assert (9, e
.Compile ().Invoke (9));
634 Expression
<Func
<string, string>> e2
= (string a
) => InstanceMethod (a
);
635 AssertNodeType (e2
, ExpressionType
.Call
);
636 Assert ("abc", e2
.Compile ().Invoke ("abc"));
641 Expression
<Func
<int, string, int, object>> e3
= (int index
, string a
, int b
) => InstanceParamsMethod (index
, a
, b
);
642 AssertNodeType (e3
, ExpressionType
.Call
);
643 Assert
<object> (4, e3
.Compile ().Invoke (1, "a", 4));
648 Expression
<Func
<object>> e4
= () => InstanceParamsMethod (0);
649 AssertNodeType (e4
, ExpressionType
.Call
);
650 Assert
<object> ("<empty>", e4
.Compile ().Invoke ());
655 Expression
<Func
<int, int>> e5
= (int a
) => GenericMethod (a
);
656 AssertNodeType (e5
, ExpressionType
.Call
);
657 Assert (5, e5
.Compile ().Invoke (5));
662 Expression
<Action
> e6
= () => Console
.WriteLine ("call test");
663 AssertNodeType (e6
, ExpressionType
.Call
);
668 Expression
<Func
<Indexer
, int, int>> e7
= (a
, b
) => a
[b
];
669 AssertNodeType (e7
, ExpressionType
.Call
);
670 Assert (3, e7
.Compile ().Invoke (new Indexer (), 3));
675 Expression
<Func
<Indexer
, string, string, string, string>> e8
= (a
, b
, c
, d
) => a
[b
, c
, d
];
676 AssertNodeType (e8
, ExpressionType
.Call
);
677 Assert ("zyb", e8
.Compile ().Invoke (new Indexer (), "z", "y", "b"));
682 Expression
<Action
<int>> e9
= (a
) => RefMethod (ref a
);
683 AssertNodeType (e9
, ExpressionType
.Call
);
684 e9
.Compile ().Invoke (1);
689 Expression
<Func
<uint?, uint>> e
= (uint? a
) => a
?? 99;
690 AssertNodeType (e
, ExpressionType
.Coalesce
);
691 var r
= e
.Compile ();
692 Assert ((uint) 5, r
.Invoke (5));
693 Assert ((uint) 99, r
.Invoke (null));
696 void CoalesceTest_2 ()
698 Expression
<Func
<MyType
?, int>> e2
= (MyType
? a
) => a
?? -3;
699 AssertNodeType (e2
, ExpressionType
.Coalesce
);
700 var r2
= e2
.Compile ();
701 Assert (2, r2
.Invoke (new MyType (2)));
702 Assert (-3, r2
.Invoke (null));
705 void ConditionTest ()
707 Expression
<Func
<bool, byte, int, int>> e
= (bool a
, byte b
, int c
) => (a
? b
: c
);
708 AssertNodeType (e
, ExpressionType
.Conditional
);
709 var r
= e
.Compile ();
710 Assert (3, r
.Invoke (true, 3, 999999));
711 Assert (999999, r
.Invoke (false, 3, 999999));
714 void ConditionTest_2 ()
716 Expression
<Func
<int, decimal, decimal?>> e2
= (int a
, decimal d
) => (a
> 0 ? d
: a
< 0 ? -d
: (decimal?) null);
717 AssertNodeType (e2
, ExpressionType
.Conditional
);
718 var r2
= e2
.Compile ();
719 Assert (null, r2
.Invoke (0, 10));
720 Assert (50, r2
.Invoke (1, 50));
721 Assert (30, r2
.Invoke (-7, -30));
724 void ConditionTest_3 ()
726 Expression
<Func
<bool?, int?>> e3
= (bool? a
) => ((bool) a
? 3 : -2);
727 AssertNodeType (e3
, ExpressionType
.Convert
);
728 var r3
= e3
.Compile ();
729 Assert (3, r3
.Invoke (true));
730 Assert (-2, r3
.Invoke (false));
733 void ConditionTest_4 ()
735 Expression
<Func
<InverseLogicalOperator
, byte, byte, byte>> e4
= (InverseLogicalOperator a
, byte b
, byte c
) => (a
? b
: c
);
736 AssertNodeType (e4
, ExpressionType
.Conditional
);
737 var r4
= e4
.Compile ();
738 Assert (3, r4
.Invoke (new InverseLogicalOperator (true), 3, 4));
739 Assert (4, r4
.Invoke (new InverseLogicalOperator (false), 3, 4));
742 void ConditionTest_5 ()
745 Expression
<Func
<int>> e
= () => false ? 1 : 4;
746 AssertNodeType (e
, ExpressionType
.Conditional
);
747 var r
= e
.Compile ();
748 Assert (4, r
.Invoke ());
753 Expression
<Func
<int>> e1
= () => default (int);
754 AssertNodeType (e1
, ExpressionType
.Constant
);
755 Assert (0, e1
.Compile ().Invoke ());
758 void ConstantTest_2 ()
760 Expression
<Func
<int?>> e2
= () => default (int?);
761 AssertNodeType (e2
, ExpressionType
.Constant
);
762 Assert (null, e2
.Compile ().Invoke ());
765 // FIXME: Redundant convert, need new expression
766 void ConstantTest_3 ()
768 Expression
<Func
<Tester
>> e3
= () => default (Tester
);
769 //AssertNodeType (e3, ExpressionType.Constant);
770 Assert (null, e3
.Compile ().Invoke ());
773 void ConstantTest_4 ()
775 Expression
<Func
<object>> e4
= () => null;
776 AssertNodeType (e4
, ExpressionType
.Constant
);
777 Assert (null, e4
.Compile ().Invoke ());
780 void ConstantTest_5 ()
782 Expression
<Func
<int>> e5
= () => 8 / 4;
783 AssertNodeType (e5
, ExpressionType
.Constant
);
784 Assert (2, e5
.Compile ().Invoke ());
787 void ConstantTest_6 ()
789 Expression
<Func
<int>> e6
= () => 0xFFFFFF >> 0x40;
790 AssertNodeType (e6
, ExpressionType
.Constant
);
791 Assert (0xFFFFFF, e6
.Compile ().Invoke ());
794 void ConstantTest_7 ()
796 Expression
<Func
<object>> e7
= () => "Alleluia";
797 AssertNodeType (e7
, ExpressionType
.Constant
);
798 Assert ("Alleluia", e7
.Compile ().Invoke ());
801 void ConstantTest_8 ()
803 Expression
<Func
<Type
>> e8
= () => typeof (int);
804 AssertNodeType (e8
, ExpressionType
.Constant
);
805 Assert (typeof (int), e8
.Compile ().Invoke ());
808 void ConstantTest_9 ()
810 Expression
<Func
<Type
>> e9
= () => typeof (void);
811 AssertNodeType (e9
, ExpressionType
.Constant
);
812 Assert (typeof (void), e9
.Compile ().Invoke ());
815 void ConstantTest_10 ()
817 Expression
<Func
<Type
>> e10
= () => typeof (Func
<,>);
818 AssertNodeType (e10
, ExpressionType
.Constant
);
819 Assert (typeof (Func
<,>), e10
.Compile ().Invoke ());
822 void ConstantTest_11 ()
824 Expression
<Func
<MyEnum
>> e11
= () => MyEnum
.Value_2
;
825 AssertNodeType (e11
, ExpressionType
.Constant
);
826 Assert (MyEnum
.Value_2
, e11
.Compile ().Invoke ());
829 void ConstantTest_13 ()
831 Expression
<Func
<int>> e13
= () => sizeof (byte);
832 AssertNodeType (e13
, ExpressionType
.Constant
);
833 Assert (1, e13
.Compile ().Invoke ());
837 unsafe void ConstantTest_14 ()
839 Expression
<Func
<Type
>> e14
= () => typeof (bool*);
840 AssertNodeType (e14
, ExpressionType
.Constant
);
841 Assert (typeof (bool*), e14
.Compile ().Invoke ());
844 void ConstantTest_15 ()
846 Expression
<Func
<int?>> e15
= () => null;
847 AssertNodeType (e15
, ExpressionType
.Constant
);
848 Assert (null, e15
.Compile ().Invoke ());
853 Expression
<Func
<int, byte>> e
= (int a
) => ((byte) a
);
854 AssertNodeType (e
, ExpressionType
.Convert
);
855 Assert (100, e
.Compile ().Invoke (100));
858 void ConvertTest_2 ()
860 Expression
<Func
<long, ushort>> e2
= (long a
) => ((ushort) a
);
861 AssertNodeType (e2
, ExpressionType
.Convert
);
862 Assert (100, e2
.Compile ().Invoke (100));
865 void ConvertTest_3 ()
867 Expression
<Func
<float?, float>> e3
= (float? a
) => ((float) a
);
868 AssertNodeType (e3
, ExpressionType
.Convert
);
869 Assert (-0.456f
, e3
.Compile ().Invoke (-0.456f
));
872 void ConvertTest_4 ()
874 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => (a
);
875 AssertNodeType (e4
, ExpressionType
.Convert
);
876 Assert (-9, e4
.Compile ().Invoke (new MyType (-9)));
879 void ConvertTest_5 ()
881 Expression
<Func
<MyType
, MyType
, bool?>> e5
= (MyType a
, MyType b
) => a
== b
;
882 AssertNodeType (e5
, ExpressionType
.Convert
);
885 void ConvertTest_6 ()
887 Expression
<Func
<MyType
?, MyType
?, bool?>> e6
= (MyType
? a
, MyType
? b
) => a
== b
;
888 AssertNodeType (e6
, ExpressionType
.Convert
);
889 Assert (false, e6
.Compile ().Invoke (null, new MyType (-20)));
890 Assert (true, e6
.Compile ().Invoke (null, null));
891 Assert (true, e6
.Compile ().Invoke (new MyType (120), new MyType (120)));
894 void ConvertTest_7 ()
896 Expression
<Func
<MyTypeExplicit
, int?>> e7
= x
=> (int?)x
;
897 AssertNodeType (e7
, ExpressionType
.Convert
);
898 Assert (33, e7
.Compile ().Invoke (new MyTypeExplicit (33)));
901 void ConvertTest_8 ()
903 Expression
<Func
<int?, object>> e8
= x
=> (object)x
;
904 AssertNodeType (e8
, ExpressionType
.Convert
);
905 Assert (null, e8
.Compile ().Invoke (null));
906 Assert (-100, e8
.Compile ().Invoke (-100));
909 unsafe void ConvertTest_9 ()
911 int*[] p
= new int* [1];
912 Expression
<Func
<object>> e9
= () => (object)p
;
913 AssertNodeType (e9
, ExpressionType
.Convert
);
914 Assert (p
, e9
.Compile ().Invoke ());
917 void ConvertTest_10 ()
919 Expression
<Func
<Func
<int>, Delegate
>> e10
= (a
) => a
+ a
;
920 AssertNodeType (e10
, ExpressionType
.Convert
);
921 Assert (null, e10
.Compile ().Invoke (null));
922 Assert (new Func
<int> (TestInt
) + new Func
<int> (TestInt
), e10
.Compile ().Invoke (TestInt
));
925 void ConvertTest_11 ()
927 Expression
<Func
<Func
<int>, Delegate
>> e11
= (a
) => a
- a
;
928 AssertNodeType (e11
, ExpressionType
.Convert
);
929 Assert (null, e11
.Compile ().Invoke (null));
932 void ConvertTest_12 ()
934 Expression
<Func
<Func
<int>>> e12
= () => TestInt
;
935 AssertNodeType (e12
, ExpressionType
.Convert
);
936 Assert (29, e12
.Compile ().Invoke () ());
939 void ConvertTest_13 ()
941 Expression
<Func
<decimal, sbyte>> e13
= a
=> (sbyte)a
;
942 AssertNodeType (e13
, ExpressionType
.Convert
);
943 Assert (6, e13
.Compile ().Invoke (6));
946 void ConvertTest_14 ()
948 Expression
<Func
<long, decimal>> e14
= a
=> a
;
949 AssertNodeType (e14
, ExpressionType
.Convert
);
950 Assert (-66, e14
.Compile ().Invoke (-66));
953 void ConvertTest_15 ()
955 Expression
<Func
<ulong?, decimal?>> e15
= a
=> a
;
956 AssertNodeType (e15
, ExpressionType
.Convert
);
957 Assert (null, e15
.Compile ().Invoke (null));
958 Assert (9, e15
.Compile ().Invoke (9));
961 void ConvertCheckedTest ()
963 Expression
<Func
<int, byte>> e
= (int a
) => checked((byte) a
);
964 AssertNodeType (e
, ExpressionType
.ConvertChecked
);
965 Assert (100, e
.Compile ().Invoke (100));
968 void ConvertCheckedTest_2 ()
971 Expression
<Func
<long, ushort>> e2
= (long a
) => unchecked((ushort) a
);
972 AssertNodeType (e2
, ExpressionType
.Convert
);
973 Assert (100, e2
.Compile ().Invoke (100));
977 void ConvertCheckedTest_3 ()
980 Expression
<Func
<float?, float>> e3
= (float? a
) => ((float) a
);
981 AssertNodeType (e3
, ExpressionType
.ConvertChecked
);
982 Assert (-0.456f
, e3
.Compile ().Invoke (-0.456f
));
986 void ConvertCheckedTest_4 ()
989 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => (a
);
990 AssertNodeType (e4
, ExpressionType
.Convert
);
991 Assert (-9, e4
.Compile ().Invoke (new MyType (-9)));
997 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
/ b
;
998 AssertNodeType (e
, ExpressionType
.Divide
);
999 Assert (2, e
.Compile ().Invoke (60, 30));
1002 void DivideTest_2 ()
1004 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
/ b
;
1005 AssertNodeType (e2
, ExpressionType
.Divide
);
1006 Assert (null, e2
.Compile ().Invoke (null, 3));
1007 Assert (1.5, e2
.Compile ().Invoke (3, 2));
1010 void DivideTest_3 ()
1012 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
/ b
;
1013 AssertNodeType (e3
, ExpressionType
.Divide
);
1014 Assert (1, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1017 void DivideTest_4 ()
1019 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
/ b
;
1020 AssertNodeType (e4
, ExpressionType
.Divide
);
1021 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1022 Assert (new MyType (-6), e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1025 void DivideTest_5 ()
1027 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
/ b
;
1028 AssertNodeType (e5
, ExpressionType
.Divide
);
1029 Assert (50, e5
.Compile ().Invoke (100, new MyType (2)));
1032 void DivideTest_6 ()
1034 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
/ b
;
1035 AssertNodeType (e6
, ExpressionType
.Divide
);
1036 Assert (50, e6
.Compile ().Invoke (100, new MyType (2)));
1037 Assert (null, e6
.Compile ().Invoke (20, null));
1042 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
== b
;
1043 AssertNodeType (e
, ExpressionType
.Equal
);
1044 Assert (false, e
.Compile ().Invoke (60, 30));
1045 Assert (true, e
.Compile ().Invoke (-1, -1));
1050 Expression
<Func
<double?, double?, bool>> e2
= (a
, b
) => a
== b
;
1051 AssertNodeType (e2
, ExpressionType
.Equal
);
1052 Assert (true, e2
.Compile ().Invoke (3, 3));
1053 Assert (false, e2
.Compile ().Invoke (3, 2));
1058 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
== b
;
1059 AssertNodeType (e3
, ExpressionType
.Equal
);
1060 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1065 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
== b
;
1066 AssertNodeType (e4
, ExpressionType
.Equal
);
1067 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1068 Assert (true, e4
.Compile ().Invoke (null, null));
1069 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (120)));
1074 Expression
<Func
<bool?, bool?, bool>> e5
= (bool? a
, bool? b
) => a
== b
;
1075 AssertNodeType (e5
, ExpressionType
.Equal
);
1076 Assert (false, e5
.Compile ().Invoke (true, null));
1077 Assert (true, e5
.Compile ().Invoke (null, null));
1078 Assert (true, e5
.Compile ().Invoke (false, false));
1083 Expression
<Func
<bool, bool>> e6
= (bool a
) => a
== null;
1084 AssertNodeType (e6
, ExpressionType
.Equal
);
1085 Assert (false, e6
.Compile ().Invoke (true));
1086 Assert (false, e6
.Compile ().Invoke (false));
1091 Expression
<Func
<string, string, bool>> e7
= (string a
, string b
) => a
== b
;
1092 AssertNodeType (e7
, ExpressionType
.Equal
);
1093 Assert (true, e7
.Compile ().Invoke (null, null));
1094 Assert (false, e7
.Compile ().Invoke ("a", "A"));
1095 Assert (true, e7
.Compile ().Invoke ("a", "a"));
1100 Expression
<Func
<object, bool>> e8
= (object a
) => null == a
;
1101 AssertNodeType (e8
, ExpressionType
.Equal
);
1102 Assert (true, e8
.Compile ().Invoke (null));
1103 Assert (false, e8
.Compile ().Invoke ("a"));
1104 Assert (false, e8
.Compile ().Invoke (this));
1109 Expression
<Func
<MyEnum
, MyEnum
, bool>> e9
= (a
, b
) => a
== b
;
1110 AssertNodeType (e9
, ExpressionType
.Equal
);
1111 Assert (false, e9
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
1112 Assert (true, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1115 void EqualTest_10 ()
1117 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e10
= (a
, b
) => a
== b
;
1118 AssertNodeType (e10
, ExpressionType
.Equal
);
1119 Assert (false, e10
.Compile ().Invoke (MyEnum
.Value_1
, null));
1120 Assert (true, e10
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1123 void EqualTest_11 ()
1125 Expression
<Func
<MyEnum
?, bool>> e11
= (a
) => a
== null;
1126 AssertNodeType (e11
, ExpressionType
.Equal
);
1127 Assert (false, e11
.Compile ().Invoke (MyEnum
.Value_1
));
1128 Assert (true, e11
.Compile ().Invoke (null));
1131 void EqualTest_12 ()
1133 Expression
<Func
<MyEnumUlong
, bool>> e12
= (a
) => a
== 0;
1134 AssertNodeType (e12
, ExpressionType
.Equal
);
1135 Assert (false, e12
.Compile ().Invoke (MyEnumUlong
.Value_1
));
1136 Assert (true, e12
.Compile ().Invoke (0));
1139 void EqualTest_13 ()
1141 Expression
<Func
<MyEnum
, bool>> e13
= (a
) => a
== MyEnum
.Value_2
;
1142 AssertNodeType (e13
, ExpressionType
.Equal
);
1143 Assert (true, e13
.Compile ().Invoke (MyEnum
.Value_2
));
1144 Assert (false, e13
.Compile ().Invoke (0));
1147 void EqualTestDelegate ()
1149 Expression
<Func
<Delegate
, Delegate
, bool>> e1
= (a
, b
) => a
== b
;
1150 AssertNodeType (e1
, ExpressionType
.Equal
);
1151 Assert (true, e1
.Compile ().Invoke (null, null));
1154 void EqualTestDelegate_2 ()
1156 EmptyDelegate ed
= delegate () {};
1158 Expression
<Func
<EmptyDelegate
, EmptyDelegate
, bool>> e2
= (a
, b
) => a
== b
;
1159 AssertNodeType (e2
, ExpressionType
.Equal
);
1160 Assert (false, e2
.Compile ().Invoke (delegate () {}, null));
1161 Assert (false, e2
.Compile ().Invoke (delegate () {}, delegate {}
));
1162 Assert (false, e2
.Compile ().Invoke (ed
, delegate {}));
1163 Assert (true, e2
.Compile ().Invoke (ed
, ed
));
1166 void ExclusiveOrTest ()
1168 Expression
<Func
<int, short, int>> e
= (int a
, short b
) => a ^ b
;
1169 AssertNodeType (e
, ExpressionType
.ExclusiveOr
);
1170 Assert (34, e
.Compile ().Invoke (60, 30));
1173 void ExclusiveOrTest_2 ()
1175 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a ^ b
;
1176 AssertNodeType (e2
, ExpressionType
.ExclusiveOr
);
1177 Assert (null, e2
.Compile ().Invoke (null, 3));
1178 Assert (1, e2
.Compile ().Invoke (3, 2));
1181 void ExclusiveOrTest_3 ()
1183 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a ^ b
;
1184 AssertNodeType (e3
, ExpressionType
.ExclusiveOr
);
1185 Assert (0, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1188 void ExclusiveOrTest_4 ()
1190 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a ^ b
;
1191 AssertNodeType (e4
, ExpressionType
.ExclusiveOr
);
1192 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1193 Assert (new MyType (-108), e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1196 void ExclusiveOrTest_5 ()
1198 Expression
<Func
<MyType
?, byte, int?>> e5
= (MyType
? a
, byte b
) => a ^ b
;
1199 AssertNodeType (e5
, ExpressionType
.ExclusiveOr
);
1200 Assert (null, e5
.Compile ().Invoke (null, 64));
1201 Assert (96, e5
.Compile ().Invoke (new MyType (64), 32));
1204 void ExclusiveOrTest_6 ()
1206 Expression
<Func
<MyEnum
, MyEnum
, MyEnum
>> e6
= (a
, b
) => a ^ b
;
1207 AssertNodeType (e6
, ExpressionType
.Convert
);
1208 Assert ((MyEnum
)3, e6
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
1209 Assert
<MyEnum
> (0, e6
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1212 void ExclusiveOrTest_7 ()
1214 Expression
<Func
<MyEnum
?, MyEnum
?, MyEnum
?>> e7
= (a
, b
) => a ^ b
;
1215 AssertNodeType (e7
, ExpressionType
.Convert
);
1216 Assert (null, e7
.Compile ().Invoke (MyEnum
.Value_1
, null));
1217 Assert
<MyEnum
?> (0, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1220 void ExclusiveOrTest_8 ()
1222 Expression
<Func
<MyEnum
?, MyEnum
?>> e8
= (a
) => a ^
null;
1223 AssertNodeType (e8
, ExpressionType
.Convert
);
1224 Assert (null, e8
.Compile ().Invoke (MyEnum
.Value_1
));
1225 Assert (null, e8
.Compile ().Invoke (null));
1228 void GreaterThanTest ()
1230 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
> b
;
1231 AssertNodeType (e
, ExpressionType
.GreaterThan
);
1232 Assert (true, e
.Compile ().Invoke (60, 30));
1235 void GreaterThanTest_2 ()
1237 Expression
<Func
<uint?, byte?, bool>> e2
= (a
, b
) => a
> b
;
1238 AssertNodeType (e2
, ExpressionType
.GreaterThan
);
1239 Assert (false, e2
.Compile ().Invoke (null, 3));
1240 Assert (false, e2
.Compile ().Invoke (2, 2));
1243 void GreaterThanTest_3 ()
1245 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
> b
;
1246 AssertNodeType (e3
, ExpressionType
.GreaterThan
);
1247 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1250 void GreaterThanTest_4 ()
1252 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
> b
;
1253 AssertNodeType (e4
, ExpressionType
.GreaterThan
);
1254 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1255 Assert (false, e4
.Compile ().Invoke (null, null));
1256 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1259 void GreaterThanTest_5 ()
1261 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
> b
;
1262 AssertNodeType (e5
, ExpressionType
.GreaterThan
);
1263 Assert (false, e5
.Compile ().Invoke (null, 33));
1264 Assert (false, e5
.Compile ().Invoke (null, 0));
1265 Assert (true, e5
.Compile ().Invoke (new MyType (120), 3));
1268 void GreaterThanTest_6 ()
1270 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
> null;
1271 AssertNodeType (e6
, ExpressionType
.GreaterThan
);
1272 Assert (false, e6
.Compile ().Invoke (60));
1275 void GreaterThanTest_7 ()
1277 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
> b
;
1278 AssertNodeType (e7
, ExpressionType
.GreaterThan
);
1279 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1280 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1283 void GreaterThanTest_8 ()
1285 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
> b
;
1286 AssertNodeType (e8
, ExpressionType
.GreaterThan
);
1287 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1288 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1291 void GreaterThanOrEqualTest ()
1293 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
>= b
;
1294 AssertNodeType (e
, ExpressionType
.GreaterThanOrEqual
);
1295 Assert (true, e
.Compile ().Invoke (60, 30));
1298 void GreaterThanOrEqualTest_2 ()
1300 Expression
<Func
<byte?, byte?, bool>> e2
= (a
, b
) => a
>= b
;
1301 AssertNodeType (e2
, ExpressionType
.GreaterThanOrEqual
);
1302 Assert (false, e2
.Compile ().Invoke (null, 3));
1303 Assert (true, e2
.Compile ().Invoke (2, 2));
1306 void GreaterThanOrEqualTest_3 ()
1308 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
>= b
;
1309 AssertNodeType (e3
, ExpressionType
.GreaterThanOrEqual
);
1310 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)), "D1");
1313 void GreaterThanOrEqualTest_4 ()
1315 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
>= b
;
1316 AssertNodeType (e4
, ExpressionType
.GreaterThanOrEqual
);
1317 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1318 Assert (false, e4
.Compile ().Invoke (null, null));
1319 Assert (true, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1322 void GreaterThanOrEqualTest_5 ()
1324 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
>= b
;
1325 AssertNodeType (e5
, ExpressionType
.GreaterThanOrEqual
);
1326 Assert (false, e5
.Compile ().Invoke (null, 33));
1327 Assert (false, e5
.Compile ().Invoke (null, 0));
1328 Assert (true, e5
.Compile ().Invoke (new MyType (120), 3));
1331 void GreaterThanOrEqualTest_6 ()
1333 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
>= null;
1334 AssertNodeType (e6
, ExpressionType
.GreaterThanOrEqual
);
1335 Assert (false, e6
.Compile ().Invoke (60));
1338 void GreaterThanOrEqualTest_7 ()
1340 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
>= b
;
1341 AssertNodeType (e7
, ExpressionType
.GreaterThanOrEqual
);
1342 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1343 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1346 void GreaterThanOrEqualTest_8 ()
1348 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
>= b
;
1349 AssertNodeType (e8
, ExpressionType
.GreaterThanOrEqual
);
1350 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1351 Assert (true, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1356 var del
= new IntDelegate (TestInt
);
1357 Expression
<Func
<IntDelegate
, int>> e
= (a
) => a ();
1358 AssertNodeType (e
, ExpressionType
.Invoke
);
1359 Assert (29, e
.Compile ().Invoke (del
));
1362 void InvokeTest_2 ()
1364 Expression
<Func
<Func
<int, string>, int, string>> e2
= (a
, b
) => a (b
);
1365 AssertNodeType (e2
, ExpressionType
.Invoke
);
1366 Assert ("4", e2
.Compile ().Invoke ((a
) => (a
+1).ToString (), 3));
1369 void LeftShiftTest ()
1371 Expression
<Func
<ulong, short, ulong>> e
= (ulong a
, short b
) => a
<< b
;
1372 AssertNodeType (e
, ExpressionType
.LeftShift
);
1373 Assert ((ulong) 0x7F000, e
.Compile ().Invoke (0xFE, 11));
1374 Assert ((ulong) 0x1FFFFFFFE, e
.Compile ().Invoke (0xFFFFFFFF, 0xA01));
1376 // .net produces a strange result
1377 // see https://bugzilla.novell.com/show_bug.cgi?id=398358
1378 // Assert ((ulong) 0xFFFFFFFE00000000, e.Compile ().Invoke (0xFFFFFFFF, 0xA01));
1381 void LeftShiftTest_2 ()
1383 Expression
<Func
<MyType
, MyType
, int>> e2
= (MyType a
, MyType b
) => a
<< b
;
1384 AssertNodeType (e2
, ExpressionType
.LeftShift
);
1385 var c2
= e2
.Compile ();
1386 Assert (1024, c2 (new MyType (256), new MyType (2)));
1389 void LeftShiftTest_3 ()
1391 Expression
<Func
<long?, sbyte, long?>> e3
= (long? a
, sbyte b
) => a
<< b
;
1392 AssertNodeType (e3
, ExpressionType
.LeftShift
);
1393 Assert (null, e3
.Compile ().Invoke (null, 11));
1394 Assert (2048, e3
.Compile ().Invoke (1024, 1));
1397 void LeftShiftTest_4 ()
1399 Expression
<Func
<MyType
?, MyType
?, int?>> e4
= (MyType
? a
, MyType
? b
) => a
<< b
;
1400 AssertNodeType (e4
, ExpressionType
.LeftShift
);
1401 var c4
= e4
.Compile ();
1402 Assert (null, c4 (new MyType (8), null));
1403 Assert (null, c4 (null, new MyType (8)));
1404 Assert (1024, c4 (new MyType (256), new MyType (2)));
1407 void LeftShiftTest_5 ()
1409 Expression
<Func
<ushort, int?>> e5
= (ushort a
) => a
<< null;
1410 AssertNodeType (e5
, ExpressionType
.LeftShift
);
1411 Assert (null, e5
.Compile ().Invoke (30));
1414 void LessThanTest ()
1416 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
< b
;
1417 AssertNodeType (e
, ExpressionType
.LessThan
);
1418 Assert (false, e
.Compile ().Invoke (60, 30));
1421 void LessThanTest_2 ()
1423 Expression
<Func
<uint?, byte?, bool>> e2
= (a
, b
) => a
< b
;
1424 AssertNodeType (e2
, ExpressionType
.LessThan
);
1425 Assert (false, e2
.Compile ().Invoke (null, 3));
1426 Assert (false, e2
.Compile ().Invoke (2, 2));
1429 void LessThanTest_3 ()
1431 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
< b
;
1432 AssertNodeType (e3
, ExpressionType
.LessThan
);
1433 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1436 void LessThanTest_4 ()
1438 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
< b
;
1439 AssertNodeType (e4
, ExpressionType
.LessThan
);
1440 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1441 Assert (false, e4
.Compile ().Invoke (null, null));
1442 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1445 void LessThanTest_5 ()
1447 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
< b
;
1448 AssertNodeType (e5
, ExpressionType
.LessThan
);
1449 Assert (false, e5
.Compile ().Invoke (null, 33));
1450 Assert (false, e5
.Compile ().Invoke (null, 0));
1451 Assert (false, e5
.Compile ().Invoke (new MyType (120), 3));
1454 void LessThanTest_6 ()
1456 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
< null;
1457 AssertNodeType (e6
, ExpressionType
.LessThan
);
1458 Assert (false, e6
.Compile ().Invoke (60));
1461 void LessThanTest_7 ()
1463 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
< b
;
1464 AssertNodeType (e7
, ExpressionType
.LessThan
);
1465 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1466 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1469 void LessThanTest_8 ()
1471 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
< b
;
1472 AssertNodeType (e8
, ExpressionType
.LessThan
);
1473 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1474 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1477 void LessThanOrEqualTest ()
1479 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
<= b
;
1480 AssertNodeType (e
, ExpressionType
.LessThanOrEqual
);
1481 Assert (false, e
.Compile ().Invoke (60, 30));
1484 void LessThanOrEqualTest_2 ()
1486 Expression
<Func
<byte?, byte?, bool>> e2
= (a
, b
) => a
<= b
;
1487 AssertNodeType (e2
, ExpressionType
.LessThanOrEqual
);
1488 Assert (false, e2
.Compile ().Invoke (null, 3));
1489 Assert (true, e2
.Compile ().Invoke (2, 2));
1492 void LessThanOrEqualTest_3 ()
1494 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
<= b
;
1495 AssertNodeType (e3
, ExpressionType
.LessThanOrEqual
);
1496 Assert (true, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1499 void LessThanOrEqualTest_4 ()
1501 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
<= b
;
1502 AssertNodeType (e4
, ExpressionType
.LessThanOrEqual
);
1503 Assert (false, e4
.Compile ().Invoke (null, new MyType (-20)));
1504 Assert (false, e4
.Compile ().Invoke (null, null));
1505 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (-20)));
1508 void LessThanOrEqualTest_5 ()
1510 Expression
<Func
<MyType
?, sbyte, bool>> e5
= (MyType
? a
, sbyte b
) => a
<= b
;
1511 AssertNodeType (e5
, ExpressionType
.LessThanOrEqual
);
1512 Assert (false, e5
.Compile ().Invoke (null, 33));
1513 Assert (false, e5
.Compile ().Invoke (null, 0));
1514 Assert (false, e5
.Compile ().Invoke (new MyType (120), 3));
1517 void LessThanOrEqualTest_6 ()
1519 Expression
<Func
<ushort, bool>> e6
= (ushort a
) => a
<= null;
1520 AssertNodeType (e6
, ExpressionType
.LessThanOrEqual
);
1521 Assert (false, e6
.Compile ().Invoke (60));
1524 void LessThanOrEqualTest_7 ()
1526 Expression
<Func
<MyEnum
, MyEnum
, bool>> e7
= (a
, b
) => a
<= b
;
1527 AssertNodeType (e7
, ExpressionType
.LessThanOrEqual
);
1528 Assert (false, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
1529 Assert (true, e7
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1532 void LessThanOrEqualTest_8 ()
1534 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e8
= (a
, b
) => a
<= b
;
1535 AssertNodeType (e8
, ExpressionType
.LessThanOrEqual
);
1536 Assert (false, e8
.Compile ().Invoke (MyEnum
.Value_1
, null));
1537 Assert (true, e8
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
1540 void ListInitTest ()
1542 Expression
<Func
<List
<object>>> e1
= () => new List
<object> { "Hello", "", null, "World", 5 }
;
1543 AssertNodeType (e1
, ExpressionType
.ListInit
);
1544 var re1
= e1
.Compile ().Invoke ();
1545 Assert (null, re1
[2]);
1546 Assert ("World", re1
[3]);
1547 Assert (5, re1
[4]);
1550 void ListInitTest_2 ()
1552 Expression
<Func
<int, Dictionary
<string, int>>> e2
= (int value) => new Dictionary
<string, int> (3) { { "A", value }
, { "B", 2 }
};
1553 AssertNodeType (e2
, ExpressionType
.ListInit
);
1554 var re2
= e2
.Compile ().Invoke (3456);
1555 Assert (3456, re2
["A"]);
1558 void MemberAccessTest ()
1560 MemberAccessData d
= new MemberAccessData ();
1562 Expression
<Func
<bool>> e
= () => d
.BoolValue
;
1563 AssertNodeType (e
, ExpressionType
.MemberAccess
);
1564 Assert (true, e
.Compile ().Invoke ());
1565 d
.BoolValue
= false;
1566 Assert (false, e
.Compile ().Invoke ());
1569 void MemberAccessTest_2 ()
1571 Expression
<Func
<decimal>> e2
= () => MemberAccessData
.DecimalValue
;
1572 AssertNodeType (e2
, ExpressionType
.MemberAccess
);
1573 Assert (decimal.MinValue
, e2
.Compile ().Invoke ());
1576 void MemberAccessTest_3 ()
1578 MemberAccessData d
= new MemberAccessData ();
1579 d
.VolatileValue
= 492;
1580 Expression
<Func
<uint>> e3
= () => d
.VolatileValue
;
1581 AssertNodeType (e3
, ExpressionType
.MemberAccess
);
1582 Assert
<uint> (492, e3
.Compile ().Invoke ());
1585 void MemberAccessTest_4 ()
1587 MemberAccessData d
= new MemberAccessData ();
1588 Expression
<Func
<string[]>> e4
= () => d
.StringValues
;
1589 AssertNodeType (e4
, ExpressionType
.MemberAccess
);
1590 Assert (null, e4
.Compile ().Invoke ());
1593 void MemberAccessTest_5 ()
1595 MemberAccessData d
= new MemberAccessData ();
1596 var e5
= d
.GetEvent ();
1597 AssertNodeType (e5
, ExpressionType
.MemberAccess
);
1598 Assert (null, e5
.Compile ().Invoke ());
1601 void MemberAccessTest_6 ()
1603 MemberAccessData d
= new MemberAccessData ();
1604 Expression
<Func
<MyType
>> e6
= () => d
.MyTypeProperty
;
1605 AssertNodeType (e6
, ExpressionType
.MemberAccess
);
1606 Assert (new MyType (), e6
.Compile ().Invoke ());
1609 void MemberAccessTest_7 ()
1611 MemberAccessData d
= new MemberAccessData ();
1612 Expression
<Func
<MyType
, short>> e7
= a
=> a
.ShortProp
;
1613 AssertNodeType (e7
, ExpressionType
.MemberAccess
);
1614 MyType mt
= new MyType ();
1616 Assert (124, e7
.Compile ().Invoke (mt
));
1619 void MemberAccessTest_8 ()
1621 Expression
<Func
<string>> e8
= () => MemberAccessData
.StaticProperty
;
1622 AssertNodeType (e8
, ExpressionType
.MemberAccess
);
1623 Assert ("alo", e8
.Compile ().Invoke ());
1626 void MemberAccessTest_9 ()
1628 string s
= "localvar";
1629 Expression
<Func
<string>> e9
= () => s
;
1630 // CSC emits this as MemberAccess
1631 AssertNodeType (e9
, ExpressionType
.Constant
);
1632 Assert ("localvar", e9
.Compile ().Invoke ());
1635 void MemberInitTest ()
1637 Expression
<Func
<MemberAccessData
>> e
= () => new MemberAccessData
{
1638 VolatileValue
= 2, StringValues
= new string [] { "sv" }
, MyTypeProperty
= new MyType (692)
1640 AssertNodeType (e
, ExpressionType
.MemberInit
);
1641 var r1
= e
.Compile ().Invoke ();
1642 Assert
<uint> (2, r1
.VolatileValue
);
1643 Assert (new string[] { "sv" }
, r1
.StringValues
);
1644 Assert (new MyType (692), r1
.MyTypeProperty
);
1647 void MemberInitTest_2 ()
1649 Expression
<Func
<MemberAccessData
>> e2
= () => new MemberAccessData
{
1650 ListValues
= new List
<string> { "a", null }
1653 AssertNodeType (e2
, ExpressionType
.MemberInit
);
1654 var r2
= e2
.Compile ().Invoke ();
1655 Assert ("a", r2
.ListValues
[0]);
1658 void MemberInitTest_3 ()
1660 Expression
<Func
<short, MyType
>> e3
= a
=> new MyType { ShortProp = a }
;
1661 AssertNodeType (e3
, ExpressionType
.MemberInit
);
1662 var r3
= e3
.Compile ().Invoke (33);
1663 Assert (33, r3
.ShortProp
);
1666 void MemberInitTest_4 ()
1668 Expression
<Func
<int>> e
= () => new int { }
;
1670 AssertNodeType (e
, ExpressionType
.MemberInit
);
1671 var r
= e
.Compile ().Invoke ();
1677 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
% b
;
1678 AssertNodeType (e
, ExpressionType
.Modulo
);
1679 Assert (29, e
.Compile ().Invoke (60, 31));
1682 void ModuloTest_2 ()
1684 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
% b
;
1685 AssertNodeType (e2
, ExpressionType
.Modulo
);
1686 Assert (null, e2
.Compile ().Invoke (null, 3));
1687 Assert (1.1, e2
.Compile ().Invoke (3.1, 2));
1690 void ModuloTest_3 ()
1692 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
% b
;
1693 AssertNodeType (e3
, ExpressionType
.Modulo
);
1694 Assert (0, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1697 void ModuloTest_4 ()
1699 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
% b
;
1700 AssertNodeType (e4
, ExpressionType
.Modulo
);
1701 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1702 Assert (new MyType (12), e4
.Compile ().Invoke (new MyType (12), new MyType (-20)));
1705 void ModuloTest_5 ()
1707 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
% b
;
1708 AssertNodeType (e5
, ExpressionType
.Modulo
);
1709 Assert (1, e5
.Compile ().Invoke (99, new MyType (2)));
1712 void ModuloTest_6 ()
1714 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
% b
;
1715 AssertNodeType (e6
, ExpressionType
.Modulo
);
1716 Assert (100, e6
.Compile ().Invoke (100, new MyType (200)));
1717 Assert (null, e6
.Compile ().Invoke (20, null));
1720 void ModuloTest_7 ()
1722 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => a
% null;
1723 AssertNodeType (e7
, ExpressionType
.Modulo
);
1724 Assert (null, e7
.Compile ().Invoke (60));
1727 void MultiplyTest ()
1729 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
* b
;
1730 AssertNodeType (e
, ExpressionType
.Multiply
);
1731 Assert (1860, e
.Compile ().Invoke (60, 31));
1732 Assert (2147483617, e
.Compile ().Invoke (int.MaxValue
, 31));
1735 void MultiplyTest_2 ()
1737 Expression
<Func
<double?, double?, double?>> e2
= (a
, b
) => a
* b
;
1738 AssertNodeType (e2
, ExpressionType
.Multiply
);
1739 Assert (null, e2
.Compile ().Invoke (null, 3));
1740 Assert (6.2, e2
.Compile ().Invoke (3.1, 2));
1743 void MultiplyTest_3 ()
1745 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
* b
;
1746 AssertNodeType (e3
, ExpressionType
.Multiply
);
1747 Assert (400, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
1750 void MultiplyTest_4 ()
1752 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
* b
;
1753 AssertNodeType (e4
, ExpressionType
.Multiply
);
1754 Assert (null, e4
.Compile ().Invoke (null, new MyType (-20)));
1755 Assert (new MyType (-240), e4
.Compile ().Invoke (new MyType (12), new MyType (-20)));
1758 void MultiplyTest_5 ()
1760 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
* b
;
1761 AssertNodeType (e5
, ExpressionType
.Multiply
);
1762 Assert (198, e5
.Compile ().Invoke (99, new MyType (2)));
1765 void MultiplyTest_6 ()
1767 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
* b
;
1768 AssertNodeType (e6
, ExpressionType
.Multiply
);
1769 Assert (0, e6
.Compile ().Invoke (int.MinValue
, new MyType (200)));
1770 Assert (null, e6
.Compile ().Invoke (20, null));
1773 void MultiplyTest_7 ()
1775 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => a
* null;
1776 AssertNodeType (e7
, ExpressionType
.Multiply
);
1777 Assert (null, e7
.Compile ().Invoke (60));
1780 void MultiplyCheckedTest ()
1783 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
* b
;
1784 AssertNodeType (e
, ExpressionType
.MultiplyChecked
);
1786 e
.Compile ().Invoke (int.MaxValue
, 309);
1787 throw new ApplicationException ("MultiplyCheckedTest #1");
1788 } catch (OverflowException
) { }
1792 void MultiplyCheckedTest_2 ()
1795 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a
* b
;
1796 AssertNodeType (e2
, ExpressionType
.MultiplyChecked
);
1797 Assert (null, e2
.Compile ().Invoke (null, 3));
1798 Assert (14025, e2
.Compile ().Invoke (byte.MaxValue
, 55));
1802 void MultiplyCheckedTest_3 ()
1805 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
* b
;
1806 AssertNodeType (e3
, ExpressionType
.Multiply
);
1807 Assert (-600, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
1811 void MultiplyCheckedTest_4 ()
1814 Expression
<Func
<double, double, double>> e4
= (a
, b
) => a
* b
;
1815 AssertNodeType (e4
, ExpressionType
.Multiply
);
1816 Assert (double.PositiveInfinity
, e4
.Compile ().Invoke (double.MaxValue
, int.MaxValue
));
1820 void MultiplyCheckedTest_5 ()
1823 Expression
<Func
<float?, float?, float?>> e5
= (a
, b
) => b
* a
;
1824 AssertNodeType (e5
, ExpressionType
.MultiplyChecked
);
1825 Assert (float.PositiveInfinity
, e5
.Compile ().Invoke (float.Epsilon
, float.PositiveInfinity
));
1831 Expression
<Func
<int, int>> e
= (a
) => -a
;
1832 AssertNodeType (e
, ExpressionType
.Negate
);
1833 Assert (30, e
.Compile ().Invoke (-30));
1836 void NegateTest_2 ()
1838 Expression
<Func
<sbyte, int>> e2
= (a
) => -(-a
);
1839 AssertNodeType (e2
, ExpressionType
.Negate
);
1840 Assert (-10, e2
.Compile ().Invoke (-10));
1843 void NegateTest_3 ()
1845 Expression
<Func
<long?, long?>> e3
= (a
) => -a
;
1846 AssertNodeType (e3
, ExpressionType
.Negate
);
1847 Assert (long.MinValue
+ 1, e3
.Compile ().Invoke (long.MaxValue
));
1848 Assert (null, e3
.Compile ().Invoke (null));
1851 void NegateTest_4 ()
1853 Expression
<Func
<MyType
, MyType
>> e4
= (a
) => -a
;
1854 AssertNodeType (e4
, ExpressionType
.Negate
);
1855 Assert (new MyType (14), e4
.Compile ().Invoke (new MyType (-14)));
1858 void NegateTest_5 ()
1860 Expression
<Func
<MyType
?, MyType
?>> e5
= (a
) => -a
;
1861 AssertNodeType (e5
, ExpressionType
.Negate
);
1862 Assert (new MyType (-33), e5
.Compile ().Invoke (new MyType (33)));
1863 Assert (null, e5
.Compile ().Invoke (null));
1866 void NegateTest_6 ()
1868 Expression
<Func
<MyTypeImplicitOnly
, int>> e6
= (MyTypeImplicitOnly a
) => -a
;
1869 AssertNodeType (e6
, ExpressionType
.Negate
);
1870 Assert (-4, e6
.Compile ().Invoke (new MyTypeImplicitOnly (4)));
1873 void NegateTest_7 ()
1875 Expression
<Func
<MyTypeImplicitOnly
?, int?>> e7
= (MyTypeImplicitOnly
? a
) => -a
;
1876 AssertNodeType (e7
, ExpressionType
.Negate
);
1877 Assert (-46, e7
.Compile ().Invoke (new MyTypeImplicitOnly (46)));
1879 // Another version of MS bug when predefined conversion is required on nullable user operator
1880 // Assert (null, e7.Compile ().Invoke (null));
1883 void NegateTest_8 ()
1885 Expression
<Func
<sbyte?, int?>> e8
= (a
) => -a
;
1886 AssertNodeType (e8
, ExpressionType
.Negate
);
1887 Assert (11, e8
.Compile ().Invoke (-11));
1890 void NegateTest_9 ()
1892 Expression
<Func
<uint, long>> e9
= (a
) => -a
;
1893 AssertNodeType (e9
, ExpressionType
.Negate
);
1894 Assert (-2, e9
.Compile ().Invoke (2));
1897 void NegateTestChecked ()
1900 Expression
<Func
<int, int>> e
= (int a
) => -a
;
1901 AssertNodeType (e
, ExpressionType
.NegateChecked
);
1903 e
.Compile ().Invoke (int.MinValue
);
1904 throw new ApplicationException ("NegateTestChecked #1");
1905 } catch (OverflowException
) { }
1909 void NegateTestChecked_2 ()
1912 Expression
<Func
<byte?, int?>> e2
= (a
) => -a
;
1913 AssertNodeType (e2
, ExpressionType
.NegateChecked
);
1914 Assert (null, e2
.Compile ().Invoke (null));
1915 Assert (-255, e2
.Compile ().Invoke (byte.MaxValue
));
1919 void NegateTestChecked_3 ()
1922 Expression
<Func
<MyType
, MyType
>> e3
= (MyType a
) => -a
;
1923 AssertNodeType (e3
, ExpressionType
.Negate
);
1924 Assert (20, e3
.Compile ().Invoke (new MyType (-20)));
1928 void NegateTestChecked_4 ()
1931 Expression
<Func
<double, double>> e4
= (a
) => -a
;
1932 AssertNodeType (e4
, ExpressionType
.Negate
);
1933 Assert (double.NegativeInfinity
, e4
.Compile ().Invoke (double.PositiveInfinity
));
1937 void NewArrayInitTest ()
1939 Expression
<Func
<int []>> e
= () => new int [0];
1940 AssertNodeType (e
, ExpressionType
.NewArrayInit
);
1941 Assert (new int [0], e
.Compile ().Invoke ());
1944 void NewArrayInitTest_2 ()
1946 Expression
<Func
<int []>> e1
= () => new int [] { }
;
1947 AssertNodeType (e1
, ExpressionType
.NewArrayInit
);
1948 Assert (new int [0], e1
.Compile ().Invoke ());
1951 void NewArrayInitTest_3 ()
1953 Expression
<Func
<ushort, ulong? []>> e2
= (ushort a
) => new ulong? [] { a }
;
1954 AssertNodeType (e2
, ExpressionType
.NewArrayInit
);
1955 Assert (new ulong? [1] { ushort.MaxValue }
, e2
.Compile ().Invoke (ushort.MaxValue
));
1958 void NewArrayInitTest_4 ()
1960 Expression
<Func
<char [] []>> e3
= () => new char [] [] { new char [] { 'a' }
};
1961 AssertNodeType (e3
, ExpressionType
.NewArrayInit
);
1962 Assert (new char [] { 'a' }
, e3
.Compile ().Invoke () [0]);
1965 void NewArrayInitTest_5 ()
1967 Expression
<Func
<int?[]>> e
= () => new int?[] { null, 3, 4 }
;
1968 AssertNodeType (e
, ExpressionType
.NewArrayInit
);
1969 Assert (3, e
.Compile ().Invoke ().Length
);
1972 void NewArrayBoundsTest ()
1974 Expression
<Func
<int [,]>> e
= () => new int [2,3];
1975 AssertNodeType (e
, ExpressionType
.NewArrayBounds
);
1976 Assert (new int [2,3].Length
, e
.Compile ().Invoke ().Length
);
1979 void NewArrayBoundsTest_2 ()
1981 Expression
<Func
<int[,]>> e2
= () => new int [0,0];
1982 AssertNodeType (e2
, ExpressionType
.NewArrayBounds
);
1983 Assert (new int [0, 0].Length
, e2
.Compile ().Invoke ().Length
);
1988 Expression
<Func
<MyType
>> e
= () => new MyType (2);
1989 AssertNodeType (e
, ExpressionType
.New
);
1990 Assert (new MyType (2), e
.Compile ().Invoke ());
1995 Expression
<Func
<MyType
>> e2
= () => new MyType ();
1996 AssertNodeType (e2
, ExpressionType
.New
);
1997 Assert (new MyType (), e2
.Compile ().Invoke ());
2002 Expression
<Func
<NewTest
<bool>>> e3
= () => new NewTest
<bool> (true);
2003 AssertNodeType (e3
, ExpressionType
.New
);
2004 Assert (new NewTest
<bool> (true), e3
.Compile ().Invoke ());
2009 Expression
<Func
<decimal, NewTest
<decimal>>> e4
= (decimal d
) => new NewTest
<decimal> (1, 5, d
);
2010 AssertNodeType (e4
, ExpressionType
.New
);
2011 Assert (new NewTest
<decimal> (1, 5, -9), e4
.Compile ().Invoke (-9));
2016 Expression
<Func
<object>> e5
= () => new { A = 9, Value = "a" }
;
2017 AssertNodeType (e5
, ExpressionType
.New
);
2018 Assert (new { A = 9, Value = "a" }
, e5
.Compile ().Invoke ());
2021 // CSC bug: emits new MyEnum as a constant
2024 Expression
<Func
<MyEnum
>> e
= () => new MyEnum ();
2025 AssertNodeType (e
, ExpressionType
.New
);
2026 Assert
<MyEnum
> (0, e
.Compile ().Invoke ());
2031 Expression
<Func
<bool, bool>> e
= (bool a
) => !a
;
2032 AssertNodeType (e
, ExpressionType
.Not
);
2033 Assert (false, e
.Compile ().Invoke (true));
2038 Expression
<Func
<MyType
, bool>> e2
= (MyType a
) => !a
;
2039 AssertNodeType (e2
, ExpressionType
.Not
);
2040 Assert (true, e2
.Compile ().Invoke (new MyType (1)));
2041 Assert (false, e2
.Compile ().Invoke (new MyType (-1)));
2046 Expression
<Func
<int, int>> e3
= (int a
) => ~a
;
2047 AssertNodeType (e3
, ExpressionType
.Not
);
2048 Assert (-8, e3
.Compile ().Invoke (7));
2053 Expression
<Func
<MyType
, int>> e4
= (MyType a
) => ~a
;
2054 AssertNodeType (e4
, ExpressionType
.Not
);
2055 Assert (0, e4
.Compile ().Invoke (new MyType (-1)));
2060 Expression
<Func
<ulong, ulong>> e5
= (ulong a
) => ~a
;
2061 AssertNodeType (e5
, ExpressionType
.Not
);
2062 Assert
<ulong> (18446744073709551608, e5
.Compile ().Invoke (7));
2067 Expression
<Func
<MyEnum
, MyEnum
>> e6
= (MyEnum a
) => ~a
;
2068 AssertNodeType (e6
, ExpressionType
.Convert
);
2069 Assert ((MyEnum
)254, e6
.Compile ().Invoke (MyEnum
.Value_1
));
2072 void NotNullableTest ()
2074 Expression
<Func
<bool?, bool?>> e
= (bool? a
) => !a
;
2075 AssertNodeType (e
, ExpressionType
.Not
);
2076 Assert (false, e
.Compile ().Invoke (true));
2077 Assert (null, e
.Compile ().Invoke (null));
2080 void NotNullableTest_2 ()
2082 Expression
<Func
<MyType
?, bool?>> e2
= (MyType
? a
) => !a
;
2083 AssertNodeType (e2
, ExpressionType
.Not
);
2084 Assert (true, e2
.Compile ().Invoke (new MyType (1)));
2085 Assert (null, e2
.Compile ().Invoke (null));
2088 void NotNullableTest_3 ()
2090 Expression
<Func
<sbyte?, int?>> e3
= (sbyte? a
) => ~a
;
2091 AssertNodeType (e3
, ExpressionType
.Not
);
2092 Assert (-5, e3
.Compile ().Invoke (4));
2093 Assert (null, e3
.Compile ().Invoke (null));
2096 void NotNullableTest_4 ()
2098 Expression
<Func
<MyType
?, int?>> e4
= (MyType
? a
) => ~a
;
2099 AssertNodeType (e4
, ExpressionType
.Not
);
2100 Assert (0, e4
.Compile ().Invoke (new MyType (-1)));
2101 Assert (null, e4
.Compile ().Invoke (null));
2104 void NotNullableTest_5 ()
2106 Expression
<Func
<MyEnum
?, MyEnum
?>> e5
= (MyEnum
? a
) => ~a
;
2107 AssertNodeType (e5
, ExpressionType
.Convert
);
2108 Assert ((MyEnum
) 254, e5
.Compile ().Invoke (MyEnum
.Value_1
));
2109 Assert (null, e5
.Compile ().Invoke (null));
2112 void NotEqualTest ()
2114 Expression
<Func
<int, int, bool>> e
= (int a
, int b
) => a
!= b
;
2115 AssertNodeType (e
, ExpressionType
.NotEqual
);
2116 Assert (true, e
.Compile ().Invoke (60, 30));
2117 Assert (false, e
.Compile ().Invoke (-1, -1));
2120 void NotEqualTest_2 ()
2122 Expression
<Func
<sbyte?, sbyte?, bool>> e2
= (a
, b
) => a
!= b
;
2123 AssertNodeType (e2
, ExpressionType
.NotEqual
);
2124 Assert (false, e2
.Compile ().Invoke (3, 3));
2125 Assert (true, e2
.Compile ().Invoke (3, 2));
2128 void NotEqualTest_3 ()
2130 Expression
<Func
<MyType
, MyType
, bool>> e3
= (MyType a
, MyType b
) => a
!= b
;
2131 AssertNodeType (e3
, ExpressionType
.NotEqual
);
2132 Assert (false, e3
.Compile ().Invoke (new MyType (-20), new MyType (-20)));
2135 void NotEqualTest_4 ()
2137 Expression
<Func
<MyType
?, MyType
?, bool>> e4
= (MyType
? a
, MyType
? b
) => a
!= b
;
2138 AssertNodeType (e4
, ExpressionType
.NotEqual
);
2139 Assert (true, e4
.Compile ().Invoke (null, new MyType (-20)));
2140 Assert (false, e4
.Compile ().Invoke (null, null));
2141 Assert (false, e4
.Compile ().Invoke (new MyType (120), new MyType (120)));
2144 void NotEqualTest_5 ()
2146 Expression
<Func
<bool?, bool?, bool>> e5
= (bool? a
, bool? b
) => a
!= b
;
2147 AssertNodeType (e5
, ExpressionType
.NotEqual
);
2148 Assert (true, e5
.Compile ().Invoke (true, null));
2149 Assert (false, e5
.Compile ().Invoke (null, null));
2150 Assert (false, e5
.Compile ().Invoke (false, false));
2153 void NotEqualTest_6 ()
2155 Expression
<Func
<bool, bool>> e6
= (bool a
) => a
!= null;
2156 AssertNodeType (e6
, ExpressionType
.NotEqual
);
2157 Assert (true, e6
.Compile ().Invoke (true));
2158 Assert (true, e6
.Compile ().Invoke (false));
2161 void NotEqualTest_7 ()
2163 Expression
<Func
<string, string, bool>> e7
= (string a
, string b
) => a
!= b
;
2164 AssertNodeType (e7
, ExpressionType
.NotEqual
);
2165 Assert (false, e7
.Compile ().Invoke (null, null));
2166 Assert (true, e7
.Compile ().Invoke ("a", "A"));
2167 Assert (false, e7
.Compile ().Invoke ("a", "a"));
2170 void NotEqualTest_8 ()
2172 Expression
<Func
<object, bool>> e8
= (object a
) => null != a
;
2173 AssertNodeType (e8
, ExpressionType
.NotEqual
);
2174 Assert (false, e8
.Compile ().Invoke (null));
2175 Assert (true, e8
.Compile ().Invoke ("a"));
2176 Assert (true, e8
.Compile ().Invoke (this));
2179 void NotEqualTest_9 ()
2181 Expression
<Func
<MyEnum
, MyEnum
, bool>> e9
= (a
, b
) => a
!= b
;
2182 AssertNodeType (e9
, ExpressionType
.NotEqual
);
2183 Assert (true, e9
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2184 Assert (false, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2187 void NotEqualTest_10 ()
2189 Expression
<Func
<MyEnum
?, MyEnum
?, bool>> e10
= (a
, b
) => a
!= b
;
2190 AssertNodeType (e10
, ExpressionType
.NotEqual
);
2191 Assert (true, e10
.Compile ().Invoke (MyEnum
.Value_1
, null));
2192 Assert (false, e10
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2195 void NotEqualTest_11 ()
2197 Expression
<Func
<MyEnum
?, bool>> e11
= (a
) => a
!= null;
2198 AssertNodeType (e11
, ExpressionType
.NotEqual
);
2199 Assert (true, e11
.Compile ().Invoke (MyEnum
.Value_1
));
2200 Assert (false, e11
.Compile ().Invoke (null));
2205 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
| b
;
2207 AssertNodeType (e
, ExpressionType
.Or
);
2208 Func
<bool, bool, bool> c
= e
.Compile ();
2210 Assert (true, c (true, true));
2211 Assert (true, c (true, false));
2212 Assert (true, c (false, true));
2213 Assert (false, c (false, false));
2218 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
| b
;
2219 AssertNodeType (e2
, ExpressionType
.Or
);
2220 var c2
= e2
.Compile ();
2221 Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
2226 Expression
<Func
<MyEnum
, MyEnum
, MyEnum
>> e3
= (a
, b
) => a
| b
;
2227 AssertNodeType (e3
, ExpressionType
.Convert
);
2228 Assert ((MyEnum
)3, e3
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2229 Assert (MyEnum
.Value_2
, e3
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_2
));
2232 void OrNullableTest ()
2234 Expression
<Func
<bool?, bool?, bool?>> e
= (bool? a
, bool? b
) => a
| b
;
2236 AssertNodeType (e
, ExpressionType
.Or
);
2237 Func
<bool?, bool?, bool?> c
= e
.Compile ();
2239 Assert (true, c (true, true));
2240 Assert (true, c (true, false));
2241 Assert (true, c (false, true));
2242 Assert (false, c (false, false));
2244 Assert (true, c (true, null));
2245 Assert (null, c (false, null));
2246 Assert (null, c (null, false));
2247 Assert (true, c (true, null));
2248 Assert (null, c (null, null));
2251 void OrNullableTest_2 ()
2253 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e2
= (MyType
? a
, MyType
? b
) => a
| b
;
2254 AssertNodeType (e2
, ExpressionType
.Or
);
2255 var c2
= e2
.Compile ();
2256 Assert (new MyType (3), c2 (new MyType (1), new MyType (2)));
2257 Assert (null, c2 (new MyType (1), null));
2261 void OrNullableTest_3 ()
2263 Expression
<Func
<MyType
?, uint, long?>> e3
= (MyType
? a
, uint b
) => a
| b
;
2264 AssertNodeType (e3
, ExpressionType
.Or
);
2265 var c3
= e3
.Compile ();
2266 Assert (9, c3 (new MyType (1), 8));
2269 void OrNullableTest_4 ()
2271 Expression
<Func
<MyEnum
?, MyEnum
?, MyEnum
?>> e4
= (a
, b
) => a
| b
;
2272 AssertNodeType (e4
, ExpressionType
.Convert
);
2273 Assert (null, e4
.Compile ().Invoke (null, MyEnum
.Value_2
));
2274 Assert ((MyEnum
)3, e4
.Compile ().Invoke (MyEnum
.Value_1
, MyEnum
.Value_2
));
2279 Expression
<Func
<bool, bool, bool>> e
= (bool a
, bool b
) => a
|| b
;
2280 AssertNodeType (e
, ExpressionType
.OrElse
);
2281 Assert (true, e
.Compile ().Invoke (true, false));
2284 void OrElseTest_2 ()
2286 Expression
<Func
<MyType
, MyType
, MyType
>> e2
= (MyType a
, MyType b
) => a
|| b
;
2287 AssertNodeType (e2
, ExpressionType
.OrElse
);
2288 Assert (new MyType (64), e2
.Compile ().Invoke (new MyType (64), new MyType (64)));
2289 Assert (new MyType (32), e2
.Compile ().Invoke (new MyType (32), new MyType (64)));
2292 void ParameterTest ()
2294 Expression
<Func
<string, string>> e
= (string a
) => a
;
2295 AssertNodeType (e
, ExpressionType
.Parameter
);
2296 Assert ("t", e
.Compile ().Invoke ("t"));
2299 void ParameterTest_2 ()
2301 Expression
<Func
<object[], object[]>> e2
= (object[] a
) => a
;
2302 AssertNodeType (e2
, ExpressionType
.Parameter
);
2303 Assert (new object [0], e2
.Compile ().Invoke (new object [0]));
2306 void ParameterTest_3 ()
2308 Expression
<Func
<IntPtr
, IntPtr
>> e3
= a
=> a
;
2309 AssertNodeType (e3
, ExpressionType
.Parameter
);
2310 Assert (IntPtr
.Zero
, e3
.Compile ().Invoke (IntPtr
.Zero
));
2313 unsafe void ParameterTest_4 ()
2315 Expression
<Func
<int*[], int* []>> e4
= (a
) => a
;
2316 AssertNodeType (e4
, ExpressionType
.Parameter
);
2317 Assert
<int*[]> (null, e4
.Compile ().Invoke (null));
2318 int* e4_el
= stackalloc int [5];
2319 int*[] ptr
= new int*[] { e4_el }
;
2320 Assert
<int*[]> (ptr
, e4
.Compile ().Invoke (ptr
));
2325 Expression
<Func
<Expression
<Func
<int>>>> e
= () => () => 2;
2326 AssertNodeType (e
, ExpressionType
.Quote
);
2327 Assert (2, e
.Compile ().Invoke ().Compile ().Invoke ());
2330 void RightShiftTest ()
2332 Expression
<Func
<ulong, short, ulong>> e
= (ulong a
, short b
) => a
>> b
;
2333 AssertNodeType (e
, ExpressionType
.RightShift
);
2334 Assert ((ulong) 0x1FD940L
, e
.Compile ().Invoke (0xFECA0000, 11));
2335 Assert ((ulong) 0x7FFFFFFF, e
.Compile ().Invoke (0xFFFFFFFF, 0xA01));
2337 // .net produces a strange result
2338 // see https://bugzilla.novell.com/show_bug.cgi?id=398358
2339 // Assert ((ulong)0, e.Compile ().Invoke (0xFFFFFFFF, 0xA01));
2342 void RightShiftTest_2 ()
2344 Expression
<Func
<MyType
, MyType
, int>> e2
= (MyType a
, MyType b
) => a
>> b
;
2345 AssertNodeType (e2
, ExpressionType
.RightShift
);
2346 var c2
= e2
.Compile ();
2347 Assert (64, c2 (new MyType (256), new MyType (2)));
2350 void RightShiftTest_3 ()
2352 Expression
<Func
<long?, sbyte, long?>> e3
= (long? a
, sbyte b
) => a
>> b
;
2353 AssertNodeType (e3
, ExpressionType
.RightShift
);
2354 Assert (null, e3
.Compile ().Invoke (null, 11));
2355 Assert (512, e3
.Compile ().Invoke (1024, 1));
2358 void RightShiftTest_4 ()
2360 Expression
<Func
<MyType
?, MyType
?, int?>> e4
= (MyType
? a
, MyType
? b
) => a
>> b
;
2361 AssertNodeType (e4
, ExpressionType
.RightShift
);
2362 var c4
= e4
.Compile ();
2363 Assert (null, c4 (new MyType (8), null));
2364 Assert (null, c4 (null, new MyType (8)));
2365 Assert (64, c4 (new MyType (256), new MyType (2)));
2368 void SubtractTest ()
2370 Expression
<Func
<int, int, int>> e
= (int a
, int b
) => a
- b
;
2371 AssertNodeType (e
, ExpressionType
.Subtract
);
2372 Assert (-10, e
.Compile ().Invoke (20, 30));
2375 void SubtractTest_2 ()
2377 Expression
<Func
<int?, int?, int?>> e2
= (a
, b
) => a
- b
;
2378 AssertNodeType (e2
, ExpressionType
.Subtract
);
2379 Assert (null, e2
.Compile ().Invoke (null, 3));
2382 void SubtractTest_3 ()
2384 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
- b
;
2385 AssertNodeType (e3
, ExpressionType
.Subtract
);
2386 Assert (-50, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2389 void SubtractTest_4 ()
2391 Expression
<Func
<MyType
?, MyType
?, MyType
?>> e4
= (MyType
? a
, MyType
? b
) => a
- b
;
2392 AssertNodeType (e4
, ExpressionType
.Subtract
);
2393 Assert (new MyType (-50), e4
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2394 Assert (null, e4
.Compile ().Invoke (null, new MyType (30)));
2397 void SubtractTest_5 ()
2399 Expression
<Func
<int, MyType
, int>> e5
= (int a
, MyType b
) => a
- b
;
2400 AssertNodeType (e5
, ExpressionType
.Subtract
);
2401 Assert (-29, e5
.Compile ().Invoke (1, new MyType (30)));
2404 void SubtractTest_6 ()
2406 Expression
<Func
<int, MyType
?, int?>> e6
= (int a
, MyType
? b
) => a
- b
;
2407 AssertNodeType (e6
, ExpressionType
.Subtract
);
2408 Assert (-61, e6
.Compile ().Invoke (-31, new MyType (30)));
2411 void SubtractTest_7 ()
2413 Expression
<Func
<ushort, int?>> e7
= (ushort a
) => null - a
;
2414 AssertNodeType (e7
, ExpressionType
.Subtract
);
2415 Assert (null, e7
.Compile ().Invoke (690));
2418 void SubtractTest_8 ()
2420 Expression
<Func
<MyEnum
, byte, MyEnum
>> e8
= (a
, b
) => a
- b
;
2421 AssertNodeType (e8
, ExpressionType
.Convert
);
2422 Assert ((MyEnum
)255, e8
.Compile ().Invoke (MyEnum
.Value_1
, 2));
2425 void SubtractTest_9 ()
2427 Expression
<Func
<MyEnum
, MyEnum
, byte>> e9
= (a
, b
) => a
- b
;
2428 AssertNodeType (e9
, ExpressionType
.Convert
);
2429 Assert (1, e9
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
2433 void SubtractTest_10 ()
2435 Expression
<Func
<MyEnum
?, byte?, MyEnum
?>> e10
= (a
, b
) => a
- b
;
2436 AssertNodeType (e10
, ExpressionType
.Convert
);
2437 Assert ((MyEnum
) 255, e10
.Compile ().Invoke (MyEnum
.Value_1
, 2));
2441 void SubtractTest_11 ()
2443 Expression
<Func
<MyEnum
?, MyEnum
?, byte?>> e11
= (a
, b
) => a
- b
;
2444 AssertNodeType (e11
, ExpressionType
.Convert
);
2445 Assert
<byte?> (1, e11
.Compile ().Invoke (MyEnum
.Value_2
, MyEnum
.Value_1
));
2448 void SubtractCheckedTest ()
2451 Expression
<Func
<long, long, long>> e
= (long a
, long b
) => a
- b
;
2452 AssertNodeType (e
, ExpressionType
.SubtractChecked
);
2454 e
.Compile ().Invoke (long.MinValue
, 309);
2455 throw new ApplicationException ("SubtractCheckedTest #1");
2456 } catch (OverflowException
) { }
2460 void SubtractCheckedTest_2 ()
2463 Expression
<Func
<byte?, byte?, int?>> e2
= (a
, b
) => a
- b
;
2464 AssertNodeType (e2
, ExpressionType
.SubtractChecked
);
2465 Assert (null, e2
.Compile ().Invoke (null, 3));
2466 Assert (-55, e2
.Compile ().Invoke (byte.MinValue
, 55));
2470 void SubtractCheckedTest_3 ()
2473 Expression
<Func
<MyType
, MyType
, MyType
>> e3
= (MyType a
, MyType b
) => a
- b
;
2474 AssertNodeType (e3
, ExpressionType
.Subtract
);
2475 Assert (-50, e3
.Compile ().Invoke (new MyType (-20), new MyType (30)));
2479 void SubtractCheckedTest_4 ()
2482 Expression
<Func
<double, double, double>> e4
= (a
, b
) => a
- b
;
2483 AssertNodeType (e4
, ExpressionType
.Subtract
);
2484 Assert (double.PositiveInfinity
, e4
.Compile ().Invoke (double.MinValue
, double.NegativeInfinity
));
2490 Expression
<Func
<object, Tester
>> e
= (object a
) => a
as Tester
;
2491 AssertNodeType (e
, ExpressionType
.TypeAs
);
2492 Assert (this, e
.Compile ().Invoke (this));
2495 void TypeAsTest_2 ()
2497 Expression
<Func
<object, int?>> e2
= (object a
) => a
as int?;
2498 AssertNodeType (e2
, ExpressionType
.TypeAs
);
2499 Assert (null, e2
.Compile ().Invoke (null));
2500 Assert (null, e2
.Compile ().Invoke (this));
2501 Assert (44, e2
.Compile ().Invoke (44));
2504 void TypeAsTest_3 ()
2506 Expression
<Func
<object, object>> e3
= (object a
) => null as object;
2507 AssertNodeType (e3
, ExpressionType
.TypeAs
);
2508 Assert (null, e3
.Compile ().Invoke (null));
2513 Expression
<Func
<object, bool>> e
= (object a
) => a
is Tester
;
2514 AssertNodeType (e
, ExpressionType
.TypeIs
);
2515 Assert (true, e
.Compile ().Invoke (this));
2516 Assert (false, e
.Compile ().Invoke (1));
2519 void TypeIsTest_2 ()
2521 Expression
<Func
<object, bool>> e2
= (object a
) => a
is int?;
2522 AssertNodeType (e2
, ExpressionType
.TypeIs
);
2523 Assert (false, e2
.Compile ().Invoke (null));
2524 Assert (true, e2
.Compile ().Invoke (1));
2527 void TypeIsTest_3 ()
2529 Expression
<Func
<object, bool>> e3
= (object a
) => null is object;
2530 AssertNodeType (e3
, ExpressionType
.TypeIs
);
2531 Assert (false, e3
.Compile ().Invoke (null));
2534 void TypeIsTest_5 ()
2536 Expression
<Func
<bool>> e5
= () => 1 is int;
2537 AssertNodeType (e5
, ExpressionType
.TypeIs
);
2538 Assert (true, e5
.Compile ().Invoke ());
2541 void TypeIsTest_6 ()
2543 Expression
<Func
<int?, bool>> e6
= (a
) => a
is int;
2544 AssertNodeType (e6
, ExpressionType
.TypeIs
);
2545 Assert (true, e6
.Compile ().Invoke (1));
2546 Assert (false, e6
.Compile ().Invoke (null));
2549 void UnaryPlusTest ()
2551 Expression
<Func
<int, int>> e
= (a
) => +a
;
2552 AssertNodeType (e
, ExpressionType
.Parameter
);
2553 Assert (-30, e
.Compile ().Invoke (-30));
2556 void UnaryPlusTest_2 ()
2558 Expression
<Func
<long?, long?>> e2
= (a
) => +a
;
2559 AssertNodeType (e2
, ExpressionType
.Parameter
);
2562 void UnaryPlusTest_3 ()
2564 Expression
<Func
<MyType
, MyType
>> e4
= (a
) => +a
;
2565 AssertNodeType (e4
, ExpressionType
.UnaryPlus
);
2566 Assert (new MyType (-14), e4
.Compile ().Invoke (new MyType (-14)));
2569 void UnaryPlusTest_4 ()
2571 Expression
<Func
<MyType
?, MyType
?>> e5
= (a
) => +a
;
2572 AssertNodeType (e5
, ExpressionType
.UnaryPlus
);
2573 Assert (new MyType (33), e5
.Compile ().Invoke (new MyType (33)));
2574 Assert (null, e5
.Compile ().Invoke (null));
2577 void UnaryPlusTest_5 ()
2579 Expression
<Func
<sbyte?, long?>> e6
= (a
) => +a
;
2580 AssertNodeType (e6
, ExpressionType
.Convert
);
2581 Assert (3, e6
.Compile ().Invoke (3));
2582 Assert (null, e6
.Compile ().Invoke (null));
2585 #pragma warning restore 169
2590 string InstanceMethod (string arg
)
2595 object InstanceParamsMethod (int index
, params object [] args
)
2599 if (args
.Length
== 0)
2601 return args
[index
];
2604 static int TestInt ()
2609 T GenericMethod
<T
> (T t
)
2614 static void RefMethod (ref int i
)
2619 static bool RunTest (MethodInfo test
)
2621 Console
.Write ("Running test {0, -25}", test
.Name
);
2623 test
.Invoke (new Tester (), null);
2624 Console
.WriteLine ("OK");
2626 } catch (Exception e
) {
2627 Console
.WriteLine ("FAILED");
2628 Console
.WriteLine (e
.ToString ());
2633 public static int Main ()
2635 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
2636 where test
.GetParameters ().Length
== 0
2638 select RunTest (test
);
2640 int failures
= tests
.Count (a
=> !a
);
2641 Console
.WriteLine (failures
+ " tests failed");