HashSetTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //
  2. // HashSetTest.cs
  3. //
  4. // Authors:
  5. // Jb Evain <[email protected]>
  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. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Collections.Generic {
  34. [TestFixture]
  35. public class HashSetTest {
  36. [Test]
  37. public void TestAdd ()
  38. {
  39. var set = new HashSet<int> ();
  40. Assert.IsTrue (set.Add (1));
  41. Assert.IsTrue (set.Add (2));
  42. Assert.IsTrue (set.Add (3));
  43. Assert.IsTrue (set.Add (4));
  44. Assert.IsFalse (set.Add (4));
  45. Assert.IsFalse (set.Add (3));
  46. Assert.IsFalse (set.Add (2));
  47. Assert.IsFalse (set.Add (1));
  48. Assert.IsTrue (set.Add (0));
  49. Assert.IsFalse (set.Add (0));
  50. }
  51. [Test]
  52. public void TestRemove ()
  53. {
  54. var set = new HashSet<int> ();
  55. Assert.IsTrue (set.Add (1));
  56. Assert.IsTrue (set.Add (2));
  57. Assert.IsTrue (set.Add (3));
  58. Assert.IsTrue (set.Add (4));
  59. Assert.IsTrue (set.Remove (2));
  60. Assert.IsTrue (set.Remove (3));
  61. AssertContainsOnly (new int [] {1, 4}, set);
  62. }
  63. [Test]
  64. public void TestMassiveAdd ()
  65. {
  66. var set = new HashSet<int> ();
  67. var massive = Enumerable.Range (0, 10000).ToArray ();
  68. foreach (var item in massive)
  69. Assert.IsTrue (set.Add (item));
  70. AssertContainsOnly (massive, set);
  71. }
  72. [Test]
  73. public void TestMassiveRemove ()
  74. {
  75. var massive = Enumerable.Range (0, 10000).ToArray ();
  76. var set = new HashSet<int> (massive);
  77. foreach (var item in massive)
  78. Assert.IsTrue (set.Remove (item));
  79. AssertIsEmpty (set);
  80. }
  81. [Test]
  82. [Category("TargetJvmNotWorking")]
  83. public void TestCopyTo ()
  84. {
  85. var data = new [] {1, 2, 3, 4, 5};
  86. var set = new HashSet<int> (data);
  87. var array = new int [set.Count];
  88. set.CopyTo (array, 0);
  89. AssertContainsOnly (data, array);
  90. }
  91. [Test]
  92. public void TestClear ()
  93. {
  94. var data = new [] {1, 2, 3, 4, 5, 6};
  95. var set = new HashSet<int> (data);
  96. Assert.AreEqual (data.Length, set.Count);
  97. set.Clear ();
  98. AssertIsEmpty (set);
  99. }
  100. [Test]
  101. public void TestContains ()
  102. {
  103. var data = new [] {1, 2, 3, 4, 5, 6};
  104. var set = new HashSet<int> (data);
  105. foreach (var item in data)
  106. Assert.IsTrue (set.Contains (item));
  107. }
  108. [Test, ExpectedException (typeof (InvalidOperationException))]
  109. public void TestModifySetWhileForeach ()
  110. {
  111. var set = new HashSet<int> (new [] {1, 2, 3, 4});
  112. foreach (var item in set)
  113. set.Add (item + 2);
  114. }
  115. [Test]
  116. public void TestRemoveWhere ()
  117. {
  118. var data = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  119. var result = new [] {2, 4, 6, 8};
  120. var set = new HashSet<int> (data);
  121. int removed = set.RemoveWhere (i => (i % 2) != 0);
  122. Assert.AreEqual (data.Length - result.Length, removed);
  123. AssertContainsOnly (result, set);
  124. }
  125. [Test]
  126. public void TestOverlaps ()
  127. {
  128. var set = new HashSet<int> (new [] {1, 2, 3, 4, 5});
  129. Assert.IsTrue (set.Overlaps (new [] {0, 2}));
  130. }
  131. [Test]
  132. public void TestIntersectWith ()
  133. {
  134. var data = new [] {1, 2, 3, 4};
  135. var other = new [] {2, 4, 5, 6};
  136. var result = new [] {2, 4};
  137. var set = new HashSet<int> (data);
  138. set.IntersectWith (other);
  139. AssertContainsOnly (result, set);
  140. }
  141. [Test]
  142. public void TestExceptWith ()
  143. {
  144. var data = new [] {1, 2, 3, 4, 5, 6};
  145. var other = new [] {2, 4, 6};
  146. var result = new [] {1, 3, 5};
  147. var set = new HashSet<int> (data);
  148. set.ExceptWith (other);
  149. AssertContainsOnly (result, set);
  150. }
  151. [Test]
  152. public void TestUnionWith ()
  153. {
  154. var data = new [] {1, 2, 3, 4, 5, 6};
  155. var other = new [] {4, 5, 6, 7, 8, 9};
  156. var result = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  157. var set = new HashSet<int> (data);
  158. set.UnionWith (other);
  159. AssertContainsOnly (result, set);
  160. }
  161. [Test]
  162. public void TestSymmetricExceptWith ()
  163. {
  164. var data = new [] {1, 2, 3, 4, 5};
  165. var other = new [] {4, 5, 6, 7, 8, 9, 9};
  166. var result = new [] {1, 2, 3, 6, 7, 8, 9};
  167. var set = new HashSet<int> (data);
  168. set.SymmetricExceptWith (other);
  169. AssertContainsOnly (result, set);
  170. }
  171. [Test]
  172. public void TestEmptyHashSubsetOf ()
  173. {
  174. var set = new HashSet<int> ();
  175. Assert.IsTrue (set.IsSubsetOf (new int [0]));
  176. Assert.IsTrue (set.IsSubsetOf (new [] {1, 2}));
  177. }
  178. [Test]
  179. public void TestSubsetOf ()
  180. {
  181. var data = new [] {1, 2, 3};
  182. var other = new [] {1, 2, 3, 4, 5};
  183. var other2 = new [] {1, 2, 3};
  184. var other3 = new [] {0, 1, 2};
  185. var set = new HashSet<int> (data);
  186. Assert.IsTrue (set.IsSubsetOf (other));
  187. Assert.IsTrue (set.IsSubsetOf (other2));
  188. Assert.IsFalse (set.IsSubsetOf (other3));
  189. }
  190. [Test]
  191. public void TestProperSubsetOf ()
  192. {
  193. var data = new [] {1, 2, 3};
  194. var other = new [] {1, 2, 3, 4, 5};
  195. var other2 = new [] {1, 2, 3};
  196. var other3 = new [] {0, 1, 2};
  197. var set = new HashSet<int> (data);
  198. Assert.IsTrue (set.IsProperSubsetOf (other));
  199. Assert.IsFalse (set.IsProperSubsetOf (other2));
  200. Assert.IsFalse (set.IsProperSubsetOf (other3));
  201. }
  202. [Test]
  203. public void TestSupersetOf ()
  204. {
  205. var data = new [] {1, 2, 3, 4, 5};
  206. var other = new [] {2, 3, 4};
  207. var other2 = new [] {1, 2, 3, 4, 5};
  208. var other3 = new [] {4, 5, 6};
  209. var set = new HashSet<int> (data);
  210. Assert.IsTrue (set.IsSupersetOf (other));
  211. Assert.IsTrue (set.IsSupersetOf (other2));
  212. Assert.IsFalse (set.IsSupersetOf (other3));
  213. }
  214. [Test]
  215. public void TestProperSupersetOf ()
  216. {
  217. var data = new [] {1, 2, 3, 4, 5};
  218. var other = new [] {2, 3, 4};
  219. var other2 = new [] {1, 2, 3, 4, 5};
  220. var other3 = new [] {4, 5, 6};
  221. var set = new HashSet<int> (data);
  222. Assert.IsTrue (set.IsProperSupersetOf (other));
  223. Assert.IsFalse (set.IsProperSupersetOf (other2));
  224. Assert.IsFalse (set.IsProperSupersetOf (other3));
  225. }
  226. [Test]
  227. public void TestSetEquals ()
  228. {
  229. var data = new [] {1, 2, 3, 4};
  230. var other = new [] {1, 2, 3, 4};
  231. var other2 = new [] {1, 2, 2, 4};
  232. var other3 = new [] {1, 2};
  233. var other4 = new [] {1, 2, 3, 4, 5};
  234. var other5 = new [] {1, 1, 1, 1};
  235. var set = new HashSet<int> (data);
  236. Assert.IsTrue (set.SetEquals (other));
  237. Assert.IsFalse (set.SetEquals (other2));
  238. Assert.IsFalse (set.SetEquals (other3));
  239. Assert.IsFalse (set.SetEquals (other4));
  240. Assert.IsFalse (set.SetEquals (other5));
  241. }
  242. [Test]
  243. public void TestCopyToFull ()
  244. {
  245. var data = new [] {1, 2, 3, 4};
  246. var set = new HashSet<int> (data);
  247. var res = new int [set.Count];
  248. set.CopyTo (res, 0);
  249. AssertContainsOnly (res, data);
  250. }
  251. [Test]
  252. public void TestCopyToEmpty ()
  253. {
  254. var set = new HashSet<int> ();
  255. var res = new int [0];
  256. set.CopyTo (res, 0);
  257. }
  258. [Test]
  259. public void TestCopyToPrecise ()
  260. {
  261. var set = new HashSet<int> ();
  262. set.Add (42);
  263. var dest = new int [12];
  264. set.CopyTo (dest, 6, 1);
  265. Assert.AreEqual (42, dest [6]);
  266. }
  267. [Test]
  268. public void TestICollection ()
  269. {
  270. var set = new HashSet<int> () as ICollection<int>;
  271. set.Add (42);
  272. set.Add (42);
  273. Assert.AreEqual (1, set.Count);
  274. }
  275. static void AssertContainsOnly<T> (IEnumerable<T> result, IEnumerable<T> data)
  276. {
  277. Assert.AreEqual (result.Count (), data.Count ());
  278. var store = new List<T> (result);
  279. foreach (var element in data) {
  280. Assert.IsTrue (store.Contains (element));
  281. store.Remove (element);
  282. }
  283. AssertIsEmpty (store);
  284. }
  285. static void AssertIsEmpty<T> (IEnumerable<T> source)
  286. {
  287. Assert.AreEqual (0, source.Count ());
  288. }
  289. delegate void D ();
  290. bool Throws (D d)
  291. {
  292. try {
  293. d ();
  294. return false;
  295. } catch {
  296. return true;
  297. }
  298. }
  299. [Test]
  300. // based on #491858, #517415
  301. public void Enumerator_Current ()
  302. {
  303. #pragma warning disable 0168
  304. var e1 = new HashSet<int>.Enumerator ();
  305. Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
  306. var d = new HashSet<int> ();
  307. var e2 = d.GetEnumerator ();
  308. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  309. e2.MoveNext ();
  310. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  311. e2.Dispose ();
  312. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  313. var e3 = ((IEnumerable<int>) d).GetEnumerator ();
  314. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  315. e3.MoveNext ();
  316. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  317. e3.Dispose ();
  318. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  319. var e4 = ((IEnumerable) d).GetEnumerator ();
  320. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  321. e4.MoveNext ();
  322. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  323. ((IDisposable) e4).Dispose ();
  324. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  325. #pragma warning restore 0168
  326. }
  327. [Test]
  328. public void TestNullsWithComparerThrowingException ()
  329. {
  330. // NOTE: We should get the same errors when using StringComparer.Ordinal on Mono 2.6.1, but the look-alike gives us more control over this test case
  331. var set = new HashSet<string> (new StringComparerOrdinalLookAlike ());
  332. Assert.IsTrue (set.Add (string.Empty), "#1a");
  333. Assert.IsFalse (set.Contains (null), "#2a");
  334. Assert.IsTrue (set.Add (null), "#2b");
  335. Assert.IsTrue (set.Contains (null), "#2c");
  336. Assert.AreEqual (2, set.Count, "#3");
  337. Assert.IsTrue (set.Add ("a"), "#4");
  338. AssertContainsOnly (new string [] { string.Empty, null, "a" }, set);
  339. Assert.IsFalse (set.Add (null), "#5");
  340. Assert.IsTrue (set.Add ("b"), "#6");
  341. Assert.IsFalse (set.Add ("b"), "#7");
  342. Assert.IsFalse (set.Add (string.Empty), "#8");
  343. Assert.IsFalse (set.Add ("a"), "#9");
  344. Assert.IsFalse (set.Add (null), "#10");
  345. Assert.IsTrue (set.Add ("c"), "#11");
  346. Assert.IsFalse (set.Add ("c"), "#12");
  347. Assert.AreEqual (5, set.Count, "#13");
  348. Assert.IsTrue (set.Remove (null), "#14");
  349. Assert.IsTrue (set.Remove ("b"), "#15");
  350. Assert.IsFalse (set.Remove (null), "#16");
  351. Assert.AreEqual (3, set.Count, "#17");
  352. AssertContainsOnly (new string [] { string.Empty, "a", "c" }, set);
  353. }
  354. private class StringComparerOrdinalLookAlike : IEqualityComparer<string>
  355. {
  356. public bool Equals(string x, string y)
  357. {
  358. return string.CompareOrdinal(x, y) == 0;
  359. }
  360. public int GetHashCode(string str)
  361. {
  362. if (str != null)
  363. return str.GetHashCode();
  364. throw new ArgumentNullException (); // Important aspect for test (same as what StringComparer.Ordinal does, and different from GenericEqualityComparer<string>)
  365. }
  366. }
  367. }
  368. }