Halve the stack depth for both the new single step tests
[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
30 using NUnit.Framework;
31
32 using System;
33 using System.Security.AccessControl;
34 using System.Threading;
35
36 namespace MonoTests.System.Threading {
37
38         [TestFixture]
39         public class SemaphoreTest {
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
43                 public void Constructor_IntInt_NegativeInitialCount ()
44                 {
45                         new Semaphore (-1, 1);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
50                 public void Constructor_IntInt_ZeroMaximumCount ()
51                 {
52                         new Semaphore (0, 0);
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (ArgumentException))]
57                 public void Constructor_IntInt_InitialBiggerThanMaximum ()
58                 {
59                         new Semaphore (2, 1);
60                 }
61
62                 [Test]
63                 public void Constructor_IntInt ()
64                 {
65                         new Semaphore (1, 2);
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
70                 public void Constructor_IntIntString_NegativeInitialCount ()
71                 {
72                         new Semaphore (-1, 1, "mono");
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
77                 public void Constructor_IntIntString_ZeroMaximumCount ()
78                 {
79                         new Semaphore (0, 0, "mono");
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentException))]
84                 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
85                 {
86                         new Semaphore (2, 1, "mono");
87                 }
88
89                 [Test]
90                 public void Constructor_IntIntString_NullName ()
91                 {
92                         new Semaphore (0, 1, null);
93                 }
94
95                 [Test]
96                 public void Constructor_IntIntStringBool_NegativeInitialCount ()
97                 {
98                         bool created = true;
99                         try {
100                                 new Semaphore (-1, 1, "mono", out created);
101                         }
102                         catch (ArgumentOutOfRangeException) {
103                                 Assert.IsTrue (created, "Created");
104                         }
105                 }
106
107                 [Test]
108                 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
109                 {
110                         bool created = true;
111                         try {
112                                 new Semaphore (0, 0, "mono", out created);
113                         }
114                         catch (ArgumentOutOfRangeException) {
115                                 Assert.IsTrue (created, "Created");
116                         }
117                 }
118
119                 [Test]
120                 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
121                 {
122                         bool created = true;
123                         try {
124                                 new Semaphore (2, 1, "mono", out created);
125                         }
126                         catch (ArgumentException) {
127                                 Assert.IsTrue (created, "Created");
128                         }
129                 }
130
131                 [Test]
132                 public void Constructor_IntIntStringBool_NullName ()
133                 {
134                         bool created = false;
135                         new Semaphore (0, 1, null, out created);
136                         Assert.IsTrue (created, "Created");
137                 }
138
139                 [Test]
140                 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
141                 {
142                         bool created = true;
143                         try {
144                                 new Semaphore (-1, 1, "mono", out created, null);
145                         }
146                         catch (ArgumentOutOfRangeException) {
147                                 Assert.IsTrue (created, "Created");
148                         }
149                 }
150
151                 [Test]
152                 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
153                 {
154                         bool created = true;
155                         try {
156                                 new Semaphore (0, 0, "mono", out created, null);
157                         }
158                         catch (ArgumentOutOfRangeException) {
159                                 Assert.IsTrue (created, "Created");
160                         }
161                 }
162
163                 [Test]
164                 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
165                 {
166                         bool created = true;
167                         try {
168                                 new Semaphore (2, 1, "mono", out created, null);
169                         }
170                         catch (ArgumentException) {
171                                 Assert.IsTrue (created, "Created");
172                         }
173                 }
174
175                 [Test]
176                 public void Constructor_IntIntStringBoolSecurity_NullName ()
177                 {
178                         bool created = false;
179                         new Semaphore (0, 1, null, out created, null);
180                         Assert.IsTrue (created, "Created");
181                 }
182
183                 [Test]
184                 [Category ("MobileNotWorking")]
185                 public void Constructor_IntIntStringBoolSecurity ()
186                 {
187                         bool created = false;
188                         SemaphoreSecurity ss = new SemaphoreSecurity ();
189                         new Semaphore (0, 1, "secure", out created, ss);
190                         Assert.IsTrue (created, "Created");
191                 }
192
193                 [Test]
194                 [Category ("MobileNotWorking")]
195                 [ExpectedException (typeof (ArgumentNullException))]
196                 public void OpenExisting_NullName ()
197                 {
198                         Semaphore.OpenExisting (null);
199                 }
200
201                 [Test]
202                 [Category ("MobileNotWorking")]
203                 [ExpectedException (typeof (ArgumentException))]
204                 public void OpenExisting_EmptyName ()
205                 {
206                         Semaphore.OpenExisting (String.Empty);
207                 }
208
209                 [Test]
210                 [Category ("MobileNotWorking")]
211                 [ExpectedException (typeof (ArgumentException))]
212                 public void OpenExisting_TooLongName ()
213                 {
214                         Semaphore.OpenExisting (new String (' ', 261));
215                 }
216
217                 [Test]
218                 [Category ("MobileNotWorking")]
219                 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
220                 public void OpenExisting_Unexisting ()
221                 {
222                         Semaphore.OpenExisting (new String ('a', 260));
223                 }
224
225                 [Test]
226                 [Category ("NotWorking")] // not implemented in Mono
227                 public void OpenExisting_BadRights ()
228                 {
229                         Semaphore s = new Semaphore (0, 1, "bad-rights");
230                         SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
231                         Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
232                         // rights bits aren't validated
233                         Assert.IsNotNull (existing, "OpenExisting");
234                         Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
235                 }
236
237                 [Test]
238                 [Category ("NotWorking")] // not implemented in Mono
239                 public void AccessControl_Unnamed ()
240                 {
241                         Semaphore s = new Semaphore (0, 1, null);
242                         SemaphoreSecurity ss = s.GetAccessControl ();
243                         Assert.IsNotNull (ss, "GetAccessControl");
244                         s.SetAccessControl (ss);
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ArgumentNullException))]
249                 [Category ("NotWorking")] // not implemented in Mono
250                 public void SetAccessControl_Null ()
251                 {
252                         Semaphore s = new Semaphore (0, 1, null);
253                         s.SetAccessControl (null);
254                 }
255
256                 [Test]
257                 public void Release ()
258                 {
259                         Semaphore s = new Semaphore (0, 1, null);
260                         s.Release ();
261                 }
262
263                 [Test]
264                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
265                 public void Release_Zero ()
266                 {
267                         Semaphore s = new Semaphore (0, 1, null);
268                         s.Release (0);
269                 }
270         }
271 }
272