2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / BlockingCollectionTests.cs
1 #if NET_4_0
2 // BlockingCollectionTests.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.Collections.Concurrent;
29 using System.Collections.Generic;
30
31 using NUnit.Framework;
32
33 namespace ParallelFxTests
34 {
35         [TestFixture()]
36         public class BlockingCollectionTests
37         {
38                 BlockingCollection<int> defaultCollection;
39                 BlockingCollection<int> boundedCollection;
40                 
41                 [SetUpAttribute]
42                 public void Setup()
43                 {
44                         defaultCollection = new BlockingCollection<int>();
45                         boundedCollection = new BlockingCollection<int>(10);
46                 }
47                 
48                 [TestAttribute]
49                 public void DefaultAddTestCase()
50                 {
51                         defaultCollection.Add(1);
52                         defaultCollection.Add(2);
53                         Assert.AreEqual(2, defaultCollection.Count, "#1");
54
55                 }
56                 
57                 [TestAttribute]
58                 public void BoundedAddTestCase()
59                 {
60                         boundedCollection.Add(1);
61                         boundedCollection.Add(2);
62                         Assert.AreEqual(2, boundedCollection.Count, "#1");
63                 }
64                 
65                 [TestAttribute]
66                 public void BoundedIsFullTestCase()
67                 {
68                         boundedCollection.Add(1);
69                         boundedCollection.Add(2);
70                         boundedCollection.Add(3);
71                         boundedCollection.Add(4);
72                         boundedCollection.Add(5);
73                         boundedCollection.Add(6);
74                         boundedCollection.Add(7);
75                         boundedCollection.Add(8);
76                         boundedCollection.Add(9);
77                         boundedCollection.Add(10);
78                         Assert.AreEqual(boundedCollection.BoundedCapacity, boundedCollection.Count, "#1");
79                 }
80                 
81                 [TestAttribute]
82                 public void TakeTestCase()
83                 {
84                         defaultCollection.Add(1);
85                         defaultCollection.Add(2);
86                         boundedCollection.Add(1);
87                         boundedCollection.Add(2);
88                         
89                         int value = defaultCollection.Take();
90                         Assert.AreEqual(1, value, "#1");
91                         value = boundedCollection.Take();
92                         Assert.AreEqual(1, value, "#2");
93                 }
94                 
95                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
96                 public void DefaultAddCompletedTestCase()
97                 {
98                         defaultCollection.Add(1);
99                         defaultCollection.Add(2);
100                         defaultCollection.CompleteAdding();
101                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
102                         
103                         defaultCollection.Add(3);
104                 }
105                 
106                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
107                 public void BoundedAddCompletedTestCase()
108                 {
109                         boundedCollection.Add(1);
110                         boundedCollection.Add(2);
111                         boundedCollection.Add(3);
112                         boundedCollection.Add(4);
113                         boundedCollection.Add(5);
114                         boundedCollection.Add(6);
115                         boundedCollection.Add(7);
116                         boundedCollection.Add(8);
117                         boundedCollection.Add(9);
118                         boundedCollection.Add(10);
119                         boundedCollection.CompleteAdding();
120                         Assert.IsTrue(boundedCollection.IsAddingCompleted, "#1");
121                         
122                         boundedCollection.Add(3);
123                 }
124                 
125                 [TestAttribute]
126                 public void IsCompletedTestCase()
127                 {
128                         defaultCollection.Add(1);
129                         defaultCollection.Add(2);
130                         
131                         defaultCollection.CompleteAdding();
132                         Assert.IsFalse(defaultCollection.IsCompleted, "#3");
133                         
134                         defaultCollection.Take();
135                         defaultCollection.Take();
136                         
137                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
138                         Assert.AreEqual(0, defaultCollection.Count, "#2");
139                         Assert.IsTrue(defaultCollection.IsCompleted, "#4");
140                 }
141                 
142                 [TestAttribute]
143                 public void ConsumingEnumerableTestCase()
144                 {
145                         defaultCollection.Add(1);
146                         defaultCollection.Add(2);
147                         defaultCollection.Add(3);
148                         defaultCollection.Add(4);
149                         defaultCollection.Add(5);
150                         defaultCollection.Add(6);
151                         defaultCollection.CompleteAdding ();
152                         
153                         IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
154                         Assert.IsNotNull(enumerable, "#1");
155                         int i = 1;
156                         foreach (int j in enumerable) {
157                                 int temp = i++;
158                                 Assert.AreEqual(temp, j, "#" + temp);
159                         }
160                         Assert.AreEqual(0, defaultCollection.Count, "#" + i);
161                 }
162         }
163 }
164 #endif