for TARGET_J2EE only:
[mono.git] / mcs / class / System.Core / Test / System.Collections.Generic / HashSetTest.cs
1 //
2 // HashSetTest.cs
3 //
4 // Authors:
5 //  Jb Evain  <jbevain@novell.com>
6 //
7 // Copyright (C) 2007 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 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Linq;
33
34 using NUnit.Framework;
35
36 namespace Mono.Tests.System.Collections.Generic {
37
38         [TestFixture]
39         public class HashSetTest {
40
41                 [Test]
42                 public void TestAdd ()
43                 {
44                         var set = new HashSet<int> ();
45
46                         Assert.IsTrue (set.Add (1));
47                         Assert.IsTrue (set.Add (2));
48                         Assert.IsTrue (set.Add (3));
49                         Assert.IsTrue (set.Add (4));
50                         Assert.IsFalse (set.Add (4));
51                         Assert.IsFalse (set.Add (3));
52                         Assert.IsFalse (set.Add (2));
53                         Assert.IsFalse (set.Add (1));
54                         Assert.IsTrue (set.Add (0));
55                         Assert.IsFalse (set.Add (0));
56                 }
57
58                 [Test]
59                 public void TestRemove ()
60                 {
61                         var set = new HashSet<int> ();
62
63                         Assert.IsTrue (set.Add (1));
64                         Assert.IsTrue (set.Add (2));
65                         Assert.IsTrue (set.Add (3));
66                         Assert.IsTrue (set.Add (4));
67
68                         Assert.IsTrue (set.Remove (2));
69                         Assert.IsTrue (set.Remove (3));
70
71                         AssertContainsOnly (new int [] {1, 4}, set);
72                 }
73
74                 [Test]
75                 public void TestMassiveAdd ()
76                 {
77                         var set = new HashSet<int> ();
78
79                         var massive = Enumerable.Range (0, 10000).ToArray ();
80                         foreach (var item in massive)
81                                 Assert.IsTrue (set.Add (item));
82
83                         AssertContainsOnly (massive, set);
84                 }
85
86                 [Test]
87                 public void TestMassiveRemove ()
88                 {
89                         var massive = Enumerable.Range (0, 10000).ToArray ();
90                         var set = new HashSet<int> (massive);
91
92                         foreach (var item in massive)
93                                 Assert.IsTrue (set.Remove (item));
94
95                         AssertIsEmpty (set);
96                 }
97
98                 [Test]
99                 public void TestCopyTo ()
100                 {
101                         var data = new [] {1, 2, 3, 4, 5};
102                         var set = new HashSet<int> (data);
103
104                         var array = new int [set.Count];
105                         set.CopyTo (array, 0);
106
107                         AssertContainsOnly (data, array);
108                 }
109
110                 [Test]
111                 public void TestClear ()
112                 {
113                         var data = new [] {1, 2, 3, 4, 5, 6};
114                         var set = new HashSet<int> (data);
115
116                         Assert.AreEqual (data.Length, set.Count);
117                         set.Clear ();
118                         AssertIsEmpty (set);
119                 }
120
121                 [Test]
122                 public void TestContains ()
123                 {
124                         var data = new [] {1, 2, 3, 4, 5, 6};
125                         var set = new HashSet<int> (data);
126
127                         foreach (var item in data)
128                                 Assert.IsTrue (set.Contains (item));
129                 }
130
131                 [Test, ExpectedException (typeof (InvalidOperationException))]
132                 public void TestModifySetWhileForeach ()
133                 {
134                         var set = new HashSet<int> (new [] {1, 2, 3, 4});
135                         foreach (var item in set)
136                                 set.Add (item + 2);
137                 }
138
139                 [Test]
140                 public void TestRemoveWhere ()
141                 {
142                         var data = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
143                         var result = new [] {2, 4, 6, 8};
144
145                         var set = new HashSet<int> (data);
146                         int removed = set.RemoveWhere (i => (i % 2) != 0);
147
148                         Assert.AreEqual (data.Length - result.Length, removed);
149                         AssertContainsOnly (result, set);
150                 }
151
152                 [Test]
153                 public void TestOverlaps ()
154                 {
155                         var set = new HashSet<int> (new [] {1, 2, 3, 4, 5});
156
157                         Assert.IsTrue (set.Overlaps (new [] {0, 2}));
158                 }
159
160                 [Test]
161                 public void TestIntersectWith ()
162                 {
163                         var data = new [] {1, 2, 3, 4};
164                         var other = new [] {2, 4, 5, 6};
165                         var result = new [] {2, 4};
166
167                         var set = new HashSet<int> (data);
168
169                         set.IntersectWith (other);
170
171                         AssertContainsOnly (result, set);
172                 }
173
174                 [Test]
175                 public void TestExceptWith ()
176                 {
177                         var data = new [] {1, 2, 3, 4, 5, 6};
178                         var other = new [] {2, 4, 6};
179                         var result = new [] {1, 3, 5};
180                         var set = new HashSet<int> (data);
181
182                         set.ExceptWith (other);
183
184                         AssertContainsOnly (result, set);
185                 }
186
187                 [Test]
188                 public void TestUnionWith ()
189                 {
190                         var data = new [] {1, 2, 3, 4, 5, 6};
191                         var other = new [] {4, 5, 6, 7, 8, 9};
192                         var result = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
193
194                         var set = new HashSet<int> (data);
195                         set.UnionWith (other);
196
197                         AssertContainsOnly (result, set);
198                 }
199
200                 [Test]
201                 public void TestSymmetricExceptWith ()
202                 {
203                         var data = new [] {1, 2, 3, 4, 5};
204                         var other = new [] {4, 5, 6, 7, 8, 9};
205                         var result = new [] {1, 2, 3, 6, 7, 8, 9};
206
207                         var set = new HashSet<int> (data);
208                         set.SymmetricExceptWith (other);
209
210                         AssertContainsOnly (result, set);
211                 }
212
213                 [Test]
214                 public void TestEmptyHashSubsetOf ()
215                 {
216                         var set = new HashSet<int> ();
217
218                         Assert.IsTrue (set.IsSubsetOf (new int [0]));
219                         Assert.IsTrue (set.IsSubsetOf (new [] {1, 2}));
220                 }
221
222                 [Test]
223                 public void TestSubsetOf ()
224                 {
225                         var data = new [] {1, 2, 3};
226                         var other = new [] {1, 2, 3, 4, 5};
227                         var other2 = new [] {1, 2, 3};
228                         var other3 = new [] {0, 1, 2};
229
230                         var set = new HashSet<int> (data);
231
232                         Assert.IsTrue (set.IsSubsetOf (other));
233                         Assert.IsTrue (set.IsSubsetOf (other2));
234                         Assert.IsFalse (set.IsSubsetOf (other3));
235                 }
236
237                 [Test]
238                 public void TestProperSubsetOf ()
239                 {
240                         var data = new [] {1, 2, 3};
241                         var other = new [] {1, 2, 3, 4, 5};
242                         var other2 = new [] {1, 2, 3};
243                         var other3 = new [] {0, 1, 2};
244
245                         var set = new HashSet<int> (data);
246
247                         Assert.IsTrue (set.IsProperSubsetOf (other));
248                         Assert.IsFalse (set.IsProperSubsetOf (other2));
249                         Assert.IsFalse (set.IsProperSubsetOf (other3));
250                 }
251
252                 [Test]
253                 public void TestSupersetOf ()
254                 {
255                         var data = new [] {1, 2, 3, 4, 5};
256                         var other = new [] {2, 3, 4};
257                         var other2 = new [] {1, 2, 3, 4, 5};
258                         var other3 = new [] {4, 5, 6};
259
260                         var set = new HashSet<int> (data);
261
262                         Assert.IsTrue (set.IsSupersetOf (other));
263                         Assert.IsTrue (set.IsSupersetOf (other2));
264                         Assert.IsFalse (set.IsSupersetOf (other3));
265                 }
266
267                 [Test]
268                 public void TestProperSupersetOf ()
269                 {
270                         var data = new [] {1, 2, 3, 4, 5};
271                         var other = new [] {2, 3, 4};
272                         var other2 = new [] {1, 2, 3, 4, 5};
273                         var other3 = new [] {4, 5, 6};
274
275                         var set = new HashSet<int> (data);
276
277                         Assert.IsTrue (set.IsProperSupersetOf (other));
278                         Assert.IsFalse (set.IsProperSupersetOf (other2));
279                         Assert.IsFalse (set.IsProperSupersetOf (other3));
280                 }
281
282                 [Test]
283                 public void TestSetEquals ()
284                 {
285                         var data = new [] {1, 2, 3, 4};
286
287                         var other = new [] {1, 2, 3, 4};
288                         var other2 = new [] {1, 2, 2, 4};
289                         var other3 = new [] {1, 2};
290                         var other4 = new [] {1, 2, 3, 4, 5};
291                         var other5 = new [] {1, 1, 1, 1};
292
293                         var set = new HashSet<int> (data);
294
295                         Assert.IsTrue (set.SetEquals (other));
296                         Assert.IsFalse (set.SetEquals (other2));
297                         Assert.IsFalse (set.SetEquals (other3));
298                         Assert.IsFalse (set.SetEquals (other4));
299                         Assert.IsFalse (set.SetEquals (other5));
300                 }
301
302                 static void AssertContainsOnly<T> (IEnumerable<T> result, IEnumerable<T> data)
303                 {
304                         Assert.AreEqual (result.Count (), data.Count ());
305
306                         var store = new List<T> (result);
307                         foreach (var element in data) {
308                                 Assert.IsTrue (store.Contains (element));
309                                 store.Remove (element);
310                         }
311
312                         AssertIsEmpty (store);
313                 }
314
315                 static void AssertIsEmpty<T> (IEnumerable<T> source)
316                 {
317                         Assert.AreEqual (0, source.Count ());
318                 }
319         }
320 }