PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 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
 26void test_hashset_add () {
 27	// Check adding of strings
 28	var hashset = new HashSet<string> (str_hash, str_equal);
 29
 30	hashset.add ("42");
 31	assert (hashset.contains ("42"));
 32	assert (hashset.size == 1);
 33	
 34	hashset.add ("43");
 35	assert (hashset.contains ("42"));
 36	assert (hashset.contains ("43"));
 37	assert (hashset.size == 2);
 38	
 39	// Check add same element
 40	assert (hashset.size == 2);
 41	hashset.add ("43");
 42	assert (hashset.contains ("42"));
 43	assert (hashset.contains ("43"));
 44	assert (hashset.size == 2);
 45	
 46	// Check adding of ints
 47	var hashsetInt = new HashSet<int> ();
 48
 49	hashsetInt.add (42);
 50	assert (hashsetInt.contains (42));
 51	assert (hashsetInt.size == 1);
 52	
 53	hashsetInt.add (43);
 54	assert (hashsetInt.contains (42));
 55	assert (hashsetInt.contains (43));
 56	assert (hashsetInt.size == 2);
 57	
 58	// Check add same element
 59	assert (hashsetInt.size == 2);
 60	hashsetInt.add (43);
 61	assert (hashsetInt.contains (42));
 62	assert (hashsetInt.contains (43));
 63	assert (hashsetInt.size == 2);
 64	
 65	// Check adding of objects
 66	var hashsetObject = new HashSet<Object> ();
 67	
 68	var fooObject = new Object();
 69	hashsetObject.add (fooObject);
 70	assert (hashsetObject.contains (fooObject));
 71	assert (hashsetObject.size == 1);
 72	
 73	var fooObject2 = new Object();
 74	hashsetObject.add (fooObject2);
 75	assert (hashsetObject.contains (fooObject));
 76	assert (hashsetObject.contains (fooObject2));
 77	assert (hashsetObject.size == 2);
 78	
 79	// Check add same element
 80	assert (hashsetObject.size == 2);
 81	hashsetObject.add (fooObject2);
 82	assert (hashsetObject.contains (fooObject));
 83	assert (hashsetObject.contains (fooObject2));
 84	assert (hashsetObject.size == 2);
 85}
 86
 87void test_hashset_clear () {
 88	var hashset = new HashSet<string> (str_hash, str_equal);
 89	assert (hashset.size == 0);
 90	
 91	// Check clear on empty set
 92	hashset.clear ();
 93	assert (hashset.size == 0);
 94	
 95	// Check clear one item
 96	hashset.add ("1");
 97	assert (hashset.size == 1);
 98	hashset.clear ();
 99	assert (hashset.size == 0);
100	
101	// Check clear multiple items
102	hashset.add ("1");
103	hashset.add ("2");
104	hashset.add ("3");
105	assert (hashset.size == 3);
106	hashset.clear ();
107	assert (hashset.size == 0);
108}
109
110void test_hashset_contains () {
111	var hashsetString = new HashSet<string> (str_hash, str_equal);
112	
113	// Check on empty set
114	assert (!hashsetString.contains ("1"));
115	
116	// Check items
117	hashsetString.add ("10");
118	assert (hashsetString.contains ("10"));
119	assert (!hashsetString.contains ("20"));
120	assert (!hashsetString.contains ("30"));
121	
122	hashsetString.add ("20");
123	assert (hashsetString.contains ("10"));
124	assert (hashsetString.contains ("20"));
125	assert (!hashsetString.contains ("30"));
126	
127	hashsetString.add ("30");
128	assert (hashsetString.contains ("10"));
129	assert (hashsetString.contains ("20"));
130	assert (hashsetString.contains ("30"));
131	
132	// Clear and recheck
133	hashsetString.clear ();
134	assert (!hashsetString.contains ("10"));
135	assert (!hashsetString.contains ("20"));
136	assert (!hashsetString.contains ("30"));
137	
138	var hashsetInt = new HashSet<int> ();
139	
140	// Check items
141	hashsetInt.add (10);
142	assert (hashsetInt.contains (10));
143	assert (!hashsetInt.contains (20));
144	assert (!hashsetInt.contains (30));
145	
146	hashsetInt.add (20);
147	assert (hashsetInt.contains (10));
148	assert (hashsetInt.contains (20));
149	assert (!hashsetInt.contains (30));
150	
151	hashsetInt.add (30);
152	assert (hashsetInt.contains (10));
153	assert (hashsetInt.contains (20));
154	assert (hashsetInt.contains (30));
155	
156	// Clear and recheck
157	hashsetInt.clear ();
158	assert (!hashsetInt.contains (10));
159	assert (!hashsetInt.contains (20));
160	assert (!hashsetInt.contains (30));
161}
162
163void test_hashset_remove () {
164	var hashsetString = new HashSet<string> (str_hash, str_equal);
165	
166	// Check remove if list is empty
167	hashsetString.remove ("42");
168	
169	// Add 5 different elements
170	hashsetString.add ("42");
171	hashsetString.add ("43");
172	hashsetString.add ("44");
173	hashsetString.add ("45");
174	hashsetString.add ("46");
175	assert (hashsetString.size == 5);
176	
177	// Check remove first
178	hashsetString.remove ("42");
179	assert (hashsetString.size == 4);
180	assert (hashsetString.contains ("43"));
181	assert (hashsetString.contains ("44"));
182	assert (hashsetString.contains ("45"));
183	assert (hashsetString.contains ("46"));
184	
185	// Check remove last
186	hashsetString.remove ("46");
187	assert (hashsetString.size == 3);
188	assert (hashsetString.contains ("43"));
189	assert (hashsetString.contains ("44"));
190	assert (hashsetString.contains ("45"));
191	
192	// Check remove in between
193	hashsetString.remove ("44");
194	assert (hashsetString.size == 2);
195	assert (hashsetString.contains ("43"));
196	assert (hashsetString.contains ("45"));
197
198	// Check removing of int element
199	var hashsetInt = new HashSet<int> ();
200	
201	// Add 5 different elements
202	hashsetInt.add (42);
203	hashsetInt.add (43);
204	hashsetInt.add (44);
205	hashsetInt.add (45);
206	hashsetInt.add (46);
207	assert (hashsetInt.size == 5);
208	
209	// Remove first
210	hashsetInt.remove (42);
211	assert (hashsetInt.size == 4);
212	assert (hashsetInt.contains (43));
213	assert (hashsetInt.contains (44));
214	assert (hashsetInt.contains (45));
215	assert (hashsetInt.contains (46));
216	
217	// Remove last
218	hashsetInt.remove (46);
219	assert (hashsetInt.size == 3);
220	assert (hashsetInt.contains (43));
221	assert (hashsetInt.contains (44));
222	assert (hashsetInt.contains (45));
223	
224	// Remove in between
225	hashsetInt.remove (44);
226	assert (hashsetInt.size == 2);
227	assert (hashsetInt.contains (43));
228	assert (hashsetInt.contains (45));
229}
230
231void test_hashset_size () {
232	var hashset = new HashSet<string> (str_hash, str_equal);
233	
234	// Check empty list
235	assert (hashset.size == 0);
236	
237	// Check when one item
238	hashset.add ("1");
239	assert (hashset.size == 1);
240	
241	// Check when more items
242	hashset.add ("2");
243	assert (hashset.size == 2);
244	
245	// Check when items cleared
246	hashset.clear ();
247	assert (hashset.size == 0);
248}
249
250void test_hashset_iterator () {
251	var hashset = new HashSet<string> (str_hash, str_equal);
252	
253	// Check iterate empty list
254	var iterator = hashset.iterator ();
255	assert (!iterator.next());
256	
257	// Check iterate list
258	hashset.add ("42");
259	hashset.add ("43");
260	
261	iterator = hashset.iterator ();
262	
263	// A set is usually not ordered, so this is not a requirement 
264	assert (iterator.next());
265	string firstString = iterator.get();
266	assert (hashset.contains (firstString)); 
267	
268	assert (iterator.next());
269	string secondString = iterator.get();
270	assert (hashset.contains (secondString));
271	assert (!str_equal (firstString, secondString)); // they can not be identical neither equal
272	
273	assert (!iterator.next());
274}
275void main (string[] args) {
276	Test.init (ref args);
277
278	Test.add_func ("/HashSet/Collection/add", test_hashset_add);
279	Test.add_func ("/HashSet/Collection/clear", test_hashset_clear);
280	Test.add_func ("/HashSet/Collection/contains", test_hashset_contains);
281	Test.add_func ("/HashSet/Collection/remove", test_hashset_remove);
282	Test.add_func ("/HashSet/Collection/size", test_hashset_size);
283	
284	Test.add_func ("/HashSet/Iterable/iterator", test_hashset_iterator);
285
286	Test.run ();
287}
288