More tests.
[mcs.git] / tests / dtest-006.cs
blob3e1bf0eb30440a5a424490a9b716364ebbb7ce23
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
6 // Dynamic binary operator, unary operators and convert tests
8 public struct InverseLogicalOperator
10 bool value;
11 public InverseLogicalOperator (bool value)
13 this.value = value;
16 public static bool operator true (InverseLogicalOperator u)
18 return u.value;
21 public static bool operator false (InverseLogicalOperator u)
23 return u.value;
27 public struct MyType
29 int value;
31 public MyType (int value) : this ()
33 this.value = value;
36 public short ShortProp { get; set; }
38 public override int GetHashCode ()
40 throw new NotImplementedException ();
43 public static bool operator true (MyType a)
45 return a.value != 1;
48 public static bool operator false (MyType a)
50 return a.value == 0;
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)
125 return a.value > 0;
128 public static int operator ~ (MyType a)
130 return ~a.value;
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)
145 return a.value >> b;
148 public static int operator << (MyType a, int b)
150 return a.value << 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 ();
170 class MyTypeExplicit
172 int value;
174 public MyTypeExplicit (int value)
176 this.value = value;
179 public static explicit operator int (MyTypeExplicit m)
181 return m.value;
185 struct MyTypeImplicitOnly
187 short b;
189 public MyTypeImplicitOnly (short b)
191 this.b = b;
194 public static implicit operator short (MyTypeImplicitOnly m)
196 return m.b;
199 public static implicit operator bool (MyTypeImplicitOnly m)
201 return m.b != 0;
205 enum MyEnum : byte
207 Value_1 = 1,
208 Value_2 = 2
211 enum MyEnumUlong : ulong
213 Value_1 = 1,
214 Value_2 = 2
218 class Tester
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)) {
226 name += ": ";
227 throw new ApplicationException (name + expected + " != " + value);
231 static void AssertChecked<T> (Func<T> expected, T value, string name)
233 try {
234 Assert (expected (), value, name);
235 throw new ApplicationException (name + ": OverflowException expected");
236 } catch (OverflowException) {
237 // passed
241 static void AssertChecked (Action expected, string name)
243 try {
244 expected ();
245 throw new ApplicationException (name + ": OverflowException expected");
246 } catch (OverflowException) {
247 // passed
251 #pragma warning disable 169
253 void AddTest ()
255 dynamic d = 5;
257 int v = 2;
258 Assert (d + v, 7, "#1");
259 double v2 = 0.5;
260 Assert (d + v2, 5.5, "#1a");
262 d = new MyType (5);
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);
272 decimal v4 = 4m;
273 Assert (d + v4, 9m, "#4");
276 void AddNullableTest ()
278 dynamic d = 5;
280 int? v2 = null;
281 Assert<int?> (d + v2, null, "#1");
282 Assert<int?> (d + null, null, "#1a");
283 Assert<int?> (null + d, null, "#1b");
285 v2 = -2;
286 Assert (d + v2, 3, "#2");
287 dynamic d2 = (int?) -2;
288 Assert (d2 + 1, -1, "#2a");
290 d = new MyType (5);
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);
298 decimal? v4 = 4m;
299 Assert (d + v4, 9m, "#4");
300 v4 = null;
301 Assert<decimal?> (d + v4, null, "#4a");
304 void AddEnumTest ()
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 ()
320 checked {
321 dynamic d = 5;
323 int v = int.MaxValue;
324 AssertChecked (() => d + v, 7, "#1");
326 int? v2 = v;
327 AssertChecked (() => d + v2, null, "#2");
329 d = new MyType (3);
330 MyType v3 = new MyType (int.MaxValue);
331 Assert (new MyType (-2147483646), d + v3, "#3");
335 void AddStringTest ()
337 dynamic d = "foo";
338 string v = "->";
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");
344 uint? v2 = 4;
345 Assert (d + v2, "foo4", "#2");
348 void AddAssignTest ()
350 dynamic d = 5;
352 int v = 2;
353 d += v;
354 Assert (d, 7, "#1");
356 d = 5.0;
357 double v2 = 0.5;
358 d += v2;
359 Assert (d, 5.5, "#1a");
360 d += v;
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");
367 d = 5m;
368 decimal v4 = 4m;
369 d += v4;
370 Assert (d, 9m, "#4");
373 void AddAssignNullableTest ()
375 dynamic d = (int?) 5;
377 // FEATURE
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
382 // fails to resolve
384 long? v2 = null;
385 d += v2;
386 Assert<int?> (d, null, "#1");
387 d += null;
388 Assert<int?> (d, null, "#1a");
390 long? l = (long?) 3;
391 d = l;
392 v2 = -2;
393 d += v2;
394 Assert (d, 3, "#2");
395 d = (int?) -2;
396 d += 1;
397 Assert (d, -1, "#2a");
399 MyType? v3 = new MyType (30);
400 d += v3;
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");
406 decimal? v4 = 4m;
407 d = 2m;
408 d += v4;
409 Assert (d, 9m, "#4");
410 d += null;
411 Assert<decimal?> (d, null, "#4a");
415 void AddAssignEnumTest ()
417 dynamic d = MyEnum.Value_1;
419 d = MyEnum.Value_1;
420 d += 1;
421 Assert (d, MyEnum.Value_2, "#2");
423 dynamic d2 = (MyEnumUlong?) MyEnumUlong.Value_1;
424 d2 += (byte) 1;
425 Assert (d2, MyEnumUlong.Value_2, "#3");
428 void AddAssignCheckedTest ()
430 checked {
431 dynamic d = 5;
433 int v = int.MaxValue;
434 AssertChecked (() => { d += v; Assert (d, 0, "#1-"); }, "#1");
436 d = new MyType (5);
437 MyType v3 = new MyType (int.MaxValue);
438 d += v3;
439 Assert (d, new MyType (-2147483644), "#3-");
443 void AddAssignStringTest ()
445 dynamic d = "foo";
446 string v = "->";
447 d += v;
448 Assert (d, "foo->", "#1");
450 d = "foo";
451 d += 1;
452 Assert (d, "foo1", "#1a");
454 d += null;
455 Assert (d, "foo1", "#1b");
457 uint? v2 = 4;
458 d = "foo";
459 d += v2;
460 Assert (d, "foo4", "#2");
463 void AddAssignEvent ()
465 dynamic d = null;
467 // FIXME: Will have to special case events
468 // ev_assign += d;
471 void AndTest ()
473 dynamic d = true;
475 var v = false;
476 Assert (d & v, false, "#1");
477 Assert (d & true, true, "#1a");
479 d = 42;
480 var v2 = 62;
481 Assert (d & v2, 42, "#2");
482 Assert (d & 0, 0, "#2a");
484 d = new MyType (10);
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");
494 void AndTestEnum ()
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 ()
508 dynamic d = 5;
510 int? v2 = null;
511 Assert<int?> (d & v2, null, "#1");
512 Assert<int?> (d & null, null, "#1a");
513 Assert<int?> (null & d, null, "#1b");
515 v2 = -2;
516 Assert (d & v2, 4, "#2");
517 dynamic d2 = (int?) -2;
518 Assert (d2 & 1, 0, "#2a");
520 d = new MyType (22);
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 ()
530 dynamic d = true;
532 var v = false;
533 d &= v;
534 Assert (d, false, "#1");
535 d = true;
536 d &= true;
537 Assert (d, true, "#1a");
539 d = 42;
540 var v2 = 62;
541 d &= v2;
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;
553 d &= MyEnum.Value_2;
554 Assert<MyEnum>(d, 0, "#1");
556 d = MyEnum.Value_2;
557 d &= d;
558 Assert (d, MyEnum.Value_2, "#2");
560 dynamic d2 = (MyEnumUlong?) MyEnumUlong.Value_1;
561 Assert<MyEnumUlong> (d2 & MyEnumUlong.Value_2, 0, "#3");
564 void AndAlsoTest ()
566 dynamic d = true;
568 var v = false;
569 Assert<bool> (d && v, false, "#1");
571 Assert (d && true, true, "#1a");
573 d = true;
574 Assert (d && d, true, "#2");
576 dynamic d3 = new MyType (-7);
577 Assert<MyType> (d3 && new MyType (6), new MyType (0), "#3");
580 void DivideTest ()
582 dynamic d = 5;
584 int v = 2;
585 Assert (d / v, 2, "#1");
587 d = new MyType (5);
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);
594 decimal v4 = 4m;
595 Assert (d / v4, 1.5m, "#4");
598 void DivideNullableTest ()
600 dynamic d = 5;
602 double? v2 = null;
603 Assert<double?> (d / v2, null, "#1");
604 Assert<double?> (d / null, null, "#1a");
605 Assert<double?> (null / d, null, "#1b");
607 v2 = -2;
608 Assert (d / v2, -2.5, "#2");
609 dynamic d2 = (int?) -2;
610 Assert (d2 / 1, -2, "#2a");
612 d = new MyType (5);
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);
620 decimal? v4 = 4m;
621 Assert (d / v4, 1.25m, "#4");
622 v4 = null;
623 Assert<decimal?> (d / v4, null, "#4a");
626 void DivideCheckedTest ()
628 checked {
629 // TODO:
633 void DivideAssignTest ()
635 dynamic d = 5;
637 int v = 2;
638 d /= v;
639 Assert (d, 2, "#1");
641 d = 5.0;
642 double v2 = 0.5;
643 d /= v2;
644 Assert (d, 10, "#1a");
645 d /= v;
646 Assert (d, 5, "#1b");
648 dynamic d3 = new MyType (-7);
649 d3 /= new MyType (6);
650 Assert<MyType> (d3, new MyType (-1), "#3");
652 d = 5m;
653 decimal v4 = 4m;
654 d /= v4;
655 Assert (d, 1.25m, "#4");
658 void DivideAssignCheckedTest ()
660 checked {
661 // TODO:
665 void ConvertImplicitTest ()
667 dynamic d = 3;
668 decimal v1 = d;
669 Assert (3m, v1, "#1");
671 d = new MyTypeImplicitOnly (5);
672 int v2 = d;
673 Assert (5, v2, "#2");
675 d = (byte) 4;
676 int v3 = d;
677 Assert (4, v3, "#3");
679 int[] v4 = new int[] { d };
680 Assert (4, v4[0], "#4");
682 d = true;
683 var v5 = new [] { d, 1 };
684 Assert (true, v5[0], "#5");
685 Assert (1, v5[1], "#5a");
687 d = "aa";
688 bool b = false;
689 var r = b ? d : "ss";
690 Assert ("ss", r, "#6");
693 int ConvertImplicitReturnTest ()
695 dynamic d = (byte) 3;
696 return d;
699 IEnumerable<string> ConvertImplicitReturnTest_2 ()
701 dynamic d = "aaa";
702 yield return d;
705 void ConvertExplicitTest ()
707 dynamic d = 300;
708 Assert (44, (byte) d, "#1");
709 Assert<byte?> (44, (byte?) d, "#1a");
711 d = 3m;
712 Assert (3, d, "#2");
714 d = new MyTypeImplicitOnly (5);
715 Assert (5, (int) d, "#3");
717 d = new MyTypeExplicit (-2);
718 Assert (-2, (int) d, "#4");
720 d = null;
721 Assert (null, (object) d, "#5");
724 void ConvertExplicitCheckedTest ()
726 checked {
727 dynamic d = 300;
728 AssertChecked (() => (byte) d, 7, "#1");
730 d = ulong.MaxValue;
731 AssertChecked<uint?> (() => (uint?) d, 2, "#2");
735 void ConvertArray ()
737 dynamic idx = (uint) 1;
738 var arr = new int [5];
739 arr [idx] = 2;
740 Assert (2, arr [idx], "#1");
743 void EqualTest ()
745 dynamic d = 5;
747 int v = 2;
748 Assert (d == v, false, "#1");
749 double v2 = 5;
750 Assert (d == v2, true, "#1a");
752 d = true;
753 Assert (d == false, false, "#2");
754 bool b2 = true;
755 Assert (d == b2, true, "#2a");
757 d = new MyType (30);
758 MyType v3 = new MyType (30);
759 Assert (d == v3, true, "#3");
760 dynamic d3 = new MyTypeImplicitOnly (-7);
761 Assert (d3 == 11, false, "#3b");
763 d = 2m;
764 decimal v4 = 4m;
765 Assert (d == v4, false, "#4");
766 Assert (d == 2m, true, "#4a");
768 d = null;
769 Assert (d == null, true, "#5");
772 void EqualNullableTest ()
774 dynamic d = 5;
776 int? v2 = null;
777 Assert (d == v2, false, "#1");
778 Assert (d == null, false, "#1a");
779 Assert (null == d, false, "#1b");
781 v2 = -2;
782 Assert (d == v2, false, "#2");
783 dynamic d2 = (int?) -2;
784 Assert (d2 == 1, false, "#2a");
785 d2 = (uint?) 44;
786 Assert (d2 == 44, true, "#2b");
788 d = new MyType (30);
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");
795 d = 4.1m;
796 decimal? v4 = 4m;
797 Assert (d == v4, false, "#4");
798 v4 = null;
799 Assert (d == v4, false, "#4a");
801 d = (bool?) true;
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 ()
823 dynamic d = "text";
825 Assert (d == "te", false, "#1");
826 Assert (d == "text", true, "#1a");
827 Assert (d == null, false, "#1b");
830 void EqualDelegateTest ()
832 dynamic d = this;
834 // Assert (d == delegate { }, true, "#1");
836 EmptyDelegate b = EqualDelegateTest;
837 d = b;
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 ()
858 dynamic d = true;
860 var v = false;
861 Assert (d ^ v, true, "#1");
862 Assert (d ^ true, false, "#1a");
864 d = 42;
865 var v2 = 62;
866 Assert (d ^ v2, 20, "#2");
867 Assert (d ^ 0, 42, "#2a");
869 d = new MyType (42);
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 ()
881 dynamic d = 5;
883 int? v2 = null;
884 Assert<int?> (d ^ v2, null, "#1");
885 Assert<int?> (d ^ null, null, "#1a");
886 Assert<int?> (null ^ d, null, "#1b");
888 v2 = -2;
889 Assert (d ^ v2, -5, "#2");
890 dynamic d2 = (int?) -2;
891 Assert (d2 ^ 1, -1, "#2a");
893 d = new MyType (5);
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 ()
915 dynamic d = true;
917 var v = false;
918 d ^= v;
919 Assert (d, true, "#1");
920 d = true;
921 d ^= true;
922 Assert (d, false, "#1a");
924 d = 42;
925 var v2 = 62;
926 d ^= v2;
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;
938 d ^= MyEnum.Value_2;
939 Assert<MyEnum>(d, (MyEnum) 3, "#1");
941 d = MyEnum.Value_2;
942 d ^= d;
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 ()
951 dynamic d = 5;
953 int v = 2;
954 Assert (d > v, true, "#1");
955 double v2 = 5;
956 Assert (d > v2, false, "#1a");
958 d = 4.6;
959 Assert (d > 4.59, true, "#2");
960 var b2 = 4.6;
961 Assert (d > b2, false, "#2a");
963 d = new MyType (30);
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");
972 d = 2m;
973 decimal v4 = 4m;
974 Assert (d > v4, false, "#4");
975 Assert (d > 2m, false, "#4a");
978 void GreaterThanNullableTest ()
980 dynamic d = 5;
982 int? v2 = null;
983 Assert (d > v2, false, "#1");
984 Assert (d > null, false, "#1a");
985 Assert (null > d, false, "#1b");
987 v2 = -2;
988 Assert (d > v2, true, "#2");
989 dynamic d2 = (int?) -2;
990 Assert (d2 > 1, false, "#2a");
991 d2 = (uint?) 44;
992 Assert (d2 > 44, false, "#2b");
994 d = new MyType (30);
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");
1001 d = 4.1m;
1002 decimal? v4 = 4m;
1003 Assert (d > v4, true, "#4");
1004 v4 = null;
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 ()
1024 dynamic d = 5;
1026 int v = 2;
1027 Assert (d >= v, true, "#1");
1028 double v2 = 5;
1029 Assert (d >= v2, true, "#1a");
1031 d = 4.6;
1032 Assert (d >= 4.59, true, "#2");
1033 var b2 = 4.6;
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");
1045 d = 2m;
1046 decimal v4 = 4m;
1047 Assert (d >= v4, false, "#4");
1048 Assert (d >= 2m, true, "#4a");
1051 void GreaterThanEqualNullableTest ()
1053 dynamic d = 5;
1055 int? v2 = null;
1056 Assert (d >= v2, false, "#1");
1057 Assert (d >= null, false, "#1a");
1058 Assert (null >= d, false, "#1b");
1060 v2 = -2;
1061 Assert (d >= v2, true, "#2");
1062 dynamic d2 = (int?) -2;
1063 Assert (d2 >= 1, false, "#2a");
1064 d2 = (uint?) 44;
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");
1074 d = 4.1m;
1075 decimal? v4 = 4m;
1076 Assert (d >= v4, true, "#4");
1077 v4 = null;
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;
1099 int v = 2;
1100 Assert<ulong> (d << v, 0x1FC000, "#1");
1101 Assert<ulong> (d << 1, 0xFE000, "#1a");
1102 short s = 2;
1103 Assert<ulong> (d << s, 0x1FC000, "#1b");
1105 d = 0x7F000;
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 ()
1115 dynamic d = 5;
1117 int? v2 = null;
1118 Assert<int?> (d << v2, null, "#1");
1119 d = 5;
1120 Assert<int?> (d << null, null, "#1a");
1121 d = 5;
1122 Assert<int?> (null << d, null, "#1b");
1124 v2 = -2;
1125 Assert (d << v2, 0x40000000, "#2");
1126 dynamic d2 = (int?) -2;
1127 Assert (d2 << 1, -4, "#2a");
1130 void LeftShiftAssignTest ()
1132 dynamic d = 0x7F000;
1134 int v = 2;
1135 d <<= v;
1136 Assert (d, 0x1FC000, "#1");
1137 d <<= 1;
1138 Assert (d, 0x3F8000, "#1a");
1139 sbyte s = 2;
1140 d <<= s;
1141 Assert (d, 0xFE0000, "#1b");
1144 void LeftShiftAssignNullableTest ()
1146 dynamic d = 5;
1148 var v2 = -2;
1149 d <<= v2;
1150 Assert (d, 0x40000000, "#2");
1151 dynamic d2 = (int?) -2;
1152 d2 <<= 1;
1153 Assert (d2, -4, "#2a");
1156 void LessThanTest ()
1158 dynamic d = 5;
1160 int v = 2;
1161 Assert (d < v, false, "#1");
1162 double v2 = 5;
1163 Assert (d < v2, false, "#1a");
1165 d = 4.6;
1166 Assert (d < 4.59, false, "#2");
1167 var b2 = 4.6;
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");
1179 d = 2m;
1180 decimal v4 = 4m;
1181 Assert (d < v4, true, "#4");
1182 Assert (d < 2m, false, "#4a");
1185 void LessThanNullableTest ()
1187 dynamic d = 5;
1189 int? v2 = null;
1190 Assert (d < v2, false, "#1");
1191 Assert (d < null, false, "#1a");
1192 Assert (null < d, false, "#1b");
1194 v2 = -2;
1195 Assert (d < v2, false, "#2");
1196 dynamic d2 = (int?) -2;
1197 Assert (d2 < 1, true, "#2a");
1198 d2 = (uint?) 44;
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");
1210 d = 4.1m;
1211 decimal? v4 = 4m;
1212 Assert (d < v4, false, "#4");
1213 v4 = null;
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 ()
1233 dynamic d = 5;
1235 int v = 2;
1236 Assert (d <= v, false, "#1");
1237 double v2 = 5;
1238 Assert (d <= v2, true, "#1a");
1240 d = 4.6;
1241 Assert (d <= 4.59, false, "#2");
1242 var b2 = 4.6;
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");
1254 d = 2m;
1255 decimal v4 = 4m;
1256 Assert (d <= v4, true, "#4");
1257 Assert (d <= 2m, true, "#4a");
1260 void LessThanOrEqualNullableTest ()
1262 dynamic d = 5;
1264 int? v2 = null;
1265 Assert (d <= v2, false, "#1");
1266 Assert (d <= null, false, "#1a");
1267 Assert (null <= d, false, "#1b");
1269 v2 = -2;
1270 Assert (d <= v2, false, "#2");
1271 dynamic d2 = (int?) -2;
1272 Assert (d2 <= 1, true, "#2a");
1273 d2 = (uint?) 44;
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");
1283 d = 4.1m;
1284 decimal? v4 = 4m;
1285 Assert (d <= v4, false, "#4");
1286 v4 = null;
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");
1304 void ModuloTest ()
1306 dynamic d = 5;
1308 int v = 2;
1309 Assert (d % v, 1, "#1");
1311 d = new MyType (5);
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);
1318 decimal v4 = 4m;
1319 Assert (d % v4, 1m, "#4");
1322 void ModuloNullableTest ()
1324 dynamic d = 5;
1326 double? v2 = null;
1327 Assert<double?> (d % v2, null, "#1");
1328 Assert<double?> (d % null, null, "#1a");
1329 Assert<double?> (null % d, null, "#1b");
1331 v2 = -2;
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);
1344 decimal? v4 = 4m;
1345 Assert (d % v4, 1m, "#4");
1346 v4 = null;
1347 Assert<decimal?> (d % v4, null, "#4a");
1350 void ModuloAssignTest ()
1352 dynamic d = 5;
1354 int v = 2;
1355 d %= v;
1356 Assert (d, 1, "#1");
1358 d = 5.0;
1359 double v2 = 0.5;
1360 d %= v2;
1361 Assert (d, 0, "#1a");
1362 d %= v;
1363 Assert (d, 0, "#1b");
1365 dynamic d3 = new MyType (-7);
1366 d3 %= new MyType (6);
1367 Assert<MyType> (d3, new MyType (-1), "#3");
1369 d = 5m;
1370 decimal v4 = 4m;
1371 d %= v4;
1372 Assert (d, 1m, "#4");
1375 void MultiplyTest ()
1377 dynamic d = 5;
1379 int v = 2;
1380 Assert (d * v, 10, "#1");
1381 double v2 = 0.5;
1382 Assert (d * v2, 2.5, "#1a");
1384 d = new MyType (5);
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");
1390 decimal v4 = 4m;
1391 d = 7.9m;
1392 Assert (d * v4, 31.6m, "#4");
1395 void MultiplyNullableTest ()
1397 dynamic d = 5;
1399 int? v2 = null;
1400 Assert<int?> (d * v2, null, "#1");
1401 Assert<int?> (d * null, null, "#1a");
1402 Assert<int?> (null * d, null, "#1b");
1404 v2 = -2;
1405 Assert (d * v2, -10, "#2");
1406 dynamic d2 = (int?) -2;
1407 Assert (d2 * 1, -2, "#2a");
1409 d = new MyType (5);
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);
1417 decimal? v4 = 4m;
1418 Assert (d * v4, 20m, "#4");
1419 v4 = null;
1420 Assert<decimal?> (d * v4, null, "#4a");
1423 void MultiplyCheckedTest ()
1425 checked {
1426 dynamic d = 5;
1428 int v = int.MaxValue;
1429 AssertChecked (() => d * v, 7, "#1");
1431 int? v2 = v;
1432 AssertChecked (() => d * v2, null, "#2");
1434 d = new MyType (4);
1435 MyType v3 = new MyType (int.MaxValue);
1436 Assert (d * v3, new MyType (-4), "#3");
1440 void MultiplyAssignTest ()
1442 dynamic d = 5;
1444 int v = 2;
1445 d *= v;
1446 Assert (d, 10, "#1");
1448 d = 5.0;
1449 double v2 = 0.5;
1450 d *= v2;
1451 Assert (d, 2.5, "#1a");
1452 d *= v;
1453 Assert (d, 5, "#1b");
1455 dynamic d3 = new MyType (-7);
1456 d3 *= new MyType (6);
1457 Assert<MyType> (d3, new MyType (-42), "#3");
1459 d = 5m;
1460 decimal v4 = 4m;
1461 d *= v4;
1462 Assert (d, 20m, "#4");
1464 int i = 3;
1465 d = 5;
1466 i *= d;
1467 Assert (i, 15, "#5");
1470 void MultiplyAssignCheckedTest ()
1472 checked {
1473 dynamic d = 5;
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);
1480 d *= v3;
1481 Assert (d, new MyType (-44), "#3-");
1485 void Negate ()
1487 dynamic d = -8;
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");
1497 d = (uint) 7;
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);
1511 d = n1;
1512 Assert (new MyType (-4), -d, "#2");
1514 MyTypeImplicitOnly? n2 = new MyTypeImplicitOnly (4);
1515 d = n2;
1516 Assert (-4, -d, "#3");
1518 d = (sbyte?) 7;
1519 Assert (-7, -d, "#4");
1522 void NegateChecked ()
1524 checked {
1525 dynamic d = int.MinValue;
1526 AssertChecked (() => -d, 0, "#1");
1530 void Not ()
1532 dynamic d = true;
1533 Assert (false, !d, "#1");
1535 var de = new MyType (-1);
1536 Assert (false, !d, "#2");
1539 void NotEqualTest ()
1541 dynamic d = 5;
1543 int v = 2;
1544 Assert (d != v, true, "#1");
1545 double v2 = 5;
1546 Assert (d != v2, false, "#1a");
1548 d = true;
1549 Assert (d != false, true, "#2");
1550 bool b2 = true;
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");
1559 d = 2m;
1560 decimal v4 = 4m;
1561 Assert (d != v4, true, "#4");
1562 Assert (d != 2m, false, "#4a");
1564 d = null;
1565 Assert (d != null, false, "#5");
1568 void NotEqualNullableTest ()
1570 dynamic d = 5;
1572 int? v2 = null;
1573 Assert (d != v2, true, "#1");
1574 Assert (d != null, true, "#1a");
1575 Assert (null != d, true, "#1b");
1577 v2 = -2;
1578 Assert (d != v2, true, "#2");
1579 dynamic d2 = (int?) -2;
1580 Assert (d2 != 1, true, "#2a");
1581 d2 = (uint?) 44;
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");
1591 d = 4.1m;
1592 decimal? v4 = 4m;
1593 Assert (d != v4, true, "#4");
1594 v4 = null;
1595 Assert (d != v4, true, "#4a");
1597 d = (bool?) true;
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 ()
1619 dynamic d = "text";
1621 Assert (d != "te", true, "#1");
1622 Assert (d != "text", false, "#1a");
1623 Assert (d != null, true, "#1b");
1626 void OnesComplement ()
1628 dynamic d = 7;
1629 Assert (-8, ~d, "#1");
1631 d = new MyType (-1);
1632 Assert (0, ~d, "#2");
1634 d = (ulong) 7;
1635 Assert (18446744073709551608, ~d, "#3");
1637 d = MyEnum.Value_1;
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");
1650 void OrTest ()
1652 dynamic d = true;
1654 var v = false;
1655 Assert (d | v, true, "#1");
1656 Assert (d | false, true, "#1a");
1658 d = 42;
1659 var v2 = 62;
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");
1673 void OrTestEnum ()
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 ()
1687 dynamic d = 5;
1689 int? v2 = null;
1690 Assert<int?> (d | v2, null, "#1");
1691 Assert<int?> (d | null, null, "#1a");
1692 Assert<int?> (null | d, null, "#1b");
1694 v2 = -2;
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 ()
1708 dynamic d = true;
1710 var v = false;
1711 d |= v;
1712 Assert (d, true, "#1");
1713 d = true;
1714 d |= true;
1715 Assert (d, true, "#1a");
1717 d = 42;
1718 var v2 = 62;
1719 d |= v2;
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");
1734 d = MyEnum.Value_2;
1735 d |= d;
1736 Assert (d, MyEnum.Value_2, "#2");
1738 dynamic d2 = (MyEnumUlong?) MyEnumUlong.Value_1;
1739 Assert<MyEnumUlong> (d2 | MyEnumUlong.Value_2, (MyEnumUlong)3, "#3");
1742 void OrElseTest ()
1744 dynamic d = true;
1746 var v = false;
1747 Assert<bool> (d || v, true, "#1");
1749 Assert (d || true, true, "#1a");
1751 d = true;
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;
1762 int v = 2;
1763 Assert<ulong> (d >> v, 0x1FC00, "#1");
1764 Assert<ulong> (d >> 1, 0x3F800, "#1a");
1765 short s = 2;
1766 Assert<ulong> (d >> s, 0x1FC00, "#1b");
1768 d = 0x7F000;
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 ()
1777 dynamic d = 5;
1779 int? v2 = null;
1780 Assert<int?> (d >> v2, null, "#1");
1781 d = 5;
1782 Assert<int?> (d >> null, null, "#1a");
1783 d = 5;
1784 Assert<int?> (null >> d, null, "#1b");
1786 v2 = -2;
1787 Assert (d >> v2, 0, "#2");
1788 dynamic d2 = (int?) -200;
1789 Assert (d2 >> 1, -100, "#2a");
1792 void RightShiftAssignTest ()
1794 dynamic d = 0x7F000;
1796 int v = 2;
1797 d >>= v;
1798 Assert (d, 0x1FC00, "#1");
1799 d >>= 1;
1800 Assert (d, 0xFE00, "#1a");
1801 sbyte s = 2;
1802 d >>= s;
1803 Assert (d, 0x3F80, "#1b");
1806 void RightShiftAssignNullableTest ()
1808 dynamic d = 0x2A0;
1810 var v2 = -2;
1811 d >>= v2;
1812 Assert (d, 0, "#2");
1813 dynamic d2 = (int?) -2;
1814 d2 >>= 1;
1815 Assert (d2, -1, "#2a");
1818 void SubtractTest ()
1820 dynamic d = 5;
1822 int v = 2;
1823 Assert (d - v, 3, "#1");
1824 double v2 = 0.5;
1825 Assert (d - v2, 4.5, "#1a");
1827 d = new MyType (5);
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);
1834 decimal v4 = 4m;
1835 Assert (d - v4, 1m, "#4");
1838 void SubtractNullableTest ()
1840 dynamic d = 5;
1842 int? v2 = null;
1843 Assert<int?> (d - v2, null, "#1");
1844 Assert<int?> (d - null, null, "#1a");
1845 Assert<int?> (null - d, null, "#1b");
1847 v2 = -2;
1848 Assert (d - v2, 7, "#2");
1849 dynamic d2 = (int?) -2;
1850 Assert (d2 - 1, -3, "#2a");
1852 d = new MyType (5);
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);
1860 decimal? v4 = 4m;
1861 Assert (d - v4, 1m, "#4");
1862 v4 = null;
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 ()
1882 checked {
1883 dynamic d = 5;
1885 int v = int.MinValue;
1886 AssertChecked (() => d - v, 7, "#1");
1888 int? v2 = v;
1889 AssertChecked (() => d - v2, null, "#2");
1891 d = new MyType (5);
1892 MyType v3 = new MyType (int.MinValue);
1893 Assert (d - v3, new MyType (-2147483643), "#3");
1897 void SubtractAssignTest ()
1899 dynamic d = 5;
1901 int v = 2;
1902 d -= v;
1903 Assert (d, 3, "#1");
1905 d = 5.0;
1906 double v2 = 0.5;
1907 d -= v2;
1908 Assert (d, 4.5, "#1a");
1909 d -= v;
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");
1916 d = 5m;
1917 decimal v4 = 4m;
1918 d -= v4;
1919 Assert (d, 1m, "#4");
1922 void SubtractAssignEnumTest ()
1924 dynamic d = MyEnum.Value_1;
1926 d -= 1;
1927 Assert<MyEnum> (d, 0, "#2");
1929 dynamic d2 = (MyEnumUlong?) MyEnumUlong.Value_2;
1930 d2 -= (byte) 1;
1931 Assert (d2, MyEnumUlong.Value_1, "#3");
1934 void SubtractAssignCheckedTest ()
1936 checked {
1937 dynamic d = 5;
1939 int v = int.MinValue;
1940 AssertChecked (() => { d -= v; Assert (d, 0, "#1a"); }, "#1");
1942 d = new MyType (5);
1943 MyType v3 = new MyType (int.MinValue);
1944 d -= v3;
1945 Assert (d, new MyType (-2147483643), "#3a");
1949 void SubtractAssignEvent ()
1951 Action print = () => { Console.WriteLine ("foo"); };
1952 dynamic d = print;
1954 // FIXME: Will have to special case events
1955 //ev_assign -= d;
1956 //ev_assign ();
1959 void UnaryDecrement ()
1961 dynamic d = 3;
1962 Assert (3, d--, "#1");
1963 Assert (2, d, "#1a");
1965 d = 3;
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 ()
1976 checked {
1977 dynamic d = int.MinValue;
1979 AssertChecked (() => { d--; Assert (d, -1073741824, "#1a"); }, "#1");
1981 d = new MyType (int.MinValue);
1982 d--;
1983 Assert (d, new MyType (-1073741824), "#2");
1987 void UnaryIncrement ()
1989 dynamic d = 3;
1990 Assert (3, d++, "#1");
1991 Assert (4, d, "#1a");
1993 d = 3;
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 ()
2004 checked {
2005 dynamic d = int.MaxValue;
2007 AssertChecked (() => { d++; Assert (d, 0, "#1a"); }, "#1");
2009 d = new MyType (int.MaxValue);
2010 d++;
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");
2023 void UnaryIsTrue ()
2025 dynamic d = true;
2026 Assert (3, d ? 3 : 5, "#1");
2028 d = 4;
2029 Assert (false, d < 1, "#2");
2031 d = new InverseLogicalOperator (true);
2032 Assert (1, d ? 1 : -1, "#3");
2035 void UnaryPlus ()
2037 dynamic d = -8;
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");
2047 d = (uint) 7;
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);
2058 d = n1;
2059 Assert (new MyType (334455), +d, "#2");
2061 MyTypeImplicitOnly? n2 = new MyTypeImplicitOnly (4);
2062 d = n2;
2063 Assert (4, +d, "#3");
2065 d = (sbyte?) 7;
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);
2074 try {
2075 test.Invoke (new Tester (), null);
2076 Console.WriteLine ("OK");
2077 return true;
2078 } catch (Exception e) {
2079 Console.WriteLine ("FAILED");
2080 Console.WriteLine (e.ToString ());
2081 return false;
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
2089 orderby test.Name
2090 select RunTest (test);
2092 int failures = tests.Count (a => !a);
2093 Console.WriteLine (failures + " tests failed");
2094 return failures;