Update libgee to 0.9.92 (3462b25)
[profile/ivi/libgee.git] / tests / testreadonlylist.vala
1 /* testreadonlylist.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  *      Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
24  */
25
26 using Gee;
27
28 public class ReadOnlyListTests : ReadOnlyCollectionTests {
29
30         public ReadOnlyListTests () {
31                 this.with_name ("ReadOnlyList");
32         }
33
34         public ReadOnlyListTests.with_name (string name) {
35                 base.with_name (name);
36                 add_test ("[ReadOnlyList] immutable iterator", test_immutable_iterator);
37                 add_test ("[ReadOnlyList] immutable", test_immutable);
38                 add_test ("[ReadOnlyList] accurate view", test_accurate_view);
39         }
40
41         public override void set_up () {
42                 test_collection = new ArrayList<string> ();
43                 ro_collection = get_ro_view (test_collection);
44         }
45
46         public override void tear_down () {
47                 test_collection = null;
48                 ro_collection = null;
49         }
50
51         protected override Collection<string> get_ro_view (Collection<string> collection) {
52                 return ((Gee.List) collection).read_only_view;
53         }
54
55         public new void test_immutable_iterator () {
56                 var test_list = test_collection as Gee.List<string>;
57                 var ro_list = ro_collection as Gee.List<string>;
58
59                 assert (test_list.add ("one"));
60                 assert (test_list.add ("two"));
61
62                 assert (ro_list.size == 2);
63                 assert (ro_list.get (0) == "one");
64                 assert (ro_list.get (1) == "two");
65
66                 ListIterator<string> iterator = ro_list.list_iterator ();
67
68                 assert (iterator.has_next ());
69                 assert (iterator.next ());
70                 assert (iterator.get () == "one");
71                 assert (iterator.index () == 0);
72
73                 assert (iterator.has_next ());
74                 assert (iterator.next ());
75                 assert (iterator.get () == "two");
76                 assert (iterator.index () == 1);
77
78                 assert (! iterator.has_next ());
79                 assert (! iterator.next ());
80
81                 iterator = ro_list.list_iterator ();
82                 assert (iterator.next ());
83
84                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
85                                        TestTrapFlags.SILENCE_STDERR)) {
86                         iterator.remove ();
87                         Posix.exit (0);
88                 }
89                 Test.trap_assert_failed ();
90                 assert (ro_list.size == 2);
91                 assert (ro_list.get (0) == "one");
92                 assert (ro_list.get (1) == "two");
93                 assert (iterator.index () == 0);
94
95                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
96                                        TestTrapFlags.SILENCE_STDERR)) {
97                         iterator.set ("three");
98                         Posix.exit (0);
99                 }
100                 Test.trap_assert_failed ();
101                 assert (ro_list.size == 2);
102                 assert (ro_list.get (0) == "one");
103                 assert (ro_list.get (1) == "two");
104                 assert (iterator.index () == 0);
105
106                 assert (ro_list.size == 2);
107                 assert (ro_list.get (0) == "one");
108                 assert (ro_list.get (1) == "two");
109                 assert (iterator.index () == 0);
110
111                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
112                                        TestTrapFlags.SILENCE_STDERR)) {
113                         iterator.add ("three");
114                         Posix.exit (0);
115                 }
116                 Test.trap_assert_failed ();
117                 assert (ro_list.size == 2);
118                 assert (ro_list.get (0) == "one");
119                 assert (ro_list.get (1) == "two");
120                 assert (iterator.index () == 0);
121         }
122
123         public new void test_immutable () {
124                 var test_list = test_collection as Gee.List<string>;
125                 var ro_list = ro_collection as Gee.List<string>;
126
127                 assert (test_list.add ("one"));
128                 assert (ro_list.size == 1);
129                 assert (ro_list.contains ("one"));
130
131                 Collection<string> dummy = new ArrayList<string> ();
132                 assert (dummy.add ("one"));
133                 assert (dummy.add ("two"));
134
135                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
136                                        TestTrapFlags.SILENCE_STDERR)) {
137                         ro_list.set (0, "two");
138                         Posix.exit (0);
139                 }
140                 Test.trap_assert_failed ();
141                 assert (ro_list.size == 1);
142                 assert (ro_list.contains ("one"));
143
144                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
145                                        TestTrapFlags.SILENCE_STDERR)) {
146                         ro_list.insert (1, "two");
147                         Posix.exit (0);
148                 }
149                 Test.trap_assert_failed ();
150                 assert (ro_list.size == 1);
151                 assert (ro_list.contains ("one"));
152
153                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
154                                        TestTrapFlags.SILENCE_STDERR)) {
155                         ro_list.remove_at (1);
156                         Posix.exit (0);
157                 }
158                 Test.trap_assert_failed ();
159                 assert (ro_list.size == 1);
160                 assert (ro_list.contains ("one"));
161
162                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
163                                        TestTrapFlags.SILENCE_STDERR)) {
164                         ro_list.insert_all (1, dummy);
165                         Posix.exit (0);
166                 }
167                 Test.trap_assert_failed ();
168                 assert (ro_list.size == 1);
169                 assert (ro_list.contains ("one"));
170
171                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
172                                        TestTrapFlags.SILENCE_STDERR)) {
173                         ro_list.sort ();
174                         Posix.exit (0);
175                 }
176                 Test.trap_assert_failed ();
177                 assert (ro_list.size == 1);
178                 assert (ro_list.contains ("one"));
179         }
180
181         public new void test_accurate_view () {
182                 var test_list = test_collection as Gee.List<string>;
183                 var ro_list = ro_collection as Gee.List<string>;
184
185                 Collection<string> dummy = new ArrayList<string> ();
186                 assert (dummy.add ("one"));
187                 assert (dummy.add ("two"));
188
189                 assert (ro_list.element_type == typeof (string));
190
191                 assert (ro_list.size == 0);
192                 assert (ro_list.is_empty);
193                 assert (! ro_list.contains ("one"));
194                 assert (ro_list.index_of ("one") == -1);
195
196                 assert (test_list.add ("one"));
197                 assert (ro_list.size == 1);
198                 assert (! ro_list.is_empty);
199                 assert (ro_list.get (0) == "one");
200                 assert (ro_list.index_of ("one") == 0);
201                 assert (ro_list.first () == "one");
202                 assert (ro_list.last () == "one");
203
204                 assert (test_list.add ("two"));
205                 assert (ro_list.size == 2);
206                 assert (! ro_list.is_empty);
207                 assert (ro_list.get (0) == "one");
208                 assert (ro_list.index_of ("one") == 0);
209                 assert (ro_list.get (1) == "two");
210                 assert (ro_list.index_of ("two") == 1);
211                 assert (ro_list.first () == "one");
212                 assert (ro_list.last () == "two");
213
214                 assert (test_list.remove ("one"));
215                 assert (ro_list.size == 1);
216                 assert (! ro_list.is_empty);
217                 assert (! ro_list.contains ("one"));
218                 assert (ro_list.index_of ("one") == -1);
219                 assert (ro_list.contains ("two"));
220                 assert (ro_list.get (0) == "two");
221                 assert (ro_list.index_of ("two") == 0);
222                 assert (ro_list.first () == "two");
223                 assert (ro_list.last () == "two");
224
225                 test_list.clear ();
226                 assert (ro_list.size == 0);
227                 assert (ro_list.is_empty);
228                 assert (ro_list.index_of ("one") == -1);
229                 assert (ro_list.index_of ("two") == -1);
230         }
231 }