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");
692 var v
= new [] { d, 1 }
;
693 Assert ("aa", v
[0], "#7");
695 dynamic [,] a
= new dynamic [,] { { 1, 2 }
, { 'b', 'x' }
};
696 Assert (2, a
[0, 1], "#8");
697 Assert ('x', a
[1, 1], "#8a");
700 int ConvertImplicitReturnTest ()
702 dynamic d
= (byte) 3;
706 IEnumerable
<string> ConvertImplicitReturnTest_2 ()
712 void ConvertExplicitTest ()
715 Assert (44, (byte) d
, "#1");
716 Assert
<byte?> (44, (byte?) d
, "#1a");
721 d
= new MyTypeImplicitOnly (5);
722 Assert (5, (int) d
, "#3");
724 d
= new MyTypeExplicit (-2);
725 Assert (-2, (int) d
, "#4");
728 Assert (null, (object) d
, "#5");
731 void ConvertExplicitCheckedTest ()
735 AssertChecked (() => (byte) d
, 7, "#1");
738 AssertChecked
<uint?> (() => (uint?) d
, 2, "#2");
744 dynamic idx
= (uint) 1;
745 var arr
= new int [5];
747 Assert (2, arr
[idx
], "#1");
755 Assert (d
== v
, false, "#1");
757 Assert (d
== v2
, true, "#1a");
760 Assert (d
== false, false, "#2");
762 Assert (d
== b2
, true, "#2a");
765 MyType v3
= new MyType (30);
766 Assert (d
== v3
, true, "#3");
767 dynamic d3
= new MyTypeImplicitOnly (-7);
768 Assert (d3
== 11, false, "#3b");
772 Assert (d
== v4
, false, "#4");
773 Assert (d
== 2m
, true, "#4a");
776 Assert (d
== null, true, "#5");
779 void EqualNullableTest ()
784 Assert (d
== v2
, false, "#1");
785 Assert (d
== null, false, "#1a");
786 Assert (null == d
, false, "#1b");
789 Assert (d
== v2
, false, "#2");
790 dynamic d2
= (int?) -2;
791 Assert (d2
== 1, false, "#2a");
793 Assert (d2
== 44, true, "#2b");
796 MyType
? v3
= new MyType (30);
797 Assert (d
== v3
, true, "#3");
798 dynamic d3
= new MyType
? (new MyType (-7));
799 Assert (d3
== new MyType (6), false, "#3a");
800 Assert (d3
== null, false, "#3b");
804 Assert (d
== v4
, false, "#4");
806 Assert (d
== v4
, false, "#4a");
809 Assert (d
== true, true, "#5");
810 Assert (d
== null, false, "#5a");
811 Assert (d
== false, false, "#5b");
814 void EqualEnumTest ()
816 dynamic d
= MyEnum
.Value_1
;
818 Assert (d
== null, false, "#1");
820 Assert (d
== MyEnum
.Value_1
, true, "#2");
821 Assert (d
== 0, false, "#2a");
823 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
824 Assert (d2
== MyEnumUlong
.Value_2
, true, "#3");
825 Assert (d2
== null, false, "#3a");
828 void EqualStringTest ()
832 Assert (d
== "te", false, "#1");
833 Assert (d
== "text", true, "#1a");
834 Assert (d
== null, false, "#1b");
837 void EqualDelegateTest ()
841 // Assert (d == delegate { }, true, "#1");
843 EmptyDelegate b
= EqualDelegateTest
;
846 //Assert (d == EqualDelegateTest, true, "#2");
850 void EqualTestDelegate_2 ()
852 EmptyDelegate ed = delegate () {};
854 Expression<Func<EmptyDelegate, EmptyDelegate, bool>> e2 = (a, b) => a == b;
855 AssertNodeType (e2, ExpressionType.Equal);
856 Assert (false, e2.Compile ().Invoke (delegate () {}, null));
857 Assert (false, e2.Compile ().Invoke (delegate () {}, delegate {}));
858 Assert (false, e2.Compile ().Invoke (ed, delegate {}));
859 Assert (true, e2.Compile ().Invoke (ed, ed));
863 void ExclusiveOrTest ()
868 Assert (d ^ v
, true, "#1");
869 Assert (d ^
true, false, "#1a");
873 Assert (d ^ v2
, 20, "#2");
874 Assert (d ^
0, 42, "#2a");
877 MyType v3
= new MyType (30);
878 Assert (d ^ v3
, new MyType (52), "#3");
879 dynamic d3
= new MyType (-7);
880 Assert
<MyType
> (d3 ^
new MyType (6), new MyType (-1), "#3a");
882 d3
= new MyTypeImplicitOnly (-7);
883 Assert (d3 ^
11, -14, "#3b");
886 void ExclusiveOrNullableTest ()
891 Assert
<int?> (d ^ v2
, null, "#1");
892 Assert
<int?> (d ^
null, null, "#1a");
893 Assert
<int?> (null ^ d
, null, "#1b");
896 Assert (d ^ v2
, -5, "#2");
897 dynamic d2
= (int?) -2;
898 Assert (d2 ^
1, -1, "#2a");
901 MyType
? v3
= new MyType (30);
902 Assert (d ^ v3
, new MyType (27), "#3");
903 dynamic d3
= new MyType
? (new MyType (-7));
904 Assert (d3 ^
new MyType (6), new MyType (-1), "#3a");
905 Assert
<MyType
?> (d3 ^
null, null, "#3b");
908 void ExclusiveOrTestEnum ()
910 dynamic d
= MyEnum
.Value_1
;
912 Assert
<MyEnum
?> (d ^
null, null, "#1");
914 Assert
<MyEnum
> (d ^ d
, 0, "#2");
916 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
917 Assert
<MyEnumUlong
> (d2 ^ MyEnumUlong
.Value_2
, (MyEnumUlong
) 3, "#3");
920 void ExclusiveOrAssignedTest ()
926 Assert (d
, true, "#1");
929 Assert (d
, false, "#1a");
934 Assert (d
, 20, "#2");
936 MyType v3
= new MyType (30);
937 dynamic d3
= new MyType (-7);
938 d3 ^
= new MyType (6);
939 Assert (d3
, new MyType (-1), "#3");
942 void ExclusiveOrAssignedTestEnum ()
944 dynamic d
= MyEnum
.Value_1
;
946 Assert
<MyEnum
>(d
, (MyEnum
) 3, "#1");
950 Assert
<MyEnum
> (d
, 0, "#2");
952 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
953 Assert (d2 ^ MyEnumUlong
.Value_2
, (MyEnumUlong
)3, "#3");
956 void GreaterThanTest ()
961 Assert (d
> v
, true, "#1");
963 Assert (d
> v2
, false, "#1a");
966 Assert (d
> 4.59, true, "#2");
968 Assert (d
> b2
, false, "#2a");
971 MyType v3
= new MyType (30);
972 Assert (d
> v3
, false, "#3");
973 dynamic d3
= new MyType (-7);
974 Assert (d3
> new MyType (6), false, "#3a");
976 d3
= new MyTypeImplicitOnly (-7);
977 Assert (d3
> 11, false, "#3b");
981 Assert (d
> v4
, false, "#4");
982 Assert (d
> 2m
, false, "#4a");
985 void GreaterThanNullableTest ()
990 Assert (d
> v2
, false, "#1");
991 Assert (d
> null, false, "#1a");
992 Assert (null > d
, false, "#1b");
995 Assert (d
> v2
, true, "#2");
996 dynamic d2
= (int?) -2;
997 Assert (d2
> 1, false, "#2a");
999 Assert (d2
> 44, false, "#2b");
1001 d
= new MyType (30);
1002 MyType
? v3
= new MyType (30);
1003 Assert (d
> v3
, false, "#3");
1004 dynamic d3
= new MyType
? (new MyType (-7));
1005 Assert (d3
> new MyType (6), false, "#3a");
1006 Assert (d3
> null, false, "#3b");
1010 Assert (d
> v4
, true, "#4");
1012 Assert (d
> v4
, false, "#4a");
1015 void GreaterThanEnumTest ()
1017 dynamic d
= MyEnum
.Value_1
;
1019 Assert (d
> null, false, "#1");
1021 Assert (d
> MyEnum
.Value_1
, false, "#2");
1022 Assert (d
> 0, true, "#2a");
1024 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1025 Assert (d2
> MyEnumUlong
.Value_2
, false, "#3");
1026 Assert (d2
> null, false, "#3a");
1029 void GreaterThanEqualTest ()
1034 Assert (d
>= v
, true, "#1");
1036 Assert (d
>= v2
, true, "#1a");
1039 Assert (d
>= 4.59, true, "#2");
1041 Assert (d
>= b2
, true, "#2a");
1043 d
= new MyType (30);
1044 MyType v3
= new MyType (30);
1045 Assert (d
>= v3
, true, "#3");
1046 dynamic d3
= new MyType (-7);
1047 Assert (d3
>= new MyType (6), false, "#3a");
1049 d3
= new MyTypeImplicitOnly (-7);
1050 Assert (d3
>= 11, false, "#3b");
1054 Assert (d
>= v4
, false, "#4");
1055 Assert (d
>= 2m
, true, "#4a");
1058 void GreaterThanEqualNullableTest ()
1063 Assert (d
>= v2
, false, "#1");
1064 Assert (d
>= null, false, "#1a");
1065 Assert (null >= d
, false, "#1b");
1068 Assert (d
>= v2
, true, "#2");
1069 dynamic d2
= (int?) -2;
1070 Assert (d2
>= 1, false, "#2a");
1072 Assert (d2
>= 44, true, "#2b");
1074 d
= new MyType (30);
1075 MyType
? v3
= new MyType (30);
1076 Assert (d
>= v3
, true, "#3");
1077 dynamic d3
= new MyType
? (new MyType (-7));
1078 Assert (d3
>= new MyType (6), false, "#3a");
1079 Assert (d3
>= null, false, "#3b");
1083 Assert (d
>= v4
, true, "#4");
1085 Assert (d
>= v4
, false, "#4a");
1088 void GreaterThanEqualEnumTest ()
1090 dynamic d
= MyEnum
.Value_1
;
1092 Assert (d
>= null, false, "#1");
1094 Assert (d
>= MyEnum
.Value_1
, true, "#2");
1095 Assert (d
>= 0, true, "#2a");
1097 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1098 Assert (d2
>= MyEnumUlong
.Value_2
, true, "#3");
1099 Assert (d2
>= null, false, "#3a");
1102 void LeftShiftTest ()
1104 dynamic d
= (ulong) 0x7F000;
1107 Assert
<ulong> (d
<< v
, 0x1FC000, "#1");
1108 Assert
<ulong> (d
<< 1, 0xFE000, "#1a");
1110 Assert
<ulong> (d
<< s
, 0x1FC000, "#1b");
1113 MyTypeImplicitOnly v3
= new MyTypeImplicitOnly (3);
1114 Assert (d
<< v3
, 0x3F8000, "#3");
1115 dynamic d3
= new MyType (-7);
1116 Assert (d3
<< new MyTypeImplicitOnly (6), -448, "#3a");
1117 Assert (d3
<< 11, -14336, "#3b");
1120 void LeftShiftNullableTest ()
1125 Assert
<int?> (d
<< v2
, null, "#1");
1127 Assert
<int?> (d
<< null, null, "#1a");
1129 Assert
<int?> (null << d
, null, "#1b");
1132 Assert (d
<< v2
, 0x40000000, "#2");
1133 dynamic d2
= (int?) -2;
1134 Assert (d2
<< 1, -4, "#2a");
1137 void LeftShiftAssignTest ()
1139 dynamic d
= 0x7F000;
1143 Assert (d
, 0x1FC000, "#1");
1145 Assert (d
, 0x3F8000, "#1a");
1148 Assert (d
, 0xFE0000, "#1b");
1151 void LeftShiftAssignNullableTest ()
1157 Assert (d
, 0x40000000, "#2");
1158 dynamic d2
= (int?) -2;
1160 Assert (d2
, -4, "#2a");
1163 void LessThanTest ()
1168 Assert (d
< v
, false, "#1");
1170 Assert (d
< v2
, false, "#1a");
1173 Assert (d
< 4.59, false, "#2");
1175 Assert (d
< b2
, false, "#2a");
1177 d
= new MyType (30);
1178 MyType v3
= new MyType (30);
1179 Assert (d
< v3
, false, "#3");
1180 dynamic d3
= new MyType (-7);
1181 Assert (d3
< new MyType (6), true, "#3a");
1183 d3
= new MyTypeImplicitOnly (-7);
1184 Assert (d3
< 11, true, "#3b");
1188 Assert (d
< v4
, true, "#4");
1189 Assert (d
< 2m
, false, "#4a");
1192 void LessThanNullableTest ()
1197 Assert (d
< v2
, false, "#1");
1198 Assert (d
< null, false, "#1a");
1199 Assert (null < d
, false, "#1b");
1202 Assert (d
< v2
, false, "#2");
1203 dynamic d2
= (int?) -2;
1204 Assert (d2
< 1, true, "#2a");
1206 Assert (d2
< 44, false, "#2b");
1208 d
= new MyType (30);
1209 MyType
? v3
= new MyType (30);
1210 Assert (d
< v3
, false, "#3");
1211 dynamic d3
= new MyType
? (new MyType (-7));
1212 Assert (d3
< new MyType (6), true, "#3a");
1214 d3
= new MyTypeImplicitOnly (-7);
1215 Assert (d3
< null, false, "#3b");
1219 Assert (d
< v4
, false, "#4");
1221 Assert (d
< v4
, false, "#4a");
1224 void LessThanEnumTest ()
1226 dynamic d
= MyEnum
.Value_1
;
1228 Assert (d
< null, false, "#1");
1230 Assert (d
< MyEnum
.Value_1
, false, "#2");
1231 Assert (d
< 0, false, "#2a");
1233 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1234 Assert (d2
< MyEnumUlong
.Value_2
, false, "#3");
1235 Assert (d2
< null, false, "#3a");
1238 void LessThanOrEqualTest ()
1243 Assert (d
<= v
, false, "#1");
1245 Assert (d
<= v2
, true, "#1a");
1248 Assert (d
<= 4.59, false, "#2");
1250 Assert (d
<= b2
, true, "#2a");
1252 d
= new MyType (30);
1253 MyType v3
= new MyType (30);
1254 Assert (d
<= v3
, true, "#3");
1255 dynamic d3
= new MyType (-7);
1256 Assert (d3
<= new MyType (6), true, "#3a");
1258 d3
= new MyTypeImplicitOnly (-7);
1259 Assert (d3
<= 11, true, "#3b");
1263 Assert (d
<= v4
, true, "#4");
1264 Assert (d
<= 2m
, true, "#4a");
1267 void LessThanOrEqualNullableTest ()
1272 Assert (d
<= v2
, false, "#1");
1273 Assert (d
<= null, false, "#1a");
1274 Assert (null <= d
, false, "#1b");
1277 Assert (d
<= v2
, false, "#2");
1278 dynamic d2
= (int?) -2;
1279 Assert (d2
<= 1, true, "#2a");
1281 Assert (d2
<= 44, true, "#2b");
1283 d
= new MyType (30);
1284 MyType
? v3
= new MyType (30);
1285 Assert (d
<= v3
, true, "#3");
1286 dynamic d3
= new MyType
? (new MyType (-7));
1287 Assert (d3
<= new MyType (6), true, "#3a");
1288 Assert (d3
<= null, false, "#3b");
1292 Assert (d
<= v4
, false, "#4");
1294 Assert (d
<= v4
, false, "#4a");
1297 void LessThanOrEqualEnumTest ()
1299 dynamic d
= MyEnum
.Value_1
;
1301 Assert (d
<= null, false, "#1");
1303 Assert (d
<= MyEnum
.Value_1
, true, "#2");
1304 Assert (d
<= 0, false, "#2a");
1306 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1307 Assert (d2
<= MyEnumUlong
.Value_2
, true, "#3");
1308 Assert (d2
<= null, false, "#3a");
1316 Assert (d
% v
, 1, "#1");
1319 MyType v3
= new MyType (30);
1320 Assert (d
% v3
, new MyType (5), "#3");
1321 dynamic d3
= new MyType (-7);
1322 Assert
<MyType
> (d3
% new MyType (6), new MyType (-1), "#3a");
1324 d
= new MyTypeImplicitOnly (5);
1326 Assert (d
% v4
, 1m
, "#4");
1329 void ModuloNullableTest ()
1334 Assert
<double?> (d
% v2
, null, "#1");
1335 Assert
<double?> (d
% null, null, "#1a");
1336 Assert
<double?> (null % d
, null, "#1b");
1339 Assert (d
% v2
, 1, "#2");
1340 dynamic d2
= (int?) -2;
1341 Assert (d2
% 1, 0, "#2a");
1343 d
= new MyType (-2);
1344 MyType
? v3
= new MyType (30);
1345 Assert (d
% v3
, new MyType (-2), "#3");
1346 dynamic d3
= new MyType
? (new MyType (-7));
1347 Assert (d3
% new MyType (6), new MyType (-1), "#3a");
1348 Assert
<MyType
?> (d3
+ null, null, "#3b");
1350 d
= new MyTypeImplicitOnly (5);
1352 Assert (d
% v4
, 1m
, "#4");
1354 Assert
<decimal?> (d
% v4
, null, "#4a");
1357 void ModuloAssignTest ()
1363 Assert (d
, 1, "#1");
1368 Assert (d
, 0, "#1a");
1370 Assert (d
, 0, "#1b");
1372 dynamic d3
= new MyType (-7);
1373 d3
%= new MyType (6);
1374 Assert
<MyType
> (d3
, new MyType (-1), "#3");
1379 Assert (d
, 1m
, "#4");
1382 void MultiplyTest ()
1387 Assert (d
* v
, 10, "#1");
1389 Assert (d
* v2
, 2.5, "#1a");
1392 MyType v3
= new MyType (30);
1393 Assert (d
* v3
, new MyType (150), "#3");
1394 dynamic d3
= new MyType (-7);
1395 Assert
<MyType
> (d3
* new MyType (6), new MyType (-42), "#3a");
1399 Assert (d
* v4
, 31.6m
, "#4");
1402 void MultiplyNullableTest ()
1407 Assert
<int?> (d
* v2
, null, "#1");
1408 Assert
<int?> (d
* null, null, "#1a");
1409 Assert
<int?> (null * d
, null, "#1b");
1412 Assert (d
* v2
, -10, "#2");
1413 dynamic d2
= (int?) -2;
1414 Assert (d2
* 1, -2, "#2a");
1417 MyType
? v3
= new MyType (30);
1418 Assert (d
* v3
, new MyType (150), "#3");
1419 dynamic d3
= new MyType
? (new MyType (-7));
1420 Assert (d3
* new MyType (6), new MyType (-42), "#3a");
1421 Assert
<MyType
?> (d3
* null, null, "#3b");
1423 d
= new MyTypeImplicitOnly (5);
1425 Assert (d
* v4
, 20m
, "#4");
1427 Assert
<decimal?> (d
* v4
, null, "#4a");
1430 void MultiplyCheckedTest ()
1435 int v
= int.MaxValue
;
1436 AssertChecked (() => d
* v
, 7, "#1");
1439 AssertChecked (() => d
* v2
, null, "#2");
1442 MyType v3
= new MyType (int.MaxValue
);
1443 Assert (d
* v3
, new MyType (-4), "#3");
1447 void MultiplyAssignTest ()
1453 Assert (d
, 10, "#1");
1458 Assert (d
, 2.5, "#1a");
1460 Assert (d
, 5, "#1b");
1462 dynamic d3
= new MyType (-7);
1463 d3
*= new MyType (6);
1464 Assert
<MyType
> (d3
, new MyType (-42), "#3");
1469 Assert (d
, 20m
, "#4");
1474 Assert (i
, 15, "#5");
1477 void MultiplyAssignCheckedTest ()
1482 int v
= int.MaxValue
;
1483 AssertChecked (() => { d *= v; Assert (d, 0, "#1-"); }
, "#1");
1485 d
= new MyType (44);
1486 MyType v3
= new MyType (int.MaxValue
);
1488 Assert (d
, new MyType (-44), "#3-");
1495 Assert (8, -d
, "#1");
1496 Assert (-8, -(-d
), "#1a");
1498 d
= new MyType (-14);
1499 Assert (new MyType (14), -d
, "#2");
1501 d
= new MyTypeImplicitOnly (4);
1502 Assert (-4, -d
, "#3");
1505 Assert (-7, -d
, "#4");
1507 d
= double.NegativeInfinity
;
1508 Assert (double.PositiveInfinity
, -d
, "#5");
1511 void NegateNullable ()
1513 dynamic d
= (int?) -8;
1514 Assert (8, -d
, "#1");
1515 Assert (-8, -(-d
), "#1a");
1517 MyType
? n1
= new MyType (4);
1519 Assert (new MyType (-4), -d
, "#2");
1521 MyTypeImplicitOnly
? n2
= new MyTypeImplicitOnly (4);
1523 Assert (-4, -d
, "#3");
1526 Assert (-7, -d
, "#4");
1529 void NegateChecked ()
1532 dynamic d
= int.MinValue
;
1533 AssertChecked (() => -d
, 0, "#1");
1540 Assert (false, !d
, "#1");
1542 var de
= new MyType (-1);
1543 Assert (false, !d
, "#2");
1546 void NotEqualTest ()
1551 Assert (d
!= v
, true, "#1");
1553 Assert (d
!= v2
, false, "#1a");
1556 Assert (d
!= false, true, "#2");
1558 Assert (d
!= b2
, false, "#2a");
1560 d
= new MyType (30);
1561 MyType v3
= new MyType (30);
1562 Assert (d
!= v3
, false, "#3");
1563 dynamic d3
= new MyType (-7);
1564 Assert (d3
!= new MyType (6), true, "#3a");
1568 Assert (d
!= v4
, true, "#4");
1569 Assert (d
!= 2m
, false, "#4a");
1572 Assert (d
!= null, false, "#5");
1575 void NotEqualNullableTest ()
1580 Assert (d
!= v2
, true, "#1");
1581 Assert (d
!= null, true, "#1a");
1582 Assert (null != d
, true, "#1b");
1585 Assert (d
!= v2
, true, "#2");
1586 dynamic d2
= (int?) -2;
1587 Assert (d2
!= 1, true, "#2a");
1589 Assert (d2
!= 44, false, "#2b");
1591 d
= new MyType (30);
1592 MyType
? v3
= new MyType (30);
1593 Assert (d
!= v3
, false, "#3");
1594 dynamic d3
= new MyType
? (new MyType (-7));
1595 Assert (d3
!= new MyType (6), true, "#3a");
1596 Assert (d3
!= null, true, "#3b");
1600 Assert (d
!= v4
, true, "#4");
1602 Assert (d
!= v4
, true, "#4a");
1605 Assert (d
!= true, false, "#5");
1606 Assert (d
!= null, true, "#5a");
1607 Assert (d
!= false, true, "#5b");
1610 void NotEqualEnumTest ()
1612 dynamic d
= MyEnum
.Value_1
;
1614 Assert (d
!= null, true, "#1");
1616 Assert (d
!= MyEnum
.Value_1
, false, "#2");
1617 Assert (d
!= 0, true, "#2a");
1619 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1620 Assert (d2
!= MyEnumUlong
.Value_2
, false, "#3");
1621 Assert (d2
!= null, true, "#3a");
1624 void NotEqualStringTest ()
1628 Assert (d
!= "te", true, "#1");
1629 Assert (d
!= "text", false, "#1a");
1630 Assert (d
!= null, true, "#1b");
1633 void OnesComplement ()
1636 Assert (-8, ~d
, "#1");
1638 d
= new MyType (-1);
1639 Assert (0, ~d
, "#2");
1642 Assert (18446744073709551608, ~d
, "#3");
1645 Assert ((MyEnum
) 254, ~d
, "#4");
1648 void OnesComplementNullable ()
1650 dynamic d
= (int?) 7;
1651 Assert (-8, ~d
, "#1");
1653 d
= (MyEnum
?) MyEnum
.Value_1
;
1654 Assert ((MyEnum
) 254, ~d
, "#4");
1662 Assert (d
| v
, true, "#1");
1663 Assert (d
| false, true, "#1a");
1667 Assert (d
| v2
, 62, "#2");
1668 Assert (d
| 0, 42, "#2a");
1670 d
= new MyType (42);
1671 MyType v3
= new MyType (30);
1672 Assert (d
| v3
, new MyType (62), "#3");
1673 dynamic d3
= new MyType (-7);
1674 Assert
<MyType
> (d3
| new MyType (6), new MyType (-1), "#3a");
1676 d3
= new MyTypeImplicitOnly (-7);
1677 Assert (d3
| 11, -5, "#3b");
1682 dynamic d
= MyEnum
.Value_1
;
1684 Assert
<MyEnum
?> (d
| null, null, "#1");
1686 Assert (d
| d
, MyEnum
.Value_1
, "#2");
1688 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
1689 Assert
<MyEnumUlong
> (d2
| MyEnumUlong
.Value_2
, (MyEnumUlong
) 3, "#3");
1692 void OrTestNullable ()
1697 Assert
<int?> (d
| v2
, null, "#1");
1698 Assert
<int?> (d
| null, null, "#1a");
1699 Assert
<int?> (null | d
, null, "#1b");
1702 Assert (d
| v2
, -1, "#2");
1703 dynamic d2
= (int?) -2;
1704 Assert (d2
| 1, -1, "#2a");
1706 d
= new MyType (-2);
1707 MyType
? v3
= new MyType (30);
1708 Assert (d
| v3
, new MyType (-2), "#3");
1709 dynamic d3
= new MyType
? (new MyType (-7));
1710 Assert (d3
| new MyType (6), new MyType (-1), "#3a");
1713 void OrAssignedTest ()
1719 Assert (d
, true, "#1");
1722 Assert (d
, true, "#1a");
1727 Assert (d
, 62, "#2");
1729 MyType v3
= new MyType (30);
1730 dynamic d3
= new MyType (-7);
1731 d3
|= new MyType (6);
1732 Assert
<MyType
> (d3
, new MyType (-1), "#3");
1735 void OrAssignedTestEnum ()
1737 dynamic d
= MyEnum
.Value_1
;
1738 d
|= MyEnum
.Value_2
;
1739 Assert
<MyEnum
> (d
, (MyEnum
) 3, "#1");
1743 Assert (d
, MyEnum
.Value_2
, "#2");
1745 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_1
;
1746 Assert
<MyEnumUlong
> (d2
| MyEnumUlong
.Value_2
, (MyEnumUlong
)3, "#3");
1754 Assert
<bool> (d
|| v
, true, "#1");
1756 Assert (d
|| true, true, "#1a");
1759 Assert (d
|| d
, true, "#2");
1761 dynamic d3
= new MyType (-7);
1762 Assert
<MyType
> (d3
|| new MyType (6), new MyType (-1), "#3");
1765 void RightShiftTest ()
1767 dynamic d
= (ulong) 0x7F000;
1770 Assert
<ulong> (d
>> v
, 0x1FC00, "#1");
1771 Assert
<ulong> (d
>> 1, 0x3F800, "#1a");
1773 Assert
<ulong> (d
>> s
, 0x1FC00, "#1b");
1776 MyTypeImplicitOnly v3
= new MyTypeImplicitOnly (3);
1777 Assert (d
>> v3
, 0xFE00, "#3");
1778 dynamic d3
= new MyType (-7);
1779 Assert (d3
>> new MyTypeImplicitOnly (11), -1, "#3a");
1782 void RightShiftNullableTest ()
1787 Assert
<int?> (d
>> v2
, null, "#1");
1789 Assert
<int?> (d
>> null, null, "#1a");
1791 Assert
<int?> (null >> d
, null, "#1b");
1794 Assert (d
>> v2
, 0, "#2");
1795 dynamic d2
= (int?) -200;
1796 Assert (d2
>> 1, -100, "#2a");
1799 void RightShiftAssignTest ()
1801 dynamic d
= 0x7F000;
1805 Assert (d
, 0x1FC00, "#1");
1807 Assert (d
, 0xFE00, "#1a");
1810 Assert (d
, 0x3F80, "#1b");
1813 void RightShiftAssignNullableTest ()
1819 Assert (d
, 0, "#2");
1820 dynamic d2
= (int?) -2;
1822 Assert (d2
, -1, "#2a");
1825 void SubtractTest ()
1830 Assert (d
- v
, 3, "#1");
1832 Assert (d
- v2
, 4.5, "#1a");
1835 MyType v3
= new MyType (30);
1836 Assert (d
- v3
, new MyType (-25), "#3");
1837 dynamic d3
= new MyType (-7);
1838 Assert (d3
- new MyType (6), new MyType (-13), "#3a");
1840 d
= new MyTypeImplicitOnly (5);
1842 Assert (d
- v4
, 1m
, "#4");
1845 void SubtractNullableTest ()
1850 Assert
<int?> (d
- v2
, null, "#1");
1851 Assert
<int?> (d
- null, null, "#1a");
1852 Assert
<int?> (null - d
, null, "#1b");
1855 Assert (d
- v2
, 7, "#2");
1856 dynamic d2
= (int?) -2;
1857 Assert (d2
- 1, -3, "#2a");
1860 MyType
? v3
= new MyType (30);
1861 Assert (d
- v3
, new MyType (-25), "#3");
1862 dynamic d3
= new MyType
? (new MyType (-7));
1863 Assert (d3
- new MyType (6), new MyType (-13), "#3a");
1864 Assert
<MyType
?> (d3
- null, null, "#3b");
1866 d
= new MyTypeImplicitOnly (5);
1868 Assert (d
- v4
, 1m
, "#4");
1870 Assert
<decimal?> (d
- v4
, null, "#4a");
1873 void SubtractEnumTest ()
1875 dynamic d
= MyEnum
.Value_1
;
1877 Assert
<MyEnum
> (d
- 1, 0, "#1");
1879 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1880 Assert (d2
- (byte) 1, MyEnumUlong
.Value_1
, "#2");
1881 Assert
<MyEnumUlong
?> (d2
- (object) null, null, "#2a");
1883 // CSC: Invalid System.InvalidOperationException
1884 Assert
<MyEnum
?> (d
- null, null, "#3");
1887 void SubtractCheckedTest ()
1892 int v
= int.MinValue
;
1893 AssertChecked (() => d
- v
, 7, "#1");
1896 AssertChecked (() => d
- v2
, null, "#2");
1899 MyType v3
= new MyType (int.MinValue
);
1900 Assert (d
- v3
, new MyType (-2147483643), "#3");
1904 void SubtractAssignTest ()
1910 Assert (d
, 3, "#1");
1915 Assert (d
, 4.5, "#1a");
1917 Assert (d
, 2.5, "#1b");
1919 dynamic d3
= new MyType (-7);
1920 d3
-= new MyType (6);
1921 Assert
<MyType
> (d3
, new MyType (-13), "#3");
1926 Assert (d
, 1m
, "#4");
1929 void SubtractAssignEnumTest ()
1931 dynamic d
= MyEnum
.Value_1
;
1934 Assert
<MyEnum
> (d
, 0, "#2");
1936 dynamic d2
= (MyEnumUlong
?) MyEnumUlong
.Value_2
;
1938 Assert (d2
, MyEnumUlong
.Value_1
, "#3");
1941 void SubtractAssignCheckedTest ()
1946 int v
= int.MinValue
;
1947 AssertChecked (() => { d -= v; Assert (d, 0, "#1a"); }
, "#1");
1950 MyType v3
= new MyType (int.MinValue
);
1952 Assert (d
, new MyType (-2147483643), "#3a");
1956 void SubtractAssignEvent ()
1958 Action print
= () => { Console.WriteLine ("foo"); }
;
1961 // FIXME: Will have to special case events
1966 void UnaryDecrement ()
1969 Assert (3, d
--, "#1");
1970 Assert (2, d
, "#1a");
1973 Assert (2, --d
, "#2");
1974 Assert (2, d
, "#2a");
1976 d
= new MyType (-3);
1977 Assert (new MyType (-3), d
--, "#3");
1978 Assert (new MyType (-1), d
, "#3a");
1981 void UnaryDecrementCheckedTest ()
1984 dynamic d
= int.MinValue
;
1986 AssertChecked (() => { d--; Assert (d, -1073741824, "#1a"); }
, "#1");
1988 d
= new MyType (int.MinValue
);
1990 Assert (d
, new MyType (-1073741824), "#2");
1994 void UnaryIncrement ()
1997 Assert (3, d
++, "#1");
1998 Assert (4, d
, "#1a");
2001 Assert (4, ++d
, "#2");
2002 Assert (4, d
, "#2a");
2004 d
= new MyType (-3);
2005 Assert (new MyType (-3), d
++, "#3");
2006 Assert (new MyType (-6), d
, "#3a");
2009 void UnaryIncrementCheckedTest ()
2012 dynamic d
= int.MaxValue
;
2014 AssertChecked (() => { d++; Assert (d, 0, "#1a"); }
, "#1");
2016 d
= new MyType (int.MaxValue
);
2018 Assert (d
, new MyType (-2), "#2");
2022 //void UnaryIsFalse ()
2024 // dynamic d = this;
2025 // object r = d == null;
2026 // Assert (false, (bool) r, "#1");
2027 // Assert<object> (true, d != null, "#1a");
2033 Assert (3, d
? 3 : 5, "#1");
2036 Assert (false, d
< 1, "#2");
2038 d
= new InverseLogicalOperator (true);
2039 Assert (1, d
? 1 : -1, "#3");
2045 Assert (-8, +d
, "#1");
2046 Assert (-8, +(+d
), "#1a");
2048 d
= new MyType (14);
2049 Assert (new MyType (334455), +d
, "#2");
2051 d
= new MyTypeImplicitOnly (4);
2052 Assert (4, +d
, "#3");
2055 Assert
<uint> (7, +d
, "#4");
2058 void UnaryPlusNullable ()
2060 dynamic d
= (int?) -8;
2061 Assert (-8, +d
, "#1");
2062 Assert (-8, +(+d
), "#1a");
2064 MyType
? n1
= new MyType (4);
2066 Assert (new MyType (334455), +d
, "#2");
2068 MyTypeImplicitOnly
? n2
= new MyTypeImplicitOnly (4);
2070 Assert (4, +d
, "#3");
2073 Assert (7, +d
, "#4");
2076 #pragma warning restore 169
2078 static bool RunTest (MethodInfo test
)
2080 Console
.Write ("Running test {0, -25}", test
.Name
);
2082 test
.Invoke (new Tester (), null);
2083 Console
.WriteLine ("OK");
2085 } catch (Exception e
) {
2086 Console
.WriteLine ("FAILED");
2087 Console
.WriteLine (e
.ToString ());
2092 public static int Main ()
2094 var tests
= from test
in typeof (Tester
).GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
)
2095 where test
.GetParameters ().Length
== 0
2097 select RunTest (test
);
2099 int failures
= tests
.Count (a
=> !a
);
2100 Console
.WriteLine (failures
+ " tests failed");