Update Changelog
[profile/ivi/libgee.git] / tests / testqueue.vala
1 /* testqueue.vala
2  *
3  * Copyright (C) 2009  Didier Villevalois
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
21  */
22
23 using Gee;
24
25 public abstract class QueueTests : CollectionTests {
26
27         public QueueTests (string name) {
28                 base (name);
29                 add_test ("[Queue] capacity bound", test_capacity_bound);
30                 add_test ("[Queue] one element operation", test_one_element_operation);
31                 add_test ("[Queue] GObject properties", test_gobject_properties);
32         }
33
34         public void test_capacity_bound () {
35                 var test_queue = test_collection as Gee.Queue<string>;
36
37                 // Check the test queue is not null
38                 assert (test_queue != null);
39
40                 if (test_queue.capacity == Gee.Queue.UNBOUNDED_CAPACITY) {
41                         // Unbounded capacity
42                         assert (test_queue.remaining_capacity == Gee.Queue.UNBOUNDED_CAPACITY);
43                         assert (! test_queue.is_full);
44                 } else {
45                         // Bounded capacity
46                         assert (test_queue.is_empty);
47
48                         // Check that we can fill it completely
49                         int capacity = test_queue.capacity;
50                         for (int i = 1; i <= capacity; i++) {
51                                 assert (! test_queue.is_full);
52                                 assert (test_queue.offer ("one"));
53                                 assert (test_queue.remaining_capacity == capacity - i);
54                         }
55                         assert (test_queue.is_full);
56
57                         // Check that we can empty it completely
58                         for (int i = 1; i <= capacity; i++) {
59                                 assert (test_queue.poll () == "one");
60                                 assert (! test_queue.is_full);
61                                 assert (test_queue.remaining_capacity == i);
62                         }
63                         assert (test_queue.is_empty);
64                 }
65         }
66
67         public void test_one_element_operation () {
68                 var test_queue = test_collection as Gee.Queue<string>;
69                 ArrayList<string> recipient = new ArrayList<string> ();
70
71                 // Check the test queue is not null
72                 assert (test_queue != null);
73
74                 // Check offer one element when there is none yet
75                 assert (test_queue.offer ("one"));
76                 assert (test_queue.peek () == "one");
77                 assert (test_queue.size == 1);
78                 assert (! test_queue.is_empty);
79
80                 // Check poll when there is one element
81                 assert (test_queue.poll () == "one");
82                 assert (test_queue.size == 0);
83                 assert (test_queue.is_empty);
84
85                 // Check poll when there is no element
86                 assert (test_queue.peek () == null);
87                 assert (test_queue.poll () == null);
88
89                 // Check drain one element when there is one
90                 recipient.clear ();
91                 assert (test_queue.offer ("one"));
92                 assert (test_queue.drain (recipient, 1) == 1);
93                 assert (test_queue.size == 0);
94                 assert (test_queue.is_empty);
95                 assert (recipient.size == 1);
96                 assert (recipient.get (0) == "one");
97
98                 // Check drain one element when there is none
99                 recipient.clear ();
100                 assert (test_queue.drain (recipient, 1) == 0);
101                 assert (test_queue.size == 0);
102                 assert (test_queue.is_empty);
103                 assert (recipient.size == 0);
104
105                 // Check drain all elements when there is one
106                 recipient.clear ();
107                 assert (test_queue.offer ("one"));
108                 assert (test_queue.drain (recipient) == 1);
109                 assert (test_queue.size == 0);
110                 assert (test_queue.is_empty);
111                 assert (recipient.size == 1);
112                 assert (recipient.get (0) == "one");
113         }
114
115         public new void test_gobject_properties () {
116                 var test_queue = test_collection as Gee.Queue<string>;
117
118                 // Check the list exists
119                 assert (test_queue != null);
120                 Value value;
121
122                 value = Value (typeof (int));
123                 test_queue.get_property ("capacity", ref value);
124                 assert (value.get_int () == test_queue.capacity);
125                 value.unset ();
126
127                 value = Value (typeof (int));
128                 test_queue.get_property ("remaining-capacity", ref value);
129                 assert (value.get_int () == test_queue.remaining_capacity);
130                 value.unset ();
131
132                 value = Value (typeof (bool));
133                 test_queue.get_property ("is-full", ref value);
134                 assert (value.get_boolean () == test_queue.is_full);
135                 value.unset ();
136         }
137 }