2 using System
.Collections
.Generic
;
4 using System
.Reflection
;
6 // Dynamic binary operator, unary operators and convert tests
8 public struct InverseLogicalOperator
11 public InverseLogicalOperator (bool value)
16 public static bool operator true (InverseLogicalOperator u
)
21 public static bool operator false (InverseLogicalOperator u
)
31 public MyType (int value) : this ()
36 public short ShortProp { get; set; }
38 public override int GetHashCode ()
40 throw new NotImplementedException ();
43 public static bool operator true (MyType a
)
48 public static bool operator false (MyType a
)
53 public static MyType
operator + (MyType a
, MyType b
)
55 return new MyType (a
.value + b
.value);
58 public static MyType
operator - (MyType a
, MyType b
)
60 return new MyType (a
.value - b
.value);
63 public static MyType
operator / (MyType a
, MyType b
)
65 return new MyType (a
.value / b
.value);
68 public static MyType
operator * (MyType a
, MyType b
)
70 return new MyType (a
.value * b
.value);
73 public static MyType
operator % (MyType a
, MyType b
)
75 return new MyType (a
.value % b
.value);
78 public static MyType
operator &(MyType a
, MyType b
)
80 return new MyType (a
.value & b
.value);
83 public static MyType
operator | (MyType a
, MyType b
)
85 return new MyType (a
.value | b
.value);
88 public static MyType
operator ^
(MyType a
, MyType b
)
90 return new MyType (a
.value ^ b
.value);
93 public static bool operator == (MyType a
, MyType b
)
95 return a
.value == b
.value;
98 public static bool operator != (MyType a
, MyType b
)
100 return a
.value != b
.value;
103 public static bool operator > (MyType a
, MyType b
)
105 return a
.value > b
.value;
108 public static bool operator < (MyType a
, MyType b
)
110 return a
.value < b
.value;
113 public static bool operator >= (MyType a
, MyType b
)
115 return a
.value >= b
.value;
118 public static bool operator <= (MyType a
, MyType b
)
120 return a
.value <= b
.value;
123 public static bool operator ! (MyType a
)
128 public static int operator ~
(MyType a
)
133 public static MyType
operator ++ (MyType a
)
135 return new MyType (a
.value * 2);
138 public static MyType
operator -- (MyType a
)
140 return new MyType (a
.value / 2);
143 public static int operator >> (MyType a
, int b
)
148 public static int operator << (MyType a
, int b
)
153 public static MyType
operator - (MyType a
)
155 return new MyType (-a
.value);
158 public static MyType
operator + (MyType a
)
160 return new MyType (334455); // magic number
163 public override string ToString ()
165 return value.ToString ();
174 public MyTypeExplicit (int value)
179 public static explicit operator int (MyTypeExplicit m
)
185 struct MyTypeImplicitOnly
189 public MyTypeImplicitOnly (short b
)
194 public static implicit operator short (MyTypeImplicitOnly m
)
199 public static implicit operator bool (MyTypeImplicitOnly m
)
211 enum MyEnumUlong
: ulong
220 delegate void EmptyDelegate ();
221 event Action ev_assign
;
223 static void Assert
<T
> (T expected
, T
value, string name
)
225 if (!EqualityComparer
<T
>.Default
.Equals (expected
, value)) {
227 throw new ApplicationException (name
+ expected
+ " != " + value);
231 static void AssertChecked
<T
> (Func
<T
> expected
, T
value, string name
)
234 Assert (expected (), value, name
);
235 throw new ApplicationException (name
+ ": OverflowException expected");
236 } catch (OverflowException
) {
241 static void AssertChecked (Action expected
, string name
)
245 throw new ApplicationException (name
+ ": OverflowException expected");
246 } catch (OverflowException
) {
251 #pragma warning disable 169
258 Assert (d
+ v
, 7, "#1");
260 Assert (d
+ v2
, 5.5, "#1a");
263 MyType v3
= new MyType (30);
264 Assert (d
+ v3
, new MyType (35), "#3");
265 dynamic d3
= new MyType (-7);
266 Assert
<MyType
> (d3
+ new MyType (6), new MyType (-1), "#3a");
268 d3
= new MyTypeImplicitOnly (6);
269 Assert (d3
+ new MyTypeImplicitOnly (11), 17, "#3b");
271 d
= new MyTypeImplicitOnly (5);
273 Assert (d
+ v4
, 9m
, "#4");
276 void AddNullableTest ()
281 Assert
<int?> (d
+ v2
, null, "#1");
282 Assert
<int?> (d
+ null, null, "#1a");
283 Assert
<int?> (null + d
, null, "#1b");
286 Assert (d
+ v2
, 3, "#2");
287 dynamic d2
= (int?) -2;
288 Assert (d2
+ 1, -1, "#2a");
291 MyType
? v3
= new MyType (30);
292 Assert (d
+ v3
, new MyType (35), "#3");
293 dynamic d3
= new MyType
? (new MyType (-7));
294 Assert (d3
+ new MyType (6), new MyType (-1), "#3a");
295 Assert
<MyType
?> (d3
+ null, null, "#3b");
297 d
= new MyTypeImplicitOnly (5);
299 Assert (d
+ v4
, 9m
, "#4");
301 Assert
<decimal?> (d
+ v4
, null, "#4a");
306 dynamic d
= MyEnum
.Value_1
;
308 Assert (d
+ 1, MyEnum
.Value_2
, "#1");
310 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
311 Assert (d2
+ (byte) 1, MyEnumUlong
.Value_2
, "#2");
312 Assert
<MyEnumUlong
?> (d2
+ (object) null, null, "#2a");
314 // CSC: Invalid System.InvalidOperationException
315 Assert
<MyEnum
?> (d
+ null, null, "#1");
318 void AddCheckedTest ()
323 int v
= int.MaxValue
;
324 AssertChecked (() => d
+ v
, 7, "#1");
327 AssertChecked (() => d
+ v2
, null, "#2");
330 MyType v3
= new MyType (int.MaxValue
);
331 Assert (new MyType (-2147483646), d
+ v3
, "#3");
335 void AddStringTest ()
339 Assert (d
+ v
, "foo->", "#1");
340 Assert (d
+ 1, "foo1", "#1a");
341 Assert (d
+ null, "foo", "#1b");
342 Assert (d
+ 1 + v
, "foo1->", "#1a");
345 Assert (d
+ v2
, "foo4", "#2");
348 void AddAssignTest ()
359 Assert (d
, 5.5, "#1a");
361 Assert (d
, 7.5, "#1b");
363 dynamic d3
= new MyType (-7);
364 d3
+= new MyType (6);
365 Assert
<MyType
> (d3
, new MyType (-1), "#3");
370 Assert (d
, 9m
, "#4");
373 void AddAssignNullableTest ()
375 dynamic d
= (int?) 5;
378 // For now it's impossible to use nullable compound assignment
379 // due to the way how DLR works. GetType () on nullable object returns
380 // underlying type and not nullable type, that means that
381 // C# binder is initialized with wrong operand type and any operation
386 Assert<int?> (d, null, "#1");
388 Assert<int?> (d, null, "#1a");
397 Assert (d, -1, "#2a");
399 MyType? v3 = new MyType (30);
401 Assert (d, new MyType (35), "#3");
402 dynamic d3 = new MyType? (new MyType (-7));
403 Assert (d3 + new MyType (6), new MyType (-1), "#3a");
404 Assert<MyType?> (d3 + null, null, "#3b");
409 Assert (d, 9m, "#4");
411 Assert<decimal?> (d, null, "#4a");
415 void AddAssignEnumTest ()
417 dynamic d
= MyEnum
.Value_1
;
421 Assert (d
, MyEnum
.Value_2
, "#2");
423 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
425 Assert (d2
, MyEnumUlong
.Value_2
, "#3");
428 void AddAssignCheckedTest ()
433 int v
= int.MaxValue
;
434 AssertChecked (() => { d += v; Assert (d, 0, "#1-"); }
, "#1");
437 MyType v3
= new MyType (int.MaxValue
);
439 Assert (d
, new MyType (-2147483644), "#3-");
443 void AddAssignStringTest ()
448 Assert (d
, "foo->", "#1");
452 Assert (d
, "foo1", "#1a");
455 Assert (d
, "foo1", "#1b");
460 Assert (d
, "foo4", "#2");
463 void AddAssignEvent ()
467 // FIXME: Will have to special case events
476 Assert (d
& v
, false, "#1");
477 Assert (d
& true, true, "#1a");
481 Assert (d
& v2
, 42, "#2");
482 Assert (d
& 0, 0, "#2a");
485 MyType v3
= new MyType (30);
486 Assert (d
& v3
, new MyType (10), "#3");
487 dynamic d3
= new MyType (-7);
488 Assert
<MyType
> (d3
& new MyType (6), new MyType (0), "#3a");
490 d3
= new MyTypeImplicitOnly (6);
491 Assert (d3
& 11, 2, "#3b");
496 dynamic d
= MyEnum
.Value_1
;
498 Assert
<MyEnum
?> (d
& null, null, "#1");
500 Assert (d
& d
, MyEnum
.Value_1
, "#2");
502 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
503 Assert
<MyEnumUlong
> (d2
& MyEnumUlong
.Value_2
, 0, "#3");
506 void AndTestNullable ()
511 Assert
<int?> (d
& v2
, null, "#1");
512 Assert
<int?> (d
& null, null, "#1a");
513 Assert
<int?> (null & d
, null, "#1b");
516 Assert (d
& v2
, 4, "#2");
517 dynamic d2
= (int?) -2;
518 Assert (d2
& 1, 0, "#2a");
521 MyType
? v3
= new MyType (30);
522 Assert (d
& v3
, new MyType (22), "#3");
523 dynamic d3
= new MyType
? (new MyType (-7));
524 Assert (d3
& new MyType (6), new MyType (0), "#3a");
525 Assert
<MyType
?> (d3
+ null, null, "#3b");
528 void AndAssignedTest ()
534 Assert (d
, false, "#1");
537 Assert (d
, true, "#1a");
542 Assert (d
, 42, "#2");
544 MyType v3
= new MyType (30);
545 dynamic d3
= new MyType (-7);
546 d3
&= new MyType (6);
547 Assert
<MyType
> (d3
, new MyType (0), "#3");
550 void AndAssignedTestEnum ()
552 dynamic d
= MyEnum
.Value_1
;
554 Assert
<MyEnum
>(d
, 0, "#1");
558 Assert (d
, MyEnum
.Value_2
, "#2");
560 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
561 Assert
<MyEnumUlong
> (d2
& MyEnumUlong
.Value_2
, 0, "#3");
569 Assert
<bool> (d
&& v
, false, "#1");
571 Assert (d
&& true, true, "#1a");
574 Assert (d
&& d
, true, "#2");
576 dynamic d3
= new MyType (-7);
577 Assert
<MyType
> (d3
&& new MyType (6), new MyType (0), "#3");
585 Assert (d
/ v
, 2, "#1");
588 MyType v3
= new MyType (30);
589 Assert (d
/ v3
, new MyType (0), "#3");
590 dynamic d3
= new MyType (-7);
591 Assert
<MyType
> (d3
+ new MyType (6), new MyType (-1), "#3a");
593 d
= new MyTypeImplicitOnly (6);
595 Assert (d
/ v4
, 1.5m
, "#4");
598 void DivideNullableTest ()
603 Assert
<double?> (d
/ v2
, null, "#1");
604 Assert
<double?> (d
/ null, null, "#1a");
605 Assert
<double?> (null / d
, null, "#1b");
608 Assert (d
/ v2
, -2.5, "#2");
609 dynamic d2
= (int?) -2;
610 Assert (d2
/ 1, -2, "#2a");
613 MyType
? v3
= new MyType (30);
614 Assert (d
/ v3
, new MyType (0), "#3");
615 dynamic d3
= new MyType
? (new MyType (-7));
616 Assert (d3
/ new MyType (6), new MyType (-1), "#3a");
617 Assert
<MyType
?> (d3
+ null, null, "#3b");
619 d
= new MyTypeImplicitOnly (5);
621 Assert (d
/ v4
, 1.25m
, "#4");
623 Assert
<decimal?> (d
/ v4
, null, "#4a");
626 void DivideCheckedTest ()
633 void DivideAssignTest ()
644 Assert (d
, 10, "#1a");
646 Assert (d
, 5, "#1b");
648 dynamic d3
= new MyType (-7);
649 d3
/= new MyType (6);
650 Assert
<MyType
> (d3
, new MyType (-1), "#3");
655 Assert (d
, 1.25m
, "#4");
658 void DivideAssignCheckedTest ()
665 void ConvertImplicitTest ()
669 Assert (3m
, v1
, "#1");
671 d
= new MyTypeImplicitOnly (5);
673 Assert (5, v2
, "#2");
677 Assert (4, v3
, "#3");
679 int[] v4
= new int[] { d }
;
680 Assert (4, v4
[0], "#4");
683 var v5
= new [] { d, 1 }
;
684 Assert (true, v5
[0], "#5");
685 Assert (1, v5
[1], "#5a");
689 var r
= b
? d
: "ss";
690 Assert ("ss", r
, "#6");
693 int ConvertImplicitReturnTest ()
695 dynamic d
= (byte) 3;
699 IEnumerable
<string> ConvertImplicitReturnTest_2 ()
705 void ConvertExplicitTest ()
708 Assert (44, (byte) d
, "#1");
709 Assert
<byte?> (44, (byte?) d
, "#1a");
714 d
= new MyTypeImplicitOnly (5);
715 Assert (5, (int) d
, "#3");
717 d
= new MyTypeExplicit (-2);
718 Assert (-2, (int) d
, "#4");
721 Assert (null, (object) d
, "#5");
724 void ConvertExplicitCheckedTest ()
728 AssertChecked (() => (byte) d
, 7, "#1");
731 AssertChecked
<uint?> (() => (uint?) d
, 2, "#2");
737 dynamic idx
= (uint) 1;
738 var arr
= new int [5];
740 Assert (2, arr
[idx
], "#1");
748 Assert (d
== v
, false, "#1");
750 Assert (d
== v2
, true, "#1a");
753 Assert (d
== false, false, "#2");
755 Assert (d
== b2
, true, "#2a");
758 MyType v3
= new MyType (30);
759 Assert (d
== v3
, true, "#3");
760 dynamic d3
= new MyTypeImplicitOnly (-7);
761 Assert (d3
== 11, false, "#3b");
765 Assert (d
== v4
, false, "#4");
766 Assert (d
== 2m
, true, "#4a");
769 Assert (d
== null, true, "#5");
772 void EqualNullableTest ()
777 Assert (d
== v2
, false, "#1");
778 Assert (d
== null, false, "#1a");
779 Assert (null == d
, false, "#1b");
782 Assert (d
== v2
, false, "#2");
783 dynamic d2
= (int?) -2;
784 Assert (d2
== 1, false, "#2a");
786 Assert (d2
== 44, true, "#2b");
789 MyType
? v3
= new MyType (30);
790 Assert (d
== v3
, true, "#3");
791 dynamic d3
= new MyType
? (new MyType (-7));
792 Assert (d3
== new MyType (6), false, "#3a");
793 Assert (d3
== null, false, "#3b");
797 Assert (d
== v4
, false, "#4");
799 Assert (d
== v4
, false, "#4a");
802 Assert (d
== true, true, "#5");
803 Assert (d
== null, false, "#5a");
804 Assert (d
== false, false, "#5b");
807 void EqualEnumTest ()
809 dynamic d
= MyEnum
.Value_1
;
811 Assert (d
== null, false, "#1");
813 Assert (d
== MyEnum
.Value_1
, true, "#2");
814 Assert (d
== 0, false, "#2a");
816 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
817 Assert (d2
== MyEnumUlong
.Value_2
, true, "#3");
818 Assert (d2
== null, false, "#3a");
821 void EqualStringTest ()
825 Assert (d
== "te", false, "#1");
826 Assert (d
== "text", true, "#1a");
827 Assert (d
== null, false, "#1b");
830 void EqualDelegateTest ()
834 // Assert (d == delegate { }, true, "#1");
836 EmptyDelegate b
= EqualDelegateTest
;
839 //Assert (d == EqualDelegateTest, true, "#2");
843 void EqualTestDelegate_2 ()
845 EmptyDelegate ed = delegate () {};
847 Expression<Func<EmptyDelegate, EmptyDelegate, bool>> e2 = (a, b) => a == b;
848 AssertNodeType (e2, ExpressionType.Equal);
849 Assert (false, e2.Compile ().Invoke (delegate () {}, null));
850 Assert (false, e2.Compile ().Invoke (delegate () {}, delegate {}));
851 Assert (false, e2.Compile ().Invoke (ed, delegate {}));
852 Assert (true, e2.Compile ().Invoke (ed, ed));
856 void ExclusiveOrTest ()
861 Assert (d ^ v
, true, "#1");
862 Assert (d ^
true, false, "#1a");
866 Assert (d ^ v2
, 20, "#2");
867 Assert (d ^
0, 42, "#2a");
870 MyType v3
= new MyType (30);
871 Assert (d ^ v3
, new MyType (52), "#3");
872 dynamic d3
= new MyType (-7);
873 Assert
<MyType
> (d3 ^
new MyType (6), new MyType (-1), "#3a");
875 d3
= new MyTypeImplicitOnly (-7);
876 Assert (d3 ^
11, -14, "#3b");
879 void ExclusiveOrNullableTest ()
884 Assert
<int?> (d ^ v2
, null, "#1");
885 Assert
<int?> (d ^
null, null, "#1a");
886 Assert
<int?> (null ^ d
, null, "#1b");
889 Assert (d ^ v2
, -5, "#2");
890 dynamic d2
= (int?) -2;
891 Assert (d2 ^
1, -1, "#2a");
894 MyType
? v3
= new MyType (30);
895 Assert (d ^ v3
, new MyType (27), "#3");
896 dynamic d3
= new MyType
? (new MyType (-7));
897 Assert (d3 ^
new MyType (6), new MyType (-1), "#3a");
898 Assert
<MyType
?> (d3 ^
null, null, "#3b");
901 void ExclusiveOrTestEnum ()
903 dynamic d
= MyEnum
.Value_1
;
905 Assert
<MyEnum
?> (d ^
null, null, "#1");
907 Assert
<MyEnum
> (d ^ d
, 0, "#2");
909 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
910 Assert
<MyEnumUlong
> (d2 ^ MyEnumUlong
.Value_2
, (MyEnumUlong
) 3, "#3");
913 void ExclusiveOrAssignedTest ()
919 Assert (d
, true, "#1");
922 Assert (d
, false, "#1a");
927 Assert (d
, 20, "#2");
929 MyType v3
= new MyType (30);
930 dynamic d3
= new MyType (-7);
931 d3 ^
= new MyType (6);
932 Assert (d3
, new MyType (-1), "#3");
935 void ExclusiveOrAssignedTestEnum ()
937 dynamic d
= MyEnum
.Value_1
;
939 Assert
<MyEnum
>(d
, (MyEnum
) 3, "#1");
943 Assert
<MyEnum
> (d
, 0, "#2");
945 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
946 Assert (d2 ^ MyEnumUlong
.Value_2
, (MyEnumUlong
)3, "#3");
949 void GreaterThanTest ()
954 Assert (d
> v
, true, "#1");
956 Assert (d
> v2
, false, "#1a");
959 Assert (d
> 4.59, true, "#2");
961 Assert (d
> b2
, false, "#2a");
964 MyType v3
= new MyType (30);
965 Assert (d
> v3
, false, "#3");
966 dynamic d3
= new MyType (-7);
967 Assert (d3
> new MyType (6), false, "#3a");
969 d3
= new MyTypeImplicitOnly (-7);
970 Assert (d3
> 11, false, "#3b");
974 Assert (d
> v4
, false, "#4");
975 Assert (d
> 2m
, false, "#4a");
978 void GreaterThanNullableTest ()
983 Assert (d
> v2
, false, "#1");
984 Assert (d
> null, false, "#1a");
985 Assert (null > d
, false, "#1b");
988 Assert (d
> v2
, true, "#2");
989 dynamic d2
= (int?) -2;
990 Assert (d2
> 1, false, "#2a");
992 Assert (d2
> 44, false, "#2b");
995 MyType
? v3
= new MyType (30);
996 Assert (d
> v3
, false, "#3");
997 dynamic d3
= new MyType
? (new MyType (-7));
998 Assert (d3
> new MyType (6), false, "#3a");
999 Assert (d3
> null, false, "#3b");
1003 Assert (d
> v4
, true, "#4");
1005 Assert (d
> v4
, false, "#4a");
1008 void GreaterThanEnumTest ()
1010 dynamic d
= MyEnum
.Value_1
;
1012 Assert (d
> null, false, "#1");
1014 Assert (d
> MyEnum
.Value_1
, false, "#2");
1015 Assert (d
> 0, true, "#2a");
1017 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1018 Assert (d2
> MyEnumUlong
.Value_2
, false, "#3");
1019 Assert (d2
> null, false, "#3a");
1022 void GreaterThanEqualTest ()
1027 Assert (d
>= v
, true, "#1");
1029 Assert (d
>= v2
, true, "#1a");
1032 Assert (d
>= 4.59, true, "#2");
1034 Assert (d
>= b2
, true, "#2a");
1036 d
= new MyType (30);
1037 MyType v3
= new MyType (30);
1038 Assert (d
>= v3
, true, "#3");
1039 dynamic d3
= new MyType (-7);
1040 Assert (d3
>= new MyType (6), false, "#3a");
1042 d3
= new MyTypeImplicitOnly (-7);
1043 Assert (d3
>= 11, false, "#3b");
1047 Assert (d
>= v4
, false, "#4");
1048 Assert (d
>= 2m
, true, "#4a");
1051 void GreaterThanEqualNullableTest ()
1056 Assert (d
>= v2
, false, "#1");
1057 Assert (d
>= null, false, "#1a");
1058 Assert (null >= d
, false, "#1b");
1061 Assert (d
>= v2
, true, "#2");
1062 dynamic d2
= (int?) -2;
1063 Assert (d2
>= 1, false, "#2a");
1065 Assert (d2
>= 44, true, "#2b");
1067 d
= new MyType (30);
1068 MyType
? v3
= new MyType (30);
1069 Assert (d
>= v3
, true, "#3");
1070 dynamic d3
= new MyType
? (new MyType (-7));
1071 Assert (d3
>= new MyType (6), false, "#3a");
1072 Assert (d3
>= null, false, "#3b");
1076 Assert (d
>= v4
, true, "#4");
1078 Assert (d
>= v4
, false, "#4a");
1081 void GreaterThanEqualEnumTest ()
1083 dynamic d
= MyEnum
.Value_1
;
1085 Assert (d
>= null, false, "#1");
1087 Assert (d
>= MyEnum
.Value_1
, true, "#2");
1088 Assert (d
>= 0, true, "#2a");
1090 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1091 Assert (d2
>= MyEnumUlong
.Value_2
, true, "#3");
1092 Assert (d2
>= null, false, "#3a");
1095 void LeftShiftTest ()
1097 dynamic d
= (ulong) 0x7F000;
1100 Assert
<ulong> (d
<< v
, 0x1FC000, "#1");
1101 Assert
<ulong> (d
<< 1, 0xFE000, "#1a");
1103 Assert
<ulong> (d
<< s
, 0x1FC000, "#1b");
1106 MyTypeImplicitOnly v3
= new MyTypeImplicitOnly (3);
1107 Assert (d
<< v3
, 0x3F8000, "#3");
1108 dynamic d3
= new MyType (-7);
1109 Assert (d3
<< new MyTypeImplicitOnly (6), -448, "#3a");
1110 Assert (d3
<< 11, -14336, "#3b");
1113 void LeftShiftNullableTest ()
1118 Assert
<int?> (d
<< v2
, null, "#1");
1120 Assert
<int?> (d
<< null, null, "#1a");
1122 Assert
<int?> (null << d
, null, "#1b");
1125 Assert (d
<< v2
, 0x40000000, "#2");
1126 dynamic d2
= (int?) -2;
1127 Assert (d2
<< 1, -4, "#2a");
1130 void LeftShiftAssignTest ()
1132 dynamic d
= 0x7F000;
1136 Assert (d
, 0x1FC000, "#1");
1138 Assert (d
, 0x3F8000, "#1a");
1141 Assert (d
, 0xFE0000, "#1b");
1144 void LeftShiftAssignNullableTest ()
1150 Assert (d
, 0x40000000, "#2");
1151 dynamic d2
= (int?) -2;
1153 Assert (d2
, -4, "#2a");
1156 void LessThanTest ()
1161 Assert (d
< v
, false, "#1");
1163 Assert (d
< v2
, false, "#1a");
1166 Assert (d
< 4.59, false, "#2");
1168 Assert (d
< b2
, false, "#2a");
1170 d
= new MyType (30);
1171 MyType v3
= new MyType (30);
1172 Assert (d
< v3
, false, "#3");
1173 dynamic d3
= new MyType (-7);
1174 Assert (d3
< new MyType (6), true, "#3a");
1176 d3
= new MyTypeImplicitOnly (-7);
1177 Assert (d3
< 11, true, "#3b");
1181 Assert (d
< v4
, true, "#4");
1182 Assert (d
< 2m
, false, "#4a");
1185 void LessThanNullableTest ()
1190 Assert (d
< v2
, false, "#1");
1191 Assert (d
< null, false, "#1a");
1192 Assert (null < d
, false, "#1b");
1195 Assert (d
< v2
, false, "#2");
1196 dynamic d2
= (int?) -2;
1197 Assert (d2
< 1, true, "#2a");
1199 Assert (d2
< 44, false, "#2b");
1201 d
= new MyType (30);
1202 MyType
? v3
= new MyType (30);
1203 Assert (d
< v3
, false, "#3");
1204 dynamic d3
= new MyType
? (new MyType (-7));
1205 Assert (d3
< new MyType (6), true, "#3a");
1207 d3
= new MyTypeImplicitOnly (-7);
1208 Assert (d3
< null, false, "#3b");
1212 Assert (d
< v4
, false, "#4");
1214 Assert (d
< v4
, false, "#4a");
1217 void LessThanEnumTest ()
1219 dynamic d
= MyEnum
.Value_1
;
1221 Assert (d
< null, false, "#1");
1223 Assert (d
< MyEnum
.Value_1
, false, "#2");
1224 Assert (d
< 0, false, "#2a");
1226 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1227 Assert (d2
< MyEnumUlong
.Value_2
, false, "#3");
1228 Assert (d2
< null, false, "#3a");
1231 void LessThanOrEqualTest ()
1236 Assert (d
<= v
, false, "#1");
1238 Assert (d
<= v2
, true, "#1a");
1241 Assert (d
<= 4.59, false, "#2");
1243 Assert (d
<= b2
, true, "#2a");
1245 d
= new MyType (30);
1246 MyType v3
= new MyType (30);
1247 Assert (d
<= v3
, true, "#3");
1248 dynamic d3
= new MyType (-7);
1249 Assert (d3
<= new MyType (6), true, "#3a");
1251 d3
= new MyTypeImplicitOnly (-7);
1252 Assert (d3
<= 11, true, "#3b");
1256 Assert (d
<= v4
, true, "#4");
1257 Assert (d
<= 2m
, true, "#4a");
1260 void LessThanOrEqualNullableTest ()
1265 Assert (d
<= v2
, false, "#1");
1266 Assert (d
<= null, false, "#1a");
1267 Assert (null <= d
, false, "#1b");
1270 Assert (d
<= v2
, false, "#2");
1271 dynamic d2
= (int?) -2;
1272 Assert (d2
<= 1, true, "#2a");
1274 Assert (d2
<= 44, true, "#2b");
1276 d
= new MyType (30);
1277 MyType
? v3
= new MyType (30);
1278 Assert (d
<= v3
, true, "#3");
1279 dynamic d3
= new MyType
? (new MyType (-7));
1280 Assert (d3
<= new MyType (6), true, "#3a");
1281 Assert (d3
<= null, false, "#3b");
1285 Assert (d
<= v4
, false, "#4");
1287 Assert (d
<= v4
, false, "#4a");
1290 void LessThanOrEqualEnumTest ()
1292 dynamic d
= MyEnum
.Value_1
;
1294 Assert (d
<= null, false, "#1");
1296 Assert (d
<= MyEnum
.Value_1
, true, "#2");
1297 Assert (d
<= 0, false, "#2a");
1299 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1300 Assert (d2
<= MyEnumUlong
.Value_2
, true, "#3");
1301 Assert (d2
<= null, false, "#3a");
1309 Assert (d
% v
, 1, "#1");
1312 MyType v3
= new MyType (30);
1313 Assert (d
% v3
, new MyType (5), "#3");
1314 dynamic d3
= new MyType (-7);
1315 Assert
<MyType
> (d3
% new MyType (6), new MyType (-1), "#3a");
1317 d
= new MyTypeImplicitOnly (5);
1319 Assert (d
% v4
, 1m
, "#4");
1322 void ModuloNullableTest ()
1327 Assert
<double?> (d
% v2
, null, "#1");
1328 Assert
<double?> (d
% null, null, "#1a");
1329 Assert
<double?> (null % d
, null, "#1b");
1332 Assert (d
% v2
, 1, "#2");
1333 dynamic d2
= (int?) -2;
1334 Assert (d2
% 1, 0, "#2a");
1336 d
= new MyType (-2);
1337 MyType
? v3
= new MyType (30);
1338 Assert (d
% v3
, new MyType (-2), "#3");
1339 dynamic d3
= new MyType
? (new MyType (-7));
1340 Assert (d3
% new MyType (6), new MyType (-1), "#3a");
1341 Assert
<MyType
?> (d3
+ null, null, "#3b");
1343 d
= new MyTypeImplicitOnly (5);
1345 Assert (d
% v4
, 1m
, "#4");
1347 Assert
<decimal?> (d
% v4
, null, "#4a");
1350 void ModuloAssignTest ()
1356 Assert (d
, 1, "#1");
1361 Assert (d
, 0, "#1a");
1363 Assert (d
, 0, "#1b");
1365 dynamic d3
= new MyType (-7);
1366 d3
%= new MyType (6);
1367 Assert
<MyType
> (d3
, new MyType (-1), "#3");
1372 Assert (d
, 1m
, "#4");
1375 void MultiplyTest ()
1380 Assert (d
* v
, 10, "#1");
1382 Assert (d
* v2
, 2.5, "#1a");
1385 MyType v3
= new MyType (30);
1386 Assert (d
* v3
, new MyType (150), "#3");
1387 dynamic d3
= new MyType (-7);
1388 Assert
<MyType
> (d3
* new MyType (6), new MyType (-42), "#3a");
1392 Assert (d
* v4
, 31.6m
, "#4");
1395 void MultiplyNullableTest ()
1400 Assert
<int?> (d
* v2
, null, "#1");
1401 Assert
<int?> (d
* null, null, "#1a");
1402 Assert
<int?> (null * d
, null, "#1b");
1405 Assert (d
* v2
, -10, "#2");
1406 dynamic d2
= (int?) -2;
1407 Assert (d2
* 1, -2, "#2a");
1410 MyType
? v3
= new MyType (30);
1411 Assert (d
* v3
, new MyType (150), "#3");
1412 dynamic d3
= new MyType
? (new MyType (-7));
1413 Assert (d3
* new MyType (6), new MyType (-42), "#3a");
1414 Assert
<MyType
?> (d3
* null, null, "#3b");
1416 d
= new MyTypeImplicitOnly (5);
1418 Assert (d
* v4
, 20m
, "#4");
1420 Assert
<decimal?> (d
* v4
, null, "#4a");
1423 void MultiplyCheckedTest ()
1428 int v
= int.MaxValue
;
1429 AssertChecked (() => d
* v
, 7, "#1");
1432 AssertChecked (() => d
* v2
, null, "#2");
1435 MyType v3
= new MyType (int.MaxValue
);
1436 Assert (d
* v3
, new MyType (-4), "#3");
1440 void MultiplyAssignTest ()
1446 Assert (d
, 10, "#1");
1451 Assert (d
, 2.5, "#1a");
1453 Assert (d
, 5, "#1b");
1455 dynamic d3
= new MyType (-7);
1456 d3
*= new MyType (6);
1457 Assert
<MyType
> (d3
, new MyType (-42), "#3");
1462 Assert (d
, 20m
, "#4");
1467 Assert (i
, 15, "#5");
1470 void MultiplyAssignCheckedTest ()
1475 int v
= int.MaxValue
;
1476 AssertChecked (() => { d *= v; Assert (d, 0, "#1-"); }
, "#1");
1478 d
= new MyType (44);
1479 MyType v3
= new MyType (int.MaxValue
);
1481 Assert (d
, new MyType (-44), "#3-");
1488 Assert (8, -d
, "#1");
1489 Assert (-8, -(-d
), "#1a");
1491 d
= new MyType (-14);
1492 Assert (new MyType (14), -d
, "#2");
1494 d
= new MyTypeImplicitOnly (4);
1495 Assert (-4, -d
, "#3");
1498 Assert (-7, -d
, "#4");
1500 d
= double.NegativeInfinity
;
1501 Assert (double.PositiveInfinity
, -d
, "#5");
1504 void NegateNullable ()
1506 dynamic d
= (int?) -8;
1507 Assert (8, -d
, "#1");
1508 Assert (-8, -(-d
), "#1a");
1510 MyType
? n1
= new MyType (4);
1512 Assert (new MyType (-4), -d
, "#2");
1514 MyTypeImplicitOnly
? n2
= new MyTypeImplicitOnly (4);
1516 Assert (-4, -d
, "#3");
1519 Assert (-7, -d
, "#4");
1522 void NegateChecked ()
1525 dynamic d
= int.MinValue
;
1526 AssertChecked (() => -d
, 0, "#1");
1533 Assert (false, !d
, "#1");
1535 var de
= new MyType (-1);
1536 Assert (false, !d
, "#2");
1539 void NotEqualTest ()
1544 Assert (d
!= v
, true, "#1");
1546 Assert (d
!= v2
, false, "#1a");
1549 Assert (d
!= false, true, "#2");
1551 Assert (d
!= b2
, false, "#2a");
1553 d
= new MyType (30);
1554 MyType v3
= new MyType (30);
1555 Assert (d
!= v3
, false, "#3");
1556 dynamic d3
= new MyType (-7);
1557 Assert (d3
!= new MyType (6), true, "#3a");
1561 Assert (d
!= v4
, true, "#4");
1562 Assert (d
!= 2m
, false, "#4a");
1565 Assert (d
!= null, false, "#5");
1568 void NotEqualNullableTest ()
1573 Assert (d
!= v2
, true, "#1");
1574 Assert (d
!= null, true, "#1a");
1575 Assert (null != d
, true, "#1b");
1578 Assert (d
!= v2
, true, "#2");
1579 dynamic d2
= (int?) -2;
1580 Assert (d2
!= 1, true, "#2a");
1582 Assert (d2
!= 44, false, "#2b");
1584 d
= new MyType (30);
1585 MyType
? v3
= new MyType (30);
1586 Assert (d
!= v3
, false, "#3");
1587 dynamic d3
= new MyType
? (new MyType (-7));
1588 Assert (d3
!= new MyType (6), true, "#3a");
1589 Assert (d3
!= null, true, "#3b");
1593 Assert (d
!= v4
, true, "#4");
1595 Assert (d
!= v4
, true, "#4a");
1598 Assert (d
!= true, false, "#5");
1599 Assert (d
!= null, true, "#5a");
1600 Assert (d
!= false, true, "#5b");
1603 void NotEqualEnumTest ()
1605 dynamic d
= MyEnum
.Value_1
;
1607 Assert (d
!= null, true, "#1");
1609 Assert (d
!= MyEnum
.Value_1
, false, "#2");
1610 Assert (d
!= 0, true, "#2a");
1612 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1613 Assert (d2
!= MyEnumUlong
.Value_2
, false, "#3");
1614 Assert (d2
!= null, true, "#3a");
1617 void NotEqualStringTest ()
1621 Assert (d
!= "te", true, "#1");
1622 Assert (d
!= "text", false, "#1a");
1623 Assert (d
!= null, true, "#1b");
1626 void OnesComplement ()
1629 Assert (-8, ~d
, "#1");
1631 d
= new MyType (-1);
1632 Assert (0, ~d
, "#2");
1635 Assert (18446744073709551608, ~d
, "#3");
1638 Assert ((MyEnum
) 254, ~d
, "#4");
1641 void OnesComplementNullable ()
1643 dynamic d
= (int?) 7;
1644 Assert (-8, ~d
, "#1");
1646 d
= (MyEnum
?) MyEnum
.Value_1
;
1647 Assert ((MyEnum
) 254, ~d
, "#4");
1655 Assert (d
| v
, true, "#1");
1656 Assert (d
| false, true, "#1a");
1660 Assert (d
| v2
, 62, "#2");
1661 Assert (d
| 0, 42, "#2a");
1663 d
= new MyType (42);
1664 MyType v3
= new MyType (30);
1665 Assert (d
| v3
, new MyType (62), "#3");
1666 dynamic d3
= new MyType (-7);
1667 Assert
<MyType
> (d3
| new MyType (6), new MyType (-1), "#3a");
1669 d3
= new MyTypeImplicitOnly (-7);
1670 Assert (d3
| 11, -5, "#3b");
1675 dynamic d
= MyEnum
.Value_1
;
1677 Assert
<MyEnum
?> (d
| null, null, "#1");
1679 Assert (d
| d
, MyEnum
.Value_1
, "#2");
1681 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
1682 Assert
<MyEnumUlong
> (d2
| MyEnumUlong
.Value_2
, (MyEnumUlong
) 3, "#3");
1685 void OrTestNullable ()
1690 Assert
<int?> (d
| v2
, null, "#1");
1691 Assert
<int?> (d
| null, null, "#1a");
1692 Assert
<int?> (null | d
, null, "#1b");
1695 Assert (d
| v2
, -1, "#2");
1696 dynamic d2
= (int?) -2;
1697 Assert (d2
| 1, -1, "#2a");
1699 d
= new MyType (-2);
1700 MyType
? v3
= new MyType (30);
1701 Assert (d
| v3
, new MyType (-2), "#3");
1702 dynamic d3
= new MyType
? (new MyType (-7));
1703 Assert (d3
| new MyType (6), new MyType (-1), "#3a");
1706 void OrAssignedTest ()
1712 Assert (d
, true, "#1");
1715 Assert (d
, true, "#1a");
1720 Assert (d
, 62, "#2");
1722 MyType v3
= new MyType (30);
1723 dynamic d3
= new MyType (-7);
1724 d3
|= new MyType (6);
1725 Assert
<MyType
> (d3
, new MyType (-1), "#3");
1728 void OrAssignedTestEnum ()
1730 dynamic d
= MyEnum
.Value_1
;
1731 d
|= MyEnum
.Value_2
;
1732 Assert
<MyEnum
> (d
, (MyEnum
) 3, "#1");
1736 Assert (d
, MyEnum
.Value_2
, "#2");
1738 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
1739 Assert
<MyEnumUlong
> (d2
| MyEnumUlong
.Value_2
, (MyEnumUlong
)3, "#3");
1747 Assert
<bool> (d
|| v
, true, "#1");
1749 Assert (d
|| true, true, "#1a");
1752 Assert (d
|| d
, true, "#2");
1754 dynamic d3
= new MyType (-7);
1755 Assert
<MyType
> (d3
|| new MyType (6), new MyType (-1), "#3");
1758 void RightShiftTest ()
1760 dynamic d
= (ulong) 0x7F000;
1763 Assert
<ulong> (d
>> v
, 0x1FC00, "#1");
1764 Assert
<ulong> (d
>> 1, 0x3F800, "#1a");
1766 Assert
<ulong> (d
>> s
, 0x1FC00, "#1b");
1769 MyTypeImplicitOnly v3
= new MyTypeImplicitOnly (3);
1770 Assert (d
>> v3
, 0xFE00, "#3");
1771 dynamic d3
= new MyType (-7);
1772 Assert (d3
>> new MyTypeImplicitOnly (11), -1, "#3a");
1775 void RightShiftNullableTest ()
1780 Assert
<int?> (d
>> v2
, null, "#1");
1782 Assert
<int?> (d
>> null, null, "#1a");
1784 Assert
<int?> (null >> d
, null, "#1b");
1787 Assert (d
>> v2
, 0, "#2");
1788 dynamic d2
= (int?) -200;
1789 Assert (d2
>> 1, -100, "#2a");
1792 void RightShiftAssignTest ()
1794 dynamic d
= 0x7F000;
1798 Assert (d
, 0x1FC00, "#1");
1800 Assert (d
, 0xFE00, "#1a");
1803 Assert (d
, 0x3F80, "#1b");
1806 void RightShiftAssignNullableTest ()
1812 Assert (d
, 0, "#2");
1813 dynamic d2
= (int?) -2;
1815 Assert (d2
, -1, "#2a");
1818 void SubtractTest ()
1823 Assert (d
- v
, 3, "#1");
1825 Assert (d
- v2
, 4.5, "#1a");
1828 MyType v3
= new MyType (30);
1829 Assert (d
- v3
, new MyType (-25), "#3");
1830 dynamic d3
= new MyType (-7);
1831 Assert (d3
- new MyType (6), new MyType (-13), "#3a");
1833 d
= new MyTypeImplicitOnly (5);
1835 Assert (d
- v4
, 1m
, "#4");
1838 void SubtractNullableTest ()
1843 Assert
<int?> (d
- v2
, null, "#1");
1844 Assert
<int?> (d
- null, null, "#1a");
1845 Assert
<int?> (null - d
, null, "#1b");
1848 Assert (d
- v2
, 7, "#2");
1849 dynamic d2
= (int?) -2;
1850 Assert (d2
- 1, -3, "#2a");
1853 MyType
? v3
= new MyType (30);
1854 Assert (d
- v3
, new MyType (-25), "#3");
1855 dynamic d3
= new MyType
? (new MyType (-7));
1856 Assert (d3
- new MyType (6), new MyType (-13), "#3a");
1857 Assert
<MyType
?> (d3
- null, null, "#3b");
1859 d
= new MyTypeImplicitOnly (5);
1861 Assert (d
- v4
, 1m
, "#4");
1863 Assert
<decimal?> (d
- v4
, null, "#4a");
1866 void SubtractEnumTest ()
1868 dynamic d
= MyEnum
.Value_1
;
1870 Assert
<MyEnum
> (d
- 1, 0, "#1");
1872 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1873 Assert (d2
- (byte) 1, MyEnumUlong
.Value_1
, "#2");
1874 Assert
<MyEnumUlong
?> (d2
- (object) null, null, "#2a");
1876 // CSC: Invalid System.InvalidOperationException
1877 Assert
<MyEnum
?> (d
- null, null, "#3");
1880 void SubtractCheckedTest ()
1885 int v
= int.MinValue
;
1886 AssertChecked (() => d
- v
, 7, "#1");
1889 AssertChecked (() => d
- v2
, null, "#2");
1892 MyType v3
= new MyType (int.MinValue
);
1893 Assert (d
- v3
, new MyType (-2147483643), "#3");
1897 void SubtractAssignTest ()
1903 Assert (d
, 3, "#1");
1908 Assert (d
, 4.5, "#1a");
1910 Assert (d
, 2.5, "#1b");
1912 dynamic d3
= new MyType (-7);
1913 d3
-= new MyType (6);
1914 Assert
<MyType
> (d3
, new MyType (-13), "#3");
1919 Assert (d
, 1m
, "#4");
1922 void SubtractAssignEnumTest ()
1924 dynamic d
= MyEnum
.Value_1
;
1927 Assert
<MyEnum
> (d
, 0, "#2");
1929 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1931 Assert (d2
, MyEnumUlong
.Value_1
, "#3");
1934 void SubtractAssignCheckedTest ()
1939 int v
= int.MinValue
;
1940 AssertChecked (() => { d -= v; Assert (d, 0, "#1a"); }
, "#1");
1943 MyType v3
= new MyType (int.MinValue
);
1945 Assert (d
, new MyType (-2147483643), "#3a");
1949 void SubtractAssignEvent ()
1951 Action print
= () => { Console.WriteLine ("foo"); }
;
1954 // FIXME: Will have to special case events
1959 void UnaryDecrement ()
1962 Assert (3, d
--, "#1");
1963 Assert (2, d
, "#1a");
1966 Assert (2, --d
, "#2");
1967 Assert (2, d
, "#2a");
1969 d
= new MyType (-3);
1970 Assert (new MyType (-3), d
--, "#3");
1971 Assert (new MyType (-1), d
, "#3a");
1974 void UnaryDecrementCheckedTest ()
1977 dynamic d
= int.MinValue
;
1979 AssertChecked (() => { d--; Assert (d, -1073741824, "#1a"); }
, "#1");
1981 d
= new MyType (int.MinValue
);
1983 Assert (d
, new MyType (-1073741824), "#2");
1987 void UnaryIncrement ()
1990 Assert (3, d
++, "#1");
1991 Assert (4, d
, "#1a");
1994 Assert (4, ++d
, "#2");
1995 Assert (4, d
, "#2a");
1997 d
= new MyType (-3);
1998 Assert (new MyType (-3), d
++, "#3");
1999 Assert (new MyType (-6), d
, "#3a");
2002 void UnaryIncrementCheckedTest ()
2005 dynamic d
= int.MaxValue
;
2007 AssertChecked (() => { d++; Assert (d, 0, "#1a"); }
, "#1");
2009 d
= new MyType (int.MaxValue
);
2011 Assert (d
, new MyType (-2), "#2");
2015 //void UnaryIsFalse ()
2017 // dynamic d = this;
2018 // object r = d == null;
2019 // Assert (false, (bool) r, "#1");
2020 // Assert<object> (true, d != null, "#1a");
2026 Assert (3, d
? 3 : 5, "#1");
2029 Assert (false, d
< 1, "#2");
2031 d
= new InverseLogicalOperator (true);
2032 Assert (1, d
? 1 : -1, "#3");
2038 Assert (-8, +d
, "#1");
2039 Assert (-8, +(+d
), "#1a");
2041 d
= new MyType (14);
2042 Assert (new MyType (334455), +d
, "#2");
2044 d
= new MyTypeImplicitOnly (4);
2045 Assert (4, +d
, "#3");
2048 Assert
<uint> (7, +d
, "#4");
2051 void UnaryPlusNullable ()
2053 dynamic d
= (int?) -8;
2054 Assert (-8, +d
, "#1");
2055 Assert (-8, +(+d
), "#1a");
2057 MyType
? n1
= new MyType (4);
2059 Assert (new MyType (334455), +d
, "#2");
2061 MyTypeImplicitOnly
? n2
= new MyTypeImplicitOnly (4);
2063 Assert (4, +d
, "#3");
2066 Assert (7, +d
, "#4");
2069 #pragma warning restore 169
2071 static bool RunTest (MethodInfo test
)
2073 Console
.Write ("Running test {0, -25}", test
.Name
);
2075 test
.Invoke (new Tester (), null);
2076 Console
.WriteLine ("OK");
2078 } catch (Exception e
) {
2079 Console
.WriteLine ("FAILED");
2080 Console
.WriteLine (e
.ToString ());
2085 public static int Main ()
2087 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
2088 where test
.GetParameters ().Length
== 0
2090 select RunTest (test
);
2092 int failures
= tests
.Count (a
=> !a
);
2093 Console
.WriteLine (failures
+ " tests failed");