PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 * Lesser General Public License for more details.
 14
 15 * You should have received a copy of the GNU Lesser General Public
 16 * License along with this library; if not, write to the Free Software
 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 18 *
 19 * Author:
 20 * 	J?rg Billeter <j@bitron.ch>
 21 */
 22
 23using GLib;
 24using Gee;
 25
 26const string CODE_NOT_REACHABLE = "*code should not be reached*";
 27
 28void test_hashmap_get () {
 29	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
 30	
 31	// Check get from empty map
 32	assert (hashmap.get ("foo") == null);
 33	
 34	// Check get from map with one items
 35	hashmap.set ("key", "value");
 36	assert (hashmap.get ("key") == "value");
 37	
 38	// Check get from non-existing key
 39	assert (hashmap.get ("foo") == null);
 40	
 41	// Check get from map with multiple items
 42	hashmap.set ("key2", "value2");
 43	hashmap.set ("key3", "value3");
 44	assert (hashmap.get ("key") == "value");
 45	assert (hashmap.get ("key2") == "value2");
 46	assert (hashmap.get ("key3") == "value3");
 47	
 48}
 49
 50void test_hashmap_set () {
 51	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
 52	
 53	// check map is empty
 54	assert (hashmap.size == 0);
 55	
 56	// check set an item to map
 57	hashmap.set ("abc", "one");
 58	assert (hashmap.contains ("abc"));
 59	assert (hashmap.get ("abc") == "one");
 60	assert (hashmap.size == 1);
 61	
 62	// check set an item to map with same value
 63	hashmap.set ("def", "one");
 64	assert (hashmap.contains ("def"));
 65	assert (hashmap.get ("abc") == "one");
 66	assert (hashmap.get ("def") == "one");
 67	assert (hashmap.size == 2);
 68	
 69	// check set with same key
 70	hashmap.set ("def", "two");
 71	assert (hashmap.contains ("def"));
 72	assert (hashmap.get ("abc") == "one");
 73	assert (hashmap.get ("def") == "two");
 74	assert (hashmap.size == 2);
 75}
 76
 77void test_hashmap_remove () {
 78	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
 79	
 80	// check removing when map is empty
 81	hashmap.remove ("foo");
 82	assert (hashmap.size == 0);
 83	
 84	// add items
 85	hashmap.set ("aaa", "111");
 86	hashmap.set ("bbb", "222");
 87	hashmap.set ("ccc", "333");
 88	hashmap.set ("ddd", "444");
 89	assert (hashmap.size == 4);
 90	
 91	// check remove on first place
 92	hashmap.remove ("aaa");
 93	assert (hashmap.size == 3);
 94	
 95	// check remove in between 
 96	hashmap.remove ("ccc");
 97	assert (hashmap.size == 2);
 98	
 99	// check remove in last place
100	hashmap.remove ("ddd");
101	assert (hashmap.size == 1);
102	
103	// check remove invalid key
104	hashmap.remove ("bar");
105	
106	// check remove last in map
107	hashmap.remove ("bbb");
108	assert (hashmap.size == 0);
109}
110
111void test_hashmap_contains () {
112	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
113	
114	// Check on empty map
115	assert (!hashmap.contains ("111"));
116	
117	// Check items
118	hashmap.set ("10", "111");
119	assert (hashmap.contains ("10"));
120	assert (!hashmap.contains ("20"));
121	assert (!hashmap.contains ("30"));
122	
123	assert (hashmap.get ("10") == "111");
124	
125	hashmap.set ("20", "222");
126	assert (hashmap.contains ("10"));
127	assert (hashmap.contains ("20"));
128	assert (!hashmap.contains ("30"));
129	
130	assert (hashmap.get ("10") == "111");
131	assert (hashmap.get ("20") == "222");
132	
133	hashmap.set ("30", "333");
134	assert (hashmap.contains ("10"));
135	assert (hashmap.contains ("20"));
136	assert (hashmap.contains ("30"));
137	
138	assert (hashmap.get ("10") == "111");
139	assert (hashmap.get ("20") == "222");
140	assert (hashmap.get ("30") == "333");
141	
142	// Clear and recheck
143	hashmap.clear ();
144	assert (!hashmap.contains ("10"));
145	assert (!hashmap.contains ("20"));
146	assert (!hashmap.contains ("30"));
147	
148	var hashmapOfInt = new HashMap<int,int> ();
149	
150	// Check items
151	hashmapOfInt.set (10, 111);
152	assert (hashmapOfInt.contains (10));
153	assert (!hashmapOfInt.contains (20));
154	assert (!hashmapOfInt.contains (30));
155	
156	assert (hashmapOfInt.get (10) == 111);
157	
158	hashmapOfInt.set (20, 222);
159	assert (hashmapOfInt.contains (10));
160	assert (hashmapOfInt.contains (20));
161	assert (!hashmapOfInt.contains (30));
162	
163	assert (hashmapOfInt.get (10) == 111);
164	assert (hashmapOfInt.get (20) == 222);
165	
166	hashmapOfInt.set (30, 333);
167	assert (hashmapOfInt.contains (10));
168	assert (hashmapOfInt.contains (20));
169	assert (hashmapOfInt.contains (30));
170	
171	assert (hashmapOfInt.get (10) == 111);
172	assert (hashmapOfInt.get (20) == 222);
173	assert (hashmapOfInt.get (30) == 333);
174}
175
176void test_hashmap_size () {
177	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
178	
179	// Check empty map
180	assert (hashmap.size == 0);
181	
182	// Check when one item
183	hashmap.set ("1", "1");
184	assert (hashmap.size == 1);
185	
186	// Check when more items
187	hashmap.set ("2", "2");
188	assert (hashmap.size == 2);
189	
190	// Check when items cleared
191	hashmap.clear ();
192	assert (hashmap.size == 0);
193}
194
195void test_hashmap_get_keys () {
196	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
197	
198	// Check keys on empty map
199	var keySet = hashmap.get_keys ();
200	assert (keySet.size == 0);
201	
202	// Check keys on map with one item
203	hashmap.set ("aaa", "111");
204	assert (keySet.size == 1);
205	assert (keySet.contains ("aaa"));
206	keySet = hashmap.get_keys ();
207	assert (keySet.size == 1);
208	assert (keySet.contains ("aaa"));
209	
210	// Check modify key set directly
211	if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
212		keySet.add ("ccc");
213		return;
214	}
215	Test.trap_assert_failed ();
216	Test.trap_assert_stderr (CODE_NOT_REACHABLE);
217	
218	// Check keys on map with multiple items
219	hashmap.set ("bbb", "222");
220	assert (keySet.size == 2);
221	assert (keySet.contains ("aaa"));
222	assert (keySet.contains ("bbb"));
223	keySet = hashmap.get_keys ();
224	assert (keySet.size == 2);
225	assert (keySet.contains ("aaa"));
226	assert (keySet.contains ("bbb"));
227	
228	// Check keys on map clear
229	hashmap.clear ();
230	assert (keySet.size == 0);
231	keySet = hashmap.get_keys ();
232	assert (keySet.size == 0);
233	
234}
235
236void test_hashmap_get_values () {
237	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
238	
239	// Check keys on empty map
240	var valueCollection = hashmap.get_values ();
241	assert (valueCollection.size == 0);
242	
243	// Check keys on map with one item
244	hashmap.set ("aaa", "111");
245	assert (valueCollection.size == 1);
246	assert (valueCollection.contains ("111"));
247	valueCollection = hashmap.get_values ();
248	assert (valueCollection.size == 1);
249	assert (valueCollection.contains ("111"));
250	
251	// Check modify key set directly
252	if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
253		valueCollection.add ("ccc");
254		return;
255	}
256	Test.trap_assert_failed ();
257	Test.trap_assert_stderr (CODE_NOT_REACHABLE);
258	
259	// Check keys on map with multiple items
260	hashmap.set ("bbb", "222");
261	assert (valueCollection.size == 2);
262	assert (valueCollection.contains ("111"));
263	assert (valueCollection.contains ("222"));
264	valueCollection = hashmap.get_values ();
265	assert (valueCollection.size == 2);
266	assert (valueCollection.contains ("111"));
267	assert (valueCollection.contains ("222"));
268	
269	// Check keys on map clear
270	hashmap.clear ();
271	assert (valueCollection.size == 0);
272	valueCollection = hashmap.get_values ();
273	assert (valueCollection.size == 0);
274
275}
276
277void test_hashmap_clear () {
278	var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
279	assert (hashmap.size == 0);
280	
281	// Check clear on empty map
282	hashmap.clear ();
283	assert (hashmap.size == 0);
284	
285	// Check clear one item
286	hashmap.set ("1", "1");
287	assert (hashmap.size == 1);
288	hashmap.clear ();
289	assert (hashmap.size == 0);
290	
291	// Check clear multiple items
292	hashmap.set ("1", "1");
293	hashmap.set ("2", "2");
294	hashmap.set ("3", "3");
295	assert (hashmap.size == 3);
296	hashmap.clear ();
297	assert (hashmap.size == 0);
298}
299
300void main (string[] args) {
301	Test.init (ref args);
302
303	Test.add_func ("/HashMap/Map/get", test_hashmap_get);
304	Test.add_func ("/HashMap/Map/set", test_hashmap_set);
305	Test.add_func ("/HashMap/Map/remove", test_hashmap_remove);
306	Test.add_func ("/HashMap/Map/contains", test_hashmap_contains);
307	Test.add_func ("/HashMap/Map/size", test_hashmap_size);
308	Test.add_func ("/HashMap/Map/get_keys", test_hashmap_get_keys);
309	Test.add_func ("/HashMap/Map/get_values", test_hashmap_get_values);
310	Test.add_func ("/HashMap/Map/clear", test_hashmap_clear);
311
312	Test.run ();
313}
314