Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / interlocked-2.2.cs
1 using System;
2 using System.Threading;
3
4 public class InterlockTest
5 {
6         public int test;
7         public long ltest;
8
9         public static int Main() {
10                 int a,b;
11                 long la, lb;
12
13                 InterlockTest it = new InterlockTest ();
14
15                 /* int */
16                 it.test = 2;
17                 int c = Interlocked.Add (ref it.test, 1);
18                 if (c != 3)
19                         return 1;
20
21                 if (it.test != 3)
22                         return 2;
23
24                 a = 1;
25                 b = Interlocked.Add (ref a, 1);
26                 if (a != 2)
27                         return 3;
28                 if (b != 2)
29                         return 4;
30
31                 /* long */
32                 it.ltest = 2;
33                 long lc = Interlocked.Add (ref it.ltest, 1);
34                 if (lc != 3)
35                         return 5;
36
37                 if (it.ltest != 3)
38                         return 6;
39
40                 la = 1;
41                 lb = Interlocked.Add (ref la, 1);
42                 if (la != 2)
43                         return 7;
44                 if (lb != 2)
45                         return 8;
46
47                 if (Interlocked.Read (ref la) != 2)
48                         return 9;
49
50                 la = 1;
51                 lc = Interlocked.Exchange (ref la, 2);
52                 if (lc != 1)
53                         return 10;
54
55                 if (la != 2)
56                         return 11;
57
58                 /* Generics */
59                 InterlockTest o1 = new InterlockTest ();
60                 InterlockTest o2 = new InterlockTest ();
61                 InterlockTest o = o1;
62
63                 InterlockTest o3 = Interlocked.CompareExchange (ref o, o2, o2);
64                 if (o3 != o1)
65                         return 12;
66                 if (o != o1)
67                         return 13;
68
69                 InterlockTest o4 = Interlocked.CompareExchange (ref o, o2, o1);
70                 if (o4 != o1)
71                         return 14;
72                 if (o != o2)
73                         return 15;
74
75                 /* long increment/decrement */
76                 la = 0x12345678;
77                 lb = Interlocked.Increment (ref la);
78                 if (la != 0x12345679)
79                         return 16;
80                 if (lb != 0x12345679)
81                         return 16;
82                 lb = Interlocked.Decrement (ref la);
83                 if (la != 0x12345678)
84                         return 17;
85                 if (lb != 0x12345678)
86                         return 18;              
87
88                 la = 1;
89                 lb = Interlocked.CompareExchange (ref la, 2, 1);
90                 if (la != 2)
91                         return 19;
92                 if (lb != 1)
93                         return 20;
94
95                 Console.WriteLine ("done!");
96
97                 return 0;
98         }
99 }