/gee/tests/testhashset.vala

http://libgdom3.googlecode.com/ · Vala · 288 lines · 181 code · 55 blank · 52 comment · 30 complexity · 5053b745d4c65675773936e98564a8c8 MD5 · raw file

  1. /* testhashset.vala
  2. *
  3. * Copyright (C) 2008 J?rg Billeter
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. *
  17. * Author:
  18. * J?rg Billeter <j@bitron.ch>
  19. */
  20. using GLib;
  21. using Gee;
  22. void test_hashset_add () {
  23. // Check adding of strings
  24. var hashset = new HashSet<string> (str_hash, str_equal);
  25. hashset.add ("42");
  26. assert (hashset.contains ("42"));
  27. assert (hashset.size == 1);
  28. hashset.add ("43");
  29. assert (hashset.contains ("42"));
  30. assert (hashset.contains ("43"));
  31. assert (hashset.size == 2);
  32. // Check add same element
  33. assert (hashset.size == 2);
  34. hashset.add ("43");
  35. assert (hashset.contains ("42"));
  36. assert (hashset.contains ("43"));
  37. assert (hashset.size == 2);
  38. // Check adding of ints
  39. var hashsetInt = new HashSet<int> ();
  40. hashsetInt.add (42);
  41. assert (hashsetInt.contains (42));
  42. assert (hashsetInt.size == 1);
  43. hashsetInt.add (43);
  44. assert (hashsetInt.contains (42));
  45. assert (hashsetInt.contains (43));
  46. assert (hashsetInt.size == 2);
  47. // Check add same element
  48. assert (hashsetInt.size == 2);
  49. hashsetInt.add (43);
  50. assert (hashsetInt.contains (42));
  51. assert (hashsetInt.contains (43));
  52. assert (hashsetInt.size == 2);
  53. // Check adding of objects
  54. var hashsetObject = new HashSet<Object> ();
  55. var fooObject = new Object();
  56. hashsetObject.add (fooObject);
  57. assert (hashsetObject.contains (fooObject));
  58. assert (hashsetObject.size == 1);
  59. var fooObject2 = new Object();
  60. hashsetObject.add (fooObject2);
  61. assert (hashsetObject.contains (fooObject));
  62. assert (hashsetObject.contains (fooObject2));
  63. assert (hashsetObject.size == 2);
  64. // Check add same element
  65. assert (hashsetObject.size == 2);
  66. hashsetObject.add (fooObject2);
  67. assert (hashsetObject.contains (fooObject));
  68. assert (hashsetObject.contains (fooObject2));
  69. assert (hashsetObject.size == 2);
  70. }
  71. void test_hashset_clear () {
  72. var hashset = new HashSet<string> (str_hash, str_equal);
  73. assert (hashset.size == 0);
  74. // Check clear on empty set
  75. hashset.clear ();
  76. assert (hashset.size == 0);
  77. // Check clear one item
  78. hashset.add ("1");
  79. assert (hashset.size == 1);
  80. hashset.clear ();
  81. assert (hashset.size == 0);
  82. // Check clear multiple items
  83. hashset.add ("1");
  84. hashset.add ("2");
  85. hashset.add ("3");
  86. assert (hashset.size == 3);
  87. hashset.clear ();
  88. assert (hashset.size == 0);
  89. }
  90. void test_hashset_contains () {
  91. var hashsetString = new HashSet<string> (str_hash, str_equal);
  92. // Check on empty set
  93. assert (!hashsetString.contains ("1"));
  94. // Check items
  95. hashsetString.add ("10");
  96. assert (hashsetString.contains ("10"));
  97. assert (!hashsetString.contains ("20"));
  98. assert (!hashsetString.contains ("30"));
  99. hashsetString.add ("20");
  100. assert (hashsetString.contains ("10"));
  101. assert (hashsetString.contains ("20"));
  102. assert (!hashsetString.contains ("30"));
  103. hashsetString.add ("30");
  104. assert (hashsetString.contains ("10"));
  105. assert (hashsetString.contains ("20"));
  106. assert (hashsetString.contains ("30"));
  107. // Clear and recheck
  108. hashsetString.clear ();
  109. assert (!hashsetString.contains ("10"));
  110. assert (!hashsetString.contains ("20"));
  111. assert (!hashsetString.contains ("30"));
  112. var hashsetInt = new HashSet<int> ();
  113. // Check items
  114. hashsetInt.add (10);
  115. assert (hashsetInt.contains (10));
  116. assert (!hashsetInt.contains (20));
  117. assert (!hashsetInt.contains (30));
  118. hashsetInt.add (20);
  119. assert (hashsetInt.contains (10));
  120. assert (hashsetInt.contains (20));
  121. assert (!hashsetInt.contains (30));
  122. hashsetInt.add (30);
  123. assert (hashsetInt.contains (10));
  124. assert (hashsetInt.contains (20));
  125. assert (hashsetInt.contains (30));
  126. // Clear and recheck
  127. hashsetInt.clear ();
  128. assert (!hashsetInt.contains (10));
  129. assert (!hashsetInt.contains (20));
  130. assert (!hashsetInt.contains (30));
  131. }
  132. void test_hashset_remove () {
  133. var hashsetString = new HashSet<string> (str_hash, str_equal);
  134. // Check remove if list is empty
  135. hashsetString.remove ("42");
  136. // Add 5 different elements
  137. hashsetString.add ("42");
  138. hashsetString.add ("43");
  139. hashsetString.add ("44");
  140. hashsetString.add ("45");
  141. hashsetString.add ("46");
  142. assert (hashsetString.size == 5);
  143. // Check remove first
  144. hashsetString.remove ("42");
  145. assert (hashsetString.size == 4);
  146. assert (hashsetString.contains ("43"));
  147. assert (hashsetString.contains ("44"));
  148. assert (hashsetString.contains ("45"));
  149. assert (hashsetString.contains ("46"));
  150. // Check remove last
  151. hashsetString.remove ("46");
  152. assert (hashsetString.size == 3);
  153. assert (hashsetString.contains ("43"));
  154. assert (hashsetString.contains ("44"));
  155. assert (hashsetString.contains ("45"));
  156. // Check remove in between
  157. hashsetString.remove ("44");
  158. assert (hashsetString.size == 2);
  159. assert (hashsetString.contains ("43"));
  160. assert (hashsetString.contains ("45"));
  161. // Check removing of int element
  162. var hashsetInt = new HashSet<int> ();
  163. // Add 5 different elements
  164. hashsetInt.add (42);
  165. hashsetInt.add (43);
  166. hashsetInt.add (44);
  167. hashsetInt.add (45);
  168. hashsetInt.add (46);
  169. assert (hashsetInt.size == 5);
  170. // Remove first
  171. hashsetInt.remove (42);
  172. assert (hashsetInt.size == 4);
  173. assert (hashsetInt.contains (43));
  174. assert (hashsetInt.contains (44));
  175. assert (hashsetInt.contains (45));
  176. assert (hashsetInt.contains (46));
  177. // Remove last
  178. hashsetInt.remove (46);
  179. assert (hashsetInt.size == 3);
  180. assert (hashsetInt.contains (43));
  181. assert (hashsetInt.contains (44));
  182. assert (hashsetInt.contains (45));
  183. // Remove in between
  184. hashsetInt.remove (44);
  185. assert (hashsetInt.size == 2);
  186. assert (hashsetInt.contains (43));
  187. assert (hashsetInt.contains (45));
  188. }
  189. void test_hashset_size () {
  190. var hashset = new HashSet<string> (str_hash, str_equal);
  191. // Check empty list
  192. assert (hashset.size == 0);
  193. // Check when one item
  194. hashset.add ("1");
  195. assert (hashset.size == 1);
  196. // Check when more items
  197. hashset.add ("2");
  198. assert (hashset.size == 2);
  199. // Check when items cleared
  200. hashset.clear ();
  201. assert (hashset.size == 0);
  202. }
  203. void test_hashset_iterator () {
  204. var hashset = new HashSet<string> (str_hash, str_equal);
  205. // Check iterate empty list
  206. var iterator = hashset.iterator ();
  207. assert (!iterator.next());
  208. // Check iterate list
  209. hashset.add ("42");
  210. hashset.add ("43");
  211. iterator = hashset.iterator ();
  212. // A set is usually not ordered, so this is not a requirement
  213. assert (iterator.next());
  214. string firstString = iterator.get();
  215. assert (hashset.contains (firstString));
  216. assert (iterator.next());
  217. string secondString = iterator.get();
  218. assert (hashset.contains (secondString));
  219. assert (!str_equal (firstString, secondString)); // they can not be identical neither equal
  220. assert (!iterator.next());
  221. }
  222. void main (string[] args) {
  223. Test.init (ref args);
  224. Test.add_func ("/HashSet/Collection/add", test_hashset_add);
  225. Test.add_func ("/HashSet/Collection/clear", test_hashset_clear);
  226. Test.add_func ("/HashSet/Collection/contains", test_hashset_contains);
  227. Test.add_func ("/HashSet/Collection/remove", test_hashset_remove);
  228. Test.add_func ("/HashSet/Collection/size", test_hashset_size);
  229. Test.add_func ("/HashSet/Iterable/iterator", test_hashset_iterator);
  230. Test.run ();
  231. }