001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import junit.framework.*;
034: import java.util.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class _DequeJava6TestCase extends TestCase {
040:
041: public void testAddFirst() throws Exception {
042: for (Deque deque : new Deque[] { new ArrayDeque(),
043: new LinkedList() }) {
044: Collections.addAll(deque, "a", "b", "c");
045: deque.addFirst("x");
046: assertEquals("[x, a, b, c]", deque.toString());
047: }
048: }
049:
050: public void testAddLast() throws Exception {
051: for (Deque deque : new Deque[] { new ArrayDeque(),
052: new LinkedList() }) {
053: Collections.addAll(deque, "a", "b", "c");
054: deque.addLast("x");
055: assertEquals("[a, b, c, x]", deque.toString());
056: }
057: }
058:
059: public void testDescendingIterator() throws Exception {
060: for (Deque deque : new Deque[] { new ArrayDeque(),
061: new LinkedList() }) {
062: Collections.addAll(deque, "a", "b", "c");
063: Iterator iterator = deque.descendingIterator();
064: assertTrue(iterator.hasNext());
065: assertEquals("c", iterator.next());
066: assertTrue(iterator.hasNext());
067: assertEquals("b", iterator.next());
068: iterator.remove();
069: assertTrue(iterator.hasNext());
070: assertEquals("a", iterator.next());
071: assertFalse(iterator.hasNext());
072: assertEquals("[a, c]", deque.toString());
073: }
074: }
075:
076: public void testElement() throws Exception {
077: for (Deque deque : new Deque[] { new ArrayDeque(),
078: new LinkedList() }) {
079: Collections.addAll(deque, "a", "b", "c");
080: assertEquals("a", deque.element());
081: assertEquals(3, deque.size());
082: deque.clear();
083: try {
084: deque.element();
085: fail();
086: } catch (NoSuchElementException e) {
087: //ok
088: }
089: }
090: }
091:
092: public void testGetFirst() throws Exception {
093: for (Deque deque : new Deque[] { new ArrayDeque(),
094: new LinkedList() }) {
095: Collections.addAll(deque, "a", "b", "c");
096: assertEquals("a", deque.getFirst());
097: assertEquals("[a, b, c]", deque.toString());
098: deque.clear();
099: try {
100: deque.getFirst();
101: fail();
102: } catch (NoSuchElementException e) {
103: //ok
104: }
105: }
106: }
107:
108: public void testGetLast() throws Exception {
109: for (Deque deque : new Deque[] { new ArrayDeque(),
110: new LinkedList() }) {
111: Collections.addAll(deque, "a", "b", "c");
112: assertEquals("c", deque.getLast());
113: assertEquals("[a, b, c]", deque.toString());
114: deque.clear();
115: try {
116: deque.getLast();
117: fail();
118: } catch (NoSuchElementException e) {
119: //ok
120: }
121: }
122: }
123:
124: public void testOffer() throws Exception {
125: for (Deque deque : new Deque[] { new ArrayDeque(),
126: new LinkedList() }) {
127: Collections.addAll(deque, "a", "b", "c");
128: assertTrue(deque.offer("x"));
129: assertEquals(4, deque.size());
130: assertEquals("x", deque.getLast());
131: }
132: }
133:
134: public void testOfferFirst() throws Exception {
135: for (Deque deque : new Deque[] { new ArrayDeque(),
136: new LinkedList() }) {
137: Collections.addAll(deque, "a", "b", "c");
138: assertTrue(deque.offerFirst("x"));
139: assertEquals(4, deque.size());
140: assertEquals("x", deque.getFirst());
141: }
142: }
143:
144: public void testOfferLast() throws Exception {
145: for (Deque deque : new Deque[] { new ArrayDeque(),
146: new LinkedList() }) {
147: Collections.addAll(deque, "a", "b", "c");
148: assertTrue(deque.offerLast("x"));
149: assertEquals(4, deque.size());
150: assertEquals("x", deque.getLast());
151: }
152: }
153:
154: public void testPeek() throws Exception {
155: for (Deque deque : new Deque[] { new ArrayDeque(),
156: new LinkedList() }) {
157: Collections.addAll(deque, "a", "b", "c");
158: assertEquals("a", deque.peek());
159: assertEquals(3, deque.size());
160: deque.clear();
161: assertNull(deque.peek());
162: }
163: }
164:
165: public void testPeekFirst() throws Exception {
166: for (Deque deque : new Deque[] { new ArrayDeque(),
167: new LinkedList() }) {
168: Collections.addAll(deque, "a", "b", "c");
169: assertEquals("a", deque.peekFirst());
170: assertEquals(3, deque.size());
171: deque.clear();
172: assertNull(deque.peekFirst());
173: }
174: }
175:
176: public void testPeekLast() throws Exception {
177: for (Deque deque : new Deque[] { new ArrayDeque(),
178: new LinkedList() }) {
179: Collections.addAll(deque, "a", "b", "c");
180: assertEquals("c", deque.peekLast());
181: assertEquals(3, deque.size());
182: deque.clear();
183: assertNull(deque.peekLast());
184: }
185: }
186:
187: public void testPoll() throws Exception {
188: for (Deque deque : new Deque[] { new ArrayDeque(),
189: new LinkedList() }) {
190: Collections.addAll(deque, "a", "b", "c");
191: assertEquals("a", deque.poll());
192: assertEquals(2, deque.size());
193: deque.clear();
194: assertNull(deque.poll());
195: }
196: }
197:
198: public void testPollFirst() throws Exception {
199: for (Deque deque : new Deque[] { new ArrayDeque(),
200: new LinkedList() }) {
201: Collections.addAll(deque, "a", "b", "c");
202: assertEquals("a", deque.pollFirst());
203: assertEquals(2, deque.size());
204: deque.clear();
205: assertNull(deque.pollFirst());
206: }
207: }
208:
209: public void testPollLast() throws Exception {
210: for (Deque deque : new Deque[] { new ArrayDeque(),
211: new LinkedList() }) {
212: Collections.addAll(deque, "a", "b", "c");
213: assertEquals("c", deque.pollLast());
214: assertEquals(2, deque.size());
215: deque.clear();
216: assertNull(deque.pollLast());
217: }
218: }
219:
220: public void testPop() throws Exception {
221: for (Deque deque : new Deque[] { new ArrayDeque(),
222: new LinkedList() }) {
223: Collections.addAll(deque, "a", "b", "c");
224: assertEquals("a", deque.pop());
225: assertEquals(2, deque.size());
226: deque.clear();
227: try {
228: deque.pop();
229: fail();
230: } catch (NoSuchElementException e) {
231: //ok
232: }
233: }
234: }
235:
236: public void testPush() throws Exception {
237: for (Deque deque : new Deque[] { new ArrayDeque(),
238: new LinkedList() }) {
239: Collections.addAll(deque, "a", "b", "c");
240: deque.push("x");
241: assertEquals(4, deque.size());
242: assertEquals("x", deque.getFirst());
243: }
244: }
245:
246: public void testRemove() throws Exception {
247: for (Deque deque : new Deque[] { new ArrayDeque(),
248: new LinkedList() }) {
249: Collections.addAll(deque, "a", "b", "c");
250: assertEquals("a", deque.remove());
251: assertEquals(2, deque.size());
252: deque.clear();
253: try {
254: deque.remove();
255: fail();
256: } catch (NoSuchElementException e) {
257: //ok
258: }
259: }
260: }
261:
262: public void testRemoveFirst() throws Exception {
263: for (Deque deque : new Deque[] { new ArrayDeque(),
264: new LinkedList() }) {
265: Collections.addAll(deque, "a", "b", "c");
266: assertEquals("a", deque.removeFirst());
267: assertEquals("[b, c]", deque.toString());
268: deque.clear();
269: try {
270: deque.removeFirst();
271: fail();
272: } catch (NoSuchElementException e) {
273: //ok
274: }
275: }
276: }
277:
278: public void testRemoveLast() throws Exception {
279: for (Deque deque : new Deque[] { new ArrayDeque(),
280: new LinkedList() }) {
281: Collections.addAll(deque, "a", "b", "c");
282: assertEquals("c", deque.removeLast());
283: assertEquals("[a, b]", deque.toString());
284: deque.clear();
285: try {
286: deque.removeLast();
287: fail();
288: } catch (NoSuchElementException e) {
289: //ok
290: }
291: }
292: }
293:
294: public void testRemoveFirstOccurrence() throws Exception {
295: for (Deque deque : new Deque[] { new ArrayDeque(),
296: new LinkedList() }) {
297: Collections.addAll(deque, "a", "b", "c", "b", "d");
298: assertTrue(deque.removeFirstOccurrence("b"));
299: assertEquals(4, deque.size());
300: assertEquals("[a, c, b, d]", deque.toString());
301: assertFalse(deque.removeFirstOccurrence("x"));
302: }
303: }
304:
305: public void testRemoveLastOccurrence() throws Exception {
306: for (Deque deque : new Deque[] { new ArrayDeque(),
307: new LinkedList() }) {
308: Collections.addAll(deque, "a", "b", "c", "b", "d");
309: assertTrue(deque.removeLastOccurrence("b"));
310: assertEquals(4, deque.size());
311: assertEquals("[a, b, c, d]", deque.toString());
312: assertFalse(deque.removeLastOccurrence("x"));
313: }
314: }
315:
316: }
|