Merge pull request #1870 from saper/langinfo_h
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentQueueTests.cs
1 // ConcurrentQueueTest.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24
25 using System;
26 using System.Linq;
27 using System.Threading;
28 using System.Collections.Generic;
29 using System.Collections.Concurrent;
30
31 using NUnit.Framework;
32 using MonoTests.System.Threading.Tasks;
33
34 namespace MonoTests.System.Collections.Concurrent
35 {
36         
37         
38         [TestFixture()]
39         public class ConcurrentQueueTests
40         {
41                 ConcurrentQueue<int> queue;
42                 
43                 [SetUpAttribute]
44                 public void Setup()
45                 {
46                         queue = new ConcurrentQueue<int>();
47                         for (int i = 0; i < 10; i++) {
48                                 queue.Enqueue(i);
49                         }
50                 }
51                 
52                 [Test]
53                 public void StressEnqueueTestCase ()
54                 {
55                         /*ParallelTestHelper.Repeat (delegate {
56                                 queue = new ConcurrentQueue<int> ();
57                                 int amount = -1;
58                                 const int count = 10;
59                                 const int threads = 5;
60                                 
61                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
62                                         int t = Interlocked.Increment (ref amount);
63                                         for (int i = 0; i < count; i++)
64                                                 queue.Enqueue (t);
65                                 }, threads);
66                                 
67                                 Assert.AreEqual (threads * count, queue.Count, "#-1");
68                                 int[] values = new int[threads];
69                                 int temp;
70                                 while (queue.TryDequeue (out temp)) {
71                                         values[temp]++;
72                                 }
73                                 
74                                 for (int i = 0; i < threads; i++)
75                                         Assert.AreEqual (count, values[i], "#" + i);
76                         });*/
77                         
78                         CollectionStressTestHelper.AddStressTest (new ConcurrentQueue<int> ());
79                 }
80                 
81                 [Test]
82                 public void StressDequeueTestCase ()
83                 {
84                         /*ParallelTestHelper.Repeat (delegate {
85                                 queue = new ConcurrentQueue<int> ();
86                                 const int count = 10;
87                                 const int threads = 5;
88                                 const int delta = 5;
89                                 
90                                 for (int i = 0; i < (count + delta) * threads; i++)
91                                         queue.Enqueue (i);
92                                 
93                                 bool state = true;
94                                 
95                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
96                                         int t;
97                                         for (int i = 0; i < count; i++)
98                                                 state &= queue.TryDequeue (out t);
99                                 }, threads);
100                                 
101                                 Assert.IsTrue (state, "#1");
102                                 Assert.AreEqual (delta * threads, queue.Count, "#2");
103                                 
104                                 string actual = string.Empty;
105                                 int temp;
106                                 while (queue.TryDequeue (out temp)) {
107                                         actual += temp;
108                                 }
109                                 string expected = Enumerable.Range (count * threads, delta * threads)
110                                         .Aggregate (string.Empty, (acc, v) => acc + v);
111                                 
112                                 Assert.AreEqual (expected, actual, "#3");
113                         });*/
114                         
115                         CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
116                 }
117                 
118                 [Test]
119                 public void StressTryPeekTestCase ()
120                 {
121                         ParallelTestHelper.Repeat (delegate {
122                                 var queue = new ConcurrentQueue<object> ();
123                                 queue.Enqueue (new object());
124                                 
125                                 const int threads = 10;
126                                 int threadCounter = 0;
127                                 bool success = true;
128                                 
129                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
130                                         int threadId = Interlocked.Increment (ref threadCounter);
131                                         object temp;
132                                         if (threadId < threads)
133                                         {
134                                                 while (queue.TryPeek (out temp))
135                                                         if (temp == null)
136                                                                 success = false;
137                                         } else {
138                                                 queue.TryDequeue (out temp);
139                                         }
140                                 }, threads);
141                                 
142                                 Assert.IsTrue (success, "TryPeek returned unexpected null value.");
143                         }, 10);
144                 }
145                 
146                 [Test]
147                 public void CountTestCase()
148                 {
149                         Assert.AreEqual(10, queue.Count, "#1");
150                         int value;
151                         queue.TryPeek(out value);
152                         queue.TryDequeue(out value);
153                         queue.TryDequeue(out value);
154                         Assert.AreEqual(8, queue.Count, "#2");
155                 }
156                 
157                 //[Ignore]
158                 [Test]
159                 public void EnumerateTestCase()
160                 {
161                         string s = string.Empty;
162                         foreach (int i in queue) {
163                                 s += i;
164                         }
165                         Assert.AreEqual("0123456789", s, "#1 : " + s);
166                 }
167                 
168                 [Test()]
169                 public void TryPeekTestCase()
170                 {
171                         int value;
172                         queue.TryPeek(out value);
173                         Assert.AreEqual(0, value, "#1 : " + value);
174                         queue.TryDequeue(out value);
175                         Assert.AreEqual(0, value, "#2 : " + value);
176                         queue.TryDequeue(out value);
177                         Assert.AreEqual(1, value, "#3 : " + value);
178                         queue.TryPeek(out value);
179                         Assert.AreEqual(2, value, "#4 : " + value);
180                         queue.TryPeek(out value);
181                         Assert.AreEqual(2, value, "#5 : " + value);
182                 }
183                 
184                 [Test()]
185                 public void TryDequeueTestCase()
186                 {
187                         int value;
188                         queue.TryPeek(out value);
189                         Assert.AreEqual(0, value, "#1");
190                         Assert.IsTrue(queue.TryDequeue(out value), "#2");
191                         Assert.IsTrue(queue.TryDequeue(out value), "#3");
192                         Assert.AreEqual(1, value, "#4");
193                 }
194                 
195                 [Test()]
196                 public void TryDequeueEmptyTestCase()
197                 {
198                         int value;
199                         queue = new ConcurrentQueue<int> ();
200                         queue.Enqueue(1);
201                         Assert.IsTrue(queue.TryDequeue(out value), "#1");
202                         Assert.IsFalse(queue.TryDequeue(out value), "#2");
203                         Assert.IsTrue(queue.IsEmpty, "#3");
204                 }
205                 
206                 [Test]
207                 public void ToArrayTest()
208                 {
209                         int[] array = queue.ToArray();
210                         string s = string.Empty;
211                         foreach (int i in array) {
212                                 s += i;
213                         }
214                         Assert.AreEqual("0123456789", s, "#1 : " + s);
215                         queue.CopyTo(array, 0);
216                         s = string.Empty;
217                         foreach (int i in array) {
218                                 s += i;
219                         }
220                         Assert.AreEqual("0123456789", s, "#2 : " + s);
221                 }
222
223                 [Test, ExpectedException (typeof (ArgumentNullException))]
224                 public void ToExistingArray_Null ()
225                 {
226                         queue.CopyTo (null, 0);
227                 }
228
229                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
230                 public void ToExistingArray_OutOfRange ()
231                 {
232                         queue.CopyTo (new int[3], -1);
233                 }
234
235                 [Test, ExpectedException (typeof (ArgumentException))]
236                 public void ToExistingArray_IndexOverflow ()
237                 {
238                         queue.CopyTo (new int[3], 4);
239                 }
240
241                 [Test, ExpectedException (typeof (ArgumentException))]
242                 public void ToExistingArray_Overflow ()
243                 {
244                         queue.CopyTo (new int[3], 0);
245                 }
246
247                 static WeakReference CreateWeakReference (object obj)
248                 {
249                         return new WeakReference (obj);
250                 }
251
252                 [Test]
253                 // This depends on precise stack scanning
254                 [Category ("NotWorking")]
255                 public void TryDequeueReferenceTest ()
256                 {
257                         var obj = new Object ();
258                         var weakReference = CreateWeakReference(obj);
259                         var queue = new ConcurrentQueue<object> ();
260
261                         queue.Enqueue (obj);
262                         queue.TryDequeue (out obj);
263                         obj = null;
264
265                         GC.Collect ();
266                         GC.WaitForPendingFinalizers ();
267
268                         Assert.IsFalse (weakReference.IsAlive);
269                 }
270         }
271 }