1/* testdeque.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
23using Gee;
24
25public abstract class DequeTests : QueueTests {
26
27	protected DequeTests (string name) {
28		base (name);
29		add_test ("[Deque] queue use", test_queue_use);
30		add_test ("[Deque] stack use", test_stack_use);
31		add_test ("[Deque] reversed stack use", test_reversed_stack_use);
32	}
33
34	public void test_queue_use () {
35		var test_deque = test_collection as Gee.Deque<string>;
36		ArrayList<string> recipient = new ArrayList<string> ();
37
38		// Check the test deque is not null
39		assert (test_deque != null);
40		string[] to_offer = {
41		        "one", "two", "three", "four", "five", "six", "seven", "
42eight",
43		        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen
44",
45		        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen
46", "twenty",
47		        "twenty one", "twenty two", "twenty three", "twenty four",
48		        "twenty five", "twenty six", "twenty seven", "twenty eight",
49		        "twenty nine", "thirty", "thirty one", "thirty two", "thirty four",
50		        "thirty five", "thirty six", "thirty seven", "thirty eight",
51		        "thirty nine", "fourty"
52		};
53
54		// Check normal FIFO behavior
55		for (int i = 0; i < to_offer.length; i++) {
56			assert (test_deque.offer_tail (to_offer[i]));
57			assert (test_deque.size == i + 1);
58		}
59		for (int i = 0; i < to_offer.length; i++) {
60			assert (test_deque.peek_head () == to_offer[i]);
61			assert (test_deque.poll_head () == to_offer[i]);
62			assert (test_deque.size == to_offer.length - i - 1);
63		}
64
65		// Check normal behavior when no element
66		assert (test_deque.peek_head () == null);
67		assert (test_deque.poll_head () == null);
68
69		// Check drain with FIFO behavior
70		recipient.clear ();
71		for (int i = 0; i < to_offer.length; i++) {
72			assert (test_deque.offer_tail (to_offer[i]));
73		}
74		assert (test_deque.size == to_offer.length);
75		assert (test_deque.drain_head (recipient, 1) == 1);
76		assert (test_deque.size == to_offer.length - 1);
77		assert (recipient.size == 1);
78		assert (recipient.get (0) == to_offer[0]);
79		assert (test_deque.drain_head (recipient) == to_offer.length - 1);
80		assert (test_deque.size == 0);
81		assert (recipient.size == to_offer.length);
82		for (int i = 1; i < to_offer.length; i++) {
83			assert (recipient.get (i) == to_offer[i]);
84		}
85
86		// Check drain one when no element
87		recipient.clear ();
88		assert (test_deque.drain_head (recipient, 1) == 0);
89		assert (test_deque.size == 0);
90		assert (recipient.size == 0);
91
92		// Check drain all when no element
93		recipient.clear ();
94		assert (test_deque.drain_head (recipient) == 0);
95		assert (test_deque.size == 0);
96		assert (recipient.size == 0);
97	}
98
99	public void test_stack_use () {
100		var test_deque = test_collection as Gee.Deque<string>;
101		ArrayList<string> recipient = new ArrayList<string> ();
102		string[] to_offer = {
103		        "one", "two", "three", "four", "five", "six", "seven", "
104eight",
105		        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen
106",
107		        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen
108", "twenty",
109		        "twenty one", "twenty two", "twenty three", "twenty four",
110		        "twenty five", "twenty six", "twenty seven", "twenty eight",
111		        "twenty nine", "thirty", "thirty one", "thirty two", "thirty four",
112		        "thirty five", "thirty six", "thirty seven", "thirty eight",
113		        "thirty nine", "fourty"
114		};
115
116		// Check the test deque is not null
117		assert (test_deque != null);
118
119		// Check normal LIFO behavior
120		for (int i = 0; i < to_offer.length; i++) {
121			assert (test_deque.offer_head (to_offer[i]));
122			assert (test_deque.size == i + 1);
123		}
124		for (int i = to_offer.length - 1; i >= 0; i--) {
125			assert (test_deque.peek_head () == to_offer[i]);
126			assert (test_deque.poll_head () == to_offer[i]);
127			assert (test_deque.size == i);
128		}
129
130		// Check normal behavior when no element
131		assert (test_deque.peek_head () == null);
132		assert (test_deque.poll_head () == null);
133
134		// Check drain with LIFO behavior
135		recipient.clear ();
136		for (int i = 0; i < to_offer.length; i++) {
137			assert (test_deque.offer_head (to_offer[i]));
138		}
139		assert (test_deque.size == to_offer.length);
140		assert (test_deque.drain_head (recipient, 1) == 1);
141		assert (test_deque.size == to_offer.length - 1);
142		assert (recipient.size == 1);
143		assert (recipient.get (0) == to_offer[to_offer.length - 1]);
144		assert (test_deque.drain_head (recipient) == to_offer.length - 1);
145		assert (test_deque.size == 0);
146		assert (recipient.size == to_offer.length);
147		for (int i = 1; i < to_offer.length; i++) {
148			assert (recipient.get (i) == to_offer[to_offer.length - i - 1]);
149		}
150
151		// Check drain one when no element
152		recipient.clear ();
153		assert (test_deque.drain_head (recipient, 1) == 0);
154		assert (test_deque.size == 0);
155		assert (recipient.size == 0);
156
157		// Check drain all when no element
158		recipient.clear ();
159		assert (test_deque.drain_head (recipient) == 0);
160		assert (test_deque.size == 0);
161		assert (recipient.size == 0);
162	}
163
164	public void test_reversed_stack_use () {
165		var test_deque = test_collection as Gee.Deque<string>;
166		ArrayList<string> recipient = new ArrayList<string> ();
167		string[] to_offer = {
168		        "one", "two", "three", "four", "five", "six", "seven", "
169eight",
170		        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen
171",
172		        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen
173", "twenty",
174		        "twenty one", "twenty two", "twenty three", "twenty four",
175		        "twenty five", "twenty six", "twenty seven", "twenty eight",
176		        "twenty nine", "thirty", "thirty one", "thirty two", "thirty four",
177		        "thirty five", "thirty six", "thirty seven", "thirty eight",
178		        "thirty nine", "fourty"
179		};
180
181		// Check the test deque is not null
182		assert (test_deque != null);
183
184		// Check normal LIFO behavior
185		for (int i = 0; i < to_offer.length; i++) {
186			assert (test_deque.offer_tail (to_offer[i]));
187			assert (test_deque.size == i + 1);
188		}
189		for (int i = 0; i < to_offer.length; i++) {
190			assert (test_deque.peek_tail () == to_offer[to_offer.length - i - 1]);
191			assert (test_deque.poll_tail () == to_offer[to_offer.length - i - 1]);
192			assert (test_deque.size == to_offer.length - i - 1);
193		}
194
195		// Check normal behavior when no element
196		assert (test_deque.peek_tail () == null);
197		assert (test_deque.poll_tail () == null);
198
199		// Check drain with LIFO behavior
200		recipient.clear ();
201		for (int i = 0; i < to_offer.length; i++) {
202			assert (test_deque.offer_tail (to_offer[i]));
203		}
204		assert (test_deque.size == to_offer.length);
205		assert (test_deque.drain_tail (recipient, 1) == 1);
206		assert (test_deque.size == to_offer.length - 1);
207		assert (recipient.size == 1);
208		assert (recipient.get (0) == to_offer[to_offer.length - 1]);
209		assert (test_deque.drain_tail (recipient) == to_offer.length - 1);
210		assert (test_deque.size == 0);
211		assert (recipient.size == to_offer.length);
212		for (int i = 1; i < to_offer.length; i++) {
213			assert (recipient.get (i) == to_offer[to_offer.length - i - 1]);
214		}
215
216		// Check drain one when no element
217		recipient.clear ();
218		assert (test_deque.drain_tail (recipient, 1) == 0);
219		assert (test_deque.size == 0);
220		assert (recipient.size == 0);
221
222		// Check drain all when no element
223		recipient.clear ();
224		assert (test_deque.drain_tail (recipient) == 0);
225		assert (test_deque.size == 0);
226		assert (recipient.size == 0);
227	}
228}
229
230