2 // SemaphoreTest.cs - Unit tests for System.Threading.Semaphore
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
34 using System.Security.AccessControl;
35 using System.Threading;
37 namespace MonoTests.System.Threading {
40 public class SemaphoreTest {
43 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44 public void Constructor_IntInt_NegativeInitialCount ()
46 new Semaphore (-1, 1);
50 [ExpectedException (typeof (ArgumentOutOfRangeException))]
51 public void Constructor_IntInt_ZeroMaximumCount ()
57 [ExpectedException (typeof (ArgumentException))]
58 public void Constructor_IntInt_InitialBiggerThanMaximum ()
64 [Category ("NotWorking")] // not implemented in Mono
65 public void Constructor_IntInt ()
71 [ExpectedException (typeof (ArgumentOutOfRangeException))]
72 public void Constructor_IntIntString_NegativeInitialCount ()
74 new Semaphore (-1, 1, "mono");
78 [ExpectedException (typeof (ArgumentOutOfRangeException))]
79 public void Constructor_IntIntString_ZeroMaximumCount ()
81 new Semaphore (0, 0, "mono");
85 [ExpectedException (typeof (ArgumentException))]
86 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
88 new Semaphore (2, 1, "mono");
92 [Category ("NotWorking")] // not implemented in Mono
93 public void Constructor_IntIntString_NullName ()
95 new Semaphore (0, 1, null);
99 public void Constructor_IntIntStringBool_NegativeInitialCount ()
103 new Semaphore (-1, 1, "mono", out created);
105 catch (ArgumentOutOfRangeException) {
106 Assert.IsTrue (created, "Created");
111 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
115 new Semaphore (0, 0, "mono", out created);
117 catch (ArgumentOutOfRangeException) {
118 Assert.IsTrue (created, "Created");
123 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
127 new Semaphore (2, 1, "mono", out created);
129 catch (ArgumentException) {
130 Assert.IsTrue (created, "Created");
135 [Category ("NotWorking")] // not implemented in Mono
136 public void Constructor_IntIntStringBool_NullName ()
138 bool created = false;
139 new Semaphore (0, 1, null, out created);
140 Assert.IsTrue (created, "Created");
144 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
148 new Semaphore (-1, 1, "mono", out created, null);
150 catch (ArgumentOutOfRangeException) {
151 Assert.IsTrue (created, "Created");
156 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
160 new Semaphore (0, 0, "mono", out created, null);
162 catch (ArgumentOutOfRangeException) {
163 Assert.IsTrue (created, "Created");
168 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
172 new Semaphore (2, 1, "mono", out created, null);
174 catch (ArgumentException) {
175 Assert.IsTrue (created, "Created");
180 [Category ("NotWorking")] // not implemented in Mono
181 public void Constructor_IntIntStringBoolSecurity_NullName ()
183 bool created = false;
184 new Semaphore (0, 1, null, out created, null);
185 Assert.IsTrue (created, "Created");
189 [Category ("NotWorking")] // not implemented in Mono
190 public void Constructor_IntIntStringBoolSecurity ()
192 bool created = false;
193 SemaphoreSecurity ss = new SemaphoreSecurity ();
194 new Semaphore (0, 1, "secure", out created, ss);
195 Assert.IsTrue (created, "Created");
199 [ExpectedException (typeof (ArgumentNullException))]
200 public void OpenExisting_NullName ()
202 Semaphore.OpenExisting (null);
206 [ExpectedException (typeof (ArgumentException))]
207 public void OpenExisting_EmptyName ()
209 Semaphore.OpenExisting (String.Empty);
213 [ExpectedException (typeof (ArgumentException))]
214 public void OpenExisting_TooLongName ()
216 Semaphore.OpenExisting (new String (' ', 261));
220 [Category ("NotWorking")] // not implemented in Mono
221 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
222 public void OpenExisting_Unexisting ()
224 Semaphore.OpenExisting (new String ('a', 260));
228 [Category ("NotWorking")] // not implemented in Mono
229 public void OpenExisting_BadRights ()
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");
240 [Category ("NotWorking")] // not implemented in Mono
241 public void AccessControl_Unnamed ()
243 Semaphore s = new Semaphore (0, 1, null);
244 SemaphoreSecurity ss = s.GetAccessControl ();
245 Assert.IsNotNull (ss, "GetAccessControl");
246 s.SetAccessControl (ss);
250 [ExpectedException (typeof (ArgumentNullException))]
251 [Category ("NotWorking")] // not implemented in Mono
252 public void SetAccessControl_Null ()
254 Semaphore s = new Semaphore (0, 1, null);
255 s.SetAccessControl (null);
259 [Category ("NotWorking")] // not implemented in Mono
260 public void Release ()
262 Semaphore s = new Semaphore (0, 1, null);
267 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268 [Category ("NotWorking")] // not implemented in Mono
269 public void Release_Zero ()
271 Semaphore s = new Semaphore (0, 1, null);