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