PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 113 lines | 97 code | 8 blank | 8 comment | 19 complexity | 62062e9890f4347847f2f6a04d3b3eb3 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_typemapsNamespace;
  4. public class runme
  5. {
  6. static void Main()
  7. {
  8. // Test the C# types customisation by modifying the default char * typemaps to return a single char
  9. Things things = new Things();
  10. System.Text.StringBuilder initialLetters = new System.Text.StringBuilder();
  11. char myChar = things.start("boo");
  12. initialLetters.Append(myChar);
  13. myChar = Things.stop("hiss");
  14. initialLetters.Append(myChar);
  15. myChar = csharp_typemaps.partyon("off");
  16. initialLetters.Append(myChar);
  17. if (initialLetters.ToString() != "bho")
  18. throw new Exception("initial letters failed");
  19. // $csinput expansion
  20. csharp_typemaps.myInt = 1;
  21. try {
  22. csharp_typemaps.myInt = -1;
  23. throw new Exception("oops");
  24. } catch (ApplicationException) {
  25. }
  26. // Eager garbage collector test
  27. {
  28. const int NUM_THREADS = 8;
  29. Thread[] threads = new Thread[NUM_THREADS];
  30. TestThread[] testThreads = new TestThread[NUM_THREADS];
  31. // invoke the threads
  32. for (int i=0; i<NUM_THREADS; i++) {
  33. testThreads[i] = new TestThread(i);
  34. threads[i] = new Thread(new ThreadStart(testThreads[i].Run));
  35. threads[i].Start();
  36. }
  37. // wait for the threads to finish
  38. for (int i=0; i<NUM_THREADS; i++) {
  39. threads[i].Join();
  40. }
  41. for (int i=0; i<NUM_THREADS; i++) {
  42. if (testThreads[i].Failed) throw new Exception("Test Failed");
  43. }
  44. }
  45. }
  46. }
  47. public class TestThread {
  48. private int threadId;
  49. public bool Failed;
  50. public TestThread(int id) {
  51. threadId = id;
  52. }
  53. public void Run() {
  54. Failed = false;
  55. try {
  56. // Older versions of SWIG used IntPtr instead of HandleRef to hold the underlying
  57. // C++ pointer, so this test would (usually) fail as the garbage collector would
  58. // sometimes collect the Number class while it was being used in unmanaged code
  59. for (int i=0; i<5000; i++) { // run test for a few seconds
  60. {
  61. Obj obj = new Obj();
  62. Number n = new Number(i);
  63. Number triple = obj.triple(n);
  64. if (triple.Value != i*3)
  65. throw new ApplicationException("triple failed: " + triple.Value);
  66. }
  67. {
  68. Obj obj = new Obj();
  69. Number n = new Number(i);
  70. Number times6 = obj.times6(n);
  71. if (times6.Value != i*6)
  72. throw new ApplicationException("times6 failed: " + times6.Value);
  73. }
  74. {
  75. Obj obj = new Obj();
  76. Number n = new Number(i);
  77. Number times9 = obj.times9(n);
  78. if (times9.Value != i*9)
  79. throw new ApplicationException("times9 failed: " + times9.Value);
  80. }
  81. {
  82. Number n = new Number(i);
  83. Number quadruple = csharp_typemaps.quadruple(n);
  84. if (quadruple.Value != i*4)
  85. throw new ApplicationException("quadruple failed: " + quadruple.Value);
  86. }
  87. {
  88. Number n = new Number(i);
  89. Number times8 = csharp_typemaps.times8(n);
  90. if (times8.Value != i*8)
  91. throw new ApplicationException("times8 failed: " + times8.Value);
  92. }
  93. {
  94. Number n = new Number(i);
  95. Number times12 = csharp_typemaps.times12(n);
  96. if (times12.Value != i*12)
  97. throw new ApplicationException("times12 failed: " + times12.Value);
  98. }
  99. }
  100. } catch (Exception e) {
  101. Console.Error.WriteLine("Test failed (thread " + threadId + "): " + e.Message);
  102. Failed = true;
  103. }
  104. }
  105. }