[corlib] Fix a few NSLogWriter issues. (#13799)
[mono-project.git] / mcs / tests / test-400.cs
blobb5369297d11dd74dcbae57ac185bb490f7670f3b
1 // Compiler options: -unsafe
3 //
4 // Tests unsafe operators. address-of, dereference, member access
5 //
6 using System;
8 unsafe struct Y {
9 public int a;
10 public int s;
13 unsafe class X {
14 static int TestDereference ()
16 Y y;
17 Y *z;
18 Y a;
20 z = &y;
21 y.a = 1;
22 y.s = 2;
24 a.a = z->a;
25 a.s = z->s;
27 if (a.a != y.a)
28 return 1;
29 if (a.s != y.s)
30 return 2;
32 return 0;
35 static int TestPtrAdd ()
37 int [] a = new int [10];
38 int i;
40 for (i = 0; i < 10; i++)
41 a [i] = i;
43 i = 0;
44 fixed (int *b = &a [0]){
45 int *p = b;
47 for (i = 0; i < 10; i++){
48 if (*p != a [i])
49 return 10+i;
50 p++;
53 return 0;
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;
63 static int TestPtrAssign ()
66 fixed (int *ii = &i){
67 *ii = 10;
70 fixed (char *cc = &c){
71 *cc = 'b';
74 fixed (long *ll = &l){
75 *ll = 100;
78 fixed (double *dd = &d){
79 *dd = 3.0;
82 fixed (float *ff = &f){
83 *ff = 1.2F;
86 fixed (short *ss = &s){
87 *ss = 102;
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;
105 static int TestPtrArithmetic ()
107 char [] array = new char [10];
108 char *pb;
110 array [5] = 'j';
111 fixed (char *pa = array){
112 pb = pa + 1;
116 // This one tests pointer element access
118 if (pa [5] != 'j')
119 return 199;
121 Console.WriteLine ("V: " + (pb - pa));
122 if ((pb - pa) != 1)
123 return 200;
125 pb++;
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;
144 return 0;
147 static int TestMultiple ()
149 char [] array = new char [10];
150 int count = 0;
152 fixed (char *pa = array, pb = array){
153 count++;
155 if (count != 1)
156 return 300;
157 return 0;
160 public static int Main ()
162 int v;
164 if ((v = TestDereference ()) != 0)
165 return v;
167 if ((v = TestPtrAdd ()) != 0)
168 return v;
170 if ((v = TestPtrAssign ()) != 0)
171 return v;
173 if ((v = TestPtrArithmetic ()) != 0)
174 return v;
176 if ((v = TestMultiple ()) != 0)
177 return v;
179 Console.WriteLine ("Ok");
180 return 0;