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