3 * Copyright (C) 2008 Jürg Billeter
4 * Copyright (C) 2009 Didier Villevalois, Julien Peeters
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jürg Billeter <j@bitron.ch>
22 * Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
23 * Julien Peeters <contact@julienpeeters.fr>
29 public abstract class MultiMapTests : Gee.TestCase {
31 public MultiMapTests (string name) {
33 add_test ("[MultiMap] type correctness", test_type_correctness);
34 add_test ("[MultiMap] size", test_size);
35 add_test ("[MultiMap] getting and setting", test_getting_setting);
36 add_test ("[MultiMap] keys, all keys and values", test_keys_all_keys_values);
39 protected MultiMap<string,string> test_multi_map;
41 public void test_type_correctness () {
42 // Check the multimap exists
43 assert (test_multi_map != null);
45 // Check the advertised key and value types
46 assert (test_multi_map.key_type == typeof (string));
47 assert (test_multi_map.value_type == typeof (string));
50 private void test_size () {
51 // Check the map exists
52 assert (test_multi_map != null);
54 assert (test_multi_map.size == 0);
55 test_multi_map.set ("0", "0");
56 assert (test_multi_map.size == 1);
57 test_multi_map.set ("0", "1");
58 assert (test_multi_map.size == 2);
59 test_multi_map.remove ("0", "1");
60 assert (test_multi_map.size == 1);
61 test_multi_map.set ("0", "1");
62 test_multi_map.remove_all ("0");
63 assert (test_multi_map.size == 0);
64 test_multi_map.set ("0", "0");
65 assert (test_multi_map.size == 1);
66 test_multi_map.set ("1", "1");
67 assert (test_multi_map.size == 2);
70 private void test_getting_setting () {
71 // Check the map exists
72 assert (test_multi_map != null);
74 test_multi_map.set ("0", "0");
75 assert (test_multi_map.contains ("0"));
76 assert (test_multi_map.get ("0").size == 1);
77 assert (test_multi_map.get ("0").contains ("0"));
79 assert (test_multi_map.get ("1").size == 0);
81 test_multi_map.set ("0", "1");
82 assert (test_multi_map.get ("0").size == 2);
83 assert (test_multi_map.get ("0").contains ("0"));
84 assert (test_multi_map.get ("0").contains ("1"));
86 test_multi_map.set ("1", "1");
87 assert (test_multi_map.contains ("1"));
88 assert (test_multi_map.get ("0").size == 2);
89 assert (test_multi_map.get ("0").contains ("0"));
90 assert (test_multi_map.get ("0").contains ("1"));
91 assert (test_multi_map.get ("1").size == 1);
92 assert (test_multi_map.get ("0").contains ("1"));
94 // Check remove if bindings exist
95 assert (test_multi_map.remove ("0", "0"));
96 assert (test_multi_map.contains ("0"));
97 assert (! test_multi_map.get ("0").contains ("0"));
98 assert (test_multi_map.get ("0").contains ("1"));
99 assert (test_multi_map.contains ("1"));
100 assert (test_multi_map.get ("1").contains ("1"));
102 // Check remove if only one binding exists
103 assert (test_multi_map.remove ("0", "1"));
104 assert (! test_multi_map.contains ("0"));
105 assert (! test_multi_map.get ("0").contains ("0"));
106 assert (! test_multi_map.get ("0").contains ("1"));
107 assert (test_multi_map.contains ("1"));
108 assert (test_multi_map.get ("1").contains ("1"));
110 // Check remove if no binding exists
111 assert (! test_multi_map.remove ("0", "1"));
112 assert (! test_multi_map.contains ("0"));
113 assert (! test_multi_map.get ("0").contains ("0"));
114 assert (! test_multi_map.get ("0").contains ("1"));
115 assert (test_multi_map.contains ("1"));
116 assert (test_multi_map.get ("1").contains ("1"));
118 test_multi_map.clear ();
119 assert (! test_multi_map.contains ("0"));
120 assert (! test_multi_map.contains ("1"));
123 test_multi_map.set ("0", "0");
124 test_multi_map.set ("0", "1");
125 test_multi_map.set ("1", "1");
126 assert (test_multi_map.size == 3);
128 assert (test_multi_map.contains ("0"));
129 assert (test_multi_map.contains ("1"));
130 assert (test_multi_map.get ("0").size == 2);
131 assert (test_multi_map.get ("0").contains ("0"));
132 assert (test_multi_map.get ("0").contains ("1"));
133 assert (test_multi_map.get ("1").size == 1);
134 assert (test_multi_map.get ("0").contains ("1"));
136 // Check remove_all if bindings exist
137 assert (test_multi_map.remove_all ("0"));
138 assert (! test_multi_map.contains ("0"));
139 assert (! test_multi_map.get ("0").contains ("0"));
140 assert (! test_multi_map.get ("0").contains ("1"));
141 assert (test_multi_map.contains ("1"));
142 assert (test_multi_map.get ("1").contains ("1"));
144 // Check remove_all if no binding exists
145 assert (! test_multi_map.remove_all ("0"));
146 assert (! test_multi_map.contains ("0"));
147 assert (! test_multi_map.get ("0").contains ("0"));
148 assert (! test_multi_map.get ("0").contains ("1"));
149 assert (test_multi_map.contains ("1"));
150 assert (test_multi_map.get ("1").contains ("1"));
153 private void test_keys_all_keys_values () {
154 // Check the map exists
155 assert (test_multi_map != null);
157 test_multi_map.set ("0", "0");
158 test_multi_map.set ("0", "1");
159 test_multi_map.set ("1", "1");
161 // Check for keys, all_keys and values
162 Set<string> keys = test_multi_map.get_keys ();
163 MultiSet<string> all_keys = test_multi_map.get_all_keys ();
164 Collection<string> values = test_multi_map.get_values ();
166 assert (keys.contains ("0"));
167 assert (keys.contains ("1"));
168 assert (all_keys.count ("0") == 2);
169 assert (all_keys.count ("1") == 1);
170 assert (values.contains ("0"));
171 assert (values.contains ("1"));
173 bool zero_found = false;
174 bool zero_found_once = true;
175 bool one_found = false;
176 bool one_found_twice = false;
177 bool nothing_more = true;
178 foreach (string value in values) {
181 zero_found_once = false;
184 } else if (value == "1") {
186 if (one_found_twice) {
187 one_found_twice = false;
189 one_found_twice = true;
194 nothing_more = false;
198 assert (zero_found_once);
200 assert (one_found_twice);
201 assert (nothing_more);