New test.
[mono.git] / mcs / class / corlib / Test / System.Threading.Collections / ConcurrentStackTests.cs
1 #if NET_4_0
2 // ConcurrentStackRe.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.Collections;
28 using NUnit.Framework;
29
30 namespace ParallelFxTests
31 {
32         [TestFixture()]
33         public class ConcurrentStackTests
34         {
35                 ConcurrentStack<int> stack;
36                 
37                 [SetUpAttribute]
38                 public void Setup()
39                 {
40                         stack = new ConcurrentStack<int>();
41                         for (int i = 0; i < 10; i++) {
42                                 stack.Push(i);
43                         }
44                 }
45                 
46                 [Test]
47                 public void CountTestCase()
48                 {
49                         Assert.IsTrue(stack.Count == 10, "#1");
50                         int value;
51                         stack.TryPeek(out value);
52                         stack.TryPop(out value);
53                         stack.TryPop(out value);
54                         Assert.IsTrue(stack.Count == 8, "#2");
55                         stack.Clear();
56                         Assert.IsTrue(stack.Count == 0, "#3");
57                         Assert.IsTrue(stack.IsEmpty, "#4");
58                 }
59                 
60                 //[Ignore]
61                 [Test()]
62                 public void EnumerateTestCase()
63                 {
64                         string s = string.Empty;
65                         foreach (int i in stack) {
66                                 s += i;
67                         }
68                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
69                 }
70                 
71                 [Test()]
72                 public void TryPeekTestCase()
73                 {
74                         int value;
75                         stack.TryPeek(out value);
76                         Assert.IsTrue(value == 9, "#1 : " + value);
77                         stack.TryPop(out value);
78                         Assert.IsTrue(value == 9, "#2 : " + value);
79                         stack.TryPop(out value);
80                         Assert.IsTrue(value == 8, "#3 : " + value);
81                         stack.TryPeek(out value);
82                         Assert.IsTrue(value == 7, "#4 : " + value);
83                         stack.TryPeek(out value);
84                         Assert.IsTrue(value == 7, "#5 : " + value);
85                 }
86                 
87                 [Test()]
88                 public void TryPopTestCase()
89                 {
90                         int value;
91                         stack.TryPeek(out value);
92                         Assert.IsTrue(value == 9, "#1");
93                         stack.TryPop(out value);
94                         stack.TryPop(out value);
95                         Assert.IsTrue(value == 8, "#2 : " + value);
96                 }
97                 
98                 [Test()]
99                 public void TryPopEmptyTestCase()
100                 {
101                         int value;
102                         stack.Clear();
103                         stack.Push(1);
104                         Assert.IsTrue(stack.TryPop(out value), "#1");
105                         Assert.IsFalse(stack.TryPop(out value), "#2");
106                         Assert.IsTrue(stack.IsEmpty, "#3");
107                 }
108                 
109                 [Test]
110                 public void ToArrayTest()
111                 {
112                         int[] array = stack.ToArray();
113                         string s = string.Empty;
114                         foreach (int i in array) {
115                                 s += i;
116                         }
117                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
118                         stack.CopyTo(array, 0);
119                         s = string.Empty;
120                         foreach (int i in array) {
121                                 s += i;
122                         }
123                         Assert.IsTrue(s == "9876543210", "#1 : " + s);
124                 }
125         }
126 }
127 #endif