5 // Jb Evain <jbevain@novell.com>
7 // Copyright (C) 2007 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.
30 using System.Collections;
31 using System.Collections.Generic;
34 using NUnit.Framework;
36 namespace Mono.Tests.System.Collections.Generic {
39 public class HashSetTest {
42 public void TestAdd ()
44 var set = new HashSet<int> ();
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));
59 public void TestRemove ()
61 var set = new HashSet<int> ();
63 Assert.IsTrue (set.Add (1));
64 Assert.IsTrue (set.Add (2));
65 Assert.IsTrue (set.Add (3));
66 Assert.IsTrue (set.Add (4));
68 Assert.IsTrue (set.Remove (2));
69 Assert.IsTrue (set.Remove (3));
71 AssertContainsOnly (new int [] {1, 4}, set);
75 public void TestMassiveAdd ()
77 var set = new HashSet<int> ();
79 var massive = Enumerable.Range (0, 10000).ToArray ();
80 foreach (var item in massive)
81 Assert.IsTrue (set.Add (item));
83 AssertContainsOnly (massive, set);
87 public void TestMassiveRemove ()
89 var massive = Enumerable.Range (0, 10000).ToArray ();
90 var set = new HashSet<int> (massive);
92 foreach (var item in massive)
93 Assert.IsTrue (set.Remove (item));
99 public void TestCopyTo ()
101 var data = new [] {1, 2, 3, 4, 5};
102 var set = new HashSet<int> (data);
104 var array = new int [set.Count];
105 set.CopyTo (array, 0);
107 AssertContainsOnly (data, array);
111 public void TestClear ()
113 var data = new [] {1, 2, 3, 4, 5, 6};
114 var set = new HashSet<int> (data);
116 Assert.AreEqual (data.Length, set.Count);
122 public void TestContains ()
124 var data = new [] {1, 2, 3, 4, 5, 6};
125 var set = new HashSet<int> (data);
127 foreach (var item in data)
128 Assert.IsTrue (set.Contains (item));
131 [Test, ExpectedException (typeof (InvalidOperationException))]
132 public void TestModifySetWhileForeach ()
134 var set = new HashSet<int> (new [] {1, 2, 3, 4});
135 foreach (var item in set)
140 public void TestRemoveWhere ()
142 var data = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
143 var result = new [] {2, 4, 6, 8};
145 var set = new HashSet<int> (data);
146 int removed = set.RemoveWhere (i => (i % 2) != 0);
148 Assert.AreEqual (data.Length - result.Length, removed);
149 AssertContainsOnly (result, set);
153 public void TestOverlaps ()
155 var set = new HashSet<int> (new [] {1, 2, 3, 4, 5});
157 Assert.IsTrue (set.Overlaps (new [] {0, 2}));
161 public void TestIntersectWith ()
163 var data = new [] {1, 2, 3, 4};
164 var other = new [] {2, 4, 5, 6};
165 var result = new [] {2, 4};
167 var set = new HashSet<int> (data);
169 set.IntersectWith (other);
171 AssertContainsOnly (result, set);
175 public void TestExceptWith ()
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);
182 set.ExceptWith (other);
184 AssertContainsOnly (result, set);
188 public void TestUnionWith ()
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};
194 var set = new HashSet<int> (data);
195 set.UnionWith (other);
197 AssertContainsOnly (result, set);
201 public void TestSymmetricExceptWith ()
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};
207 var set = new HashSet<int> (data);
208 set.SymmetricExceptWith (other);
210 AssertContainsOnly (result, set);
214 public void TestEmptyHashSubsetOf ()
216 var set = new HashSet<int> ();
218 Assert.IsTrue (set.IsSubsetOf (new int [0]));
219 Assert.IsTrue (set.IsSubsetOf (new [] {1, 2}));
223 public void TestSubsetOf ()
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};
230 var set = new HashSet<int> (data);
232 Assert.IsTrue (set.IsSubsetOf (other));
233 Assert.IsTrue (set.IsSubsetOf (other2));
234 Assert.IsFalse (set.IsSubsetOf (other3));
238 public void TestProperSubsetOf ()
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};
245 var set = new HashSet<int> (data);
247 Assert.IsTrue (set.IsProperSubsetOf (other));
248 Assert.IsFalse (set.IsProperSubsetOf (other2));
249 Assert.IsFalse (set.IsProperSubsetOf (other3));
253 public void TestSupersetOf ()
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};
260 var set = new HashSet<int> (data);
262 Assert.IsTrue (set.IsSupersetOf (other));
263 Assert.IsTrue (set.IsSupersetOf (other2));
264 Assert.IsFalse (set.IsSupersetOf (other3));
268 public void TestProperSupersetOf ()
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};
275 var set = new HashSet<int> (data);
277 Assert.IsTrue (set.IsProperSupersetOf (other));
278 Assert.IsFalse (set.IsProperSupersetOf (other2));
279 Assert.IsFalse (set.IsProperSupersetOf (other3));
283 public void TestSetEquals ()
285 var data = new [] {1, 2, 3, 4};
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};
293 var set = new HashSet<int> (data);
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));
302 static void AssertContainsOnly<T> (IEnumerable<T> result, IEnumerable<T> data)
304 Assert.AreEqual (result.Count (), data.Count ());
306 var store = new List<T> (result);
307 foreach (var element in data) {
308 Assert.IsTrue (store.Contains (element));
309 store.Remove (element);
312 AssertIsEmpty (store);
315 static void AssertIsEmpty<T> (IEnumerable<T> source)
317 Assert.AreEqual (0, source.Count ());