/trunk/Examples/test-suite/csharp/csharp_exceptions_runme.cs
C# | 342 lines | 304 code | 19 blank | 19 comment | 72 complexity | f2414b236b3a3ce37531826e70c1810a MD5 | raw file
1using System; 2using System.Threading; 3using csharp_exceptionsNamespace; 4 5public class runme 6{ 7 static void Main() 8 { 9 // %exception tests 10 try { 11 csharp_exceptions.ThrowByValue(); 12 throw new Exception("ThrowByValue not working"); 13 } catch (DivideByZeroException) { 14 } 15 try { 16 csharp_exceptions.ThrowByReference(); 17 throw new Exception("ThrowByReference not working"); 18 } catch (DivideByZeroException) { 19 } 20 21 // %csnothrowexception 22 csharp_exceptions.NoThrowException(); 23 csharp_exceptions.NullReference(new Ex("should not throw")); 24 25 // exception specifications 26 bool testFailed = false; 27 try { 28 csharp_exceptions.ExceptionSpecificationValue(); 29 testFailed = true; 30 } catch (ApplicationException) { 31 } 32 if (testFailed) throw new Exception("ExceptionSpecificationValue not working"); 33 try { 34 csharp_exceptions.ExceptionSpecificationReference(); 35 testFailed = true; 36 } catch (ApplicationException) { 37 } 38 if (testFailed) throw new Exception("ExceptionSpecificationReference not working"); 39 try { 40 csharp_exceptions.ExceptionSpecificationString(); 41 testFailed = true; 42 } catch (ApplicationException e) { 43 if (e.Message != "ExceptionSpecificationString") throw new Exception("ExceptionSpecificationString unexpected message: " + e.Message); 44 } 45 if (testFailed) throw new Exception("ExceptionSpecificationString not working"); 46 try { 47 csharp_exceptions.ExceptionSpecificationInteger(); 48 testFailed = true; 49 } catch (ApplicationException) { 50 } 51 if (testFailed) throw new Exception("ExceptionSpecificationInteger not working"); 52 53 // null reference exceptions 54 try { 55 csharp_exceptions.NullReference(null); 56 throw new Exception("NullReference not working"); 57 } catch (ArgumentNullException) { 58 } 59 try { 60 csharp_exceptions.NullValue(null); 61 throw new Exception("NullValue not working"); 62 } catch (ArgumentNullException) { 63 } 64 65 // enums 66 try { 67 csharp_exceptions.ExceptionSpecificationEnumValue(); 68 testFailed = true; 69 } catch (ApplicationException) { 70 } 71 if (testFailed) throw new Exception("ExceptionSpecificationEnumValue not working"); 72 try { 73 csharp_exceptions.ExceptionSpecificationEnumReference(); 74 testFailed = true; 75 } catch (ApplicationException) { 76 } 77 if (testFailed) throw new Exception("ExceptionSpecificationEnumReference not working"); 78 79 // std::string 80 try { 81 csharp_exceptions.NullStdStringValue(null); 82 throw new Exception("NullStdStringValue not working"); 83 } catch (ArgumentNullException) { 84 } 85 try { 86 csharp_exceptions.NullStdStringReference(null); 87 throw new Exception("NullStdStringReference not working"); 88 } catch (ArgumentNullException) { 89 } 90 try { 91 csharp_exceptions.ExceptionSpecificationStdStringValue(); 92 testFailed = true; 93 } catch (ApplicationException e) { 94 if (e.Message != "ExceptionSpecificationStdStringValue") throw new Exception("ExceptionSpecificationStdStringValue unexpected message: " + e.Message); 95 } 96 if (testFailed) throw new Exception("ExceptionSpecificationStdStringValue not working"); 97 try { 98 csharp_exceptions.ExceptionSpecificationStdStringReference(); 99 testFailed = true; 100 } catch (ApplicationException e) { 101 if (e.Message != "ExceptionSpecificationStdStringReference") throw new Exception("ExceptionSpecificationStdStringReference unexpected message: " + e.Message); 102 } 103 if (testFailed) throw new Exception("ExceptionSpecificationStdStringReference not working"); 104 105 // Memory leak check (The C++ exception stack was never unwound in the original approach to throwing exceptions from unmanaged code) 106 try { 107 csharp_exceptions.MemoryLeakCheck(); 108 throw new Exception("MemoryLeakCheck not working"); 109 } catch (DivideByZeroException) { 110 } 111 if (Counter.count != 0) throw new Exception("Memory leaks when throwing exception. count: " + Counter.count); 112 113 // test exception pending in the csconstruct typemap 114 try { 115 new constructor(null); 116 throw new Exception("constructor 1 not working"); 117 } catch (ArgumentNullException) { 118 } 119 try { 120 new constructor(); 121 throw new Exception("constructor 2 not working"); 122 } catch (ApplicationException) { 123 } 124 125 // test exception pending in the csout typemaps 126 try { 127 csharp_exceptions.ushorttest(); 128 throw new Exception("csout not working"); 129 } catch (IndexOutOfRangeException) { 130 } 131 132 // test exception pending in the csvarout/csvarin typemaps and canthrow attribute in unmanaged code typemaps (1) global variables 133 // 1) global variables 134 int numberout = 0; 135 try { 136 csharp_exceptions.numberin = -1; 137 throw new Exception("global csvarin not working"); 138 } catch (IndexOutOfRangeException) { 139 } 140 csharp_exceptions.numberin = 5; 141 if (csharp_exceptions.numberin != 5) 142 throw new Exception("global numberin not 5"); 143 csharp_exceptions.numberout = 20; 144 try { 145 numberout += csharp_exceptions.numberout; 146 throw new Exception("global csvarout not working"); 147 } catch (IndexOutOfRangeException) { 148 } 149 // 2) static member variables 150 try { 151 InOutStruct.staticnumberin = -1; 152 throw new Exception("static csvarin not working"); 153 } catch (IndexOutOfRangeException) { 154 } 155 InOutStruct.staticnumberin = 5; 156 if (InOutStruct.staticnumberin != 5) 157 throw new Exception("static staticnumberin not 5"); 158 InOutStruct.staticnumberout = 20; 159 try { 160 numberout += InOutStruct.staticnumberout; 161 throw new Exception("static csvarout not working"); 162 } catch (IndexOutOfRangeException) { 163 } 164 // 3) member variables 165 InOutStruct io = new InOutStruct(); 166 try { 167 io.numberin = -1; 168 throw new Exception("member csvarin not working"); 169 } catch (IndexOutOfRangeException) { 170 } 171 io.numberin = 5; 172 if (io.numberin != 5) 173 throw new Exception("member numberin not 5"); 174 io.numberout = 20; 175 try { 176 numberout += io.numberout; 177 throw new Exception("member csvarout not working"); 178 } catch (IndexOutOfRangeException) { 179 } 180 // test SWIG_exception macro - it must return from unmanaged code without executing any further unmanaged code 181 try { 182 csharp_exceptions.exceptionmacrotest(-1); 183 throw new Exception("exception macro not working"); 184 } catch (IndexOutOfRangeException) { 185 } 186 if (csharp_exceptions.exception_macro_run_flag) 187 throw new Exception("exceptionmacrotest was executed"); 188 189 // test all the types of exceptions 190 try { 191 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedApplicationException); 192 throw new Exception("ApplicationException not caught"); 193 } catch (ApplicationException e) { 194 if (e.Message != "msg") throw new Exception("ApplicationException msg incorrect"); 195 } 196 try { 197 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArithmeticException); 198 throw new Exception("ArithmeticException not caught"); 199 } catch (ArithmeticException e) { 200 if (e.Message != "msg") throw new Exception("ArithmeticException msg incorrect"); 201 } 202 try { 203 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedDivideByZeroException); 204 throw new Exception("DivideByZeroException not caught"); 205 } catch (DivideByZeroException e) { 206 if (e.Message != "msg") throw new Exception("DivideByZeroException msg incorrect"); 207 } 208 try { 209 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedIndexOutOfRangeException); 210 throw new Exception("IndexOutOfRangeException not caught"); 211 } catch (IndexOutOfRangeException e) { 212 if (e.Message != "msg") throw new Exception("IndexOutOfRangeException msg incorrect"); 213 } 214 try { 215 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedInvalidOperationException); 216 throw new Exception("InvalidOperationException not caught"); 217 } catch (InvalidOperationException e) { 218 if (e.Message != "msg") throw new Exception("InvalidOperationException msg incorrect"); 219 } 220 try { 221 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedIOException); 222 throw new Exception("IOException not caught"); 223 } catch (System.IO.IOException e) { 224 if (e.Message != "msg") throw new Exception("IOException msg incorrect"); 225 } 226 try { 227 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedNullReferenceException); 228 throw new Exception("NullReferenceException not caught"); 229 } catch (NullReferenceException e) { 230 if (e.Message != "msg") throw new Exception("NullReferenceException msg incorrect"); 231 } 232 try { 233 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedOutOfMemoryException); 234 throw new Exception("OutOfMemoryException not caught"); 235 } catch (OutOfMemoryException e) { 236 if (e.Message != "msg") throw new Exception("OutOfMemoryException msg incorrect"); 237 } 238 try { 239 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedOverflowException); 240 throw new Exception("OverflowException not caught"); 241 } catch (OverflowException e) { 242 if (e.Message != "msg") throw new Exception("OverflowException msg incorrect"); 243 } 244 try { 245 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedSystemException); 246 throw new Exception("SystemException not caught"); 247 } catch (SystemException e) { 248 if (e.Message != "msg") throw new Exception("SystemException msg incorrect"); 249 } 250 251 try { 252 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentException); 253 throw new Exception("ArgumentException not caught"); 254 } catch (ArgumentException e) { 255 if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentException msg incorrect"); 256 if (e.ParamName != "parm") throw new Exception("ArgumentException parm incorrect"); 257 } 258 try { 259 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentNullException); 260 throw new Exception("ArgumentNullException not caught"); 261 } catch (ArgumentNullException e) { 262 if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentNullException msg incorrect"); 263 if (e.ParamName != "parm") throw new Exception("ArgumentNullException parm incorrect"); 264 } 265 try { 266 csharp_exceptions.check_exception(UnmanagedExceptions.UnmanagedArgumentOutOfRangeException); 267 throw new Exception("ArgumentOutOfRangeException not caught"); 268 } catch (ArgumentOutOfRangeException e) { 269 if (e.Message.Replace(CRLF,"\n") != "msg\nParameter name: parm") throw new Exception("ArgumentOutOfRangeException msg incorrect"); 270 if (e.ParamName != "parm") throw new Exception("ArgumentOutOfRangeException parm incorrect"); 271 } 272 273 274 // exceptions in multiple threads test 275 { 276 ThrowsClass throwsClass = new ThrowsClass(1234.5678); 277 const int NUM_THREADS = 8; 278 Thread[] threads = new Thread[NUM_THREADS]; 279 TestThread[] testThreads = new TestThread[NUM_THREADS]; 280 // invoke the threads 281 for (int i=0; i<NUM_THREADS; i++) { 282 testThreads[i] = new TestThread(throwsClass, i); 283 threads[i] = new Thread(new ThreadStart(testThreads[i].Run)); 284 threads[i].Start(); 285 } 286 // wait for the threads to finish 287 for (int i=0; i<NUM_THREADS; i++) { 288 threads[i].Join(); 289 } 290 for (int i=0; i<NUM_THREADS; i++) { 291 if (testThreads[i].Failed) throw new Exception("Test Failed"); 292 } 293 } 294 295 296 // test inner exceptions 297 try { 298 csharp_exceptions.InnerExceptionTest(); 299 throw new Exception("InnerExceptionTest exception not caught"); 300 } catch (InvalidOperationException e) { 301 if (e.Message != "My OuterException message") throw new Exception("OuterException msg incorrect"); 302 if (e.InnerException.Message != "My InnerException message") throw new Exception("InnerException msg incorrect"); 303 } 304 } 305 public static string CRLF = "\r\n"; // Some CLR implementations use a CRLF instead of just CR 306} 307 308public class TestThread { 309 private int threadId; 310 private ThrowsClass throwsClass; 311 public bool Failed; 312 public TestThread(ThrowsClass t, int id) { 313 throwsClass = t; 314 threadId = id; 315 } 316 public void Run() { 317 Failed = false; 318 try { 319 for (int i=0; i<6000; i++) { // run test for about 10 seconds on a 1GHz machine (Mono) 320 try { 321 throwsClass.ThrowException(i); 322 throw new Exception("No exception thrown"); 323 } catch (ArgumentOutOfRangeException e) { 324 String expectedMessage = "caught:" + i + "\n" + "Parameter name: input"; 325 if (e.Message.Replace(runme.CRLF,"\n") != expectedMessage) 326 throw new Exception("Exception message incorrect. Expected:\n[" + expectedMessage + "]\n" + "Received:\n[" + e.Message + "]"); 327 if (e.ParamName != "input") 328 throw new Exception("Exception ParamName incorrect. Expected:\n[input]\n" + "Received:\n[" + e.ParamName + "]"); 329 if (e.InnerException != null) 330 throw new Exception("Unexpected inner exception"); 331 } 332 if (throwsClass.dub != 1234.5678) // simple check which attempts to catch memory corruption 333 throw new Exception("throwsException.dub = " + throwsClass.dub + " expected: 1234.5678"); 334 } 335 } catch (Exception e) { 336 Console.Error.WriteLine("Test failed (thread " + threadId + "): " + e.Message); 337 Failed = true; 338 } 339 } 340} 341 342