1 /* testreadonlymap.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 * Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
26 public class ReadOnlyMapTests : Gee.TestCase {
28 public ReadOnlyMapTests () {
30 add_test ("[ReadOnlyMap] unique read-only view instance",
31 test_unique_read_only_view_instance);
32 add_test ("[ReadOnlyMap] immutable iterator", test_immutable_iterator);
33 add_test ("[ReadOnlyMap] immutable", test_immutable);
34 add_test ("[ReadOnlyMap] accurate view", test_accurate_view);
37 protected Map<string,string> test_map;
38 protected Map<string,string> ro_map;
40 public override void set_up () {
41 test_map = new TreeMap<string,string> ();
42 ro_map = test_map.read_only_view;
45 public override void tear_down () {
50 public void test_unique_read_only_view_instance () {
51 var another_ro_map = test_map.read_only_view;
52 assert (ro_map == another_ro_map);
54 ro_map.set_data ("marker", new Object ());
55 assert (another_ro_map.get_data<Object> ("marker") != null);
57 another_ro_map = null;
60 another_ro_map = test_map.read_only_view;
61 assert (another_ro_map.get_data<Object> ("marker") == null);
63 // Check that the read-only view of the view is itself
64 assert (another_ro_map == another_ro_map.read_only_view);
67 public void test_immutable_iterator () {
68 test_map.set ("one", "one");
69 test_map.set ("two", "two");
71 assert (ro_map.size == 2);
72 assert (ro_map.has_key ("one"));
73 assert (ro_map.has ("one", "one"));
74 assert (ro_map.has_key ("two"));
75 assert (ro_map.has ("two", "two"));
77 Iterator<string> iterator = ro_map.keys.iterator ();
79 assert (iterator.has_next ());
80 assert (iterator.next ());
81 assert (iterator.get () == "one");
83 assert (iterator.has_next ());
84 assert (iterator.next ());
85 assert (iterator.get () == "two");
87 assert (! iterator.has_next ());
88 assert (! iterator.next ());
90 iterator = ro_map.keys.iterator ();
91 assert (iterator.has_next ());
92 assert (iterator.next ());
93 assert (iterator.get () == "one");
95 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
96 TestTrapFlags.SILENCE_STDERR)) {
100 Test.trap_assert_failed ();
102 assert (ro_map.size == 2);
103 assert (ro_map.has_key ("one"));
104 assert (ro_map.has ("one", "one"));
105 assert (ro_map.has_key ("two"));
106 assert (ro_map.has ("two", "two"));
109 public void test_immutable () {
110 test_map.set ("one", "one");
111 assert (ro_map.size == 1);
112 assert (ro_map.has_key ("one"));
113 assert (ro_map.has ("one", "one"));
115 Map<string,string> dummy = new HashMap<string,string> ();
116 dummy.set ("one", "one");
117 dummy.set ("two", "two");
119 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
120 TestTrapFlags.SILENCE_STDERR)) {
121 ro_map.set ("two", "two");
124 Test.trap_assert_failed ();
125 assert (ro_map.size == 1);
126 assert (ro_map.has_key ("one"));
127 assert (ro_map.has ("one", "one"));
129 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
130 TestTrapFlags.SILENCE_STDERR)) {
134 Test.trap_assert_failed ();
135 assert (ro_map.size == 1);
136 assert (ro_map.has_key ("one"));
137 assert (ro_map.has ("one", "one"));
139 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
140 TestTrapFlags.SILENCE_STDERR)) {
141 assert (ro_map.unset ("one"));
144 Test.trap_assert_failed ();
145 assert (ro_map.size == 1);
146 assert (ro_map.has_key ("one"));
147 assert (ro_map.has ("one", "one"));
149 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
150 TestTrapFlags.SILENCE_STDERR)) {
151 ro_map.set_all (dummy);
154 Test.trap_assert_failed ();
155 assert (ro_map.size == 1);
156 assert (ro_map.has_key ("one"));
157 assert (ro_map.has ("one", "one"));
159 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
160 TestTrapFlags.SILENCE_STDERR)) {
161 assert (ro_map.unset_all (dummy));
164 Test.trap_assert_failed ();
165 assert (ro_map.size == 1);
166 assert (ro_map.has_key ("one"));
167 assert (ro_map.has ("one", "one"));
170 public void test_accurate_view () {
171 Map<string,string> dummy = new HashMap<string,string> ();
172 dummy.set ("one", "one");
173 dummy.set ("two", "two");
175 assert (ro_map.size == 0);
176 assert (ro_map.is_empty);
177 assert (! ro_map.has_key ("one"));
178 assert (! ro_map.has ("one", "one"));
179 assert (ro_map.get ("one") == null);
181 test_map.set ("one", "one");
182 assert (ro_map.size == 1);
183 assert (! ro_map.is_empty);
184 assert (ro_map.has_key ("one"));
185 assert (ro_map.has ("one", "one"));
186 assert (ro_map.get ("one") == "one");
188 test_map.set ("two", "two");
189 assert (ro_map.size == 2);
190 assert (! ro_map.is_empty);
191 assert (ro_map.has_key ("one"));
192 assert (ro_map.has ("one", "one"));
193 assert (ro_map.get ("one") == "one");
194 assert (ro_map.has_key ("two"));
195 assert (ro_map.has ("two", "two"));
196 assert (ro_map.get ("two") == "two");
197 assert (ro_map.has_all (dummy));
199 assert (test_map.unset ("one"));
200 assert (ro_map.size == 1);
201 assert (! ro_map.is_empty);
202 assert (! ro_map.has_key ("one"));
203 assert (! ro_map.has ("one", "one"));
204 assert (ro_map.get ("one") == null);
205 assert (ro_map.has_key ("two"));
206 assert (ro_map.has ("two", "two"));
207 assert (ro_map.get ("two") == "two");
208 assert (! ro_map.has_all (dummy));
211 assert (ro_map.size == 0);
212 assert (ro_map.is_empty);
213 assert (! ro_map.has ("one", "one"));
214 assert (! ro_map.has ("two", "two"));
215 assert (ro_map.get ("one") == null);
216 assert (ro_map.get ("two") == null);