Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-null-operator-01.cs
1 using System;
2
3 struct S
4 {
5     public int Prop { get; set; }
6 }
7
8 interface I
9 {
10     int Method ();
11 }
12
13 class CI : I
14 {
15     public int Method ()
16     {
17         return 33;
18     }
19
20     public int Prop { get; set; }
21 }
22
23 class C
24 {
25     static int prop_calls;
26     static string Prop {
27         get {
28             ++prop_calls;
29             return null;
30         }
31     }
32
33     static int TestArray ()
34     {
35         int[] k = null;
36         var t1 = k?.ToString ();
37         if (t1 != null)
38             return 1;
39
40         var t2 = k?.GetLength (0);
41         if (t2 != null)
42             return 2;
43
44         var t3 = k?.Length;
45         if (t3 != null)
46             return 3;
47
48         var t4 = k?.GetLength (0).ToString () ?? "N";
49         if (t4 != "N")
50             return 4;
51
52         var t5 = k?.Length.ToString () ?? "N";
53         if (t5 != "N")
54             return 5;            
55
56         k = new int[] { 3 };
57         var t11 = k?.ToString ();
58         if (t11.GetType () != typeof (string))
59             return 10;
60
61         var t12 = k?.GetLength (0);
62         if (t12.GetType () != typeof (int))
63             return 11;
64
65         var t13 = k?.Length;
66         if (t13.GetType () != typeof (int))
67             return 12;
68
69         return 0;
70     }
71
72     static int TestReferenceType ()
73     {
74         string s = null;
75         var t1 = s?.Split ();
76         if (t1 != null)
77             return 1;
78
79         var t2 = s?.Length;
80         if (t2 != null)
81             return 2;
82
83         var t3 = Prop?.Length;
84         if (t3 != null)
85             return 3;
86         if (prop_calls != 1)
87             return 4;
88
89         var t4 = Prop?.Split ();
90         if (t4 != null)
91             return 5;
92         if (prop_calls != 2)
93             return 6;
94
95         return 0;
96     }
97
98     static int TestGeneric<T> (T t) where T : class, I
99     {
100         var t1 = t?.Method ();
101         if (t1 != null)
102             return 1;
103
104         T[] at = null;
105         var t2 = at?.Length;
106         if (t2 != null)
107             return 2;
108
109         return 0;
110     }
111
112     static int TestNullable ()
113     {
114         int? i = 4;
115         var m = i?.CompareTo (3);
116         if (m.GetType () != typeof (int))
117             return 1;
118
119         if (m != 1)
120             return 2;
121
122         DateTime? dt = null;
123         dt?.ToString ();
124         if (dt?.ToString () != null)
125             return 3;
126
127         byte? b = 0;
128         if (b?.ToString () != "0")
129             return 4;
130
131         S? s = null;
132         var p1 = s?.Prop;
133         if (p1 != null)
134             return 5;
135
136         return 0;
137     }
138
139     static int Main ()
140     {
141         int res;
142         res = TestNullable ();
143         if (res != 0)
144             return 100 + res;
145
146         res = TestArray ();
147         if (res != 0)
148             return 200 + res;
149
150         res = TestReferenceType ();
151         if (res != 0)
152             return 300 + res;
153
154         CI ci = null;
155         res = TestGeneric<CI> (ci);
156         if (res != 0)
157             return 400 + res;
158
159         Console.WriteLine ("ok");
160         return 0;
161     }
162 }