• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.badlogic.gdx.utils;
3 
4 import static org.junit.Assert.*;
5 
6 import java.util.Iterator;
7 
8 import org.junit.Test;
9 
10 public class QueueTest {
11 
12 	@Test
resizableQueueTest()13 	public void resizableQueueTest () {
14 		final Queue<Integer> q = new Queue<Integer>(8);
15 
16 		assertTrue("New queue is not empty!", q.size == 0);
17 
18 		for (int i = 0; i < 100; i++) {
19 
20 			for (int j = 0; j < i; j++) {
21 				try {
22 					q.addLast(j);
23 				} catch (IllegalStateException e) {
24 					fail("Failed to add element " + j + " (" + i + ")");
25 				}
26 				final Integer peeked = q.last();
27 				assertTrue("peekLast shows " + peeked + ", should be " + j + " (" + i + ")", peeked.equals(j));
28 				final int size = q.size;
29 				assertTrue("Size should be " + (j + 1) + " but is " + size + " (" + i + ")", size == j + 1);
30 			}
31 
32 			if (i != 0) {
33 				final Integer peek = q.first();
34 				assertTrue("First thing is not zero but " + peek + " (" + i + ")", peek == 0);
35 			}
36 
37 			for (int j = 0; j < i; j++) {
38 				final Integer pop = q.removeFirst();
39 				assertTrue("Popped should be " + j + " but is " + pop + " (" + i + ")", pop == j);
40 
41 				final int size = q.size;
42 				assertTrue("Size should be " + (i - 1 - j) + " but is " + size + " (" + i + ")", size == i - 1 - j);
43 			}
44 
45 			assertTrue("Not empty after cycle " + i, q.size == 0);
46 		}
47 
48 		for (int i = 0; i < 56; i++) {
49 			q.addLast(42);
50 		}
51 		q.clear();
52 		assertTrue("Clear did not clear properly", q.size == 0);
53 	}
54 
55 	/** Same as resizableQueueTest, but in reverse */
56 	@Test
resizableDequeTest()57 	public void resizableDequeTest () {
58 		final Queue<Integer> q = new Queue<Integer>(8);
59 
60 		assertTrue("New deque is not empty!", q.size == 0);
61 
62 		for (int i = 0; i < 100; i++) {
63 
64 			for (int j = 0; j < i; j++) {
65 				try {
66 					q.addFirst(j);
67 				} catch (IllegalStateException e) {
68 					fail("Failed to add element " + j + " (" + i + ")");
69 				}
70 				final Integer peeked = q.first();
71 				assertTrue("peek shows " + peeked + ", should be " + j + " (" + i + ")", peeked.equals(j));
72 				final int size = q.size;
73 				assertTrue("Size should be " + (j + 1) + " but is " + size + " (" + i + ")", size == j + 1);
74 			}
75 
76 			if (i != 0) {
77 				final Integer peek = q.last();
78 				assertTrue("Last thing is not zero but " + peek + " (" + i + ")", peek == 0);
79 			}
80 
81 			for (int j = 0; j < i; j++) {
82 				final Integer pop = q.removeLast();
83 				assertTrue("Popped should be " + j + " but is " + pop + " (" + i + ")", pop == j);
84 
85 				final int size = q.size;
86 				assertTrue("Size should be " + (i - 1 - j) + " but is " + size + " (" + i + ")", size == i - 1 - j);
87 			}
88 
89 			assertTrue("Not empty after cycle " + i, q.size == 0);
90 		}
91 
92 		for (int i = 0; i < 56; i++) {
93 			q.addFirst(42);
94 		}
95 		q.clear();
96 		assertTrue("Clear did not clear properly", q.size == 0);
97 	}
98 
99 	@Test
getTest()100 	public void getTest () {
101 		final Queue<Integer> q = new Queue<Integer>(7);
102 		for (int i = 0; i < 5; i++) {
103 			for (int j = 0; j < 4; j++) {
104 				q.addLast(j);
105 			}
106 			assertEquals("get(0) is not equal to peek (" + i + ")", q.get(0), q.first());
107 			assertEquals("get(size-1) is not equal to peekLast (" + i + ")", q.get(q.size - 1), q.last());
108 			for (int j = 0; j < 4; j++) {
109 				assertTrue(q.get(j) == j);
110 			}
111 			for (int j = 0; j < 4 - 1; j++) {
112 				q.removeFirst();
113 				assertEquals("get(0) is not equal to peek (" + i + ")", q.get(0), q.first());
114 			}
115 			q.removeFirst();
116 			assert q.size == 0; // Failing this means broken test
117 			try {
118 				q.get(0);
119 				fail("get() on empty queue did not throw");
120 			} catch (IndexOutOfBoundsException ignore) {
121 				// Expected
122 			}
123 		}
124 	}
125 
126 	@Test
removeTest()127 	public void removeTest () {
128 		final Queue<Integer> q = new Queue<Integer>();
129 
130 		// Test head < tail.
131 		for (int j = 0; j <= 6; j++)
132 			q.addLast(j);
133 		assertValues(q, 0, 1, 2, 3, 4, 5, 6);
134 		q.removeIndex(0);
135 		assertValues(q, 1, 2, 3, 4, 5, 6);
136 		q.removeIndex(1);
137 		assertValues(q, 1, 3, 4, 5, 6);
138 		q.removeIndex(4);
139 		assertValues(q, 1, 3, 4, 5);
140 		q.removeIndex(2);
141 		assertValues(q, 1, 3, 5);
142 
143 		// Test head >= tail and index >= head.
144 		q.clear();
145 		for (int j = 2; j >= 0; j--)
146 			q.addFirst(j);
147 		for (int j = 3; j <= 6; j++)
148 			q.addLast(j);
149 		assertValues(q, 0, 1, 2, 3, 4, 5, 6);
150 		q.removeIndex(1);
151 		assertValues(q, 0, 2, 3, 4, 5, 6);
152 		q.removeIndex(0);
153 		assertValues(q, 2, 3, 4, 5, 6);
154 
155 		// Test head >= tail and index < tail.
156 		q.clear();
157 		for (int j = 2; j >= 0; j--)
158 			q.addFirst(j);
159 		for (int j = 3; j <= 6; j++)
160 			q.addLast(j);
161 		assertValues(q, 0, 1, 2, 3, 4, 5, 6);
162 		q.removeIndex(5);
163 		assertValues(q, 0, 1, 2, 3, 4, 6);
164 		q.removeIndex(5);
165 		assertValues(q, 0, 1, 2, 3, 4);
166 	}
167 
168 	@Test
indexOfTest()169 	public void indexOfTest () {
170 		final Queue<Integer> q = new Queue<Integer>();
171 
172 		// Test head < tail.
173 		for (int j = 0; j <= 6; j++)
174 			q.addLast(j);
175 		for (int j = 0; j <= 6; j++)
176 			assertEquals(q.indexOf(j, false), j);
177 
178 		// Test head >= tail.
179 		q.clear();
180 		for (int j = 2; j >= 0; j--)
181 			q.addFirst(j);
182 		for (int j = 3; j <= 6; j++)
183 			q.addLast(j);
184 		for (int j = 0; j <= 6; j++)
185 			assertEquals(q.indexOf(j, false), j);
186 	}
187 
188 	@Test
iteratorTest()189 	public void iteratorTest () {
190 		final Queue<Integer> q = new Queue<Integer>();
191 
192 		// Test head < tail.
193 		for (int j = 0; j <= 6; j++)
194 			q.addLast(j);
195 		Iterator<Integer> iter = q.iterator();
196 		for (int j = 0; j <= 6; j++)
197 			assertEquals(iter.next().intValue(), j);
198 		iter = q.iterator();
199 		iter.next();
200 		iter.remove();
201 		assertValues(q, 1, 2, 3, 4, 5, 6);
202 		iter.next();
203 		iter.remove();
204 		assertValues(q, 2, 3, 4, 5, 6);
205 		iter.next();
206 		iter.next();
207 		iter.remove();
208 		assertValues(q, 2, 4, 5, 6);
209 		iter.next();
210 		iter.next();
211 		iter.next();
212 		iter.remove();
213 		assertValues(q, 2, 4, 5);
214 
215 		// Test head >= tail.
216 		q.clear();
217 		for (int j = 2; j >= 0; j--)
218 			q.addFirst(j);
219 		for (int j = 3; j <= 6; j++)
220 			q.addLast(j);
221 		iter = q.iterator();
222 		for (int j = 0; j <= 6; j++)
223 			assertEquals(iter.next().intValue(), j);
224 		iter = q.iterator();
225 		iter.next();
226 		iter.remove();
227 		assertValues(q, 1, 2, 3, 4, 5, 6);
228 		iter.next();
229 		iter.remove();
230 		assertValues(q, 2, 3, 4, 5, 6);
231 		iter.next();
232 		iter.next();
233 		iter.remove();
234 		assertValues(q, 2, 4, 5, 6);
235 		iter.next();
236 		iter.next();
237 		iter.next();
238 		iter.remove();
239 		assertValues(q, 2, 4, 5);
240 	}
241 
242 	@Test
toStringTest()243 	public void toStringTest () {
244 		Queue<Integer> q = new Queue<Integer>(1);
245 		assertTrue(q.toString().equals("[]"));
246 		q.addLast(4);
247 		assertTrue(q.toString().equals("[4]"));
248 		q.addLast(5);
249 		q.addLast(6);
250 		q.addLast(7);
251 		assertTrue(q.toString().equals("[4, 5, 6, 7]"));
252 	}
253 
254 	@Test
hashEqualsText()255 	public void hashEqualsText () {
256 		Queue<Integer> q1 = new Queue<Integer>();
257 		Queue<Integer> q2 = new Queue<Integer>();
258 
259 		assertEqualsAndHash(q1, q2);
260 		q1.addFirst(1);
261 		assertNotEquals(q1, q2);
262 		q2.addFirst(1);
263 		assertEqualsAndHash(q1, q2);
264 
265 		q1.clear();
266 		q1.addLast(1);
267 		q1.addLast(2);
268 		q2.addLast(2);
269 		assertEqualsAndHash(q1, q2);
270 
271 		for (int i = 0; i < 100; i++) {
272 			q1.addLast(i);
273 			q1.addLast(i);
274 			q1.removeFirst();
275 
276 			assertNotEquals(q1, q2);
277 
278 			q2.addLast(i);
279 			q2.addLast(i);
280 			q2.removeFirst();
281 
282 			assertEqualsAndHash(q1, q2);
283 		}
284 	}
285 
assertEqualsAndHash(Queue<?> q1, Queue<?> q2)286 	private void assertEqualsAndHash (Queue<?> q1, Queue<?> q2) {
287 		assertEquals(q1, q2);
288 		assertEquals("Hash codes are not equal", q1.hashCode(), q2.hashCode());
289 	}
290 
assertValues(Queue<Integer> q, Integer... values)291 	private void assertValues (Queue<Integer> q, Integer... values) {
292 		for (int i = 0, n = values.length; i < n; i++)
293 			if (values[i] != q.get(i)) fail(q + " != " + new Array(values));
294 	}
295 }
296