[mono/tests] Fix out of tree build.
[mono-project.git] / mcs / tests / test-null-operator-01.cs
blob63cae1282ee984b2337b3fbdccfebbdc131795a6
1 using System;
3 struct S
5 public int Prop { get; set; }
8 interface I
10 int Method ();
13 class CI : I
15 public int Method ()
17 return 33;
20 public int Prop { get; set; }
23 class C
25 static int prop_calls;
26 static string Prop {
27 get {
28 ++prop_calls;
29 return null;
33 static int TestArray ()
35 int[] k = null;
36 var t1 = k?.ToString ();
37 if (t1 != null)
38 return 1;
40 var t2 = k?.GetLength (0);
41 if (t2 != null)
42 return 2;
44 var t3 = k?.Length;
45 if (t3 != null)
46 return 3;
48 var t4 = k?.GetLength (0).ToString () ?? "N";
49 if (t4 != "N")
50 return 4;
52 var t5 = k?.Length.ToString () ?? "N";
53 if (t5 != "N")
54 return 5;
56 k = new int[] { 3 };
57 var t11 = k?.ToString ();
58 if (t11.GetType () != typeof (string))
59 return 10;
61 var t12 = k?.GetLength (0);
62 if (t12.GetType () != typeof (int))
63 return 11;
65 var t13 = k?.Length;
66 if (t13.GetType () != typeof (int))
67 return 12;
69 return 0;
72 static int TestReferenceType ()
74 string s = null;
75 var t1 = s?.Split ();
76 if (t1 != null)
77 return 1;
79 var t2 = s?.Length;
80 if (t2 != null)
81 return 2;
83 var t3 = Prop?.Length;
84 if (t3 != null)
85 return 3;
86 if (prop_calls != 1)
87 return 4;
89 var t4 = Prop?.Split ();
90 if (t4 != null)
91 return 5;
92 if (prop_calls != 2)
93 return 6;
95 return 0;
98 static int TestGeneric<T> (T t) where T : class, I
100 var t1 = t?.Method ();
101 if (t1 != null)
102 return 1;
104 T[] at = null;
105 var t2 = at?.Length;
106 if (t2 != null)
107 return 2;
109 return 0;
112 static int TestNullable ()
114 int? i = 4;
115 var m = i?.CompareTo (3);
116 if (m.GetType () != typeof (int))
117 return 1;
119 if (m != 1)
120 return 2;
122 DateTime? dt = null;
123 dt?.ToString ();
124 if (dt?.ToString () != null)
125 return 3;
127 byte? b = 0;
128 if (b?.ToString () != "0")
129 return 4;
131 S? s = null;
132 var p1 = s?.Prop;
133 if (p1 != null)
134 return 5;
136 return 0;
139 static int Main ()
141 int res;
142 res = TestNullable ();
143 if (res != 0)
144 return 100 + res;
146 res = TestArray ();
147 if (res != 0)
148 return 200 + res;
150 res = TestReferenceType ();
151 if (res != 0)
152 return 300 + res;
154 CI ci = null;
155 res = TestGeneric<CI> (ci);
156 if (res != 0)
157 return 400 + res;
159 Console.WriteLine ("ok");
160 return 0;