[SRE] Improved token fixups processing.
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / ConcurrentBagTests.cs
1 // 
2 // ConcurrentBagTests.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 #if NET_4_0
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 using System.Threading;
35 using System.Linq;
36
37 using NUnit;
38 using NUnit.Framework;
39 using NUnit.Framework.Constraints;
40
41 namespace MonoTests.System.Collections.Concurrent
42 {
43         [TestFixture]
44         public class ConcurrentBagTests
45         {
46                 ConcurrentBag<int> bag;
47                 
48                 [SetUp]
49                 public void Setup ()
50                 {
51                         bag = new ConcurrentBag<int> ();
52                 }
53
54                 [Test]
55                 public void BasicAddTakeTest ()
56                 {
57                         bag.Add (1);
58                         Assert.IsFalse (bag.IsEmpty);
59                         Assert.AreEqual (1, bag.Count);
60
61                         var array = bag.ToArray ();
62                         Assert.AreEqual (1, array.Length);
63                         Assert.AreEqual (1, array[0]);
64
65                         int result;
66                         Assert.IsTrue (bag.TryTake (out result));
67                         Assert.AreEqual (1, result);
68                         Assert.IsTrue (bag.IsEmpty);
69                 }
70
71                 [Test]
72                 public void BasicAddTakeFromOtherThread ()
73                 {
74                         var t = new Thread (() => bag.Add (1));
75                         t.Start ();
76                         Assert.IsTrue (t.Join (300));
77
78                         Assert.IsFalse (bag.IsEmpty);
79                         Assert.AreEqual (1, bag.Count);
80
81                         var array = bag.ToArray ();
82                         Assert.AreEqual (1, array.Length);
83                         Assert.AreEqual (1, array[0]);
84
85                         int result;
86                         Assert.IsTrue (bag.TryTake (out result));
87                         Assert.AreEqual (1, result);
88                         Assert.IsTrue (bag.IsEmpty);
89                 }
90
91                 [Test]
92                 public void AddFromMultipleThreadTakeFromOneThread ()
93                 {
94                         var threads = new Thread[10];
95                         for (int i = 0; i < threads.Length; i++) {
96                                 threads[i] = new Thread (() => bag.Add (1));
97                                 threads[i].Start ();
98                         }
99                         foreach (var t in threads)
100                                 Assert.IsTrue (t.Join (2000));
101
102                         Assert.IsFalse (bag.IsEmpty);
103                         Assert.AreEqual (threads.Length, bag.Count);
104
105                         var array = bag.ToArray ();
106                         Assert.AreEqual (threads.Length, array.Length);
107
108                         Assert.That (array, new CollectionEquivalentConstraint (Enumerable.Repeat (1, 10).ToArray ()), "#1, same");
109
110                         int result;
111                         for (int i = 0; i < threads.Length; i++) {
112                                 Assert.IsTrue (bag.TryTake (out result));
113                                 Assert.AreEqual (1, result);
114                         }
115                         Assert.IsTrue (bag.IsEmpty);
116                 }
117
118                 [Test]
119                 public void AddFromOneThreadTakeFromMultiple ()
120                 {
121                         var threads = new Thread[10];
122                         for (int i = 0; i < threads.Length; i++)
123                                 bag.Add (1);
124
125                         Assert.IsFalse (bag.IsEmpty);
126                         Assert.AreEqual (threads.Length, bag.Count);
127
128                         bool valid = true;
129
130                         for (int i = 0; i < threads.Length; i++) {
131                                 int result;
132                                 threads[i] = new Thread (() => valid &= bag.TryTake (out result) && result == 1);
133                                 threads[i].Start ();
134                         }
135
136                         foreach (var t in threads)
137                                 Assert.IsTrue (t.Join (200));
138
139                         Assert.IsTrue (valid, "Aggregate test");
140                 }
141
142                 [Test]
143                 public void BasicAddPeekTest ()
144                 {
145                         bag.Add (1);
146                         Assert.IsFalse (bag.IsEmpty);
147                         Assert.AreEqual (1, bag.Count);
148
149                         int result;
150                         Assert.IsTrue (bag.TryPeek (out result));
151                         Assert.AreEqual (1, result);
152                         Assert.IsFalse (bag.IsEmpty);
153                 }
154
155                 [Test]
156                 public void BasicAddPeekFromOtherThread ()
157                 {
158                         var t = new Thread (() => bag.Add (1));
159                         t.Start ();
160                         Assert.IsTrue (t.Join (300));
161
162                         Assert.IsFalse (bag.IsEmpty);
163                         Assert.AreEqual (1, bag.Count);
164
165                         int result;
166                         Assert.IsTrue (bag.TryPeek (out result));
167                         Assert.AreEqual (1, result);
168                         Assert.IsFalse (bag.IsEmpty);
169                 }
170
171                 [Test]
172                 public void AddFromOneThreadPeekFromMultiple ()
173                 {
174                         var threads = new Thread[10];
175                         for (int i = 0; i < threads.Length; i++)
176                                 bag.Add (1);
177
178                         Assert.IsFalse (bag.IsEmpty);
179                         Assert.AreEqual (threads.Length, bag.Count);
180
181                         bool valid = true;
182
183                         for (int i = 0; i < threads.Length; i++) {
184                                 int result;
185                                 threads[i] = new Thread (() => valid &= bag.TryPeek (out result) && result == 1);
186                                 threads[i].Start ();
187                         }
188
189                         foreach (var t in threads)
190                                 Assert.IsTrue (t.Join (200));
191
192                         Assert.IsTrue (valid, "Aggregate test");
193                 }
194
195         [Test]
196         public void BasicRemoveEmptyTest ()
197         {
198             int result;
199             Assert.IsTrue(bag.IsEmpty);
200             Assert.IsFalse(bag.TryTake(out result));
201         }
202
203         [Test]
204         public void BasicRemoveTwiceTest()
205         {
206             bag.Add (1);
207             Assert.IsFalse (bag.IsEmpty);
208             Assert.AreEqual (1, bag.Count);
209
210             int result;
211             Assert.IsTrue (bag.TryTake (out result));
212             Assert.AreEqual (1, result);
213             Assert.IsTrue (bag.IsEmpty);
214             Assert.IsFalse (bag.TryTake (out result));
215             Assert.IsFalse (bag.TryTake (out result));
216         }
217
218         [Test]
219         public void AddRemoveAddTest()
220         {
221             bag.Add (1);
222             Assert.IsFalse (bag.IsEmpty);
223             Assert.AreEqual (1, bag.Count);
224
225             int result;
226             Assert.IsTrue (bag.TryTake (out result));
227             Assert.AreEqual (1, result);
228             Assert.IsTrue (bag.IsEmpty);
229
230             bag.Add (1);
231             Assert.IsFalse (bag.IsEmpty);
232             Assert.AreEqual (1, bag.Count);
233
234             Assert.IsTrue (bag.TryTake (out result));
235             Assert.AreEqual (1, result);
236             Assert.IsTrue (bag.IsEmpty);
237         }
238                 
239                 [Test]
240                 public void AddStressTest ()
241                 {
242                         CollectionStressTestHelper.AddStressTest (bag);
243                 }
244                 
245                 [Test]
246                 public void RemoveStressTest ()
247                 {
248                         CollectionStressTestHelper.RemoveStressTest (bag, CheckOrderingType.DontCare);
249                 }
250
251                 [Test]
252                 public void Bug24213 ()
253                 {
254                         var size = 2049;
255                         var bag = new ConcurrentBag<int> ();
256                         for (int i = 0; i < size; i++)
257                                 bag.Add (i);
258
259                         var array = bag.ToArray ();
260
261                         Assert.AreEqual (size, array.Length, "#1");
262
263                         for (int i = 0; i < size; i++)
264                                 Assert.AreEqual (size - 1 - i, array [i], "#C" + i);
265                 }
266         }
267 }
268 #endif