PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/csharp_exceptions_runme.cs

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