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