Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentStackTests.cs
1 // ConcurrentStackTests.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.Threading;
27 using System.Linq;
28 using System.Collections.Concurrent;
29 using NUnit.Framework;
30 using NUnit.Framework.Constraints;
31
32 namespace MonoTests.System.Collections.Concurrent
33 {
34         [TestFixture()]
35         public class ConcurrentStackTests
36         {
37                 ConcurrentStack<int> stack;
38                 
39                 [SetUpAttribute]
40                 public void Setup()
41                 {
42                         stack = new ConcurrentStack<int>();
43                         for (int i = 0; i < 10; i++) {
44                                 stack.Push(i);
45                         }
46                 }
47                 
48                 [Test]
49                 public void StressPushTestCase ()
50                 {
51                         /*ParallelTestHelper.Repeat (delegate {
52                                 stack = new ConcurrentStack<int> ();
53                                 int amount = -1;
54                                 const int count = 10;
55                                 const int threads = 5;
56                                 
57                                 ParallelTestHelper.ParallelStressTest (stack, (q) => {
58                                         int t = Interlocked.Increment (ref amount);
59                                         for (int i = 0; i < count; i++)
60                                                 stack.Push (t);
61                                 }, threads);
62                                 
63                                 Assert.AreEqual (threads * count, stack.Count, "#-1");
64                                 int[] values = new int[threads];
65                                 int temp;
66                                 while (stack.TryPop (out temp)) {
67                                         values[temp]++;
68                                 }
69                                 
70                                 for (int i = 0; i < threads; i++)
71                                         Assert.AreEqual (count, values[i], "#" + i);
72                         });*/
73                         CollectionStressTestHelper.AddStressTest (new ConcurrentStack<int> ());
74                 }
75                 
76                 [Test]
77                 public void StressPopTestCase ()
78                 {
79                         /*ParallelTestHelper.Repeat (delegate {
80                                 stack = new ConcurrentStack<int> ();
81                                 const int count = 10;
82                                 const int threads = 5;
83                                 const int delta = 5;
84                                 
85                                 for (int i = 0; i < (count + delta) * threads; i++)
86                                         stack.Push (i);
87                                 
88                                 bool state = true;
89                                 
90                                 ParallelTestHelper.ParallelStressTest (stack, (q) => {
91                                         int t;
92                                         for (int i = 0; i < count; i++)
93                                                 state &= stack.TryPop (out t);
94                                 }, threads);
95                                 
96                                 Assert.IsTrue (state, "#1");
97                                 Assert.AreEqual (delta * threads, stack.Count, "#2");
98                                 
99                                 string actual = string.Empty;
100                                 int temp;
101                                 while (stack.TryPop (out temp)) {
102                                         actual += temp;
103                                 }
104                                 string expected = Enumerable.Range (0, delta * threads).Reverse()
105                                         .Aggregate (string.Empty, (acc, v) => acc + v);
106                                 
107                                 Assert.AreEqual (expected, actual, "#3");
108                         });*/
109                         
110                         CollectionStressTestHelper.RemoveStressTest (new ConcurrentStack<int> (), CheckOrderingType.Reversed);
111                 }
112                 
113                 [Test]
114                 public void CountTestCase()
115                 {
116                         Assert.IsTrue(stack.Count == 10, "#1");
117                         int value;
118                         stack.TryPeek(out value);
119                         stack.TryPop(out value);
120                         stack.TryPop(out value);
121                         Assert.IsTrue(stack.Count == 8, "#2");
122                         stack.Clear();
123                         Assert.IsTrue(stack.Count == 0, "#3");
124                         Assert.IsTrue(stack.IsEmpty, "#4");
125                 }
126                 
127                 [Test()]
128                 public void EnumerateTestCase()
129                 {
130                         string s = string.Empty;
131                         foreach (int i in stack) {
132                                 s += i;
133                         }
134                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
135                 }
136                 
137                 [Test()]
138                 public void TryPeekTestCase()
139                 {
140                         int value;
141                         stack.TryPeek(out value);
142                         Assert.IsTrue(value == 9, "#1 : " + value);
143                         stack.TryPop(out value);
144                         Assert.IsTrue(value == 9, "#2 : " + value);
145                         stack.TryPop(out value);
146                         Assert.IsTrue(value == 8, "#3 : " + value);
147                         stack.TryPeek(out value);
148                         Assert.IsTrue(value == 7, "#4 : " + value);
149                         stack.TryPeek(out value);
150                         Assert.IsTrue(value == 7, "#5 : " + value);
151                 }
152                 
153                 [Test()]
154                 public void TryPopTestCase()
155                 {
156                         int value;
157                         stack.TryPeek(out value);
158                         Assert.IsTrue(value == 9, "#1");
159                         stack.TryPop(out value);
160                         stack.TryPop(out value);
161                         Assert.IsTrue(value == 8, "#2 : " + value);
162                 }
163                 
164                 [Test()]
165                 public void TryPopEmptyTestCase()
166                 {
167                         int value;
168                         stack.Clear();
169                         stack.Push(1);
170                         Assert.IsTrue(stack.TryPop(out value), "#1");
171                         Assert.IsFalse(stack.TryPop(out value), "#2");
172                         Assert.IsTrue(stack.IsEmpty, "#3");
173                 }
174                 
175                 [Test]
176                 public void ToArrayTest()
177                 {
178                         int[] array = stack.ToArray();
179                         string s = string.Empty;
180                         foreach (int i in array) {
181                                 s += i;
182                         }
183                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
184                         stack.CopyTo(array, 0);
185                         s = string.Empty;
186                         foreach (int i in array) {
187                                 s += i;
188                         }
189                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
190                 }
191
192                 [Test, ExpectedException (typeof (ArgumentNullException))]
193                 public void ToExistingArray_Null ()
194                 {
195                         stack.CopyTo (null, 0);
196                 }
197
198                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
199                 public void ToExistingArray_OutOfRange ()
200                 {
201                         stack.CopyTo (new int[3], -1);
202                 }
203
204                 [Test, ExpectedException (typeof (ArgumentException))]
205                 public void ToExistingArray_IndexOverflow ()
206                 {
207                         stack.CopyTo (new int[3], 4);
208                 }
209
210                 [Test, ExpectedException (typeof (ArgumentException))]
211                 public void ToExistingArray_Overflow ()
212                 {
213                         stack.CopyTo (new int[3], 0);
214                 }
215
216                 [Test]
217                 public void TryPopRangeTest ()
218                 {
219                         int[] values = new int[3];
220                         Assert.AreEqual (3, stack.TryPopRange (values));
221                         Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9, 8, 7 }));
222                         Assert.AreEqual (10 - values.Length, stack.Count);
223                         for (int i = 9 - values.Length; i >= 0; i--) {
224                                 int outValue;
225                                 Assert.IsTrue (stack.TryPop (out outValue));
226                                 Assert.AreEqual (i, outValue);
227                         }
228                 }
229
230                 [Test]
231                 public void TryPopRangeEmpty ()
232                 {
233                         stack = new ConcurrentStack<int>();
234                         Assert.AreEqual (0, stack.TryPopRange (new int [1]));
235                 }
236
237                 [Test]
238                 public void TryPopRangeTestWithOneElement ()
239                 {
240                         int[] values = new int[1];
241                         Assert.AreEqual (1, stack.TryPopRange (values));
242                         Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9 }));
243                         Assert.AreEqual (10 - values.Length, stack.Count);
244                         for (int i = 9 - values.Length; i >= 0; i--) {
245                                 int outValue;
246                                 Assert.IsTrue (stack.TryPop (out outValue));
247                                 Assert.AreEqual (i, outValue);
248                         }
249                 }
250
251                 [Test]
252                 public void TryPopRangeFullTest ()
253                 {
254                         int[] values = new int[10];
255                         Assert.AreEqual (10, stack.TryPopRange (values));
256                         Assert.That (values, new CollectionEquivalentConstraint (Enumerable.Range (0, 10).Reverse ()));
257                         Assert.AreEqual (0, stack.Count);
258                 }
259
260                 [Test]
261                 public void TryPopRangePartialFillTest ()
262                 {
263                         int[] values = new int[5];
264                         Assert.AreEqual (2, stack.TryPopRange (values, 3, 2));
265                         Assert.That (values, new CollectionEquivalentConstraint (new int[] { 0, 0, 0, 9, 8 }));
266                         Assert.AreEqual (8, stack.Count);
267                 }
268
269                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
270                 public void TryPopRange_NegativeIndex ()
271                 {
272                         stack.TryPopRange (new int[3], -2, 3);
273                 }
274
275                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
276                 public void TryPopRange_LargeIndex ()
277                 {
278                         stack.TryPopRange (new int[3], 200, 3);
279                 }
280
281                 [Test, ExpectedException (typeof (ArgumentException))]
282                 public void TryPopRange_LargeCount ()
283                 {
284                         stack.TryPopRange (new int[3], 2, 5);
285                 }
286
287                 [Test, ExpectedException (typeof (ArgumentNullException))]
288                 public void TryPopRange_NullArray ()
289                 {
290                         stack.TryPopRange (null);
291                 }
292                 
293                 [Test]
294                 public void PushRangeTestCase()
295                 {
296                         var testStack = new ConcurrentStack<int>();
297                         
298                         var testData = new int[] { 1, 2, 3, 4, 5 };                     
299                         testStack.PushRange (testData);
300                         
301                         Assert.AreEqual (testData.Length, testStack.Count);
302                 }
303         }
304 }