2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / interlocked-2.2.cs
blobb25f84a03feed954432f2db9f67d7c7d0cb2e7d1
1 using System;
2 using System.Threading;
4 public class InterlockTest
6 public int test;
7 public long ltest;
9 public static int Main() {
10 int a,b;
11 long la, lb;
13 InterlockTest it = new InterlockTest ();
15 /* int */
16 it.test = 2;
17 int c = Interlocked.Add (ref it.test, 1);
18 if (c != 3)
19 return 1;
21 if (it.test != 3)
22 return 2;
24 a = 1;
25 b = Interlocked.Add (ref a, 1);
26 if (a != 2)
27 return 3;
28 if (b != 2)
29 return 4;
31 /* long */
32 it.ltest = 2;
33 long lc = Interlocked.Add (ref it.ltest, 1);
34 if (lc != 3)
35 return 5;
37 if (it.ltest != 3)
38 return 6;
40 la = 1;
41 lb = Interlocked.Add (ref la, 1);
42 if (la != 2)
43 return 7;
44 if (lb != 2)
45 return 8;
47 if (Interlocked.Read (ref la) != 2)
48 return 9;
50 la = 1;
51 lc = Interlocked.Exchange (ref la, 2);
52 if (lc != 1)
53 return 10;
55 if (la != 2)
56 return 11;
58 /* Generics */
59 InterlockTest o1 = new InterlockTest ();
60 InterlockTest o2 = new InterlockTest ();
61 InterlockTest o = o1;
63 InterlockTest o3 = Interlocked.CompareExchange (ref o, o2, o2);
64 if (o3 != o1)
65 return 12;
66 if (o != o1)
67 return 13;
69 InterlockTest o4 = Interlocked.CompareExchange (ref o, o2, o1);
70 if (o4 != o1)
71 return 14;
72 if (o != o2)
73 return 15;
75 Console.WriteLine ("done!");
77 return 0;