summaryrefslogtreecommitdiff
path: root/Examples/test-suite/csharp/csharp_exceptions_runme.cs
blob: 51805ce87ee7f3d8e3ac672cf855ddd02d2bd3dc (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Threading;
using csharp_exceptionsNamespace;

public class runme
{
    static void Main() 
    {
      // %exception tests
      try {
        csharp_exceptions.ThrowByValue();
        throw new Exception("ThrowByValue not working");
      } catch (DivideByZeroException) {
      }
      try {
        csharp_exceptions.ThrowByReference();
        throw new Exception("ThrowByReference not working");
      } catch (DivideByZeroException) {
      }

      // %csnothrowexception
      csharp_exceptions.NoThrowException();
      csharp_exceptions.NullReference(new Ex("should not throw"));

      // exception specifications
      bool testFailed = false;
      try {
        csharp_exceptions.ExceptionSpecificationValue();
        testFailed = true;
      } catch (ApplicationException) {
      }
      if (testFailed) throw new Exception("ExceptionSpecificationValue not working");
      try {
        csharp_exceptions.ExceptionSpecificationReference();
        testFailed = true;
      } catch (ApplicationException) {
      }
      if (testFailed) throw new Exception("ExceptionSpecificationReference not working");
      try {
        csharp_exceptions.ExceptionSpecificationString();
        testFailed = true;
      } catch (ApplicationException e) {
        if (e.Message != "ExceptionSpecificationString") throw new Exception("ExceptionSpecificationString unexpected message: " + e.Message);
      }
      if (testFailed) throw new Exception("ExceptionSpecificationString not working");
      try {
        csharp_exceptions.ExceptionSpecificationInteger();
        testFailed = true;
      } catch (ApplicationException) {
      }
      if (testFailed) throw new Exception("ExceptionSpecificationInteger not working");

      // null reference exceptions
      try {
        csharp_exceptions.NullReference(null);
        throw new Exception("NullReference not working");
      } catch (ArgumentNullException) {
      }
      try {
        csharp_exceptions.NullValue(null);
        throw new Exception("NullValue not working");
      } catch (ArgumentNullException) {
      }

      // enums
      try {
        csharp_exceptions.ExceptionSpecificationEnumValue();
        testFailed = true;
      } catch (ApplicationException) {
      }
      if (testFailed) throw new Exception("ExceptionSpecificationEnumValue not working");
      try {
        csharp_exceptions.ExceptionSpecificationEnumReference();
        testFailed = true;
      } catch (ApplicationException) {
      }
      if (testFailed) throw new Exception("ExceptionSpecificationEnumReference not working");

      // std::string
      try {
        csharp_exceptions.NullStdStringValue(null);
        throw new Exception("NullStdStringValue not working");
      } catch (ArgumentNullException) {
      }
      try {
        csharp_exceptions.NullStdStringReference(null);
        throw new Exception("NullStdStringReference not working");
      } catch (ArgumentNullException) {
      }
      try {
        csharp_exceptions.ExceptionSpecificationStdStringValue();
        testFailed = true;
      } catch (ApplicationException e) {
        if (e.Message != "ExceptionSpecificationStdStringValue") throw new Exception("ExceptionSpecificationStdStringValue unexpected message: " + e.Message);
      }
      if (testFailed) throw new Exception("ExceptionSpecificationStdStringValue not working");
      try {
        csharp_exceptions.ExceptionSpecificationStdStringReference();
        testFailed = true;
      } catch (ApplicationException e) {
        if (e.Message != "ExceptionSpecificationStdStringReference") throw new Exception("ExceptionSpecificationStdStringReference unexpected message: " + e.Message);
      }
      if (testFailed) throw new Exception("ExceptionSpecificationStdStringReference not working");
      
      // Memory leak check (The C++ exception stack was never unwound in the original approach to throwing exceptions from unmanaged code)
      try {
        csharp_exceptions.MemoryLeakCheck();
        throw new Exception("MemoryLeakCheck not working");
      } catch (DivideByZeroException) {
      }
      if (Counter.count != 0) throw new Exception("Memory leaks when throwing exception. count: " + Counter.count);

      // test exception pending in the csconstruct typemap
      try {
        new constructor(null);
        throw new Exception("constructor 1 not working");
      } catch (ArgumentNullException) {
      }
      try {
        new constructor();
        throw new Exception("constructor 2 not working");
      } catch (ApplicationException) {
      }

      // test exception pending in the csout typemaps
      try {
        csharp_exceptions.ushorttest();
        throw new Exception("csout not working");
      } catch (IndexOutOfRangeException) {
      }

      // test exception pending in the csvarout/csvarin typemaps and canthrow attribute in unmanaged code typemaps (1) global variables
      // 1) global variables
      int numberout = 0;
      try {
        csharp_exceptions.numberin = -1;
        throw new Exception("global csvarin not working");
      } catch (IndexOutOfRangeException) {
      }
      csharp_exceptions.numberin = 5;
      if (csharp_exceptions.numberin != 5)
        throw new Exception("global numberin not 5");
      csharp_exceptions.numberout = 20;
      try {
        numberout += csharp_exceptions.numberout;
        throw new Exception("global csvarout not working");
      } catch (IndexOutOfRangeException) {
      }
      // 2) static member variables
      try {
        InOutStruct.staticnumberin = -1;
        throw new Exception("static csvarin not working");
      } catch (IndexOutOfRangeException) {
      }
      InOutStruct.staticnumberin = 5;
      if (InOutStruct.staticnumberin != 5)
        throw new Exception("static staticnumberin not 5");
      InOutStruct.staticnumberout = 20;
      try {
        numberout += InOutStruct.staticnumberout;
        throw new Exception("static csvarout not working");
      } catch (IndexOutOfRangeException) {
      }
      // 3) member variables
      InOutStruct io = new InOutStruct();
      try {
        io.numberin = -1;
        throw new Exception("member csvarin not working");
      } catch (IndexOutOfRangeException) {
      }
      io.numberin = 5;
      if (io.numberin != 5)
        throw new Exception("member numberin not 5");
      io.numberout = 20;
      try {
        numberout += io.numberout;
        throw new Exception("member csvarout not working");
      } catch (IndexOutOfRangeException) {
      }
      // test SWIG_exception macro - it must return from unmanaged code without executing any further unmanaged code
      try {
        csharp_exceptions.exceptionmacrotest(-1);
        throw new Exception("exception macro not working");
      } catch (IndexOutOfRangeException) {
      }
      if (csharp_exceptions.exception_macro_run_flag)
        throw new Exception("exceptionmacrotest was executed");

      // test all the types of exceptions
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedApplicationException);
        throw new Exception("ApplicationException not caught");
      } catch (ApplicationException e) {
        if (e.Message != "msg") throw new Exception("ApplicationException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArithmeticException);
        throw new Exception("ArithmeticException not caught");
      } catch (ArithmeticException e) {
        if (e.Message != "msg") throw new Exception("ArithmeticException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedDivideByZeroException);
        throw new Exception("DivideByZeroException not caught");
      } catch (DivideByZeroException e) {
        if (e.Message != "msg") throw new Exception("DivideByZeroException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedIndexOutOfRangeException);
        throw new Exception("IndexOutOfRangeException not caught");
      } catch (IndexOutOfRangeException e) {
        if (e.Message != "msg") throw new Exception("IndexOutOfRangeException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedInvalidOperationException);
        throw new Exception("InvalidOperationException not caught");
      } catch (InvalidOperationException e) {
        if (e.Message != "msg") throw new Exception("InvalidOperationException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedIOException);
        throw new Exception("IOException not caught");
      } catch (System.IO.IOException e) {
        if (e.Message != "msg") throw new Exception("IOException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedNullReferenceException);
        throw new Exception("NullReferenceException not caught");
      } catch (NullReferenceException e) {
        if (e.Message != "msg") throw new Exception("NullReferenceException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedOutOfMemoryException);
        throw new Exception("OutOfMemoryException not caught");
      } catch (OutOfMemoryException e) {
        if (e.Message != "msg") throw new Exception("OutOfMemoryException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedOverflowException);
        throw new Exception("OverflowException not caught");
      } catch (OverflowException e) {
        if (e.Message != "msg") throw new Exception("OverflowException msg incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedSystemException);
        throw new Exception("SystemException not caught");
      } catch (SystemException e) {
        if (e.Message != "msg") throw new Exception("SystemException msg incorrect");
      }

      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentException);
        throw new Exception("ArgumentException not caught");
      } catch (ArgumentException e) {
        if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentException msg incorrect");
        if (e.ParamName != "parm") throw new Exception("ArgumentException parm incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentNullException);
        throw new Exception("ArgumentNullException not caught");
      } catch (ArgumentNullException e) {
        if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentNullException msg incorrect");
        if (e.ParamName != "parm") throw new Exception("ArgumentNullException parm incorrect");
      }
      try {
        csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentOutOfRangeException);
        throw new Exception("ArgumentOutOfRangeException not caught");
      } catch (ArgumentOutOfRangeException e) {
        if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentOutOfRangeException msg incorrect");
        if (e.ParamName != "parm") throw new Exception("ArgumentOutOfRangeException parm incorrect");
      }


      // exceptions in multiple threads test
      {
        ThrowsClass throwsClass = new ThrowsClass(1234.5678);
        const int NUM_THREADS = 8;
        Thread[] threads = new Thread[NUM_THREADS];
        TestThread[] testThreads = new TestThread[NUM_THREADS];
        // invoke the threads
        for (int i=0; i<NUM_THREADS; i++) {
            testThreads[i] = new TestThread(throwsClass, i);
            threads[i] = new Thread(new ThreadStart(testThreads[i].Run));
            threads[i].Start();
        }
        // wait for the threads to finish
        for (int i=0; i<NUM_THREADS; i++) {
            threads[i].Join();
        }
        for (int i=0; i<NUM_THREADS; i++) {
            if (testThreads[i].Failed) throw new Exception("Test Failed");
        }
      }


      // test inner exceptions
      try {
        csharp_exceptions.InnerExceptionTest();
        throw new Exception("InnerExceptionTest exception not caught");
      } catch (InvalidOperationException e) {
        if (e.Message != "My OuterException message") throw new Exception("OuterException msg incorrect");
        if (e.InnerException.Message != "My InnerException message") throw new Exception("InnerException msg incorrect");
      }
    }
    public static string CRLF = "\r\n"; // Some CLR implementations use a CRLF instead of just CR
}

public class TestThread {
   private int threadId;
   private ThrowsClass throwsClass;
   public bool Failed;
   public TestThread(ThrowsClass t, int id) {
       throwsClass = t;
       threadId = id;
   }
   public void Run() {
     Failed = false;
     try {
       for (int i=0; i<6000; i++) { // run test for about 10 seconds on a 1GHz machine (Mono)
         try {
           throwsClass.ThrowException(i);
           throw new Exception("No exception thrown");
         } catch (ArgumentOutOfRangeException e) {
           String expectedMessage = "caught:" + i + "\n" + "Parameter name: input";
           if (e.Message.Replace(runme.CRLF,"\n") != expectedMessage)
             throw new Exception("Exception message incorrect. Expected:\n[" + 
                 expectedMessage + "]\n" + "Received:\n[" + 
                 e.Message + "]");
           if (e.ParamName != "input")
             throw new Exception("Exception ParamName incorrect. Expected:\n[input]\n" + "Received:\n[" + e.ParamName + "]");
           if (e.InnerException != null)
             throw new Exception("Unexpected inner exception");
         }
         if (throwsClass.dub != 1234.5678) // simple check which attempts to catch memory corruption
           throw new Exception("throwsException.dub = " + throwsClass.dub + " expected: 1234.5678");
       }
     } catch (Exception e) {
       Console.Error.WriteLine("Test failed (thread " + threadId + "): " + e.Message + "\n  TestThread Inner stack trace: " + e.StackTrace);
       Failed = true;
     }
   }
}