[interp] Remove unreachable code (#12411)
[mono-project.git] / mono / tests / cross-domain.cs
blob9e340acdb2f964fd0eb7ad9d6e17bb511575a952
1 using System;
2 using System.Net;
3 using System.Runtime.Remoting;
4 using System.Runtime.InteropServices;
5 using System.Text;
6 using System.Runtime.Serialization;
8 public class Test: MarshalByRefObject
9 {
10 public static int Main (string[] args)
12 AppDomain domain = AppDomain.CreateDomain ("testdomain1");
13 Test server = (Test) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, "Test");
14 return server.RunTest ();
17 public int RunTest ()
19 try
21 object t = null;
22 string s = (string)t;
23 AppDomain domain = AppDomain.CreateDomain ("testdomain");
24 Remo server = (Remo) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName,"Remo");
25 if (System.Threading.Thread.GetDomainID () == server.GetDomainId ())
26 throw new TestException ("Object not created in new domain", 1);
28 Dada d = new Dada ();
29 d.p = 22;
31 server.Run ();
32 server.Run2 (88, "hola");
33 server.Run3 (99, d, "adeu");
35 string r = server.Run4 (200, d, "que");
36 CheckValue (r, "vist", 140);
38 try {
39 server.Run5 (200, d, "que");
40 throw new TestException ("Exception expected", 150);
42 catch (Exception ex) {
43 CheckValue (ex.Message, "peta", 151);
46 Dada d2;
47 d = server.Run6 (99, out d2, "adeu");
48 CheckValue (d.p, 987, 161);
49 CheckValue (d2.p, 987, 162);
51 d.p = 22;
52 d2 = server.Run7 (d);
53 CheckValue (d.p, 22, 170);
54 CheckValue (d2.p, 33, 170);
56 byte[] ba = new byte[5];
57 for (byte n=0; n<ba.Length; n++)
58 ba [n] = n;
60 server.Run8 (ba);
62 for (int n=0; n<ba.Length; n++)
63 CheckValue (ba[n], (byte) (ba.Length - n), 180);
65 StringBuilder sb = new StringBuilder ("un");
66 server.Run9_1 (sb);
67 Test.CheckValue (sb, new StringBuilder ("un"), 190);
69 StringBuilder sb2 = new StringBuilder ("prefix");
70 StringBuilder sb3 = server.Run9_2 (sb2);
71 Test.CheckValue (sb2, new StringBuilder ("prefix"), 192);
72 Test.CheckValue (sb3, new StringBuilder ("prefix-middle"), 192);
73 StringBuilder sb4 = server.Run9_3 (sb3);
74 Test.CheckValue (sb3, new StringBuilder ("prefix-middle"), 193);
75 Test.CheckValue (sb4, new StringBuilder ("prefix-middle-end"), 193);
79 catch (TestException ex)
81 Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
82 return ex.Code;
84 catch (Exception ex)
86 Console.WriteLine ("TEST ERROR: " + ex);
87 return -1;
89 return 0;
92 public static void CheckDomain (object ob, Type t, int ec)
94 if (ob == null) return;
95 if (ob.GetType () != t) {
96 if (t.ToString() == ob.GetType().ToString())
97 throw new TestException ("Parameter not properly marshalled", ec);
98 else
99 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
103 public static void CheckValue (object ob1, object ob2, int ec)
105 if ((ob1 == null || ob2 == null) && ob1 != ob2)
106 throw new TestException ("Null objects are not equal", ec);
108 if (ob2.GetType () != ob1.GetType ())
109 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
111 if (ob1 is StringBuilder) {
112 if (Object.ReferenceEquals (ob1, ob2))
113 throw new TestException ("StringBuilders are ReferenceEquals", ec);
115 StringBuilder sb1 = (StringBuilder) ob1;
116 StringBuilder sb2 = (StringBuilder) ob2;
118 if (sb1.ToString () != sb2.ToString ())
119 throw new TestException ("String in StringBuilders are not equal", ec);
121 if (sb1.Length != sb2.Length)
122 throw new TestException ("Lengths in StringBuilders are not equal", ec);
124 else if (!ob1.Equals (ob2))
125 throw new TestException ("Objects are not equal", ec);
129 public class Remo: MarshalByRefObject
131 int domid;
133 public Remo ()
135 domid = System.Threading.Thread.GetDomainID ();
138 public int GetDomainId ()
140 return domid;
143 public void CheckThisDomain (int ec)
145 if (domid != System.Threading.Thread.GetDomainID ())
146 throw new TestException ("Wrong domain", ec);
149 public void Run ()
151 CheckThisDomain (10);
154 public void Run2 (int a, string b)
156 CheckThisDomain (20);
157 Test.CheckValue (a, 88, 21);
158 Test.CheckValue (b, "hola", 22);
161 public void Run3 (int a, Dada d, string b)
163 CheckThisDomain (30);
164 Test.CheckValue (a, 99, 31);
165 Test.CheckValue (b, "adeu", 32);
166 Test.CheckValue (d.p, 22, 33);
169 public string Run4 (int a, Dada d, string b)
171 CheckThisDomain (40);
172 Test.CheckValue (a, 200, 41);
173 Test.CheckValue (b, "que", 42);
174 Test.CheckValue (d.p, 22, 43);
175 return "vist";
178 public Dada Run5 (int a, Dada d, string b)
180 CheckThisDomain (50);
181 Test.CheckValue (a, 200, 51);
182 Test.CheckValue (b, "que", 52);
183 Test.CheckValue (d.p, 22, 53);
184 Peta ();
185 return d;
188 public Dada Run6 (int a, out Dada d, string b)
190 CheckThisDomain (60);
191 Test.CheckValue (a, 99, 61);
192 Test.CheckValue (b, "adeu", 62);
194 d = new Dada ();
195 d.p = 987;
196 return d;
199 public Dada Run7 (Dada d)
201 CheckThisDomain (70);
202 Test.CheckValue (d.p, 22, 71);
203 d.p = 33;
204 return d;
207 public void Run8 ([In,Out] byte[] bytes)
209 CheckThisDomain (80);
210 Test.CheckDomain (bytes, typeof(byte[]), 81);
211 for (int n=0; n < bytes.Length; n++) {
212 Test.CheckValue (bytes[n], (byte)n, 82);
213 bytes[n] = (byte) (bytes.Length - n);
217 public void Run9_1 ([In,Out] StringBuilder sb)
219 CheckThisDomain (90);
220 Test.CheckValue (sb, new StringBuilder ("un"), 91);
221 sb.Append ("-dos");
224 public StringBuilder Run9_2 ([In] StringBuilder sb)
226 CheckThisDomain (91);
227 Test.CheckValue (sb, new StringBuilder ("prefix"), 92);
228 sb.Append ("-middle");
229 return sb;
232 public StringBuilder Run9_3 ([In,Out] StringBuilder sb)
234 CheckThisDomain (92);
235 Test.CheckValue (sb, new StringBuilder ("prefix-middle"), 93);
236 sb.Append ("-end");
238 return sb;
241 public void Peta ()
243 throw new Exception ("peta");
247 [Serializable]
248 public class Dada
250 public int p;
253 [Serializable]
254 public class MyException: Exception
256 public MyException (string s): base (s) {}
259 [Serializable]
260 public class TestException: Exception
262 public int Code = -1;
264 public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
265 Code = i.GetInt32 ("Code");
268 public TestException (string txt, int code): base (txt + " (code: " + code + ")")
270 Code = code;
273 public override void GetObjectData (SerializationInfo info, StreamingContext context)
275 base.GetObjectData (info, context);
276 info.AddValue ("Code", Code);