HashSetTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. public void TestCopyTo ()
  83. {
  84. var data = new [] {1, 2, 3, 4, 5};
  85. var set = new HashSet<int> (data);
  86. var array = new int [set.Count];
  87. set.CopyTo (array, 0);
  88. AssertContainsOnly (data, array);
  89. }
  90. [Test]
  91. public void TestClear ()
  92. {
  93. var data = new [] {1, 2, 3, 4, 5, 6};
  94. var set = new HashSet<int> (data);
  95. Assert.AreEqual (data.Length, set.Count);
  96. set.Clear ();
  97. AssertIsEmpty (set);
  98. }
  99. [Test]
  100. public void TestContains ()
  101. {
  102. var data = new [] {1, 2, 3, 4, 5, 6};
  103. var set = new HashSet<int> (data);
  104. foreach (var item in data)
  105. Assert.IsTrue (set.Contains (item));
  106. }
  107. [Test, ExpectedException (typeof (InvalidOperationException))]
  108. public void TestModifySetWhileForeach ()
  109. {
  110. var set = new HashSet<int> (new [] {1, 2, 3, 4});
  111. foreach (var item in set)
  112. set.Add (item + 2);
  113. }
  114. [Test]
  115. public void TestRemoveWhere ()
  116. {
  117. var data = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  118. var result = new [] {2, 4, 6, 8};
  119. var set = new HashSet<int> (data);
  120. int removed = set.RemoveWhere (i => (i % 2) != 0);
  121. Assert.AreEqual (data.Length - result.Length, removed);
  122. AssertContainsOnly (result, set);
  123. }
  124. [Test]
  125. public void TestOverlaps ()
  126. {
  127. var set = new HashSet<int> (new [] {1, 2, 3, 4, 5});
  128. Assert.IsTrue (set.Overlaps (new [] {0, 2}));
  129. }
  130. [Test]
  131. public void TestIntersectWith ()
  132. {
  133. var data = new [] {1, 2, 3, 4};
  134. var other = new [] {2, 4, 5, 6};
  135. var result = new [] {2, 4};
  136. var set = new HashSet<int> (data);
  137. set.IntersectWith (other);
  138. AssertContainsOnly (result, set);
  139. }
  140. [Test]
  141. public void TestExceptWith ()
  142. {
  143. var data = new [] {1, 2, 3, 4, 5, 6};
  144. var other = new [] {2, 4, 6};
  145. var result = new [] {1, 3, 5};
  146. var set = new HashSet<int> (data);
  147. set.ExceptWith (other);
  148. AssertContainsOnly (result, set);
  149. }
  150. [Test]
  151. public void TestUnionWith ()
  152. {
  153. var data = new [] {1, 2, 3, 4, 5, 6};
  154. var other = new [] {4, 5, 6, 7, 8, 9};
  155. var result = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  156. var set = new HashSet<int> (data);
  157. set.UnionWith (other);
  158. AssertContainsOnly (result, set);
  159. }
  160. [Test]
  161. public void TestSymmetricExceptWith ()
  162. {
  163. var data = new [] {1, 2, 3, 4, 5};
  164. var other = new [] {4, 5, 6, 7, 8, 9};
  165. var result = new [] {1, 2, 3, 6, 7, 8, 9};
  166. var set = new HashSet<int> (data);
  167. set.SymmetricExceptWith (other);
  168. AssertContainsOnly (result, set);
  169. }
  170. [Test]
  171. public void TestEmptyHashSubsetOf ()
  172. {
  173. var set = new HashSet<int> ();
  174. Assert.IsTrue (set.IsSubsetOf (new int [0]));
  175. Assert.IsTrue (set.IsSubsetOf (new [] {1, 2}));
  176. }
  177. [Test]
  178. public void TestSubsetOf ()
  179. {
  180. var data = new [] {1, 2, 3};
  181. var other = new [] {1, 2, 3, 4, 5};
  182. var other2 = new [] {1, 2, 3};
  183. var other3 = new [] {0, 1, 2};
  184. var set = new HashSet<int> (data);
  185. Assert.IsTrue (set.IsSubsetOf (other));
  186. Assert.IsTrue (set.IsSubsetOf (other2));
  187. Assert.IsFalse (set.IsSubsetOf (other3));
  188. }
  189. [Test]
  190. public void TestProperSubsetOf ()
  191. {
  192. var data = new [] {1, 2, 3};
  193. var other = new [] {1, 2, 3, 4, 5};
  194. var other2 = new [] {1, 2, 3};
  195. var other3 = new [] {0, 1, 2};
  196. var set = new HashSet<int> (data);
  197. Assert.IsTrue (set.IsProperSubsetOf (other));
  198. Assert.IsFalse (set.IsProperSubsetOf (other2));
  199. Assert.IsFalse (set.IsProperSubsetOf (other3));
  200. }
  201. [Test]
  202. public void TestSupersetOf ()
  203. {
  204. var data = new [] {1, 2, 3, 4, 5};
  205. var other = new [] {2, 3, 4};
  206. var other2 = new [] {1, 2, 3, 4, 5};
  207. var other3 = new [] {4, 5, 6};
  208. var set = new HashSet<int> (data);
  209. Assert.IsTrue (set.IsSupersetOf (other));
  210. Assert.IsTrue (set.IsSupersetOf (other2));
  211. Assert.IsFalse (set.IsSupersetOf (other3));
  212. }
  213. [Test]
  214. public void TestProperSupersetOf ()
  215. {
  216. var data = new [] {1, 2, 3, 4, 5};
  217. var other = new [] {2, 3, 4};
  218. var other2 = new [] {1, 2, 3, 4, 5};
  219. var other3 = new [] {4, 5, 6};
  220. var set = new HashSet<int> (data);
  221. Assert.IsTrue (set.IsProperSupersetOf (other));
  222. Assert.IsFalse (set.IsProperSupersetOf (other2));
  223. Assert.IsFalse (set.IsProperSupersetOf (other3));
  224. }
  225. [Test]
  226. public void TestSetEquals ()
  227. {
  228. var data = new [] {1, 2, 3, 4};
  229. var other = new [] {1, 2, 3, 4};
  230. var other2 = new [] {1, 2, 2, 4};
  231. var other3 = new [] {1, 2};
  232. var other4 = new [] {1, 2, 3, 4, 5};
  233. var other5 = new [] {1, 1, 1, 1};
  234. var set = new HashSet<int> (data);
  235. Assert.IsTrue (set.SetEquals (other));
  236. Assert.IsFalse (set.SetEquals (other2));
  237. Assert.IsFalse (set.SetEquals (other3));
  238. Assert.IsFalse (set.SetEquals (other4));
  239. Assert.IsFalse (set.SetEquals (other5));
  240. }
  241. static void AssertContainsOnly<T> (IEnumerable<T> result, IEnumerable<T> data)
  242. {
  243. Assert.AreEqual (result.Count (), data.Count ());
  244. var store = new List<T> (result);
  245. foreach (var element in data) {
  246. Assert.IsTrue (store.Contains (element));
  247. store.Remove (element);
  248. }
  249. AssertIsEmpty (store);
  250. }
  251. static void AssertIsEmpty<T> (IEnumerable<T> source)
  252. {
  253. Assert.AreEqual (0, source.Count ());
  254. }
  255. }
  256. }