summaryrefslogtreecommitdiff
path: root/Examples/test-suite/csharp/threads_runme.cs
blob: fa901990cc887e3fbe90a50f91a711de4a7d37dc (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
using System;
using System.Threading;
using threadsNamespace;

public class runme
{
    static void Main() 
    {
      Kerfuffle kerf = new Kerfuffle();
      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(kerf, 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");
      }
    }
}

public class TestThread {
   private int threadId;
   private Kerfuffle kerf;
   public bool Failed;
   public TestThread(Kerfuffle t, int id) {
       kerf = t;
       threadId = id;
   }
   public void Run() {
     Failed = false;
     try {
       for (int i=0; i<30000; i++) { // run test for a few seconds on a 1GHz machine
         string given = "This is the test string that should come back. A number: " + i;
         string received = kerf.StdString(given);
         if (received != given) {
           throw new ApplicationException("StdString string does not match. Received:\n[" + received + "].\nExpected:\n{" + given + "}");
         }
       }
       for (int i=0; i<30000; i++) { // run test for a few seconds on a 1GHz machine
         string given = "This is the test string that should come back. A number: " + i;
         string received = kerf.CharString(given);
         if (received != given) {
           throw new ApplicationException("StdString string does not match. Received:\n[" + received + "].\nExpected:\n{" + given + "}");
         }
       }
     } catch (Exception e) {
       Console.Error.WriteLine("Test failed (thread " + threadId + "): " + e.Message);
       Failed = true;
     }
   }
}