merge -r 58060:58217
[mono.git] / mcs / class / System / Test / System.Threading / SemaphoreTest.cs
1 //
2 // SemaphoreTest.cs - Unit tests for System.Threading.Semaphore
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Security.AccessControl;
35 using System.Threading;
36
37 namespace MonoTests.System.Threading {
38
39         [TestFixture]
40         public class SemaphoreTest {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44                 public void Constructor_IntInt_NegativeInitialCount ()
45                 {
46                         new Semaphore (-1, 1);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
51                 public void Constructor_IntInt_ZeroMaximumCount ()
52                 {
53                         new Semaphore (0, 0);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentException))]
58                 public void Constructor_IntInt_InitialBiggerThanMaximum ()
59                 {
60                         new Semaphore (2, 1);
61                 }
62
63                 [Test]
64                 [Category ("NotWorking")] // not implemented in Mono
65                 public void Constructor_IntInt ()
66                 {
67                         new Semaphore (1, 2);
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
72                 public void Constructor_IntIntString_NegativeInitialCount ()
73                 {
74                         new Semaphore (-1, 1, "mono");
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
79                 public void Constructor_IntIntString_ZeroMaximumCount ()
80                 {
81                         new Semaphore (0, 0, "mono");
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentException))]
86                 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
87                 {
88                         new Semaphore (2, 1, "mono");
89                 }
90
91                 [Test]
92                 [Category ("NotWorking")] // not implemented in Mono
93                 public void Constructor_IntIntString_NullName ()
94                 {
95                         new Semaphore (0, 1, null);
96                 }
97
98                 [Test]
99                 public void Constructor_IntIntStringBool_NegativeInitialCount ()
100                 {
101                         bool created = true;
102                         try {
103                                 new Semaphore (-1, 1, "mono", out created);
104                         }
105                         catch (ArgumentOutOfRangeException) {
106                                 Assert.IsTrue (created, "Created");
107                         }
108                 }
109
110                 [Test]
111                 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
112                 {
113                         bool created = true;
114                         try {
115                                 new Semaphore (0, 0, "mono", out created);
116                         }
117                         catch (ArgumentOutOfRangeException) {
118                                 Assert.IsTrue (created, "Created");
119                         }
120                 }
121
122                 [Test]
123                 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
124                 {
125                         bool created = true;
126                         try {
127                                 new Semaphore (2, 1, "mono", out created);
128                         }
129                         catch (ArgumentException) {
130                                 Assert.IsTrue (created, "Created");
131                         }
132                 }
133
134                 [Test]
135                 [Category ("NotWorking")] // not implemented in Mono
136                 public void Constructor_IntIntStringBool_NullName ()
137                 {
138                         bool created = false;
139                         new Semaphore (0, 1, null, out created);
140                         Assert.IsTrue (created, "Created");
141                 }
142
143                 [Test]
144                 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
145                 {
146                         bool created = true;
147                         try {
148                                 new Semaphore (-1, 1, "mono", out created, null);
149                         }
150                         catch (ArgumentOutOfRangeException) {
151                                 Assert.IsTrue (created, "Created");
152                         }
153                 }
154
155                 [Test]
156                 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
157                 {
158                         bool created = true;
159                         try {
160                                 new Semaphore (0, 0, "mono", out created, null);
161                         }
162                         catch (ArgumentOutOfRangeException) {
163                                 Assert.IsTrue (created, "Created");
164                         }
165                 }
166
167                 [Test]
168                 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
169                 {
170                         bool created = true;
171                         try {
172                                 new Semaphore (2, 1, "mono", out created, null);
173                         }
174                         catch (ArgumentException) {
175                                 Assert.IsTrue (created, "Created");
176                         }
177                 }
178
179                 [Test]
180                 [Category ("NotWorking")] // not implemented in Mono
181                 public void Constructor_IntIntStringBoolSecurity_NullName ()
182                 {
183                         bool created = false;
184                         new Semaphore (0, 1, null, out created, null);
185                         Assert.IsTrue (created, "Created");
186                 }
187
188                 [Test]
189                 [Category ("NotWorking")] // not implemented in Mono
190                 public void Constructor_IntIntStringBoolSecurity ()
191                 {
192                         bool created = false;
193                         SemaphoreSecurity ss = new SemaphoreSecurity ();
194                         new Semaphore (0, 1, "secure", out created, ss);
195                         Assert.IsTrue (created, "Created");
196                 }
197
198                 [Test]
199                 [ExpectedException (typeof (ArgumentNullException))]
200                 public void OpenExisting_NullName ()
201                 {
202                         Semaphore.OpenExisting (null);
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (ArgumentException))]
207                 public void OpenExisting_EmptyName ()
208                 {
209                         Semaphore.OpenExisting (String.Empty);
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (ArgumentException))]
214                 public void OpenExisting_TooLongName ()
215                 {
216                         Semaphore.OpenExisting (new String (' ', 261));
217                 }
218
219                 [Test]
220                 [Category ("NotWorking")] // not implemented in Mono
221                 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
222                 public void OpenExisting_Unexisting ()
223                 {
224                         Semaphore.OpenExisting (new String ('a', 260));
225                 }
226
227                 [Test]
228                 [Category ("NotWorking")] // not implemented in Mono
229                 public void OpenExisting_BadRights ()
230                 {
231                         Semaphore s = new Semaphore (0, 1, "bad-rights");
232                         SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
233                         Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
234                         // rights bits aren't validated
235                         Assert.IsNotNull (existing, "OpenExisting");
236                         Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
237                 }
238
239                 [Test]
240                 [Category ("NotWorking")] // not implemented in Mono
241                 public void AccessControl_Unnamed ()
242                 {
243                         Semaphore s = new Semaphore (0, 1, null);
244                         SemaphoreSecurity ss = s.GetAccessControl ();
245                         Assert.IsNotNull (ss, "GetAccessControl");
246                         s.SetAccessControl (ss);
247                 }
248
249                 [Test]
250                 [ExpectedException (typeof (ArgumentNullException))]
251                 [Category ("NotWorking")] // not implemented in Mono
252                 public void SetAccessControl_Null ()
253                 {
254                         Semaphore s = new Semaphore (0, 1, null);
255                         s.SetAccessControl (null);
256                 }
257
258                 [Test]
259                 [Category ("NotWorking")] // not implemented in Mono
260                 public void Release ()
261                 {
262                         Semaphore s = new Semaphore (0, 1, null);
263                         s.Release ();
264                 }
265
266                 [Test]
267                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268                 [Category ("NotWorking")] // not implemented in Mono
269                 public void Release_Zero ()
270                 {
271                         Semaphore s = new Semaphore (0, 1, null);
272                         s.Release (0);
273                 }
274         }
275 }
276
277 #endif