Merge pull request #1909 from esdrubal/reflection
[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
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
32
33 using System.Threading;
34 using System.Linq;
35
36 using NUnit;
37 using NUnit.Framework;
38 using NUnit.Framework.Constraints;
39
40 namespace MonoTests.System.Collections.Concurrent
41 {
42         [TestFixture]
43         public class ConcurrentBagTests
44         {
45                 ConcurrentBag<int> bag;
46                 
47                 [SetUp]
48                 public void Setup ()
49                 {
50                         bag = new ConcurrentBag<int> ();
51                 }
52
53                 [Test]
54                 public void BasicAddTakeTest ()
55                 {
56                         bag.Add (1);
57                         Assert.IsFalse (bag.IsEmpty);
58                         Assert.AreEqual (1, bag.Count);
59
60                         var array = bag.ToArray ();
61                         Assert.AreEqual (1, array.Length);
62                         Assert.AreEqual (1, array[0]);
63
64                         int result;
65                         Assert.IsTrue (bag.TryTake (out result));
66                         Assert.AreEqual (1, result);
67                         Assert.IsTrue (bag.IsEmpty);
68                 }
69
70                 [Test]
71                 public void BasicAddTakeFromOtherThread ()
72                 {
73                         var t = new Thread (() => bag.Add (1));
74                         t.Start ();
75                         Assert.IsTrue (t.Join (300));
76
77                         Assert.IsFalse (bag.IsEmpty);
78                         Assert.AreEqual (1, bag.Count);
79
80                         var array = bag.ToArray ();
81                         Assert.AreEqual (1, array.Length);
82                         Assert.AreEqual (1, array[0]);
83
84                         int result;
85                         Assert.IsTrue (bag.TryTake (out result));
86                         Assert.AreEqual (1, result);
87                         Assert.IsTrue (bag.IsEmpty);
88                 }
89
90                 [Test]
91                 public void AddFromMultipleThreadTakeFromOneThread ()
92                 {
93                         var threads = new Thread[10];
94                         for (int i = 0; i < threads.Length; i++) {
95                                 threads[i] = new Thread (() => bag.Add (1));
96                                 threads[i].Start ();
97                         }
98                         foreach (var t in threads)
99                                 Assert.IsTrue (t.Join (2000));
100
101                         Assert.IsFalse (bag.IsEmpty);
102                         Assert.AreEqual (threads.Length, bag.Count);
103
104                         var array = bag.ToArray ();
105                         Assert.AreEqual (threads.Length, array.Length);
106
107                         Assert.That (array, new CollectionEquivalentConstraint (Enumerable.Repeat (1, 10).ToArray ()), "#1, same");
108
109                         int result;
110                         for (int i = 0; i < threads.Length; i++) {
111                                 Assert.IsTrue (bag.TryTake (out result));
112                                 Assert.AreEqual (1, result);
113                         }
114                         Assert.IsTrue (bag.IsEmpty);
115                 }
116
117                 [Test]
118                 public void AddFromOneThreadTakeFromMultiple ()
119                 {
120                         var threads = new Thread[10];
121                         for (int i = 0; i < threads.Length; i++)
122                                 bag.Add (1);
123
124                         Assert.IsFalse (bag.IsEmpty);
125                         Assert.AreEqual (threads.Length, bag.Count);
126
127                         bool valid = true;
128
129                         for (int i = 0; i < threads.Length; i++) {
130                                 int result;
131                                 threads[i] = new Thread (() => valid &= bag.TryTake (out result) && result == 1);
132                                 threads[i].Start ();
133                         }
134
135                         foreach (var t in threads)
136                                 Assert.IsTrue (t.Join (200));
137
138                         Assert.IsTrue (valid, "Aggregate test");
139                 }
140
141                 [Test]
142                 public void BasicAddPeekTest ()
143                 {
144                         bag.Add (1);
145                         Assert.IsFalse (bag.IsEmpty);
146                         Assert.AreEqual (1, bag.Count);
147
148                         int result;
149                         Assert.IsTrue (bag.TryPeek (out result));
150                         Assert.AreEqual (1, result);
151                         Assert.IsFalse (bag.IsEmpty);
152                 }
153
154                 [Test]
155                 public void BasicAddPeekFromOtherThread ()
156                 {
157                         var t = new Thread (() => bag.Add (1));
158                         t.Start ();
159                         Assert.IsTrue (t.Join (300));
160
161                         Assert.IsFalse (bag.IsEmpty);
162                         Assert.AreEqual (1, bag.Count);
163
164                         int result;
165                         Assert.IsTrue (bag.TryPeek (out result));
166                         Assert.AreEqual (1, result);
167                         Assert.IsFalse (bag.IsEmpty);
168                 }
169
170                 [Test]
171                 public void AddFromOneThreadPeekFromMultiple ()
172                 {
173                         var threads = new Thread[10];
174                         for (int i = 0; i < threads.Length; i++)
175                                 bag.Add (1);
176
177                         Assert.IsFalse (bag.IsEmpty);
178                         Assert.AreEqual (threads.Length, bag.Count);
179
180                         bool valid = true;
181
182                         for (int i = 0; i < threads.Length; i++) {
183                                 int result;
184                                 threads[i] = new Thread (() => valid &= bag.TryPeek (out result) && result == 1);
185                                 threads[i].Start ();
186                         }
187
188                         foreach (var t in threads)
189                                 Assert.IsTrue (t.Join (200));
190
191                         Assert.IsTrue (valid, "Aggregate test");
192                 }
193
194         [Test]
195         public void BasicRemoveEmptyTest ()
196         {
197             int result;
198             Assert.IsTrue(bag.IsEmpty);
199             Assert.IsFalse(bag.TryTake(out result));
200         }
201
202         [Test]
203         public void BasicRemoveTwiceTest()
204         {
205             bag.Add (1);
206             Assert.IsFalse (bag.IsEmpty);
207             Assert.AreEqual (1, bag.Count);
208
209             int result;
210             Assert.IsTrue (bag.TryTake (out result));
211             Assert.AreEqual (1, result);
212             Assert.IsTrue (bag.IsEmpty);
213             Assert.IsFalse (bag.TryTake (out result));
214             Assert.IsFalse (bag.TryTake (out result));
215         }
216
217         [Test]
218         public void AddRemoveAddTest()
219         {
220             bag.Add (1);
221             Assert.IsFalse (bag.IsEmpty);
222             Assert.AreEqual (1, bag.Count);
223
224             int result;
225             Assert.IsTrue (bag.TryTake (out result));
226             Assert.AreEqual (1, result);
227             Assert.IsTrue (bag.IsEmpty);
228
229             bag.Add (1);
230             Assert.IsFalse (bag.IsEmpty);
231             Assert.AreEqual (1, bag.Count);
232
233             Assert.IsTrue (bag.TryTake (out result));
234             Assert.AreEqual (1, result);
235             Assert.IsTrue (bag.IsEmpty);
236         }
237                 
238                 [Test]
239                 public void AddStressTest ()
240                 {
241                         CollectionStressTestHelper.AddStressTest (bag);
242                 }
243                 
244                 [Test]
245                 public void RemoveStressTest ()
246                 {
247                         CollectionStressTestHelper.RemoveStressTest (bag, CheckOrderingType.DontCare);
248                 }
249
250                 [Test]
251                 public void Bug24213 ()
252                 {
253                         var size = 2049;
254                         var bag = new ConcurrentBag<int> ();
255                         for (int i = 0; i < size; i++)
256                                 bag.Add (i);
257
258                         var array = bag.ToArray ();
259
260                         Assert.AreEqual (size, array.Length, "#1");
261
262                         for (int i = 0; i < size; i++)
263                                 Assert.AreEqual (size - 1 - i, array [i], "#C" + i);
264                 }
265         }
266 }