Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[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                 public void Constructor_CreatedWithSameName ()
185                 {
186                         var s1 = new Semaphore(1, 5, "name");
187                         bool created;
188                         var s2 = new Semaphore(1, 5, "name", out created);
189                         Assert.IsFalse (created);
190                 }
191
192                 [Test]
193                 [Category ("MobileNotWorking")]
194                 public void Constructor_IntIntStringBoolSecurity ()
195                 {
196                         bool created = false;
197                         SemaphoreSecurity ss = new SemaphoreSecurity ();
198                         new Semaphore (0, 1, "secure", out created, ss);
199                         Assert.IsTrue (created, "Created");
200                 }
201
202                 [Test]
203                 [Category ("MobileNotWorking")]
204                 [ExpectedException (typeof (ArgumentNullException))]
205                 public void OpenExisting_NullName ()
206                 {
207                         Semaphore.OpenExisting (null);
208                 }
209
210                 [Test]
211                 [Category ("MobileNotWorking")]
212                 [ExpectedException (typeof (ArgumentException))]
213                 public void OpenExisting_EmptyName ()
214                 {
215                         Semaphore.OpenExisting (String.Empty);
216                 }
217
218                 [Test]
219                 [Category ("MobileNotWorking")]
220                 [ExpectedException (typeof (ArgumentException))]
221                 public void OpenExisting_TooLongName ()
222                 {
223                         Semaphore.OpenExisting (new String (' ', 261));
224                 }
225
226                 [Test]
227                 [Category ("MobileNotWorking")]
228                 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
229                 public void OpenExisting_Unexisting ()
230                 {
231                         Semaphore.OpenExisting (new String ('a', 260));
232                 }
233
234                 [Test]
235                 [Category ("NotWorking")] // not implemented in Mono
236                 public void OpenExisting_BadRights ()
237                 {
238                         Semaphore s = new Semaphore (0, 1, "bad-rights");
239                         SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
240                         Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
241                         // rights bits aren't validated
242                         Assert.IsNotNull (existing, "OpenExisting");
243                         Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
244                 }
245
246                 [Test]
247                 [Category ("NotWorking")] // not implemented in Mono
248                 public void AccessControl_Unnamed ()
249                 {
250                         Semaphore s = new Semaphore (0, 1, null);
251                         SemaphoreSecurity ss = s.GetAccessControl ();
252                         Assert.IsNotNull (ss, "GetAccessControl");
253                         s.SetAccessControl (ss);
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (ArgumentNullException))]
258                 [Category ("NotWorking")] // not implemented in Mono
259                 public void SetAccessControl_Null ()
260                 {
261                         Semaphore s = new Semaphore (0, 1, null);
262                         s.SetAccessControl (null);
263                 }
264
265                 [Test]
266                 public void Release ()
267                 {
268                         Semaphore s = new Semaphore (0, 1, null);
269                         s.Release ();
270                 }
271
272                 [Test]
273                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
274                 public void Release_Zero ()
275                 {
276                         Semaphore s = new Semaphore (0, 1, null);
277                         s.Release (0);
278                 }
279         }
280 }
281