Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unsafe-1.cs « tests « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b49bc0fe2779fbcb91b15265e7025fd3d00abca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Tests unsafe operators.  address-of, dereference, member access
//
using System;

unsafe struct Y {
	public int a;
	public int s;
}

unsafe class X {
	static int TestDereference ()
	{
		Y y;
		Y *z; 
		Y a;

		z = &y;
		y.a = 1;
		y.s = 2;

		a.a = z->a;
		a.s = z->s;

		if (a.a != y.a)
			return 1;
		if (a.s != y.s)
			return 2;

		return 0;
	}

	static int TestPtrAdd ()
	{
		int [] a = new int [10];
		int i;
		
		for (i = 0; i < 10; i++)
			a [i] = i;

		i = 0;
		fixed (int *b = &a [0]){ 
			int *p = b;

			for (i = 0; i < 10; i++){
				if (*p != a [i])
					return 10+i;
				p++;
			}
		}
		return 0;
	}

	static int i = 1;
	static char c = 'a';
	static long l = 123;
	static double d = 1.2;
	static float f = 1.3F;
	static short s = 4;
	
	static int TestPtrAssign ()
	{

		fixed (int *ii = &i){
			*ii = 10;
		}

		fixed (char *cc = &c){
			*cc = 'b';
		}

		fixed (long *ll = &l){
			*ll = 100;
		}

		fixed (double *dd = &d){
			*dd = 3.0;
		}

		fixed (float *ff = &f){
			*ff = 1.2F;
		}

		fixed (short *ss = &s){
			*ss = 102;
		}

		if (i != 10)
			return 100;
		if (c != 'b')
			return 101;
		if (l != 100)
			return 102;
		if (d != 3.0)
			return 103;
		if (f != 1.2F)
			return 104;
		if (s != 102)
			return 105;
		return 0;
	}

	static int TestPtrArithmetic ()
	{
		char [] array = new char [10];
		char *pb;

		array [5] = 'j';
		fixed (char *pa = array){
			pb = pa + 1;


			//
			// This one tests pointer element access
			//
			if (pa [5] != 'j')
				return 199;
			
			Console.WriteLine ("V: " + (pb - pa));
			if ((pb - pa) != 1)
				return 200;

			pb++;

			if (pb == pa)
				return 201;
			if (pb < pa)
				return 202;
			if (pa > pb)
				return 203;
			if (pa >= pb)
				return 204;
			if (pb <= pa)
				return 205;
			pb = pb - 2;
			if (pb != pa){
				Console.WriteLine ("VV: " + (pb - pa));
				return 206;
			}
		}

		return 0;
	}
	
	static int Main ()
	{
		int v;

		if ((v = TestDereference ()) != 0)
			return v;

		if ((v = TestPtrAdd ()) != 0)
			return v;

		if ((v = TestPtrAssign ()) != 0)
			return v;

		if ((v = TestPtrArithmetic ()) != 0)
			return v;
		Console.WriteLine ("Ok");
		return 0;
	}
}