Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-400.cs
1 // Compiler options: -unsafe
2
3 //
4 // Tests unsafe operators.  address-of, dereference, member access
5 //
6 using System;
7
8 unsafe struct Y {
9         public int a;
10         public int s;
11 }
12
13 unsafe class X {
14         static int TestDereference ()
15         {
16                 Y y;
17                 Y *z; 
18                 Y a;
19
20                 z = &y;
21                 y.a = 1;
22                 y.s = 2;
23
24                 a.a = z->a;
25                 a.s = z->s;
26
27                 if (a.a != y.a)
28                         return 1;
29                 if (a.s != y.s)
30                         return 2;
31
32                 return 0;
33         }
34
35         static int TestPtrAdd ()
36         {
37                 int [] a = new int [10];
38                 int i;
39                 
40                 for (i = 0; i < 10; i++)
41                         a [i] = i;
42
43                 i = 0;
44                 fixed (int *b = &a [0]){ 
45                         int *p = b;
46
47                         for (i = 0; i < 10; i++){
48                                 if (*p != a [i])
49                                         return 10+i;
50                                 p++;
51                         }
52                 }
53                 return 0;
54         }
55
56         static int i = 1;
57         static char c = 'a';
58         static long l = 123;
59         static double d = 1.2;
60         static float f = 1.3F;
61         static short s = 4;
62         
63         static int TestPtrAssign ()
64         {
65
66                 fixed (int *ii = &i){
67                         *ii = 10;
68                 }
69
70                 fixed (char *cc = &c){
71                         *cc = 'b';
72                 }
73
74                 fixed (long *ll = &l){
75                         *ll = 100;
76                 }
77
78                 fixed (double *dd = &d){
79                         *dd = 3.0;
80                 }
81
82                 fixed (float *ff = &f){
83                         *ff = 1.2F;
84                 }
85
86                 fixed (short *ss = &s){
87                         *ss = 102;
88                 }
89
90                 if (i != 10)
91                         return 100;
92                 if (c != 'b')
93                         return 101;
94                 if (l != 100)
95                         return 102;
96                 if (d != 3.0)
97                         return 103;
98                 if (f != 1.2F)
99                         return 104;
100                 if (s != 102)
101                         return 105;
102                 return 0;
103         }
104
105         static int TestPtrArithmetic ()
106         {
107                 char [] array = new char [10];
108                 char *pb;
109
110                 array [5] = 'j';
111                 fixed (char *pa = array){
112                         pb = pa + 1;
113
114
115                         //
116                         // This one tests pointer element access
117                         //
118                         if (pa [5] != 'j')
119                                 return 199;
120                         
121                         Console.WriteLine ("V: " + (pb - pa));
122                         if ((pb - pa) != 1)
123                                 return 200;
124
125                         pb++;
126
127                         if (pb == pa)
128                                 return 201;
129                         if (pb < pa)
130                                 return 202;
131                         if (pa > pb)
132                                 return 203;
133                         if (pa >= pb)
134                                 return 204;
135                         if (pb <= pa)
136                                 return 205;
137                         pb = pb - 2;
138                         if (pb != pa){
139                                 Console.WriteLine ("VV: " + (pb - pa));
140                                 return 206;
141                         }
142                 }
143
144                 return 0;
145         }
146
147         static int TestMultiple ()
148         {
149                 char [] array = new char [10];
150                 int count = 0;
151                 
152                 fixed (char *pa = array, pb = array){
153                         count++;
154                 }
155                 if (count != 1)
156                         return 300;
157                 return 0;
158         }
159         
160         public static int Main ()
161         {
162                 int v;
163
164                 if ((v = TestDereference ()) != 0)
165                         return v;
166
167                 if ((v = TestPtrAdd ()) != 0)
168                         return v;
169
170                 if ((v = TestPtrAssign ()) != 0)
171                         return v;
172
173                 if ((v = TestPtrArithmetic ()) != 0)
174                         return v;
175
176                 if ((v = TestMultiple ()) != 0)
177                         return v;
178                 
179                 Console.WriteLine ("Ok");
180                 return 0;
181         }
182 }