Update Changelog
[profile/ivi/libgee.git] / tests / testreadonlycollection.vala
1 /* testreadonlycollection.vala
2  *
3  * Copyright (C) 2008  Jürg Billeter
4  * Copyright (C) 2009  Didier Villevalois
5  *
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.
10
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.
15
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
19  *
20  * Author:
21  *      Tomaž Vajngerl <quikee@gmail.com>
22  *      Julien Peeters <contact@julienpeeters.fr>
23  */
24
25 using Gee;
26
27 public class ReadOnlyCollectionTests : Gee.TestCase {
28
29         public ReadOnlyCollectionTests () {
30                 this.with_name ("ReadOnlyCollection");
31         }
32
33         public ReadOnlyCollectionTests.with_name (string name) {
34                 base (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);
40         }
41
42         protected Collection<string> test_collection;
43         protected Collection<string> ro_collection;
44
45         public override void set_up () {
46                 test_collection = new HashMultiSet<string> ();
47                 ro_collection = get_ro_view (test_collection);
48         }
49
50         public override void tear_down () {
51                 test_collection = null;
52                 ro_collection = null;
53         }
54
55         protected virtual Collection<string> get_ro_view (Collection<string> collection) {
56                 return collection.read_only_view;
57         }
58
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);
62
63                 ro_collection.set_data ("marker", new Object ());
64                 assert (another_ro_collection.get_data<Object> ("marker") != null);
65
66                 another_ro_collection = null;
67                 ro_collection = null;
68
69                 another_ro_collection = get_ro_view (test_collection);
70                 assert (another_ro_collection.get_data<Object> ("marker") == null);
71
72                 // Check that the read-only view of the view is itself
73                 assert (another_ro_collection == get_ro_view (another_ro_collection));
74         }
75
76         public void test_immutable_iterator () {
77                 assert (test_collection.add ("one"));
78                 assert (test_collection.add ("two"));
79
80                 assert (ro_collection.size == 2);
81                 assert (ro_collection.contains ("one"));
82                 assert (ro_collection.contains ("two"));
83
84                 Iterator<string> iterator = ro_collection.iterator ();
85
86                 bool one_found = false;
87                 bool two_found = false;
88
89                 while (iterator.next ()) {
90                         assert (iterator.valid);
91                         switch(iterator.get ()) {
92                         case "one":
93                                 assert (! one_found);
94                                 one_found = true;
95                                 break;
96                         case "two":
97                                 assert (! two_found);
98                                 two_found = true;
99                                 break;
100                         default:
101                                 assert_not_reached ();
102                         }
103                 }
104
105                 assert (one_found);
106                 assert (two_found);
107
108                 assert (! iterator.has_next ());
109                 assert (! iterator.next ());
110
111                 iterator = ro_collection.iterator ();
112                 assert (iterator.has_next ());
113                 assert (iterator.next ());
114
115                 assert (iterator.read_only);
116                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
117                                        TestTrapFlags.SILENCE_STDERR)) {
118                         iterator.remove ();
119                         Posix.exit (0);
120                 }
121                 Test.trap_assert_failed ();
122
123                 assert (ro_collection.size == 2);
124                 assert (ro_collection.contains ("one"));
125                 assert (ro_collection.contains ("two"));
126         }
127
128         public void test_immutable () {
129                 assert (test_collection.add ("one"));
130                 assert (ro_collection.size == 1);
131                 assert (ro_collection.contains ("one"));
132
133                 Collection<string> dummy = new ArrayList<string> ();
134                 assert (dummy.add ("one"));
135                 assert (dummy.add ("two"));
136
137                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
138                                        TestTrapFlags.SILENCE_STDERR)) {
139                         assert (ro_collection.add ("two"));
140                         Posix.exit (0);
141                 }
142                 Test.trap_assert_failed ();
143                 assert (ro_collection.size == 1);
144                 assert (ro_collection.contains ("one"));
145
146                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
147                                        TestTrapFlags.SILENCE_STDERR)) {
148                         ro_collection.clear ();
149                         Posix.exit (0);
150                 }
151                 Test.trap_assert_failed ();
152                 assert (ro_collection.size == 1);
153                 assert (ro_collection.contains ("one"));
154
155                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
156                                        TestTrapFlags.SILENCE_STDERR)) {
157                         assert (ro_collection.remove ("one"));
158                         Posix.exit (0);
159                 }
160                 Test.trap_assert_failed ();
161                 assert (ro_collection.size == 1);
162                 assert (ro_collection.contains ("one"));
163
164                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
165                                        TestTrapFlags.SILENCE_STDERR)) {
166                         assert (ro_collection.add_all (dummy));
167                         Posix.exit (0);
168                 }
169                 Test.trap_assert_failed ();
170                 assert (ro_collection.size == 1);
171                 assert (ro_collection.contains ("one"));
172
173                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
174                                        TestTrapFlags.SILENCE_STDERR)) {
175                         assert (ro_collection.remove_all (dummy));
176                         Posix.exit (0);
177                 }
178                 Test.trap_assert_failed ();
179                 assert (ro_collection.size == 1);
180                 assert (ro_collection.contains ("one"));
181
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));
186                         Posix.exit (0);
187                 }
188                 Test.trap_assert_failed ();
189                 assert (ro_collection.size == 1);
190                 assert (ro_collection.contains ("one"));
191         }
192
193         public void test_accurate_view () {
194                 Collection<string> dummy = new ArrayList<string> ();
195                 assert (dummy.add ("one"));
196                 assert (dummy.add ("two"));
197
198                 assert (ro_collection.element_type == typeof (string));
199
200                 assert (ro_collection.size == 0);
201                 assert (ro_collection.is_empty);
202                 assert (! ro_collection.contains ("one"));
203
204                 assert (test_collection.add ("one"));
205                 assert (ro_collection.size == 1);
206                 assert (! ro_collection.is_empty);
207                 assert (ro_collection.contains ("one"));
208
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));
215
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));
222
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"));
228         }
229 }