/gee/tests/testhashmap.vala

http://libgdom3.googlecode.com/ · Vala · 314 lines · 193 code · 64 blank · 57 comment · 59 complexity · c32b35f71901f990f81d1b74731e5bcb MD5 · raw file

  1. /* testhashmap.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. const string CODE_NOT_REACHABLE = "*code should not be reached*";
  23. void test_hashmap_get () {
  24. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  25. // Check get from empty map
  26. assert (hashmap.get ("foo") == null);
  27. // Check get from map with one items
  28. hashmap.set ("key", "value");
  29. assert (hashmap.get ("key") == "value");
  30. // Check get from non-existing key
  31. assert (hashmap.get ("foo") == null);
  32. // Check get from map with multiple items
  33. hashmap.set ("key2", "value2");
  34. hashmap.set ("key3", "value3");
  35. assert (hashmap.get ("key") == "value");
  36. assert (hashmap.get ("key2") == "value2");
  37. assert (hashmap.get ("key3") == "value3");
  38. }
  39. void test_hashmap_set () {
  40. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  41. // check map is empty
  42. assert (hashmap.size == 0);
  43. // check set an item to map
  44. hashmap.set ("abc", "one");
  45. assert (hashmap.contains ("abc"));
  46. assert (hashmap.get ("abc") == "one");
  47. assert (hashmap.size == 1);
  48. // check set an item to map with same value
  49. hashmap.set ("def", "one");
  50. assert (hashmap.contains ("def"));
  51. assert (hashmap.get ("abc") == "one");
  52. assert (hashmap.get ("def") == "one");
  53. assert (hashmap.size == 2);
  54. // check set with same key
  55. hashmap.set ("def", "two");
  56. assert (hashmap.contains ("def"));
  57. assert (hashmap.get ("abc") == "one");
  58. assert (hashmap.get ("def") == "two");
  59. assert (hashmap.size == 2);
  60. }
  61. void test_hashmap_remove () {
  62. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  63. // check removing when map is empty
  64. hashmap.remove ("foo");
  65. assert (hashmap.size == 0);
  66. // add items
  67. hashmap.set ("aaa", "111");
  68. hashmap.set ("bbb", "222");
  69. hashmap.set ("ccc", "333");
  70. hashmap.set ("ddd", "444");
  71. assert (hashmap.size == 4);
  72. // check remove on first place
  73. hashmap.remove ("aaa");
  74. assert (hashmap.size == 3);
  75. // check remove in between
  76. hashmap.remove ("ccc");
  77. assert (hashmap.size == 2);
  78. // check remove in last place
  79. hashmap.remove ("ddd");
  80. assert (hashmap.size == 1);
  81. // check remove invalid key
  82. hashmap.remove ("bar");
  83. // check remove last in map
  84. hashmap.remove ("bbb");
  85. assert (hashmap.size == 0);
  86. }
  87. void test_hashmap_contains () {
  88. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  89. // Check on empty map
  90. assert (!hashmap.contains ("111"));
  91. // Check items
  92. hashmap.set ("10", "111");
  93. assert (hashmap.contains ("10"));
  94. assert (!hashmap.contains ("20"));
  95. assert (!hashmap.contains ("30"));
  96. assert (hashmap.get ("10") == "111");
  97. hashmap.set ("20", "222");
  98. assert (hashmap.contains ("10"));
  99. assert (hashmap.contains ("20"));
  100. assert (!hashmap.contains ("30"));
  101. assert (hashmap.get ("10") == "111");
  102. assert (hashmap.get ("20") == "222");
  103. hashmap.set ("30", "333");
  104. assert (hashmap.contains ("10"));
  105. assert (hashmap.contains ("20"));
  106. assert (hashmap.contains ("30"));
  107. assert (hashmap.get ("10") == "111");
  108. assert (hashmap.get ("20") == "222");
  109. assert (hashmap.get ("30") == "333");
  110. // Clear and recheck
  111. hashmap.clear ();
  112. assert (!hashmap.contains ("10"));
  113. assert (!hashmap.contains ("20"));
  114. assert (!hashmap.contains ("30"));
  115. var hashmapOfInt = new HashMap<int,int> ();
  116. // Check items
  117. hashmapOfInt.set (10, 111);
  118. assert (hashmapOfInt.contains (10));
  119. assert (!hashmapOfInt.contains (20));
  120. assert (!hashmapOfInt.contains (30));
  121. assert (hashmapOfInt.get (10) == 111);
  122. hashmapOfInt.set (20, 222);
  123. assert (hashmapOfInt.contains (10));
  124. assert (hashmapOfInt.contains (20));
  125. assert (!hashmapOfInt.contains (30));
  126. assert (hashmapOfInt.get (10) == 111);
  127. assert (hashmapOfInt.get (20) == 222);
  128. hashmapOfInt.set (30, 333);
  129. assert (hashmapOfInt.contains (10));
  130. assert (hashmapOfInt.contains (20));
  131. assert (hashmapOfInt.contains (30));
  132. assert (hashmapOfInt.get (10) == 111);
  133. assert (hashmapOfInt.get (20) == 222);
  134. assert (hashmapOfInt.get (30) == 333);
  135. }
  136. void test_hashmap_size () {
  137. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  138. // Check empty map
  139. assert (hashmap.size == 0);
  140. // Check when one item
  141. hashmap.set ("1", "1");
  142. assert (hashmap.size == 1);
  143. // Check when more items
  144. hashmap.set ("2", "2");
  145. assert (hashmap.size == 2);
  146. // Check when items cleared
  147. hashmap.clear ();
  148. assert (hashmap.size == 0);
  149. }
  150. void test_hashmap_get_keys () {
  151. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  152. // Check keys on empty map
  153. var keySet = hashmap.get_keys ();
  154. assert (keySet.size == 0);
  155. // Check keys on map with one item
  156. hashmap.set ("aaa", "111");
  157. assert (keySet.size == 1);
  158. assert (keySet.contains ("aaa"));
  159. keySet = hashmap.get_keys ();
  160. assert (keySet.size == 1);
  161. assert (keySet.contains ("aaa"));
  162. // Check modify key set directly
  163. if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
  164. keySet.add ("ccc");
  165. return;
  166. }
  167. Test.trap_assert_failed ();
  168. Test.trap_assert_stderr (CODE_NOT_REACHABLE);
  169. // Check keys on map with multiple items
  170. hashmap.set ("bbb", "222");
  171. assert (keySet.size == 2);
  172. assert (keySet.contains ("aaa"));
  173. assert (keySet.contains ("bbb"));
  174. keySet = hashmap.get_keys ();
  175. assert (keySet.size == 2);
  176. assert (keySet.contains ("aaa"));
  177. assert (keySet.contains ("bbb"));
  178. // Check keys on map clear
  179. hashmap.clear ();
  180. assert (keySet.size == 0);
  181. keySet = hashmap.get_keys ();
  182. assert (keySet.size == 0);
  183. }
  184. void test_hashmap_get_values () {
  185. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  186. // Check keys on empty map
  187. var valueCollection = hashmap.get_values ();
  188. assert (valueCollection.size == 0);
  189. // Check keys on map with one item
  190. hashmap.set ("aaa", "111");
  191. assert (valueCollection.size == 1);
  192. assert (valueCollection.contains ("111"));
  193. valueCollection = hashmap.get_values ();
  194. assert (valueCollection.size == 1);
  195. assert (valueCollection.contains ("111"));
  196. // Check modify key set directly
  197. if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
  198. valueCollection.add ("ccc");
  199. return;
  200. }
  201. Test.trap_assert_failed ();
  202. Test.trap_assert_stderr (CODE_NOT_REACHABLE);
  203. // Check keys on map with multiple items
  204. hashmap.set ("bbb", "222");
  205. assert (valueCollection.size == 2);
  206. assert (valueCollection.contains ("111"));
  207. assert (valueCollection.contains ("222"));
  208. valueCollection = hashmap.get_values ();
  209. assert (valueCollection.size == 2);
  210. assert (valueCollection.contains ("111"));
  211. assert (valueCollection.contains ("222"));
  212. // Check keys on map clear
  213. hashmap.clear ();
  214. assert (valueCollection.size == 0);
  215. valueCollection = hashmap.get_values ();
  216. assert (valueCollection.size == 0);
  217. }
  218. void test_hashmap_clear () {
  219. var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
  220. assert (hashmap.size == 0);
  221. // Check clear on empty map
  222. hashmap.clear ();
  223. assert (hashmap.size == 0);
  224. // Check clear one item
  225. hashmap.set ("1", "1");
  226. assert (hashmap.size == 1);
  227. hashmap.clear ();
  228. assert (hashmap.size == 0);
  229. // Check clear multiple items
  230. hashmap.set ("1", "1");
  231. hashmap.set ("2", "2");
  232. hashmap.set ("3", "3");
  233. assert (hashmap.size == 3);
  234. hashmap.clear ();
  235. assert (hashmap.size == 0);
  236. }
  237. void main (string[] args) {
  238. Test.init (ref args);
  239. Test.add_func ("/HashMap/Map/get", test_hashmap_get);
  240. Test.add_func ("/HashMap/Map/set", test_hashmap_set);
  241. Test.add_func ("/HashMap/Map/remove", test_hashmap_remove);
  242. Test.add_func ("/HashMap/Map/contains", test_hashmap_contains);
  243. Test.add_func ("/HashMap/Map/size", test_hashmap_size);
  244. Test.add_func ("/HashMap/Map/get_keys", test_hashmap_get_keys);
  245. Test.add_func ("/HashMap/Map/get_values", test_hashmap_get_values);
  246. Test.add_func ("/HashMap/Map/clear", test_hashmap_clear);
  247. Test.run ();
  248. }