1 /* testreadonlycollection.vala
3 * Copyright (C) 2008 Jürg Billeter
4 * Copyright (C) 2009 Didier Villevalois
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 * Tomaž Vajngerl <quikee@gmail.com>
22 * Julien Peeters <contact@julienpeeters.fr>
27 public class ReadOnlyCollectionTests : Gee.TestCase {
29 public ReadOnlyCollectionTests () {
30 this.with_name ("ReadOnlyCollection");
33 public ReadOnlyCollectionTests.with_name (string name) {
35 add_test ("[ReadOnlyCollection] unique read-only view instance",
36 test_unique_read_only_view_instance);
37 add_test ("[ReadOnlyCollection] immutable iterator", test_immutable_iterator);
38 add_test ("[ReadOnlyCollection] immutable", test_immutable);
39 add_test ("[ReadOnlyCollection] accurate view", test_accurate_view);
42 protected Collection<string> test_collection;
43 protected Collection<string> ro_collection;
45 public override void set_up () {
46 test_collection = new HashMultiSet<string> ();
47 ro_collection = get_ro_view (test_collection);
50 public override void tear_down () {
51 test_collection = null;
55 protected virtual Collection<string> get_ro_view (Collection<string> collection) {
56 return collection.read_only_view;
59 public void test_unique_read_only_view_instance () {
60 var another_ro_collection = get_ro_view (test_collection);
61 assert (ro_collection == another_ro_collection);
63 ro_collection.set_data ("marker", new Object ());
64 assert (another_ro_collection.get_data<Object> ("marker") != null);
66 another_ro_collection = null;
69 another_ro_collection = get_ro_view (test_collection);
70 assert (another_ro_collection.get_data<Object> ("marker") == null);
72 // Check that the read-only view of the view is itself
73 assert (another_ro_collection == get_ro_view (another_ro_collection));
76 public void test_immutable_iterator () {
77 assert (test_collection.add ("one"));
78 assert (test_collection.add ("two"));
80 assert (ro_collection.size == 2);
81 assert (ro_collection.contains ("one"));
82 assert (ro_collection.contains ("two"));
84 Iterator<string> iterator = ro_collection.iterator ();
86 bool one_found = false;
87 bool two_found = false;
89 while (iterator.next ()) {
90 assert (iterator.valid);
91 switch(iterator.get ()) {
101 assert_not_reached ();
108 assert (! iterator.has_next ());
109 assert (! iterator.next ());
111 iterator = ro_collection.iterator ();
112 assert (iterator.has_next ());
113 assert (iterator.next ());
115 assert (iterator.read_only);
116 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
117 TestTrapFlags.SILENCE_STDERR)) {
121 Test.trap_assert_failed ();
123 assert (ro_collection.size == 2);
124 assert (ro_collection.contains ("one"));
125 assert (ro_collection.contains ("two"));
128 public void test_immutable () {
129 assert (test_collection.add ("one"));
130 assert (ro_collection.size == 1);
131 assert (ro_collection.contains ("one"));
133 Collection<string> dummy = new ArrayList<string> ();
134 assert (dummy.add ("one"));
135 assert (dummy.add ("two"));
137 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
138 TestTrapFlags.SILENCE_STDERR)) {
139 assert (ro_collection.add ("two"));
142 Test.trap_assert_failed ();
143 assert (ro_collection.size == 1);
144 assert (ro_collection.contains ("one"));
146 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
147 TestTrapFlags.SILENCE_STDERR)) {
148 ro_collection.clear ();
151 Test.trap_assert_failed ();
152 assert (ro_collection.size == 1);
153 assert (ro_collection.contains ("one"));
155 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
156 TestTrapFlags.SILENCE_STDERR)) {
157 assert (ro_collection.remove ("one"));
160 Test.trap_assert_failed ();
161 assert (ro_collection.size == 1);
162 assert (ro_collection.contains ("one"));
164 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
165 TestTrapFlags.SILENCE_STDERR)) {
166 assert (ro_collection.add_all (dummy));
169 Test.trap_assert_failed ();
170 assert (ro_collection.size == 1);
171 assert (ro_collection.contains ("one"));
173 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
174 TestTrapFlags.SILENCE_STDERR)) {
175 assert (ro_collection.remove_all (dummy));
178 Test.trap_assert_failed ();
179 assert (ro_collection.size == 1);
180 assert (ro_collection.contains ("one"));
182 assert (dummy.remove ("one"));
183 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
184 TestTrapFlags.SILENCE_STDERR)) {
185 assert (ro_collection.retain_all (dummy));
188 Test.trap_assert_failed ();
189 assert (ro_collection.size == 1);
190 assert (ro_collection.contains ("one"));
193 public void test_accurate_view () {
194 Collection<string> dummy = new ArrayList<string> ();
195 assert (dummy.add ("one"));
196 assert (dummy.add ("two"));
198 assert (ro_collection.element_type == typeof (string));
200 assert (ro_collection.size == 0);
201 assert (ro_collection.is_empty);
202 assert (! ro_collection.contains ("one"));
204 assert (test_collection.add ("one"));
205 assert (ro_collection.size == 1);
206 assert (! ro_collection.is_empty);
207 assert (ro_collection.contains ("one"));
209 assert (test_collection.add ("two"));
210 assert (ro_collection.size == 2);
211 assert (! ro_collection.is_empty);
212 assert (ro_collection.contains ("one"));
213 assert (ro_collection.contains ("two"));
214 assert (ro_collection.contains_all (dummy));
216 assert (test_collection.remove ("one"));
217 assert (ro_collection.size == 1);
218 assert (! ro_collection.is_empty);
219 assert (! ro_collection.contains ("one"));
220 assert (ro_collection.contains ("two"));
221 assert (! ro_collection.contains_all (dummy));
223 test_collection.clear ();
224 assert (ro_collection.size == 0);
225 assert (ro_collection.is_empty);
226 assert (! ro_collection.contains ("one"));
227 assert (! ro_collection.contains ("two"));