001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.reducedmodel;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: /**
042: * Tests the functionality of the ModelList list class.
043: * @version $Id: ModelListTest.java 4255 2007-08-28 19:17:37Z mgricken $
044: */
045: public final class ModelListTest extends DrJavaTestCase {
046: protected ModelList<Integer> fEmpty;
047: protected ModelList<Integer> fFull;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: fFull = new ModelList<Integer>();
052: fEmpty = new ModelList<Integer>();
053: }
054:
055: public void testInsert() {
056: ModelList<Integer>.Iterator itFull = fFull.getIterator();
057: ModelList<Integer>.Iterator itEmpty = fEmpty.getIterator();
058: assertTrue("#0.0", fEmpty.isEmpty());
059: assertTrue("#0.1", fFull.isEmpty());
060: assertEquals("#0.2", 0, fEmpty.length());
061: assertEquals("#0.3", 0, fFull.length());
062: assertTrue("#0.4", itEmpty.atStart());
063: assertTrue("#0.5", itFull.atStart());
064: itFull.insert(new Integer(5));
065: assertTrue("#1.0", !itFull.atStart());
066: assertEquals("#1.1", 1, fFull.length());
067: assertEquals("#1.2", new Integer(5), itFull.current());
068: assertTrue("#2.0", fEmpty.isEmpty());
069: assertTrue("#2.1", !fFull.isEmpty());
070: itFull.insert(new Integer(4));
071: assertEquals("#2.2", 2, fFull.length());
072: assertEquals("#2.3", new Integer(4), itFull.current());
073: assertTrue("#2.4", !fFull.isEmpty());
074: }
075:
076: public void testInsertFront() {
077: fFull.insertFront(new Integer(3));
078: fFull.insertFront(new Integer(2));
079: fFull.insertFront(new Integer(1));
080: fFull.insertFront(new Integer(0));
081: ModelList<Integer>.Iterator itFull = fFull.getIterator();
082: for (int i = 0; i < 4; i++) {
083: itFull.next();
084: assertEquals(new Integer(i), itFull.current());
085: }
086: }
087:
088: public void testRemove() {
089: ModelList<Integer>.Iterator itFull = fFull.getIterator();
090: ModelList<Integer>.Iterator itEmpty = fEmpty.getIterator();
091: assertTrue("#0.0", fEmpty.isEmpty());
092: assertEquals("#0.1", 0, fEmpty.length());
093: assertEquals("#0.2", 0, fFull.length());
094:
095: itFull.insert(new Integer(5));
096: assertTrue("#2.0", !fFull.isEmpty());
097: assertEquals("#2.1", 1, fFull.length());
098: itFull.remove();
099: assertTrue("#3.0", fFull.isEmpty());
100: assertEquals("#3.1", 0, fFull.length());
101: }
102:
103: public void testNext() {
104: ModelList<Integer>.Iterator itFull = fFull.getIterator();
105: ModelList<Integer>.Iterator itEmpty = fEmpty.getIterator();
106:
107: itFull.insert(new Integer(6));
108: itFull.insert(new Integer(5));
109: itFull.insert(new Integer(4));
110: //now at start of list, after head.
111: assertEquals("#1.0", new Integer(4), itFull.current());
112: itFull.next();
113: assertEquals("#1.1", new Integer(5), itFull.current());
114: itFull.next();
115: assertEquals("#1.2", new Integer(6), itFull.current());
116: itFull.next();
117: }
118:
119: public void testPrev() {
120: ModelList<Integer>.Iterator itFull = fFull.getIterator();
121: ModelList<Integer>.Iterator itEmpty = fEmpty.getIterator();
122:
123: itFull.insert(new Integer(6));
124: itFull.insert(new Integer(5));
125: itFull.insert(new Integer(4));
126: itFull.next();
127: itFull.next();
128: itFull.next();
129:
130: itFull.prev();
131: assertEquals("#1.1", new Integer(6), itFull.current());
132: itFull.prev();
133: assertEquals("#1.2", new Integer(5), itFull.current());
134: itFull.prev();
135: assertEquals("#1.3", new Integer(4), itFull.current());
136: itFull.prev();
137: }
138:
139: public void testCurrent() {
140: ModelList<Integer>.Iterator itFull = fFull.getIterator();
141: itFull.next();
142: }
143:
144: public void testPrevItem() {
145: ModelList<Integer>.Iterator itFull = fFull.getIterator();
146: itFull.insert(new Integer(0));
147: itFull.insert(new Integer(1));
148: itFull.next();
149: assertEquals("#0.0", new Integer(1), itFull.prevItem());
150: }
151:
152: public void testNextItem() {
153: ModelList<Integer>.Iterator itFull = fFull.getIterator();
154: itFull.insert(new Integer(0));
155: assertEquals("#0.2", new Integer(0), itFull.current());
156: itFull.insert(new Integer(1));
157: assertEquals("#0.1", new Integer(1), itFull.current());
158: assertEquals("#0.0", new Integer(0), itFull.nextItem());
159: }
160:
161: public void testCollapse() {
162: ModelList<Integer>.Iterator itFull = fFull.getIterator();
163: ModelList<Integer>.Iterator itEmpty = fEmpty.getIterator();
164: ModelList<Integer>.Iterator itEmpty2 = itEmpty.copy();
165: assertEquals("#0.0", 0, fEmpty.length());
166: assertEquals("#0.1", 0, itEmpty.pos());
167: itEmpty.collapse(itEmpty2);
168: assertEquals("#0.0", 0, fEmpty.length());
169: assertEquals("#0.2", 0, itFull.pos());
170:
171: itFull.insert(new Integer(6));
172: assertEquals("#0.3", 1, itFull.pos());
173: ModelList<Integer>.Iterator itFull2 = itFull.copy();
174: assertEquals("#0.4", 1, itFull2.pos());
175:
176: assertEquals("#1.0", 1, fFull.length());
177: itFull.collapse(itFull2);
178: assertEquals("#1.1", 1, fFull.length());
179: assertEquals("#1.2", 1, itFull2.pos());
180: assertEquals("#1.3", 1, itFull.pos());
181:
182: itFull.insert(new Integer(5));
183: assertEquals("#2.0", 2, fFull.length());
184: assertEquals("#2.2", 1, itFull.pos());
185: assertEquals("#2.3", 2, itFull2.pos());
186: itFull.collapse(itFull2);
187: assertEquals("#2.1", 2, fFull.length());
188:
189: //collapse to the right
190: itFull.insert(new Integer(4));
191: assertEquals("#3.0", 3, fFull.length());
192: assertEquals("#3.0b", new Integer(4), itFull.current());
193: assertEquals("#3.0a", new Integer(6), itFull2.current());
194: assertEquals("#3.0h", 3, itFull2.pos());
195: itFull.collapse(itFull2);
196: assertEquals("3.0d", new Integer(6), itFull2.current());
197: assertEquals("3.0e", 2, itFull2.pos());
198: assertEquals("3.0f", new Integer(4), itFull.current());
199: assertEquals("3.0g", 1, itFull.pos());
200: itFull.next();
201: assertEquals("#3.0c", new Integer(6), itFull.current());
202: assertEquals("#3.1", 2, fFull.length());
203: itFull.prev();
204: assertEquals("#4.0", new Integer(4), itFull.current());
205: assertEquals("#4.1", new Integer(6), itFull2.current());
206:
207: //collapse to the left
208: itFull.insert(new Integer(7));
209: assertEquals("#5.0a", 3, fFull.length());
210: assertEquals("#5.0b", new Integer(7), itFull.current());
211: assertEquals("#5.0c", new Integer(6), itFull2.current());
212: itFull2.collapse(itFull);
213: assertEquals("#5.1a", 2, fFull.length());
214: assertEquals("#5.1b", new Integer(7), itFull.current());
215: assertEquals("#5.1c", new Integer(6), itFull2.current());
216: assertEquals("#5.2a", new Integer(6), itFull.nextItem());
217: assertEquals("#5.2b", new Integer(7), itFull2.prevItem());
218: }
219:
220: public void testNotifyInsert() {
221: ModelList<Integer>.Iterator itFull2 = fFull.getIterator();
222:
223: itFull2.insert(new Integer(0));
224: ModelList<Integer>.Iterator itFull = itFull2.copy();
225: itFull2.insert(new Integer(1));
226: assertEquals(new Integer(0), itFull.current());
227: }
228:
229: public void testNotifyRemove() {
230: ModelList<Integer>.Iterator itFull = fFull.getIterator();
231: ModelList<Integer>.Iterator itFull2 = fFull.getIterator();
232:
233: itFull2.insert(new Integer(0));
234: itFull2.insert(new Integer(1));
235: itFull.next();
236: assertEquals("#0.1", new Integer(1), itFull.current());
237: itFull2.remove();
238: assertEquals("#0.0", new Integer(0), itFull.current());
239: }
240:
241: public void testNotifyCollapse() {
242: ModelList<Integer>.Iterator itFull = fFull.getIterator();
243: ModelList<Integer>.Iterator itFull2 = fFull.getIterator();
244: ModelList<Integer>.Iterator itFull3 = fFull.getIterator();
245:
246: itFull2.insert(new Integer(0));
247: itFull2.insert(new Integer(1));
248: itFull2.insert(new Integer(2));
249: itFull2.insert(new Integer(3));
250: itFull2.insert(new Integer(4));
251:
252: assertTrue("#0.0.0", itFull.atStart());
253: // we have (4 3 2 1 0), itFull2 points at 4, itFull and itFull3
254: // point at 0. We want to move itFull back to point at 2 to show
255: // notifyCollapse works
256:
257: for (int i = 0; i < 3; i++) {
258: itFull.next();
259: }
260: for (int j = 0; j < 5; j++) {
261: itFull3.next();
262: }
263: assertEquals("#0.0", new Integer(2), itFull.current());
264: assertEquals("#0.1", new Integer(0), itFull3.current());
265: itFull2.collapse(itFull3);
266:
267: assertEquals("#1.0", new Integer(4), itFull2.current());
268: assertEquals("#1.1", new Integer(0), itFull3.current());
269: assertEquals("#1.2", new Integer(0), itFull.current());
270: }
271:
272: public void testListenerCount() {
273: ModelList<Character> testList = new ModelList<Character>();
274:
275: assertEquals("No iterators", 0, testList.listenerCount());
276:
277: ModelList<Character>.Iterator iter1 = testList.getIterator();
278:
279: assertEquals("One iterator", 1, testList.listenerCount());
280:
281: ModelList<Character>.Iterator iter2 = testList.getIterator();
282:
283: assertEquals("Two iterators", 2, testList.listenerCount());
284:
285: iter1.dispose();
286: iter1 = null;
287:
288: assertEquals("Removed first iterator", 1, testList
289: .listenerCount());
290:
291: iter2.dispose();
292: iter2 = null;
293:
294: assertEquals("Removed second iterator", 0, testList
295: .listenerCount());
296: }
297: }
|