1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.common.common;
6 using Xunit;
7 
8 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.common.common
9 {
10     // <Title> Dynamic and static interaction utility class </Title>
11     // <Description>
12     // </Description>
13     // <RelatedBugs></RelatedBugs>
14     // <Expects Status=success></Expects>
15     // <Code>
16     using System;
17 
18     public class Verify
19     {
Eval(Func<bool> testmethod)20         internal static int Eval(Func<bool> testmethod)
21         {
22             int result = 0;
23             try
24             {
25                 if (!testmethod())
26                 {
27                     result++;
28                     //System.Console.WriteLine("Test failed at {0}\n", testmethod.Method.Name);
29                 }
30             }
31             catch (Exception e)
32             {
33                 result++;
34                 //System.Console.WriteLine("Catch an unknown exception when run test {0}, \nexception: {1}", testmethod.Method.Name, e.ToString());
35             }
36 
37             return result;
38         }
39     }
40     // </Code>
41 }
42 
43 
44 
45 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target001.target001
46 {
47     // <Area> Dynamic -- implicit conversion</Area>
48     // <Title> The target type is predefined value type </Title>
49     // <Description>
50     // bool, char, sbyte, byte and their nullable form.
51     // </Description>
52     // <RelatedBugs></RelatedBugs>
53     // <Expects Status=success></Expects>
54     // <Code>
55 
56     public class TargetIsPredefinedValueType
57     {
58         #region non-nullable
BoolTypeWithIdentityConversionInIfStatement()59         private static bool BoolTypeWithIdentityConversionInIfStatement()
60         {
61             int failcount = 0;
62             dynamic d = false;
63             if (d)
64             {
65                 failcount++;
66                 System.Console.WriteLine("Test failed at conversion result");
67             }
68 
69             return failcount == 0;
70         }
71 
CharTypeWithIdentityConversionInAssignment()72         private static bool CharTypeWithIdentityConversionInAssignment()
73         {
74             int failcount = 0;
75             dynamic d = 'a';
76             char result = d;
77             if (result != 'a')
78             {
79                 failcount++;
80                 System.Console.WriteLine("Test failed at conversion result");
81             }
82 
83             return failcount == 0;
84         }
85 
SByteTypeWithIdentityConversionInAssignment()86         private static bool SByteTypeWithIdentityConversionInAssignment()
87         {
88             int failcount = 0;
89             dynamic d = (sbyte)-128;
90             sbyte result = d;
91             if (result != -128)
92             {
93                 failcount++;
94                 System.Console.WriteLine("Test failed at conversion result");
95             }
96 
97             return failcount == 0;
98         }
99 
ByteTypeWithIdentityConversionInAssignment()100         private static bool ByteTypeWithIdentityConversionInAssignment()
101         {
102             int failcount = 0;
103             dynamic d = (byte)0;
104             byte result = d;
105             if (result != 0)
106             {
107                 failcount++;
108                 System.Console.WriteLine("Test failed at conversion result");
109             }
110 
111             return failcount == 0;
112         }
113 
114         #endregion
115         #region nullable
NullableBoolTypeWithIdentityConversionInAssignment()116         private static bool NullableBoolTypeWithIdentityConversionInAssignment()
117         {
118             int failcount = 0;
119             bool? origin = true;
120             dynamic d = origin;
121             bool? result = d;
122             if (result != origin)
123             {
124                 failcount++;
125                 System.Console.WriteLine("Test failed at conversion result");
126             }
127 
128             return failcount == 0;
129         }
130 
NullableCharTypeWithIdentityConversionInAssignment()131         private static bool NullableCharTypeWithIdentityConversionInAssignment()
132         {
133             int failcount = 0;
134             char? origin = '\0';
135             dynamic d = origin;
136             char? result = d;
137             if (result != origin)
138             {
139                 failcount++;
140                 System.Console.WriteLine("Test failed at conversion result");
141             }
142 
143             return failcount == 0;
144         }
145 
NullableSbyteTypeWithIdentityConversionInAssignment()146         private static bool NullableSbyteTypeWithIdentityConversionInAssignment()
147         {
148             int failcount = 0;
149             sbyte? origin = 127;
150             dynamic d = origin;
151             sbyte? result = d;
152             if (result != origin)
153             {
154                 failcount++;
155                 System.Console.WriteLine("Test failed at conversion result");
156             }
157 
158             return failcount == 0;
159         }
160 
NullableByteTypeWithIdentityConversionInAssignment()161         private static bool NullableByteTypeWithIdentityConversionInAssignment()
162         {
163             int failcount = 0;
164             byte? origin = 255;
165             dynamic d = origin;
166             byte? result = d;
167             if (result != origin)
168             {
169                 failcount++;
170                 System.Console.WriteLine("Test failed at conversion result");
171             }
172 
173             return failcount == 0;
174         }
175 
176         #endregion
177         [Fact]
DynamicCSharpRunTest()178         public static void DynamicCSharpRunTest()
179         {
180             Assert.Equal(0, MainMethod());
181         }
182 
MainMethod()183         public static int MainMethod()
184         {
185             int result = 0;
186             result += Verify.Eval(BoolTypeWithIdentityConversionInIfStatement);
187             result += Verify.Eval(CharTypeWithIdentityConversionInAssignment);
188             result += Verify.Eval(SByteTypeWithIdentityConversionInAssignment);
189             result += Verify.Eval(ByteTypeWithIdentityConversionInAssignment);
190             result += Verify.Eval(NullableBoolTypeWithIdentityConversionInAssignment);
191             result += Verify.Eval(NullableCharTypeWithIdentityConversionInAssignment);
192             result += Verify.Eval(NullableSbyteTypeWithIdentityConversionInAssignment);
193             result += Verify.Eval(NullableByteTypeWithIdentityConversionInAssignment);
194             return result;
195         }
196     }
197     // </Code>
198 }
199 
200 
201 
202 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target002.target002
203 {
204     // <Area> Dynamic -- implicit conversion</Area>
205     // <Title> The target type is predefined value type </Title>
206     // <Description>
207     // short, ushort, int, uint, long, ulong and their nullable form.
208     // </Description>
209     // <RelatedBugs></RelatedBugs>
210     // <Expects Status=success></Expects>
211     // <Code>
212 
213     public class TargetIsPredefinedValueType
214     {
215         #region non-nullable
ShortTypeWithNumbericConversionFromSbyteInAssignment()216         private static bool ShortTypeWithNumbericConversionFromSbyteInAssignment()
217         {
218             int failcount = 0;
219             sbyte origin = sbyte.MinValue;
220             dynamic d = origin;
221             short result = d;
222             if (result != origin)
223             {
224                 failcount++;
225                 System.Console.WriteLine("Test failed at conversion result");
226             }
227 
228             return failcount == 0;
229         }
230 
UshortTypeWithNumbericConversionFromCharInAssignment()231         private static bool UshortTypeWithNumbericConversionFromCharInAssignment()
232         {
233             int failcount = 0;
234             char origin = char.MinValue;
235             dynamic d = origin;
236             ushort result = d;
237             if (result != origin)
238             {
239                 failcount++;
240                 System.Console.WriteLine("Test failed at conversion result");
241             }
242 
243             return failcount == 0;
244         }
245 
IntTypeWithNumbericConversionFromSbyteInAssignment()246         private static bool IntTypeWithNumbericConversionFromSbyteInAssignment()
247         {
248             int failcount = 0;
249             sbyte origin = sbyte.MinValue;
250             dynamic d = origin;
251             int result = d;
252             if (result != origin)
253             {
254                 failcount++;
255                 System.Console.WriteLine("Test failed at conversion result");
256             }
257 
258             return failcount == 0;
259         }
260 
UintTypeWithNumbericConversionFromByteInAssignment()261         private static bool UintTypeWithNumbericConversionFromByteInAssignment()
262         {
263             int failcount = 0;
264             byte origin = byte.MaxValue;
265             dynamic d = origin;
266             uint result = d;
267             if (result != origin)
268             {
269                 failcount++;
270                 System.Console.WriteLine("Test failed at conversion result");
271             }
272 
273             return failcount == 0;
274         }
275 
LongTypeWithNumbericConversionFromShortInAssignment()276         private static bool LongTypeWithNumbericConversionFromShortInAssignment()
277         {
278             int failcount = 0;
279             short origin = short.MaxValue;
280             dynamic d = origin;
281             long result = d;
282             if (result != origin)
283             {
284                 failcount++;
285                 System.Console.WriteLine("Test failed at conversion result");
286             }
287 
288             return failcount == 0;
289         }
290 
UlongTypeWithNumbericConversionFromUshortInAssignment()291         private static bool UlongTypeWithNumbericConversionFromUshortInAssignment()
292         {
293             int failcount = 0;
294             ushort origin = ushort.MaxValue;
295             dynamic d = origin;
296             ulong result = d;
297             if (result != origin)
298             {
299                 failcount++;
300                 System.Console.WriteLine("Test failed at conversion result");
301             }
302 
303             return failcount == 0;
304         }
305 
306         #endregion
307         #region nullable
NullableShortTypeWithNumbericConversionFromByteInAssignment()308         private static bool NullableShortTypeWithNumbericConversionFromByteInAssignment()
309         {
310             int failcount = 0;
311             byte? origin = byte.MaxValue;
312             dynamic d = origin;
313             short? result = d;
314             if (result != origin)
315             {
316                 failcount++;
317                 System.Console.WriteLine("Test failed at conversion result");
318             }
319 
320             return failcount == 0;
321         }
322 
NullableUshortTypeWithNumbericConversionFromByteInAssignment()323         private static bool NullableUshortTypeWithNumbericConversionFromByteInAssignment()
324         {
325             int failcount = 0;
326             byte? origin = byte.MinValue;
327             dynamic d = origin;
328             short? result = d;
329             if (result != origin)
330             {
331                 failcount++;
332                 System.Console.WriteLine("Test failed at conversion result");
333             }
334 
335             return failcount == 0;
336         }
337 
NullableIntTypeWithNumbericConversionFromShortInAssignment()338         private static bool NullableIntTypeWithNumbericConversionFromShortInAssignment()
339         {
340             int failcount = 0;
341             short? origin = short.MaxValue;
342             dynamic d = origin;
343             int? result = d;
344             if (result != origin)
345             {
346                 failcount++;
347                 System.Console.WriteLine("Test failed at conversion result");
348             }
349 
350             return failcount == 0;
351         }
352 
NullableUintTypeWithNumbericConversionFromUshortInAssignment()353         private static bool NullableUintTypeWithNumbericConversionFromUshortInAssignment()
354         {
355             int failcount = 0;
356             ushort? origin = ushort.MaxValue;
357             dynamic d = origin;
358             uint? result = d;
359             if (result != origin)
360             {
361                 failcount++;
362                 System.Console.WriteLine("Test failed at conversion result");
363             }
364 
365             return failcount == 0;
366         }
367 
NullableLongTypeWithNumbericConversionFromIntInAssignment()368         private static bool NullableLongTypeWithNumbericConversionFromIntInAssignment()
369         {
370             int failcount = 0;
371             int? origin = int.MinValue;
372             dynamic d = origin;
373             long? result = d;
374             if (result != origin)
375             {
376                 failcount++;
377                 System.Console.WriteLine("Test failed at conversion result");
378             }
379 
380             return failcount == 0;
381         }
382 
NullableUlongTypeWithNullableNumbericConversionFromUintInAssignment()383         private static bool NullableUlongTypeWithNullableNumbericConversionFromUintInAssignment()
384         {
385             int failcount = 0;
386             uint origin = uint.MaxValue;
387             dynamic d = origin;
388             ulong? result = d;
389             if (result != origin)
390             {
391                 failcount++;
392                 System.Console.WriteLine("Test failed at conversion result");
393             }
394 
395             return failcount == 0;
396         }
397 
398         #endregion
399         [Fact]
DynamicCSharpRunTest()400         public static void DynamicCSharpRunTest()
401         {
402             Assert.Equal(0, MainMethod());
403         }
404 
MainMethod()405         public static int MainMethod()
406         {
407             int result = 0;
408             result += Verify.Eval(ShortTypeWithNumbericConversionFromSbyteInAssignment);
409             result += Verify.Eval(UshortTypeWithNumbericConversionFromCharInAssignment);
410             result += Verify.Eval(IntTypeWithNumbericConversionFromSbyteInAssignment);
411             result += Verify.Eval(UintTypeWithNumbericConversionFromByteInAssignment);
412             result += Verify.Eval(LongTypeWithNumbericConversionFromShortInAssignment);
413             result += Verify.Eval(UlongTypeWithNumbericConversionFromUshortInAssignment);
414             result += Verify.Eval(NullableShortTypeWithNumbericConversionFromByteInAssignment);
415             result += Verify.Eval(NullableUshortTypeWithNumbericConversionFromByteInAssignment);
416             result += Verify.Eval(NullableIntTypeWithNumbericConversionFromShortInAssignment);
417             result += Verify.Eval(NullableUintTypeWithNumbericConversionFromUshortInAssignment);
418             result += Verify.Eval(NullableLongTypeWithNumbericConversionFromIntInAssignment);
419             result += Verify.Eval(NullableUlongTypeWithNullableNumbericConversionFromUintInAssignment);
420             return result;
421         }
422     }
423     // </Code>
424 }
425 
426 
427 
428 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target003.target003
429 {
430     // <Area> Dynamic -- implicit conversion</Area>
431     // <Title> The target type is predefined value type </Title>
432     // <Description>
433     // float, double, decimal, GUID, DateTime and their nullable form.
434     // </Description>
435     // <RelatedBugs></RelatedBugs>
436     // <Expects Status=success></Expects>
437     // <Code>
438     using System;
439 
440     public class TargetIsPredefinedValueType
441     {
442         #region non-nullable
FloatTypeWithNumbericConversionFromIntInAssignment()443         private static bool FloatTypeWithNumbericConversionFromIntInAssignment()
444         {
445             int failcount = 0;
446             int origin = int.MaxValue;
447             dynamic d = origin;
448             float result = d;
449             if (result != origin)
450             {
451                 failcount++;
452                 System.Console.WriteLine("Test failed at conversion result");
453             }
454 
455             return failcount == 0;
456         }
457 
DoubleTypeWithNumbericConversionFromUlongInAssignment()458         private static bool DoubleTypeWithNumbericConversionFromUlongInAssignment()
459         {
460             int failcount = 0;
461             ulong origin = ulong.MinValue;
462             dynamic d = origin;
463             double result = d;
464             if (result != origin)
465             {
466                 failcount++;
467                 System.Console.WriteLine("Test failed at conversion result");
468             }
469 
470             return failcount == 0;
471         }
472 
DecimalTypeWithNumbericConversionFromUintInAssignment()473         private static bool DecimalTypeWithNumbericConversionFromUintInAssignment()
474         {
475             int failcount = 0;
476             uint origin = uint.MaxValue;
477             dynamic d = origin;
478             decimal result = d;
479             if (result != origin)
480             {
481                 failcount++;
482                 System.Console.WriteLine("Test failed at conversion result");
483             }
484 
485             return failcount == 0;
486         }
487 
GuidTypeWithIdentityConversionInAssignment()488         private static bool GuidTypeWithIdentityConversionInAssignment()
489         {
490             int failcount = 0;
491             Guid origin = new Guid("11111111-2222-3333-4444-555555555555");
492             dynamic d = origin;
493             Guid result = d;
494             if (result != origin)
495             {
496                 failcount++;
497                 System.Console.WriteLine("Test failed at conversion result");
498             }
499 
500             return failcount == 0;
501         }
502 
DatetimeTypeWithIdentityConversionInAssignment()503         private static bool DatetimeTypeWithIdentityConversionInAssignment()
504         {
505             int failcount = 0;
506             DateTime origin = DateTime.Now;
507             dynamic d = origin;
508             DateTime result = d;
509             if (result != origin)
510             {
511                 failcount++;
512                 System.Console.WriteLine("Test failed at conversion result");
513             }
514 
515             return failcount == 0;
516         }
517 
518         #endregion
519         #region nullable
NullableFloatTypeWithNumbericConversionFromLongInAssignment()520         private static bool NullableFloatTypeWithNumbericConversionFromLongInAssignment()
521         {
522             int failcount = 0;
523             long? origin = long.MaxValue;
524             dynamic d = origin;
525             float? result = d;
526             if (result != origin)
527             {
528                 failcount++;
529                 System.Console.WriteLine("Test failed at conversion result");
530             }
531 
532             return failcount == 0;
533         }
534 
NullableDoubleTypeWithNumbericConversionFromFloatInAssignment()535         private static bool NullableDoubleTypeWithNumbericConversionFromFloatInAssignment()
536         {
537             int failcount = 0;
538             float? origin = +0.0f;
539             dynamic d = origin;
540             double? result = d;
541             if (result != origin)
542             {
543                 failcount++;
544                 System.Console.WriteLine("Test failed at conversion result");
545             }
546 
547             return failcount == 0;
548         }
549 
NullableDecimalTypeWithNumbericConversionFromUlongInAssignment()550         private static bool NullableDecimalTypeWithNumbericConversionFromUlongInAssignment()
551         {
552             int failcount = 0;
553             ulong? origin = ulong.MaxValue;
554             dynamic d = origin;
555             decimal? result = d;
556             if (result != origin)
557             {
558                 failcount++;
559                 System.Console.WriteLine("Test failed at conversion result");
560             }
561 
562             return failcount == 0;
563         }
564 
NullableGuidTypeWithIdentityConversionInAssignment()565         private static bool NullableGuidTypeWithIdentityConversionInAssignment()
566         {
567             int failcount = 0;
568             Guid? origin = new Guid("11111111-2222-3333-4444-555555555555");
569             dynamic d = origin;
570             Guid? result = d;
571             if (result != origin)
572             {
573                 failcount++;
574                 System.Console.WriteLine("Test failed at conversion result");
575             }
576 
577             return failcount == 0;
578         }
579 
NullableDatetimeTypeWithNullableIdentityConversionInAssignment()580         private static bool NullableDatetimeTypeWithNullableIdentityConversionInAssignment()
581         {
582             int failcount = 0;
583             DateTime origin = DateTime.Now;
584             dynamic d = origin;
585             DateTime? result = d;
586             if (result != origin)
587             {
588                 failcount++;
589                 System.Console.WriteLine("Test failed at conversion result");
590             }
591 
592             return failcount == 0;
593         }
594 
595         #endregion
596         [Fact]
DynamicCSharpRunTest()597         public static void DynamicCSharpRunTest()
598         {
599             Assert.Equal(0, MainMethod());
600         }
601 
MainMethod()602         public static int MainMethod()
603         {
604             int result = 0;
605             result += Verify.Eval(FloatTypeWithNumbericConversionFromIntInAssignment);
606             result += Verify.Eval(DoubleTypeWithNumbericConversionFromUlongInAssignment);
607             result += Verify.Eval(DecimalTypeWithNumbericConversionFromUintInAssignment);
608             result += Verify.Eval(GuidTypeWithIdentityConversionInAssignment);
609             result += Verify.Eval(DatetimeTypeWithIdentityConversionInAssignment);
610             result += Verify.Eval(NullableFloatTypeWithNumbericConversionFromLongInAssignment);
611             result += Verify.Eval(NullableDoubleTypeWithNumbericConversionFromFloatInAssignment);
612             result += Verify.Eval(NullableDecimalTypeWithNumbericConversionFromUlongInAssignment);
613             result += Verify.Eval(NullableGuidTypeWithIdentityConversionInAssignment);
614             result += Verify.Eval(NullableDatetimeTypeWithNullableIdentityConversionInAssignment);
615             return result;
616         }
617     }
618     // </Code>
619 }
620 
621 
622 
623 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target004.target004
624 {
625     // <Area> Dynamic -- implicit conversion</Area>
626     // <Title> The target type is user-defined value type </Title>
627     // <Description>
628     // User defined struct, generic struct, enum and their nullable form.
629     // </Description>
630     // <RelatedBugs></RelatedBugs>
631     // <Expects Status=success></Expects>
632     // <Code>
633 
634     public enum E
635     {
636         One,
637         Two
638     }
639 
640     public struct S
641     {
642         public int f;
SManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target004.target004.S643         public S(int v)
644         {
645             f = v;
646         }
647     }
648 
649     public struct S2
650     {
651         public int f;
S2ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target004.target004.S2652         public S2(int v)
653         {
654             f = v;
655         }
656 
operator S2ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target004.target004.S2657         public static implicit operator S2(S s1)
658         {
659             return new S2(s1.f);
660         }
661     }
662 
663     public struct GS<T>
664     {
665         public T f;
GSManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target004.target004.GS666         public GS(T v)
667         {
668             f = v;
669         }
670     }
671 
672     public class TargetIsUserdefinedValueType
673     {
674         #region non-nullable
UserdefinedStructWithIdentityConversionInAssignment()675         private static bool UserdefinedStructWithIdentityConversionInAssignment()
676         {
677             int failcount = 0;
678             S origin = new S(10);
679             dynamic d = origin;
680             S result = d;
681             if (result.f != origin.f)
682             {
683                 failcount++;
684                 System.Console.WriteLine("Test failed at conversion result");
685             }
686 
687             return failcount == 0;
688         }
689 
UserdefinedStructWithUserdefinedImplicitConversionInAssignment()690         private static bool UserdefinedStructWithUserdefinedImplicitConversionInAssignment()
691         {
692             int failcount = 0;
693             S origin = new S(10);
694             dynamic d = origin;
695             S2 result = d;
696             if (result.f != origin.f)
697             {
698                 failcount++;
699                 System.Console.WriteLine("Test failed at conversion result");
700             }
701 
702             return failcount == 0;
703         }
704 
UserdefinedGenericStructWithIdentityConversionInAssignment()705         private static bool UserdefinedGenericStructWithIdentityConversionInAssignment()
706         {
707             int failcount = 0;
708             GS<int> origin = new GS<int>(10);
709             dynamic d = origin;
710             GS<int> result = d;
711             if (result.f != origin.f)
712             {
713                 failcount++;
714                 System.Console.WriteLine("Test failed at conversion result");
715             }
716 
717             return failcount == 0;
718         }
719 
UserdefinedEnumWithIdentityConversionInAssignment()720         private static bool UserdefinedEnumWithIdentityConversionInAssignment()
721         {
722             int failcount = 0;
723             E origin = E.One;
724             dynamic d = origin;
725             E result = d;
726             if (result != origin)
727             {
728                 failcount++;
729                 System.Console.WriteLine("Test failed at conversion result");
730             }
731 
732             return failcount == 0;
733         }
734 
735         #endregion
736         #region nullable
NullableUserdefinedStructWithIdentityConversionInAssignment()737         private static bool NullableUserdefinedStructWithIdentityConversionInAssignment()
738         {
739             int failcount = 0;
740             S? origin = new S(10);
741             dynamic d = origin;
742             S? result = d;
743             if (result.Value.f != origin.Value.f)
744             {
745                 failcount++;
746                 System.Console.WriteLine("Test failed at conversion result");
747             }
748 
749             return failcount == 0;
750         }
751 
NullableUserdefinedStructWithNullableUserdefinedImplicitConversionInAssignment()752         private static bool NullableUserdefinedStructWithNullableUserdefinedImplicitConversionInAssignment()
753         {
754             int failcount = 0;
755             S origin = new S(10);
756             dynamic d = origin;
757             S2? result = d;
758             if (result.Value.f != origin.f)
759             {
760                 failcount++;
761                 System.Console.WriteLine("Test failed at conversion result");
762             }
763 
764             return failcount == 0;
765         }
766 
NullableUserdefinedGenericStructWithNullableIdentityConversionInAssignment()767         private static bool NullableUserdefinedGenericStructWithNullableIdentityConversionInAssignment()
768         {
769             int failcount = 0;
770             GS<int> origin = new GS<int>(10);
771             dynamic d = origin;
772             GS<int>? result = d;
773             if (result.Value.f != origin.f)
774             {
775                 failcount++;
776                 System.Console.WriteLine("Test failed at conversion result");
777             }
778 
779             return failcount == 0;
780         }
781 
NullableUserdefinedEnumWithNullableIdentityConversionInAssignment()782         private static bool NullableUserdefinedEnumWithNullableIdentityConversionInAssignment()
783         {
784             int failcount = 0;
785             E origin = E.One;
786             dynamic d = origin;
787             E? result = d;
788             if (result != origin)
789             {
790                 failcount++;
791                 System.Console.WriteLine("Test failed at conversion result");
792             }
793 
794             return failcount == 0;
795         }
796 
797         #endregion
798         [Fact]
DynamicCSharpRunTest()799         public static void DynamicCSharpRunTest()
800         {
801             Assert.Equal(0, MainMethod());
802         }
803 
MainMethod()804         public static int MainMethod()
805         {
806             int result = 0;
807             result += Verify.Eval(UserdefinedStructWithIdentityConversionInAssignment);
808             result += Verify.Eval(UserdefinedStructWithUserdefinedImplicitConversionInAssignment);
809             result += Verify.Eval(UserdefinedGenericStructWithIdentityConversionInAssignment);
810             result += Verify.Eval(UserdefinedEnumWithIdentityConversionInAssignment);
811             result += Verify.Eval(NullableUserdefinedStructWithIdentityConversionInAssignment);
812             result += Verify.Eval(NullableUserdefinedStructWithNullableUserdefinedImplicitConversionInAssignment);
813             result += Verify.Eval(NullableUserdefinedGenericStructWithNullableIdentityConversionInAssignment);
814             result += Verify.Eval(NullableUserdefinedEnumWithNullableIdentityConversionInAssignment);
815             return result;
816         }
817     }
818     // </Code>
819 }
820 
821 
822 
823 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target005.target005
824 {
825     // <Area> Dynamic -- implicit conversion</Area>
826     // <Title> The target type is predefined reference type </Title>
827     // <Description>
828     // string, object, System.ValueType, System.Enum, System.Array, System.Delegate
829     // </Description>
830     // <RelatedBugs></RelatedBugs>
831     // <Expects Status=success></Expects>
832     // <Code>
833     using System;
834 
835     public enum E
836     {
837         One,
838         Two
839     }
840 
D()841     public delegate int D();
842     public struct S
843     {
MManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target005.target005.S844         public static int M()
845         {
846             return 10;
847         }
848 
849         public int f;
SManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target005.target005.S850         public S(int v)
851         {
852             f = v;
853         }
854     }
855 
856     public class MyException : Exception
857     {
858         public int code;
MyException(int c)859         public MyException(int c)
860         {
861             code = c;
862         }
863     }
864 
865     public class TargetIsPredefinedReferenceType
866     {
StringWithIdentityConversionInAssignment()867         private static bool StringWithIdentityConversionInAssignment()
868         {
869             int failcount = 0;
870             string origin = "aabb";
871             dynamic d = origin;
872             String result = d;
873             if (result != origin)
874             {
875                 failcount++;
876                 System.Console.WriteLine("Test failed at conversion result");
877             }
878 
879             return failcount == 0;
880         }
881 
ObjectWithReferenceConversionFromStringInAssignment()882         private static bool ObjectWithReferenceConversionFromStringInAssignment()
883         {
884             int failcount = 0;
885             // no callsite for object
886             //
887             string origin = "aabb";
888             dynamic d = origin;
889             object result = d;
890             if ((string)result != origin)
891             {
892                 failcount++;
893                 System.Console.WriteLine("Test failed at conversion result");
894             }
895 
896             return failcount == 0;
897         }
898 
ObjectWithBoxingConversionFromIntInAssignment()899         private static bool ObjectWithBoxingConversionFromIntInAssignment()
900         {
901             int failcount = 0;
902             // no callsite for object
903             //
904             int origin = 10;
905             dynamic d = origin;
906             Object result = d;
907             if ((int)result != origin)
908             {
909                 failcount++;
910                 System.Console.WriteLine("Test failed at conversion result");
911             }
912 
913             return failcount == 0;
914         }
915 
ValueTypeWithBoxingConversionFromUserdefinedStructInAssignment()916         private static bool ValueTypeWithBoxingConversionFromUserdefinedStructInAssignment()
917         {
918             int failcount = 0;
919             S origin = new S(10);
920             dynamic d = origin;
921             ValueType result = d;
922             if (((S)result).f != origin.f)
923             {
924                 failcount++;
925                 System.Console.WriteLine("Test failed at conversion result");
926             }
927 
928             return failcount == 0;
929         }
930 
EnumWithBoxingConversionFromUserdefinedEnumInAssignment()931         private static bool EnumWithBoxingConversionFromUserdefinedEnumInAssignment()
932         {
933             int failcount = 0;
934             E origin = E.Two;
935             dynamic d = origin;
936             Enum result = d;
937             if ((E)result != origin)
938             {
939                 failcount++;
940                 System.Console.WriteLine("Test failed at conversion result");
941             }
942 
943             return failcount == 0;
944         }
945 
ArrayWithReferenceConversionFromUserdefinedArrayInAssignment()946         private static bool ArrayWithReferenceConversionFromUserdefinedArrayInAssignment()
947         {
948             int failcount = 0;
949             var origin = new[]
950             {
951             1, 2, 3
952             }
953 
954             ;
955             dynamic d = origin;
956             Array result = d;
957             if (((int[])result)[1] != origin[1])
958             {
959                 failcount++;
960                 System.Console.WriteLine("Test failed at conversion result");
961             }
962 
963             return failcount == 0;
964         }
965 
DelegateWithReferenceConversionFromUserdefinedDelegateInAssignment()966         private static bool DelegateWithReferenceConversionFromUserdefinedDelegateInAssignment()
967         {
968             int failcount = 0;
969             D origin = S.M;
970             dynamic d = origin;
971             Delegate result = d;
972             if ((int)result.DynamicInvoke(null) != origin())
973             {
974                 failcount++;
975                 System.Console.WriteLine("Test failed at conversion result");
976             }
977 
978             return failcount == 0;
979         }
980 
ExceptionWithReferenceConversionFromUserdefinedExceptionInAssignment()981         private static bool ExceptionWithReferenceConversionFromUserdefinedExceptionInAssignment()
982         {
983             int failcount = 0;
984             var origin = new MyException(0xE000);
985             dynamic d = origin;
986             Exception result = d;
987             if (((MyException)result).code != origin.code)
988             {
989                 failcount++;
990                 System.Console.WriteLine("Test failed at conversion result");
991             }
992 
993             return failcount == 0;
994         }
995 
996         [Fact]
DynamicCSharpRunTest()997         public static void DynamicCSharpRunTest()
998         {
999             Assert.Equal(0, MainMethod());
1000         }
1001 
MainMethod()1002         public static int MainMethod()
1003         {
1004             int result = 0;
1005             result += Verify.Eval(StringWithIdentityConversionInAssignment);
1006             result += Verify.Eval(ObjectWithReferenceConversionFromStringInAssignment);
1007             result += Verify.Eval(ObjectWithBoxingConversionFromIntInAssignment);
1008             result += Verify.Eval(ValueTypeWithBoxingConversionFromUserdefinedStructInAssignment);
1009             result += Verify.Eval(EnumWithBoxingConversionFromUserdefinedEnumInAssignment);
1010             result += Verify.Eval(ArrayWithReferenceConversionFromUserdefinedArrayInAssignment);
1011             result += Verify.Eval(DelegateWithReferenceConversionFromUserdefinedDelegateInAssignment);
1012             result += Verify.Eval(ExceptionWithReferenceConversionFromUserdefinedExceptionInAssignment);
1013             return result;
1014         }
1015     }
1016     // </Code>
1017 }
1018 
1019 
1020 
1021 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target006.target006
1022 {
1023     // <Area> Dynamic -- implicit conversion</Area>
1024     // <Title> The target type is array type </Title>
1025     // <Description>
1026     // </Description>
1027     // <RelatedBugs></RelatedBugs>
1028     // <Expects Status=success></Expects>
1029     // <Code>
1030 
1031     public class TargetIsArray
1032     {
ArrayWithIdentityConversionInAssignment()1033         private static bool ArrayWithIdentityConversionInAssignment()
1034         {
1035             int failcount = 0;
1036             var origin = new[]
1037             {
1038             1, 2, 3
1039             }
1040 
1041             ;
1042             dynamic d = origin;
1043             int[] result = d;
1044             if (result[1] != origin[1])
1045             {
1046                 failcount++;
1047                 System.Console.WriteLine("Test failed at conversion result");
1048             }
1049 
1050             return failcount == 0;
1051         }
1052 
ArrayWithReferenceConversionInAssignment()1053         private static bool ArrayWithReferenceConversionInAssignment()
1054         {
1055             int failcount = 0;
1056             var origin = new[]
1057             {
1058             "aa", "bb", "cc"
1059             }
1060 
1061             ;
1062             dynamic d = origin;
1063             object[] result = d;
1064             if ((string)(result[1]) != origin[1])
1065             {
1066                 failcount++;
1067                 System.Console.WriteLine("Test failed at conversion result");
1068             }
1069 
1070             return failcount == 0;
1071         }
1072 
1073         [Fact]
DynamicCSharpRunTest()1074         public static void DynamicCSharpRunTest()
1075         {
1076             Assert.Equal(0, MainMethod());
1077         }
1078 
MainMethod()1079         public static int MainMethod()
1080         {
1081             int result = 0;
1082             result += Verify.Eval(ArrayWithIdentityConversionInAssignment);
1083             result += Verify.Eval(ArrayWithReferenceConversionInAssignment);
1084             return result;
1085         }
1086     }
1087     // </Code>
1088 }
1089 
1090 
1091 
1092 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target007.target007
1093 {
1094     // <Area> Dynamic -- implicit conversion</Area>
1095     // <Title> The target type is delegate </Title>
1096     // <Description>
1097     // </Description>
1098     // <RelatedBugs></RelatedBugs>
1099     // <Expects Status=success></Expects>
1100     // <Code>
1101 
D()1102     public delegate int D();
GD()1103     public delegate T GD<T>();
VGD(T2 i)1104     public delegate T1 VGD<out T1, in T2>(T2 i);
1105     public class C
1106     {
M1()1107         public static int M1()
1108         {
1109             return 10;
1110         }
1111 
M2(int i)1112         public static int M2(int i)
1113         {
1114             return i;
1115         }
1116     }
1117 
1118     public class TargetIsDelegate
1119     {
DelegateWithIdentityConversionInAssignment()1120         private static bool DelegateWithIdentityConversionInAssignment()
1121         {
1122             int failcount = 0;
1123             D origin = C.M1;
1124             dynamic d = origin;
1125             D result = d;
1126             if (result() != origin())
1127             {
1128                 failcount++;
1129                 System.Console.WriteLine("Test failed at conversion result");
1130             }
1131 
1132             return failcount == 0;
1133         }
1134 
GenericDelegateWithIdentityConversionInAssignment()1135         private static bool GenericDelegateWithIdentityConversionInAssignment()
1136         {
1137             int failcount = 0;
1138             GD<int> origin = C.M1;
1139             dynamic d = origin;
1140             GD<int> result = d;
1141             if (result() != origin())
1142             {
1143                 failcount++;
1144                 System.Console.WriteLine("Test failed at conversion result");
1145             }
1146 
1147             return failcount == 0;
1148         }
1149 
VariantGenericDelegateWithIdentityConversionInAssignment()1150         private static bool VariantGenericDelegateWithIdentityConversionInAssignment()
1151         {
1152             int failcount = 0;
1153             VGD<int, int> origin = C.M2;
1154             dynamic d = origin;
1155             VGD<int, int> result = d;
1156             if (result(11) != origin(11))
1157             {
1158                 failcount++;
1159                 System.Console.WriteLine("Test failed at conversion result");
1160             }
1161 
1162             return failcount == 0;
1163         }
1164 
1165         [Fact]
DynamicCSharpRunTest()1166         public static void DynamicCSharpRunTest()
1167         {
1168             Assert.Equal(0, MainMethod());
1169         }
1170 
MainMethod()1171         public static int MainMethod()
1172         {
1173             int result = 0;
1174             result += Verify.Eval(DelegateWithIdentityConversionInAssignment);
1175             result += Verify.Eval(GenericDelegateWithIdentityConversionInAssignment);
1176             result += Verify.Eval(VariantGenericDelegateWithIdentityConversionInAssignment);
1177             return result;
1178         }
1179     }
1180     // </Code>
1181 }
1182 
1183 
1184 
1185 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target008.target008
1186 {
1187     // <Area> Dynamic -- implicit conversion</Area>
1188     // <Title> The target type is user-defined class </Title>
1189     // <Description>
1190     // </Description>
1191     // <RelatedBugs></RelatedBugs>
1192     // <Expects Status=success></Expects>
1193     // <Code>
1194 
1195     public class B
1196     {
1197     }
1198 
1199     public class C : B
1200     {
C()1201         public C()
1202         {
1203         }
1204 
1205         public int f1 = 10;
C(int v)1206         public C(int v)
1207         {
1208             f1 = v;
1209         }
1210 
operator C2(C c)1211         public static implicit operator C2(C c)
1212         {
1213             return new C2(c.f1);
1214         }
1215     }
1216 
1217     public class C2
1218     {
1219         public int f1 = 10;
C2(int v)1220         public C2(int v)
1221         {
1222             f1 = v;
1223         }
1224     }
1225 
1226     public class GB<T>
1227     {
1228     }
1229 
1230     public class GGC<T> : GB<T>
1231     {
1232     }
1233 
1234     public class TargetIsUserdefinedClass
1235     {
UserdefinedClassWithReferenceConversionInAssignment()1236         private static bool UserdefinedClassWithReferenceConversionInAssignment()
1237         {
1238             int failcount = 0;
1239             var origin = new C();
1240             dynamic d = origin;
1241             B result = d;
1242             if (result != origin)
1243             {
1244                 failcount++;
1245                 System.Console.WriteLine("Test failed at conversion result");
1246             }
1247 
1248             return failcount == 0;
1249         }
1250 
UserdefinedClassWithUserdefinedImplicitConversionInAssignment()1251         private static bool UserdefinedClassWithUserdefinedImplicitConversionInAssignment()
1252         {
1253             int failcount = 0;
1254             var origin = new C(22);
1255             dynamic d = origin;
1256             C2 result = d;
1257             if (result.f1 != origin.f1)
1258             {
1259                 failcount++;
1260                 System.Console.WriteLine("Test failed at conversion result");
1261             }
1262 
1263             return failcount == 0;
1264         }
1265 
UserdefinedGenericClassWithReferenceConversionInAssignment()1266         private static bool UserdefinedGenericClassWithReferenceConversionInAssignment()
1267         {
1268             int failcount = 0;
1269             var origin = new GGC<int>();
1270             dynamic d = origin;
1271             GB<int> result = d;
1272             if (result != origin)
1273             {
1274                 failcount++;
1275                 System.Console.WriteLine("Test failed at conversion result");
1276             }
1277 
1278             return failcount == 0;
1279         }
1280 
1281         [Fact]
DynamicCSharpRunTest()1282         public static void DynamicCSharpRunTest()
1283         {
1284             Assert.Equal(0, MainMethod());
1285         }
1286 
MainMethod()1287         public static int MainMethod()
1288         {
1289             int result = 0;
1290             result += Verify.Eval(UserdefinedClassWithReferenceConversionInAssignment);
1291             result += Verify.Eval(UserdefinedClassWithUserdefinedImplicitConversionInAssignment);
1292             result += Verify.Eval(UserdefinedGenericClassWithReferenceConversionInAssignment);
1293             return result;
1294         }
1295     }
1296     // </Code>
1297 }
1298 
1299 
1300 
1301 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target010.target010
1302 {
1303     // <Area> Dynamic -- implicit conversion</Area>
1304     // <Title> The target type is type parameter </Title>
1305     // <Description>
1306     // </Description>
1307     // <RelatedBugs></RelatedBugs>
1308     // <Expects Status=success></Expects>
1309     // <Code>
1310 
1311     public class B
1312     {
1313     }
1314 
1315     public interface I
1316     {
1317     }
1318 
1319     public class C : B, I
1320     {
1321     }
1322 
1323     public class TargetIsTypeParameter
1324     {
1325         private static bool TestMethodForReferenceConversionFromStringToObjectInAssignment<T>() where T : class
1326         {
1327             int failcount = 0;
1328             var origin = "aa";
1329             dynamic d = origin;
1330             T result = d;
1331             if (result != (object)origin)
1332             {
1333                 failcount++;
1334                 System.Console.WriteLine("Test failed at conversion result");
1335             }
1336 
1337             return failcount == 0;
1338         }
1339 
ReferenceConversionFromStringToObjectInAssignment()1340         private static bool ReferenceConversionFromStringToObjectInAssignment()
1341         {
1342             return TestMethodForReferenceConversionFromStringToObjectInAssignment<object>();
1343         }
1344 
1345         private static bool TestMethodForReferenceConversionToBaseClassInAssignment<T>() where T : B
1346         {
1347             int failcount = 0;
1348             var origin = new C();
1349             dynamic d = origin;
1350             T result = d;
1351             if (result != (B)origin)
1352             {
1353                 failcount++;
1354                 System.Console.WriteLine("Test failed at conversion result");
1355             }
1356 
1357             return failcount == 0;
1358         }
1359 
ReferenceConversionToBaseClassInAssignment()1360         private static bool ReferenceConversionToBaseClassInAssignment()
1361         {
1362             return TestMethodForReferenceConversionToBaseClassInAssignment<B>();
1363         }
1364 
1365         private static bool TestMethodForReferenceConversionToBaseInterfaceInAssignment<T>() where T : class, I
1366         {
1367             int failcount = 0;
1368             var origin = new C();
1369             dynamic d = origin;
1370             T result = d;
1371             if (result != (I)origin)
1372             {
1373                 failcount++;
1374                 System.Console.WriteLine("Test failed at conversion result");
1375             }
1376 
1377             return failcount == 0;
1378         }
1379 
ReferenceConversionToBaseInterfaceInAssignment()1380         private static bool ReferenceConversionToBaseInterfaceInAssignment()
1381         {
1382             return TestMethodForReferenceConversionToBaseInterfaceInAssignment<I>();
1383         }
1384 
1385         [Fact]
DynamicCSharpRunTest()1386         public static void DynamicCSharpRunTest()
1387         {
1388             Assert.Equal(0, MainMethod());
1389         }
1390 
MainMethod()1391         public static int MainMethod()
1392         {
1393             int result = 0;
1394             result += Verify.Eval(ReferenceConversionFromStringToObjectInAssignment);
1395             result += Verify.Eval(ReferenceConversionToBaseClassInAssignment);
1396             result += Verify.Eval(ReferenceConversionToBaseInterfaceInAssignment);
1397             return result;
1398         }
1399     }
1400     // </Code>
1401 }
1402 
1403 
1404 
1405 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target012.target012
1406 {
1407     // <Title>Guid as dynamic</Title>
1408     // <Description>
1409     // </Description>
1410     // <RelatedBugs></RelatedBugs>
1411     //<Expects Status=success></Expects>
1412     // <Code>
1413     using System;
1414 
1415     public class C
1416     {
1417 
DynamicCSharpRunTest()1418         public static void DynamicCSharpRunTest()
1419         {
1420             Assert.Equal(0, MainMethod());
1421         }
1422 
MainMethod()1423         public static int MainMethod()
1424         {
1425             dynamic g1 = new Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
1426             Strc str1 = new Strc()
1427             {
1428                 Val = g1
1429             }
1430 
1431             ;
1432             if (str1.Val.Equals(g1))
1433                 return 0;
1434             else
1435                 return 1;
1436         }
1437     }
1438 
1439     internal struct Strc
1440     {
1441         public Guid Val;
LogManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.target012.target012.Strc1442         public static void Log(string s)
1443         {
1444             System.Console.WriteLine("{0}", s);
1445         }
1446     }
1447     // </Code>
1448 }
1449 
1450 
1451 
1452 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.conversion001.conversion001
1453 {
1454     public class Test
1455     {
1456         [Fact]
DynamicCSharpRunTest()1457         public static void DynamicCSharpRunTest()
1458         {
1459             Assert.Equal(0, MainMethod(null));
1460         }
1461 
MainMethod(string[] args)1462         public static int MainMethod(string[] args)
1463         {
1464             dynamic d;
1465             d = int.MaxValue;
1466             //int to long
1467             if ((long)d != int.MaxValue)
1468                 return 1;
1469             //int to ulong
1470             if ((ulong)d != int.MaxValue)
1471                 return 1;
1472             //int to uint
1473             if ((uint)d != int.MaxValue)
1474                 return 1;
1475             //int to double
1476             if ((double)d != int.MaxValue)
1477                 return 1;
1478             //int to float
1479             if ((float)d != int.MaxValue)
1480                 return 1;
1481             //int to decimal
1482             if ((decimal)d != int.MaxValue)
1483                 return 1;
1484             d = short.MaxValue;
1485             //short to int
1486             if ((int)d != short.MaxValue)
1487                 return 1;
1488             //short to long
1489             if ((long)d != short.MaxValue)
1490                 return 1;
1491             //short to uint
1492             if ((uint)d != short.MaxValue)
1493                 return 1;
1494             return 0;
1495         }
1496     }
1497     // </Code>
1498 }
1499 
1500 
1501 
1502 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.conversion002.conversion002
1503 {
1504     // <Area> Dynamic -- implicit conversion</Area>
1505     // <Title> Implicit conversion from runtime type of dynamic object </Title>
1506     // <Description>
1507     // No implicit conversion but exist explicit conversion.
1508     // There are no distinction between implicit and explicit conversion operators in convert of expression tree.
1509     // </Description>
1510     // <RelatedBugs></RelatedBugs>
1511     // <Expects Status=success></Expects>
1512     // <Code>
1513     using System;
1514 
1515     public class C
1516     {
1517         public int f1 = 10;
C(int v)1518         public C(int v)
1519         {
1520             f1 = v;
1521         }
1522 
operator C2(C c)1523         public static explicit operator C2(C c)
1524         {
1525             return new C2(c.f1);
1526         }
1527     }
1528 
1529     public class C2
1530     {
1531         public int f1 = 10;
C2(int v)1532         public C2(int v)
1533         {
1534             f1 = v;
1535         }
1536     }
1537 
1538     public class ConversionInRuntimeType
1539     {
NoPredefinedImplicitConversionButExistExplicitConversion()1540         private static bool NoPredefinedImplicitConversionButExistExplicitConversion()
1541         {
1542             int failcount = 0;
1543             ulong origin = 100;
1544             dynamic d = origin;
1545             try
1546             {
1547                 int result = (int)d;
1548                 if ((ulong)result != origin)
1549                 {
1550                     failcount++;
1551                     System.Console.WriteLine("Test failed at conversion result");
1552                 }
1553             }
1554             catch (Exception e)
1555             {
1556                 failcount++;
1557                 System.Console.WriteLine("Test failed: catch an unknown exception {0}", e);
1558             }
1559 
1560             return failcount == 0;
1561         }
1562 
NoUserdefinedImplicitConversionButExistExplicitConversion()1563         private static bool NoUserdefinedImplicitConversionButExistExplicitConversion()
1564         {
1565             int failcount = 0;
1566             C origin = new C(22);
1567             dynamic d = origin;
1568             try
1569             {
1570                 C2 result = (C2)d;
1571                 if (result.f1 != origin.f1)
1572                 {
1573                     failcount++;
1574                     System.Console.WriteLine("Test failed at conversion result");
1575                 }
1576             }
1577             catch (Exception e)
1578             {
1579                 failcount++;
1580                 System.Console.WriteLine("Test failed: catch an unknown exception {0}", e);
1581             }
1582 
1583             return failcount == 0;
1584         }
1585 
1586         [Fact]
DynamicCSharpRunTest()1587         public static void DynamicCSharpRunTest()
1588         {
1589             Assert.Equal(0, MainMethod());
1590         }
1591 
MainMethod()1592         public static int MainMethod()
1593         {
1594             int result = 0;
1595             result += Verify.Eval(NoPredefinedImplicitConversionButExistExplicitConversion);
1596             result += Verify.Eval(NoUserdefinedImplicitConversionButExistExplicitConversion);
1597             return result;
1598         }
1599     }
1600     // </Code>
1601 }
1602 
1603 
1604 
1605 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.conversion003.conversion003
1606 {
1607     // <Area> Dynamic -- implicit conversion</Area>
1608     // <Title>  Implicit conversion from runtime type of dynamic object  </Title>
1609     // <Description>
1610     // No implicit and explicit conversions.
1611     // </Description>
1612     // <RelatedBugs></RelatedBugs>
1613     // <Expects Status=success></Expects>
1614     // <Code>
1615     using System;
1616 
1617     public class C
1618     {
1619     }
1620 
1621     public class C2
1622     {
1623     }
1624 
1625     public class ConversionInRuntimeType
1626     {
NoPredefinedImplicitAndExplicitConversion()1627         private static bool NoPredefinedImplicitAndExplicitConversion()
1628         {
1629             int failcount = 0;
1630             string origin = "aa";
1631             dynamic d = origin;
1632             try
1633             {
1634                 int result = d;
1635                 failcount++;
1636                 System.Console.WriteLine("Test failed: should throw exception");
1637             }
1638             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
1639             {
1640                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "string", "int"))
1641                     failcount++;
1642             }
1643             catch (Exception e)
1644             {
1645                 failcount++;
1646                 System.Console.WriteLine("Test failed: catch an unknown exception {0}", e);
1647             }
1648 
1649             return failcount == 0;
1650         }
1651 
NoPredefinedImplicitAndExplicitConversionForArray()1652         private static bool NoPredefinedImplicitAndExplicitConversionForArray()
1653         {
1654             int failcount = 0;
1655             var origin = new int[]
1656             {
1657             1, 2, 3
1658             }
1659 
1660             ;
1661             dynamic d = origin;
1662             try
1663             {
1664                 long[] result = d;
1665                 failcount++;
1666                 System.Console.WriteLine("Test failed: should throw exception");
1667             }
1668             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
1669             {
1670                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "int[]", "long[]"))
1671                     failcount++;
1672             }
1673             catch (Exception e)
1674             {
1675                 failcount++;
1676                 System.Console.WriteLine("Test failed: catch an unknown exception {0}", e);
1677             }
1678 
1679             return failcount == 0;
1680         }
1681 
NoUserdefinedImplicitAndExplicitConversion()1682         private static bool NoUserdefinedImplicitAndExplicitConversion()
1683         {
1684             int failcount = 0;
1685             C origin = new C();
1686             dynamic d = origin;
1687             try
1688             {
1689                 C2 result = d;
1690                 failcount++;
1691                 System.Console.WriteLine("Test failed: should throw exception");
1692             }
1693             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
1694             {
1695                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "C", "C2"))
1696                     failcount++;
1697             }
1698             catch (Exception e)
1699             {
1700                 failcount++;
1701                 System.Console.WriteLine("Test failed: catch an unknown exception {0}", e);
1702             }
1703 
1704             return failcount == 0;
1705         }
1706 
1707         [Fact]
DynamicCSharpRunTest()1708         public static void DynamicCSharpRunTest()
1709         {
1710             Assert.Equal(0, MainMethod());
1711         }
1712 
MainMethod()1713         public static int MainMethod()
1714         {
1715             int result = 0;
1716             result += Verify.Eval(NoPredefinedImplicitAndExplicitConversion);
1717             result += Verify.Eval(NoPredefinedImplicitAndExplicitConversionForArray);
1718             result += Verify.Eval(NoUserdefinedImplicitAndExplicitConversion);
1719             return result;
1720         }
1721     }
1722     // </Code>
1723 }
1724 
1725 
1726 
1727 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.conversion005.conversion005
1728 {
1729     public class Test
1730     {
1731         [Fact]
DynamicCSharpRunTest()1732         public static void DynamicCSharpRunTest()
1733         {
1734             Assert.Equal(0, MainMethod(null));
1735         }
1736 
MainMethod(string[] args)1737         public static int MainMethod(string[] args)
1738         {
1739             dynamic d = null;
1740             string s = d;
1741             return 0;
1742         }
1743     }
1744     // </Code>
1745 }
1746 
1747 
1748 
1749 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context001.context001
1750 {
1751     // <Area> Dynamic -- implicit conversion</Area>
1752     // <Title> The conversion occurs in assignment operators </Title>
1753     // <Description>
1754     // </Description>
1755     // <RelatedBugs></RelatedBugs>
1756     // <Expects Status=success></Expects>
1757     // <Code>
1758 
D(int i)1759     public delegate int D(int i);
1760     public class C
1761     {
1762         public int F1;
1763         public static uint F2;
1764         public D F3;
1765         public static D F4;
M(int i)1766         public static int M(int i)
1767         {
1768             return i;
1769         }
1770 
1771         public long P1
1772         {
1773             get;
1774             set;
1775         }
1776 
1777         public static ulong P2
1778         {
1779             get;
1780             set;
1781         }
1782 
1783         private short _f1;
1784         public short this[int i]
1785         {
1786             get
1787             {
1788                 return _f1;
1789             }
1790 
1791             set
1792             {
1793                 _f1 = value;
1794             }
1795         }
1796     }
1797 
1798     public class ConversionInAssignmentOperators
1799     {
LeftIsLocalVariable()1800         private static bool LeftIsLocalVariable()
1801         {
1802             int failcount = 0;
1803             sbyte origin = 10;
1804             dynamic d = origin;
1805             int result = d;
1806             if (result != origin)
1807             {
1808                 failcount++;
1809                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1810             }
1811 
1812             return failcount == 0;
1813         }
1814 
LeftIsInstanceField()1815         private static bool LeftIsInstanceField()
1816         {
1817             int failcount = 0;
1818             sbyte origin = 10;
1819             dynamic d = origin;
1820             var c = new C();
1821             c.F1 = d;
1822             if (c.F1 != origin)
1823             {
1824                 failcount++;
1825                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1826             }
1827 
1828             return failcount == 0;
1829         }
1830 
LeftIsStaticField()1831         private static bool LeftIsStaticField()
1832         {
1833             int failcount = 0;
1834             byte origin = 10;
1835             dynamic d = origin;
1836             C.F2 = d;
1837             if (C.F2 != origin)
1838             {
1839                 failcount++;
1840                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1841             }
1842 
1843             return failcount == 0;
1844         }
1845 
LeftIsInstanceProperty()1846         private static bool LeftIsInstanceProperty()
1847         {
1848             int failcount = 0;
1849             sbyte origin = 10;
1850             dynamic d = origin;
1851             var c = new C();
1852             c.P1 = d;
1853             if (c.P1 != origin)
1854             {
1855                 failcount++;
1856                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1857             }
1858 
1859             return failcount == 0;
1860         }
1861 
LeftIsStaticProperty()1862         private static bool LeftIsStaticProperty()
1863         {
1864             int failcount = 0;
1865             byte origin = 10;
1866             dynamic d = origin;
1867             C.P2 = d;
1868             if (C.P2 != (ulong)origin)
1869             {
1870                 failcount++;
1871                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1872             }
1873 
1874             return failcount == 0;
1875         }
1876 
LeftIsInstanceDelegateField()1877         private static bool LeftIsInstanceDelegateField()
1878         {
1879             int failcount = 0;
1880             D origin = C.M;
1881             dynamic d = origin;
1882             var c = new C();
1883             c.F3 = d;
1884             if (c.F3(11) != origin(11))
1885             {
1886                 failcount++;
1887                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1888             }
1889 
1890             return failcount == 0;
1891         }
1892 
LeftIsStaticDelegateField()1893         private static bool LeftIsStaticDelegateField()
1894         {
1895             int failcount = 0;
1896             D origin = C.M;
1897             dynamic d = origin;
1898             C.F4 = d;
1899             if (C.F4(22) != origin(22))
1900             {
1901                 failcount++;
1902                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1903             }
1904 
1905             return failcount == 0;
1906         }
1907 
LeftIsInstanceIndexer()1908         private static bool LeftIsInstanceIndexer()
1909         {
1910             int failcount = 0;
1911             sbyte origin = 10;
1912             dynamic d = origin;
1913             var c = new C();
1914             c[1] = d;
1915             if (c[1] != origin)
1916             {
1917                 failcount++;
1918                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1919             }
1920 
1921             return failcount == 0;
1922         }
1923 
InObjectInitializer()1924         private static bool InObjectInitializer()
1925         {
1926             int failcount = 0;
1927             sbyte origin = 10;
1928             dynamic d = origin;
1929             byte origin2 = 20;
1930             dynamic d2 = origin2;
1931             var c = new C()
1932             {
1933                 F1 = d,
1934                 P1 = d2
1935             }
1936 
1937             ;
1938             if ((c.F1 != origin) || (c.P1 != origin2))
1939             {
1940                 failcount++;
1941                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1942             }
1943 
1944             return failcount == 0;
1945         }
1946 
1947         [Fact]
DynamicCSharpRunTest()1948         public static void DynamicCSharpRunTest()
1949         {
1950             Assert.Equal(0, MainMethod());
1951         }
1952 
MainMethod()1953         public static int MainMethod()
1954         {
1955             int result = 0;
1956             result += Verify.Eval(LeftIsLocalVariable);
1957             result += Verify.Eval(LeftIsInstanceField);
1958             result += Verify.Eval(LeftIsStaticField);
1959             result += Verify.Eval(LeftIsInstanceProperty);
1960             result += Verify.Eval(LeftIsStaticProperty);
1961             result += Verify.Eval(LeftIsInstanceDelegateField);
1962             result += Verify.Eval(LeftIsStaticDelegateField);
1963             result += Verify.Eval(LeftIsInstanceIndexer);
1964             result += Verify.Eval(InObjectInitializer);
1965             return result;
1966         }
1967     }
1968     // </Code>
1969 }
1970 
1971 
1972 
1973 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context002.context002
1974 {
1975     // <Area> Dynamic -- implicit conversion</Area>
1976     // <Title> The conversion occurs in boolean expression </Title>
1977     // <Description>
1978     // </Description>
1979     // <RelatedBugs></RelatedBugs>
1980     // <Expects Status=success></Expects>
1981     // <Code>
1982     using System.Linq;
1983 
1984     public class ConversionInBooleanExpression
1985     {
InIfStatement()1986         private static bool InIfStatement()
1987         {
1988             int failcount = 0;
1989             bool origin = false;
1990             dynamic d = origin;
1991             if (d)
1992             {
1993                 failcount++;
1994                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
1995             }
1996 
1997             return failcount == 0;
1998         }
1999 
InWhileStatement()2000         private static bool InWhileStatement()
2001         {
2002             int failcount = 0;
2003             bool origin = false;
2004             dynamic d = origin;
2005             while (d)
2006             {
2007                 failcount++;
2008                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2009                 break;
2010             }
2011 
2012             return failcount == 0;
2013         }
2014 
InDoWhileStatement()2015         private static bool InDoWhileStatement()
2016         {
2017             int failcount = 0;
2018             bool origin = false;
2019             dynamic d = origin;
2020             int count = 0;
2021             do
2022             {
2023                 if (count == 0)
2024                 {
2025                     count++;
2026                 }
2027                 else
2028                 {
2029                     failcount++;
2030                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2031                     break;
2032                 }
2033             }
2034             while (d);
2035             return failcount == 0;
2036         }
2037 
InForStatement()2038         private static bool InForStatement()
2039         {
2040             int failcount = 0;
2041             bool origin = false;
2042             dynamic d = origin;
2043             for (; d;)
2044             {
2045                 failcount++;
2046                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2047                 break;
2048             }
2049 
2050             return failcount == 0;
2051         }
2052 
InConditionalOperator()2053         private static bool InConditionalOperator()
2054         {
2055             int failcount = 0;
2056             bool origin = false;
2057             dynamic d = origin;
2058             if (d ? true : false)
2059             {
2060                 failcount++;
2061                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2062             }
2063 
2064             return failcount == 0;
2065         }
2066 
InWhereClauseOfQueryExpression()2067         private static bool InWhereClauseOfQueryExpression()
2068         {
2069             int failcount = 0;
2070             var a = new[]
2071             {
2072             1, 2, 3
2073             }
2074 
2075             ;
2076             bool origin = true;
2077             dynamic d = origin;
2078             var q = (
2079                 from m in a
2080                 where d
2081                 select m).ToArray();
2082             if (q[1] != a[1])
2083             {
2084                 failcount++;
2085                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2086             }
2087 
2088             return failcount == 0;
2089         }
2090 
2091         [Fact]
DynamicCSharpRunTest()2092         public static void DynamicCSharpRunTest()
2093         {
2094             Assert.Equal(0, MainMethod());
2095         }
2096 
MainMethod()2097         public static int MainMethod()
2098         {
2099             int result = 0;
2100             result += Verify.Eval(InIfStatement);
2101             result += Verify.Eval(InWhileStatement);
2102             result += Verify.Eval(InDoWhileStatement);
2103             result += Verify.Eval(InForStatement);
2104             result += Verify.Eval(InConditionalOperator);
2105             result += Verify.Eval(InWhereClauseOfQueryExpression);
2106             return result;
2107         }
2108     }
2109     // </Code>
2110 }
2111 
2112 
2113 
2114 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context002a.context002a
2115 {
2116     // <Area> Dynamic -- implicit conversion</Area>
2117     // <Title> The conversion occurs in boolean expression </Title>
2118     // <Description>
2119     // </Description>
2120     // <RelatedBugs></RelatedBugs>
2121     // <Expects Status=success></Expects>
2122     // <Code>
2123     using System.Linq;
2124 
2125     public class ConversionInBooleanExpression
2126     {
2127         public class C
2128         {
C()2129             public C()
2130             {
2131             }
2132 
C(int f)2133             public C(int f)
2134             {
2135                 Field = f;
2136             }
2137 
2138             public int Field;
operator bool(C p1)2139             public static explicit operator bool (C p1)
2140             {
2141                 if (p1.Field == 0)
2142                     return true;
2143                 else
2144                     return false;
2145             }
2146         }
2147 
InIfStatement0()2148         private static bool InIfStatement0()
2149         {
2150             int failcount = 0;
2151             C origin = new C(0);
2152             dynamic d = origin;
2153             if ((bool)d)
2154             {
2155             }
2156             else
2157             {
2158                 failcount++;
2159                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2160             }
2161 
2162             return failcount == 0;
2163         }
2164 
InIfStatement1()2165         private static bool InIfStatement1()
2166         {
2167             int failcount = 0;
2168             C origin = new C(1);
2169             dynamic d = origin;
2170             if ((bool)d)
2171             {
2172                 failcount++;
2173                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2174             }
2175 
2176             return failcount == 0;
2177         }
2178 
InWhileStatement0()2179         private static bool InWhileStatement0()
2180         {
2181             int failcount = 0;
2182             C origin = new C(0);
2183             dynamic d = origin;
2184             while ((bool)d)
2185             {
2186                 failcount++; //touch here once.
2187                 break;
2188             }
2189 
2190             return failcount == 1;
2191         }
2192 
InWhileStatement1()2193         private static bool InWhileStatement1()
2194         {
2195             int failcount = 0;
2196             C origin = new C(1);
2197             dynamic d = origin;
2198             while ((bool)d)
2199             {
2200                 failcount++;
2201                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2202                 break;
2203             }
2204 
2205             return failcount == 0;
2206         }
2207 
InDoWhileStatement0()2208         private static bool InDoWhileStatement0()
2209         {
2210             int failcount = 0;
2211             C origin = new C(0);
2212             dynamic d = origin;
2213             int count = 0;
2214             do
2215             {
2216                 if (count == 0)
2217                 {
2218                     count++; // touch here once then break;
2219                     break;
2220                 }
2221                 else
2222                 {
2223                 }
2224             }
2225             while (d);
2226             return failcount == 0 && count == 1;
2227         }
2228 
InDoWhileStatement1()2229         private static bool InDoWhileStatement1()
2230         {
2231             int failcount = 0;
2232             C origin = new C(1);
2233             dynamic d = origin;
2234             int count = 0;
2235             do
2236             {
2237                 if (count == 0)
2238                 {
2239                     count++;
2240                 }
2241                 else
2242                 {
2243                     failcount++;
2244                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2245                     break;
2246                 }
2247             }
2248             while ((bool)d);
2249             return failcount == 0;
2250         }
2251 
InForStatement0()2252         private static bool InForStatement0()
2253         {
2254             int failcount = 0;
2255             int count = 0;
2256             C origin = new C(0);
2257             dynamic d = origin;
2258             for (; (bool)d;)
2259             {
2260                 if (count == 0)
2261                 {
2262                     count++; // touch here once then break;
2263                     break;
2264                 }
2265                 else
2266                 {
2267                     failcount++;
2268                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2269                     break;
2270                 }
2271             }
2272 
2273             return failcount == 0 && count == 1;
2274         }
2275 
InForStatement1()2276         private static bool InForStatement1()
2277         {
2278             int failcount = 0;
2279             C origin = new C(1);
2280             dynamic d = origin;
2281             for (; (bool)d;)
2282             {
2283                 failcount++;
2284                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2285                 break;
2286             }
2287 
2288             return failcount == 0;
2289         }
2290 
InConditionalOperator0()2291         private static bool InConditionalOperator0()
2292         {
2293             int failcount = 0;
2294             C origin = new C(0);
2295             dynamic d = origin;
2296             if (!((bool)d ? true : false))
2297             {
2298                 failcount++;
2299                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2300             }
2301 
2302             return failcount == 0;
2303         }
2304 
InConditionalOperator1()2305         private static bool InConditionalOperator1()
2306         {
2307             int failcount = 0;
2308             C origin = new C(1);
2309             dynamic d = origin;
2310             if ((bool)d ? true : false)
2311             {
2312                 failcount++;
2313                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2314             }
2315 
2316             return failcount == 0;
2317         }
2318 
InWhereClauseOfQueryExpression0()2319         private static bool InWhereClauseOfQueryExpression0()
2320         {
2321             int failcount = 0;
2322             var a = new[]
2323             {
2324             1, 2, 3
2325             }
2326 
2327             ;
2328             C origin = new C(0);
2329             dynamic d = origin;
2330             var q = (
2331                 from m in a
2332                 where (bool)d
2333                 select m).ToArray();
2334             if (q[1] != a[1])
2335             {
2336                 failcount++;
2337                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2338             }
2339 
2340             return failcount == 0;
2341         }
2342 
InWhereClauseOfQueryExpression1()2343         private static bool InWhereClauseOfQueryExpression1()
2344         {
2345             int failcount = 0;
2346             var a = new[]
2347             {
2348             1, 2, 3
2349             }
2350 
2351             ;
2352             C origin = new C(1);
2353             dynamic d = origin;
2354             var q = (
2355                 from m in a
2356                 where (bool)d
2357                 select m).ToArray();
2358             if (q.Length != 0)
2359             {
2360                 failcount++;
2361                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2362             }
2363 
2364             return failcount == 0;
2365         }
2366 
2367         [Fact]
DynamicCSharpRunTest()2368         public static void DynamicCSharpRunTest()
2369         {
2370             Assert.Equal(0, MainMethod());
2371         }
2372 
MainMethod()2373         public static int MainMethod()
2374         {
2375             int result = 0;
2376             result += Verify.Eval(InIfStatement0);
2377             result += Verify.Eval(InWhileStatement0);
2378             result += Verify.Eval(InDoWhileStatement0);
2379             result += Verify.Eval(InForStatement0);
2380             result += Verify.Eval(InConditionalOperator0);
2381             result += Verify.Eval(InWhereClauseOfQueryExpression0);
2382             result += Verify.Eval(InIfStatement1);
2383             result += Verify.Eval(InWhileStatement1);
2384             result += Verify.Eval(InDoWhileStatement1);
2385             result += Verify.Eval(InForStatement1);
2386             result += Verify.Eval(InConditionalOperator1);
2387             result += Verify.Eval(InWhereClauseOfQueryExpression1);
2388             return result;
2389         }
2390     }
2391     // </Code>
2392 }
2393 
2394 
2395 
2396 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context002b.context002b
2397 {
2398     // <Area> Dynamic -- implicit conversion</Area>
2399     // <Title> The conversion occurs in boolean expression </Title>
2400     // <Description>
2401     // </Description>
2402     // <RelatedBugs></RelatedBugs>
2403     // <Expects Status=success></Expects>
2404     // <Code>
2405     using System.Linq;
2406 
2407     public class ConversionInBooleanExpression
2408     {
2409         public class C
2410         {
C()2411             public C()
2412             {
2413             }
2414 
C(int f)2415             public C(int f)
2416             {
2417                 Field = f;
2418             }
2419 
2420             public int Field;
operator bool(C p1)2421             public static implicit operator bool (C p1)
2422             {
2423                 if (p1.Field == 0)
2424                     return true;
2425                 else
2426                     return false;
2427             }
2428         }
2429 
InIfStatement0()2430         private static bool InIfStatement0()
2431         {
2432             int failcount = 0;
2433             C origin = new C(0);
2434             dynamic d = origin;
2435             if (!d)
2436             {
2437                 failcount++;
2438                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2439             }
2440 
2441             return failcount == 0;
2442         }
2443 
InIfStatement1()2444         private static bool InIfStatement1()
2445         {
2446             int failcount = 0;
2447             C origin = new C(1);
2448             dynamic d = origin;
2449             if (d)
2450             {
2451                 failcount++;
2452                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2453             }
2454 
2455             return failcount == 0;
2456         }
2457 
InWhileStatement0()2458         private static bool InWhileStatement0()
2459         {
2460             int failcount = 0;
2461             C origin = new C(0);
2462             dynamic d = origin;
2463             while (!d)
2464             {
2465                 failcount++;
2466                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2467                 break;
2468             }
2469 
2470             return failcount == 0;
2471         }
2472 
InWhileStatement1()2473         private static bool InWhileStatement1()
2474         {
2475             int failcount = 0;
2476             C origin = new C(1);
2477             dynamic d = origin;
2478             while (d)
2479             {
2480                 failcount++;
2481                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2482                 break;
2483             }
2484 
2485             return failcount == 0;
2486         }
2487 
InDoWhileStatement0()2488         private static bool InDoWhileStatement0()
2489         {
2490             int failcount = 0;
2491             C origin = new C(0);
2492             dynamic d = origin;
2493             int count = 0;
2494             do
2495             {
2496                 if (count == 0)
2497                 {
2498                     count++;
2499                 }
2500                 else
2501                 {
2502                     failcount++;
2503                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2504                     break;
2505                 }
2506             }
2507             while (!d);
2508             return failcount == 0;
2509         }
2510 
InDoWhileStatement1()2511         private static bool InDoWhileStatement1()
2512         {
2513             int failcount = 0;
2514             C origin = new C(1);
2515             dynamic d = origin;
2516             int count = 0;
2517             do
2518             {
2519                 if (count == 0)
2520                 {
2521                     count++;
2522                 }
2523                 else
2524                 {
2525                     failcount++;
2526                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2527                     break;
2528                 }
2529             }
2530             while (d);
2531             return failcount == 0;
2532         }
2533 
InForStatement0()2534         private static bool InForStatement0()
2535         {
2536             int failcount = 0;
2537             C origin = new C(0);
2538             dynamic d = origin;
2539             for (; !d;)
2540             {
2541                 failcount++;
2542                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2543                 break;
2544             }
2545 
2546             return failcount == 0;
2547         }
2548 
InForStatement1()2549         private static bool InForStatement1()
2550         {
2551             int failcount = 0;
2552             C origin = new C(1);
2553             dynamic d = origin;
2554             for (; d;)
2555             {
2556                 failcount++;
2557                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2558                 break;
2559             }
2560 
2561             return failcount == 0;
2562         }
2563 
InConditionalOperator0()2564         private static bool InConditionalOperator0()
2565         {
2566             int failcount = 0;
2567             C origin = new C(0);
2568             dynamic d = origin;
2569             if (!(d ? true : false))
2570             {
2571                 failcount++;
2572                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2573             }
2574 
2575             return failcount == 0;
2576         }
2577 
InConditionalOperator1()2578         private static bool InConditionalOperator1()
2579         {
2580             int failcount = 0;
2581             C origin = new C(1);
2582             dynamic d = origin;
2583             if (d ? true : false)
2584             {
2585                 failcount++;
2586                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2587             }
2588 
2589             return failcount == 0;
2590         }
2591 
InWhereClauseOfQueryExpression0()2592         private static bool InWhereClauseOfQueryExpression0()
2593         {
2594             int failcount = 0;
2595             var a = new[]
2596             {
2597             1, 2, 3
2598             }
2599 
2600             ;
2601             C origin = new C(0);
2602             dynamic d = origin;
2603             var q = (
2604                 from m in a
2605                 where d
2606                 select m).ToArray();
2607             if (q[1] != a[1])
2608             {
2609                 failcount++;
2610                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2611             }
2612 
2613             return failcount == 0;
2614         }
2615 
InWhereClauseOfQueryExpression1()2616         private static bool InWhereClauseOfQueryExpression1()
2617         {
2618             int failcount = 0;
2619             var a = new[]
2620             {
2621             1, 2, 3
2622             }
2623 
2624             ;
2625             C origin = new C(1);
2626             dynamic d = origin;
2627             var q = (
2628                 from m in a
2629                 where !d
2630                 select m).ToArray();
2631             if (q[1] != a[1])
2632             {
2633                 failcount++;
2634                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2635             }
2636 
2637             return failcount == 0;
2638         }
2639 
2640         [Fact]
DynamicCSharpRunTest()2641         public static void DynamicCSharpRunTest()
2642         {
2643             Assert.Equal(0, MainMethod());
2644         }
2645 
MainMethod()2646         public static int MainMethod()
2647         {
2648             int result = 0;
2649             result += Verify.Eval(InIfStatement0);
2650             result += Verify.Eval(InWhileStatement0);
2651             result += Verify.Eval(InDoWhileStatement0);
2652             result += Verify.Eval(InForStatement0);
2653             result += Verify.Eval(InConditionalOperator0);
2654             result += Verify.Eval(InWhereClauseOfQueryExpression0);
2655             result += Verify.Eval(InIfStatement1);
2656             result += Verify.Eval(InWhileStatement1);
2657             result += Verify.Eval(InDoWhileStatement1);
2658             result += Verify.Eval(InForStatement1);
2659             result += Verify.Eval(InConditionalOperator1);
2660             result += Verify.Eval(InWhereClauseOfQueryExpression1);
2661             return result;
2662         }
2663     }
2664     // </Code>
2665 }
2666 
2667 
2668 
2669 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context002c.context002c
2670 {
2671     // <Area> Dynamic -- implicit conversion</Area>
2672     // <Title> The conversion occurs in boolean expression </Title>
2673     // <Description>
2674     // </Description>
2675     // <RelatedBugs></RelatedBugs>
2676     // <Expects Status=success></Expects>
2677     // <Code>
2678 
2679     public class ConversionInBooleanExpression
2680     {
2681         public class C
2682         {
operator true(C c)2683             public static bool operator true(C c)
2684             {
2685                 return true;
2686             }
2687 
operator false(C c)2688             public static bool operator false(C c)
2689             {
2690                 return false;
2691             }
2692         }
2693 
2694         public class C1
2695         {
operator true(C1 c)2696             public static bool operator true(C1 c)
2697             {
2698                 return false;
2699             }
2700 
operator false(C1 c)2701             public static bool operator false(C1 c)
2702             {
2703                 return true;
2704             }
2705         }
2706 
InIfStatement0()2707         private static bool InIfStatement0()
2708         {
2709             int failcount = 0;
2710             C origin = new C();
2711             dynamic d = origin;
2712             if (d)
2713             {
2714             }
2715             else
2716             {
2717                 failcount++;
2718                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2719             }
2720 
2721             return failcount == 0;
2722         }
2723 
InIfStatement1()2724         private static bool InIfStatement1()
2725         {
2726             int failcount = 0;
2727             C1 origin = new C1();
2728             dynamic d = origin;
2729             if (d)
2730             {
2731                 failcount++;
2732                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2733             }
2734 
2735             return failcount == 0;
2736         }
2737 
InWhileStatement0()2738         private static bool InWhileStatement0()
2739         {
2740             int failcount = 0;
2741             C origin = new C();
2742             dynamic d = origin;
2743             while (d)
2744             {
2745                 failcount++; //touch here once.
2746                 break;
2747             }
2748 
2749             return failcount == 1;
2750         }
2751 
InWhileStatement1()2752         private static bool InWhileStatement1()
2753         {
2754             int failcount = 0;
2755             C1 origin = new C1();
2756             dynamic d = origin;
2757             while (d)
2758             {
2759                 failcount++;
2760                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2761                 break;
2762             }
2763 
2764             return failcount == 0;
2765         }
2766 
InDoWhileStatement0()2767         private static bool InDoWhileStatement0()
2768         {
2769             int failcount = 0;
2770             C origin = new C();
2771             dynamic d = origin;
2772             int count = 0;
2773             do
2774             {
2775                 if (count == 0)
2776                 {
2777                     count++; // touch here once then break;
2778                     break;
2779                 }
2780                 else
2781                 {
2782                 }
2783             }
2784             while (d);
2785             return failcount == 0 && count == 1;
2786         }
2787 
InDoWhileStatement1()2788         private static bool InDoWhileStatement1()
2789         {
2790             int failcount = 0;
2791             C1 origin = new C1();
2792             dynamic d = origin;
2793             int count = 0;
2794             do
2795             {
2796                 if (count == 0)
2797                 {
2798                     count++;
2799                 }
2800                 else
2801                 {
2802                     failcount++;
2803                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2804                     break;
2805                 }
2806             }
2807             while (d);
2808             return failcount == 0;
2809         }
2810 
InForStatement0()2811         private static bool InForStatement0()
2812         {
2813             int failcount = 0;
2814             int count = 0;
2815             C origin = new C();
2816             dynamic d = origin;
2817             for (; d;)
2818             {
2819                 if (count == 0)
2820                 {
2821                     count++; // touch here once then break;
2822                     break;
2823                 }
2824                 else
2825                 {
2826                     failcount++;
2827                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
2828                     break;
2829                 }
2830             }
2831 
2832             return failcount == 0 && count == 1;
2833         }
2834 
InForStatement1()2835         private static bool InForStatement1()
2836         {
2837             int failcount = 0;
2838             C1 origin = new C1();
2839             dynamic d = origin;
2840             for (; d;)
2841             {
2842                 failcount++;
2843                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2844                 break;
2845             }
2846 
2847             return failcount == 0;
2848         }
2849 
InConditionalOperator0()2850         private static bool InConditionalOperator0()
2851         {
2852             int failcount = 0;
2853             C origin = new C();
2854             dynamic d = origin;
2855             if (!(d ? true : false))
2856             {
2857                 failcount++;
2858                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2859             }
2860 
2861             return failcount == 0;
2862         }
2863 
InConditionalOperator1()2864         private static bool InConditionalOperator1()
2865         {
2866             int failcount = 0;
2867             C1 origin = new C1();
2868             dynamic d = origin;
2869             if (d ? true : false)
2870             {
2871                 failcount++;
2872                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2873             }
2874 
2875             return failcount == 0;
2876         }
2877 
2878         [Fact]
DynamicCSharpRunTest()2879         public static void DynamicCSharpRunTest()
2880         {
2881             Assert.Equal(0, MainMethod());
2882         }
2883 
MainMethod()2884         public static int MainMethod()
2885         {
2886             int result = 0;
2887             result += Verify.Eval(InIfStatement0);
2888             result += Verify.Eval(InWhileStatement0);
2889             result += Verify.Eval(InDoWhileStatement0);
2890             result += Verify.Eval(InForStatement0);
2891             result += Verify.Eval(InConditionalOperator0);
2892             result += Verify.Eval(InIfStatement1);
2893             result += Verify.Eval(InWhileStatement1);
2894             result += Verify.Eval(InDoWhileStatement1);
2895             result += Verify.Eval(InForStatement1);
2896             result += Verify.Eval(InConditionalOperator1);
2897             return result;
2898         }
2899     }
2900     // </Code>
2901 }
2902 
2903 
2904 
2905 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context003.context003
2906 {
2907     // <Area> Dynamic -- implicit conversion</Area>
2908     // <Title> The conversion occurs in return statement </Title>
2909     // <Description>
2910     // </Description>
2911     // <RelatedBugs></RelatedBugs>
2912     // <Expects Status=success></Expects>
2913     // <Code>
2914 
2915     public class ConversionInReturn
2916     {
TestingMethod(byte origin)2917         public static ulong TestingMethod(byte origin)
2918         {
2919             dynamic d = origin;
2920             return d;
2921         }
2922 
InMethod()2923         private static bool InMethod()
2924         {
2925             int failcount = 0;
2926             if (TestingMethod(25) != 25)
2927             {
2928                 failcount++;
2929                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2930             }
2931 
2932             return failcount == 0;
2933         }
2934 
2935         private static byte s_origin_TestingProperty;
2936         public static int TestingProperty
2937         {
2938             get
2939             {
2940                 dynamic d = s_origin_TestingProperty;
2941                 return d;
2942             }
2943 
2944             set
2945             {
2946                 s_origin_TestingProperty = (byte)value;
2947             }
2948         }
2949 
InPropertyGet()2950         private static bool InPropertyGet()
2951         {
2952             int failcount = 0;
2953             TestingProperty = 33;
2954             if (TestingProperty != 33)
2955             {
2956                 failcount++;
2957                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2958             }
2959 
2960             return failcount == 0;
2961         }
2962 
2963         public int this[byte origin]
2964         {
2965             get
2966             {
2967                 dynamic d = origin;
2968                 return d;
2969             }
2970         }
2971 
InIndexerGet()2972         private static bool InIndexerGet()
2973         {
2974             int failcount = 0;
2975             var t = new ConversionInReturn();
2976             if (t[33] != 33)
2977             {
2978                 failcount++;
2979                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2980             }
2981 
2982             return failcount == 0;
2983         }
2984 
operator +(ConversionInReturn c, sbyte origin)2985         public static long operator +(ConversionInReturn c, sbyte origin)
2986         {
2987             dynamic d = origin;
2988             return d;
2989         }
2990 
InOperator()2991         private static bool InOperator()
2992         {
2993             int failcount = 0;
2994             var t = new ConversionInReturn();
2995             if ((t + 44) != 44)
2996             {
2997                 failcount++;
2998                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
2999             }
3000 
3001             return failcount == 0;
3002         }
3003 
3004         [Fact]
DynamicCSharpRunTest()3005         public static void DynamicCSharpRunTest()
3006         {
3007             Assert.Equal(0, MainMethod());
3008         }
3009 
MainMethod()3010         public static int MainMethod()
3011         {
3012             int result = 0;
3013             result += Verify.Eval(InMethod);
3014             result += Verify.Eval(InPropertyGet);
3015             result += Verify.Eval(InIndexerGet);
3016             result += Verify.Eval(InOperator);
3017             return result;
3018         }
3019     }
3020     // </Code>
3021 }
3022 
3023 
3024 
3025 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context004.context004
3026 {
3027     // <Area> Dynamic -- implicit conversion</Area>
3028     // <Title> The conversion occurs in yield return statement </Title>
3029     // <Description>
3030     // </Description>
3031     // <RelatedBugs></RelatedBugs>
3032     // <Expects Status=success></Expects>
3033     // <Code>
3034     using System.Collections;
3035     using System.Collections.Generic;
3036 
3037     public class NCTestingReturnIEnumeratorT : IEnumerable<long>
3038     {
NCTestingReturnIEnumeratorT(byte origin)3039         public NCTestingReturnIEnumeratorT(byte origin)
3040         {
3041             _origin = origin;
3042         }
3043 
3044         private byte _origin;
GetEnumerator()3045         IEnumerator<long> IEnumerable<long>.GetEnumerator()
3046         {
3047             dynamic d = _origin;
3048             yield return d;
3049         }
3050 
IEnumerable.GetEnumerator()3051         IEnumerator IEnumerable.GetEnumerator()
3052         {
3053             yield return 1;
3054         }
3055     }
3056 
3057     public class NCTestingReturnIEnumerator : IEnumerable
3058     {
NCTestingReturnIEnumerator(byte origin)3059         public NCTestingReturnIEnumerator(byte origin)
3060         {
3061             _origin = origin;
3062         }
3063 
3064         private byte _origin;
GetEnumerator()3065         public IEnumerator GetEnumerator()
3066         {
3067             dynamic d = _origin;
3068             yield return d;
3069         }
3070     }
3071 
3072     public class ConversionInYieldReturn
3073     {
TestingReturnIEnumerable(byte origin)3074         public static IEnumerable TestingReturnIEnumerable(byte origin)
3075         {
3076             dynamic d = origin;
3077             yield return d;
3078         }
3079 
IteratorReturnIEnumerable()3080         private static bool IteratorReturnIEnumerable()
3081         {
3082             int failcount = 0;
3083             // no callsite
3084             //
3085             bool isPass = false;
3086             foreach (var v in TestingReturnIEnumerable(25))
3087             {
3088                 if ((byte)v == 25)
3089                 {
3090                     isPass = true;
3091                 }
3092 
3093                 break;
3094             }
3095 
3096             if (!isPass)
3097             {
3098                 failcount++;
3099                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3100             }
3101 
3102             return failcount == 0;
3103         }
3104 
TestingReturnIEnumerableT(byte origin)3105         public static IEnumerable<long> TestingReturnIEnumerableT(byte origin)
3106         {
3107             dynamic d = origin;
3108             yield return d;
3109         }
3110 
IteratorReturnIEnumerableT()3111         private static bool IteratorReturnIEnumerableT()
3112         {
3113             int failcount = 0;
3114             bool isPass = false;
3115             foreach (var v in TestingReturnIEnumerableT(33))
3116             {
3117                 if (v == 33)
3118                 {
3119                     isPass = true;
3120                 }
3121 
3122                 break;
3123             }
3124 
3125             if (!isPass)
3126             {
3127                 failcount++;
3128                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3129             }
3130 
3131             return failcount == 0;
3132         }
3133 
IteratorReturnIEnumerator()3134         private static bool IteratorReturnIEnumerator()
3135         {
3136             int failcount = 0;
3137             // no callsite
3138             //
3139             bool isPass = false;
3140             foreach (var v in new NCTestingReturnIEnumerator(25))
3141             {
3142                 if ((byte)v == 25)
3143                 {
3144                     isPass = true;
3145                 }
3146 
3147                 break;
3148             }
3149 
3150             if (!isPass)
3151             {
3152                 failcount++;
3153                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3154             }
3155 
3156             return failcount == 0;
3157         }
3158 
IteratorReturnIEnumeratorT()3159         private static bool IteratorReturnIEnumeratorT()
3160         {
3161             int failcount = 0;
3162             bool isPass = false;
3163             foreach (var v in new NCTestingReturnIEnumeratorT(33))
3164             {
3165                 if (v == 33)
3166                 {
3167                     isPass = true;
3168                 }
3169 
3170                 break;
3171             }
3172 
3173             if (!isPass)
3174             {
3175                 failcount++;
3176                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3177             }
3178 
3179             return failcount == 0;
3180         }
3181 
3182         [Fact]
DynamicCSharpRunTest()3183         public static void DynamicCSharpRunTest()
3184         {
3185             Assert.Equal(0, MainMethod());
3186         }
3187 
MainMethod()3188         public static int MainMethod()
3189         {
3190             int result = 0;
3191             result += Verify.Eval(IteratorReturnIEnumerable);
3192             result += Verify.Eval(IteratorReturnIEnumerableT);
3193             result += Verify.Eval(IteratorReturnIEnumerator);
3194             result += Verify.Eval(IteratorReturnIEnumeratorT);
3195             return result;
3196         }
3197     }
3198     // </Code>
3199 }
3200 
3201 
3202 
3203 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context005.context005
3204 {
3205     // <Area> Dynamic -- implicit conversion</Area>
3206     // <Title> The conversion occurs in evaluating the type of null coalescing operator </Title>
3207     // <Description>
3208     // Any type is implicitly convertible to dynamic
3209     // and dynamic is implicitly convertible to any type.
3210     // but previous should be high priority.
3211     // </Description>
3212     // <RelatedBugs></RelatedBugs>
3213     // <Expects Status=success></Expects>
3214     // <Code>
3215 
3216     public class ConversionInNullCoalescing
3217     {
DynamicObjectInSecondOperandAndFirstIsNull()3218         private static bool DynamicObjectInSecondOperandAndFirstIsNull()
3219         {
3220             int failcount = 0;
3221             byte origin = 24;
3222             dynamic d = origin;
3223             long? first = null;
3224             if ((long)(first ?? d) != 24)
3225             {
3226                 failcount++;
3227                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3228             }
3229 
3230             return failcount == 0;
3231         }
3232 
DynamicObjectInSecondOperand()3233         private static bool DynamicObjectInSecondOperand()
3234         {
3235             int failcount = 0;
3236             byte origin = 24;
3237             dynamic d = origin;
3238             long? first = 33;
3239             if ((long)(first ?? d) != 33)
3240             {
3241                 failcount++;
3242                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3243             }
3244 
3245             return failcount == 0;
3246         }
3247 
DynamicObjectInFirstOperand()3248         private static bool DynamicObjectInFirstOperand()
3249         {
3250             int failcount = 0;
3251             byte origin = 24;
3252             dynamic d = origin;
3253             long? second = 2;
3254             if ((long)(d ?? second) != 24)
3255             {
3256                 failcount++;
3257                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3258             }
3259 
3260             return failcount == 0;
3261         }
3262 
DynamicObjectInFirstOperandAndFirstIsNull()3263         private static bool DynamicObjectInFirstOperandAndFirstIsNull()
3264         {
3265             int failcount = 0;
3266             dynamic d = null;
3267             long? second = 2;
3268             if ((long)(d ?? second) != 2)
3269             {
3270                 failcount++;
3271                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3272             }
3273 
3274             return failcount == 0;
3275         }
3276 
3277         [Fact]
DynamicCSharpRunTest()3278         public static void DynamicCSharpRunTest()
3279         {
3280             Assert.Equal(0, MainMethod());
3281         }
3282 
MainMethod()3283         public static int MainMethod()
3284         {
3285             int result = 0;
3286             result += Verify.Eval(DynamicObjectInSecondOperandAndFirstIsNull);
3287             result += Verify.Eval(DynamicObjectInSecondOperand);
3288             result += Verify.Eval(DynamicObjectInFirstOperand);
3289             result += Verify.Eval(DynamicObjectInFirstOperandAndFirstIsNull);
3290             return result;
3291         }
3292     }
3293     // </Code>
3294 }
3295 
3296 
3297 
3298 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context006.context006
3299 {
3300     // <Area> Dynamic -- implicit conversion</Area>
3301     // <Title> The conversion occurs in evaluating the type of conditional operator </Title>
3302     // <Description>
3303     // Any type is implicitly convertible to dynamic
3304     // and dynamic is implicitly convertible to any type.
3305     // but previous should be high priority.
3306     // </Description>
3307     // <RelatedBugs></RelatedBugs>
3308     // <Expects Status=success></Expects>
3309     // <Code>
3310 
3311     public class ConversionInConditionalOperator
3312     {
DynamicObjectInSecondAndThirdOperandAndConditionIsTrue()3313         private static bool DynamicObjectInSecondAndThirdOperandAndConditionIsTrue()
3314         {
3315             int failcount = 0;
3316             byte origin = 24;
3317             dynamic d1 = origin;
3318             dynamic d2 = 33;
3319             bool cond = true;
3320             if ((int)(cond ? d1 : d2) != 24)
3321             {
3322                 failcount++;
3323                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3324             }
3325 
3326             return failcount == 0;
3327         }
3328 
DynamicObjectInSecondAndThirdOperandAndConditionIsFalse()3329         private static bool DynamicObjectInSecondAndThirdOperandAndConditionIsFalse()
3330         {
3331             int failcount = 0;
3332             byte origin = 24;
3333             dynamic d1 = origin;
3334             dynamic d2 = 33;
3335             bool cond = false;
3336             if ((int)(cond ? d1 : d2) != 33)
3337             {
3338                 failcount++;
3339                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3340             }
3341 
3342             return failcount == 0;
3343         }
3344 
DynamicObjectInSecondOperandAndConditionIsTrue()3345         private static bool DynamicObjectInSecondOperandAndConditionIsTrue()
3346         {
3347             int failcount = 0;
3348             byte origin = 24;
3349             dynamic d = origin;
3350             long third = 33;
3351             bool cond = true;
3352             if ((long)(cond ? d : third) != 24)
3353             {
3354                 failcount++;
3355                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3356             }
3357 
3358             return failcount == 0;
3359         }
3360 
DynamicObjectInThirdOperandAndConditionIsFalse()3361         private static bool DynamicObjectInThirdOperandAndConditionIsFalse()
3362         {
3363             int failcount = 0;
3364             byte origin = 24;
3365             dynamic d = origin;
3366             ulong second = 33;
3367             bool cond = false;
3368             if ((ulong)(cond ? second : d) != 24)
3369             {
3370                 failcount++;
3371                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3372             }
3373 
3374             return failcount == 0;
3375         }
3376 
DynamicObjectInSecondOperandAndThirdIsNullableAndConditionIsTrue()3377         private static bool DynamicObjectInSecondOperandAndThirdIsNullableAndConditionIsTrue()
3378         {
3379             int failcount = 0;
3380             byte origin = 24;
3381             dynamic d = origin;
3382             long? third = 33;
3383             bool cond = true;
3384             if ((long)(cond ? d : third) != 24)
3385             {
3386                 failcount++;
3387                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3388             }
3389 
3390             return failcount == 0;
3391         }
3392 
DynamicObjectInThirdOperandAndSecondIsNullableAndConditionIsFalse()3393         private static bool DynamicObjectInThirdOperandAndSecondIsNullableAndConditionIsFalse()
3394         {
3395             int failcount = 0;
3396             byte origin = 24;
3397             dynamic d = origin;
3398             ulong? second = 33;
3399             bool cond = false;
3400             if ((ulong)(cond ? second : d) != 24)
3401             {
3402                 failcount++;
3403                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3404             }
3405 
3406             return failcount == 0;
3407         }
3408 
3409         [Fact]
DynamicCSharpRunTest()3410         public static void DynamicCSharpRunTest()
3411         {
3412             Assert.Equal(0, MainMethod());
3413         }
3414 
MainMethod()3415         public static int MainMethod()
3416         {
3417             int result = 0;
3418             result += Verify.Eval(DynamicObjectInSecondAndThirdOperandAndConditionIsTrue);
3419             result += Verify.Eval(DynamicObjectInSecondAndThirdOperandAndConditionIsFalse);
3420             result += Verify.Eval(DynamicObjectInSecondOperandAndConditionIsTrue);
3421             result += Verify.Eval(DynamicObjectInThirdOperandAndConditionIsFalse);
3422             result += Verify.Eval(DynamicObjectInSecondOperandAndThirdIsNullableAndConditionIsTrue);
3423             result += Verify.Eval(DynamicObjectInThirdOperandAndSecondIsNullableAndConditionIsFalse);
3424             return result;
3425         }
3426     }
3427     // </Code>
3428 }
3429 
3430 
3431 
3432 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context007.context007
3433 {
3434     // <Area> Dynamic -- implicit conversion</Area>
3435     // <Title> The conversion occurs in array creation expressions </Title>
3436     // <Description>
3437     // </Description>
3438     // <RelatedBugs></RelatedBugs>
3439     // <Expects Status=success></Expects>
3440     // <Code>
3441 
3442     public class ConversionInArrayCreation
3443     {
DynamicObjectInDimensionExpressionList()3444         private static bool DynamicObjectInDimensionExpressionList()
3445         {
3446             int failcount = 0;
3447             byte origin = 24;
3448             dynamic d = origin;
3449             var a = new int[(int)d];
3450             a[0] = 10;
3451             if ((a[0] != 10) || (a.Length != 24))
3452             {
3453                 failcount++;
3454                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3455             }
3456 
3457             return failcount == 0;
3458         }
3459 
DynamicObjectInArrayInitializer()3460         private static bool DynamicObjectInArrayInitializer()
3461         {
3462             int failcount = 0;
3463             byte origin = 24;
3464             dynamic d = origin;
3465             dynamic d2 = 33;
3466             var a = new int[]
3467             {
3468             d, d2
3469             }
3470 
3471             ;
3472             if ((a[0] != 24) || (a[1] != 33))
3473             {
3474                 failcount++;
3475                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3476             }
3477 
3478             return failcount == 0;
3479         }
3480 
3481         [Fact]
DynamicCSharpRunTest()3482         public static void DynamicCSharpRunTest()
3483         {
3484             Assert.Equal(0, MainMethod());
3485         }
3486 
MainMethod()3487         public static int MainMethod()
3488         {
3489             int result = 0;
3490             result += Verify.Eval(DynamicObjectInDimensionExpressionList);
3491             result += Verify.Eval(DynamicObjectInArrayInitializer);
3492             return result;
3493         }
3494     }
3495     // </Code>
3496 }
3497 
3498 
3499 
3500 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context008b.context008b
3501 {
3502     // <Area> Dynamic -- implicit conversion</Area>
3503     // <Title> The conversion occurs in using statement </Title>
3504     // <Description>
3505     // </Description>
3506     // <RelatedBugs></RelatedBugs>
3507     // <Expects Status=success></Expects>
3508     // <Code>
3509     using System;
3510 
3511     public class C : IDisposable
3512     {
3513         public static bool CalledDisposeFlag = false;
Dispose()3514         public void Dispose()
3515         {
3516             CalledDisposeFlag = true;
3517         }
3518 
M(int i)3519         public int M(int i)
3520         {
3521             return i;
3522         }
3523     }
3524 
3525     public class ConversionInUsingStatement
3526     {
DynamicLocalVariableDefinitionInUsing()3527         private static bool DynamicLocalVariableDefinitionInUsing()
3528         {
3529             int failcount = 0;
3530             C.CalledDisposeFlag = false;
3531             using (dynamic d = new C())
3532             {
3533                 if (((int)d.M(10)) != 10)
3534                 {
3535                     failcount++;
3536                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
3537                 }
3538             }
3539 
3540             if (!C.CalledDisposeFlag)
3541             {
3542                 failcount++;
3543                 System.Console.WriteLine("Test failed: Didn't called the Dispose");
3544             }
3545 
3546             return failcount == 0;
3547         }
3548 
3549         [Fact]
DynamicCSharpRunTest()3550         public static void DynamicCSharpRunTest()
3551         {
3552             Assert.Equal(0, MainMethod());
3553         }
3554 
MainMethod()3555         public static int MainMethod()
3556         {
3557             int result = 0;
3558             result += Verify.Eval(DynamicLocalVariableDefinitionInUsing);
3559             return result;
3560         }
3561     }
3562     // </Code>
3563 }
3564 
3565 
3566 
3567 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context010.context010
3568 {
3569     // <Area> Dynamic -- implicit conversion</Area>
3570     // <Title> The conversion occurs in boolean expression </Title>
3571     // <Description>
3572     // The runtime type doesn't convertible to bool
3573     // </Description>
3574     // <RelatedBugs></RelatedBugs>
3575     // <Expects Status=success></Expects>
3576     // <Code>
3577 
3578     public class C1
3579     {
operator true(C1 c)3580         public static bool operator true(C1 c)
3581         {
3582             return true;
3583         }
3584 
operator false(C1 c)3585         public static bool operator false(C1 c)
3586         {
3587             return false;
3588         }
3589     }
3590 
3591     public class C2
3592     {
3593     }
3594 
3595     public class ConversionInUsingStatement
3596     {
RuntimeTypeNotConvertibleToBoolButWithTrueOperator()3597         private static bool RuntimeTypeNotConvertibleToBoolButWithTrueOperator()
3598         {
3599             int failcount = 1;
3600             dynamic d = new C1();
3601             if (d)
3602             {
3603                 failcount--;
3604             }
3605             else
3606             {
3607                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3608             }
3609 
3610             return failcount == 0;
3611         }
3612 
RuntimeTypeNotConvertibleToBool()3613         private static bool RuntimeTypeNotConvertibleToBool()
3614         {
3615             int failcount = 0;
3616             dynamic d = new C2();
3617             try
3618             {
3619                 if (d)
3620                 {
3621                     failcount++;
3622                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
3623                 }
3624             }
3625             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
3626             {
3627                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "C2", "bool"))
3628                     failcount++;
3629             }
3630 
3631             return failcount == 0;
3632         }
3633 
3634         [Fact]
DynamicCSharpRunTest()3635         public static void DynamicCSharpRunTest()
3636         {
3637             Assert.Equal(0, MainMethod());
3638         }
3639 
MainMethod()3640         public static int MainMethod()
3641         {
3642             int result = 0;
3643             result += Verify.Eval(RuntimeTypeNotConvertibleToBool);
3644             result += Verify.Eval(RuntimeTypeNotConvertibleToBoolButWithTrueOperator);
3645             return result;
3646         }
3647     }
3648     // </Code>
3649 }
3650 
3651 
3652 
3653 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context011.context011
3654 {
3655     // <Area> Dynamic -- implicit conversion</Area>
3656     // <Title> The conversion occurs in using statement </Title>
3657     // <Description>
3658     // </Description>
3659     // <RelatedBugs></RelatedBugs>
3660     // <Expects Status=success></Expects>
3661     // <Code>
3662     using System;
3663 
3664     public class C1
3665     {
3666     }
3667 
3668     public class C : IDisposable
3669     {
3670         public static bool CalledDisposeFlag = false;
IDisposable.Dispose()3671         void IDisposable.Dispose()
3672         {
3673             CalledDisposeFlag = true;
3674         }
3675 
M(int i)3676         public int M(int i)
3677         {
3678             return i;
3679         }
3680     }
3681 
3682     public class ConversionInUsingStatement
3683     {
DynamicObjectDoesntImplementIDisposable()3684         private static bool DynamicObjectDoesntImplementIDisposable()
3685         {
3686             int failcount = 0;
3687             dynamic d = new C1();
3688             try
3689             {
3690                 using (d)
3691                 {
3692                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
3693                 }
3694 
3695                 failcount++;
3696             }
3697             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
3698             {
3699                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConvCast, e.Message, "C1", "System.IDisposable"))
3700                     failcount++;
3701             }
3702 
3703             return failcount == 0;
3704         }
3705 
3706         [Fact]
DynamicCSharpRunTest()3707         public static void DynamicCSharpRunTest()
3708         {
3709             Assert.Equal(0, MainMethod());
3710         }
3711 
MainMethod()3712         public static int MainMethod()
3713         {
3714             int result = 0;
3715             result += Verify.Eval(DynamicObjectDoesntImplementIDisposable);
3716             return result;
3717         }
3718     }
3719     // </Code>
3720 }
3721 
3722 
3723 
3724 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.context012.context012
3725 {
3726     // <Area> Dynamic -- implicit conversion</Area>
3727     // <Title> The dynamic value is null when converting </Title>
3728     // <Description>
3729     // </Description>
3730     // <RelatedBugs></RelatedBugs>
3731     // <Expects Status=success></Expects>
3732     // <Code>
3733     using System;
3734 
3735     public class NullDynamicValue
3736     {
InUsing()3737         private static bool InUsing()
3738         {
3739             int failcount = 0;
3740             dynamic d = null;
3741             try
3742             {
3743                 using (IDisposable i = d)
3744                 {
3745                 }
3746             }
3747             catch (Exception e)
3748             {
3749                 failcount++;
3750                 System.Console.WriteLine("Test failed: Caught an unexpected exception {0}", e);
3751             }
3752 
3753             return failcount == 0;
3754         }
3755 
InAssignment()3756         private static bool InAssignment()
3757         {
3758             int failcount = 0;
3759             dynamic d = null;
3760             try
3761             {
3762                 int result = d;
3763                 failcount++;
3764                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3765             }
3766             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
3767             {
3768             }
3769             catch (Exception e)
3770             {
3771                 failcount++;
3772                 System.Console.WriteLine("Test failed: Caught an unexpected exception {0}", e);
3773             }
3774 
3775             return failcount == 0;
3776         }
3777 
InBooleanExpression()3778         private static bool InBooleanExpression()
3779         {
3780             int failcount = 0;
3781             dynamic d = null;
3782             try
3783             {
3784                 if (d)
3785                 {
3786                     failcount++;
3787                     System.Console.WriteLine("Test failed: Conversion result is incorrect");
3788                 }
3789             }
3790             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
3791             {
3792                 if (!ErrorVerifier.Verify(ErrorMessageId.ValueCantBeNull, e.Message, "bool"))
3793                     failcount++;
3794             }
3795             catch (Exception e)
3796             {
3797                 failcount++;
3798                 System.Console.WriteLine("Test failed: Caught an unexpected exception {0}", e);
3799             }
3800 
3801             return failcount == 0;
3802         }
3803 
TestingInReturn()3804         private static bool TestingInReturn()
3805         {
3806             dynamic d = null;
3807             return d;
3808         }
3809 
InReturn()3810         private static bool InReturn()
3811         {
3812             int failcount = 0;
3813             try
3814             {
3815                 TestingInReturn();
3816                 failcount++;
3817                 System.Console.WriteLine("Test failed: Conversion result is incorrect");
3818             }
3819             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
3820             {
3821                 if (!ErrorVerifier.Verify(ErrorMessageId.ValueCantBeNull, e.Message, "bool"))
3822                     failcount++;
3823             }
3824             catch (Exception e)
3825             {
3826                 failcount++;
3827                 System.Console.WriteLine("Test failed: Caught an unexpected exception {0}", e);
3828             }
3829 
3830             return failcount == 0;
3831         }
3832 
3833         [Fact]
DynamicCSharpRunTest()3834         public static void DynamicCSharpRunTest()
3835         {
3836             Assert.Equal(0, MainMethod());
3837         }
3838 
MainMethod()3839         public static int MainMethod()
3840         {
3841             int result = 0;
3842             result += Verify.Eval(InUsing);
3843             result += Verify.Eval(InAssignment);
3844             result += Verify.Eval(InBooleanExpression);
3845             result += Verify.Eval(InReturn);
3846             return result;
3847         }
3848     }
3849     // </Code>
3850 }
3851 
3852 
3853 
3854 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj001.dynamicobj001
3855 {
3856     // <Area> Dynamic -- identity conversion</Area>
3857     // <Title> The conversion between dynamic and object as parameter type (generic)</Title>
3858     // <Description> List<object> is not implicitly convertible to IEnumerable<dynamic> and vice-versa - error CS0266 </Description>
3859     // <RelatedBugs></RelatedBugs>
3860     // <Expects Status=success></Expects>
3861     // <Code>
3862     using System.Collections.Generic;
3863 
3864     public class Test
3865     {
3866         [Fact]
DynamicCSharpRunTest()3867         public static void DynamicCSharpRunTest()
3868         {
3869             Assert.Equal(0, MainMethod());
3870         }
3871 
MainMethod()3872         public static int MainMethod()
3873         {
3874             ICollection<dynamic> v1 = new List<object>();
3875             ICollection<object> v2 = new List<dynamic>();
3876             IEnumerable<dynamic> v3 = new List<object>();
3877             IEnumerable<object> v4 = new List<dynamic>();
3878             IDictionary<dynamic, int> v5 = new Dictionary<object, int>();
3879             IDictionary<object, int> v6 = new Dictionary<dynamic, int>();
3880             IList<dynamic> v7 = new List<object>();
3881             IList<object> v8 = new List<dynamic>();
3882             return 0;
3883         }
3884     }
3885     // </Code>
3886 }
3887 
3888 
3889 
3890 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj002.dynamicobj002
3891 {
3892     // <Area> Dynamic -- identity conversion</Area>
3893     // <Title> The conversion between dynamic and object as parameter type (generic)</Title>
3894     // <Description> List<object> is not implicitly convertible to IEnumerable<dynamic> and vice-versa - error CS0266 </Description>
3895     // <RelatedBugs></RelatedBugs>
3896     // <Expects Status=success></Expects>
3897     // <Code>
3898     using System.Collections.Generic;
3899 
3900     public class Test
3901     {
3902         private static IEnumerable<object> s_v4 = null;
3903         private ICollection<object> _v6;
3904         private IList<object> _v8 = null;
3905         [Fact]
DynamicCSharpRunTest()3906         public static void DynamicCSharpRunTest()
3907         {
3908             Assert.Equal(0, MainMethod());
3909         }
3910 
MainMethod()3911         public static int MainMethod()
3912         {
3913             IEnumerable<dynamic> v3;
3914             v3 = new List<object>();
3915             s_v4 = new List<dynamic>();
3916             ICollection<dynamic> v5 = null;
3917             v5 = new List<object>();
3918             return 0;
3919         }
3920 
M(int n = -1)3921         private void M(int n = -1)
3922         {
3923             _v6 = new List<dynamic>();
3924             IList<dynamic> v7;
3925             v7 = new List<object>();
3926             _v8 = new List<dynamic>();
3927             IDictionary<dynamic, string> v9;
3928             v9 = new Dictionary<object, string>();
3929             IDictionary<object, decimal> v10 = new Dictionary<dynamic, decimal>();
3930         }
3931     }
3932     // </Code>
3933 }
3934 
3935 
3936 
3937 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003
3938 {
3939     // <Area> Dynamic -- identity conversion</Area>
3940     // <Title> The conversion between dynamic and object as parameter type (generic)</Title>
3941     // <Description> List<object> is not implicitly convertible to IEnumerable<dynamic> and vice-versa - error CS0266 </Description>
3942     // <RelatedBugs></RelatedBugs>
3943     // <Expects Status=success></Expects>
3944     // <Code>
3945     using System.Collections.Generic;
3946 
3947     public class TestClass
3948     {
3949         [Fact]
RunTest()3950         public void RunTest()
3951         {
3952             Test.DynamicCSharpRunTest();
3953         }
3954     }
3955 
3956     public struct Test
3957     {
MyDel01ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test3958         internal delegate List<object> MyDel01();
MyDel02ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test3959         private delegate List<dynamic> MyDel02();
M4Del01ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test3960         private static List<object> M4Del01()
3961         {
3962             return new List<object>();
3963         }
3964 
M4Del02ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test3965         internal List<dynamic> M4Del02()
3966         {
3967             return new List<dynamic>();
3968         }
3969 
3970         internal List<object> Prop01
3971         {
3972             get
3973             {
3974                 return new List<object>();
3975             }
3976         }
3977 
3978         public List<dynamic> Prop02
3979         {
3980             get
3981             {
3982                 return new List<dynamic>();
3983             }
3984         }
3985 
3986         internal List<object> this[int n]
3987         {
3988             get
3989             {
3990                 return new List<object>(n);
3991             }
3992         }
3993 
3994         public List<dynamic> this[long n1, short n2]
3995         {
3996             get
3997             {
3998                 return new List<dynamic>((int)n1);
3999             }
4000         }
4001 
4002         private static IEnumerable<object> s_v4 = null;
4003         private ICollection<object> _v6;
4004         private IList<object> _v8;
4005         private IDictionary<string, dynamic> _v9;
4006 
DynamicCSharpRunTestManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test4007         public static void DynamicCSharpRunTest()
4008         {
4009             Assert.Equal(0, MainMethod());
4010         }
4011 
MainMethodManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test4012         public static int MainMethod()
4013         {
4014             Test t = new Test();
4015             IEnumerable<dynamic> v3 = null;
4016             MyDel01 d01 = new MyDel01(M4Del01);
4017             v3 = d01();
4018             s_v4 = t.Prop02;
4019             ICollection<dynamic> v5 = t[99];
4020             return 0;
4021         }
4022 
MManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj003.dynamicobj003.Test4023         private void M(string s = "AAA")
4024         {
4025             _v6 = this[1, 2];
4026             IList<dynamic> v7;
4027             v7 = Prop01;
4028             MyDel02 d02 = new MyDel02(M4Del02);
4029             _v8 = d02();
4030             _v9 = new Dictionary<string, object>();
4031             IDictionary<byte, object> v10 = new Dictionary<byte, dynamic>();
4032         }
4033     }
4034     // </Code>
4035 }
4036 
4037 
4038 
4039 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj004.dynamicobj004
4040 {
4041     // <Area> Dynamic -- identity conversion</Area>
4042     // <Title> The conversion between dynamic and object as parameter type (generic)</Title>
4043     // <Description> List<object> is not implicitly convertible to IEnumerable<dynamic> and vice-versa - error CS0266 </Description>
4044     // <RelatedBugs></RelatedBugs>
4045     // <Expects Status=success></Expects>
4046     // <Code>
4047     using System.Collections.Generic;
4048 
4049     public class Test
4050     {
4051         [Fact]
DynamicCSharpRunTest()4052         public static void DynamicCSharpRunTest()
4053         {
4054             Assert.Equal(0, MainMethod());
4055         }
4056 
MainMethod()4057         public static int MainMethod()
4058         {
4059             IEnumerable<dynamic> v1 = new Stack<object>();
4060             IEnumerable<object> v2 = new Queue<dynamic>();
4061             // ICollection<dynamic> v3 = new Stack<object>(); // only ICollection not ICollection<T>
4062             // ICollection<object> v4 = new Queue<dynamic>();
4063             IEnumerable<KeyValuePair<int, dynamic>> v5 = new Dictionary<int, object>();
4064             IEnumerable<KeyValuePair<int, object>> v6 = new SortedDictionary<int, dynamic>();
4065             ICollection<KeyValuePair<int, dynamic>> v7 = new Dictionary<int, object>();
4066             ICollection<KeyValuePair<int, object>> v8 = new Dictionary<int, dynamic>();
4067             IDictionary<int, dynamic> v9 = new Dictionary<int, object>();
4068             IDictionary<int, object> v10 = new Dictionary<int, dynamic>();
4069             return 0;
4070         }
4071     }
4072     // </Code>
4073 }
4074 
4075 
4076 
4077 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005
4078 {
4079     // <Area> Dynamic -- identity conversion</Area>
4080     // <Title> The conversion between dynamic and object as parameter type (generic)</Title>
4081     // <Description> List<object> is not implicitly convertible to IEnumerable<dynamic> and vice-versa - error CS0266 </Description>
4082     // <RelatedBugs></RelatedBugs>
4083     // <Expects Status=success></Expects>
4084     // <Code>
4085     using System;
4086     using System.Collections;
4087     using System.Collections.Generic;
4088 
4089     public class TestClass
4090     {
4091         [Fact]
RunTest()4092         public void RunTest()
4093         {
4094             Test.DynamicCSharpRunTest();
4095         }
4096     }
4097 
4098     public struct Test
4099     {
4100         private static IEnumerable<dynamic> s_v3;
4101         private IList<object> _v6;
4102 
DynamicCSharpRunTestManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4103         public static void DynamicCSharpRunTest()
4104         {
4105             Assert.Equal(0, MainMethod());
4106         }
4107 
MainMethodManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4108         public static int MainMethod()
4109         {
4110             Test t = new Test();
4111             ICollection<dynamic> v1 = Test.SPropObj;
4112             ICollection<object> v2;
4113             v2 = t.MethDyn();
4114             s_v3 = t[100];
4115             IEnumerable<object> v4 = t.PropDyn;
4116             DelObj d1 = new DelObj(t.M4DelObj);
4117             IList<dynamic> v5 = d1(-1);
4118             DelDyn d2 = new DelDyn(Test.M4DelDyn);
4119             t._v6 = d2();
4120             return 0;
4121         }
4122 
4123         public static MyStack<object> SPropObj
4124         {
4125             get
4126             {
4127                 return new MyStack<object>();
4128             }
4129         }
4130 
4131         public MyStack<dynamic> PropDyn
4132         {
4133             get
4134             {
4135                 return new MyStack<dynamic>();
4136             }
4137         }
4138 
MethDynManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4139         public MyStack<dynamic> MethDyn(string s = "AAA")
4140         {
4141             return new MyStack<dynamic>();
4142         }
4143 
4144         public MyStack<object> this[long n]
4145         {
4146             get
4147             {
4148                 return new MyStack<object>();
4149             }
4150         }
4151 
DelObjManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4152         private delegate MyStack<object> DelObj(int x, string s = null);
DelDynManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4153         private delegate MyStack<dynamic> DelDyn(int x = 0, int y = 0);
M4DelObjManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4154         public MyStack<object> M4DelObj(int x, string s = "Hi")
4155         {
4156             return new MyStack<object>();
4157         }
4158 
M4DelDynManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj005.dynamicobj005.Test4159         public static MyStack<dynamic> M4DelDyn(int x = 1, int y = 1)
4160         {
4161             return new MyStack<dynamic>();
4162         }
4163     }
4164 
4165     public class MyStack<T> : IEnumerable<T>, ICollection<T>, IList<T>
4166     {
4167         private int _count = 0;
4168         private const int maxcount = 128;
4169         private T[] _ary = null;
IndexOf(T t)4170         public int IndexOf(T t)
4171         {
4172             return 1;
4173         }
4174 
Insert(int n, T t)4175         public void Insert(int n, T t)
4176         {
4177             Push(t);
4178         }
4179 
RemoveAt(int n)4180         public void RemoveAt(int n)
4181         {
4182         }
4183 
4184         public T this[int n]
4185         {
4186             get
4187             {
4188                 return Pop();
4189             }
4190 
4191             set
4192             {
4193             }
4194         }
4195 
Clear()4196         public void Clear()
4197         {
4198         }
4199 
Contains(T t)4200         public bool Contains(T t)
4201         {
4202             return true;
4203         }
4204 
4205         public bool IsReadOnly
4206         {
4207             get
4208             {
4209                 return true;
4210             }
4211         }
4212 
4213         public int Count
4214         {
4215             get
4216             {
4217                 return 1;
4218             }
4219         }
4220 
CopyTo(T[] t, int n)4221         public void CopyTo(T[] t, int n)
4222         {
4223         }
4224 
Remove(T t)4225         public bool Remove(T t)
4226         {
4227             return true;
4228         }
4229 
Add(T t)4230         public void Add(T t)
4231         {
4232             Push(t);
4233         }
4234 
Push(T t)4235         public void Push(T t)
4236         {
4237             if (null == _ary)
4238             {
4239                 _ary = new T[maxcount];
4240                 _count = 0;
4241             }
4242 
4243             if (_count + 1 < maxcount)
4244                 _ary[_count++] = t;
4245             else
4246                 throw new OverflowException("max=128");
4247         }
4248 
Pop()4249         public T Pop()
4250         {
4251             if (null == _ary || 0 == _count)
4252             {
4253                 return default(T);
4254             }
4255 
4256             return _ary[_count--];
4257         }
4258 
GetEnumerator()4259         public IEnumerator<T> GetEnumerator()
4260         {
4261             for (int i = 0; i < _count; i++)
4262             {
4263                 yield return (T)_ary[i];
4264             }
4265 
4266             yield break;
4267         }
4268 
IEnumerable.GetEnumerator()4269         IEnumerator IEnumerable.GetEnumerator()
4270         {
4271             return this.GetEnumerator();
4272         }
4273     }
4274     // </Code>
4275 }
4276 
4277 
4278 
4279 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj006.dynamicobj006
4280 {
4281     //<Area>Conversion</Area>
4282     //<Title>Regression</Title>
4283     //<Description>regression test</Description>
4284     //<Related bugs></Related bugs>
4285     //<Expects Status=success></Expects Status>
4286     //<Expects Status=warning>\(12,16\).*CS0649</Expects>
4287     //<Code>
4288     public interface I<T>
4289     {
4290     }
4291 
4292     internal struct S<T> : I<T>
4293     {
4294         public int x;
4295     }
4296 
4297     public class Program
4298     {
4299         [Fact]
DynamicCSharpRunTest()4300         public static void DynamicCSharpRunTest()
4301         {
4302             Assert.Equal(0, MainMethod());
4303         }
4304 
MainMethod()4305         public static int MainMethod()
4306         {
4307             S<object> s = new S<object>();
4308             I<dynamic> d = s;
4309             var x = (S<object>)d;
4310             return 0;
4311         }
4312     }
4313     //</Code>
4314 }
4315 
4316 
4317 
4318 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dynamicobj007.dynamicobj007
4319 {
4320     //<Area>Conversion</Area>
4321     //<Title>Regression</Title>
4322     //<Description></Description>
4323     //<Related bugs>Dev11:11329</Related bugs>
4324     //<Expects Status=success></Expects Status>
4325     //<Code>
4326     using System;
4327 
4328     public class P
4329     {
4330         public class My
4331         {
Foo()4332             public void Foo()
4333             {
4334                 P.s_status = 0;
4335             }
4336         }
4337 
4338         private static int s_status = 1;
4339         [Fact]
DynamicCSharpRunTest()4340         public static void DynamicCSharpRunTest()
4341         {
4342             Assert.Equal(0, MainMethod());
4343         }
4344 
MainMethod()4345         public static int MainMethod()
4346         {
4347             Action<object> a = (dynamic x) => x.Foo();
4348             a(new My());
4349             return s_status;
4350         }
4351     }
4352     //</Code>
4353 }
4354 
4355 
4356 
4357 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dlgate003.dlgate003
4358 {
4359     // <Title>Delegate conversions</Title>
4360     // <Description>
4361     // Tests to figure out if the right conversion from method groups to delegates are applied
4362     // </Description>
4363     // <RelatedBugs></RelatedBugs>
4364     // <Expects Status=success></Expects>
4365     // <Code>
4366     public class C
4367     {
D()4368         public delegate dynamic D();
D2()4369         public delegate decimal D2();
D3()4370         public delegate string D3();
D4()4371         public delegate void D4();
Foo()4372         public static object Foo()
4373         {
4374             return new object();
4375         }
4376 
4377         [Fact]
DynamicCSharpRunTest()4378         public static void DynamicCSharpRunTest()
4379         {
4380             Assert.Equal(0, MainMethod());
4381         }
4382 
MainMethod()4383         public static int MainMethod()
4384         {
4385             int rez = 0;
4386             //delegate returns dynamic, methods return non-dynamic
4387             D del = delegate ()
4388             {
4389                 return 4;
4390             }
4391 
4392             ;
4393             var x = del();
4394             if (x == 4)
4395                 rez++;
4396             del = () => 5;
4397             x = del();
4398             if (x == 5)
4399                 rez++;
4400             del = Foo;
4401             var obj = del();
4402             if (obj != null)
4403                 rez++;
4404             //delegate returns dynamic, methods return null
4405             del = delegate ()
4406             {
4407                 return null;
4408             }
4409 
4410             ;
4411             obj = del();
4412             if (obj == null)
4413                 rez++;
4414             del = () => null;
4415             obj = del();
4416             if (obj == null)
4417                 rez++;
4418             //delegates returning non-dynamic, but we return dynamic
4419             D2 del2 = delegate ()
4420             {
4421                 dynamic d = 3;
4422                 return d;
4423             }
4424 
4425             ;
4426             var dyn = del2();
4427             if (dyn == 3)
4428                 rez++;
4429             del2 = () => (dynamic)5;
4430             dyn = del2();
4431             if (dyn == 5)
4432                 rez++;
4433             D3 del3 = delegate ()
4434             {
4435                 dynamic d = 3;
4436                 return d.ToString();
4437             }
4438 
4439             ;
4440             var dyn2 = del3();
4441             if (dyn2 == "3")
4442                 rez++;
4443             del3 = () => (dynamic)(5.ToString());
4444             dyn2 = del3();
4445             if (dyn2 == "5")
4446                 rez++;
4447             return rez == 9 ? 0 : 1;
4448         }
4449     }
4450     // </Code>
4451 }
4452 
4453 
4454 
4455 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.dlgate004.dlgate004
4456 {
4457     // <Title>Delegate conversions</Title>
4458     // <Description>
4459     // Tests to figure out if the right conversion from method groups to delegates are applied
4460     // </Description>
4461     // <RelatedBugs></RelatedBugs>
4462     // <Expects Status=success></Expects>
4463     // <Code>
4464     public class C
4465     {
D(string x)4466         public delegate int D(string x);
D2(dynamic d)4467         public delegate int D2(dynamic d);
Foo(dynamic x)4468         public static int Foo(dynamic x)
4469         {
4470             return 1;
4471         }
4472 
Foo(int x)4473         public static int Foo(int x)
4474         {
4475             return x;
4476         }
4477 
4478         [Fact]
DynamicCSharpRunTest()4479         public static void DynamicCSharpRunTest()
4480         {
4481             Assert.Equal(0, MainMethod());
4482         }
4483 
MainMethod()4484         public static int MainMethod()
4485         {
4486             int rez = 0;
4487             D d = Foo;
4488             var r = d("3");
4489             if (r == 1)
4490                 rez++;
4491             D2 dd = Foo;
4492             r = dd(3);
4493             if (r == 1)
4494                 rez++;
4495             return rez == 2 ? 0 : 1;
4496         }
4497     }
4498     // </Code>
4499 }
4500 
4501 
4502 
4503 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.arrayinit001.arrayinit001
4504 {
4505     // <Title>Array initializer conversion</Title>
4506     // <Description>
4507     // Tests to figure out if the type of the arrays is the expected one
4508     // </Description>
4509     // <RelatedBugs></RelatedBugs>
4510     //<Expects Status=success></Expects>
4511     // <Code>
4512 
4513     public class Test
4514     {
4515         [Fact]
DynamicCSharpRunTest()4516         public static void DynamicCSharpRunTest()
4517         {
4518             Assert.Equal(0, MainMethod());
4519         }
4520 
MainMethod()4521         public static int MainMethod()
4522         {
4523             //array initializer
4524             dynamic d = 3;
4525             var arr = new[]
4526             {
4527             1, 2, d
4528             }
4529 
4530             ;
4531             int[] arr2 = new int[]
4532             {
4533             1, 2, d
4534             }
4535 
4536             ;
4537             int rez = 0;
4538             try
4539             {
4540                 arr[0].Foo();
4541             }
4542             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4543             {
4544                 if (ErrorVerifier.Verify(ErrorMessageId.NoSuchMember, e.Message, "int", "Foo"))
4545                     rez++;
4546             }
4547 
4548             if (arr2[2] == d)
4549                 rez++; //this should work
4550             return rez == 2 ? 0 : 1;
4551         }
4552     }
4553     // </Code>
4554 }
4555 
4556 
4557 
4558 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.ternary001.ternary001
4559 {
4560     // <Title>Ternary operator</Title>
4561     // <Description>
4562     // // </Description>
4563     // <RelatedBugs></RelatedBugs>
4564     // <Expects Status=success></Expects>
4565     // <Code>
4566     //<Expects Status=warning>\(30,32\).*CS0429</Expects>
4567 
4568     public class CC
4569     {
4570         [Fact]
DynamicCSharpRunTest()4571         public static void DynamicCSharpRunTest()
4572         {
4573             Assert.Equal(0, MainMethod());
4574         }
4575 
MainMethod()4576         public static int MainMethod()
4577         {
4578             dynamic d = 3;
4579             int x = 3;
4580             int rez = 0;
4581             try
4582             {
4583                 var t = true ? x : d;
4584                 t.Foo();
4585             }
4586             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4587             {
4588                 if (ErrorVerifier.Verify(ErrorMessageId.NoSuchMember, e.Message, "int", "Foo"))
4589                     rez++;
4590             }
4591 
4592             try
4593             {
4594                 var t = true ? d : x;
4595                 t.Foo();
4596             }
4597             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4598             {
4599                 if (ErrorVerifier.Verify(ErrorMessageId.NoSuchMember, e.Message, "int", "Foo"))
4600                     rez++;
4601             }
4602 
4603             return rez == 2 ? 0 : 1;
4604         }
4605     }
4606     // </Code>
4607 }
4608 
4609 
4610 
4611 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.using001.using001
4612 {
4613     // <Title> Conversion </Title>
4614     // <Description> These scenarios are still using normal conversion
4615     //    Let dynamic object to do the conversion to IDisposable is covered in IDO test cases
4616     // </Description>
4617     // <RelatedBugs></RelatedBugs>
4618     //<Expects Status=success></Expects>
4619     using System;
4620 
4621     public struct SDImp : IDisposable
4622     {
DisposeManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.using001.using001.SDImp4623         public void Dispose()
4624         {
4625             Test.Output += "SDImp";
4626         }
4627     }
4628 
4629     public struct SDExp : IDisposable
4630     {
IDisposable.DisposeManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.using001.using001.SDExp4631         void IDisposable.Dispose()
4632         {
4633             Test.Output += "SDExp";
4634         }
4635     }
4636 
4637     public struct S
4638     {
DisposeManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.using001.using001.S4639         public void Dispose()
4640         {
4641             System.Console.WriteLine("S");
4642         }
4643     }
4644 
4645     public class Test
4646     {
4647         public class CDImp : IDisposable
4648         {
Dispose()4649             public void Dispose()
4650             {
4651                 Test.Output += "CDImp";
4652             }
4653         }
4654 
4655         public class CDExp : IDisposable
4656         {
IDisposable.Dispose()4657             void IDisposable.Dispose()
4658             {
4659                 Test.Output += "CDExp";
4660             }
4661         }
4662 
4663         public class C
4664         {
Dispose()4665             public void Dispose()
4666             {
4667                 System.Console.WriteLine("C");
4668             }
4669         }
4670 
4671         public static string Output;
4672 
4673         [Fact]
DynamicCSharpRunTest()4674         public static void DynamicCSharpRunTest()
4675         {
4676             Assert.Equal(0, MainMethod());
4677         }
4678 
MainMethod()4679         public static int MainMethod()
4680         {
4681             dynamic d1 = new CDImp();
4682             dynamic d2 = new CDExp();
4683             dynamic d3 = new SDImp();
4684             dynamic d4 = new SDExp();
4685 
4686             Output = "";
4687 
4688             //
4689             using (IDisposable r1 = d1, r2 = d2, r3 = d3, r4 = d4)
4690             {
4691             }
4692 
4693             //
4694             using (d1)
4695             {
4696             }
4697 
4698             using (d2)
4699             {
4700             }
4701 
4702             using (d3)
4703             {
4704             }
4705 
4706             using (d4)
4707             {
4708             }
4709 
4710             if (Output != "SDExpSDImpCDExpCDImpCDImpCDExpSDImpSDExp")
4711                 return 1;
4712 
4713             return 0 == RunTests() ? 0 : 1;
4714         }
4715 
RunTests()4716         private static int RunTests()
4717         {
4718             int ret = 0;
4719             // class
4720             dynamic d = new C();
4721             try
4722             {
4723                 using (IDisposable res = d)
4724                 {
4725                 }
4726 
4727                 ret++;
4728             }
4729             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4730             {
4731                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConvCast, e.Message, "Test.C", "System.IDisposable"))
4732                     ret++;
4733             }
4734 
4735             try
4736             {
4737                 using (d)
4738                 {
4739                 }
4740 
4741                 ret++;
4742             }
4743             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4744             {
4745                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConvCast, e.Message, "Test.C", "System.IDisposable"))
4746                     ret++;
4747             }
4748 
4749             // struct
4750             dynamic dd = new S();
4751             try
4752             {
4753                 using (IDisposable res = dd)
4754                 {
4755                 }
4756 
4757                 ret++;
4758             }
4759             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4760             {
4761                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "S", "System.IDisposable"))
4762                     ret++;
4763             }
4764 
4765             try
4766             {
4767                 using (dd)
4768                 {
4769                 }
4770 
4771                 ret++;
4772             }
4773             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
4774             {
4775                 if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "S", "System.IDisposable"))
4776                     ret++;
4777             }
4778 
4779             return ret;
4780         }
4781     }
4782 }
4783 
4784 
4785 
4786 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.array001.array001
4787 {
4788     // <Title>Array and interfaces conversions</Title>
4789     // <Description>
4790     //
4791     // </Description>
4792     // <RelatedBugs></RelatedBugs>
4793     //<Expects Status=success></Expects>
4794     // <Code>
4795     using System.Collections.Generic;
4796 
4797     public class Program
4798     {
4799         [Fact]
DynamicCSharpRunTest()4800         public static void DynamicCSharpRunTest()
4801         {
4802             Assert.Equal(0, MainMethod());
4803         }
4804 
MainMethod()4805         public static int MainMethod()
4806         {
4807             dynamic[] arr = new dynamic[]
4808             {
4809             "x", "y", "z"
4810             }
4811 
4812             ;
4813             IEnumerable<dynamic> ienum = arr;
4814             dynamic rez = "";
4815             foreach (var x in ienum)
4816             {
4817                 rez += x;
4818             }
4819 
4820             if (rez == "xyz")
4821                 return 0;
4822             return 1;
4823         }
4824     }
4825     // </Code>
4826 }
4827 
4828 
4829 
4830 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.array002.array002
4831 {
4832     // <Title>Array and interfaces conversions</Title>
4833     // <Description>
4834     //
4835     // </Description>
4836     // <RelatedBugs></RelatedBugs>
4837     //<Expects Status=success></Expects>
4838     // <Code>
4839     using System.Collections.Generic;
4840 
4841     public class Program
4842     {
4843         [Fact]
DynamicCSharpRunTest()4844         public static void DynamicCSharpRunTest()
4845         {
4846             Assert.Equal(0, MainMethod());
4847         }
4848 
MainMethod()4849         public static int MainMethod()
4850         {
4851             M1();
4852             M2();
4853             M3();
4854             return 0;
4855         }
4856 
M1()4857         private static void M1()
4858         {
4859             //bindImplicitConversionFromNullable
4860             S<dynamic>? sn = null;
4861             I<object> fooo = sn;
4862         }
4863 
M2()4864         private static void M2()
4865         {
4866             //bindImplicitConversionFromArray
4867             dynamic[] da = null;
4868             IEnumerable<object> ieo = da;
4869         }
4870 
M3()4871         private static void M3()
4872         {
4873             //BindNubConversion
4874             S<dynamic> s = new S<dynamic>();
4875             S<object>? sn = s;
4876         }
4877     }
4878 
4879     public interface I<T>
4880     {
4881     }
4882 
4883     internal struct S<T> : I<T>
4884     {
4885     }
4886     // </Code>
4887 }
4888 
4889 
4890 
4891 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.array003.array003
4892 {
4893     // <Title>Array and interfaces conversions</Title>
4894     // <Description>
4895     // use dynamic array as the source for a query expression
4896     // </Description>
4897     // <RelatedBugs></RelatedBugs>
4898     // <Expects Status=success></Expects>
4899     // <Code>
4900     using System.Linq;
4901 
4902     public class Test
4903     {
4904         [Fact]
DynamicCSharpRunTest()4905         public static void DynamicCSharpRunTest()
4906         {
4907             Assert.Equal(0, MainMethod());
4908         }
4909 
MainMethod()4910         public static int MainMethod()
4911         {
4912             dynamic dr = "";
4913             dynamic[] darr = new dynamic[]
4914             {
4915             "x", "y"
4916             }
4917 
4918             ;
4919             var dr2 =
4920                 from x in darr
4921                 select x;
4922             foreach (var i in dr2)
4923             {
4924                 dr += i;
4925             }
4926 
4927             return dr == "xy" ? 0 : 1;
4928         }
4929     }
4930     // </Code>
4931 }
4932 
4933 
4934 
4935 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.bug819947ulngenum.bug819947ulngenum
4936 {
4937     // <Title> Interaction between object and dynamic</Title>
4938     // <Description>
4939     //      [binder] NullReferenceException thrown at runtime when doing enum comparison of a dynamic variable with ulong as the underlying enum type
4940     // </Description>
4941     // <RelatedBugs></RelatedBugs>
4942     // <Expects Status=success></Expects>
4943     using System;
4944 
4945     namespace Tests
4946     {
4947         //[Serializable]
4948         public enum ByteEnum : byte
4949         {
4950             Zero,
4951             One,
4952             Min = byte.MinValue,
4953             Max = byte.MaxValue
4954         }
4955 
4956         public enum SByteEnum : sbyte
4957         {
4958             MOne = -1,
4959             One = 1,
4960             Min = sbyte.MinValue,
4961             Max = sbyte.MaxValue
4962         }
4963 
4964         public enum ShortEnum : short
4965         {
4966             One = 1,
4967             Two,
4968             Three
4969         }
4970 
4971         public enum UShortEnum : ushort
4972         {
4973             One,
4974             AnotherOne = One,
4975             Two,
4976             Three,
4977             Min = ushort.MinValue,
4978             Max = ushort.MaxValue
4979         }
4980 
4981         [Flags]
4982         public enum UIntEnum : uint
4983         {
4984             Zero = 0,
4985             One = 1,
4986             Two = 2,
4987             Four = 4,
4988             Eight = 8
4989         }
4990 
4991         public enum LongEnum : long
4992         {
4993             MZero = -0,
4994             Zero = 0,
4995             Min = long.MinValue,
4996             Max = long.MaxValue
4997         }
4998 
4999         public enum ULongEnum : ulong
5000         {
5001             One = 1,
5002             Zero = 0,
5003             Min = ulong.MinValue,
5004             Max = ulong.MaxValue
5005         }
5006 
5007         public class B819947
5008         {
M(ULongEnum p)5009             public static int M(ULongEnum p)
5010             {
5011                 return 1;
5012             }
5013 
M(ulong p)5014             public static int M(ulong p)
5015             {
5016                 return 2;
5017             }
5018 
M(long p)5019             public static int M(long p)
5020             {
5021                 return 3;
5022             }
5023 
M(object p)5024             public static int M(object p)
5025             {
5026                 return 4;
5027             }
5028 
5029             [Fact]
DynamicCSharpRunTest()5030             public static void DynamicCSharpRunTest()
5031             {
5032                 Assert.Equal(0, MainMethod());
5033             }
5034 
MainMethod()5035             public static int MainMethod()
5036             {
5037                 // original - ulong
5038                 dynamic d;
5039                 ulong val = 1;
5040                 d = (ULongEnum)val;
5041                 bool ret = (d == ULongEnum.One);
5042                 d = (ULongEnum)ulong.MinValue;
5043                 ret &= (d != ULongEnum.Max);
5044                 var r1 = d + 1UL;
5045                 ret &= r1 - 1 == d;
5046                 // Runtime EX -> d + 1L;
5047                 // should call enum
5048                 ret &= 1 == M(d);
5049                 // cast to another enum
5050                 dynamic d1 = (LongEnum)d;
5051                 ret &= (d1 >= LongEnum.MZero);
5052                 // should call obj
5053                 ret &= 4 == M(d1);
5054                 checked
5055                 {
5056                     val = ulong.MaxValue;
5057                     d = (ULongEnum)val;
5058                     ret &= (d == ULongEnum.Max); // Overflow
5059                 }
5060 
5061                 unchecked
5062                 {
5063                     d = (ULongEnum)ulong.MaxValue;
5064                     ret &= !d.Equals(ULongEnum.Max - 1); // Overflow
5065                 }
5066 
5067                 var r2 = d - 1UL;
5068                 // Long (works before)
5069                 long? v2 = 0;
5070                 d = (LongEnum)v2;
5071                 ret &= (d >= LongEnum.MZero);
5072                 d = (LongEnum)long.MinValue;
5073                 ret &= d <= LongEnum.Min;
5074                 d = (UIntEnum)0;
5075                 ret &= d + (byte)1 < UIntEnum.Two;
5076                 ret &= new B819947().RunTest();
5077                 return ret ? 0 : 1;
5078             }
5079 
RunTest()5080             private bool RunTest()
5081             {
5082                 bool ret = true;
5083                 dynamic d = UShortEnum.One;
5084                 ret &= (d == UShortEnum.AnotherOne);
5085                 d = (UShortEnum)ushort.MaxValue;
5086                 ret &= (d > UShortEnum.Two);
5087                 short vs = 2;
5088                 d = (ShortEnum)vs;
5089                 ret &= (d + (int)1 == ShortEnum.Three);
5090                 sbyte? vn = -1;
5091                 d = (SByteEnum)vn;
5092                 ret &= (d + (sbyte)2 == SByteEnum.One);
5093                 d = (ByteEnum)byte.MaxValue;
5094                 ret &= (d > ByteEnum.Min);
5095                 return ret;
5096             }
5097         }
5098     }
5099 }
5100 
5101 
5102 
5103 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.numeric001.numeric001
5104 {
5105     // <Title>Numeric Conversions</Title>
5106     // <Description> </Description>
5107     // <RelatedBugs></RelatedBugs>
5108     // <Expects Status=success></Expects>
5109     // <Code>
5110     using System;
5111     using System.Linq.Expressions;
5112 
5113     public class Program
5114     {
5115         [Fact]
DynamicCSharpRunTest()5116         public static void DynamicCSharpRunTest()
5117         {
5118             Assert.Equal(0, MainMethod(null));
5119         }
5120 
MainMethod(string[] args)5121         public static int MainMethod(string[] args)
5122         {
5123             double x = uint.MaxValue;
5124             dynamic d = x;
5125             uint i = (uint)x;
5126             uint j = (uint)d;
5127             Expression<Func<object, uint>> lambda = foo => (uint)(double)foo;
5128             uint res = lambda.Compile()(x);
5129             Func<object, uint> lambda2 = foo => (uint)(double)foo;
5130             uint res2 = lambda2(x);
5131             if (i != j)
5132                 return 1;
5133             if (i != res)
5134                 return 1;
5135             if (i != res2)
5136                 return 1;
5137             return 0;
5138         }
5139     }
5140     // </Code>
5141 }
5142 
5143 
5144 
5145 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.numeric002.numeric002
5146 {
5147     // <Title>Numeric Conversions</Title>
5148     // <Description> </Description>
5149     // <RelatedBugs></RelatedBugs>
5150     // <Expects Status=success></Expects>
5151     // <Code>
5152     using System;
5153     using System.Linq.Expressions;
5154 
5155     public class Program
5156     {
5157         [Fact]
DynamicCSharpRunTest()5158         public static void DynamicCSharpRunTest()
5159         {
5160             Assert.Equal(0, MainMethod(null));
5161         }
5162 
MainMethod(string[] args)5163         public static int MainMethod(string[] args)
5164         {
5165             ulong i, j;
5166             double x = ulong.MaxValue;
5167             dynamic d;
5168 
5169             unchecked
5170             {
5171                 d = x;
5172                 i = (ulong)x;
5173                 j = (ulong)d;
5174             }
5175 
5176             Expression<Func<object, ulong>> lambda = foo => unchecked((ulong)(double)foo);
5177             ulong res = lambda.Compile()(x);
5178             Func<object, ulong> lambda2 = foo => unchecked((ulong)(double)foo);
5179             ulong res2 = lambda2(x);
5180             if (i != j)
5181                 return 1;
5182             if (i != res)
5183                 return 1;
5184             if (i != res2)
5185                 return 1;
5186             return 0;
5187         }
5188     }
5189     // </Code>
5190 }
5191 
5192 
5193 
5194 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.numeric003.numeric003
5195 {
5196     // <Title>Numeric Conversions</Title>
5197     // <Description> </Description>
5198     // <RelatedBugs></RelatedBugs>
5199     // <Expects Status=success></Expects>
5200     // <Code>
5201     using System;
5202     using System.Linq.Expressions;
5203 
5204     public class Program
5205     {
5206         [Fact]
DynamicCSharpRunTest()5207         public static void DynamicCSharpRunTest()
5208         {
5209             Assert.Equal(0, MainMethod(null));
5210         }
5211 
MainMethod(string[] args)5212         public static int MainMethod(string[] args)
5213         {
5214             double x = ushort.MaxValue;
5215             dynamic d = x;
5216             ushort i = (ushort)x;
5217             ushort j = (ushort)d;
5218             Expression<Func<object, ushort>> lambda = foo => (ushort)(double)foo;
5219             ushort res = lambda.Compile()(x);
5220             Func<object, ushort> lambda2 = foo => (ushort)(double)foo;
5221             ushort res2 = lambda2(x);
5222             if (i != j)
5223                 return 1;
5224             if (i != res)
5225                 return 1;
5226             if (i != res2)
5227                 return 1;
5228             return 0;
5229         }
5230     }
5231     // </Code>
5232 }
5233 
5234 
5235 
5236 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001.cnst001
5237 {
5238     // <Title> Conversion -- Implicit constant expression conversions </Title>
5239     // <Description>
5240     //    A constant expression of type int can be converted to type sbyte, byte, short, ushort, uint or ulong
5241     // </Description>
5242     // <RelatedBugs></RelatedBugs>
5243     // <Expects Status=success></Expects>
5244     // <Code>
5245 
5246     public class MyClass
5247     {
operator %(MyClass mine, sbyte operand)5248         public static sbyte operator %(MyClass mine, sbyte operand)
5249         {
5250             return (sbyte)(operand + 1);
5251         }
5252 
operator |(MyClass mine, byte operand)5253         public static byte operator |(MyClass mine, byte operand)
5254         {
5255             return (byte)(operand + 2);
5256         }
5257     }
5258 
5259     public struct MyStruct
5260     {
operator /ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001.cnst001.MyStruct5261         public static short operator /(short operand, MyStruct mine)
5262         {
5263             return (short)(operand - 1);
5264         }
5265 
operator ^ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001.cnst001.MyStruct5266         public static ushort operator ^(ushort operand, MyStruct mine)
5267         {
5268             return (ushort)(operand - 2);
5269         }
5270     }
5271 
5272     public enum Eint
5273     {
5274         EM0,
5275         EM1,
5276         EM2,
5277         EM3,
5278         EM4,
5279         EM5
5280     }
5281 
5282     public enum Elong : long
5283     {
5284         EM0,
5285         EM1,
5286         EM2,
5287         EM3,
5288         EM4,
5289         EM5
5290     }
5291 
5292     public enum Eshort : short
5293     {
5294         EM0,
5295         EM1,
5296         EM2,
5297         EM3,
5298         EM4,
5299         EM5
5300     }
5301 
5302     public class Test
5303     {
5304         public const int CMemint = 10;
5305         [Fact]
DynamicCSharpRunTest()5306         public static void DynamicCSharpRunTest()
5307         {
5308             Assert.Equal(0, MainMethod());
5309         }
5310 
MainMethod()5311         public static int MainMethod()
5312         {
5313             const int CLocint = -20;
5314             int result = 0;
5315             dynamic dret;
5316             dynamic duint = (uint)1;
5317             dynamic dlong = (long)2;
5318             dynamic dulong = (ulong)3;
5319             dynamic dc = new MyClass();
5320             dynamic ds = new MyStruct();
5321             dynamic des = Eshort.EM0;
5322             dynamic del = Elong.EM3;
5323             dret = duint + (1 + (-1));
5324             if ((dret.GetType() != typeof(uint)) || (dret != (uint)1))
5325                 result++;
5326             // numeric promotion to long
5327             dret = duint + (-1 + (-1));
5328             if ((dret.GetType() != typeof(long)) || (dret != -1L))
5329                 result++;
5330             dret = int.MinValue - dlong;
5331             if ((dret.GetType() != typeof(long)) || (dret != ((long)int.MinValue - 2)))
5332                 result++;
5333             dret = dulong * int.MaxValue;
5334             if ((dret.GetType() != typeof(ulong)) || (dret != ((ulong)int.MaxValue * 3)))
5335                 result++;
5336             dret = CLocint / ds;
5337             if ((dret.GetType() != typeof(short)) || (dret != (short)-21))
5338                 result++;
5339             dret = dc % (-1);
5340             if ((dret.GetType() != typeof(sbyte)) || (dret != (sbyte)0))
5341                 result++;
5342             dret = ((int)Eint.EM1) & duint;
5343             if ((dret.GetType() != typeof(uint)) || (dret != (uint)1))
5344                 result++;
5345             dret = dc | checked((int)Elong.EM2);
5346             if ((dret.GetType() != typeof(byte)) || (dret != (byte)4))
5347                 result++;
5348             dret = CMemint ^ ds;
5349             if ((dret.GetType() != typeof(ushort)) || (dret != (ushort)8))
5350                 result++;
5351             dret = (dulong == default(int));
5352             if ((dret.GetType() != typeof(bool)) || (dret != false))
5353                 result++;
5354             dret = (~CMemint > dlong);
5355             if ((dret.GetType() != typeof(bool)) || (dret != (~10 > 2)))
5356                 result++;
5357             dret = duint;
5358             dret += (true ? 100 : CLocint);
5359             if ((dret.GetType() != typeof(uint)) || (dret != (uint)101))
5360                 result++;
5361             dret = des + 1;
5362             if ((dret.GetType() != typeof(Eshort)) || (dret != Eshort.EM1))
5363                 result++;
5364             dret = (1 + 2) + des;
5365             if ((dret.GetType() != typeof(Eshort)) || (dret != Eshort.EM3))
5366                 result++;
5367             dret = del - 1;
5368             if ((dret.GetType() != typeof(Elong)) || (dret != Elong.EM2))
5369                 result++;
5370             return result;
5371         }
5372     }
5373     // </Code>
5374 }
5375 
5376 
5377 
5378 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001b.cnst001b
5379 {
5380     // <Title> Conversion -- Implicit constant expression conversions </Title>
5381     // <Description>
5382     //    A constant expression of type int can be converted to type sbyte, byte, short, ushort, uint or ulong
5383     //    the value of constant expression is out of the range of the destination type.
5384     // </Description>
5385     // <RelatedBugs></RelatedBugs>
5386     // <Expects Status=success></Expects>
5387     // <Code>
5388 
5389     public class MyClass
5390     {
operator %(MyClass mine, sbyte operand)5391         public static sbyte operator %(MyClass mine, sbyte operand)
5392         {
5393             return (sbyte)(operand + 1);
5394         }
5395 
operator |(MyClass mine, byte operand)5396         public static byte operator |(MyClass mine, byte operand)
5397         {
5398             return (byte)(operand + 2);
5399         }
5400     }
5401 
5402     internal struct MyStruct
5403     {
operator /ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001b.cnst001b.MyStruct5404         public static short operator /(short operand, MyStruct mine)
5405         {
5406             return (short)(operand - 1);
5407         }
5408 
operator ^ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst001b.cnst001b.MyStruct5409         public static ushort operator ^(ushort operand, MyStruct mine)
5410         {
5411             return (ushort)(operand - 2);
5412         }
5413     }
5414 
5415     internal enum Eint
5416     {
5417         EM0,
5418         EM1 = -1,
5419         EM2 = -2,
5420         EM3 = -3,
5421         EM4 = -4,
5422         EM5 = -5
5423     }
5424 
5425     internal enum Elong : long
5426     {
5427         EM0,
5428         EM1 = -1000,
5429         EM2 = -2000,
5430         EM3 = -3000,
5431         EM4 = -4000,
5432         EM5 = -5000
5433     }
5434 
5435     internal enum Eshort : short
5436     {
5437         EM0,
5438         EM1,
5439         EM2
5440     }
5441 
5442     public class Test
5443     {
5444         public const int CMemint = 65538;
5445         [Fact]
DynamicCSharpRunTest()5446         public static void DynamicCSharpRunTest()
5447         {
5448             Assert.Equal(0, MainMethod());
5449         }
5450 
MainMethod()5451         public static int MainMethod()
5452         {
5453             const int CLocint = -65537;
5454             int result = 0;
5455             int flag = 1;
5456             dynamic dret;
5457             dynamic duint = (uint)1;
5458             dynamic dlong = (long)2;
5459             dynamic dulong = (ulong)3;
5460             dynamic dc = new MyClass();
5461             dynamic ds = new MyStruct();
5462             dynamic de = Eshort.EM0;
5463             flag = 1;
5464             try
5465             {
5466                 dret = dulong * int.MinValue;
5467             }
5468             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5469             {
5470                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "ulong", "int"))
5471                 {
5472                     flag = 0;
5473                 }
5474             }
5475 
5476             result += flag;
5477             flag = 1;
5478             try
5479             {
5480                 dret = CLocint / ds;
5481             }
5482             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5483             {
5484                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "/", "int", "MyStruct"))
5485                 {
5486                     flag = 0;
5487                 }
5488             }
5489 
5490             result += flag;
5491             flag = 1;
5492             try
5493             {
5494                 dret = dc % (int.MaxValue);
5495             }
5496             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5497             {
5498                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "%", "MyClass", "int"))
5499                 {
5500                     flag = 0;
5501                 }
5502             }
5503 
5504             result += flag;
5505             flag = 1;
5506             try
5507             {
5508                 dret = dc | checked((int)Elong.EM2);
5509             }
5510             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5511             {
5512                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "|", "MyClass", "int"))
5513                 {
5514                     flag = 0;
5515                 }
5516             }
5517 
5518             result += flag;
5519             flag = 1;
5520             try
5521             {
5522                 dret = CMemint ^ ds;
5523             }
5524             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5525             {
5526                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "^", "int", "MyStruct"))
5527                 {
5528                     flag = 0;
5529                 }
5530             }
5531 
5532             result += flag;
5533             flag = 1;
5534             try
5535             {
5536                 dret = (dulong == (default(int) - 1));
5537             }
5538             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5539             {
5540                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "==", "ulong", "int"))
5541                 {
5542                     flag = 0;
5543                 }
5544             }
5545 
5546             result += flag;
5547             flag = 1;
5548             try
5549             {
5550                 dret = ((~10) > dulong);
5551             }
5552             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5553             {
5554                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, ">", "int", "ulong"))
5555                 {
5556                     flag = 0;
5557                 }
5558             }
5559 
5560             result += flag;
5561             flag = 1;
5562             try
5563             {
5564                 dret = dulong;
5565                 dret += (true ? -100 : CLocint);
5566             }
5567             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5568             {
5569                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+=", "ulong", "int"))
5570                 {
5571                     flag = 0;
5572                 }
5573             }
5574 
5575             result += flag;
5576             flag = 1;
5577             try
5578             {
5579                 dret = de + CLocint;
5580             }
5581             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5582             {
5583                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "Eshort", "int"))
5584                 {
5585                     flag = 0;
5586                 }
5587             }
5588 
5589             result += flag;
5590             return result;
5591         }
5592     }
5593     // </Code>
5594 }
5595 
5596 
5597 
5598 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002.cnst002
5599 {
5600     // <Title> Conversion -- Implicit constant expression conversions </Title>
5601     // <Description>
5602     //    A constant expression of type long can be converted to ulong
5603     // </Description>
5604     // <RelatedBugs></RelatedBugs>
5605     // <Expects Status=success></Expects>
5606     // <Code>
5607     //<Expects Status=warning>\(66,33\).*CS0078</Expects>
5608 
5609     public class MyClass
5610     {
operator %(MyClass mine, ulong operand)5611         public static ulong operator %(MyClass mine, ulong operand)
5612         {
5613             return (operand + 1);
5614         }
5615 
operator |(MyClass mine, ulong operand)5616         public static ulong operator |(MyClass mine, ulong operand)
5617         {
5618             return (operand + 2);
5619         }
5620     }
5621 
5622     public struct MyStruct
5623     {
operator /ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002.cnst002.MyStruct5624         public static ulong operator /(ulong operand, MyStruct mine)
5625         {
5626             return (operand - 1);
5627         }
5628 
operator ^ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002.cnst002.MyStruct5629         public static ulong operator ^(ulong operand, MyStruct mine)
5630         {
5631             return (operand - 2);
5632         }
5633     }
5634 
5635     public enum Eint
5636     {
5637         EM0,
5638         EM1,
5639         EM2,
5640         EM3,
5641         EM4,
5642         EM5
5643     }
5644 
5645     public enum Elong : long
5646     {
5647         EM0,
5648         EM1,
5649         EM2,
5650         EM3,
5651         EM4,
5652         EM5
5653     }
5654 
5655     public enum Eulong : ulong
5656     {
5657         EM0,
5658         EM1,
5659         EM2,
5660         EM3,
5661         EM4,
5662         EM5
5663     }
5664 
5665     public class Test
5666     {
5667         public const long CMemlong = 10;
5668         [Fact]
DynamicCSharpRunTest()5669         public static void DynamicCSharpRunTest()
5670         {
5671             Assert.Equal(0, MainMethod());
5672         }
5673 
MainMethod()5674         public static int MainMethod()
5675         {
5676             const long CLoclong = -20;
5677             int result = 0;
5678             dynamic dret;
5679             dynamic dulong = 3UL;
5680             dynamic dc = new MyClass();
5681             dynamic ds = new MyStruct();
5682             dynamic de = Eulong.EM3;
5683             dret = dulong * (1L + (-1l));
5684             if ((dret.GetType() != typeof(ulong)) || (dret != 0UL))
5685                 result++;
5686             dret = dulong + long.MaxValue;
5687             if ((dret.GetType() != typeof(ulong)) || (dret != ((ulong)long.MaxValue + 3UL)))
5688                 result++;
5689             dret = -CLoclong / ds;
5690             if ((dret.GetType() != typeof(ulong)) || (dret != 19UL))
5691                 result++;
5692             dret = dc % CMemlong;
5693             if ((dret.GetType() != typeof(ulong)) || (dret != 11UL))
5694                 result++;
5695             dret = ((long)Eint.EM1) & dulong;
5696             if ((dret.GetType() != typeof(ulong)) || (dret != 1UL))
5697                 result++;
5698             dret = dc | checked((long)Elong.EM2);
5699             if ((dret.GetType() != typeof(ulong)) || (dret != 4UL))
5700                 result++;
5701             dret = CMemlong ^ ds;
5702             if ((dret.GetType() != typeof(ulong)) || (dret != 8UL))
5703                 result++;
5704             dret = (dulong == default(long));
5705             if ((dret.GetType() != typeof(bool)) || (dret != false))
5706                 result++;
5707             dret = (dulong == 1L);
5708             if ((dret.GetType() != typeof(bool)) || (dret != false))
5709                 result++;
5710             dret = dulong;
5711             dret += (true ? 100 : CLoclong);
5712             if ((dret.GetType() != typeof(ulong)) || (dret != 103UL))
5713                 result++;
5714             dret = (de + 2L);
5715             if ((dret.GetType() != typeof(Eulong)) || (dret != Eulong.EM5))
5716                 result++;
5717             dret = (1L + de);
5718             if ((dret.GetType() != typeof(Eulong)) || (dret != Eulong.EM4))
5719                 result++;
5720             dret = (de - 2L);
5721             if ((dret.GetType() != typeof(Eulong)) || (dret != Eulong.EM1))
5722                 result++;
5723             return result;
5724         }
5725     }
5726     // </Code>
5727 }
5728 
5729 
5730 
5731 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002b.cnst002b
5732 {
5733     // <Title> Conversion -- Implicit constant expression conversions </Title>
5734     // <Description>
5735     //    A constant expression of type long can be converted to ulong. the value of constant expression is negative
5736     // </Description>
5737     // <RelatedBugs></RelatedBugs>
5738     // <Expects Status=success></Expects>
5739     // <Code>
5740     //<Expects Status=warning>\(70,38\).*CS0078</Expects>
5741 
5742     public class MyClass
5743     {
operator %(MyClass mine, ulong operand)5744         public static ulong operator %(MyClass mine, ulong operand)
5745         {
5746             return (operand + 1);
5747         }
5748 
operator |(MyClass mine, ulong operand)5749         public static ulong operator |(MyClass mine, ulong operand)
5750         {
5751             return (operand + 2);
5752         }
5753     }
5754 
5755     internal struct MyStruct
5756     {
operator /ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002b.cnst002b.MyStruct5757         public static ulong operator /(ulong operand, MyStruct mine)
5758         {
5759             return (operand - 1);
5760         }
5761 
operator ^ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst002b.cnst002b.MyStruct5762         public static ulong operator ^(ulong operand, MyStruct mine)
5763         {
5764             return (operand - 2);
5765         }
5766     }
5767 
5768     internal enum Eint
5769     {
5770         EM0,
5771         EM1 = -1,
5772         EM2 = -2,
5773         EM3 = -3,
5774         EM4 = -4,
5775         EM5 = -5
5776     }
5777 
5778     internal enum Elong : long
5779     {
5780         EM0,
5781         EM1 = -1,
5782         EM2 = -2,
5783         EM3 = -3,
5784         EM4 = -4,
5785         EM5 = -5
5786     }
5787 
5788     internal enum Eulong : ulong
5789     {
5790         EM0,
5791         EM1,
5792         EM2,
5793         EM3,
5794         EM4,
5795         EM5
5796     }
5797 
5798     public class Test
5799     {
5800         public const long CMemlong = -10;
5801         [Fact]
DynamicCSharpRunTest()5802         public static void DynamicCSharpRunTest()
5803         {
5804             Assert.Equal(0, MainMethod());
5805         }
5806 
MainMethod()5807         public static int MainMethod()
5808         {
5809             const long CLoclong = 20;
5810             int result = 0;
5811             int flag = 1;
5812             dynamic dret;
5813             dynamic dulong = 3UL;
5814             dynamic dc = new MyClass();
5815             dynamic ds = new MyStruct();
5816             dynamic de = Eulong.EM3;
5817             flag = 1;
5818             try
5819             {
5820                 dret = dulong + (-1L + (-1l));
5821             }
5822             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5823             {
5824                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "ulong", "long"))
5825                 {
5826                     flag = 0;
5827                 }
5828             }
5829 
5830             result += flag;
5831             flag = 1;
5832             try
5833             {
5834                 dret = long.MinValue * dulong;
5835             }
5836             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5837             {
5838                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "long", "ulong"))
5839                 {
5840                     flag = 0;
5841                 }
5842             }
5843 
5844             result += flag;
5845             flag = 1;
5846             try
5847             {
5848                 dret = -CLoclong / ds;
5849             }
5850             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5851             {
5852                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "/", "long", "MyStruct"))
5853                 {
5854                     flag = 0;
5855                 }
5856             }
5857 
5858             result += flag;
5859             flag = 1;
5860             try
5861             {
5862                 dret = dc % CMemlong;
5863             }
5864             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5865             {
5866                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "%", "MyClass", "long"))
5867                 {
5868                     flag = 0;
5869                 }
5870             }
5871 
5872             result += flag;
5873             flag = 1;
5874             try
5875             {
5876                 dret = ((long)Eint.EM1) & dulong;
5877             }
5878             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5879             {
5880                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "&", "long", "ulong"))
5881                 {
5882                     flag = 0;
5883                 }
5884             }
5885 
5886             result += flag;
5887             flag = 1;
5888             try
5889             {
5890                 dret = dc | checked((long)Elong.EM2);
5891             }
5892             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5893             {
5894                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "|", "MyClass", "long"))
5895                 {
5896                     flag = 0;
5897                 }
5898             }
5899 
5900             result += flag;
5901             flag = 1;
5902             try
5903             {
5904                 dret = CMemlong ^ ds;
5905             }
5906             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5907             {
5908                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "^", "long", "MyStruct"))
5909                 {
5910                     flag = 0;
5911                 }
5912             }
5913 
5914             result += flag;
5915             flag = 1;
5916             try
5917             {
5918                 dret = (dulong == (default(long) - 1));
5919             }
5920             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5921             {
5922                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "==", "ulong", "long"))
5923                 {
5924                     flag = 0;
5925                 }
5926             }
5927 
5928             result += flag;
5929             flag = 1;
5930             try
5931             {
5932                 dret = ((~(10L)) > dulong);
5933             }
5934             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5935             {
5936                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, ">", "long", "ulong"))
5937                 {
5938                     flag = 0;
5939                 }
5940             }
5941 
5942             result += flag;
5943             flag = 1;
5944             try
5945             {
5946                 dret = dulong;
5947                 dret += (true ? -100 : CLoclong);
5948             }
5949             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5950             {
5951                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+=", "ulong", "long"))
5952                 {
5953                     flag = 0;
5954                 }
5955             }
5956 
5957             result += flag;
5958             flag = 1;
5959             try
5960             {
5961                 dret = (de + -1L);
5962             }
5963             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5964             {
5965                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "Eulong", "long"))
5966                 {
5967                     flag = 0;
5968                 }
5969             }
5970 
5971             result += flag;
5972             flag = 1;
5973             try
5974             {
5975                 dret = (de - (-2L));
5976             }
5977             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
5978             {
5979                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "-", "Eulong", "long"))
5980                 {
5981                     flag = 0;
5982                 }
5983             }
5984 
5985             result += flag;
5986             return result;
5987         }
5988     }
5989     // </Code>
5990 }
5991 
5992 
5993 
5994 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst003.cnst003
5995 {
5996     // <Title> Conversion -- Implicit constant expression conversions </Title>
5997     // <Description>
5998     //    No constant expression conversions for char, string, bool, float, double, decimal, byte, sbyte, short, ushort, uint, ulong
5999     // </Description>
6000     // <RelatedBugs></RelatedBugs>
6001     // <Expects Status=success></Expects>
6002     // <Code>
6003 
6004     public class MyClass
6005     {
operator +(MyClass mine, byte operand)6006         public static byte operator +(MyClass mine, byte operand)
6007         {
6008             return (byte)(operand + 1);
6009         }
6010 
operator -(MyClass mine, sbyte operand)6011         public static sbyte operator -(MyClass mine, sbyte operand)
6012         {
6013             return (sbyte)(operand + 2);
6014         }
6015 
operator *(MyClass mine, int operand)6016         public static int operator *(MyClass mine, int operand)
6017         {
6018             return (operand + 3);
6019         }
6020     }
6021 
6022     internal struct MyStruct
6023     {
operator +ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst003.cnst003.MyStruct6024         public static byte operator +(byte operand, MyStruct mine)
6025         {
6026             return (byte)(operand - 1);
6027         }
6028 
operator -ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst003.cnst003.MyStruct6029         public static sbyte operator -(sbyte operand, MyStruct mine)
6030         {
6031             return (sbyte)(operand - 2);
6032         }
6033 
operator *ManagedTests.DynamicCSharp.Conformance.dynamic.dynamicType.conversions.cnst003.cnst003.MyStruct6034         public static int operator *(int operand, MyStruct mine)
6035         {
6036             return (operand - 3);
6037         }
6038     }
6039 
6040     public class Test
6041     {
6042         [Fact]
DynamicCSharpRunTest()6043         public static void DynamicCSharpRunTest()
6044         {
6045             Assert.Equal(0, MainMethod());
6046         }
6047 
MainMethod()6048         public static int MainMethod()
6049         {
6050             const sbyte CLocsbyte = 0;
6051             const byte CLocbyte = 0;
6052             const short CLocshort = 0;
6053             const ushort CLocushort = 0;
6054             int result = 0;
6055             int flag = 1;
6056             dynamic dret;
6057             dynamic dulong = 3UL;
6058             dynamic dc = new MyClass();
6059             dynamic ds = new MyStruct();
6060             flag = 1;
6061             try
6062             {
6063                 dret = dc + 1U;
6064             }
6065             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6066             {
6067                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "MyClass", "uint"))
6068                 {
6069                     flag = 0;
6070                 }
6071             }
6072 
6073             result += flag;
6074             flag = 1;
6075             try
6076             {
6077                 dret = 1UL - ds;
6078             }
6079             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6080             {
6081                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "-", "ulong", "MyStruct"))
6082                 {
6083                     flag = 0;
6084                 }
6085             }
6086 
6087             result += flag;
6088             flag = 1;
6089             try
6090             {
6091                 dret = dc - CLocbyte;
6092             }
6093             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6094             {
6095                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "-", "MyClass", "byte"))
6096                 {
6097                     flag = 0;
6098                 }
6099             }
6100 
6101             result += flag;
6102             flag = 1;
6103             try
6104             {
6105                 dret = CLocsbyte + ds;
6106             }
6107             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6108             {
6109                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "sbyte", "MyStruct"))
6110                 {
6111                     flag = 0;
6112                 }
6113             }
6114 
6115             result += flag;
6116             flag = 1;
6117             try
6118             {
6119                 dret = dc + CLocshort;
6120             }
6121             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6122             {
6123                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "MyClass", "short"))
6124                 {
6125                     flag = 0;
6126                 }
6127             }
6128 
6129             result += flag;
6130             flag = 1;
6131             try
6132             {
6133                 dret = CLocushort - ds;
6134             }
6135             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6136             {
6137                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "-", "ushort", "MyStruct"))
6138                 {
6139                     flag = 0;
6140                 }
6141             }
6142 
6143             result += flag;
6144             flag = 1;
6145             try
6146             {
6147                 dret = dc * 0.0F;
6148             }
6149             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6150             {
6151                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "MyClass", "float"))
6152                 {
6153                     flag = 0;
6154                 }
6155             }
6156 
6157             result += flag;
6158             flag = 1;
6159             try
6160             {
6161                 dret = 1.0D * ds;
6162             }
6163             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6164             {
6165                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "double", "MyStruct"))
6166                 {
6167                     flag = 0;
6168                 }
6169             }
6170 
6171             result += flag;
6172             flag = 1;
6173             try
6174             {
6175                 dret = dc * 0.0M;
6176             }
6177             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6178             {
6179                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "MyClass", "decimal"))
6180                 {
6181                     flag = 0;
6182                 }
6183             }
6184 
6185             result += flag;
6186             flag = 1;
6187             try
6188             {
6189                 dret = true * ds;
6190             }
6191             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6192             {
6193                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "bool", "MyStruct"))
6194                 {
6195                     flag = 0;
6196                 }
6197             }
6198 
6199             result += flag;
6200             flag = 1;
6201             try
6202             {
6203                 dret = dc + '0';
6204             }
6205             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6206             {
6207                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "+", "MyClass", "char"))
6208                 {
6209                     flag = 0;
6210                 }
6211             }
6212 
6213             result += flag;
6214             flag = 1;
6215             try
6216             {
6217                 dret = "1" * ds;
6218             }
6219             catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex)
6220             {
6221                 if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, ex.Message, "*", "string", "MyStruct"))
6222                 {
6223                     flag = 0;
6224                 }
6225             }
6226 
6227             result += flag;
6228             return result;
6229         }
6230     }
6231     // </Code>
6232 }
6233